←back to thread

177 points signa11 | 1 comments | | HN request time: 1.984s | source
Show context
chipdart ◴[] No.42162556[source]
From the article:

> This is painful because I am an experienced C++ programmer, and C++ has this exact problem except worse: undefined behavior. In the worst case, C++ simply doesn’t check anything, compiles your code wrong, and then does inexplicable and impossible things at runtime for no discernable reason (or it just deletes your entire function).

This is completely wrong, even in the "not even wrong" territory. It reads like an attempt to parrot a cliche without having any idea what it means. "Undefined behavior" just means the standard does not define what is the expected behavior, and purposely leaves implementations free to implement it how they see fit. This means crashing the app or sending an email to the pope.

In practical terms this means developers should not write code that triggers undefined behavior, and treat the code that does as errors requiring a fix. Advanced users can lean on implementation-defined behavior from compilers to add some expectation to the behavior, but that's discouraged.

It's so strange how someone calling themselves a seasoned C++ developer fails to understand such a basic aspect of the language.

The important tidbit is that a) it's completely wrong to parrot "undefined behavior" on "C++ doesn't check anything", and b) if you code triggers undefined behavior without your knowledge then you just broke the code and wrote a bug out of your own ignorance.

To make matters worse, there are a myriad of code checkers for C++ that catch undefined behavior and even some classes of safety errors. Take for instance cppcheck. Why is the blogger whining about undefined behavior and "c++ not checking" when adding cppcheck to any project is enough to detect most if not all cases?

replies(1): >>42162597 #
throw_a_grenade ◴[] No.42162597[source]
The quote is incomplete without its continuation:

> This means that in order to write C++, you effectively have to memorize the undefined behavior rules, which sucks. Sound familiar?

Which is the point TFA is making. I believe you expended the attention span a bit too early.

replies(1): >>42162730 #
chipdart ◴[] No.42162730[source]
> Which is the point TFA is making.

Again, the point is wrong in more than one way.

You don't simply add undefined behavior. It's wrong, and buggy code. Onboard a static code analysis tool like cppcheck to tell you when you messed up.

It takes far less work to onboard any of these tools than it takes to write a sentence in a blog post.

replies(1): >>42162899 #
1. throw_a_grenade ◴[] No.42162899[source]
TFA argues developers add it left and right, unless someone memorised all the rules. Since reportedly it's not possible to memorise all the rules by a single human, then you either "simply add undefined behaviour", or limit yourself to a subset of C++ that programmers do actually understand. Which is a solution I see in many codebases: to limit a set of permissible constructs by a style manual.