
I wrote a small e-book in which I consider how to make the code better. The book is focused on C / C ++ programmers, but it will be interesting for developers using other languages. The format of the book is not suitable for my beloved Habr, but I am interested to get feedback and discuss the thoughts presented in the article. Therefore, I decided to post here only the announcement, and the article itself can be found
here . And I invite in the comments for discussion.
I hope you enjoyed this collection of tips. Of course, it is impossible to warn about all the ways to write a program, and there is no point in this. My goal was to warn the programmer and develop a sense of danger in him. Perhaps when the programmer once again encounters something incomprehensible, he will remember my instructions and will not hurry. Sometimes a few minutes of studying the documentation or writing a simpler / clearer code will allow you to avoid introducing a hidden error that would then poison the lives of users and colleagues for several years.
Here are 42 topics that I cover:
- Do not take over the work of the compiler
- Greater than 0, this is not 1
- Copy once, check several times
- Fear operator?: And enclose it in parentheses
- Use available code checking tools.
- Check all places where the pointer is explicitly cast to integral types.
- Do not call the alloca () function inside loops.
- Remember that an exception in a destructor is dangerous.
- Use a literal '\ 0' to denote a terminal zero.
- Try not to shrink when using #ifdef
- Do not be greedy on lines of code
- When focusing on Copy-Paste, focus on the end of work.
- Align uniform code with a “table”
- Remember: not always enough compiler and good coding style
- If possible, start using the enum class.
- “See how I can” - unacceptable in programming
- Use specialized functions to wipe private data in memory.
- Knowledge gained from working with one language is not always applicable to another language.
- How to call one constructor from another
- End of File (EOF) checks may not be enough
- Check the sign of reaching the end of the file (EOF) correctly
- Do not use #pragma warning (default: X)
- Calculate the length of a string literal automatically.
- The override and final identifiers should be your new friends.
- No more comparing 'this' with nullptr
- VARIANT_BOOL
- Insidious bstr strings
- Do not make macros where you can make an ordinary function.
- Use prefix increment operator (++ i) instead of postfix (i ++) for iterators
- Visual C ++ and the wprintf () function
- In C and C ++, arrays are not passed by value.
- Fear printf
- Never dereference null pointers
- Undefined behavior is closer than you think.
- Adding a new constant to enum, do not forget to fix the switch statements
- If magic events occur on your computer, check the memory
- Be careful of the continue statement inside do {...} while (...)
- From today, use nullptr instead of NULL.
- Why incorrect code sometimes works
- Implement static code analysis
- Resist adding new libraries to your project.
- Do not give functions the name "empty"
So, the link to the article:
http://www.viva64.com/ru/b/0391/In English:
http://www.viva64.com/en/b/0391/')
I foresee that the
Habroeffekt may inadvertently happen, so I want to
make sure in advance. Therefore, I post on Yandex Disk in PDF format:
Russian:
https://yadi.sk/i/LKkWupFjr5WzREnglish:
https://yadi.sk/i/pBZqebxsr5WygPS I do not specifically place the text of the book on other sites. Quite a lot of people report shortcomings in the text, and if the text is published in several places, I’m tortured to make corrections. When the text is stabilized, I will place it somewhere else in different formats. In the meantime, please treat with understanding. And another request. I’m writing about emails (karpov [@] viva64.com), and not in the comments.