SlideShare uma empresa Scribd logo
1 de 7
Baixar para ler offline
How to make fewer errors at the stage of
code writing. Part N2.
Author: Andrey Karpov

Date: 29.03.2011


Abstract
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
                                                                        ous
how we can avoid many logical errors
                                 errors.


Introduction




You may read the previous post 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 introduct
                                                                           .                     introduction
because I always receive letters after publishing articles with a request "please inform the project's
developers about the errors you've found
                                      found".


1. Do not use the ternary operation '?:' in compound expressions
A 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 fifirst
operand. 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 is
why the ternary operation is quite dange
                                   dangerous.
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.




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

dgInt32 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 a
lower priority than the '-' operator. views custom_frame_view.cc 400

static const int kClientEdgeThickness;

int height() const;

bool ShouldShowClientEdge() const;
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 a
lower priority than the '|' operator. vm vm_file_win.c 393

#define FILE_ATTRIBUTE_NORMAL                      0x00000080

#define FILE_FLAG_NO_BUFFERING                      0x20000000

vm_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 I've made a separate paragraph to
describe them. They are quite widely-spread. I could give you some more examples but they all are
similar.

You 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 parentheses. I will speak of parentheses 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:

MyObject &ref = X ? A : B;
Certainly there are no problems with it either, but creation of a reference to A or B without '?:' operator
takes 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 have
made up a rule for myself: the '?:' operator's result must be immediately stored somewhere and it
should not be combined together with any other actions. That is, there must be an assignment
operation to the left of the '?:' operator's condition. Let's 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 like
having 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 on
programmer's preference what pattern of error protection to choose.


2. Do not feel shy to use parentheses
It has become a custom for some reason that using additional parentheses 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 parentheses, 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 parentheses 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 parentheses 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.




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

#define SVF_CASTAI 0x00000010

char *ClientConnect(...) {

    ...

    if ( !ent->r.svFlags & SVF_CASTAI ) {

    ...

}




V564 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

static SDL_Surface * GFX_SetupSurfaceScaled(Bit32u sdl_flags,

                                                                Bit32u bpp) {

    ...

    if (!sdl.blit.surface || (!sdl.blit.surface->flags&SDL_HWSURFACE)) {

    ...

}
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

#define FILE_ATTRIBUTE_DIRECTORY 0x00000010

bool 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 in
slippery fragments will not be superfluous.

I think the following is the best way: when operations are simple and usual, you don't need additional
parentheses. For example:

if (A == B && X != Y)

if (A - B < Foo() * 2)

But if you use rarer operators (~, ^, &, |, <<, >>, ?:), you'd better add explicit parentheses. They will both
make 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 discussed
above. How we should handle "?:" is a matter of taste. Personally I like the method of simplification.


Summary
Write 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 parentheses 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.

Mais conteúdo relacionado

Mais de Andrey Karpov

Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...
Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...
Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...Andrey Karpov
 
Best Bugs from Games: Fellow Programmers' Mistakes
Best Bugs from Games: Fellow Programmers' MistakesBest Bugs from Games: Fellow Programmers' Mistakes
Best Bugs from Games: Fellow Programmers' MistakesAndrey Karpov
 
Does static analysis need machine learning?
Does static analysis need machine learning?Does static analysis need machine learning?
Does static analysis need machine learning?Andrey Karpov
 
Typical errors in code on the example of C++, C#, and Java
Typical errors in code on the example of C++, C#, and JavaTypical errors in code on the example of C++, C#, and Java
Typical errors in code on the example of C++, C#, and JavaAndrey Karpov
 
How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)
How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)
How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)Andrey Karpov
 
Game Engine Code Quality: Is Everything Really That Bad?
Game Engine Code Quality: Is Everything Really That Bad?Game Engine Code Quality: Is Everything Really That Bad?
Game Engine Code Quality: Is Everything Really That Bad?Andrey Karpov
 
C++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical ReviewerC++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical ReviewerAndrey Karpov
 
The Use of Static Code Analysis When Teaching or Developing Open-Source Software
The Use of Static Code Analysis When Teaching or Developing Open-Source SoftwareThe Use of Static Code Analysis When Teaching or Developing Open-Source Software
The Use of Static Code Analysis When Teaching or Developing Open-Source SoftwareAndrey Karpov
 
Static Code Analysis for Projects, Built on Unreal Engine
Static Code Analysis for Projects, Built on Unreal EngineStatic Code Analysis for Projects, Built on Unreal Engine
Static Code Analysis for Projects, Built on Unreal EngineAndrey Karpov
 
Safety on the Max: How to Write Reliable C/C++ Code for Embedded Systems
Safety on the Max: How to Write Reliable C/C++ Code for Embedded SystemsSafety on the Max: How to Write Reliable C/C++ Code for Embedded Systems
Safety on the Max: How to Write Reliable C/C++ Code for Embedded SystemsAndrey Karpov
 
The Great and Mighty C++
The Great and Mighty C++The Great and Mighty C++
The Great and Mighty C++Andrey Karpov
 
Static code analysis: what? how? why?
Static code analysis: what? how? why?Static code analysis: what? how? why?
Static code analysis: what? how? why?Andrey Karpov
 
Zero, one, two, Freddy's coming for you
Zero, one, two, Freddy's coming for youZero, one, two, Freddy's coming for you
Zero, one, two, Freddy's coming for youAndrey Karpov
 
PVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOps
PVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOpsPVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOps
PVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOpsAndrey Karpov
 
PVS-Studio Static Analyzer as a Tool for Protection against Zero-Day Vulnerab...
PVS-Studio Static Analyzer as a Tool for Protection against Zero-Day Vulnerab...PVS-Studio Static Analyzer as a Tool for Protection against Zero-Day Vulnerab...
PVS-Studio Static Analyzer as a Tool for Protection against Zero-Day Vulnerab...Andrey Karpov
 
Analysis of commits and pull requests in Travis CI, Buddy and AppVeyor using ...
Analysis of commits and pull requests in Travis CI, Buddy and AppVeyor using ...Analysis of commits and pull requests in Travis CI, Buddy and AppVeyor using ...
Analysis of commits and pull requests in Travis CI, Buddy and AppVeyor using ...Andrey Karpov
 
PVS-Studio in the Clouds: CircleCI
PVS-Studio in the Clouds: CircleCIPVS-Studio in the Clouds: CircleCI
PVS-Studio in the Clouds: CircleCIAndrey Karpov
 
PVS-Studio in the Clouds: Azure DevOps
PVS-Studio in the Clouds: Azure DevOpsPVS-Studio in the Clouds: Azure DevOps
PVS-Studio in the Clouds: Azure DevOpsAndrey Karpov
 
Errors that static code analysis does not find because it is not used
Errors that static code analysis does not find because it is not usedErrors that static code analysis does not find because it is not used
Errors that static code analysis does not find because it is not usedAndrey Karpov
 
Machine Learning in Static Analysis of Program Source Code
Machine Learning in Static Analysis of Program Source CodeMachine Learning in Static Analysis of Program Source Code
Machine Learning in Static Analysis of Program Source CodeAndrey Karpov
 

Mais de Andrey Karpov (20)

Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...
Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...
Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...
 
Best Bugs from Games: Fellow Programmers' Mistakes
Best Bugs from Games: Fellow Programmers' MistakesBest Bugs from Games: Fellow Programmers' Mistakes
Best Bugs from Games: Fellow Programmers' Mistakes
 
Does static analysis need machine learning?
Does static analysis need machine learning?Does static analysis need machine learning?
Does static analysis need machine learning?
 
Typical errors in code on the example of C++, C#, and Java
Typical errors in code on the example of C++, C#, and JavaTypical errors in code on the example of C++, C#, and Java
Typical errors in code on the example of C++, C#, and Java
 
How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)
How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)
How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)
 
Game Engine Code Quality: Is Everything Really That Bad?
Game Engine Code Quality: Is Everything Really That Bad?Game Engine Code Quality: Is Everything Really That Bad?
Game Engine Code Quality: Is Everything Really That Bad?
 
C++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical ReviewerC++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical Reviewer
 
The Use of Static Code Analysis When Teaching or Developing Open-Source Software
The Use of Static Code Analysis When Teaching or Developing Open-Source SoftwareThe Use of Static Code Analysis When Teaching or Developing Open-Source Software
The Use of Static Code Analysis When Teaching or Developing Open-Source Software
 
Static Code Analysis for Projects, Built on Unreal Engine
Static Code Analysis for Projects, Built on Unreal EngineStatic Code Analysis for Projects, Built on Unreal Engine
Static Code Analysis for Projects, Built on Unreal Engine
 
Safety on the Max: How to Write Reliable C/C++ Code for Embedded Systems
Safety on the Max: How to Write Reliable C/C++ Code for Embedded SystemsSafety on the Max: How to Write Reliable C/C++ Code for Embedded Systems
Safety on the Max: How to Write Reliable C/C++ Code for Embedded Systems
 
The Great and Mighty C++
The Great and Mighty C++The Great and Mighty C++
The Great and Mighty C++
 
Static code analysis: what? how? why?
Static code analysis: what? how? why?Static code analysis: what? how? why?
Static code analysis: what? how? why?
 
Zero, one, two, Freddy's coming for you
Zero, one, two, Freddy's coming for youZero, one, two, Freddy's coming for you
Zero, one, two, Freddy's coming for you
 
PVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOps
PVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOpsPVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOps
PVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOps
 
PVS-Studio Static Analyzer as a Tool for Protection against Zero-Day Vulnerab...
PVS-Studio Static Analyzer as a Tool for Protection against Zero-Day Vulnerab...PVS-Studio Static Analyzer as a Tool for Protection against Zero-Day Vulnerab...
PVS-Studio Static Analyzer as a Tool for Protection against Zero-Day Vulnerab...
 
Analysis of commits and pull requests in Travis CI, Buddy and AppVeyor using ...
Analysis of commits and pull requests in Travis CI, Buddy and AppVeyor using ...Analysis of commits and pull requests in Travis CI, Buddy and AppVeyor using ...
Analysis of commits and pull requests in Travis CI, Buddy and AppVeyor using ...
 
PVS-Studio in the Clouds: CircleCI
PVS-Studio in the Clouds: CircleCIPVS-Studio in the Clouds: CircleCI
PVS-Studio in the Clouds: CircleCI
 
PVS-Studio in the Clouds: Azure DevOps
PVS-Studio in the Clouds: Azure DevOpsPVS-Studio in the Clouds: Azure DevOps
PVS-Studio in the Clouds: Azure DevOps
 
Errors that static code analysis does not find because it is not used
Errors that static code analysis does not find because it is not usedErrors that static code analysis does not find because it is not used
Errors that static code analysis does not find because it is not used
 
Machine Learning in Static Analysis of Program Source Code
Machine Learning in Static Analysis of Program Source CodeMachine Learning in Static Analysis of Program Source Code
Machine Learning in Static Analysis of Program Source Code
 

Último

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 

Último (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 

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

  • 1. How to make fewer errors at the stage of code writing. Part N2. Author: Andrey Karpov Date: 29.03.2011 Abstract 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 ous how we can avoid many logical errors errors. Introduction You may read the previous post 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 introduct . introduction because I always receive letters after publishing articles with a request "please inform the project's developers about the errors you've found found". 1. Do not use the ternary operation '?:' in compound expressions A 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 fifirst operand. 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 is why the ternary operation is quite dange dangerous.
  • 2. 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:
  • 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 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. 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 dgInt32 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 a lower priority than the '-' operator. views custom_frame_view.cc 400 static 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 a lower priority than the '|' operator. vm vm_file_win.c 393 #define FILE_ATTRIBUTE_NORMAL 0x00000080 #define FILE_FLAG_NO_BUFFERING 0x20000000 vm_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 I've made a separate paragraph to describe them. They are quite widely-spread. I could give you some more examples but they all are similar. You 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 parentheses. I will speak of parentheses 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: MyObject &ref = X ? A : B;
  • 5. Certainly there are no problems with it either, but creation of a reference to A or B without '?:' operator takes 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 have made up a rule for myself: the '?:' operator's result must be immediately stored somewhere and it should not be combined together with any other actions. That is, there must be an assignment operation to the left of the '?:' operator's condition. Let's 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 like having 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 on programmer's preference what pattern of error protection to choose. 2. Do not feel shy to use parentheses It has become a custom for some reason that using additional parentheses 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 parentheses, 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 parentheses 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 parentheses to secure yourself from errors. 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. 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. 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 #define SVF_CASTAI 0x00000010 char *ClientConnect(...) { ... if ( !ent->r.svFlags & SVF_CASTAI ) { ... } V564 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 static 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. You've probably forgotten to include parentheses or intended to use the '&&' operator. base platform_file_win.cc 216 #define FILE_ATTRIBUTE_DIRECTORY 0x00000010 bool 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 in slippery fragments will not be superfluous. I think the following is the best way: when operations are simple and usual, you don't need additional parentheses. For example: if (A == B && X != Y) if (A - B < Foo() * 2) But if you use rarer operators (~, ^, &, |, <<, >>, ?:), you'd better add explicit parentheses. They will both make 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 discussed above. How we should handle "?:" is a matter of taste. Personally I like the method of simplification. Summary Write 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 parentheses 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.