nested conditionals - your stance on them?

If something doesn't fit in any other forum then post it here.
Forum rules
READ NOW: L2j Forums Rules of Conduct
Post Reply

What do you think about nested ternary operators?

They are good.
0
No votes
They are bad.
3
33%
They need to be used wisely.
6
67%
 
Total votes: 9

User avatar
jurchiks
Posts: 6769
Joined: Sat Sep 19, 2009 4:16 pm
Location: Eastern Europe

nested conditionals - your stance on them?

Post 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.
Last edited by Zoey76 on Fri Aug 02, 2013 3:35 pm, edited 3 times in total.
Reason: Added poll.
If you have problems, FIRST TRY SOLVING THEM YOURSELF, and if you get errors, TRY TO ANALYZE THEM, and ONLY if you can't help it, THEN ask here.
Otherwise you will never learn anything if all you do is copy-paste!
Discussion breeds innovation.
xban1x
L2j Veteran
L2j Veteran
Posts: 1228
Joined: Thu Jan 17, 2013 9:46 am

Re: nested conditionals - your stance on them?

Post 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.
User avatar
jurchiks
Posts: 6769
Joined: Sat Sep 19, 2009 4:16 pm
Location: Eastern Europe

Re: nested conditionals - your stance on them?

Post 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.
If you have problems, FIRST TRY SOLVING THEM YOURSELF, and if you get errors, TRY TO ANALYZE THEM, and ONLY if you can't help it, THEN ask here.
Otherwise you will never learn anything if all you do is copy-paste!
Discussion breeds innovation.
User avatar
UnAfraid
L2j Veteran
L2j Veteran
Posts: 4199
Joined: Mon Jul 23, 2007 4:25 pm
Location: Bulgaria
Contact:

Re: nested conditionals - your stance on them?

Post 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.
Image
User avatar
jurchiks
Posts: 6769
Joined: Sat Sep 19, 2009 4:16 pm
Location: Eastern Europe

Re: nested conditionals - your stance on them?

Post by jurchiks »

Yeah, I noticed. Props for that :)
If you have problems, FIRST TRY SOLVING THEM YOURSELF, and if you get errors, TRY TO ANALYZE THEM, and ONLY if you can't help it, THEN ask here.
Otherwise you will never learn anything if all you do is copy-paste!
Discussion breeds innovation.
User avatar
Zoey76
L2j Inner Circle
L2j Inner Circle
Posts: 7005
Joined: Tue Aug 11, 2009 3:36 am

Re: nested conditionals - your stance on them?

Post 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.
Powered by Eclipse 4.30 🌌 | Eclipse Temurin 21 ☕ | MariaDB 11.2.2 🗃️ | L2J Server 2.6.3.0 - High Five 🚀

🔗 Join our Discord! 🎮💬
User avatar
UnAfraid
L2j Veteran
L2j Veteran
Posts: 4199
Joined: Mon Jul 23, 2007 4:25 pm
Location: Bulgaria
Contact:

Re: nested conditionals - your stance on them?

Post 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.
Image
Okari
Posts: 56
Joined: Mon Feb 08, 2010 1:56 pm

Re: nested conditionals - your stance on them?

Post 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.
User avatar
jurchiks
Posts: 6769
Joined: Sat Sep 19, 2009 4:16 pm
Location: Eastern Europe

Re: nested conditionals - your stance on them?

Post 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.
If you have problems, FIRST TRY SOLVING THEM YOURSELF, and if you get errors, TRY TO ANALYZE THEM, and ONLY if you can't help it, THEN ask here.
Otherwise you will never learn anything if all you do is copy-paste!
Discussion breeds innovation.
User avatar
Tryskell
Posts: 256
Joined: Wed Nov 25, 2009 5:57 pm
Location: France :)

Re: nested conditionals - your stance on them?

Post 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.
Post Reply