Press "Enter" to skip to content

Posts published in “Advanced c#”

For more readable code & liveable world use ENUM

The Enum definition, which has an important place in C#, is sometimes used to reduce the likelihood of error, while sometimes allowing the codes to be read more easily. So what is enum?

Enum (Enumaration) definitions are used to represent fixed values corresponding to specific values in programming languages, such as days of the week, directions, etc. The benefits to the developer are that it improves code readability, minimizes the margin of error due to the narrowing of the possible value set and ensuring the type security. Previously, the task enum was taken with the task constants. “Well then, why do we need to add type of enum?” You can ask, I will be explaining the reasons in the following sections, let’s continue with the readability side for now.

While developing, sometimes there are certain situations that lead to the program flow, we determine these situations, associate them with numerical values, and shape the code. For example, we have a document model and we may need to do different printing for different types of documents, which can be solved by a “practical” approach as follows:

public class DocumentPrinter {
    public static void print(String path, int documentType){
        if (documentType == 0){  // word
            // word document write
        } else if (documentType == 1){ // pdf
            // pdf document write
        } else {
           // html document write
        }
    }
}

// usage
String path= "xx/ebook.doc";
DocumentPrinter.print(path, 0);

As you will notice, however, there is a poorly readable code:

  • Document type information is only understood through the comment line; To understand what the print () method does,
  • In DocumentPrinter.print () method call, the documentType parameter is too high to pass an incorrect value.

At this point, it is possible to use the constant to make the code more readable and secure;

public class DocumentPrinter {
    public static final int DOC_WORD = 0;
    public static final int DOC_PDF = 1;
    public static final int DOC_HTML = 2;

    public static void print(String path, int documentType){
        if (documentType == DOC_WORD){
            // word document
        } else if (documentType == DOC_PDF){
            // pdf document
        } else{
           // html document
        }
    }
}

// usage
String path= "xx/ebook.doc";
DocumentPrinter.print(path, DocumentPrinter.DOC_WORD);

With the constant definitions “DOC_” prefixed with the DocumentPrinter class, we have increased the readability and ease of use of the code. But in this case, there is stil no type safety! Because someone can send DocumentPrinter.print(path, 5);!

The way to overcome the problem in the current situation is to narrow the expected value set for the documentType parameter of the print () method using the enum constants. This is why we define an enum named DocumentType and edit the existing code:

public enum DocumentType {
 WORD,
 PDF,
 HTML
}

public class DocumentPrinter {
    public static void print(String url, DocumentType type){
        if (type == DocumentType.WORD ){
            // word document
        } else if (type ==  DocumentType.PDF){
            // pdf document
        }else{
           // html document
        }
    }
}

// usage
String path= "xx/ebook.doc";
DocumentPrinter.print(path, DocumentType.WORD);

Using Enum, we have made the existing code more readable and secure, and it has become even more enjoyable to provide code complement support for today’s modern ideals.

.Net framework can have user defined enums as well as ready-made enum definitions. For example, there is an enum called DayOfWeek ready for the days of the week.

what is enum in c# .net
c# enum dayofweek

When we look at the values of DayOfWeek enum, we see that the value of 0 is given to the sunday and the values are increased by 1 each subsequent day.

c# enums

As developers, we need to try to write a clean code by passing the rejoice that the code we’re writing is running, and then thinking that the code will be read by us later on (and maybe after we’ve had enough time to alienate the code).

In this blog entry I tried to examine the contribution of enums to code readability and the ease of use provided to developers. Hope you like it!