Design Flashcards
What is clean code, and why is it important?
Clean code refers to:
- well-structured,
- readable,
- and maintainable code
- that follows best practices.
It:
1. enhances collaboration,
2. reduces bugs,
3. and facilitates easier future modifications and debugging.
What does SOLID stand for?
S - Single Responsibility Principle
O - Open/Closed Principle
L - Liskov Substitution Principle
I - Interface Segregation Principle
D - Dependency Inversion Principle
What are some techniques to improve code readability?
-Meaningful variable and function names
- Proper code indentation and formatting
- Keeping functions short and focused (Single Responsibility Principle)
- Writing descriptive comments only when necessary
What is DRY?
It stands for Don’t Repeat Yourself and advocates for avoiding duplication in code by extracting common functionalities into reusable components, functions, or modules. It promotes code reusability and maintainability.
Single Responsibility Principle (SRP)
The Single Responsibility Principle (SRP) states that a class should have only one reason to change.
If multiple responsibilities are included in a class and one of those responsibilities changes, it could lead to changes affecting unrelated functionalities, resulting in a less maintainable codebase.
Open/Closed Principle (OCP)
The Open/Closed Principle (OCP) asserts that software entities (classes, modules, functions) should be open for extension but closed for modification. This means that the behavior of a module can be extended without altering its source code.
By using techniques like inheritance, interfaces, and polymorphism, new functionality can be added to an existing codebase without changing its core implementation, thereby minimizing the risk of introducing bugs.
You can also use the Decorator pattern to apply new functionality to an existing component such that the component’s source code does not need to be changed.
Liskov Substitution Principle (LSP)
The Liskov Substitution Principle (LSP) states that objects of a superclass should be replaceable with objects of its subclasses without affecting the behavior of the program.
Examples:
- Square vs Rectangle (changing length and width results in different behavior for things like calculating area).
- Seagull vs Penguin (both birds but both don’t fly).
Solution: More granular interfaces (i.e. Interface Segregation Principle).
Interface Segregation Principle (ISP)
The Interface Segregation Principle (ISP) advocates that it is better to have many smaller interfaces than a few large ones.
A component should never have to implement or extend something that requires it to implement functionality that is not needed by that component.
Dependency Inversion Principle (DIP)
inverts the direction of dependencies away from implementation to abstractions.
- high-level modules should not depend on low-level modules and thus both should depend on abstractions
- and abstractions should not depend on details, details should depend on abstractions
What are some common code smells?
-Duplicated code
- Long methods or functions
- Large classes or modules
- Inappropriate comments
- Switch statements or conditional complexity
What is Cohesion?
Cohesion refers to the degree to which elements within a module or class belong together.
High cohesion means that elements within a module are related and work together towards a single purpose.
Low cohesion signifies that the elements are not closely related.
Single Responsibility Principle
What is Coupling?
Coupling represents the level of dependency between different modules or classes.
Loose coupling is desirable as it reduces the dependency between modules, making them more independent.
Tight coupling implies a high level of dependency, which can make changes more challenging.
Open/Close Principle
What is dependency injection?
It is a design pattern or technique where one object supplies the dependencies to another object, rather than the object creating its dependencies directly.
What is polymorphism?
objects being treated uniformly through a common interface while exhibiting behavior specific to their actual types
What is composition?
Represents a more tightly coupled has-a relationship between classes where one class contains objects of another class such that:
- When the whole is destroyed so are it’s parts
- The whole is responsible for the creation, management and destruction of the parts
- Changes to the container will likely impact the parts