Clean Code Handbook and Best Practices

Here are some software development coding guidelines, Learn Best Coding Practices and how to write Clean Code! become a Software Development Coding Expert!

Write clean code!

Writing good quality maintainable code is an art! When we try to write clean code, we grow as a developer and learn many new things in software development.

Clean Code Checklist and Best Practices

How to write clean, high quality code that everyone will appreciate, let's understand the need behind Clean Code then the Art!

Why clean code required in software?

Think of any small or big software company you are working with a team of developers, probably many of them are from different background, culture, upbringing, habit, challenges etc. and you all are working towards a common goal, and it's not just about developers, also there are people involved from non-technical or semi-technical background, and together you are creating some product or service.

Finally the company will sell that service or product to buyers (the source of revenue generation), so the codes you write are the lifeline of business.

And as a technology professional you must realize that business demand and technologies are constantly changing, so what you write today someone will change tomorrow, and you have to change someone’s code few months down the line.

Now think, how you expect someone’s code assigned to you for some alteration, surely you expect the code should be easy to read, each variables, functions, properties should have some meaningful name and easy to understand the flow & debug, right? So, when you write any code, make sure you do the same thing what you expect from others.

Good quality code can help to save huge amount of time, so money! but there are some other benefits

  1. When you write some well organized code, you don’t need to remember anything for next day work, just write and forget.
  2. It takes very less time to explain to someone like co-developer, tech lead, business analyst, or manager.
  3. If any error occur, then it’s easy to figure out where the problem is! and resolve that.
  4. Finally that helps to have a stress free mind for next day work or weekend. Eventually helps to have better health and preserve energy for something good
Software Development Coding Guidelines

Here are the top 10 Tips for Best Coding Practice / Clean Code Checklist

  1. Coding Naming Convention

    Naming Convention plays a major role to make the code more human readable and easy to understand, naming convention slightly differ based on language and organization, but name should be informative, here are are the outline that everyone follows.

    • Class Name
      Keep the name as generic as possible, unless you really need concrete type name. Class name should be in UpperCamelCase, Use namespaces,
      //Good example
      class Training 
      { 
      }
      
      
      //Bad naming example
      class TrainingForEmployee    
      { 
      }
      
      
      //Also bad naming example,
       Because Employee & Training two can be separate class  
      class EmployeeTraining
      { 
      }
      
      
      //Good Type specific example
      class CommunicationTraining 
      { 
      }
      
      
      //Bad Case example
      class communicationTraining 
      { 
      }
    • Function Name
      Give a meaningful name, should be in UpperCamelCase.
      If you need same function with different parameters, you can use function overloading, avoid adding too many parameters in same function
      public int GetStudentCount(string subject)
              {
                  int _result = 0;
                  return _result;
              }
      public int GetStudentCount(DateTime startDate, DateTime tillDate)
              {
                  int _result = 0;
                  return _result;
              }
                                                             
    • Property Name

      While defining a property, think of only the object name the property will come under, don’t add class/object name as prefix while defining property name.

      For example, if creating a student class and properties
      public class Student
      {
      public string StudentFullName { get; set; }
      public string StudentAddress { get; set; }
              public string FullName { get; set; }
              public string Address { get; set; }
        }
           
    • Variable Name
      Give some meaningful name; don't write something that will be difficult to understand for someone who does not know the context
      // bad example
      int scount = 0;
      //good example
      int studentCount = 0;
      /* Some people use underscore (_studentCount), 
      if the variable is inside a function,
      that’s also good, but maintain consistency */
      int _studentCount = 0;
      
    • Constant Name
      Constant name should be written in uppercase & separated by underscores
      static int MAX_REQUEST_ALLOWED = 10;
  2. Proper Comments

    Write proper comment wherever required, but keep the comment as short as possible, don’t write the whole story, rather give the reference number of requirement specification if required.

    Don’t write unnecessary comment, for example
    /// <summary>
    /// Indicates if stock available
    /// </summary>
    public bool IsStockAvailable { get; set; }
    

    In above example the comment is not required, because the property name itself says the same thing

  3. Indent the Code

    Indenting is very important, make sure you have maintained proper spacing between lines, functions, properties, comments and all are indented properly.

    Because there is feel good factor when a reviewer looks at your code, which really helps to read the code better and understand the flow.

    In some programming language like Python, indenting indicates the logical block, which means based on indenting the meaning of code may change, but unfortunately other programming languages are not designed that way, so that’s our responsibility to make the code properly indented.

  4. Code should follow Single Responsibility

    Don't write multiple logic in one function, rather split them into multiple functions, then call them from one place, which will help to keep the flow clean, easy to understand, easy to debug & maintain.

    Remember each small function should have only one responsibility

    It would be nice if you can follow all five solid principal in object oriented programming.

  5. No Code Duplication

    If any function is generic, and the functionality is not very specific to the class you are working on, then check with other developer or team lead, if the function is already written somewhere!

    Because writing the same function would be waste of time and difficult to maintain.

  6. Right Business Terminology

    Use right business terminology while writing comment, try to use the same name, word, terms that has been used in system specification.

  7. Smaller Code is Better

    If the function is too long and complicated, probably you are not thinking right! Because functionalities can be complicated but not function, Split them wherever possible, divide them into #region.

  8. Code Testing Friendliness

    Whenever writing any function, think how that function can be tested with minimum parameters value without testing the whole flow, this way you can save time and reduce risk of potential bug during UAT

  9. Memory Usage

    Whenever you create a new instance of any class, make sure you nullify & dispose them after use. Don’t expect everything to be taken care by garbage collector

  10. Seek help & share your knowledge

    If you are finding any difficulty to figure out some logic, after spending some time start asking your team member if they have knowledge about the same, don’t invest too much time to find information yourself, on the other hand if you fix some critical bug / functionality in application, share with team members, that may help them to save time.

    Also, Write a short comment in object level, if necessary.

Clean Code is just a mindset, we coders spend major chunk of our time to write, review & optimize code, then why not keep it clean!

So don’t just write code, write good quality code

Good to Read
You may also read
Email marketing strategy and tools for small business
Email marketing strategy and tools for small business
How to use DevOps Azure Service
DevOps Azure Service Tutorial
SignalR Live Chat Example
SignalR asp.net core example