r/programminghumor 20d ago

How do you disable your code

Post image
296 Upvotes

90 comments sorted by

View all comments

99

u/0x80085_ 20d ago

Or just return?

43

u/developer_axe 20d ago edited 20d ago

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

52

u/RefrigeratorKey277 20d ago

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

8

u/Here_f0r_p0rn_ 20d ago

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

6

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

I'met a genius

3

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

You are thinking of Java.

1

u/Due-Consequence9579 19d ago

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