Why do most code examples overuse spaces in expressions?

An operator that can be written as:

foo=(bar*5)+baz; 

usually written in sample code (documentation, tutorials, etc.) as follows:

 foo = ( bar * 5 ) + baz; 

This seems to require extra work and seems counterproductive to me. Is there a reasonable reason for this? Is this good coding practice or just for sample code?

(The reason I ask is to make sure my coding style is right and to understand why most of the code that I see on the Internet is written like this).

+4
source share
12 answers

I do not put spaces after ( or earlier ) , but I know people who do this; in addition, as I would write:

 foo = (bar * 5) + baz; 

It seems to me that this is easier to read, and about the minimum amount of "extra work" that you could create. I used the code without a long distance, and now I look back and think it looks awful. However, there is no “right” coding style; if this is your project format source code, however, you want

+11
source

There is no extra work with an IDE that applies the interval to your preference. FWIW, my favorite interval is here:

 foo = (bar * 5) + baz; 

which is not long enough, as your second example, but, in my opinion, gives the right balance between multiplicity and readability. And ultimately what it is all about. This does not affect what it compiles, so if it makes reading easier, what is wrong with it?

+8
source

The way I write all my code - and how you should write all your own.

+6
source

Its easier to read what it is.

+2
source

I usually put spaces around statements, not braces. The main reason is readability.

+2
source

As a rule, "emphasize, as in natural language." People read more easily if what they see matches the patterns they expect. They expect a space after commas, commas, etc.

+2
source

To increase the readability of the sample - especially if it is the first time you look at it and you are trying to understand how it works.

+1
source

just for clarity.

+1
source

Because it is more readable in most cases, although I missed the spaces after opening the parenthesis and before the closing bracket. In addition, it should not cost more time for recording thanks to formatting tools, for example, in Eclipse. Here we can write a short version, and Eclipse automatically inserts spaces. You can even customize the style you like.

+1
source

An interval helps you understand which conditions should be grouped together.

The example you give is pathological: parentheses are redundant and spacing is counterproductive, but look at these examples:

  foo = bar * 5 + baz;
 foo = bar * 5 + baz;

The interval in the first line groups the substring 5+baz together, which, in turn, assumes that +, contrary to reality, has the highest priority.

+1
source

If you are worried about the extra work of printing something, you are probably at risk of writing code just for writing. I never worry about how long it takes to enter something, the difference between several characters here and there doesn’t matter compared to the time you could save, which is clearer after a few months when you try to remember how it works.

Just write it as soon as you feel better, and to other people on your team.

+1
source

Just because you CAN write code without spaces does not mean that you SHOULD. Ifmysentencesdidn'thaveanyspacesthey'dbemuchhardertoreadtoo.

Individual code elements should be visible on their own. As you get older (or your eyesight worsens), you will realize that you don’t need to squint on the screen to see the text, and the people you work with will appreciate the readability of your code.

0
source

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


All Articles