Page 1 of 1

nested conditionals - your stance on them?

Posted: Fri Aug 02, 2013 1:23 pm
by jurchiks
Since nobody wants to argue on the contribution threads, I'm making this discussion here.
There are lots of people supporting the notion that nested conditionals are bad for readability and thus should be avoided, like, for example, here:
http://www.dreamincode.net/forums/topic ... operators/
http://codereview.stackexchange.com/que ... this-abuse

Example code of a nested conditional:

Code: Select all

tmp = (someLongCondition1 ? (someLongCondition2 ? method1returnValue : method2returnValue) : method3returnValue);
Of course, the problem is lessened if the conditional is broken into multiple lines, like so:

Code: Select all

tmp = (someLongCondition1    ? (someLongCondition2        ? method1returnValue        : method2returnValue)    : method3returnValue);
but for l2j that would require putting "formatter:off/on" flags around it, which, obviously, nobody is going to do.
Compare that to the following:

Code: Select all

if (!someLongCondition1){    tmp = method3returnValue;}else if (someLongCondition2){    tmp = method1returnValue;}else{    tmp = method2returnValue;}
What is your stance on nested conditionals (taking into consideration real-world examples where variables and values are longer than 1 character) - are they fine or are they bad? Would you avoid them? Why?

Reasonable explanations please, "don't care" does not count.

P.S. I would like to ask mods not to lock this topic just because you don't care about the subject. This is the off-topic section after all.
Also, would be nice if this thread could be turned into a poll.

Re: nested conditionals - your stance on them?

Posted: Fri Aug 02, 2013 1:33 pm
by xban1x
You are making this cause of 20-30 quests in which is used same nested conditional... and it usually looks almost identical in all of those quests.

None the less i will give a opinion on this, without being stupid.

In those quest, i believe it's totally ok to use it, because it's something that repeats itself in other quests and you memorize it and know what it is.

If this would be used only at 1 place and would not be something that repeats itself and is nested more then 2 times( I believe 2 is still ok because after conditions the results go "true : false : false"). If its more then 2 conditions it does truly become unreadable for any normal human being.

So that puts me kind of in middle of yes and no for nested conditionals.

Thats, my 2 cents

P.S. I cannot turn it into a pool.

Re: nested conditionals - your stance on them?

Posted: Fri Aug 02, 2013 1:40 pm
by jurchiks
I believe there weren't any quests with nested conditionals before you started writing them like that... But if you can prove me wrong, then it's a plus for you.

Re: nested conditionals - your stance on them?

Posted: Fri Aug 02, 2013 2:05 pm
by UnAfraid
I don't like long nested conditionals i'd reworked some to few lines as u may have seen in collision getters for example.

Re: nested conditionals - your stance on them?

Posted: Fri Aug 02, 2013 2:16 pm
by jurchiks
Yeah, I noticed. Props for that :)

Re: nested conditionals - your stance on them?

Posted: Fri Aug 02, 2013 2:19 pm
by Zoey76
They are called nested ternary operators in text books.

Anyway, I'm fine with them if they are not over 3 levels and they won't contain any logic.
Examples of discouraged use would look like:

Code: Select all

Object val = bool1 ? obj1 : bool2 ? obj2 : bool3 : obj3 : ...;Object val = obj0 != null && obj.processData() != null ? obj1 : obj2.getValue() < obj3.getValue() ? obj2 : obj3;
Same as Stackoverflow and other sites, this question is seen as not constructive to me.

It'll depend on the developer(s) writing the code, not much the rest of us can do.

Re: nested conditionals - your stance on them?

Posted: Fri Aug 02, 2013 5:49 pm
by UnAfraid
Well i don't like to scroll left-right (Because i have no scroll bar on my mouse :D)
So as long as they fit on my window its good for meh.

Re: nested conditionals - your stance on them?

Posted: Fri Aug 02, 2013 5:56 pm
by Okari
I think the gain in number of lines is clearly not worth the impact on readability.
I'm all for using the ternary conditional operator in simple if-else situations. But nesting them just makes it harder to read, where an if-elseif-else block would immediately be easy to understand.

Re: nested conditionals - your stance on them?

Posted: Fri Aug 02, 2013 6:00 pm
by jurchiks
Well, like I've explained to xban1x privately, my standard for line length in general is "does it fit in 1 line in pastebin/github/my screen?"
To be perfectly honest, I'm one of those who very much like the 80 character line width standard; I've written a lot of code that adheres to that rule and it really looks good and is very readable. For some users that limit is a wee bit higher - 120 characters being another popular width - but the conditionals that have been used in some of the quests committed lately are way longer than that; some even go over 300 characters. That does not even fit in a 1080p screen if the editor is in full-screen mode. As far as I counted on my work PC's screen, 1080p resolution @ normal font size fits about 265 characters when the editor is from one side of the screen to another (Checked in Notepad, which has the narrowest borders). And very few people write code in a layout like that, not with IDEs.

Re: nested conditionals - your stance on them?

Posted: Sat Aug 03, 2013 8:28 am
by Tryskell
I think it's a matter of developer's preferences. I personally don't use

Code: Select all

(check) ? result1: result2
if there are more than 2 of them imbricated. That's the limit where code esthetic doesn't worth the time spent to try to understand the nested condition.

There is really few NEEDED nested conditions on L2 world. If you got some, then you messed somewhere, or probably could recode it better.