DEV Community

Cover image for Clean Code [Part-1]
SriSarvesh
SriSarvesh

Posted on

Clean Code [Part-1]

  • Clean code involves enhancing existing code through a process called refactoring. Whether you're writing a full code for a new feature or making changes, it's essential to iterate and refine the code.

  • A code base can survive for long period if its continuously improved and refactored. Whenever you introduce something new to the code base, the goal is to enhance the existing code in the process.


Clean Code Metrics

  1. Clean code should be readable and meaningful.
  2. Should reduce the time that we take to understand whats happening.
  3. Precision is key, the code should be concise and to the point.
  4. It must steer clear of using unclear names, complex nesting, and overly large code blocks.
  5. Should follow widely accepted best practices, patterns, and principles is crucial.
  6. The code should be enjoyable, easily maintainable, and readily understandable.

Clean Code Focuses On

  1. Names that we give to (variables,functions,classes).
  2. Structure of the code that we have in our project like the no of lines that we leave between each line of code.
  3. Comments that we make in our code.
  4. Splitting Responsibility of a function or a class like what a particular function does and its purpose.
  5. No of parameters that we pass to function.
  6. Avoiding deep nesting of conditional statements and loops.
  7. Error and Exception handling in the code.

Naming Convention

1.snake_case:
In this convention multiple words are separated by underscore.And every character is in lowercase.

Applied in variables,functions and methods name.
Language uses this convention: Python

Ex:

 is_valid = true

 def return_response():
     return True
Enter fullscreen mode Exit fullscreen mode

2.camelCase:
In this casing convention the variables will be having multiple words and the first word’s character starts with lower case,but the next word's first character will be starting with uppercase.

Applied in variables,functions,methods name.
Language uses this convention: Python,Javascript

Ex:

 const isValid = true;

 function returnResponse(){
     return true;
 }
Enter fullscreen mode Exit fullscreen mode

3.PascalCase:
Here in this casing convention the words in the variable starts with the uppercase letter and no space between the words.It is same as camelCase but here the first word's first letter also starts with uppercase.

Applied in class name.
Language uses this convention: Python,Javascript

Ex:

class AdminRole{

}
Enter fullscreen mode Exit fullscreen mode

How to choose name for variables,functions,class?

  • When we want to store properties like name,age,email etc.We can store it in a variable with name describing whose properties and its what kind of properties it is.

Ex: Here the below variable name hold's the object of customer and their personal details.

 const customerDetails = {
        name:"Gopi",
        age:"2",
        email:"sample@gmail.com"
 }
Enter fullscreen mode Exit fullscreen mode
  • And when we want to store boolean value for a variable or when a boolean value is being returned by a function the name should represent a question and the value or the function body should be an answer for it.

Ex: Here the below variable store's the boolean value that tells whether an user is available or not.

 let isAvailable = true;

 function  isValidUserInput(){
  The function name should describe what kind of check is being done.
 }

Enter fullscreen mode Exit fullscreen mode
  • And when we want to name a class,it should represent what kind of object we will create based on the class.

Ex: When we want the object of different users then the class name will be. The class name's specifies what kind of user the object of the class will hold.

 class Admin{

 }

 class Customer{

 }
Enter fullscreen mode Exit fullscreen mode
  • And even when we want to create a static class that has utility methods the name should represent what kind of utility method's the class will hold.

Ex:The class name below will act as a container for the various pieces of data or functionality.

 class DateUtility{ 
   So this class will hold the the utility methods of date
 }
Enter fullscreen mode Exit fullscreen mode

Some more naming tips:

  • A variable name that we give should be specific and should tell us what value is being stored.
  • But also it should not be too specific and very long and describing each and every thing and lists too much information.
  • And also we should avoid unclear abbreviations like ymdt = “20220121CET”,instead we can name it like
const dateWithTimeZone =  “20220121CET”;
Enter fullscreen mode Exit fullscreen mode
  • Be consistent in the convention that we choose and use it fully throughout the whole code base.

  • So in the situation where there are variable's like getUsers(),fetchUsers(),retrieveUsers() we should use any one of the convention out of it.

  • And when we choose a convention for fetching users like
    fetchUsers() then for other entities also we should do it like that fetchProducts().

Top comments (0)