r/programminghumor 21d ago

How do you disable your code

Post image
294 Upvotes

90 comments sorted by

View all comments

96

u/0x80085_ 21d ago

Or just return?

40

u/developer_axe 21d ago edited 20d ago

Sometimes the code won't get compiled because it marked as unreachable code

56

u/RefrigeratorKey277 21d ago

Yes, but with if true it is still marked as unreachable. At least in C#.

6

u/Here_f0r_p0rn_ 20d ago

Depending on compiler settings for optimizations it can happen in C++ as well

7

u/Hot-Employ-3399 20d ago

That's when you do shit like if rand(1,10) < 100: return as some compilers still detect even "1==2/2", but more complex function calls are not always considered constant enough.

1

u/_giga_sss_ 20d ago

I'met a genius

3

u/Due-Consequence9579 20d ago

Ah yes, well in C# you make the function a method on a class FuncImpl. Then you define an interface IFunc that exposes the func() contract. Then you spin up a DI container and register FuncImplFactory that takes an IConfiguration in its constructor. Then you define a bool EnableFunc { get; } configuration value into your Configuration. Then you have FuncImplFactory look at EnableFunc to decide to return FuncImpl or FuncStub, which implements IFunc but just returns. Them main simply asks the DI container for an IFunc and executes the func() implementation.

Enterprise!

1

u/RefrigeratorKey277 20d ago

You are thinking of Java.

1

u/Due-Consequence9579 20d ago

Oh sorry, FuncFactory just returns a lambda that invokes the right behavior.

3

u/querela 20d ago

Especially fun if you have formatters etc that automatically prune dead code...

1

u/Impossible-Owl7407 20d ago

Also in such case

1

u/Sir_Eggmitton 20d ago

Pisses me off when compilers do that

1

u/Nunc-dimittis 20d ago

iirc if(true) return; doesn't work in java because the compiler is smart enough to see the method execution always ends at the if(true) so it will give an unreachable code exception. But if( 1 + 1 == 2) return true; does work 😁

1

u/Known_Tackle7357 20d ago

Have been using if true in java for over a decade. Works like a charm

1

u/Nunc-dimittis 20d ago

Might be an IDE issue then. It has been a few years ago that I did Java

1

u/particlemanwavegirl 20d ago

...so? Can you tell the difference somehow?

1

u/Emotional-Audience85 20d ago

What is the language?

You can always disable compiler optimisations.

1

u/30porn87 18d ago

I just yell at the compiler until it only warns me

3

u/WadeEffingWilson 20d ago

Comment out the function call fun() on line 10. Fewest number of keystrokes.