r/programminghumor • • Aug 22 '26

How do you disable your code

/img/ng4rgeq6kvkh1.jpeg
297 Upvotes

91 comments sorted by

View all comments

101

u/0x80085_ Aug 22 '26

Or just return?

43

u/[deleted] Aug 22 '26 edited Aug 22 '26

[removed] — view removed comment

56

u/RefrigeratorKey277 Aug 22 '26

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

7

u/Here_f0r_p0rn_ Aug 22 '26

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

6

u/[deleted] Aug 22 '26

[removed] — view removed comment

1

u/_giga_sss_ Aug 23 '26

I'met a genius

3

u/Due-Consequence9579 Aug 22 '26

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 Aug 22 '26

You are thinking of Java.

1

u/Due-Consequence9579 Aug 22 '26

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

4

u/querela Aug 22 '26

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

1

u/Impossible-Owl7407 Aug 22 '26

Also in such case

1

u/Sir_Eggmitton Aug 22 '26

Pisses me off when compilers do that

1

u/Nunc-dimittis Aug 22 '26

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 Aug 22 '26

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

1

u/Nunc-dimittis Aug 22 '26

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

1

u/particlemanwavegirl Aug 22 '26

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

1

u/Emotional-Audience85 Aug 23 '26

What is the language?

You can always disable compiler optimisations.

1

u/30porn87 Aug 24 '26

I just yell at the compiler until it only warns me

4

u/WadeEffingWilson Aug 22 '26

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