Write readable code

Rajkatie
Posted by in C# category on for Intermediate level | Points: 250 | Views : 321 red flag

A good code always like story, not a puzzle. When we read someones code either we bless or curse him. what we want as a developer is in our hand.
Recommendation
Read Value based sorting on key value collection before this article.

Introduction

Writing code which is easy to read, reduce guess work and reveals its true purpose that is not something which is easily achievable. As developer, we always endeavor to write code which will become communication tool for our co-worker.

Once I came across below code snippet in one of project (desktop application). I started thinking about API behind the method (Sleep) and code readability.

Directs way like this:

Thread.Sleep(3000);

another way (overloaded) like this:

Thread.Sleep(TimeSpan.FromMilliseconds(3000));

Well, what’s the point (in first code) writing code in a such way where we are able write the same code (second example).

Though both code snippet produces the same result but first one have a magic number (3000) means a numeric value that is encountered in the source but has no obvious meaning or we can say that code has code smell. At the first glance, if my peer reads the first code, first he tries to guess meaning of that magic number and if unable, then try move cursor over this method and read description about it whether specify numeric value is in second or minute or millisecond.

Here I have taken very simple example but try to think same thing if you are working in enterprise level application and imagine for a second if there is no code comment there.

In contrast to that, in second code example any developer can easily understand it without further thinking and guessing that current thread Suspends for the 3 milliseconds. This one improve code readability. So, there is no any chances for any developers to put extra efforts to find its true purpose which one is require in first one. It clearly reveals its intense means its dependency on TimeSpan struct and we all know that this struct contains methods and members which represents time interval.


In first example, we can refactor the code to improve readability. So, replace that magic number with constant means with an equivalent variable.  This way also we can also make code more readable for those who are unfamiliar with it. Remember one thing it would not be improve code readability if you just replace the “magic number” with variable names like A, B or X. So, try to choose variable names which explain their purpose concisely. But second one is still better because it is already available in-built in framework.


Look consider others below code snippet (Unreadable vs Readable)

1) ASP.Net

Response.Redirect("ManagePriority.aspx"false);

Response.Redirect("ManagePriority.aspx"ResponseEndEnum.EndResponse);
2) ASP.Net MVC

public class HomeController : Controller
   {
private readonly ILogger _logger;
public HomeController()
       {
 _logger = 
new Log4Net(); 
}
    
}
 
public class HomeController : Controller
   {
private readonly ILogger _logger;


public HomeController(ILogger logger)
       {
 _logger = 
new Log4Net(); 
}
    

}


Conclusion

As you can see it’s all about code readability or we can say in another way -

Saving the time of other developer who work with you or who may update your code later. Always try to write code in expressive style and take every opportunity to self-document code through descriptive variable, class and method names.

Reference

For more information kindly read book Clean Coder - Robert C. Martin
Recommendation
Read Value based sorting on key value collection after this article.
Page copy protected against web site content infringement by Copyscape

About the Author

Rajkatie
Full Name: Rajesh Patel
Member Level: Gold
Member Status: Member,MVP
Member Since: 2/14/2012 4:06:42 AM
Country: India
Rajesh Patel .Net Trainer(MSCD | MCT | MS) Brainbench (Asp.Net 4.5 and C# 5.0)
http://www.dotnetfunda.com/profile/rajkatie.aspx
I am Microsoft Certified Trainer. Over 9 years of professional in ASP.Net technology. Currently provides training and consulting services on web technologies such as jQuery,AngularJs,ASP.Net MVC,HTML5 and SQL Server to many developers of fortune IT organizations,College students

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)