Загрузка...

Tip 16 Single Responsibility | Better Programmer

Tip 16 How to be a better junior programmer

Today's tip is about the Single Responsibility Principle and why every function should do one thing well.

The Single Responsibility Principle (SRP) is one of the five SOLID principles introduced by Robert C. Martin. It states that a class or function should have only one reason to change—meaning it should have only one job or responsibility.

When you write a function that does multiple things—validates input, processes data, updates the database, and sends an email—you've created a function with multiple responsibilities. This makes it harder to understand, harder to test, and more likely to break when requirements change.

Junior developers often create "god functions" that try to do everything. They might write a 200-line function that handles an entire feature because they’re thinking through a problem sequentially, not structurally. This approach leads to functions that have useful code that can’t be reused and are generally hard to modify. When you need to modify one part, you risk breaking another.

The Solution:

The Single Responsibility Principle forces you to think clearly about what each piece of code is actually doing. It encourages you to break large problems into smaller, focused pieces that each do one thing well.

When writing a function, ask yourself: "What is this function's single responsibility?" If you find yourself using the word "and" when describing it—"This function validates the user AND saves to the database AND sends a notification"—you've got multiple responsibilities.

Another question you can ask yourself is: “What name should I give this function?” If you find yourself using words like “manage” or “process” and you’re coming up with names like “processCustomerOrder”, then you should think about breaking that function into various smaller functions. Your main function can orchestrate these smaller functions. This makes your code modular, testable, and much easier to modify.

Just like that, you're a better developer.

I'm posting every weekday. See you tomorrow!

User Mastery - Helping product teams engage with their users

https://usermastery.com

#programming #juniordev #coding #developer #tech

Видео Tip 16 Single Responsibility | Better Programmer канала Dave Cheong
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять