C++ Core Guidelines: Non-Rules and Myths

C++ Core Guidelines: Non-Rules and Myths

This is a cross-post from www.ModernesCpp.com.

Of course, you already know many non-rules and myths to C++. Non-rules and myths which we have to argument against when we use modern C++. The supporting section of the C++ core guidelines addresses the most resistant once but also provides alternatives.

Here are the rules for today.

Many programmers apply the first rules.

NR.1: Don’t: All declarations should be at the top of a function

This rule is a relict of old programming languages that don't allow initialisation of variables and constants after a statement. The result of a significant distance of the variable declaration and their usage is often, that you forget to initialise the variable. Exactly this happens in the example of the C++ core guidelines:

int use(int x)
{  
    int i;
    char c;
    double d;
    // ... some stuff ...if (x < i) {// ...
       i = f(x, d);
    }
    if (i < x) {
        // ...
        i = g(x, c);
    }
    return ;
}

I assume you already found the issue in this codesnippet. The variable i (the same holds for c and d) is not initialised because it is a built-in variable used in a local scope and, therefore, the program has undefined behaviour. If i would be a user-defined type such as std::string, all would be fine. So, what should you do:

  • Place the declaration of i directly before its first usage.
  • Always initialise a variable such as int i{}, or better, use auto. The compiler can not guess from a declaration such as auto i; the type of i and will, therefore, reject the program. To put it the other way around auto forces you to initialise your variables.

I also know the next rule from discussions.

NR.2: Don’t: Have only a single return-statement in a function

When you follow this rule, you implicitly apply the first non-rule.

template<class T>
std::string sign(T x)    // bad
{
    std::string res;   
    if (x < 0)
        res = "negative";
    else if (x > 0)
        res = "positive";
    else
        res = "zero";
    return res;
}

Using more return statements make the code easier to read and also faster.

template<class T>
std::string sign(T x)
{
    if (x < 0)
        return "negative";
    else if (x > 0)
        return "positive";
    return "zero";
}

 Okay. What happens if I use automatic return type deduction return different types?

// differentReturnTypes.cpp

template <typename T>
auto getValue(T x){
  if (x < 0)          // int
    return -1;
else if (x > 0)
    return 1.0;       // double
else 
    return 0.0f;      // float
}

int main(){

    getValue(5.5);
 
}  

As expected, just an error:

No alt text provided for this image

Probably, the next non-rule is the most controversial one.

NR.3: Don’t: Don’t use exceptions

First, the guideline states the main reasons against exceptions:

  1. exceptions are inefficient
  2. exceptions lead to leaks and errors
  3. exception performance is not predictable

The guidelines have profound answers to these statements.

1. Often the efficiency of exception handling is compared to a program that just terminated, or displays the error code. Often the exception handling implementation is poor. Of course, a comparison makes in such cases no sense. I want to explicitly mention the paper Technical Report on C++ Performance (TR18015.pdf), which presents two typical ways to implement exceptions:

  1. The code approach, where code is associated with each try-block
  2. The "table" approach, which uses compiler-generated static tables

Roughly said, the "code" approach has the downside that even when no exceptions are thrown, the bookkeeping of the exception handling stack must be performed and, therefore, code unrelated to error handling slows down. This downside does not hold for the "table" approach, because it introduces not stack or runtime costs when no exception is thrown. In contrast, the "table" approach seems more complicated to implement, and the static table can get quite big.

2. I have nothing to add to point 2. Exceptions can not be blamed for a missing resource management strategy.

3. If you have hard-realtime guarantees to fulfil so that a too late answer is a wrong answer, an exception implementation based on the "table" approach will not - as we saw - affect the run-time of the program in the good case. Honestly, even if you have a hard-realtime system, this hard-realtime restriction typically only hold for small part of your system. 

Instead of arguing against the non-rules, here are the reasons for using exceptions:

Exceptions

  • clearly differentiate between erroneous return and ordinary return.
  • cannot be forgotten or ignored.
  • can be used systematically.

Let me add an anecdote I once faced in legacy code. The system used error codes to signal the success or failure of a function. Okay, they checked the error codes. This was fine. But due to the error codes, the functions didn't use return values. The consequence was, that the functions operated on global variables and, consequently had no parameters because they used the global variables anyway. The end of the story was that the system was not maintainable or testable, and my job was it to refactor it. 

The typical wrong usage of exceptions handling I see is the following one. You catch every exception in every function. In the end, you get unmaintainable code with a spaghetti-like structure. Exceptions are not a tool to make a fast fix but should be part of the overall system architecture. Imagine, you design an input sub-system. You must also document and test the exceptions that can occur. Exceptions are an essential part of the non-functional channel and, therefore, part of the contract that you provide to the user of your sub-system. You need a clear boundary between the application and the sub-system. The result may be that the sub-system translates the obscure exceptions into simpler once so that the application can react. Translating an exception means that you catch obscure exceptions in the sub-system and re-throw an easy do digest exception:

try{
     // code, that may throw an obscure exception
  }   
  catch (ObscureException18& ob){
      throw InputSubsystemError("File has wrong permissions!");
}

The result of such a system architecture including the non-functional channel (exceptions) is, that you can test the sub-system in isolation, that you can test the integration of the sub-system into the application, and that you can test the system (application). 

The last myth for today is quite easy to spot.

NR.4: Don’t: Place each class declaration in its own source file

The correct way to structure your code is not to use files; the correct way is it to use namespaces. Using a file for each class declarations result in many files and can make your program, therefore, harder to manage and slower to compile.

What's next?

You can be sure. The C++ core guidelines and I'm not done with the non-rules and myths to C++. I will continue in my next post. Afterwards, when you encounter the non-rules and myths you should know how to demystify them.

Konstantinos Monachopoulos

Senior Manager - Applied Machine Learning at Arm

5 年

helpful hints , thank you!

Kenneth Goodwin

Computer Scientist

5 年

Nr1, neither are variables c and d initialized in this example.

要查看或添加评论,请登录

Rainer Grimm的更多文章

  • My ALS Journey (17/n): Christmas Special

    My ALS Journey (17/n): Christmas Special

    Today, I have a special Christmas gift. My ALS Journey so far Make the Difference Let’s do something great together:…

  • std::execution

    std::execution

    std::execution, previously known as executors or Senders/Receivers, provides “a Standard C++ framework for managing…

    1 条评论
  • C++26 Core Language: Small Improvements

    C++26 Core Language: Small Improvements

    There are more small improvements in the C++26 language, which you should know. static_assert extension First, here’s…

  • My ALS Journey (16/n): Good Bye Training / Hello Mentoring

    My ALS Journey (16/n): Good Bye Training / Hello Mentoring

    In 2025, I will no longer offer C++ classes. Instead, I will only offer C++ mentoring in the future.

    1 条评论
  • Placeholders and Extended Character Set

    Placeholders and Extended Character Set

    Placeholders are a nice way to highlight variables that are no longer needed. Additionally, the character set of C++26…

    4 条评论
  • Contracts in C++26

    Contracts in C++26

    Contracts allow you to specify preconditions, postconditions, and invariants for functions. Contracts should already be…

    5 条评论
  • Mentoring as a Key to Success

    Mentoring as a Key to Success

    I know that we are going through very challenging times. Saving is the top priority.

  • Reflection in C++26: Determine the Layout

    Reflection in C++26: Determine the Layout

    Thanks to reflection, you can determine the layout of types. My examples are based on the reflection proposal P2996R5.

    5 条评论
  • My ALS Journey (15/n): A typical Day

    My ALS Journey (15/n): A typical Day

    You may wonder how my day looks. Let me compare a day before ALS with a current day.

    3 条评论
  • Reflection in C++26: Metafunctions for Enums and Classes

    Reflection in C++26: Metafunctions for Enums and Classes

    Today, I continue my journey through reflection in C++26 and play with enums and classes. The names of the…

    2 条评论

社区洞察

其他会员也浏览了