DEV Community

# 5 Tips for better code readability

David Lambauer on August 11, 2019

Recently, I was hired to refactor a huge codebase that was messed up for years. The project was a Magento 2 project with more than 30 custom module...
Collapse
 
msamgan profile image
Mohammed Samgan Khan

I agree with all the point except for the first one. Chained Function Calls, there is no need to use extra memory space when we can use chaining.

But again, its individual perspective. Nice article.

Collapse
 
davidlambauer profile image
David Lambauer

Hi Mohd,

I think now a days, we don't have to worry about memory. I would prefer to be able to read, debug and understand the code instead of having to worry about memory.

But for sure, I am talking about PHP. It might makes sense to worry about the usage of memory in different languages.

Collapse
 
msamgan profile image
Mohammed Samgan Khan

No offense, it's just I have a personal rule not to create an unwanted variable until that particular value is used more than ones.

Thread Thread
 
davidlambauer profile image
David Lambauer

You're right, not every function call must be wrapped in a variable. It always depends, right? 🙃

Thread Thread
 
msamgan profile image
Mohammed Samgan Khan

hell yaaa...

Collapse
 
pujansrt profile image
Pujan Srivastava

In the naming convention, whenever I use boolean variable I prefer to prefix is, has, can. For example

const isPerson : boolean = true;
const hasAge : boolean = true;
const canDance : boolean = true;

Collapse
 
davidlambauer profile image
David Lambauer

Good one! Added it to the post. Thanks

Collapse
 
marcosteodoro profile image
Marcos Teodoro

Thanks for the great tips!

I improved a lot my code quality and legibility after read Clean Code and some things about Object Calisthenics rules. Some of the tips in your article match exactly with this topics.

As Martin Fowler writed:

Any fool can write code that a computer can understand. Good programmers write code that humans can understand

Collapse
 
serranoz profile image
serranoz

In the number 4 you can just return the conditional:

return $customer->hasId()

right?

Collapse
 
sergey_telpuk profile image
Sergey Telpuk

As for me, lots of factors can influence to write clear code.