Results 1 to 7 of 7

Thread: if/else

  1. #1
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default if/else

    Can this be further compressed?

    Java Code:
    if (condition1) {
    	if (condition2) {
    	   // do something
    	} else {
      	   // do something else
    	}
    } else {
    	if (condition 3) {
    	   // do yet another thing
    	} else {
    	   // do something original this time
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Java Code:
    if(condition1 && condition2) {
    	   // do something
    } else if (condition1) {
      	   // do something else
    } else if (condition3) {
    	   // do yet another thing
    } else {
    	   // do something original this time
    }
    but this is much uglier than what you have now

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by iluxa View Post
    ....

    but this is much uglier than what you have now
    Yep, and for my money, readability beats compressability any day!

  5. #5
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    Ok, thanks guys

    I was looking over some of my old code and just was wondering if it can be somehow compressed and be made more efficient

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by mac View Post
    Ok, thanks guys

    I was looking over some of my old code and just was wondering if it can be somehow compressed and be made more efficient
    Efficiency is a whole other question and often has answers that you don't expect. Little tweaks in code like this is very unlikely to increase efficiency to any noticeable extent, but if you write code that is too terse, you as the coder may lose valuable time when you have to review the code and perhaps modify it months later, and due to its terseness, have difficulty understanding what it's supposed to be doing.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Here's a very interesting article that is pertinent to this discussion: Write Dumb Code.

Posting Permissions

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