Writing Clean Code. Functions and Methods(Part 2).

Aurelian Mesaros
Flawless Bits
Published in
3 min readJan 7, 2021

This article will show you how clean functions can help you avoid duplicated code, avoid indentation(or arrow code) and mark real intent. After that we’ll show you some tips for improving your functions.

The main difference between a method and a function is that a method is on an object. A function is independent of an object. In this article functions and methods are the same thing.

Functions are often viewed as simple blocks of code, meant to avoid duplicated code. This article will show that clean functions are way more complex than that, find out more!

Main Reasons To Create A Function:

1. Avoid Duplicated Code.

  • Duplicated code means there are 2 places which have to be fixed/maintained.
  • Usually developer think this is the only reason a function should be created(It’s not).

2. Avoid Indentation (or Arrow Code).

Look at the code above:

if
if
if
while
do something
end while
endif
endif
endif

Cons:

  • Adds noise, this is a sign that the code is complicated.
  • Readability is damaged, more time is needed to understand it.
  • Hard to test.
  • More chances for having bugs.

Solutions:

  • Solution 1: Extract the method.

Dirty:

if
if
if
while
do something magic
endwhile
endif
endif
endif

Clean:

if
if
if
doSomethingMagic()
endif
endif
endif
function doSomethingMagic
while
do something magic
end while
endfunction
  • Solution 2: Return early.

Dirty:

function isValid
if condition1
if condition2
if condition3
return true
endif
endif
endif
endfunction

Clean:

function isValid
if !condition1
return false
endif
if !condition2
return false
endif
return condition3
endfunction
  • Solution 3: Throw The Error Fast.

Dirty:

function signUpUser
if !isUsernameValid(username)
if !isPasswordValid(password)
// account is created
else
throw new error Username is Invalid
endif
else
throw new error Password is Invalid
endif
endfunction

Clean:

function signUpUser
if !isUsernameValid(username)
throw new error Username is Invalid
if !isPasswordValid(password)
throw new error Password is Invalid
// account is created
endfunction

3. Mark The Real Intent.

Understanding the intent of a function is really important. Comments are often a good solution, but a good name for your function is a better one.
The function name acts as a short summary, choose it well.

Dirty:

if isUsernameValid(username) && isPasswordValid(password)
// signUpUser

Clean:

if isUserValid(username, password)
// signUpUser
function isUserValid
return isUsernameValid(username) && isPasswordValid(password)
// account is created
endfunction

Tips for Improving your Functions:

  • Pick the correct name, and mark real intent. For some tips in doing that read this article about Writing Clean Code. Proper Naming for variables, methods and classes.
  • Stick to the Single Responsibility Rule. Also, picking the name is easy if the function does only 1 task.
  • Avoid Indentation or Arrow Code(showed above how).
  • Return early(showed above how).
  • Throw the Error Fast(showed above how).
  • Stick to the 7 rule: Most adults can store between 5 and 9 items in their short-term memory. The function is easier to understand if it doesn’t have more than 7 variables/conditionals/parameters/etc.
  • Reduce the number of parameters as much as you can. Few parameters are easier to understand and test. A solution here is to pass objects and not variables.
  • Handle exception at the root level, don’t allow for try and catch blocks to spawn like baby rabbits.

--

--