How to write if conditions?

Let's say I have a function/method which returns a Boolean value. First is the test conditions, and then some logic. How best to describe it?

Option 1:
if(foo == bar && foo >= 1) {
//Logic
 return true;
}

return false;

Option 2:
if(foo != bar) return false;
if(foo < 1) return false;
//Logic
return true;
July 8th 19 at 11:37
12 answers
July 8th 19 at 11:39
It is better to write so:
return (foo == bar && foo >= 1) ? true : false;
This option is not bad if you have the function in one line, but when the logic is a little blow-up but a headache I have no objections - Madonna_Co commented on July 8th 19 at 11:42
return foo == bar && foo >= 1 then too, Why else ternary operator? - orin.Runte commented on July 8th 19 at 11:45
it's better NOT to write.

return foo == bar && $foo >= 1;

Anyway possible ternary operators should be avoided. - Allene_Crona78 commented on July 8th 19 at 11:48
: why's that? - kevon_Torphy commented on July 8th 19 at 11:51
the result of the operation, and so a Boolean value. And the ternary operator degrades the code readability. You need to use it rarely. - Allene_Crona78 commented on July 8th 19 at 11:54
: breaking the code readability. - peggie_Aufderh commented on July 8th 19 at 11:57
July 8th 19 at 11:41
The second is better, because in the first { can { get { a billion { levels { nesting } } } } }
It is better to avoid too deep of attachments to the conditions. The processor is not approved. Better to make a competent algorithm. - Madonna_Co commented on July 8th 19 at 11:44
: Premature optimization is the root of all evil. The assumption is that the compiler translates the source code, worked 1995 when the compiler was brainless. Now this is not worth it. - orin.Runte commented on July 8th 19 at 11:47
but this is not premature optimization. Conditional transitions has always been a processor with a pipeline a difficult task, especially billion. And only on GPUs - and all critical. Therefore, such deep nesting is not good. - Allene_Crona78 commented on July 8th 19 at 11:50
: if you carefully read my response, I just suggest not to use a lot of levels of investment. - kevon_Torphy commented on July 8th 19 at 11:53
: in fact, normally, the compiler optimizes. - Allene_Crona78 commented on July 8th 19 at 11:56
: with deep branches, no the compiler can not optimize. The branching is exclusively consistent implementation of click-through locations, and then loading code into the cache memory. And the slowest processor in the transition and division. And we cannot blindly trust the compiler. At any moment he will give ambiguous code, where the processor can not digest. - peggie_Aufderh commented on July 8th 19 at 11:59
July 8th 19 at 11:43
1. I agree that you need to get out as quickly as possible. So better something like this:
if(foo != bar || foo < 1) {
 return false;
}
// logic
return true;


2. The division of the conditions must be meaningful from the standpoint of the business logic. If the condition foo != bar || foo < 1 is whole and indivisible from the point of view of business logic, it is better not to share.

If it's 2 different conditions (two different thing), just arranged in one condition, it is quite possible to separate them for logical allocation (again, in terms of business logic). For example, if foo - the number of items in the order, a bar - number of products which are given off (no more no less):

// number of items in your shopping cart != the count of product which is discount 
if(foo != bar){
 return false;
}

// if cart is empty
if(foo < 1){
 return false
}

// application of discounts to the order

return true;
July 8th 19 at 11:45
If we assume that both and plus a few more.
July 8th 19 at 11:47
Option 1 of course. Readability first and foremost. Performance in my opinion, too if the first check does not pass, the second will not be checked.
The difference in performance for this example. Conditions standard are evaluated left to right. If in a long conjunction is found false, the calculation ends and the sequel will not be processed. - Madonna_Co commented on July 8th 19 at 11:50
July 8th 19 at 11:49
if (!(foo == bar && foo >= 1)) return false;

the approach is called "early exit," and is it to improve readability.
First time ever can't read it. Sometimes, when conditions 2-3, the ranks of such designs, test them somewhere, and then wash and break into several checks. - Madonna_Co commented on July 8th 19 at 11:52
: well, you could write:

if (foo !== bar || foo < 1) return false; - orin.Runte commented on July 8th 19 at 11:55
July 8th 19 at 11:51
You must exit the function as early as possible. Next could be the creation of large and complex objects, and then booyah, and the argument is null, and you already nasozdavali just unnecessary.
July 8th 19 at 11:53
You can still using Short-circuit evaluation:
res = true
res &&= (foo == bar)
res &&= (foo != baz)
if (res) some_logic1
res &&= (foo >= 1)
if (res) some_logic2
return res

The advantage of code is that checks are to each other, linearly, not inclusion.
Unlike, say, this kind of code:
res = false
if (foo == bar && foo != baz) {
some_logic1
 if (foo >= 1) {
some_logic2;
 res = true
}
}
return res


Additionally,
there is a way of describing the business logic using the decision table, which has its origins somewhere around 50-60-ies of the last century.
> res &&= (foo == bar)
> res &&= (foo != baz)

Operator &&= no in the C/C++ and, I believe, in most other languages. - Madonna_Co commented on July 8th 19 at 11:56
in C the syntax is different, I took from the language ruby.
But the point is not in the syntax. We can write res = res && (foo == bar) - orin.Runte commented on July 8th 19 at 11:59
July 8th 19 at 11:55
Option 1, of course! Even in the second ; no
July 8th 19 at 11:57
Better not to write return condition is generally one return at the end of the function.
And the conditions to merge is if they are logically United by a common sense. T..e they potentially can be put in separate function.
Written just
July 8th 19 at 11:59
First, it is better to write "normal" passage conditions (i.e., when the algorithm is on the "expected/acceptable" scenario). And then make branches to "deviations from the norm".
Of course, this advice is not always applicable :)
July 8th 19 at 12:01
Better from whose point of view?
If man, then You there is already a lot has been written.
I've been programming controllers, there is a different approach and a different understanding of "better" is in questionable cases, write several options and look at the Assembly code. It was his (so-so it is possible to imagine several simplifying reality) and will perform the CPU.
Yeah, there's also the point of view of an optimizing compiler. His point of view just expressed in inline Assembly code.
As a rule, "boring and flat" better optimized - keep it simple, stupid. :)
And what you call "boring and flat"? Can example? - Madonna_Co commented on July 8th 19 at 12:04
"boring and flat"?
As an example (my personal perception) - split conditional statement with multiple conditions on multiple conditional statements with one condition. This technique makes the text of the code (in real life) are poorly perceived, similar in appearance to the restored code when reversengineering. But, when intelligently organized sequence of nesting, allows you to save machine cycles. Not the memory.
It could (and should?) to give an illustrative example of "bowing" under the compiler, but it is not quite the topic of discussion. - orin.Runte commented on July 8th 19 at 12:07
although....
everything is determined by the compiler
good (in my understanding) will perform a complex condition consistently (well, depending on the order of actions), i.e., he will break multiple conditions on a single chain.
not good - I'd consider it the result of ALL the conditions (i.e., available as we wrote)
but not good probably already left, but I have found :))) and used to "SAG" under the compiler - KISS-principle - Allene_Crona78 commented on July 8th 19 at 12:10

Find more questions by tags Programming