How to make fewer errors at the stage of code writing. Part N2
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

How to make fewer errors at the stage of code writing. Part N2

  • 425 views
Uploaded on

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......

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.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
425
On Slideshare
424
From Embeds
1
Number of Embeds
1

Actions

Shares
Downloads
2
Comments
0
Likes
0

Embeds 1

http://www.slideshare.net 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. How to make fewer errors at the stage ofcode writing. Part N2.Author: Andrey KarpovDate: 29.03.2011AbstractThis is the second article on avoiding certain errors at the early stage of code writing. In the previouspost, we have already advised you to avoid a large number of calculations in one expression. However,we should investigate this question in detail. Lets see whats dangerous about complex expressions and oushow we can avoid many logical errors errors.IntroductionYou may read the previous post here. This time well take error samples from various famous projects to .stress their prevalence. The errors Im 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 projectsabout the errors, so I hope they will fix the defects in new code revisions. Im writing this in introduct . introductionbecause I always receive letters after publishing articles with a request "please inform the projectsdevelopers about the errors youve found 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 operator.returns its second or third operand depending on the value of the logical expression defined by the fifirstoperand. For example:int minValue = A < B ? A : B;A ternary operation has a very low priority (see the table). Programmers often forget about it and that iswhy the ternary operation is quite dange dangerous.
  • 2. Figure 1 - C/C++ operations arranged in priority descending orderNote that the ?: operation has a lower priority than addition, multiplication, bitwise OR operator andso 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 Xvalue 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:
  • 3. int Z = X + (A == B ? 1 : 2);What occurs to you first is that you just should know the priorities of operations. Well, programmers doknow them but too insidious is this ternary operation! Not only novices make mistakes with it butexperienced programmers as well. You may easily find them even in the most quality code. Here are acouple of examples.V502 Perhaps the ?: operator works in a different way than it was expected. The ?: operator has alower priority than the * operator. physics dgminkowskiconv.cpp 1061dgInt32 CalculateConvexShapeIntersection (...){ ... den = dgFloat32 (1.0e-24f) * (den > dgFloat32 (0.0f)) ? dgFloat32 (1.0f) : dgFloat32 (-1.0f); ...}V502 Perhaps the ?: operator works in a different way than it was expected. The ?: operator has alower priority than the - operator. views custom_frame_view.cc 400static const int kClientEdgeThickness;int height() const;bool ShouldShowClientEdge() const;
  • 4. void CustomFrameView::PaintMaximizedFrameBorder(gfx::Canvas* canvas) { ... int edge_height = titlebar_bottom->height() - ShouldShowClientEdge() ? kClientEdgeThickness : 0; ...}V502 Perhaps the ?: operator works in a different way than it was expected. The ?: operator has alower priority than the | operator. vm vm_file_win.c 393#define FILE_ATTRIBUTE_NORMAL 0x00000080#define FILE_FLAG_NO_BUFFERING 0x20000000vm_file* vm_file_fopen(...){ ... mds[3] = FILE_ATTRIBUTE_NORMAL | (islog == 0) ? 0 : FILE_FLAG_NO_BUFFERING; ...}As you may see, errors of this type deserve consideration. That is why Ive made a separate paragraph todescribe them. They are quite widely-spread. I could give you some more examples but they all aresimilar.You can avoid such errors if you give up trying to place several operations into a single line of code. Or ifyou still want to do that, do not be greedy to add parentheses. I will speak of parentheses a bit later.Now lets 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:MyObject &ref = X ? A : B;
  • 5. Certainly there are no problems with it either, but creation of a reference to A or B without ?: operatortakes more lines of code to implement:MyObject *tmpPtr;If (X) tmpPtr = &A;else tmpPtr = &B;MyObject &ref = *tmpPtr;So, we should not refuse to use the ?: operator. But you might easily make a mistake using it. So I havemade up a rule for myself: the ?: operators result must be immediately stored somewhere and itshould not be combined together with any other actions. That is, there must be an assignmentoperation to the left of the ?: operators condition. Lets return to the original sample:int Z = X + (A == B) ? 1 : 2;I suggest that this code should be written this way:int Z = X;Z += A == B ? 1 : 2;In case of an IPP Samples code sample, I would write it so:mds[3] = FILE_ATTRIBUTE_NORMAL;mds[3] |= (islog == 0) ? 0 : FILE_FLAG_NO_BUFFERING;You may disagree with this recommendation and I will not defend it. For instance, I myself do not likehaving two lines or more instead of one line. Another nice alternative is the obligatory enclosure of the?: operator inside parentheses. The main task for me is to show you error patterns, but it depends onprogrammers preference what pattern of error protection to choose.2. Do not feel shy to use parenthesesIt has become a custom for some reason that using additional parentheses in C/C++ programming isconsidered something shameful. Perhaps it is because the question about operations priorities is oftenasked at interview and people subconsciously start trying to use the priority mechanism to the fullextent all the time - if he makes additional parentheses, they will think hes a novice and not a true Jedi.I even came across a discussion on the Internet where some man was too dogmatic saying that usingadditional parentheses is a bad form and that if somebody is not sure about the way an expression willbe calculated, he must study instead of writing programs. Unfortunately, Ive failed to find thisdiscussion but I do not agree with such opinions. Of course, you must know priorities but if you useheterogeneous operations in an expression, youd better use parentheses to secure yourself fromerrors. This will not only protect you from potential errors but make code readable for other developers.
  • 6. Not only novice programmers but also skillful ones make mistakes caused by confusion of priorities. Anexpression does not necessarily need to be quite complicated and long; you may make a mistake inrelatively simple expressions. Lets consider some examples.V564 The & operator is applied to bool type value. Youve probably forgotten to include parentheses orintended to use the && operator. game g_client.c 1534#define SVF_CASTAI 0x00000010char *ClientConnect(...) { ... if ( !ent->r.svFlags & SVF_CASTAI ) { ...}V564 The & operator is applied to bool type value. Youve probably forgotten to include parentheses orintended to use the && operator. dosbox sdlmain.cpp 519static SDL_Surface * GFX_SetupSurfaceScaled(Bit32u sdl_flags, Bit32u bpp) { ... if (!sdl.blit.surface || (!sdl.blit.surface->flags&SDL_HWSURFACE)) { ...}
  • 7. And one more sample from Chromium:V564 The & operator is applied to bool type value. Youve probably forgotten to include parentheses orintended to use the && operator. base platform_file_win.cc 216#define FILE_ATTRIBUTE_DIRECTORY 0x00000010bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info) { ... info->is_directory = file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY != 0; ...}The expressions are simple. The developers are attaboys. Still there are errors. So, using parentheses inslippery fragments will not be superfluous.I think the following is the best way: when operations are simple and usual, you dont need additionalparentheses. For example:if (A == B && X != Y)if (A - B < Foo() * 2)But if you use rarer operators (~, ^, &, |, <<, >>, ?:), youd better add explicit parentheses. They will bothmake code clearer and secure you from a potential error. For example:If ( ! (A & B))x = A | B | (z < 1 ? 2 : 3);Exploiting parentheses when using rare operations will also help you with the "?:" operator discussedabove. How we should handle "?:" is a matter of taste. Personally I like the method of simplification.SummaryWrite simple and clear code. Splitting long and complex expressions into several strings you get longercode. But this code is much clearer to read and comprehend. It is less probable that you will make amistake in such code. Do not be afraid to create an additional variable - the compiler will optimize thecode well.Do not be greedy about using parentheses in expressions where rare operators are used or where bitand logical operations are mixed.A programmer who will read your code with brackets in future will only be grateful to you for it.