S
S
Sergey Koksharov2012-08-07 10:00:11
Programming
Sergey Koksharov, 2012-08-07 10:00:11

On programming style: Should you wrap the parenthesis?

We are coming to a common programming standard in the company. Many things have been discussed, it remains to solve the issue with the curly brace :)

Some people are used to putting the brace on the same line with the function identifier, others transfer it to a new line. The advantages of writing in one line are only that a line of code is saved, the advantages of transferring are seen in a clearer traceability of the logical block of code and, accordingly, a quick search for the necessary elements and editing.

I would like to discuss with you, which is more correct? At the same time, the goal is not just to develop a program, but to make it easy to understand in the future (both for the developer himself and for a third-party), expand, refactor, etc.

How do you act in your company when you work in a team?

Below are two variants of the same function as an illustrative example.

<?php
// вариант 1
function f1(a, b) {
  if (a == 0) {
    a *= f12() + f15(a);
    b = f2(1, b);
    if (b > 15) {
      f4(a);
      f5(b)
    }
    if (a > 17) 
      f86(a);
    return f6(a, b);
  }
  return f7(a, b);
}

// Вариант 2
function f1(a, b) 
{
  if (a == 0) 
  {
    a *= f12() + f15(a);
    b = f2(1, b);

    if (b > 15) 
    {
      f4(a);
      f5(b)
    }

    if (a > 17) 
      f86(a);

    return f6(a, b);
  }

  return f7(a, b);
}
?>

Answer the question

In order to leave comments, you need to log in

19 answer(s)
C
cypok, 2012-08-07
@cypok

There is no “correct” option, there is one adopted within the framework of this group of developers.

T
TheHorse, 2012-08-07
@TheHorse

Until recently, where I work, it depended on the language and even the framework (if any). The day before yesterday, I just got tired of seeing 2 different options within the same screen, and now we have the transfer option everywhere.
The wrapping option was chosen because visual structure and readability are more important than the number of lines of code.
PS For the option without wrapping in pascal-like languages, you just want to kill:

var i : integer;
function rock(hard: boolean); begin
   for i := 1 to 10 do begin
       //do something
   end;
end;

S
Stdit, 2012-08-07
@Stdit

Pay attention to code standards PSR-2 , Zend Framework , PEAR .

N
NotDefined, 2012-08-07
@NotDefined

There is one good argument in favor of not wrapping:
return {
val: 'text'
} - works
return
{
val: 'text'
} - no

V
Vladimir Golovanov, 2012-08-07
@Colwin

Practice shows that the following method works well (not a panacea!):
- blocks if, for, etc. are highlighted with empty lines before and after the block
- brackets are always put, even if the body is one operator
- the opening bracket is not transferred to a new line
- if the description of the operator is spread over several lines - simplify to fit on one line, by any kind of refactoring
example:

function f1(a, b) {
    if (a == 0) {
        a *= f12() + f15(a);
        b = f2(1, b);

        if (b > 15) {
            f4(a);
            f5(b)
        }

        if (a > 17) {
            f86(a);
        }

        return f6(a, b);
    }

    return f7(a, b);
}

A
Anatoly, 2012-08-07
@taliban

There is no correct option, there are such options:
1. There are already a bunch of ready-made styles, take read the recommendations for your language / framework, use it
2. Throw a coin
3. Vote and choose by majority
4. Let the most experienced of you choose

S
sdevalex, 2012-08-07
@sdevalex

It depends on the language for me:
- it does not transfer to JS due to the large number of callback functions. If you transfer, then readability drops
- in Ruby, skims are used only for very short blocks in one line
- in the rest, transfer to clearly see the boundaries that highlight the brackets.

D
debugger88, 2012-08-07
@debugger88

Java, not portable.

I
iwitaly, 2012-08-07
@iwitaly

Everyone has their own style of writing, for whom it is more convenient and as is customary in the team. I always move the bracket to a separate line, for me the code becomes much more readable and convenient.

A
Alexey Prokhorov, 2012-08-07
@megahertz

I transfer when declaring a class / method / function, I do not transfer in other cases. I stopped at this due to the fact that the frameworks I use most (Yii, ZF) use this approach.

A
AR1ES, 2012-08-07
@AR1ES

As they said, there are no right options, but ...
McConnell gave a good explanation, whose opinion, by the way, I adhere to.
A well-designed language has an explicit block structure that results in a natural style of indentation. For example VB:
If a= b Then
statement1;

End If
That is, to emulate an explicit block, you can do this:
if (a == b) {
statement1;

}
This style allows you to show the logical structure of the code.
Wrapping a parenthesis breaks the integrity of the block, giving the appearance that they are neither part of the control structure nor part of the inner block.
As an alternative

S
Stanislav Gamayunov, 2012-08-07
@happyproff

While I work alone, I write in a way that would please my eye, with a bunch of empty lines for readability and other oddities. But if we are talking about teamwork, then it is better to use a popular standard. I would choose PSR-2 .

M
Mithgol, 2012-08-07
@Mithgol

I never wrap an opening parenthesis on a new line.

A
Arktos, 2012-08-08
@Arktos

And I decide depending on the IDE. If the IDE automatically puts a closing bracket for me, then I don’t transfer it, and if it doesn’t put it, then I transfer it, because otherwise I will have to press the end key all the time, which is not convenient. Both are readable for me.

A
andreysmind, 2012-08-07
@andreysmind

We write in Ruby, we do not experience such problems.

J
JinnZest, 2012-08-08
@JinnZest

I was developing in C++ a long time ago and always carried the parenthesis. I don’t know why, but almost all the listings that I saw then were with transfers.
Now, when I'm developing on Java, everything was decided easier. In most IDEs, when autoformatting is selected, this happens automatically. I do not argue that this, in theory, can be customized, but by default, the 1st option is everywhere. And since it’s too lazy to set up this customization every time, the question disappeared by itself.

C
charon, 2012-09-07
@charon

I can't stand. By the way, in JS it's not just a matter of taste:

return {
  a: 5,
  b: 7
}

will return the object as expected,
but
return 
{
  a: 5,
  b: 7
}

will just terminate the function. For the convenience of the programmer, the interpreter itself will substitute a semicolon immediately after return

Y
Yuri Morozov, 2012-09-07
@metamorph

1TBS vs K&R vs…
en.wikipedia.org/wiki/Indent_style
Personally I prefer K&R.
It will become boring - do not forget to chill out on the subject of tabs vs spaces.

D
Denis Dmitrienko, 2019-02-12
@castasat

I write in Java for Android, I use
Allman style, PSR-2 standard

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question