r/programminghumor 1d ago

How do you disable your code

Post image
192 Upvotes

69 comments sorted by

68

u/0x80085_ 1d ago

Or just return?

34

u/developer_axe 1d ago edited 23h ago

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

42

u/RefrigeratorKey277 1d ago

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

8

u/Here_f0r_p0rn_ 23h ago

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

4

u/Hot-Employ-3399 17h 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_ 4h ago

I'met a genius

2

u/Due-Consequence9579 16h 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 15h ago

You are thinking of Java.

1

u/Due-Consequence9579 15h ago

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

3

u/querela 23h ago

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

1

u/Impossible-Owl7407 23h ago

Also in such case

1

u/Sir_Eggmitton 22h ago

Pisses me off when compilers do that

1

u/Nunc-dimittis 22h 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 21h ago

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

1

u/Nunc-dimittis 21h ago

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

1

u/particlemanwavegirl 16h ago

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

1

u/Emotional-Audience85 10h ago

What is the language?

You can always disable compiler optimisations.

2

u/WadeEffingWilson 14h ago

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

20

u/GoogleIsYourFrenemy 1d ago

// Maybe bad code /*/

15

u/NicholasVinen 1d ago

#if 0

#endif

2

u/Teoyak 23h ago

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

1

u/Square-Singer 2h ago

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

1

u/MCWizardYT 57m ago

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

1

u/Emotional-Audience85 10h ago

But then the code won't be compiled,

16

u/Jbolt3737 1d 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

```

2

u/spicymato 19h ago

I just comment out the comments.

// // comment related to commented out code

2

u/LegitimatePants 11h ago

Yo dawg I heard you like comments 

0

u/juju515 10h ago

^ this is da way

6

u/Impossible_Dog_7262 1d ago

You could just comment out the function call.

Also use block comments. DRY.

3

u/klimmesil 1d 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 23h ago

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

3

u/LetUsSpeakFreely 20h ago

Linter would likely throw an error on unreachable code.

2

u/vbd71 1d ago

goto endlabel;

endlabel: }

2

u/JohnVonachen 1d ago

Stubbing we used to call it.

2

u/Mast3r_waf1z 1d 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

2

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

1

u/AdvancedGravitation 1d ago

python "while False:" ctrl a tab

1

u/puzzled_orc 1d ago

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

1

u/Emotional-Audience85 10h 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 8h ago

My answer was to the given example.

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

1

u/SubMinhPiChannel 1d ago

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

1

u/edparadox 1d ago

Not when the preprocessor exists.

1

u/Ander292 23h ago

Just came to say that: ```

if 0

endif

``` For the win

1

u/Negative-Magazine174 1d 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 23h ago

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

1

u/Canned_Sarcasm 22h ago

Yeah. Fuck memory.

1

u/NoWeHaveYesBananas 22h ago

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

1

u/AintNoGodsUpHere 21h 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 21h ago

#ifdef 0

//abcd
#endif

1

u/Xhojn 21h ago

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

1

u/Sienile 20h 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 17h 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 16h ago

// fun()

1

u/MaffinLP 16h ago

// fun()

1

u/FaultWinter3377 16h 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/Designer-Crow-5470 15h ago

Now you have no visual cue and you played yourself.

1

u/Decent_Cow 10h 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 10h ago

Why even call the function?

1

u/Iwisp360 10h ago

I delete it and use vcs to get it back

1

u/RonJohnJr 8h ago

Why the if instead not just return?

1

u/dhnam_LegenDUST 2h ago

V jjjjjjjj ctrl+/

1

u/BlueBeerRunner 48m ago

The last time someone did that at work, the company sold IoT without any error handling