Sobes.tech
Middle

What programming principles that contribute to writing clean code do you know?

sobes.tech AI

Answer from AI

  • KISS (Keep It Simple, Stupid) - Keep it simple. Code should be as simple as possible to understand and maintain.
  • DRY (Don't Repeat Yourself) - Don't repeat yourself. Avoid duplicating code by using abstractions and common functions.
  • YAGNI (You Aren't Gonna Need It) - You won't need it. Do not add functionality that is not currently required. Develop only what is necessary.
  • SOLID - a set of five design principles.
    • S (Single Responsibility Principle) - The single responsibility principle. Each class or module should have only one reason to change.
    • O (Open/Closed Principle) - The open/closed principle. Entities (classes, modules, functions, etc.) should be open for extension but closed for modification.
    • L (Liskov Substitution Principle) - The Liskov substitution principle. Superclass objects should be replaceable with subclass objects without breaking the program.
    • I (Interface Segregation Principle) - The interface segregation principle. Clients should not depend on interfaces they do not use. Prefer many specific interfaces over a single general one.
    • D (Dependency Inversion Principle) - The dependency inversion principle. Upper-level modules should not depend on lower-level modules. Both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions.
  • Code Conventions - Following accepted standards for naming, formatting, and structuring code within a team or project (e.g., Google Java Style Guide).
  • Clean Code - A general concept that includes readability, maintainability, testability, and clarity of code. Principles include:
    • Using meaningful names for variables, functions, and classes.
    • Functions should be small and do only one thing.
    • Avoid deep nesting.
    • Write tests for your code.
  • Refactoring - The process of improving the structure and readability of existing code without changing its external behavior.
// Example of applying DRY and meaningful names
public class UserService {

    private UserRepository userRepository; // Dependency injection

    public User findUserById(Long userId) {
        // DRY: Using a common repository method
        return userRepository.findById(userId); 
    }

    public User findUserByUsername(String username) {
        // Meaningful method name
        return userRepository.findByUsername(username);
    }

    // ... other methods using UserRepository
}

// Example of applying SRP (Single Responsibility Principle)
// This class is only responsible for working with users in the database
public class UserRepository {

    public User findById(Long id) {
        // Logic to find user by ID in the database
        return null; 
    }

    public User findByUsername(String username) {
        // Logic to find user by username in the database
        return null;
    }

    public void save(User user) {
        // Logic to save user in the database
    }

    // ... other methods for working with users in the database
}
What programming principles that contribute to… - sobes.tech