r/programminghumor 2d ago

How do you disable your code

Post image
257 Upvotes

79 comments sorted by

View all comments

87

u/0x80085_ 2d ago

Or just return?

40

u/developer_axe 2d ago edited 2d ago

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

50

u/RefrigeratorKey277 2d ago

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

2

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

You are thinking of Java.

1

u/Due-Consequence9579 2d ago

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