![]() |
How to Reduce Coding Errors when Writing Code - Part 2
This is the second article on avoiding certain errors at the early stage of code writing. In the previous post, we have already advised you to avoid a large number of calculations in one expression. However, we should investigate this question in detail. Let's see what's dangerous about complex expressions and how we can avoid many logical errors.
http://imgs.g4estatic.com/make-fewer...ing/image1.png IntroductionYou may read the previous article here. This time we'll take error samples from various famous projects to stress their prevalence. The errors I'm going to demonstrate here were found with the help of the PVS-Studio analyzer during a relatively large term. I have informed developers of almost all the projects about the errors, so I hope they will fix the defects in new code revisions. I'm writing this in introduction because I always receive letters after publishing articles with a request "please inform the project's developers about the errors you've found". 1. Do not use the ternary operation '?:' in compound expressionsA ternary conditional operation is written in C/C++ code with the '?:' operator. This is an operation that returns its second or third operand depending on the value of the logical expression defined by the first operand. For example: Code:
int minValue = A < B ? A : B;http://imgs.g4estatic.com/make-fewer...ing/image2.png Figure 1 - C/C++ operations arranged in priority descending order Note that the '?:' operation has a lower priority than addition, multiplication, bitwise OR operator and so on. Consider this code: int Z = X + (A == B) ? 1 : 2; It works differently than it might seem at first sight. Most likely, the programmer intended to add the X value to number 1 or 2 depending on the (A == B) condition. But actually it is the "X + (A == B)" expression which is the condition. In fact, it is this code written in the project: int Z = (X + (A == B)) ? 1 : 2; While the programmer wanted this to be: int Z = X + (A == B ? 1 : 2); What occurs to you first is that you just should know the priorities of operations. Well, programmers do know them but too insidious is this ternary operation! Not only novices make mistakes with it but experienced programmers as well. You may easily find them even in the most quality code. Here are a couple of examples. http://imgs.g4estatic.com/make-fewer...ing/image3.png V502 Perhaps the '?:' operator works in a different way than it was expected. The '?:' operator has a lower priority than the '*' operator. physics dgminkowskiconv.cpp 1061 Code:
dgInt32 CalculateConvexShapeIntersection (...)V502 Perhaps the '?:' operator works in a different way than it was expected. The '?:' operator has a lower priority than the '-' operator. views custom_frame_view.cc 400 Code:
static const int kClientEdgeThickness;V502 Perhaps the '?:' operator works in a different way than it was expected. The '?:' operator has a lower priority than the '|' operator. vm vm_file_win.c 393 Code:
#define FILE_ATTRIBUTE_NORMAL 0x00000080You can avoid such errors if you give up trying to place several operations into a single line of code. Or if you still want to do that, do not be greedy to add brackets. I will speak of brackets a bit later. Now let's try to avoid potential errors when using '?:'. Of course the '?:' operator is certainly a syntactic sugar and you can replace it with if in most cases. Among rare exceptions are such tasks as reference initialization: Code:
MyObject &ref = X ? A : B;Code:
MyObject *tmpPtr;Code:
int Z = X + (A == B) ? 1 : 2;Code:
int Z = X;Code:
mds[3] = FILE_ATTRIBUTE_NORMAL;2. Do not feel shy to use bracketsIt has become a custom for some reason that using additional brackets in C/C++ programming is considered something shameful. Perhaps it is because the question about operations' priorities is often asked at interview and people subconsciously start trying to use the priority mechanism to the full extent all the time - if he makes additional brackets, they will think he's a novice and not a true Jedi. I even came across a discussion on the Internet where some man was too dogmatic saying that using additional brackets is a bad form and that if somebody is not sure about the way an expression will be calculated, he must study instead of writing programs. Unfortunately, I've failed to find this discussion but I do not agree with such opinions. Of course, you must know priorities but if you use heterogeneous operations in an expression, you'd better use brackets to secure yourself from errors. This will not only protect you from potential errors but make code readable for other developers. Not only novice programmers but also skillful ones make mistakes caused by confusion of priorities. An expression does not necessarily need to be quite complicated and long; you may make a mistake in relatively simple expressions. Let's consider some examples. http://imgs.g4estatic.com/make-fewer...ing/image6.png V564 The '&' operator is applied to bool type value. You've probably forgotten to include parentheses or intended to use the '&&' operator. game g_client.c 1534 Code:
#define SVF_CASTAI 0x00000010V564 The '&' operator is applied to bool type value. You've probably forgotten to include parentheses or intended to use the '&&' operator. dosbox sdlmain.cpp 519 Code:
static SDL_Surface * GFX_SetupSurfaceScaled(Bit32u sdl_flags, And one more sample from Chromium: V564 The '&' operator is applied to bool type value. You've probably forgotten to include parentheses or intended to use the '&&' operator. base platform_file_win.cc 216 Code:
#define FILE_ATTRIBUTE_DIRECTORY 0x00000010I think the following is the best way: when operations are simple and usual, you don't need additional brackets. For example: Code:
if (A == B && X != Y)Code:
If ( ! (A & B))SummaryWrite simple and clear code. Splitting long and complex expressions into several strings you get longer code. But this code is much clearer to read and comprehend. It is less probable that you will make a mistake in such code. Do not be afraid to create an additional variable - the compiler will optimize the code well. Do not be greedy about using brackets in expressions where rare operators are used or where bit and logical operations are mixed. A programmer who will read your code with brackets in future will only be grateful to you for it. |
Re: How to Reduce Coding Errors when Writing Code - Part 2
really good sir..i wanted to know one thing how you find these small errors from thousands of lines of codes..its really amazing
|
Re: How to Reduce Coding Errors when Writing Code - Part 2
Quote:
|
| All times are GMT +5.5. The time now is 17:26. |