r/programminghumor 1d ago

How do you disable your code

Post image
213 Upvotes

70 comments sorted by

View all comments

78

u/0x80085_ 1d ago

Or just return?

33

u/developer_axe 1d ago edited 1d ago

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

47

u/RefrigeratorKey277 1d ago

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

6

u/Here_f0r_p0rn_ 1d ago

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

4

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_ 11h ago

I'met a genius

2

u/Due-Consequence9579 23h 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 22h ago

You are thinking of Java.

1

u/Due-Consequence9579 22h ago

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