Start Design Your Online Store

Calendar is loading...
- Available
 
- Booked
 
- Pending
Whats App

Clean Code series

habib kanbar

What is the point of writing clean code?

I built an app for a client, after a while I went back to the code I wrote by hand, but unfortunately it took me a long time to understand that code, the code was scattered and untidy, or in other words, it was unclean code.

The most important thing a programmer is looking for is to save time .

1- Use clear names for variables and functions:

Follow these two examples:

$fnm = "Habib";
$lnm = "Kanbar";
$y = 33;
public function create()
{ 
//code
 }
$firstName= "Habib";
$lastName = "Kanbar";
$age = 33;
public function createUser()
{ 
//code
 }

As you can see, it is difficult to understand the variables in the first example.

You can try this (Code Spell Checker) extension in (vscode).

2- Don’t call a library or file you don’t need:

This consumes server resources without interest.
You can try this (PHP Intelephense) extension in (vscode).

3- Don’t repeat yourself:

In one of the projects I needed to return the date attached to a written form (1years – 5months ..)  In different parts of the project,I’ve been writing the same code every time I need to return the date like that.
My  manager asked me to create a service for this process and call it up everywhere I need it. The result was less code and tidy .

4-  Comments:

Use comments only when you need them.
When the code is clean and tidy, it will be clear to anyone who reads it, meaning that you will not need comments unless there is a need to clarify something.

5- Use static variables wisely:

If you have a fixed value that you need to use in different places, and then you need to modify that value, you will need to change it everywhere.
Create a static variable at the top of the page or put it in a separate file and then call that variable where you want. If you need to modify the variable, you will only modify it in one place.

abstract class SubscribeStatusConstant
{    
    static $ORDERS_FINISHED = "orders finished";
 
    static $SUBSCRIBE_AND_COUNT_ORDER_FINISHED = "subscribe finished, count orders is finished!";
}
 
if ($status == SubscribeStatusConstant::$ORDERS_FINISHED) {
     $response = ResponseConstant::$SUBSCRIBE_AND_COUNT_ORDER_FINISHED;
 }

Don’t forget to make the variable names clear.

6- Attention to the format of the code so that it is easy to read at first sight:

Leave a space between the functions, and use the Tab key to leave equal spaces from the beginning of the line.

Expand on this topic you can read (Clean Code by Robert C. Martin).

In the next blog, we will talk about Solid principles.

Categories : clean code
Tags: , ,

User Avatar
habib ( habib )

There Is No Biography


« »

No Comments


Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2021 Yes Soft - All right reserved.
Implemented By Yes Soft Team.