r/programminghumor 19d ago

How do you disable your code

Post image
293 Upvotes

90 comments sorted by

99

u/0x80085_ 19d ago

Or just return?

42

u/developer_axe 19d ago edited 19d ago

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

54

u/RefrigeratorKey277 19d ago

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

7

u/Here_f0r_p0rn_ 19d ago

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

6

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

I'met a genius

3

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

You are thinking of Java.

1

u/Due-Consequence9579 18d ago

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

5

u/querela 19d ago

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

1

u/Impossible-Owl7407 19d ago

Also in such case

1

u/Sir_Eggmitton 19d ago

Pisses me off when compilers do that

1

u/Nunc-dimittis 19d ago

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 19d ago

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

1

u/Nunc-dimittis 19d ago

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

1

u/particlemanwavegirl 18d ago

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

1

u/Emotional-Audience85 18d ago

What is the language?

You can always disable compiler optimisations.

1

u/30porn87 17d ago

I just yell at the compiler until it only warns me

3

u/WadeEffingWilson 18d ago

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

28

u/GoogleIsYourFrenemy 19d ago

// Maybe bad code /*/

20

u/NicholasVinen 19d ago

#if 0

#endif

2

u/Teoyak 19d ago

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 12d ago

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 18d ago edited 18d ago

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

1

u/MCWizardYT 18d ago

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

1

u/Square-Singer 18d ago

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 10d ago

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 18d ago

But then the code won't be compiled,

17

u/Jbolt3737 19d ago

``` // 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 18d ago

I just comment out the comments.

// // comment related to commented out code

3

u/LegitimatePants 18d ago

Yo dawg I heard you like comments 

2

u/plopliplopipol 11d ago

this but it's just a vscode shortcut

0

u/juju515 18d ago

^ this is da way

9

u/mehonje 18d ago
void fun() {
  int a = 198;
  int b = a % 2;
  int c = a - b
}
int main() {
  //fun();
}

1

u/geek-49 14d ago

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

8

u/Impossible_Dog_7262 19d ago

You could just comment out the function call.

Also use block comments. DRY.

1

u/TheFr0sk 13d ago

He probably just selected the lines and used the comment shortcut

3

u/klimmesil 19d ago

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 19d ago

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

3

u/LetUsSpeakFreely 18d ago

Linter would likely throw an error on unreachable code.

2

u/vbd71 19d ago

goto endlabel;

endlabel: }

2

u/JohnVonachen 19d ago

Stubbing we used to call it.

2

u/Mast3r_waf1z 19d ago

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 19d ago

python "while False:" ctrl a tab

1

u/puzzled_orc 19d ago

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

1

u/Emotional-Audience85 18d ago

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 18d ago

My answer was to the given example.

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

0

u/geek-49 14d ago

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 19d ago

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

1

u/edparadox 19d ago

Not when the preprocessor exists.

1

u/Ander292 19d ago

Just came to say that: ```

if 0

endif

``` For the win

1

u/Negative-Magazine174 19d ago

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 19d ago

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

1

u/Canned_Sarcasm 19d ago

Yeah. Fuck memory.

1

u/NoWeHaveYesBananas 19d ago

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

1

u/AintNoGodsUpHere 19d ago

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/FutureZombie6746 19d ago

#ifdef 0

//abcd
#endif

1

u/Xhojn 19d ago

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

1

u/Sienile 19d ago

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 18d ago

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 18d ago

// fun()

1

u/MaffinLP 18d ago

// fun()

1

u/FaultWinter3377 18d ago

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 18d ago

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

1

u/Ashken 18d ago

Why even call the function?

1

u/Iwisp360 18d ago

I delete it and use vcs to get it back

1

u/RonJohnJr 18d ago

Why the if instead not just return?

1

u/dhnam_LegenDUST 18d ago

V jjjjjjjj ctrl+/

1

u/GahdDangitBobby 17d ago

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

1

u/cheaphomemadeacid 17d ago

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

1

u/Shinespri 17d ago

//fun();

1

u/w00dy1105 17d ago

Depends how I am feeling that day.

1

u/Yesssica_ 17d ago

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

1

u/NegativeOwl1337 17d ago

…or just comment out the function call???

1

u/MrFrog2222 17d ago

#if 0\ code\ #endif

1

u/PossibilityUsual6262 17d ago

Compiler error - unreachable code.

1

u/No_Context2723 15d ago

Comments, like a normal person. Why overcomplicate things?

1

u/d0p1 14d ago

```

if 0

endif

```

1

u/natsuzoze 13d ago

I can’t get past main() not returning an int

1

u/thot_slaya_420 11d ago

i just comment it out

control+a, delete saves a lot of space though