I’ve recently read an article that confirms my way of writing script in following a “Happy Path”. I prefer having a function that starts by setting a single return value and structure the “if statements” to either change the preset return statement or “fall-through”. I find it so much easier than trying to negate positives especially when trying to follow the logic of my own scripts, never mind other programmers logic flow.
Could you please post a link to that article?
I also would like to see the reference. “Negating a positive” as you say which was demonstrated by @SamA74 is known as a “Guard Clause”. In short, the point being is to “fail fast”, i.e fail sooner than later.
Here we go:
Inverting the
if
condition and returning early
That is exactly what I said, fail fast.
Inverting the condition
This is exactly “Negating a positive”.
Your post now makes no sense.
The article did not start well:
I’ve been a professional programmer for the last several years.
Why label yourself as a newbie? I personally have been programming longer than the parents of many current developers have been alive. In fact, I worked with one developer whose grandparents were born after I started getting paid. But you don’t hear me bragging about it. Especially when the author then had to defensively assert that they are some sort of ‘Lead Engineer’ responsible for a vast suite of international projects.
The article then goes further downhill by not only using screenshots for code examples but slanted screenshots at that. Even tilting my head did not make them more readable. Was not even sure if the pretty pictures were meant to go with the text. Maybe they were some sort of ineffective advertisement?
But then they got to the meat of the article which can be summed up as “avoid using else statements”. Fair enough. I very seldom use them myself.
However, the best part is the comment section. Click on the link, skip the article and just read the comments. Religious fever at it’s best.
I checked on “negating” and it looks as though us Brits’ use the word to make something negative similar to “inverting the condition”. Across the pond “negate” appears to be to convert something to a empty or null.
I did not follow everything the article recommended and only selected the bits that I liked.
As mentioned I prefer to simplify script and endeavour to make it readable at a glance. I find that reading an “inverted condition” takes extra time and especially when an else follows the if statement
// not easy for me to comprehend
if ( !$conditionA || !$conditionB ) {
// do this
}else{
// do this
}
// easier for me
if ( $conditionA && $conditionB ) {
// do this
}else{
// do this
}
I agree about the comments and quite a few interesting points raised.
I think both the article and comments are only suggestions and a personal choice as to how one should code.
In straight procedural I would agree and have said before, “code to truth” which is what you have demonstrated in the second example. In a function/method, if you are elsing there is room for improvement.
Take note, all the examples in your link without else are all functions that “return”.
I will never let myself consider myself an expert. It prevents me from stopping learning. As soon as you stop learning, you’re falling behind. I would consider myself to have been a professional programmer for several years.
Both forms are used, though the empty-ness form is usually in the form of negating an event, or taking away some preexisting value - the referee negates the goal, on account of a foul up the field.
Which, in truth, is what the article is actually doing - it’s a shorthand (or lazy, depending on how you wish to see it) form of writing an else - “If X, abort, else continue”.
Don’t do this. Never do this.
Either read something or don’t read something, but don’t read parts and then agree with it. Chances are great you’ll miss the essence of what actually being conveyed and thus completely miss the point.
Not saying it happened here, but in general this is not how one should approach information.
To give yourself an out in case the article is received badly - “Well I did say I was new at this, so…”
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.