DEV Community

Discussion on: Clean code exercises - part 1

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

The if and two returns are entirely superfluous. This type of code to me indicates a lack of understanding of what a boolean expression is. My above example should really be written:

function someFunction() {
  return someBooleanExpression
}
Enter fullscreen mode Exit fullscreen mode

Or, better still:

const someFunction = () => someBooleanExpression
Enter fullscreen mode Exit fullscreen mode

There is nothing tricky or clever going on in my code. Being overly verbose is not the same as being clean

Thread Thread
 
alexmario74 profile image
Mario Santini

I see.

I think that writing pseudo code on a whiteboard explaining what I have in mind, it's not the same as writing production code.

The uncecessary if and return statement will help exposing the implementation to another humans.

It's just that, I often refctor boolean expressions as you did here, so it is just that I was courious about your reasons about failing a job interview.

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️

The code should never be written that way in the first place - it's as bad as writing something like this in pseudocode:

if true return true else return false
Enter fullscreen mode Exit fullscreen mode

Definitely a red flag

Thread Thread
 
ogzhanolguncu profile image
Oğuzhan Olguncu

Also, violates SOLID's first principle, because now function
does two thing instead one.

Thread Thread
 
peerreynders profile image
peerreynders • Edited

The Single Responsibility Principle has nothing to do with "do just one thing" - that is a common misconception.
The Single Responsibility Principle:

Gather together those things that change for the same reason, and separate those things that change for different reasons.

So "those (many) things that change for the same reason" revolve around the same single responsibility.

Don't Repeat Yourself is another one - it' not about removing repetition or duplication:

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

Sometimes duplication isn't about the same thing (The Wrong Abstraction):

prefer duplication over the wrong abstraction

Also: The SOLID Design Principles Deconstructed (2013)

Basics of the Unix Philosophy:

(i) Make each program do one thing well. To do a new job, build afresh rather than complicate old programs by adding new features. (Doug McIlroy 1978)