Reply to comment

roger's picture

Coding Standards: Non-const reference parameters in C++ are evil

This is just a minor rant:

In C++, const references are useful for parameters, because they avoid copying the arguments to a method. Non-const references, on the other hand, are pure evil, because there’s no way (at the call site) to immediately see that a method might change a variable:

int expectedVersion = 1;
ValidateVersion(expectedVersion);

By looking at this, you can’t tell that ValidateVersion is declared as void ValidateVersion(int &version) and might actually change the value.

Use a pointer instead:

ValidateVersion(&expectedVersion);

Then, when I’m looking at the call site, I have an expectation that the value might change.

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <b> <blockquote> <br> <code> <dd> <dl> <dt> <hr> <h1> <h2> <h3> <i> <img> <li> <ol> <p> <pre> <table> <td> <th> <tr> <tt> <u> <ul>
  • Images can be added to this post.

More information about formatting options