Code pattern for function calling

Asked

Viewed 60 times

1

I don’t quite remember where, but I think it was in some discussion on a topic here in the same OS, referring to that in the statement of if, or any other function of the language itself must be called with a space after the name, e.g..:

if ()
foreach ()

And functions created in the project itself should be called without this space.

Is there any methodology of good practice that determines this rule? As a Camelcase of life?

  • 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.

  • 1

    Standardized coding style depends a lot on what language you are using and the community of it.

  • @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!

  • Oh well, really, I’ve never seen a standardization that separates the parentheses from the function call.

1 answer

5


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.

  • Got it! Thank you, that’s exactly the kind of explanation I was looking for.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.