Stupid question about how you format long statements

For long statements in which they occupy more than one line, you put conditions such as AND or OR on a new line as follows:

if (something && something else) 

Or like this:

  if (something && something else) 
+6
source share
5 answers

In difficult conditions, consider extracting it into a function or variable:

 if (complexCondition(foo)) { .. 

As a bonus, the name of a function or variable can be used to convey information about what the condition means. This makes it easier to read code.

+3
source

I usually do this in the second way, since I can build expressions. However, in any case, everything is fine when you write code, if you agree.

+2
source

I prefer the performance of the first. My reasoning is that it is easier to remove a condition with cut / paste / comment for any testing purpose. It is much easier to comment out a line than to remove both from a line above and comment out a line. This is more when I execute sentences in SQL than in an if statement in any other given language, but I get off.

+1
source

Given my storytellers, I would have avoided lengthy tests in the first place. I would rather do something like:

 bool fTest1 = A == B ; bool fTest2 = C ; bool fTest3 = f(1,2,3) ; bool fSuccess = ( fTest1 | ftest2 ) & fTest3 ; if ( fSuccess ) ... 

Otherwise, something like this:

 if ( A == B && ( C == D || E == F ) && Z > Y ) { ... } else { ... } 

YMMV, of course.

The first is much easier to debug, test, register, etc.

+1
source

I usually format using IDE formatting and then rebuild a bit to make it beautiful.

0
source

Source: https://habr.com/ru/post/889066/


All Articles