The issue of style is actually a very serious matter.
Do not forget that the code is written by you not for the compiler. Make it easy for the compiler to understand, but your goal is harder: make it clear to the person .
The program code is the same literary work. You have to bring the thought to the reader, and this reader can be both yourself in six months, and your colleague, who will have to edit the code while you are on vacation.
A good code lives for a long time, which means it will be read, corrected, understood and explained many times. Understanding someone else's code is much more difficult than writing a new one from scratch, so investing in code clarity is important (if, of course, you wish your code a long, happy life).
Code reduction is not necessary . The code should be clear, no more and no less.
If it seems to you that somewhere you need to paint for clarity, do so, even if you have to enter additional variables only to give a name to the intermediate result. If, on the contrary, it seems to you that the code is too much for the simple thing that it does - bring this thing into a separate function and come up with the correct name to explain the meaning of the action. Maintain uniformity and overall pace: if a piece of code launches a space rocket in flight, then a piece of code next to it that reads data from the configuration file looks ridiculous.
Naming variables and functions . Do not feel sorry for the letters! Bytes on the hard drive fell. You do not have to write comments to clarify the meaning of the variable, otherwise the reader should see one (the name of the variable) and keep the other in mind (its meaning). On the other hand, do not bother the reader with unnecessary details. The fact that you are not just acceptableByteCount , but countOfBytesWhichDidNotPassAtLeastTwoFilters is boring. Observe a reasonable balance. If you need a loop variable, name it i or j . Adhere to generally accepted agreements, if necessary, invent your own (but reasonable!), Easily understood by others.
Name the variables correctly. The name should reflect the meaning. If you use the same variable in two different ways, you are doing wrong, divide it into two variables with a unique meaning. For example, it is not necessary to combine the length of the transferred string and the counter of the remaining characters for processing, although the initial value of the counter coincides with the length of the string.
Try to make the text read naturally. For example, the name of the action should be a verb (not vector.Normalization() , but vector.Normalize() or vector.GetNormal() ). The name of the boolean condition should be similar to the condition and most likely start with is , has and the like. (For example: hasChanges() , isPrime() , etc.) For God's sake, use English names, not Russian translit! Believe me, isZarplataComputed() looks awful. The exception is languages with Cyrillic syntax ( 1c ?) Or a generally accepted command style.
Separation of actions . Yes, it makes sense to separate the code into a function only to correctly name this code fragment. Functions are not reusable! Functions are needed for logical code breaking into parts. If you see that your function is responsible for different things, and you cannot think of a short, precise name for it, it means that your function does too much, and it needs to be divided. Often from a super-long function of 500 lines we get a dozen classes. And this is good.
And yes, the reader is much better able to understand the function that does one simple task. If the function does too much, it has more complicated preconditions and postconditions besides more complex code (which means that it is even harder to understand).
For good splitting, design from top to bottom. Example: to cook food is what? Decide what it means to cook a french breakfast. Okay, what about cooking a french breakfast? This is to buy croissants and make coffee. What is brewed coffee? This grind grain in a coffee grinder, fall asleep in the Turk, add water, put on fire, etc. This is naturally formed into the procedures PrepareMeals , PrepareFrenchBreakfast , BuyCroissants , MakeCoffee . I did not have to invent anything.
The relevance of comments . Try to write code so that comments are not needed. Very often, comments are redundant; very often they become obsolete and cease to reflect reality. People often change the logic of the code, but forget to run through the comments.
Don't forget that the code is being executed, not the comments. Therefore, an erroneous, misleading comment (for example: /* здесь не может получиться NULL */ ) is much worse than its absence. If you have a piece of code with a comment explaining what it does, turn this code into a function, and the comment into its name. It is likely that it will not be possible to completely avoid comments, but make sure that the comments describe why you are doing what you are doing, and what exactly you are doing should be clear from the code.
Formatting Bad formatting greatly affects the readability of the code. Develop a style and stick to it. What style you choose, in general, and it does not matter, as long as it is logical and consistent. (For example, if you put a space after a while , you should probably put a space after the if .) Try, however, not to deviate from generally accepted conventions (for example, it is customary to choose method names in Java in lowerCamelCase), otherwise it will be difficult for you to read someone else's code .
If you work in a team, do not break the overall style, even if you personally do not like it. If the team does not have a generally accepted style, offer it! Placing brackets, indents, spaces, maximum length of lines and everything is important so that the reader is not distracted. Inconsistent formatting is confusing and distracting much more than it seems - just like the wrong punctuation makes it difficult to correctly understand the text of a literary work.
And the last. Do not worry about improving the efficiency of the code by reducing its readability. Selecting a separate method is not a problem, modern compilers have learned to inline everything they need. And most likely they are able to combine different variables into one register much better than you. If in some place for low-level optimization you really need to degrade readability, provide this fragment with sufficient comments about what happens in the code, and most importantly, why such a trick is needed.