this post was submitted on 21 Nov 2023
422 points (96.9% liked)

Programmer Humor

31434 readers
1647 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
all 33 comments
sorted by: hot top controversial new old
[โ€“] lobsticle 60 points 8 months ago (3 children)

Very disappointing not to see an #if 0 (my personal go-to for decades) in this meme. ๐Ÿ˜ž

[โ€“] SpaceNoodle 24 points 8 months ago (1 children)

Damn, you beat me to it.

It's common enough that it's supported like a comment by numerous syntax highlighting schemes, and has the added benefits of guaranteeing that the code won't be compiled as well as encapsulating any pre-existing block comments. Conversely, if (false) is total garbage.

[โ€“] Duralf 13 points 8 months ago (3 children)

If (false) is good because it is compiled so it doesn't get stale.

[โ€“] AlmightySnoo 6 points 8 months ago* (last edited 8 months ago) (1 children)

A simple if (false) will get optimized out by any modern C or C++ compiler with optimizations on, but the problem is that the compiler will still parse and spend time on what's inside the if-block and it has to be legal code, whereas with the #if 0 trick the whole thing gets yeeted away by the preprocessor before even the compiler gets to look at it regardless of whether that block contains errors or not, it's literally just a string manipulation.

[โ€“] Duralf 10 points 8 months ago (1 children)

I think you missed the whole point of my comment ๐Ÿ˜‚. Regardless, the time spent compiling a small snippet of code is completely negligible. In the end, both #if 0 and if (false) have their complimentary uses.

[โ€“] AlmightySnoo -1 points 8 months ago (1 children)

Yeah, but I still think if (false) is silly because it adds an artificial constraint which is to make sure the disabled parts always compile even when you're not using them. The equivalent of that would be having to check that all the revisions of a single source file compile against your current codebase.

[โ€“] [email protected] 3 points 8 months ago

If(false) works in interpreted languages, the other one doesn't. It's stupid either way, that's what version control is for, but if we are doing the stupidness anyway, you can't use preprocessor flags in many languages because shit doesn't get compiled.

[โ€“] AceBonobo 5 points 8 months ago (1 children)

"you're not wrong, you're just an asshole"

[โ€“] Duralf 1 points 8 months ago

Fair enough, I do love being contrarian

[โ€“] pelya 2 points 8 months ago

Tell this to my -Wall -Werror

[โ€“] AlmightySnoo 3 points 8 months ago (1 children)

beat me to it too, it's a meme of course but the advantage compared to comments is thay you get syntax highlighting ๐Ÿ˜

[โ€“] [email protected] 2 points 8 months ago

My linter always skips preprocessors not set to build, in c# at least, greys it all out unfortunately

[โ€“] Magister 2 points 8 months ago

this is what I'm doing too, so at least it's not compiled and better than a /* */ as you can keep all the code intact in your #if 0

[โ€“] [email protected] 47 points 8 months ago

I was going through some js code a few months ago and every function in a module had return; as its first line. And that module was imported into 4 or 5 scripts.

[โ€“] [email protected] 22 points 8 months ago

You folks have clearly not met first year CS students. Screenshots code

[โ€“] AlmightySnoo 18 points 8 months ago

laughing in #if 0:

#include 

int main()
{
#if 0
        std::cout << "Look at this" << std::endl;
#else
        std::cout << "ugly abomination." << std::endl;
#endif
}
[โ€“] [email protected] 8 points 8 months ago

bonus points if you use a different variable every file so they have to go through and change every instance if they want to make changes

[โ€“] [email protected] 8 points 8 months ago (3 children)

If you're in a language that supports it, please don't use if (false) use if ($disallowAllUsers = false && $whateverTheRealConditionIs)

[โ€“] [email protected] 5 points 8 months ago (1 children)

Never seen this, what language or buildsystem is this?

[โ€“] [email protected] 3 points 8 months ago* (last edited 8 months ago) (1 children)

That specific language is PHP, but the tip is applicable in any language that supports inline assignment.

[โ€“] [email protected] 6 points 8 months ago
if (true === $wantToCauseErrorsForFun) {
    badOldFunction();
} 
[โ€“] [email protected] 1 points 8 months ago

The assignment syntax is too close to comparison, which is what is more typical in that position. I would recommend

const bool _isFeatureEnabled = false;
if (_isFeatureEnabled && ...)

if not a proper feature flag (or just remove the code).

[โ€“] SpaceNoodle -1 points 8 months ago (1 children)

It seems much worse to use a setter in an if statement.

[โ€“] [email protected] 0 points 8 months ago

Think of it as inline attribution/documentation.

[โ€“] [email protected] 4 points 8 months ago (1 children)
[โ€“] [email protected] 4 points 8 months ago
[โ€“] Asudox 4 points 8 months ago* (last edited 8 months ago) (2 children)

I don't see the need for an if block or renaming the function and leaving it there. It is extra unnecessary work for the compiler. Comments are probably the best way. Might also copy the current file, put the original in some folder like "old", and delete the old code inside the new copy.

[โ€“] [email protected] 9 points 8 months ago* (last edited 8 months ago) (1 children)

Comments are the worst as they are ignored by refactoring. That's the reason if (false) is actually really good for temporarily disabled code.

[โ€“] [email protected] 1 points 8 months ago

I never thought of that. That's quite smart!

[โ€“] frobeniusnorm 3 points 8 months ago

On a modern computer dead code analysis with constant folding should be nearly unnoticeable when compiling a large project

[โ€“] [email protected] 2 points 8 months ago

what about relying on the persistent undo history in vim?

[โ€“] Archpawn 1 points 7 months ago

In Python you put it in a multiline string, since it has those but not multiline comments.