The basic rules and ideas that guide the design of software code are known as coding principles. These criteria assist programmers in writing code that is scalable, legible, and error-free. Following coding best practices can lead to more stable and long-lasting software.
We already discussed the SOLID concept in the last post, so let’s move on to other principles in this area. Let us now look at the principles.
- DRY - Don’t Repeat Yourself
- KISS - Keep it Simple, Stupid
DRY: (Don’t Repeat Yourself)
The DRY (Don’t Repeat Yourself) principle is a fundamental coding principle that emphasizes the importance of avoiding code duplication in software development. Instead of duplicating the same code in multiple places within a codebase, developers are encouraged to create reusable functions, methods, or classes to encapsulate common functionality.
To apply the DRY principle effectively, developers should identify patterns of duplicated code and refactor them into reusable components. These components can take the form of functions, methods, classes, or even libraries and frameworks, depending on the programming language and the nature of the code.
Use Case Overview
Our use case involves the automated sending of emails under specific conditions. Whenever user details are saved or updated in the database, an email should be sent to the Admin Team. In case of any exceptions during this process, the Tech Support Team should also be notified via email. Similarly, when an order is placed, the Admin Team should be informed to initiate order processing, and any exceptions related to orders should trigger an email notification to the Tech Support Team.
namespace PrinciplesPart2.DRYNotApplied
{
public class UserModel
{
// USER PROPERTIES
}
public class OrderModel
{
// ORDER PROPERTIES
}
public class UserBusinessLogic
{
public UserBusinessLogic()
{
}
public void SaveUpdateUserDetails(UserModel userModel)
{
try
{
// Save or Update User details in the Database.
// Send Mail to Admin
MailMessage mailMessage = new MailMessage("[email protected]", "[email protected]");
SmtpClient smtpClient = new SmtpClient();
smtpClient.Send(mailMessage);
}
catch (Exception ex)
{
MailMessage mailMessage = new MailMessage("[email protected]", "[email protected]");
mailMessage.Body = ex.Message;
SmtpClient smtpClient = new SmtpClient();
smtpClient.Send(mailMessage);
}
}
}
public class OrderBusinessLogic
{
public OrderBusinessLogic()
{
}
public void OrderUpdate(OrderModel orderModel)
{
try
{
// Update details in the Database.
// Send Mail to Admin
MailMessage mailMessage = new MailMessage("[email protected]", "[email protected]","Order Placed","");
SmtpClient smtpClient = new SmtpClient();
smtpClient.Send(mailMessage);
}
catch (Exception ex)
{
MailMessage mailMessage = new MailMessage("[email protected]", "[email protected]");
mailMessage.Body = ex.Message;
SmtpClient smtpClient = new SmtpClient();
smtpClient.Send(mailMessage);
}
}
}
}
Challenges in the above code
One significant issue we face is redundancy in our codebase. For each of these business logic scenarios, we’ve written separate code segments to send emails. This redundancy has led to a total of 14 lines of code, and the duplication percentage exceeds 100%.
Implementing the DRY Principle
We’ve taken steps to adhere to the DRY (Don’t Repeat Yourself) principle in our codebase. To achieve this, we’ve introduced a new class called “EmailUtil.” This class is responsible for handling email sending operations through a static method called “SendWithMailMessage.” This method accepts a parameter of type MailMessage for email content and delivery.
By adopting this approach, we’ve successfully reduced code duplication in our existing business logic. This change not only enhances maintainability but also aligns with the Single Responsibility Principle (SRP). Furthermore, we’re working towards extending this design to support the Open-Closed Principle (OCP) and the Interface Segregation Principle (ISP) in the future.
See below code,
namespace PrinciplesPart2.DRYApplied
{
public class UserModel
{
// USER PROPERTIES
}
public class OrderModel
{
// ORDER PROPERTIES
}
public class EmailUtil
{
public static void SendWithMailMessage(MailMessage message)
{
SmtpClient smtpClient = new SmtpClient("",587);
smtpClient.Send(message);
}
}
public class UserBusinessLogic
{
public UserBusinessLogic()
{
}
public void SaveUpdateUserDetails(UserModel userModel)
{
try
{
// Save or Update User details in the Database.
// Send Mail to Admin
EmailUtil.SendWithMailMessage(new MailMessage("[email protected]", "[email protected]","",""));
}
catch (Exception ex)
{
EmailUtil.SendWithMailMessage(new MailMessage("[email protected]", "[email protected]"));
}
}
}
public class OrderBusinessLogic
{
public OrderBusinessLogic()
{
}
public void OrderUpdate(OrderModel orderModel)
{
try
{
// Update details in the Database.
// Send Mail to Admin
EmailUtil.SendWithMailMessage(new MailMessage("[email protected]", "[email protected]", "Order Placed", ""));
}
catch (Exception ex)
{
EmailUtil.SendWithMailMessage(new MailMessage("[email protected]", "[email protected]"));
}
}
}
}
KISS (Keep it Simple, Stupid)
The KISS principle, which stands for “Keep It Simple, Stupid,” is a design and programming principle that advocates for simplicity in all elements of software development, including code, architecture, and user interface design. The KISS principle is based on the idea of avoiding needless complexity and keeping things as simple as possible while yet achieving the intended functionality. The KISS will give flexibility to easy to human readable code structure.

Join the conversation! Your thoughts help the community grow.