📜 ⬆️ ⬇️

NOT useful tips for the programmer

I am a programmer with little experience, but I am already taking my first tentative steps along the career ladder. I read and learn a lot, sometimes I try something in my practice. And now I am ready to share with you some programming tips that help me a lot in my everyday work. I came to all the tips myself and they were repeatedly tested in my projects.

1) Infinite loop


We all know that in a computer a lot can happen according to the principle “It’s itself”. Somewhere there will be a voltage jump or magnetic induction. Therefore, bit inversion can easily occur in our program. And we get false instead of true. And this suggests that the cycle of the form:

while ( true ) { doSome(); } 

totally unreliable! At any time, true can change to false and we will exit the loop. How to avoid it? Very easy. We can easily increase the reliability of an infinite loop with just such a “piece” of code.

 while(true) { do { for(;;) { doSome(); } } while(1); } 

Many may say that the number of code has increased. But the preprocessor comes to the rescue.
')
 #define FOREVER_CYCLE_START while(true){do{for(;;){ #define FOREVER_CYCLE_END }}while(1);} 

And now we can easily write

 FOREVER_CYCLE_START foreverYoung(); FOREVER_CYCLE_END 

Great and everything is clear at once. A newbie will not think what it means for (;;) {}. and why the condition in for looks like a crying face.

2) Setting the curly brace


Endless disputes. What is better?

 if ( COND ) { doSome(); } 

or

 if ( COND ) { doSome(); } 

Enough! I present an option that will suit everyone!

 if ( COND ) { { doSome(); }} 

Everyone is happy and contented. You can forget about the holivar ... Although ... is it worth it to write the last two closing curly brackets on one line or is it worth splitting into two? But this is a separate issue requiring the involvement of specialists and additional discussions.

3) Assembler inserts


Never be afraid to insert assembly code in your programs. Such inserts show that you know not only high-level C ++, but also in trouble with the language of the machine. Yes, sometimes, it is necessary to rewrite or correct the assembler code in order to transfer the program to another architecture. But you will be additionally paid for this job and once again your superiors will make sure that you are a good employee. And no one else can handle your responsibilities. There is a little trick. You can insert useless assembler inserts that do nothing. For example, you can recall the different ways of exchanging information in registers:

 mov bx, ax mov ax, cx mov cx, bx xchg ax, cx push ax mov ax, cx pop cx xor ax, cx xor cx, ax xor ax, cx 

This code will add importance to your program. And the people who will work with him will refresh their knowledge. An inexperienced programmer who has just come from a university can work with you. And we know that students are not very willing to learn assembler. So he will immediately run to you for clarification. He will have the impression that you are a guru, since you can easily manipulate programming languages. And the bosses will be happy to see that new employees run to you for new knowledge and you willingly share them. What may affect your salary. And yet they know that assembly language is faster!

4) Never use const


Programmers like no one else knows that we live in a turbulent and rapidly developing world, where everything changes very quickly. So it makes no sense to use const. Even when the object is obviously const, for example, the number of earth satellites (we have only one moon), for some reason, the moon can still split into two. And you have to do:

 const int moons = 1; moons++; 

That to your disappointment the compiler will reject. And besides, you can accidentally make a const, something that you would not want. How do you like it if someone make your salary const? Everything! Goodbye career growth and salary increase.

 const salary = 100; // $   

And besides, it's just an extra waste of bytes of code. For these 5 characters one could do something useful. For example, foo ++.

5) Let the variables be as short as possible.


After all, remembering the variable x is much easier than position_of_object_on_x_axis. For example, this code looks concise and easy to read:

 if ( x < y ) { z++; } else if ( c > q ) { c += f; } else { r = z + c; } 

The code easily fits within the sight of the eyes. And it’s easy to analyze just by looking at it.

6) CamelCase or snake_case


Another eternal holivar. The same as the Hungarian notation is useful or is a relic of the past! Enough. We programmers should write code, and not find out who is right and who is not.

I analyzed these styles and developed a universal style that suits everyone without exception:
vengro_snakeCamel !

Examples of this style:
 float float_charPositionX; int int_moneySpent char* charPointer_nameOfPerson; 

7) Variable names


Any programmer knows that the code should be written in such a way that later it would be convenient to read. But we do not always know who will read it after us. And it is good if this person will know English. But not everyone knows him and we must take into account this audience. Declare variables so that they are clear in Russian.

 int int_kolichestvoBukv; float float_glubinaBasseina; bool bool_estLiZhiznNaMarse; 

Now even a child can understand the meaning of your variables. But still. there is no limit to perfection. Still played in computer games? Remember how you had to communicate with teammates, and the Russian language was not supported? Use this knowledge in programming. After all, everyone understood everything, which means it makes sense.

 int int_koJIu4ecTBo6ykB; float float_rJly6uHa6acceuHA; bool bool_ectbJIu7Ku3HbHaMapce; 

8) DEFINEs


The preprocessor has been around for a long time and has been inherited from the C language. And since it was inherited, it means it represents some value, is time-tested, and transferred to C ++, as an integral part of programming. Take a typical situation. In the first year of university you studied pascal. And on the 2nd you started teaching C ++. New syntax can be confusing. But it does not matter. To help hasten the preprocessor C ++.

You can always write:

 #define BEGIN { #define END } 

And that's all. The transition to a new language will be less painful and more understandable.

9) Global Variables


Feel free to use global variables. After all, it is very convenient. First, do not worry about initialization. They must be initialized to zero. You can easily access them from any part of the program, which allows you to avoid unnecessary parameters in functions. Increases project flexibility. And if someone says that it is not thread-safe. The way it is. And it will stop you from using threads. The human brain calculates single-threaded programs better. And global variables will save you from using threads, which will only increase the reliability of your programs.

10) Sources


Remember that all source code of the program is best stored in one file. This makes it easy to find the variables and functions that interest you. After all, a simple text search at the same time becomes a search for the entire project. All functions before eyes, all variables are available in a pair of scrolling movements. Splitting the code into several files leads to the fact that it is already necessary to monitor it, so that the files are not lost, to resolve calls to global variables from each compilation module, etc. Why all these difficulties? When everything is compact is near it is much easier to understand the source codes, rather than jumping on a bunch of different files.

Epilogue


In the course of my work for some reason I am often criticized for my code and coding style. Although not explain why. Often swear and laugh. Therefore, a request to more experienced programmers - do not offend your young colleagues. We were all new and went through inexperience, doubt and insecurity. Be kinder. And I hope my advice you will never come in handy.

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


All Articles