A little e-Book: learn from real examples how to avoid bugs

Submitted on: 4/18/2016 9:42:28 AM
By: el_programmer  
Level: Beginner
User Rating: Unrated
Compatibility: C, C++ (general)
Views: 6009
     Chief technical officer of PVS-Studio team, Andrey Karpov, wrote a considerable article, giving 42 practical recommendations on programming in C/C++. These tips are intended for C/C++ programmers, but in general they are universal and could be of interest for developers using other languages. They are based on real errors that were found during the analysis of various open-source programs. Each recommendation is given with practical examples and suggestions about the ways to fix the code. You will also find useful links and detailed explanations of various programming constructions. The aim of this article is to warn a programmer against possible mistakes that he/she may make when coding. Reading this material and digging deep into the documentation will help you avoid a situation when a hidden error, made inadvertently, may give your colleagues and users a hard time.

				The book covers 42 topics. In spite of the simple titles of the chapters, the bugs found are really various and non-standard. In addition to that, the text provides a lot of links to interesting materials that give more details on topics. To make more use of this book, please don’t hurry and go to the links provided. 
1.	Don't do the compiler's job
2.	Larger than 0 does not mean 1
3.	Copy once, check twice
4.	Beware of the ?: operator and enclose it in parentheses
5.	Use available tools to analyze your code
6.	Check all the fragments where a pointer is explicitly cast to integer types
7.	Do not call the alloca() function inside loops
8.	Remember that an exception in the destructor is dangerous.
9.	Use the '\0' literal for the terminal null character
10.	Avoid using multiple small #ifdef blocks
11.	Don't try to squeeze as many operations as possible in one line
12.	When using Copy-Paste, be especially careful with the last lines
13.	Table-style formatting
14.	A good compiler and coding style aren't always enough
15.	Start using enum class in your code, if possible
16.	"Look what I can do!" - Unacceptable in programming
17.	Use dedicated functions to clear private data
18.	The knowledge you have, working with one language isn't always applicable to another language
19.	How to properly call one constructor from another
20.	The End-of-file (EOF) check may not be enough
21.	Check that the end-of-file character is reached correctly (EOF)
22.	Do not use #pragma warning(default:X)
23.	Evaluate the string literal length automatically
24.	Override and final identifiers should become your new friends.
25.	Do not compare 'this' to nullptr anymore
26.	Insidious VARIANT_BOOL
27.	Guileful BSTR strings
28.	Avoid using a macro if you can use a simple function
29.	Use a prefix increment operator (++i) in iterators instead of a postfix (i++) operator
30.	Visual C++ and wprintf() function
31.	In C and C++ arrays are not passed by value
32.	Dangerous printf
33.	Never dereference null pointers
34.	Undefined behavior is closer than you think
35.	Adding a new constant to enum don't forget to correct switch operators
36.	If something strange is happening to your PC, check its memory.
37.	Beware of the 'continue' operator inside do {...} while (...)
38.	Use nullptr instead of NULL from now on
39.	Why incorrect code works
40.	Start using static code analysis
41.	Avoid adding a new library to the project.
42.	Don't use function names with "empty"
For those, who find reading PDF format more convenient: 

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

4/19/2016 7:35:09 AMel_programmer

For some reason the links don't work. The article can be read here:
(If this comment was disrespectful, please report it.)


Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.