With some programming languages, a compiler may reject code that, while otherwise syntactically valid, is readily determined to be logically invalid through static analysis.

An example is Visual C++ giving a compile-time error if a program contains a constant division by zero. While division by zero is undefined under the C++ standard, meaning that the compiler and program can technically do anything in a situation like this, GCC and Clang do not consider this a compile-time error and will produce a warning instead. (Indeed, this question was inspired by a Stack Overflow question on whether it's better to return an error instead of a warning when a C++ compiler encounters a constant division by zero.)

Other examples include C# prohibiting statements that do not do anything ("Only assignment, call, increment, decrement, and new object expressions can be used as a statement") and Java treating many types of unreachable code as compile-time errors ("It is a compile-time error if a statement cannot be executed because it is unreachable.").

In all of these cases, the code is otherwise syntactically valid and can technically be compiled, but the compiler refuses to do so.

Is it better to completely reject these sorts of logical mistakes in code at compile time, or to compile the program anyway and output warnings instead? In other words, should the compiler shield the programmer from obvious mistakes by refusing to compile code that is almost certainly logically wrong, or should it just compile any syntactically-valid code as a matter of principle, warning the programmer if it runs into suspicious code?

1 Answer 1


Well, I suppose this is UX design for the not-so-small number of users who are professional software developers...

Let me first state that a programming language states what is legal code and what is illegal code. Finding code that is defined to be illegal goes far beyond merely checking the syntax. And with illegal code, the compiler has no choice: It must flag the illegal code as illegal. That decision is not up to the compiler.

"Logically incorrect" is very, very hard to detect. What you can detect in many cases is code that is most likely not what the user wanted. Sometimes it is what the user want, even though the code looks suspicious to the compiler. Such code may produce a warning.

Compilers usually handle it like this: a. It is configurable what situations actually create a warning. It's quite pointless if the user / software developer is confronted with thousands of warnings when compiling old code with a new compiler. And some programming style might regularly and knowingly do what the compiler considers worth a warning. b. It may be configurable which warnings produce errors that cannot be ignored. Most likely it is configurable that all warnings produce errors. c. There may be provision to turn specific warnings off in the source code. This is often a bit clumsy, which is Ok because this isn't supposed to be used a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.