S
S
Shrei2016-06-28 00:01:04
Programming
Shrei, 2016-06-28 00:01:04

What is the best way to write if conditions?

Let's say I have a function/method that returns a boolean value. First there is a condition check, and then some logic. How best to describe?
Option 1:

if(foo == bar && foo >= 1) {
  //Логика
  return true;
}

return false;

Option 2:
if(foo != bar) return false;
if(foo < 1) return false;
//Логика
return true;

Answer the question

In order to leave comments, you need to log in

12 answer(s)
S
Sanan Yuzb, 2016-06-28
@Sanan07

It's better to write like this:
return (foo == bar && foo >= 1) ? true : false;

A
Armenian Radio, 2016-06-28
@gbg

The second one is better, because in the first { one can { get { a billion { levels of { nesting } } } } }

M
mitaichik, 2016-06-28
@mitaichik

1. I agree that you need to leave as quickly as possible. So something like this is better:

if(foo != bar || foo < 1) {
  return false;
}
// логика
return true;

2. The division of conditions should be meaningful from the point of view of business logic. If the condition foo != bar || foo < 1 is integral and indivisible from the point of view of business logic, it is better not to divide it.
If these are 2 different conditions (two different situations), simply arranged into one condition, then it is quite acceptable to separate them for logical selection (again, from the point of view of business logic). For example, if foo is the number of products in the order, and bar is the number of products for which a discount is given (no more, no less):
// кол-во товара в корзине != кол-ву товара при котором делается скидка 
if(foo != bar){
    return false;
}

// если корзина вообще пуста
if(foo < 1){
  return false
}

// применение скидки к заказу

return true;

R
Rou1997, 2016-06-28
@Rou1997

If we assume, then both, and plus a few more.

S
Sergey, 2016-06-28
Protko @Fesor

if (!(foo == bar && foo >= 1)) return false;
The approach is called "early exit" and is intended to improve readability.

M
mamkaololosha, 2016-06-28
@mamkaololosha

You must end the function as soon as possible. Next, there may be the creation of large and complex objects, and then a crap and a null parameter, and you have already created everything unnecessary.

R
Roman Mirilaczvili, 2016-06-28
@2ord

You can also use 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 the code is that the checks are one after the other, linearly, not nested.
Unlike, say, this code:
res = false
if (foo == bar && foo != baz) {
  some_logic1
  if (foo >= 1) {
    some_logic2;
    res = true
  }
}
return res

there is a way to describe business logic using a decision table , which has its roots somewhere around the 50s and 60s of the last century.

S
SolidMinus, 2016-06-28
@SolidMinus

Option 1, of course! In the second even ; No

V
Vasily Melnikov, 2016-07-07
@BacCM

It's better not to write return in the condition at all, one return at the end of the function.
And the conditions should be combined if they are logically united by a common meaning. Those. they can potentially be moved to a separate function.
Just written

A
Alexander, 2016-07-07
@AlexRaptor

First, it is better to write the "normal" passage of the condition (that is, when the algorithm goes according to the "expected/acceptable" scenario). And then to make branches on "deviations from the norm."
Of course, this advice is not always applicable :)

O
oleg_1956, 2016-07-07
@oleg_1956

Better from whose point of view?
If a person, then you have already written a lot here.
I program controllers, there is a different approach and a different understanding of "better" - in doubtful cases, I write several options and look at the assembler code. It is him (as one can imagine, somewhat simplifying reality) that the CPU will perform.
Yes, there is also the point of view of the optimizing compiler. His point of view is precisely expressed in assembly code.
As a rule, "boring and flat" is better optimized - keep it simple, stupid. :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question