Top 10 Clean Code Rules Every Developer Should Follow
1. Meaningful Variable and Function Names
Descriptive names make your code more readable and understandable.
Bad Example:
int x =10;
Good Example:
int maxRetries =10;
Explanation: Meaningful names tell the story of your code. A reader should understand what a variable or function represents just by looking at its name.
2. Keep Functions Small and Focused
Functions should do one thing and do it well.
Bad Example:
publicvoidProcessOrder(Order order){ValidateOrder(order);CalculateTotal(order);ApplyDiscount(order); // More responsibilities...}
Good Example:
Explanation: Each function should have a single responsibility. If you need to describe what a function does with "and" in the middle, it’s doing too much.
3. Avoid Deep Nesting
Reduce cognitive load by flattening your code.
Bad Example:
Good Example:
Explanation: Exiting early reduces the cognitive load on the reader, making your code simpler and easier to follow.
4. Use Comments Wisely
Use comments to explain the why, not the what.
Bad Example:
Good Example:
Explanation: Comments should add value by explaining the reasoning behind a particular implementation or complex business logic, not restating what the code does.
5. Consistent Formatting
Maintain consistent formatting for better readability.
Bad Example:
Good Example:
Explanation: Consistent code formatting makes your code easier to read and navigate. It’s important to adopt a style guide or create one and stick to it.
6. Don’t Repeat Yourself (DRY Principle)
Avoid code duplication by abstracting common logic.
Bad Example:
Good Example:
Explanation: Duplication in code leads to inconsistencies and bugs. By applying the DRY principle, you keep your codebase lean and easier to maintain.
7. Single Responsibility Principle (SRP)
Each class and function should have only one reason to change.
Bad Example:
Good Example:
Explanation: A class that does too much is harder to maintain. SRP makes your code more modular and easier to test.
8. Avoid Magic Numbers and Strings
Use constants for numbers and strings to give them context.
Bad Example:
Good Example:
Explanation: Constants provide meaning to numbers or strings, making your code easier to understand.
9. Keep It Simple (KISS Principle)
Simplicity is key. Avoid unnecessary complexity.
Bad Example:
Good Example:
Explanation: Aim to write code that any developer can easily understand without needing extra documentation or explanation. Simplicity leads to maintainability.
10. Use Exception Handling Properly
Proper exception handling ensures that your code can gracefully handle errors without crashing.
Bad Example:
Good Example:
Explanation: Proper exception handling helps in making your code more robust and ensures that it can handle unexpected situations gracefully. Always try to catch specific exceptions and include clear, descriptive error messages. Use the finally block for any necessary cleanup.
public void ValidateOrder(Order order) { /*...*/ }
public void CalculateTotal(Order order) { /*...*/ }
public void ApplyDiscount(Order order) { /*...*/ }
if (user != null)
{
if (user.IsActive)
{
if (order != null)
{
ProcessOrder(order);
}
}
}
if (user == null || !user.IsActive) return;
if (order == null) return;
ProcessOrder(order);
// Set user to active
user.IsActive = true;
// Mark user as active after successful login
user.IsActive = true;
public int Add(int a,int b){return a+b;}
public int Add(int a, int b)
{
return a + b;
}
if (userType == "admin")
{
// complex logic
}
if (userType == "superadmin")
{
// same complex logic
}
if (IsAdmin(userType))
{
// complex logic
}
public class User
{
public void Register() { /*...*/ }
public void Login() { /*...*/ }
public void SendEmail() { /*...*/ }
}
public class User
{
public void Register() { /*...*/ }
public void Login() { /*...*/ }
}
public class EmailService
{
public void SendEmail() { /*...*/ }
}