Have you ever spent more than a few seconds staring at the same line of code? How often did you find conditionals that were tricky to parse? In thi...
For further actions, you may consider blocking this person and/or reporting abuse
You are right. ヽ(・∀・)ノ
But in general it's better to write this instead:
( ̄ω ̄)
Ah yes... a glorious piece of code right there. 🤣
Ye kya harkat hai
Affirmative is a good rule of thumb, but I think in some cases, such as HTML's
disabled
, it makes more sense in the negative, as the usual assumption is that an element is enabled. Which is clearer in intent?BTW here's a handy higher-order function when you want to convert a boolean-returning function into its opposite:
Usage:
Maybe it's time for js to add the "unless" statement that Perl (and Ruby) has? We use a lot of Perl and usually our junior developers come from a javascript background, and the unless statement is one of the things they instantly like.
Or if you don't need to continue with else statements, you can do the postfix form which developers coming from other languages also seem to quickly find appealing:
Imagine the above without Perl's $sigils of course, but you can see how it allows an extra level of "positivity".
I must admit that I was initially skeptical (to say the least) when I saw the syntax, but that neat one-liner piqued my interest! Personally, that's gonna take some getting-used-to on my end, but I can definitely see why one would "instantly like" such a feature.
And I am with you about early returns. I like to avoid them, except if that will cause a lot of indentation and general ugliness, but I don't think twice about adding them when it comes to this pattern:
Feels natural. I don't use much javascript, but I see how it evolves nicely, so maybe something like that would not be a stretch to see added ;)
Some JS programmers make extensive use of the ternary operator istead of conditionals to make their code "more compact". Carefully used, this can lead to readable code:
If you misuse the ternray operator as a short form of a conditional, this may make your code virtually unreadable:
Here is a real life example:
Totally agree with using the affirmative style for writing conditionals - the logical flow makes much more sense and is very intuitive to follow. Curious to know your thoughts on using the ternary operator in Python or JavaScript?
I don't mind them at all as long as they don't go two levels deep and beyond. 😅
Of course, the affirmative style must still follow from that.
I think ternary statements are useful for assigning
const
values where there is some conditional logic to it, like in your example above. I personally find it even easier to read if the two values are put on different lines (but I realise this will be subjective to styling preferences).There's only one reason I'll used nested ternaries: when writing variables in a dynamic HTML template and a return value must be computed in a single statement. Of course the workaround there is to compute it above the template where possible and then use it later.
I feel like the example with
Not disallowed throw err
Else success path
Is a common way to structure your code to validate and clean up the success path conditions. Context is important though, I think overall it's more important to discuss the pattern with your team and try to come up with some pattern with justification. Having to negate things is not necessarily a good idea sometimes, but could make sense given the limitations of language, other context around the requirements, or existing patterns in the system.
Interesting topic and well explained! 👍🏻
I like how you making this series 👏
You can also include don't about commit message as well in this series :)
Thanks! I will certainly put this in my to-do list. 🙇♂️
It's wild how many of these bad conditionals are in production code at top tier startups
Readers of this article might be interested in my post on De Morgan's Law.
dev.to/tracygjg/de-morgans-law-a-l...
agree! but it takes a lot of time and requires coding experience. happened to me :sad