DEV Community

Discussion on: I Am Not A Real Programmer

 
qm3ster profile image
Mihail Malo • Edited

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;
else operation2;

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) return voidReturningFunction()

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) return void voidReturningFunction()
// 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:

// scary
return condition
  ? operation1
  : operation2;

// okay
if (condition) operation1;
else operation2;
return;
Thread Thread
 
itr13 profile image
Mikael Klages

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

Thread Thread
 
qm3ster profile image
Mihail Malo

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.

Thread Thread
 
itr13 profile image
Mikael Klages

Fair point, seems I forgot programming is still limited by physical boundaries :-P

Thread Thread
 
qm3ster profile image
Mihail Malo

Yup, this is how you get hacks like this:
rust-embedded.github.io/book/perip...