Results 1 to 5 of 5
  1. #1
    gbritton is offline Member
    Join Date
    Jul 2013
    Posts
    11
    Rep Power
    0

    Default if() {} else{} vs. trinary expression -- style question

    Say I have something like this:

    Java Code:
    MyRefClass ref = new ... // instantiate some class
    MyClass foo;
    boolean foobar;
    
    if (foobar) {
        foo = ref.getNewFoo(MyRefClass.CONSTANT_ONE)
    } else {
        foo = ref.getNewFoo(MyRefClass.CONSTANT_TWO)
    }
    Where CONSTANT_ONE and CONSTANT_TWO are some constants.

    I suppose I could rewrite the if/else like this:

    Java Code:
    foo = ref.getNewFoo (
                  foobar ? MyRefClass.CONSTANT_ONE : MyRefClass.CONSTANT_TWO);
    What I'd like to know are the pros/cons of the two approaches. For me, there's one pro of the second approach: I only have "foo" and "MyRefClass" referenced one time each, possibly making maintenance easier. However, I want to hear from experienced devs on this so that I use best practices going forward.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: if() {} else{} vs. trinary expression -- style question

    I would say that the best practice is to not worry too much about micro-style questions like this. Use whichever one is more maintainable by you and/or your team.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: if() {} else{} vs. trinary expression -- style question

    And it's a ternary expression, not a trinary expression.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,704
    Rep Power
    5

    Default Re: if() {} else{} vs. trinary expression -- style question

    There are no best practices here, only personal opinion.

    My opinion: when there are only two possibilities the ternary operator is just fine and perfectly readable. If there are more options... then I'd go for if/else or a switch statement; whichever you prefer.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,360
    Blog Entries
    7
    Rep Power
    20

    Default Re: if() {} else{} vs. trinary expression -- style question

    Also note that the operands of the ternary expression must be expressions while the if-else statement uses statements as its 'operands'; it makes a hell of a difference to the compiler (common subexpression folding etc) while to the programmer the ternary expression is just a bit more limited than the if-else statement.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Style question on implementing interfaces
    By gbritton in forum New To Java
    Replies: 2
    Last Post: 08-02-2013, 05:14 PM
  2. Question about standard code style
    By lenois in forum New To Java
    Replies: 9
    Last Post: 02-08-2013, 03:56 PM
  3. regular expression question
    By stuckonjava in forum New To Java
    Replies: 2
    Last Post: 05-18-2012, 11:30 AM
  4. Replies: 2
    Last Post: 07-18-2011, 07:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •