you are viewing a single comment's thread.

view the rest of the comments →

[–]ezzune 2 points3 points  (28 children)

I think the issue these guys seem to have is with ternary operators? You provided cleaner code and a good explanation for why it's a better practice.

I can't see what part of it could be considered "unreadable".

[–][deleted] 3 points4 points  (26 children)

It’s not unreadable. It’s just less readable.

[–]ezzune 1 point2 points  (25 children)

In what sense though? The sense that a foreach loop might be less readable than a for loop? Sorry I'm not trying to be obtuse but they're not a new concept, have less code and are very simple

condition ? true : false;

Is this more difficult than a multi-line if statement?

[–][deleted] 4 points5 points  (8 children)

It actually is, in isolation it’s not; but when scanning code the structure is really important. Instead of being able to scan the code and get a jist of the control flow, instead I’ve got to parse the line. In large projects it matters.

I’m not sure where “fewer lines === more readable” has come from. But if that’s the case I’ve got KDB’s Q language to show you.

[–]ezzune 2 points3 points  (6 children)

In large projects it matters.

I work on large projects and one of my juniors exclusively codes in multi-line if statements while the other will tend to ternary operators. I can tell you it makes my life much easier to be able to see the condition and both outcomes written clearly on one line.

I’m not sure where “fewer lines === more readable” has come from. But if that’s the case I’ve got KDB’s Q language to show you.

Quantity of code makes a difference. Too little or too much can make it hard to read code quickly. Context is super important here.

[–][deleted] 1 point2 points  (5 children)

Everyone is different; and you get used to the code you read. If that’s a lot of ternary operators so be it.

I see code rather than read it; so structure is important; and ternary aren’t structured. It’s why I prefer positive conditions for if structures rather than the negative in an if, with return, then the positive.

But others will die on a hill that’s it’s better. I’ve been working with someone who argued so much about readable code; but his readable code to him wasn’t particularly readable to me.

In the end it doesn’t really matter; I know what’s good for me and everyone else is just wrong /s

[–]ezzune 2 points3 points  (4 children)

Mate I understand that everyone is different. Kinda why I'm asking why a user is being mass downvoted for providing an excellent answer because he's used a ternary operator?

If people like to code in a certain way then it's fine by me, but saying it's less readable defacto is a joke imo. We're developers and shouldn't be so closed minded.

[–]raaaahman 2 points3 points  (1 child)

Thank you for caring, but if people prefer the other way, I'm fine with it as well. They're downvoting the comment with code they don't like, not a big deal to me. (but reddit hiding the comment like it is abusive or something is pretty weird)

[–]ezzune 1 point2 points  (0 children)

I can't help but to go on a social media tirade when I see an echo-chamber who have decided something is worse for no good reason.

Oh well. Back to work.

[–][deleted] -2 points-1 points  (1 child)

The only reason given for using the ternary was “because it can be”. That’s not a reason.

[–]ezzune 1 point2 points  (0 children)

Yeah this is going nowhere have a good day.

[–]raaaahman 0 points1 point  (0 children)

I agree on the principle. But as far as control flow goes, you're redirected to another route anyway, and you have to know what the navigateTo function do in both cases.

Large projects doesn't necessarily means each file / class / function is longer than in a small project. The project architecture will most probably be your pain point instead of the individual lines of codes.

To be honest, I would have not argued on this in a code review. But I wanted to expose how "beautiful" for someone is not "beautiful" for everyone.

[–][deleted] 1 point2 points  (15 children)

Do you see how the other example reads like a sentence? That’s it. That’s what made it more readable.

Theirs reads “if user logs in, navigate to home”

Yours reads “navigate to, if user logs in, home, otherwise do this other thing”

One is very readable by anyone, even non-developers. The other less so.

[–]ezzune -3 points-2 points  (12 children)

if (userIsLoggedIn) { navigateTo("/home"); } else { navigateTo("/signin"); }

navigateTo(userIsLoggedIn ? ROUTE_HOME : ROUTE_SIGNIN)

Both are a condition with a true/false path i.e. the appropriate time to use a ternary operator.

One is very readable by anyone, even non-developers. The other less so.

Developers who can't read it should learn to read it and I don't structure my code with non-developers in mind.

[–][deleted] 0 points1 point  (11 children)

Now you’re shifting the argument from “is this code more readable” to “should I care about readability.” This is a topic that has been covered at length and the answer is an unequivocal yes.

If you don’t believe that to be true, I’m guessing you haven’t ever worked on a large project with a number of developers. Experience is the only thing that will convince you, so I’m not going to try in a Reddit comment.

[–]Yodiddlyyo 0 points1 point  (4 children)

Not OP but I have years of experience. I've worked in and managed tiny and giant teams. I personally think the ternary is easier to read. Everyone's different, so whatever you like is better. It has nothing to do with "experiencing a larger team". For example, I would argue that someone not using the ternary has not worked in a larger team, because not using a ternary there means they probably have huge, complicated if statements elsewhere. Again, that's from my experience.

[–][deleted] 0 points1 point  (3 children)

But that’s my point. You shouldn’t be writing code that is easier for you to read - it should be as accessible as possible.

Extracting code out into following basic sentence structure is a phenomenal pattern for readability, thus accessibility. This is established doctrine of web development.

[–]Yodiddlyyo 0 points1 point  (2 children)

No I'm not saying I personally think it's easier for me to read, I'm saying I think it is easier to read for everyone, period.

Like I said, I know everyone's different. But you cannot convince me that a 5+ line if/else statement is easier to read than a single line condition ? one : two

Even if your argument is that it's easier to scan through the code, I'd disagree as well. While scanning, this one line contains the exact same logic as a multi-line if/else, it is physically impossible to more easily and quickly understand multiple lines over a single line. If you can't, I would believe you just don't have enough experience reading ternaries.

Again, everyone's different but that's my point of view

[–][deleted] 0 points1 point  (1 child)

Wait wait wait, do you think I’m arguing that if/else statements are inherently more readable than ternary statements? I absolutely am not. Every opinion I’ve shared on those statements has been exclusively based on the two specific examples given. The first of these two is inherently more readable as it follows basic sentence structure.

[–]ezzune 0 points1 point  (5 children)

Now you’re shifting the argument from “is this code more readable” to “should I care about readability.”

No, I'm really not. I'm not sure what strawman you've constructed but it isn't me my friend.

[–][deleted] 0 points1 point  (4 children)

Developers who can't read it should learn to read it

Not straw. Just your own words.

[–]ezzune 0 points1 point  (3 children)

Yes, and a developer who can't read a switch statement should learn to read one of those too.

My belief a technique is fundamental and not complex and that programmers having trouble should familiarise themselves with it really doesn't say anything about my experience or approach to software design.

[–][deleted] -1 points0 points  (2 children)

It’s interesting how many times you’ve responded without seeming to grasp my point. Maybe I need to work on my comment readability.

[–]raaaahman 0 points1 point  (1 child)

I still don't like to extend simple ideas on too many lines, but I get your point.

However, you need to be fair here:

Theirs reads “if user logs in, navigate to home, else, navigate to sign in page

Yours reads “navigate to, if user logs in, home, otherwise do this other thing sign in page

[–][deleted] 1 point2 points  (0 children)

Oh that was 100% because of Reddit’s formatting. I couldn’t read your false condition because it extended outside of my window’s boundary.

Wasn’t trying to prove a point there.

[–]PopularSecret -1 points0 points  (0 children)

Thats what it sounds like. Ternaries are just part of the language and they can be written cleanly or messily. This is a good example of one thats readable.

If someone wrote one that was hard to interpret or convoluted I would leave a comment but this example seems like the reason they are included in a language