
SOLID Principles you won't forget again
Introduction SOLID is a set of design principles for writing clean, maintainable, and scalable code. It stands for: S - Single Responsibility Principle (SRP) A class or function should have only one responsibility . In the bad example below, the CreateUser function should not handle email-related logic. Bad example: public class UserService { public void CreateUser ( string email ) { var user = new User ( email ); _repository . Save ( user ); var smtpClient = new SmtpClient ( "smtp.company.com" ); var message = new MailMessage ( "no-reply@myapp.com" , email , "Welcome!" , "Your account was created successfully." ); smtpClient . Send ( message ); } } Good example: public class UserService { public void CreateUser ( string email ) { var user = new User ( email ); _repository . Save ( user ); _emailService . SendWelcome ( email ); } } O — Open/Closed Principle (OCP) Code should be open for extension but closed for modification . This means you should be able to add new behavior without ch
Continue reading on Dev.to
Opens in a new tab



