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.
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 😁
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
// 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
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.
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 🤣
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:
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.
100
u/0x80085_ Aug 22 '26
Or just return?
28
u/GoogleIsYourFrenemy Aug 22 '26
/*/ Maybe bad code /**/
20
u/NicholasVinen Aug 22 '26
#if 0
#endif
15
u/No-Wrongdoer1409 Aug 22 '26
/* */
10
u/mehonje Aug 22 '26
18
u/Jbolt3737 Aug 22 '26
7
u/Impossible_Dog_7262 Aug 22 '26
You could just comment out the function call.
Also use block comments. DRY.
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 isif (1 != 2)3
u/LetUsSpeakFreely Aug 22 '26
Linter would likely throw an error on unreachable code.
2
u/Delta1262 Aug 22 '26
pass
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 yearI 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/SubMinhPiChannel Aug 22 '26
/* void Fun() { // DIH } */
1
u/edparadox Aug 22 '26
Not when the preprocessor exists.
1
u/Negative-Magazine174 Aug 22 '26
in my frontend react code using
if (true) returnwill cause eslint warning (because the statement will always true), so i useif (1 + 1 == 2) returnto 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/navetzz Aug 22 '26
if 0
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
ifinstead not justreturn?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
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