Stop Naming Your Variables "Flag": The Art of Boolean Prefixes

13 points by typesanitizer


majaha

Didn't think I'd be that guy, but doesn't this article smell kinda LLM-y?

The fancy quote marks. The kinda mushy prose. The endless bullet points.

Headings like Context Matters (The “Boolean Trap”)

Naming isn’t about style. It’s about empathy.

The version from December even has em-dashes.

pyj

Renaming things is incredibly powerful in learning a codebase.

I'd say this applies when tossed into code no one left knows or remembers, or onto an evolving project where naming has deteriorated as the problem and solution space changed changed. It's a small win just making code require less effort to read. Tool supported renaming is useful, quick and often safe, but I've cut myself before on sharp edges like auto-serialization.

olliej

A better option is to just stop using booleans for flags outside of cases where it very basic usage, ie setEnabled(bool), isEnabled() or similar.

This was one of the first bits of advice I was given by my first manager (though I can’t recall whether it was before or after I started getting paid).

Every time I come across functions with a boolean flag that is of the obvious yes/no form, or even worse (and the reason to prefer enums even for values where true/false might make sense in isolation) is functions with multiple boolean arguments.

The annoying thing for me in C++ is that you ideally want to use scoped enums, but those don’t have an implicit int conversion (which is the reason you’re using them) but they also don’t have a way to specify an explicit boolean conversion, so you can’t do if (someBooleanLikeEnum) :-/

orib

You inherit a codebase and find this:

No, I generally don't. Do you have any real examples?