Code Best practices
Anshu Toppo
ManagerBest Coding Practices: A Comprehensive Guide
Hello, I'm Ansh Opo, a senior manager at PWC, and in this article, I am going to shed light on the best practices we need to follow for improved coding. We'll delve into what clean code is, why it's so important, and how to modify existing code to make it cleaner and more streamlined.
Introduction to Clean Code
Clean code refers to any code that is easy to understand and maintain. It stands out for its consistent style and structure, it utilizes meaningful variable and function names, and it shies away from unnecessary complexities. Good practices of clean code are to use descriptive names that help other developers understand your intentions and avoid ambiguity; this reduces what is known as "code smell".
Understanding Code Smell and Its Importance in Code Structure
So, what exactly is a code smell? Essentially, it's a hint that something might be wrong. Non-descriptive codes such as "let X equals to five" do not clearly indicate what the code does, leading to ambiguities that can become problematic.
Keys to Enhancing Code Structure
- Choose Thoughtful Variable Names: Selecting a meaningful name instead, such as "count", makes it clear that this variable is meant for counting purposes.
- Adhere to a Coding Style: Implementing proper indentation, spacing, and braces will make your code more structured and easier for others to read and understand.
- Comments are Your Friends: Never underestimate the power of a good comment! A thoughtful and thorough comment that explains what your code does and how it works can make a world of difference for developers later in the process.
- Focus on Error Handling: One suggested way of handling errors gracefully is to wrap the code inside try-catch blocks and catch the errors in a professional way. A proactive approach to error handling includes employing real-time log analysis tools and defining specific exceptions for more effective management.
The Importance of the DRY Principle and Code Design
A fundamental aspect of efficient coding is the DRY (Don't Repeat Yourself) principle, which stresses the importance of reusing code rather than writing the same code multiple times. This principle, when followed, promotes code modularity, enhancing its reusability and adaptability.
Best Practices to Follow
Here are some of the highlights to keep your code clean, efficient, and effective:
- Test your Code Thoroughly: Ensuring your code does what it's supposed to do is vital. You can achieve this by writing automated tests and performing manual testing.
- Use a Version Control System: Tools like Git provide a way to track changes. This is especially helpful for teams working on the same code.
- Avoid Hard Coding Values: Using constant files or configuration files is an effective alternative that gives flexibility.
- Adopt Design Patterns: Using design patterns such as Singleton, Factory, and Abstract gives a better structure to your code.
- Backup your Code: Backing up your code daily is a safety measure against potential system failure.
- Proper Planning: Before starting any project or feature, it's crucial to understand what the project or feature is meant to achieve.
Summarizing the Benefits of Good Code Practices
Here is a summation of why good coding practices and clean code can have a substantial positive impact on your projects:
- Enhanced Readability: Easy-to-understand, well-structured code helps your team understand different components better and speeds up the development process.
- Improved Maintainability: Well-organized code is easier to maintain.
- Increased Reusability: Code that's designed with modularity in mind can be reused in the same project or in others, saving precious time.
- Optimized Efficiency: An efficient code can handle increasing amounts of data and withstand traffic without crashing.
- Quality Enhancement: A well-written and well-tested code is less likely to have bugs and security vulnerabilities. It is more likely to meet user requirements and expectations.
Let's follow these practices to produce efficient, clean, and high-quality code. Thank you for reading!
Video Transcription
Hi everyone. This is Ansh Opo. I'm currently working as a manager in PWC and I'm here to share the code best practices. Let's start in front of the screen. There is this code which says sum it have two values.It is having a result and it is doing some calculation and returning a result. So here the function looks very normal but there is a code smell to it. What are the issues in this code A and B? It doesn't tell us what it is. The sum is there, we can write a more compact way and readable way. The sum the name of the function used is not very descriptive. Also there is this code like add number which is a more compact and readable version to it. Instead of passing A and B, we can pass number one and number two. And instead of using unnecessary variable result, we can just add these numbers and return it to the function. Also there is the second function for area circle and it is having certain values are we see a pi value which is having a magic number or a hard coded value. It is doing some calculations and returning an area. So the issue with this code is the area of circle is not descriptive what it is doing. We should avoid using the magic numbers. Also, there is a lack of commented tells what the function does. Rather.
The good approach would be to use a comment like calculate the area of the circle given radius, having a proper descriptive names, having avoiding use the hard coded numbers rather using a constant numbers and also having a proper indentation or the style format. What we are talking about we are talking about the clean code. What is a clean code? A clean code refers to a code which is easier to understand and maintain. It follows a consistent style and structure and uses meaningful variable and function names and it avoids unnecessary complexities.
Hm uh Good practices of a clean code as I have it traded to use a meaningful variable and function name descriptive names helps to other uh helps other developers to understand what it is. What we're trying to do with the code. Like a code smell. What is a code smell? It can be a problem going forward like let X equals to five. What it is is doing. It's not descriptive rather than assigning a thoughtful name as an count and giving to a value. It says that OK, this variable is being used for a count purpose, having a proper coding style or a proper indentations, proper spacing and braces, make your code look more structured and it is easier to read and understand. Comment is one of the underrated practices who are the developers. We should always try to have a meaningful comment which explains what this code is going to do, how it is going to work. What would be the purpose of code example, let X and Y and giving just a comment at X and Y is not a standard comment rather than to explain it that to calculate a sum of the positive number, it tells that OK, this value is going to return the sum of the positive numbers. One of the good approach is to handle the errors gracefully is to have a try catch block instead of just throwing errors when an undefined value is encountered is not a very good approach.
Rather a good example would be wrapping the function in try catch block and catching the errors. Apart from having the try catch block, we can have a real time log analysis tool. We can add specific exceptions. Like one of the exceptions that all developers would be, you know, uh knowing is a null pointer exception. So defying those kind of exceptions in a code for a proper error handling of. One of the good practices is a dry principle. What is a dry principle? Dry principle means don't repeat yourself, avoid writing the same piece of code multiple times rather than we can define a service class or of help of function and can reuse in the code when we talk about the reusability. Also this code design, which is more modular and more reus uh reusable. It can be easily adapted across the code and can be reused also. And one of the good practices is to moize the functions like breaking the lung function into sub modules which is very handy to use. It is easier to understand and maintain. One of the good approach is also to test the code thoroughly. Make sure our code is intended to do what it is supposed to do by testing, we can write the automated test cases, can perform manual testing also, which can make a code more readable and error free.
One of the good approach is to use the version control system like get an SVM so that we can you know, track the code changes. And it is also helpful for the collaborators like the fellow developers who are working on the same piece of code. Also, one of the practices we should not follow is the hard coded values. We should avoid it in all costs. But how can we avoid it? We should use a constant files or a configuration file for these values. Also one of the good practices that are all most talked everywhere is to use the design patterns such as the singleton pattern, the factory pattern, the abstract pattern and publish subscribe patterns. So these design patterns gives a more structure and it is a good design for the code base. Also, one of the good practices is to back up the code daily a system might crash or might some days you might face some network failure or a power outage. So it's a good practice. That end of each day, we push our code to get and we can avoid the code loss. Also, one of the key handles is to proper plan. Like before we start a project or we start a feature, we should ask questions, what is the purpose of this project? What is these expectations? What would be the future plan? How can we accommodate in a code? Let's sum up the good code practices.
First is the clean code. If you write a clean code using all these, what will happen? A clean code would make your code more readable. It is easier for your collaborators to understand the different components and it increases the readability. Also a structured code, a well organized code is easier to maintain and we should practice the usability. A well designed modular code is easier to adapt and can be reused in the same project or in the other. Also a good efficient code and an optimized code can handle the amount of data, the increasing amount of data and also in handle the traffic without any crash time or a downtime. And the most importantly, key code enhances the quality of the code like a well written code. A well tested code is less likely to have bugs and security vulnerabilities. It is more likely to use the requirement. Uh Sorry, it is more likely to meet the user requirement and expectations. I hope this would help you to have a be best code practices going forward. Thank you.