Differences of boolean return

Asked

Viewed 74 times

2

I came across the following example:

$umidade = 91;

$vai_chover = ($umidade > 90);

if ($vai_chover) { echo "vai chover"; }

To $vai_chover returns a boolean in if, but I found too much code to do too little, it wouldn’t be the same if it were written like this:

$umidade = 91;

if ($umidade > 89) { echo "vai chover"; }

Theoretically the if does the same test, which changes only that it is not stored in a variable.

In which cases we use one or the other?

2 answers

3


To understand better you must read What is a variable?.

Knowing what is a variable the question to be asked is whether you need to store its value somewhere. Why store the value in a variable and then use it only once?

Looks like none, right? I agree with that and generally I see no point in doing it that way.

But there is a "school" (in the sense of a group that follows certain ways of doing) that preaches the readability of the code above anything else. And they’re right. You only write the code once and read several, and it’s even common for people who haven’t read it several times. The better you document what you’re doing and make it obvious what that means, the better the code is.

One of the ways to do this is to put too much comment to say it’s happening. But comment violates the DRY and you end up having two pieces of information about the same thing, so every time you mess with something in the code you have to remember to mess with the comment, if you don’t, you have the worst of both worlds, a comment that says the code does something that it doesn’t do.

See more about this at:

And also Writing easy-to-maintain, readable code?.

So how to legibly document what that is and not look like a magic expression no comments?

Easy, create a code describing what that is. The simple way to do that is to create an identifier for that. This identifier can be a function or a variable.

If you do a function it can be used in several other points of the code and will be well DRY. If you choose the variable you can use again right there in that section, but not outside the current function, and it will be DRY if there is a repeat.

I answered this in Assign an expression to a variable and "if" elegant in PHP and also "== true" is useful for something? Better "!" or "== false"?.

But if I only use it once, wouldn’t that be redundant? No, it’s not, there’s a reason for it, it doesn’t change anything in execution, but it documents better canonically. It’s good, no more magic, the code is explaining that it’s going to rain, it’s not just an analysis of a number that doesn’t indicate what that is.

Then you must have picked up a code or explanation from someone who follows the school of good documentation, from the explicit rather than the implicit (which reminds me of Python who has this in his basic philosophy and almost nobody follows, especially the new users of this language who do not give a damn about programming).

Do you want to follow this school? Some do not want to. But they know why they do not want to. A judgment is a good decision. You know why you won’t follow it. Any decision without thinking it through is a bad decision.

Maybe the way is the middle, is to think well what you are doing and make more readable when it is necessary, when it is not quite obvious even without an identifier name.

You need to find your way. Or follow your team’s way. The most important thing is to think about all the code you create, and few people do that. Every code you enter must be justified, and if you fail to put something you could have written down you have to be justified.

I usually put that in my lectures:

Enquanto você não souber o que cada caractere do seu código faz, até mesmo o espaço em branco, você ainda não sabe programar

And people don’t understand that the code should reflect what’s set outside of it. It seems obvious, but it’s not. If it were, everyone would follow it very well. People tend to think they’re following, even if it’s not true.

So your problem needs a clear definition that humidity over 90 is when it rains? Or you just need to know that this amount of moisture should do something?

In this specific code, I would say that the variable is not necessary because the next action already indicates what that is. But that’s only because the code is artificial.

I often say that you can learn a mechanism with an artificial code, but only learn to program with real codes. This one doesn’t teach the right way to do it because it’s all redundant.

Yes, all of it, because neither the if need in this code. You already know the value before so there is no reason to compare something.

This code can demonstrate the use of a variable as a form of documentation, but it cannot teach when to use this technique, because this case is inadequate.

As an example of the mechanism it serves, I am not complaining about it, but it does not teach how it should be a real encoding, it just needs to be very clear, most do not have, they start copying without thinking.

In parallel, if you think that something redundant should not be used, then why did you use keys? They are redundant in this case. There are parentheses that are also in the first example.

There’s the other point I noticed in the question: the title speaks in return. Does this mean that the function still returns a value? This value is whether it rained or not? So the variable would be used again? It makes a lot of sense to exist. If it doesn’t return anything, why do you have it in the title? Is it a poorly defined title? If a problem is poorly defined the solution will always be wrong, even if it works.

This must be in a function, what is the name of the function? Does it not document well what it does? If her name is very descriptive and she is very simple, as it seems, then it may not need to describe further.

Fiat 147 todo detonado andando pelas ruas

Completion

It’s not much, it’s just an extra variable name. Documenting well is no small thing. If you think it doesn’t matter try to put all variables that are mandatory only with one letter, or use swear words instead of meaningful names (I’ve seen them do this).

In some languages creating a variable can have a performance cost in certain situations, so it is something to wonder if you need the maximum performance, which is not the case when opting for PHP.

You don’t have a single answer.

Learning technology matters little. Developing software is clinging to detail. It is a bit an art that common sense and good taste count, without leaving the accuracy that the solution requires. It is foundation. It is commitment. It’s questioning everything, so I also use it in my lectures:

Aquele que não questiona tende a aceitar e a se sujeitar à ideia alheia...

-3

There is no difference between the two examples other than the value you placed after the comparison operator. It’s like you said, the first example is too much code for too little.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.