Let’s define the terms well. Let’s call the function what is in the library or created by you. What is talking about in the question (if
and foreach
) are reserved keywords of the language. It is not a function and it is necessary to understand this well.
Although the languages are liberal with the use of white space before the parentheses it is interesting to give more readability that use them properly, and mainly consistent.
In a function it is more common for people to choose not to put a blank space before opening the parentheses. So:
SomaProdutos(lista)
Or in the job declaration:
int SomaProdutos(Array lista) { ... }
Nothing prevents you from doing:
SomaProdutos (lista)
But this gives the impression that it is not a function. It looks like a name and a set of information grouped with parentheses. Even if it is not, beating the eye quickly gives a quick doubt in the brain.
Already the statements are not functions and if you do not put the space looks like it is one. Note that these statements which involve a condition or some extra argument usually, in most languages, be within parentheses to avoid syntactic ambiguity, but this does not mean that it is the same as a function. Then separate becomes clearer that it’s not a function, something like that:
if (x == 1)
for (var i = 0; i < y; i++) { ... }
I put in the Github for future reference.
It doesn’t change anything if you take out the space, but for a short moment you don’t know if it’s a function or not depending on how your eye goes through this text.
It is not a rule, but a matter of organization. Those who have relaxation when not worrying about it do not worry about other things. The brain doesn’t have a buzzer that turns the relax on and off. It’s not the end of the world to do it the bad way, but it’s not the best and it doesn’t cost anything to do it well. There are Ides that even format it the way they choose.
But the worst thing is that people use the space or not according to the moment. Changing the pattern randomly is worse than using a bad pattern. And a pattern that few programmers use, or at least a few good ones, is a bad thing. It is better to program as most people do, so it facilitates communication.
I’m glad you’re worried about the white space. Use enough horizontal spaces to give readability, but don’t put them where they might give the wrong impression.
I have a more controversial (shouldn’t be) opinion about vertical spaces. After all, if you have to separate a vertical block, the code is too complex to be in just one function, or else you separate what you shouldn’t be, breaking the reading flow, but since most people follow what others do without thinking about it has become a vertically spaced pattern. There are cases that the person does not even know why is skipping a line.
I honestly do not understand the downvotes, I believe the question is within the scope of the forum (?), it is specifically about some programming pattern that I wanted to confirm if it really exists, because I could not find a terminology related to it.
– Vinicius Gabriel
Standardized coding style depends a lot on what language you are using and the community of it.
– fernandosavio
@fernandosavio yes, I understand. Is that my doubt was specifically about that differentiation of spacing after the statements and functions, but I appreciate the collaboration!
– Vinicius Gabriel
Oh well, really, I’ve never seen a standardization that separates the parentheses from the function call.
– fernandosavio