How to write true if ()

I always write if such statements look like this:

if (...) {
    /* do something */
}

When I create threads in stackoverflow, sometimes people change this code to:

if (...)
{
    /* do something */
}

So, the first {goes to a new line.

Does it make sense what the true way is?

+3
source share
6 answers

I would suggest reading Code Complete . He understands this issue in quite some detail (devoting him almost an entire chapter) and gives what I consider to be a convincing argument for No. 1.

He actually says not to use the second explicitly, as he did not pass the black box test:

if (blah) 
{
    //Foo
}

When a black box turns into:

XXXXXXXXX
X
    XXX
X

, , ( ).

( ), :

if (blah)
    {
    //Foo
}

:

XXXXXXXXXX
    X
    XXXXXXX
X

: , ... , :

if (blah) 
    {
    //Foo
    }

. , , . , . . , ...

+7

PHP, , ( , , , ). PHP.

+4

, . . - " PHP".

+2

. "" . , , , .

+2

. , Zend Framework . - , . , Zend Framework, , . , , , / .

+2

, . , . .

+1

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


All Articles