Skip to main content

DRY Software Principle Using C#

DRY Software Principle Using C# by Ziggy Rafiq


DRY Software Principle Using C#

DRY stands for "Don't Repeat Yourself," which is a software principle in C# that emphasizes the importance of avoiding code duplication. The idea is that every piece of knowledge or logic in a system should have a single, unambiguous representation within that system.

In practice, this means that instead of copying and pasting code, developers should create reusable functions, classes, and modules to avoid duplicating code. By doing this, they can simplify code maintenance and reduce the risk of introducing errors when changes are made.

Here are some ways to apply the DRY principle in C# development:

  • Create reusable functions and classes: Instead of copying and pasting code, create reusable functions and classes that can be used across multiple parts of the application.
  •  Use inheritance and polymorphism: Inheritance and polymorphism can help reduce code duplication by allowing you to create a base class with common functionality that can be inherited by other classes.
  • Use constants and enums: If you have a value that is used multiple times in your code, consider defining it as a constant or enum instead of hard-coding it in multiple places.
  • Avoid copy-pasting code: Resist the temptation to copy and paste code, as this can lead to inconsistencies and errors. Instead, create reusable functions or modules that can be used across different parts of the codebase.

By following the DRY principle, you can create more maintainable and scalable code in C#, reduce the risk of introducing errors, and make it easier to add new features to your application.

Comments

Most Viewed Ziggy Rafiq Blog Posts

A Complete Guide to Using GUIDs in C# with Code Examples

  Overview In this post, we are looking at GUIDs (Globally Unique Identifiers), which are widely used in C# for generating unique identifiers for objects, entities, and resources in a system. In this post, we'll explore the basics of GUIDs in C#, their advantages, and how to convert strings to GUIDs. In this post, we have used Guid Generator to create the GUID from the following URL Address https://guidgenerator.com/ What is GUID GUID (Globally Unique Identifier) in C# is a 128-bit value that is used to identify objects, entities, or resources in a unique manner across different systems and applications. It is also known as UUID (Universally Unique Identifier) in some other programming languages.   GUIDs are generated using a combination of unique factors such as the MAC address of the network adapter, the current time and date, and a random number. The resulting GUID is a string of 32 hexadecimal digits separated by hyphens, such as "b86f2096-237a-4059-8329-1bbcea72769b...

Primitives Data Types and None-Primitives Data Types in C# with Code Examples

  Overview I wrote this post to provide an explanation of primitive and non-primitive data types in C#. C# is a strongly typed programming language, where each variable and expression must have a specific data type. C# data types are categorized into two primary groups: primitive data types and non-primitive data types. Primitive data types are the simplest data types available in programming languages. They are typically pre-defined data types and can represent a single value, such as a boolean value, character, or integer. Examples of primitive data types include int, char, float, double, and boolean, which are common in programming languages like C++, C, and Java. Non-primitive data types are also referred to as composite data types or reference data types. They are constructed from primitive data types and are more complex than primitive data types. Non-primitive data types can hold multiple values and allow for the creation of more intricate data structures like tables, li...

How to use Enum Data Values with .Net 6.0 Framework and Entity Framework Core 6

How to use Enum Data Values with .Net 6.0 Framework and Entity Framework Core 6 Overview An Enum (Enumeration) is a group of constants that are read-only value types. By default, the first value of the Enum variable is 0 i.e. Here we will create an Enum of Priorities type with read-only values of Highest, Normal and Low. We will set the read-only values using an integer assigning a number next to the value. By default, the integer value will start with 0. Here we will be assigning the integer value next to the Enum value such as in the below example and we will use a comma (,) to separate the item in the list of Enum(Enumeration).  We create Enum by using the Enum keyword and then using class, interface, and abstract. The reason we use an Enum is to ensure we improve our application performance and improve application readability, and maintainability, and reduces the complexity of the application hence why if you take a look at the example below of Status (NotStarted, Started, Comp...