3 ms·
I'm going to respond point-by-point, and you'll tell me if that helps at all. > I can't help but wonder how they can possibly work while keeping your code well
by kqr 6y ago
I'm going to respond point-by-point, and you'll tell me if that helps at all.
> I can't help but wonder how they can possibly work while keeping your code well-organised.
They don't keep your code well-organised. They are an intentional complexity debt you pay for their other benefits.
> A lot of features touch a lot of different parts of the code.
This is a sign of high coupling and/or low cohesion. It's a code smell. The ideal is to first refactor these features and make them loosely coupled and highly cohesive. Then they will have only one or very few connection points with the rest of the code.
Feature flags are really hard to use in bad code (and in my experience, most code is bad until the first feature flag that touches that part of the code.) So step one when feature flagging is to refactor and improve the code to the point where there's a logical place to switch between the behaviours.
> Some features require parts of the code to be refactored
Yes. You don't need feature flags to refactor. Refactoring does not change any external behaviour, so you can safely do that guided by your compiler and test suite.
> Are there good frameworks that abstract this mess a way in some easily readable and maintainable manner?
There are frameworks that help with this, but they aren't really necessary. In essence, a feature flag is a single if/else branch somewhere, that plugs in either this feature or that feature (or no feature at all.)
> Do you postpone refactoring the code until after a successful launch?
Opposite! You frequently have to do the refactoring first, because the code was not written to be extendable/subsettable – which is a requirement for feature flagging to be really useful.
(In a way, feature flags are like tests in that both force you to write better code that is more loosely coupled and more cohesive.)
> But then how do you launch the refactored code?
I'm probably sounding like a broken record now, but if it's pure refactoring, just ship it. (Assuming it passes the build and tests.)
> No matter how I think about feature flags, I can't help but conclude that it would turn your code into a hard-to-maintain mess.
Feature flags are meant to be temporary, so we excuse whatever additional maintenance cost they come with. I'm more worried about maintenance a year down the line than I am for the next few weeks or months.
If you're using feature flags, you sort of have to first refactor your codebase to a better state where the feature you want to toggle is extendable/subsettable, and a year down the line when the feature flag is removed – you're still getting the maintenance improvement from the refactoring you had to do.
So in that sense, feature flags lead to even better code in the long run, because you can't "cheat" and just swap some code out for different code. You have to actually make the code properly designed and architected first.