How to reduce cyclomatic complexity and make code more readable

Wait... What is cyclomatic complexity?

You have just created a function in your system and want to determine how ‘complex’ the function is. But how do you determine that? Is it several lines? is it a fancy way of using the reduce function?

Happy path coding

The happy path also called happy flow is a term used to describe a function where the best case journey is taken into consideration when coding. You write the code like no errors will ever happen, and expect the user to interact with your user interface the way you want to.

Safeguarding with Inverted ifs

Now we want to check for the different scenarios as we do in the first example.

  1. Check if the customer is set
  2. Check if the name is not empty otherwise it is ‘Unknown’
  3. Check if the customer has valid age.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kasper Due

Kasper Due

Senior Frontend Developer at EazyProject A/S