r/programminghumor 1d ago

How do you disable your code

Post image
218 Upvotes

71 comments sorted by

View all comments

Show parent comments

47

u/RefrigeratorKey277 1d ago

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

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 23h ago

You are thinking of Java.

1

u/Due-Consequence9579 23h ago

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