For example, if we need a slim code (printing code or for a video)
I think the comic wanting to use as big a font as possible is exactly this case
I find that the formatting that is the most cohesive, keeping related ideas together is actually this:
if(condition)operation1;elseoperation2;
Generally, multiline conditional operations are a bit of a code smell.
If there is only one long branch (or one branch at all), it should often be refactored into an early return.
If there are multiple, you totally lose sight of the condition by the time you get to the else. This probably means both branches should be extracted as function calls with good summary names (and limited scope of variables, passed as arguments).
The only case that has me really conflicted is
if(condition){voidReturningFunction();return;}
On one hand, it would be less noisy to do
if(condition)returnvoidReturningFunction()
But on the other hand, this means that we are potentially returning the return value of voidReturningFunction, which might even in the future change its signature.
Maybe the middle ground would be
if(condition)returnvoidvoidReturningFunction()// but it's esoteric
Not sure. Perhaps having two lines here is optimal, especially if other returns in the function are not void (A case possible in some languages like JS but not others).
Seems like the same reasoning as why I would usually not use
condition?operation1:operation2;
for side effects, especially in the return position:
Another note on the static variable, the name "is(..)Robot" implies it's meant to describe the state of a single instance of a class, while in most languages static would make it the same for all instances.
IE in c# you may have
public static bool IsKillerRobot { get; private set; }
Which would be as safe as any other property, but when a single robot is meant to turn killer, all of them would
I think the comic wanting to use as big a font as possible is exactly this case
I find that the formatting that is the most cohesive, keeping related ideas together is actually this:
Generally, multiline conditional operations are a bit of a code smell.
If there is only one long branch (or one branch at all), it should often be refactored into an early return.
If there are multiple, you totally lose sight of the condition by the time you get to the
else
. This probably means both branches should be extracted as function calls with good summary names (and limited scope of variables, passed as arguments).The only case that has me really conflicted is
On one hand, it would be less noisy to do
But on the other hand, this means that we are potentially returning the return value of
voidReturningFunction
, which might even in the future change its signature.Maybe the middle ground would be
Not sure. Perhaps having two lines here is optimal, especially if other returns in the function are not void (A case possible in some languages like JS but not others).
Seems like the same reasoning as why I would usually not use
for side effects, especially in the return position:
Another note on the static variable, the name "is(..)Robot" implies it's meant to describe the state of a single instance of a class, while in most languages static would make it the same for all instances.
IE in c# you may have
Which would be as safe as any other property, but when a single robot is meant to turn killer, all of them would
Presumably we are in the runtime of a single robot, not a manager handling an arbitrary number of them, but I see what you mean.
Fair point, seems I forgot programming is still limited by physical boundaries :-P
Yup, this is how you get hacks like this:
rust-embedded.github.io/book/perip...