After the third layer, the programmer falls into the limbo, an unconstructed pointerless plane. Factories become their reality. Only a garbage collection can bring them back to reality
I got a decade of working with JVM. if statements for feature flags are fine, all that matters is where you put them so you don't have 300 statements for a flag
I actually love macros. My company has them everywhere, and we set up a compile flag to create a "preprocess" file that's basically what the code looks like after all the macros and aliases have the real values subbed in if you care to look at that.
Macros are legitimately good for systems programming. Haven't got the dependency for a specific feature? Just don't build it. Need a piece of code to run balls to the wall? Compile in only exactly which code branches will be used in execution. Etc etc.
C used to have a feature for telling the compiler whether to inline a function or not, but they deprecated that feature because it was decided that programmers were terrible at determining whether it was beneficial to inline a function or not, and inlining should be left up to the compiler alone.
Meanwhile, function macros are mostly just inline functions with serious flaws that regularly lead to problems...plus, due to implementation details, they can only be inlined, even if the person who wrote them was wrong about inlining the function everywhere it's used being optimal.
If the compiler is so much better at figuring out when it's optimal to inline a piece of code, why didn't we deprecate function macros alongside the 'inline' flag?
This is not a good take. "function" macros, if you wish to call them that, have totally different semantic than functions. The still do just a text replacement, and are really useful for generic code blocks. Want some code that works for multiple types? Use a parameterized macro.
C is used a lot in bare metal programming/embedded systems, where there are actual use cases for stuff like that. There's no harm to keeping it available in the language, just only use it if you need it.
One of the textbook examples is a square. You #define SQR(x) x*x
Compare this to say: double sqr(double x) return x*x;
That carries a tonne more meaning. It implicitly converts our types to a double precision floating point number. Maybe we copy x into a new variable, maybe the compiler optimises that out. Now, we could write a hundred different overloads of this to safely handle any possible types we might want to square, but they all run into the simple problem: we didn't want a function. We wanted a text shorthand.
Service feature flags cannot be compiled out because the whole point of them is to be able to dynamically turn them on and off without a whole deployment
We can argue about the name, but "this frature has to go live on the 15th at midnight, noone wants to be here, and the deploy is too long anyways to turn it on precisely at midnight" requires you to put an if(now() > getActivationDate()) somewhere.
If you exclude the code at compile time, how do you plan to turn it on without another deploy? Of course there are cases where another deploy is an option, I tried to provide one where it isn't, or it isn't a good option.
Why would you want that at compile time and not runtime? Like if you deploy to prod, see your festure is buggy/not working/isnt allowed to be active until 3 days post prod release, you can toggle it instantly
The feature flags we're all talkin about don't come in at compile time. Also, your app sucks if you have to recompile to enable a feature. How do you do A/B testing?
We've recently baked all this in to our harness and automated processes so flags get created automatically, we have a ceremony around turning them on, then they soak in prod, while we're monitoring, before a PR for flag removal gets created by an agent.
You do need some kind of flag process otherwise they litter the codebase, or you forget to turn them on in prod, or whatever.
Kinda, though behind the scenes pattern matching (if implemented correctly) is closer to a dictionary, so it's constant speed instead of having to go through each case
It has a lot more power in terms of full pattern matching and object deconstruction, but yes, the simplest forms are glorified switch statements from C (except they're actually expressions, they can return a value; Rust is nearly purely expressions, even for conditional-like code structures).
250
u/naveenda 17h ago
What's wrong with that?
81
u/Ahmouse 16h ago
REAL programmers use C macros
20
u/jewdai 14h ago
I think they might be referring to runtime feature flags managed by a third party provider.
12
u/wthja 9h ago
How else should I implement it? Feature flag is turned on/off remotely, hence a simple if statement after reading from remote.
6
u/AZMPlay 15h ago edited 7h ago
Any compiler/preprocessor worth its salt will implement feature flags by including/excluding code wholesale.
Any optimizer worth its salt will optimize away the constant condition in an if statement away otherwise.
Edit: Egg on my face, I had forgotten about runtime feature flags. Damn.
1
u/SecureVillage 9h ago
We've recently baked all this in to our harness and automated processes so flags get created automatically, we have a ceremony around turning them on, then they soak in prod, while we're monitoring, before a PR for flag removal gets created by an agent.
You do need some kind of flag process otherwise they litter the codebase, or you forget to turn them on in prod, or whatever.
7
u/[deleted] 21h ago
[removed] — view removed comment