Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By KevinWorkman
  • 1 Post By Paul279

Thread: Newbie question: Which is the better code?

  1. #1
    Paul279 is offline Member
    Join Date
    Oct 2012
    Posts
    44
    Rep Power
    0

    Default Newbie question: Which is the better code?

    Hello,
    I have very often situations where I write:

    Java Code:
    int aValue;
    
    if(something == 1){
    aValue = 5;
    }else{
    aValue = 10
    }
    or is it better to write such a code:

    Java Code:
    int aValue = 5;
    
    if(something != 5){
    aValue = 10;
    }
    For me the code example 2 is better, than it is shorter, but is it important how this code is written for the performance?
    So thanks for advice

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

    Default Re: Newbie question: Which is the better code?

    This is all a matter of personal preference as well as context- different situations might call for different code. There isn't a single correct answer to this question. Use whichever makes the most sense to you.

    You could also use the ternary operator:

    Java Code:
    int aValue = (something == 1 ? 5 : 10);
    gimbal2 likes this.
    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
    Paul279 is offline Member
    Join Date
    Oct 2012
    Posts
    44
    Rep Power
    0

    Default Re: Newbie question: Which is the better code?

    thank you,
    and it is a difference in performance how to write such a code?

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,073
    Rep Power
    6

    Default Re: Newbie question: Which is the better code?

    Quote Originally Posted by Paul279 View Post

    For me the code example 2 is better, than it is shorter, but is it important how this code is written for the performance?
    What Kevin says. But two other things:

    - shorter code is not necessarily better, nor does it necessarily perform better
    - yes it is important to design code towards performance - the big wins you will fix at the architectural level, not at the level of the code statements. But it is NOT a good idea to do micro-optimizations; the JVM is far better at it than you are.

    You're using Java, let it work for you. Don't worry about this stuff and write code that you can read and understand. In that respect I agree with you, the second version is slightly better. In our opinion. I like Kev's addition even more.

    edit: I feel I should give examples of an optimizations that are worth it. Performance bottlenecks always come from the runtime environment. Things that can seriously slow down execution speed are accesses to the harddrive, network connections (such as over the internet), etc. So if you can prevent unnecessary access to those bottlenecks, that's a good optimization to do - IF the performance loss is actually an issue. It might also just work to put in a faster disc, or upgrade the network speed. Hardware is cheap.
    Last edited by gimbal2; 08-11-2014 at 03:09 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Paul279 is offline Member
    Join Date
    Oct 2012
    Posts
    44
    Rep Power
    0

    Default Re: Newbie question: Which is the better code?

    Thanks for your reply.

    Now i will write my code how it is more understandable.

    Wish you a nice evening
    gimbal2 likes this.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default Re: Newbie question: Which is the better code?

    If we're really talking micro optimization here: your second solution is (a bit) worse because if that test is true, you assign a value to your variable twice; otherwise it is assigned once; so on average it is assigned (1+2)/2 times versus (1+1)/2 times in your first solution. If you really want to write less source code, Kevin's suggestion (the ternary ?: operator) is fine.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,073
    Rep Power
    6

    Default Re: Newbie question: Which is the better code?

    Quote Originally Posted by JosAH View Post
    If we're really talking micro optimization here: your second solution is (a bit) worse because if that test is true, you assign a value to your variable twice; otherwise it is assigned once; so on average it is assigned (1+2)/2 times versus (1+1)/2 times in your first solution. If you really want to write less source code, Kevin's suggestion (the ternary ?: operator) is fine.

    kind regards,

    Jos
    Nice observation! But it still all depends on what byte code rolls out of it to truly know if this code leads to an actual micro-performance-penalty. Probably in this simplistic case it is exactly as described, but since I've taken to hang around in stackoverflow I've seen some things and stuff that make me question everything I assume about Java. This thread in particular made my jaw drop:

    java - Why is processing a sorted array faster than an unsorted array? - Stack Overflow
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default Re: Newbie question: Which is the better code?

    Quote Originally Posted by gimbal2 View Post
    Nice observation! But it still all depends on what byte code rolls out of it to truly know if this code leads to an actual micro-performance-penalty. Probably in this simplistic case it is exactly as described, but since I've taken to hang around in stackoverflow I've seen some things and stuff that make me question everything I assume about Java. This thread in particular made my jaw drop:

    java - Why is processing a sorted array faster than an unsorted array? - Stack Overflow
    An interesting thread indeed, but in the OPs second example it's not the branch prediction that can optimize the code, but a 'dead store' elimination that can change the code; the OPs example is basically this:

    Java Code:
    int x= v1;
    if (<C>)
       x= v2;
    If <C> doesn't refer to x, line #1 is a dead store operation and (if the compiler optimizes for speed) the code will be changed to:

    Java Code:
    int x;
    if (<C>)
       x= v2;
    else
       x= v1;
    ... which is basically what Kevin also suggested (and the OP showed as the first example).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,073
    Rep Power
    6

    Default Re: Newbie question: Which is the better code?

    Well I learned something new today. Thank you.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 3
    Last Post: 09-01-2012, 04:16 PM
  2. newbie question
    By palcente in forum New To Java
    Replies: 4
    Last Post: 02-01-2012, 10:41 PM
  3. Newbie question about basic code - "Hello World"
    By Kimomaru in forum New To Java
    Replies: 3
    Last Post: 10-13-2011, 06:56 PM
  4. Probably a newbie question.... :P
    By gblax123 in forum New To Java
    Replies: 9
    Last Post: 02-23-2011, 10:21 PM
  5. newbie question
    By ronguilmet in forum New To Java
    Replies: 2
    Last Post: 11-16-2009, 02:37 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
  •