Middle
What design patterns do you use (DRY, SOLID, GRASP, etc.)?
sobes.tech AI
Answer from AI
In my work on the frontend, I adhere to the following patterns and principles:
- DRY (Don't Repeat Yourself) — avoid code duplication, extract repeating parts into functions or components.
- SOLID — especially useful when building complex components and services:
- Single Responsibility for separating logic.
- Open/Closed for extendability without modifying existing code.
- GRASP — used to define object responsibilities, for example, choosing controllers to manage state.
- Design Patterns — often use:
- Module for organizing code.
- Observer for subscribing to events.
- Factory for creating components with different configurations.
Example of using the Observer pattern in React:
import React, { useState, useEffect } from 'react';
function useEventEmitter(emitter, eventName) {
const [data, setData] = useState(null);
useEffect(() => {
function handler(eventData) {
setData(eventData);
}
emitter.on(eventName, handler);
return () => {
emitter.off(eventName, handler);
};
}, [emitter, eventName]);
return data;
}
This approach helps write maintainable and scalable code.