Upgrade to Pro — share decks privately, control downloads, hide ads and more …

5 terrible tips for a C++ programmer

5 terrible tips for a C++ programmer

Hi! I'm Andrey Karpov, the CMO at PVS-Studio. I've been writing articles about static analysis and code quality for many years now. Now we are presenting you an e-book that contains 60 terrible tips for C++ developers. It's both sad and funny. Some tips may seem unbelievable or silly - but I've seen such patterns in real commercial products. Here I'm posting only 5 tips - to see the complete ebook, click here: https://pvs-studio.com/en/blog/posts/cpp/1053/

PVS-Studio

July 06, 2023
Tweet

More Decks by PVS-Studio

Other Decks in Programming

Transcript

  1. 60 terrible tips for a C++ programmer Part 1 Tips

    1 – 5 Andrey Karpov pvs-studio.com
  2. For many years I’ve been writing articles on code quality

    and static analysis methodology. Now I’ve written an entire e-book with a whole bunch of terrible tips for developers. Something will seem funny to you, something – not so much. If any tip seems too silly – just know, I’ve really encountered all of those cases somewhere in the programming world. This presentation contains just 5 tips – so that you don’t get tired. However, if you can’t wait to see all of them, here’s the full version: "60 terrible tips for a C++ programmer". In any case, I hope you have a good read! Introduction 2
  3. ▪ Terrible tip N1. Only C++ ▪ Terrible tip N2.

    Tab character in string literals ▪ Terrible tip N3. Nested macros ▪ Terrible tip N4. Disable warnings ▪ Terrible tip N5. The shorter the variable name is, the better Contents: tips 1 – 5 3
  4. ▪ There's nothing wrong with writing code in C++. The

    world has many great projects written in C++. ▪ It's bad when developers use this language only because it's "cool" or because it's the only language the team knows. ▪ The variety of programming languages reflects the variety of tasks that software developers face. Different languages help developers solve different classes of problems effectively. ▪ The C++ language claims to be a versatile programming language. However, versatility doesn't guarantee that specific applications will be implemented quickly and easily. There may be languages that are a better fit for projects than others. An appropriate programming language can help implement a project without significant investments of time and effort. Let’s dive in 6
  5. Terrible tip N2 8 If you need a tab character

    in a string literal, feel free to press the tab key. Save \t ... for somebody else. No worries.
  6. ▪ We’re talking here about string literals where words should

    be separated from each other with a tab character: ▪ Sometimes developers just carelessly press the TAB button, instead of using '\t'. This thing happens in real projects. ▪ Such code compiles and can even work. ▪ But: the reason why the tab character was used won’t be achieved – tab is used to align the table regardless of the length of lines written in it Scenarios 9 const char str[] = "AAA\tBBB\tCCC";
  7. How will the program work without the tab character: 1.

    Depending on the settings, the code editor could insert spaces instead of tabs into the string literal. 2. The developer who maintains the code won't immediately understand what's used as delimiters — tab characters or spaces. 3. During refactoring (or using code autoformatting utilities), tabs may turn into spaces, which affects the result of the program. Consequences 10
  8. Terrible tip N3 12 Use nested macros everywhere. It's a

    good way to shorten code. You will free up hard drive space. Your teammates will have lots of fun when debugging.
  9. Code with macros complicates developers’ lives: 1. Code with macros

    draws bugs to it 2. Reading code becomes complicated 3. Writing macros is difficult 4. Debugging gets complicated 5. Static analysis tools tend to issue additional false positives In reality… 13
  10. ▪ I can recognize the benefits of macros when someone

    needs to generate repetitive code - like in old programs written with usage of MFC. Best practice 14 BEGIN_MESSAGE_MAP(efcDialog, EFCDIALOG_PARENT ) //{{AFX_MSG_MAP(efcDialog) ON_WM_CREATE() ON_WM_DESTROY() //}}AFX_MSG_MAP END_MESSAGE_MAP() Almost at any time, one can write a simple function instead of a macro. C++ offers many tools like templated functions, automatic type inference (auto, decltype), constexpr functions. If we are talking about evaluating expressions at compile-time, macros are not needed and are even harmful. For the same purposes, it is much better and safer to use constexpr.
  11. Terrible tip N4 16 Disable compiler warnings. They distract from

    work and prevent you from writing compact code.
  12. Fixing an error found by a compiler is much easier

    and faster than when debugging code. Once I discovered that some of the big project's components are compiled with warnings completely disabled. The developers could have disabled warnings temporarily – to make fixing compilation errors easier. However, code that compiles with warnings turned off, could work incorrectly. предупреждениями. When going through warnings one can find a number of other errors in code that have been hiding before. What really happens 17
  13. Best practices 18 Aim for zero warnings when compiling code

    – otherwise warnings will pile up and lead to the broken window effect. If there are too many warnings and their number keeps growing, developers will easily overlook a new warning that would report an error. A good approach is to set the compiler to interpret warnings as errors. It shapes the zero-tolerance approach to warnings.
  14. Terrible tip N5 20 Use one or two letters to

    name variables. This way you'll fit a more complex expression on one line on the screen.
  15. Indeed, this will help you write much shorter code. The

    code’s length will be as insignificant as its clarity. Short variable names make code effectively write-only. One could write the code and even debug it – but only while the author remembers what the variable means. Another way to complicate the code is to use abbreviations. Example: ArrayCapacity vs AC How this works 21
  16. When do short variable names help 22 Each approach requires

    thought and moderation. It’s traditional and acceptable to name counters i, j, k. Sometimes abbreviations are a good choice: for example, if the code implements numerical methods, process modeling, etc. Sometimes it’s convenient to give variables short names and then add comments. int n; /**< number of people in cell */ int S, L, I, R, D; /**< S, L, I, R, D are numbers of Susceptible, Latently infected, Infectious, Recovered and Dead people in cell */
  17. Conclusion 24 I hope you enjoyed your time having a

    cup of coffee and reading the terrible tips! You can find a complete list of 60 terrible tips here.