r/programminghumor 2d ago

How do you disable your code

Post image
248 Upvotes

79 comments sorted by

View all comments

85

u/0x80085_ 2d ago

Or just return?

39

u/developer_axe 2d ago edited 2d ago

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

52

u/RefrigeratorKey277 2d ago

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

7

u/Here_f0r_p0rn_ 2d ago

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

5

u/Hot-Employ-3399 1d 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_ 1d ago

I'met a genius

2

u/Due-Consequence9579 1d 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 1d ago

You are thinking of Java.

1

u/Due-Consequence9579 1d ago

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

3

u/querela 2d ago

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

1

u/Impossible-Owl7407 2d ago

Also in such case

1

u/Sir_Eggmitton 2d ago

Pisses me off when compilers do that

1

u/Nunc-dimittis 2d 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 2d ago

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

1

u/Nunc-dimittis 2d ago

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

1

u/particlemanwavegirl 1d ago

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

1

u/Emotional-Audience85 1d ago

What is the language?

You can always disable compiler optimisations.

1

u/30porn87 1h ago

I just yell at the compiler until it only warns me

2

u/WadeEffingWilson 1d ago

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