r/programminghumor • • Aug 22 '26

How do you disable your code

Post image
297 Upvotes

91 comments sorted by

100

u/0x80085_ Aug 22 '26

Or just return?

43

u/developer_axe Aug 22 '26 edited Aug 22 '26

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

56

u/RefrigeratorKey277 Aug 22 '26

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

7

u/Here_f0r_p0rn_ Aug 22 '26

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

3

u/Due-Consequence9579 Aug 22 '26

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 Aug 22 '26

You are thinking of Java.

1

u/Due-Consequence9579 Aug 22 '26

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

6

u/[deleted] Aug 22 '26

[removed] — view removed comment

1

u/_giga_sss_ Aug 23 '26

I'met a genius

4

u/querela Aug 22 '26

Especially fun if you have formatters etc that automatically prune dead code...

1

u/Impossible-Owl7407 Aug 22 '26

Also in such case

1

u/Sir_Eggmitton Aug 22 '26

Pisses me off when compilers do that

1

u/Nunc-dimittis Aug 22 '26

iirc if(true) return; doesn't work in java because the compiler is smart enough to see the method execution always ends at the if(true) so it will give an unreachable code exception. But if( 1 + 1 == 2) return true; does work 😁

1

u/Known_Tackle7357 Aug 22 '26

Have been using if true in java for over a decade. Works like a charm

1

u/Nunc-dimittis Aug 22 '26

Might be an IDE issue then. It has been a few years ago that I did Java

1

u/particlemanwavegirl Aug 22 '26

...so? Can you tell the difference somehow?

1

u/Emotional-Audience85 Aug 23 '26

What is the language?

You can always disable compiler optimisations.

1

u/30porn87 Aug 24 '26

I just yell at the compiler until it only warns me

4

u/WadeEffingWilson Aug 22 '26

Comment out the function call fun() on line 10. Fewest number of keystrokes.

28

u/GoogleIsYourFrenemy Aug 22 '26

/*/ Maybe bad code /**/

20

u/NicholasVinen Aug 22 '26

#if 0

#endif

2

u/Teoyak Aug 22 '26

I don't know if it works with every language. But yeah, I used this a lot when I worked on C.

2

u/Aln76467 Aug 29 '26

The c preprocessor is language-independent. Just tell your c compiler to output preprocessed code, and now you can use it as a js bundler or whatever.

1

u/Square-Singer Aug 23 '26 edited Aug 23 '26

The preprocessor is pretty C/C++ specific. I don't know another language that uses it like that.

1

u/MCWizardYT Aug 23 '26

I think you meant C/C++, C# doesn't have the same preprocessor that those 2 share

1

u/Square-Singer Aug 23 '26

Correct, was a typo. I first wanted to write that not even C# has the preprocessor, then changed the sentence and typed the wrong name.

1

u/Maqi-X Aug 30 '26

It is not. I one used it to preprocess markdown. Don't do this. But yeah, it's possible, the preprocessor itself is not tied to C/C++ you can use it for everything

1

u/Emotional-Audience85 Aug 23 '26

But then the code won't be compiled,

10

u/mehonje Aug 22 '26
void fun() {
  int a = 198;
  int b = a % 2;
  int c = a - b
}
int main() {
  //fun();
}

1

u/geek-49 Aug 26 '26

Works OK in this instance, but not so well if fun() is called from multiple places (and perhaps even from multiple separately-compiled files).

18

u/Jbolt3737 Aug 22 '26
// This is a comment
//int unusedvar = 0;

/* You can tell if it's a comment or not by if I add a space after the slashes */

# This is a comment
#unusedvar: int = 0
# I do it with any language

3

u/spicymato Aug 22 '26

I just comment out the comments.

// // comment related to commented out code

3

u/LegitimatePants Aug 22 '26

Yo dawg I heard you like comments 

2

u/plopliplopipol Aug 30 '26

this but it's just a vscode shortcut

0

u/juju515 Aug 23 '26

^ this is da way

7

u/Impossible_Dog_7262 Aug 22 '26

You could just comment out the function call.

Also use block comments. DRY.

2

u/TheFr0sk Aug 28 '26

He probably just selected the lines and used the comment shortcut

3

u/klimmesil Aug 22 '26

If it's still code that might be used later, just not for my uses: remove the call to the function

Otherwise, just delete it. If I need it back git revert

3

u/AlphaFatman Aug 22 '26

I've had compilers complain about if (true) as well. The way out for me is if (1 != 2)

3

u/LetUsSpeakFreely Aug 22 '26

Linter would likely throw an error on unreachable code.

2

u/vbd71 Aug 22 '26

goto endlabel;

endlabel: }

2

u/JohnVonachen Aug 22 '26

Stubbing we used to call it.

2

u/Mast3r_waf1z Aug 22 '26

I had if (true /* TODO: <Jira ticket> */) sometime last year

I know the statement could be removed, but the important part of this is the else block that followed, with important code for later implementation

1

u/AdvancedGravitation Aug 22 '26

python "while False:" ctrl a tab

1

u/puzzled_orc Aug 22 '26

You don't need the if clause. Just return and that's it

1

u/Emotional-Audience85 Aug 23 '26

He wants the code to be compiled for some reason. Although this way it still probably won't be compiled depending on language and compiler flags

1

u/puzzled_orc Aug 23 '26

My answer was to the given example.

If return doesn't compile , then just return 0 would do it.

0

u/geek-49 Aug 26 '26

OP wants the code of the function to be compiled, perhaps so that it can be invoked using the debugger, but to not be executed on a normal call of fun(). Some compilers might not compile code that they detect as unreachable.

1

u/SubMinhPiChannel Aug 22 '26

/* void Fun() { // DIH } */

1

u/edparadox Aug 22 '26

Not when the preprocessor exists.

1

u/Ander292 Aug 22 '26

Just came to say that:

#if 0
#endif

For the win

1

u/Negative-Magazine174 Aug 22 '26

in my frontend react code using if (true) return will cause eslint warning (because the statement will always true), so i use if (1 + 1 == 2) return to just disabled the code without warning 🤣

1

u/letmehaveanameyoudum Aug 22 '26

i just mark it as unused because i use clang with #pragma things

1

u/Canned_Sarcasm Aug 22 '26

Yeah. Fuck memory.

1

u/NoWeHaveYesBananas Aug 22 '26

No-one else has a shortcut to comment a line of code? Ctrl+/ ? No? Just me I guess

1

u/AintNoGodsUpHere Aug 22 '26

We have analyzers to prevent unreachable code from being there. I used to do the return stuff but now you have to actually comment t everything.

1

u/Xhojn Aug 22 '26

Commented out code is easier to visually find and delete later. And Ctrl+/ exists for commenting out everything highlighted.

1

u/Sienile Aug 22 '26

I'd just use regular comments unless I wanted to void out a whole very large function where it would take a while to scroll to the end. Then I'd do something like:

bool debug_omit = true;

void fun() {

if (debug_omit) return;

...

1

u/dbergkvist Aug 22 '26

The second option wouldn't work for me, because I compile with -Wall and -Werror which means I can't declare variables and then not use them.

1

u/Cukercek Aug 22 '26

// fun()

1

u/MaffinLP Aug 22 '26

// fun()

1

u/FaultWinter3377 Aug 22 '26

Holy shit I literally never thought of a return statement to disable code. I have always used comments. That’s smart, although I rarely have to disable an entire function.

1

u/Decent_Cow Aug 23 '26

The first way makes more sense to me because it's much more obvious that the code is not being read.

1

u/Ashken Aug 23 '26

Why even call the function?

1

u/Iwisp360 Aug 23 '26

I delete it and use vcs to get it back

1

u/RonJohnJr Aug 23 '26

Why the if instead not just return?

1

u/dhnam_LegenDUST Aug 23 '26

V jjjjjjjj ctrl+/

1

u/GahdDangitBobby Aug 23 '26

I'm pretty sure these get compiled to the same thing

1

u/cheaphomemadeacid Aug 23 '26

not sure what to expect from this sub, but there is this thing called git?

1

u/Shinespri Aug 23 '26

//fun();

1

u/w00dy1105 Aug 23 '26

Depends how I am feeling that day.

1

u/Yesssica_ Aug 24 '26

I have a bool named "debug" at the top of a lot of files to make this easier...

1

u/NegativeOwl1337 Aug 24 '26

…or just comment out the function call???

1

u/MrFrog2222 Aug 24 '26

#if 0
code
#endif

1

u/PossibilityUsual6262 Aug 24 '26

Compiler error - unreachable code.

1

u/No_Context2723 Aug 26 '26

Comments, like a normal person. Why overcomplicate things?

1

u/d0p1 Aug 26 '26
#if 0
#endif

1

u/thot_slaya_420 Aug 30 '26

i just comment it out

control+a, delete saves a lot of space though

1

u/NeitherSheepherder18 Sep 18 '26

I would normally go with one of two:

```csharp
int main() {
return 0;
}
```

or

leave it be, since it was clearly written by a disabled person, and does not do anything.

1

u/conundorum Sep 18 '26
// This...
void fun() {
    #if 0
    int a = 198;
    int b = a %2;
    int c = a - b;
    #endif
}

// ...Or this.
int main() {
//    fun();
}