10 Tips to Write Clean Code

awedis - Apr 3 '21 - - Dev Community

"It's all about writing a code" No Please!

It's about writing a good & readable code, refactoring a lot for better results, optimizing, structuring, organizing, analyzing user requirements, designing, building, testing, etc... believe me, this won't end

In this article I'll be sharing my experience how to write a clean code, anyone can learn new technology & implement it, but we must always follow best practices, & pay attention to minor details 😊


I'll divide the process into 10 steps (will try to make each point short as possible & hope it will clarify or help you to become better in your coding journey)

1- Simple & Readable
  • My code should a junior & senior understand it, naming, structuring your code in a simple & readable way will make your life easier before others
  • By simple I mean it direct, easy and it covers the purpose, where's at the same time will be readable
2- Having your own standard
  • Find your own style & standard this will boost your productivity especially when you're dealing with multiple projects
  • It will make you become quicker with changing stuff or updating a major portion of code
3- SRP (Single Responsibility Principle)
  • A function that performs everything, this is literally a headache
  • Let's make our functions responsible for a single task, this will make your function clean and easy to understand, scalable & much more flexible. And try to keep the arguments as few as possible inside the function
4- Avoid comments
  • Avoid commenting old features or old code (to tackle this master git 😉)
  • Avoid commenting to specify the purpose of a line or "what it does"
  • Your code must not need any comments to be understandable
  • Only write a comment iff you have something related to a third party, and make it short as possible (the aim of this is to clarify things for another developer) but again even if you are using some third party API's your code must be easy & direct, the new developer can access the documentation & figure out everything so there's no need to comment here too
5- Your code should be easy with others
  • Let's make the code as simple as possible & while making your code simpler, is actually the process of optimizing it
  • Be direct and always think of others (is this readable? will they understand it? will I after 2 weeks?)
6- Whitespaces & Indentation
  • Many people think using extra of these will make it harder to read or even it will affect the compiler 😄
  • I recommend using as much as possible, just check the examples below and you will know what I mean
const fetchUsers=()=>{
let response=['some data'];
return response;
}
Enter fullscreen mode Exit fullscreen mode
const fetchUsers = () => {
    let response = ['some data'];
    return response;
}
Enter fullscreen mode Exit fullscreen mode
7- Organize your project
  • A good folder structuring, & easy file access are just perfect meaning for a happy life 🌴
  • Structure your code so that others can search for folders & files easily, make it ready to scale and organized as much as possible
8- Meaningful & Direct names
  • While coding you will be writing a lot of names for variables, functions, classes, arguments, modules, packages, directories, etc...
  • It will be a good practice to write a code that is clean, easy to understand & just meaningful, check the examples below
let cars: array = [];
let myCars: array = [];
Enter fullscreen mode Exit fullscreen mode

both of them are easy & readable I know but which one is direct & easy for the developer to know its purpose?

  • Another example:
const searchUser = () => {}
const searchUserByName = () => {}
Enter fullscreen mode Exit fullscreen mode

Here if the first one is generic go with it, but if the function does a search by the only name then use that one, the idea is to name your function what it does only

9- Avoid repetition
  • Always make sure your code is reusable & scalable
  • Break down a task into smaller chunks, and make sure at least 90% of it is reusable
10- Refactor
  • Read other codes, optimize them & make them cleaner. I like using the term "compress", which means making it simpler & smaller
  • Refactoring a code is a really good skill, it will make you aware of what's going on, & while you're refactoring, it will become better, it's always good practice to return to your code after a while & enhance it

These are some main points that I wanted to mention, still, there are many details that you need to take care of like unit testing, performance optimization, etc...

It's not just about the tech or the skills, it's about being organized too, hope this was clear & simple as how the code should be 😊 & I'm always looking forward to dealing with this kind of codes 😁
Thank you, and wish you all a great programming experience

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player