#003 - Clean Code - Functions
Tags: codequality, productivity
Published: 2020-09-26 00:35:54

## Functions

functions are what we do virtualing all the code we do , functions contain all the code that we do , so it's worth to know how to do them well.

how big should a fuction be? big ?? smal?? 

A function should do it once , do it well and do it only. But what does one thing mean?

### The first rule of functions

![Elephant Vs. Hippo: Extremely Rare Images Show Attack ...](https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fi.ytimg.com%2Fvi%2FFJBtujXM5cQ%2Fmaxresdefault.jpg&f=1&nofb=1)

The functions should be small , in old ages the size should be small as the screen size , those days technologie is cheaper than what is was before , now we have multiple screens we big sizes and lot of pixels , so this math relation is no longer correct , maybe the range of a clean function should be between 4-7 lines. 

to minimize the function size we should extract all checks if elses to an boolean function .

Remember the scope rule we talk about , if the variable is far from the scope the name should be long and if it is close for be small , In the class we have more than 1 function so the functions should have long descriptive names.

if we have a function that it's big and operate on set of variables , well this is the behavoir of a class. When we convert our function into a class we take the set of variables that the entier class work on into a constructor.

### are you out of your mind?

![23 Amazing Labyrinths To Get Lost In | Gizmodo Australia](https://external-content.duckduckgo.com/iu/?u=http%3A%2F%2Fimg.gawkerassets.com%2Fimg%2F18ltqopypurybgif%2Fku-xlarge.gif&f=1&nofb=1)

you will say wait my code is now full of small functions I feel lost inside a forest , well that's where a good name came into play a well named classes and functions will never make you lost if you lost it mean your names are bad.

### the goegraphy metaphor

![img](https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse3.mm.bing.net%2Fth%3Fid%3DOIP.Nk3v2175ugYXfnK0EbqhcwHaFj%26pid%3DApi&f=1)

Why do we feel so uncomfortable about writing small functions? well it's about our human nature , yes really! We evolve from explorers who love landscapes we like landscapes. so Big functions look more familiar to us.

We put hidden marks to recognize the path like special shapes or signs , in coding those marks are the comments the try and catches .  But if someone new join the project he will feel lost. But if we put navigations he will not feel lost. Just as every day we get in the car the road have signs to where we are and where were going. So good logical seperation and naming will help newbies to explore our land (code) .

### The bedroom metaphor

![Messy Room | Delia Tee](https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fdeliat.files.wordpress.com%2F2009%2F12%2Fmessy_room_one___busy_dad_by_anakingusan.jpg&f=1&nofb=1)

We all have our messy room that we don't like any one to touch it , because we know where our stuff are even if it's messy I know if I need my pants that I wear yesterday it's on the floor. If someone organize it for us , we will go back and rearrange it the way we like.

But not all mens love to be single , so some of them will make the biggest mistake of their life , they will link themself to  an **annoying** wife. Now we need to organize all the rooms , all the jars with names on them.

So to go back to our real life , the work . We are going to work with a team so the best way is to put the code in nicely named place , where it can be found later and understoud.

### One thing

![img](https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse4.mm.bing.net%2Fth%3Fid%3DOIP.TSfC2SrmZcn6Sb5TOTzbfQAAAA%26pid%3DApi&f=1)

I hear word say one thing , didn't you?  one thing could mean anything. And one thing here should be not for us who write the code it's for those who will read the code after us. 

Let's say we need from our function to scrable an website , but this website need to login and our crawler need an driver browser and so on. It's doing onething which is scrable an website but this one thing contain many sub things with it. So we need a thing to tell us if our function is doing only one thing.

### Extract till you drop

<img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse1.mm.bing.net%2Fth%3Fid%3DOIP.Q2wu61UJmS63aXdwrUjwfgHaHa%26pid%3DApi&f=1" alt="img" style="zoom: 67%;" />

how to make sure that our function will do one thing?

We extract things from function until we have nothing to exctract any more , and sure we need to exctract things that do one thing Again and again.
Well let's be realistic forget everything we talk about before and look at this.

<img src="https://external-preview.redd.it/hJGBRzWE_h-GGr4xaiYeY1_x0Kf3s5UgKFuLx1BPAF0.png?width=640&height=508&crop=smart&auto=webp&s=1e0bd9ccff9924c307e7f8d17c4848888789168e" alt="Post image" style="zoom: 80%;" />