Simplifying Conditional Logic
3 tips to improve the conditional logic of your code.

Just a guy who loves to write code and watch anime.
Search for a command to run...
3 tips to improve the conditional logic of your code.

Just a guy who loves to write code and watch anime.
Agreed with everything, except:
hasMoreThanFiveArticles is overkill & strong coupling for the following reasons:
if the limit changes to 4, now you've got a function name to update.
You could argue that ha! I'm going to pass in the limit as a parameter:
hasMoreThanNArticles(user, 4)
but if you look back, you'll realize, you've been bikeshedding on this the whole time:
if (user.articles > 5)
that's just the cleanest way possible to tell & write if there are more than 5 things of something.
I agree in that context Ákos, the examples were there to demonstrate the techniques/approaches =D
Hence "pragmatism" was emphasized.
Tiger Abrodi I'm sitting on a pile of hard to understand code, let me know if you need a complicated example next time 😃
Qualities that make outstanding builders.

Bipedal vs quadrupedal: how many legs This is about the number of legs the creature walks on. Bipedal. Two legs. Humans, ostriches, T-rex, kangaroos, most fantasy humanoids. Quadrupedal. Four legs. Do

Intro You hear "lerp" and "smoothstep" everywhere in game dev. They sound like math jargon. They're not. Both are small tools that do the same job: smoothly move from one value to another. The problem

What is Kinematics Kinematics is the math field. It's the study of how things move without worrying about forces (which would be dynamics). FK and IK are the two branches: forward kinematics and inver

Intro Textures are usually the biggest cost in a 3D scene. Memory, bandwidth, and load time all get eaten by them. Resizing them is the obvious lever. There's more. This post is about the less obvious

In this article I will go over three ways you can better write your code's conditional logic, making it more readable, easier to understand, and work with.
In order to explicitly convey that a variable is a boolean variable, you can prefix it with either is, has, or should, whatever suits best with the business domain's language, they make the names of the boolean variables much clearer.
Example:

Another thing you can do is to extract boolean logic to variables and giving the variables meaningful names. This makes the code much easier to read and understand.

Now, take into account, this technique should be used with pragmatism as any other technique, sometimes it makes more sense and is more readable by inlining the boolean logic.
Take this as a tip, because from what I've seen, this technique can be used in many places and would've made the code more readable and easier to understand.
Instead of extracting the logic to variables, in some instances, it can make sense to extract them to functions, if you for example have to do a lot of logic or already want to cover edge cases.

There are ways we can improve the conditional logic of our code, making it more readable and easier to understand.
The last two techniques when it comes to extracting, should be used with pragmatism, not in every scenario do they fit, in some cases, it is actually more readable to inline the logic.