If that holds true for you, then you are one lucky guy
With the risk of making this sound like a rant, My world consists of
an event driven control system, distributed over anything from 15 to
45 processes, 3 to 5 interface cards with their own CPU, talking to a
bunch of distributed nodes, with yet another level of code. That mess
controls various sorting machines that generate events by means of
upwards of 50 operators doing stuff in a random order.
Ok, so something that started out as a perfectly reasonable assignment
statement:
some_condition = (foo == bar);
gradually mutated into a confusing mess. That's no reason not to write
the perfectly reasonable assignment in the first place.
In other contexts, this would be called the gateway drug. Putting yet
another conditional on top of the pile works fine in most cases, until
someone suddenly screw up, I get a phonecall at 3 am, and have to
figure out that piece of code with a plant manager screaming on the
phone telling me just how much overtime he have to pay.
Are you suggesting that
if (foo == bar) {
some_condition = 1;
}
else {
some_condition = 0;
}
is immune to such problems? Isn't it more likely that a maintenance
programmer would try to make the same change to both branches of the
if/else, and likely get one of them wrong?
If the same code goes into both branches, then someone is doing a
very bad job. I'm talking about the conditions before that.
The evil start when you take existing clean code and make it overly
complex. The solution isn't to avoid writing clean code in the
first place. The solution is to be more careful during maintenance.
I know. Yet, I do not have the privilege of working in an
environment, where that is practiced at all times. Normally, the
combination of eager project manager, customers with an urgent need
for some change to the buisness logic, and a nearby deadline combines
to sloppy code. Again, if that is a fact you have not had to live
with, I envy you.
[...]
Let's suppose that, apart from the typo (042 for 0x42),
Actually, the typo was here: moon_phase = M_WAXING
the above expression accurately reflects the intended semantics.
How would you write it more clearly? Personally, I'd add at least
one set of parentheses and align the subexpressions to reflect the
semantic grouping. I'd also pick more meaningful names. But I'd
probably keep the pattern of assigning the result condition to a
variable with a meaningful name.
I had to change the names to protect the guilty parties. I tried to
convey the general spirit of them, though.
In this case, the whole mess was refactored away because of changes
made outside this bit. If that hadn't been the case, I'd at least have
put the conditionals into an if statement, leading to the
assignment. In my opinion the readability of a multi-line perlish
construction (assignment - condition) is nil. So the least effort
would be to change the order, so the flow looked like (condition -
assignment)
Would you replace all the "&&" and "||" operators with a nest of
if/else statements?
In this case I would, had it been the only change needed.