loading...
Cover image for #003 - Clean Code - Functions

#003 - Clean Code - Functions

elkhatibomar profile image Omar ・4 min read

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 ...

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

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

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

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

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

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.

Post image

Posted on by:

elkhatibomar profile

Omar

@elkhatibomar

Computer Scientist , Full stuck software engineer , Linux user since childhood , Classical computer Science books lover , DevOps in progress...

Discussion

pic
Editor guide