top | item 47044202

(no title)

BeeOnRope | 13 days ago

Dead code is extremely common in C or C++ after inlining, other optimizations.

discuss

order

dotancohen|13 days ago

Or stubs. I'll often flesh out a class before implementing the methods.

duped|13 days ago

If dead code (1) is common in your codebase then your code base is missing heaps of refactors

(1) "dead" meaning unused types, unreachable branches

muldvarp|13 days ago

Not really, no. If you use a regex library it is very likely that 80% of that code is effectively dead code.

catlifeonmars|13 days ago

OP means that the code has a dual purpose: one purpose is to be compiled, the other is to communicate structure or intent to programmers.

deathanatos|13 days ago

Do we know that? I've written "dead" code. It's point was to communicate structure or intent, but it was also still dead. This pattern, in one form or another, crops up a lot IME (in multiple languages, even, with varying abilities to optimize it):

  if condition that is "always" false:
    abort with message detailing the circumstances
That `if` is "dead", in the sense that the condition is always false. But "dead" sometimes is just a proof — or if I'm not rigourous enough, an assumption — in my head. If the compiler can prove the same proof I have in my head, then the dead code is eliminated. If can't, well, presumably it is left in the binary, either to never be executed, or to be executed in the case that the proof in my head is wrong.

somat|13 days ago

That's the problem

BeeOnRope|13 days ago

Why is that a problem? Inlining and optimization aren't minor aspects of compiling to native code, they are responsible for order-of-magnitude speedups.

My point is that it is easy to say "don't remove my code" while looking at a simple single-function example, but in actual compilation huge portions of a function are "dead" after inlining, constant propagation and other optimizations: not talking anything about C-specific UB or other shenanigans. You don't want to throw that out.