Sobes.tech
Junior — Middle

Have you ever applied SOLID principles in your projects?

sobes.tech AI

Answer from AI

Yes, I have applied SOLID principles in my Python projects to improve code quality and facilitate maintenance.

For example, the Single Responsibility Principle helps to separate classes and functions so that each element is responsible for only one task. This simplifies testing and modification.

An example of applying the Open/Closed principle in Python:

from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self):
        pass

class Rectangle(Shape):
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        import math
        return math.pi * self.radius ** 2

# Function to calculate total area

def total_area(shapes):
    return sum(shape.area() for shape in shapes)

shapes = [Rectangle(3, 4), Circle(5)]
print(total_area(shapes))

Adding new shapes here does not require changing existing code, which aligns with the Open/Closed principle.

Using SOLID helps create flexible and extendable architectures, which is especially important as projects grow.