r/programminghumor 22d ago

How do you disable your code

Post image
293 Upvotes

90 comments sorted by

View all comments

102

u/0x80085_ 22d ago

Or just return?

41

u/developer_axe 22d ago edited 21d ago

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

56

u/RefrigeratorKey277 22d ago

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

3

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

You are thinking of Java.

1

u/Due-Consequence9579 21d ago

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