Introduction
The target audience of this article is everyone; a person with 25 years of experience and a person with 25 days of experience. The art of writing effective code is difficult to learn and takes patience from the person who is writing it.
There are two perspectives to writing code. One is to think about the present and get the work done – which is easier and quicker; the other one is to chase the future and make the code reusable, self explanatory, and efficient for others. The former approach is widely chosen when deadlines are at stake while the later is limited to POCs, GitHub projects etc. Let’s deep dive into the nitty-gritty details about effective code writing and how it can be useful for us.
Effective Code
The definition of “effective code” is very broad but in layman's terms, it means the code you write today will work if reused 10 years later. It seems like an absurd statement but consider the scenario of System Namespace. It was written with .NET Framework 2.0 way back in 2005. We still use this namespace in our code with .NET Framework 4.7 in the year 2017. Do you think, they rewrite all the classes from scratch every time they release a new version? I don’t think so. They simply reuse the existing code and write only the new functionalities for every new release. This is what effective code looks like.
How to Write Effective Code
Before jumping to the key features of writing efficient code, let’s see a common scenario where most people fail to choose the right approach to write code and how it impacts the end result. Consider the problem stated below and we’ll try to find out its best solution:
Problem
Write a program to add two numbers.
Description
Whenever I’m taking an interview or talking to fellow developers about best practices in programming, one question I always ask is about the approach to add two numbers. The problem is straightforward, simple, and can be solved with 2/3 lines of code.
Solution
99.9% of the answers I received are like this,
Step 1 - Declare three variables int a, int b, int c
Step 2 - Pass user inputs to the first two variables i.e. a, b
Step 3 - Assign the sum of a, b to c i.e. c = a + b;
Step 4 - Return c.
Program
- class Program {
- static void Main(string[] args) {
- int a = 10;
- int b = 10;
- int c = 0;
- c = Add(a, b);
- }
- public static int Add(int a, int b) {
- int c = a + b;
- return c;
- }
- }
If this is the answer in your mind, then do read the article till the end. The above written code is good and will work for all the integer values. But is this code efficient? Let’s find out.
Scenario 1
If I pass the inputs as 15 and 25, this program will return me 40. That’s correct and some of the programmers have the imagination limit up to this point while writing the code.
Scenario 2
If I pass the inputs as 15.6 and 22.7, will this program work? Not at all, our written code will fail to execute this scenario, so is the imagination of the programmer while designing the algorithm of this program. The above code is not efficient. Where do you think is the lag. Choosing the data types? Not considering the second scenario? Hold on to your thoughts till the end of the article and carry on reading.
Attributes of an Effective Code
I have read articles written by MVPs, authors, senior architects and have seen many expert videos about the coding styles and programming practices one should consider before writing code. Almost all of them focus on the same points which are as follows:
1. Understanding the Requirement
Before even writing a single line of code, a programmer should be absolutely clear about the requirement. If not, ask questions until it is crystal clear. If this simple thing is considered in the above explained scenario, people will never use int as the data type.
In my question, I have never asked to add two integer numbers. My requirement is way too generic – “Write a program to add two numbers”. People should ask me questions, what kind of numbers? Are they only integers? What about real numbers and millions of other questions?
2. Self Explanatory and Reusable
This is the most important point because 8 out of 10 programmers work in teams. They write code for a project on which other people are also working. Until and unless a programmer is working solo on a project which is rare, whatever he writes must be readable & reusable to others.
Let’s take the example of the above scenarios and assume that the Client wants a program to add two integer numbers. In this case, is the code self explanatory and reusable?
Program
- class Program
- {
- static void Main(string[] args)
- {
- int a = 10;
- int b = 10;
- int c = 0;
- c = Add(a, b);
- }
- public static int Add(int a, int b)
- {
- int c = a + b;
- return c;
- }
- }
First thing to check is can I understand this program without involving its owner. The function name is Add. What does it describe? Add what? As I noticed the parameters of the method are of integer type, I’ll make a guess that it is a program to add two integer numbers. I’m able to make a guess only because this is a very simple example.
What if the method is of 100 lines and does Addition of time (seconds) and return minutes. In that case, the output of Add(60, 60) is 2. Hence our random guess failed to understand the code. Somewhere I read – “Readability > Cleverness” which totally fits in here.
Second thing to check is reusability. Can I reuse it? Reusability is a broad subject but for the sake of this example, can I implement this code or can I inherit it? To reuse this in my code, I have to inherit the whole Program class which is not possible because of two things – What if the accessibility modifier is private or what if some of the methods of Program are not meant to be inherited.
The efficient approach is to create an interface, define the method signature, implement it to a class and then use it as follows,
- namespace EffectiveCode
- {
- class Program
- {
- private static void Main(string[] args)
- {
- int a = 10;
- int b = 10;
- int c = 0;
- IAdd add = new Add();
- c = add.AddTwoNumber(10, 0);
- Console.WriteLine(c.ToString());
- Console.ReadKey();
- }
- }
- interface IAdd
- {
- int AddTwoNumber(int firstNumber, int secondNumber);
- }
- class Add : IAdd
- {
- public int AddTwoNumber(int firstNumber, int secondNumber)
- {
- return firstNumber + secondNumber;
- }
- }
- }
Here, if I have to reuse the code, or to implement it on my own, I don’t have the dependency of Program class. This is what (Single Responsibility Principle) of the famous design principles SOLID says.
3. Coding Style
A long time ago, I wrote a blog post about the two types of programmers in this world. The link for the blog is here
As I’m a big Silicon Valley person, there is a whole episode of the show dedicated to the famous “tab vs. space” debate in programming. Here is the link
The importance of choosing a coding style is consistency. If you don’t have a coding style and still write code, trust me you need help. Coding style makes your code readable and easy to understand. Let’s take an example to understand more.
- /// <summary>
- /// Programming Without a Coding Style
- /// </summary>
- class Program
- {
- private int MyProperty1 { get; set; }
- private int MyProperty2 { get; set; }
- private int MyProperty3 { get; set; }
- public int MyProperty4;
- public const string MyProperty5 = "test";
- }
- /// <summary>
- /// Programming Using a Coding Style
- /// </summary>
- class Program
- {
- private int _myProperty1 { get; set; }
- private int _myProperty2 { get; set; }
- private int _myProperty3 { get; set; }
- public int myProperty4;
- public const string MYPROPERTY5 = "test";
- }
Here, the first example uses no coding style. We cannot identify which item has what significance. In the second example, I have used the Standard Coding style where the private properties has the naming convention of “_” prefix with camel case, the class variables are written in camel case and the constant variables are written in ALL CAPS. You can see it is very easy to read and differentiate if you follow a coding style. The getaway from this guideline is to use a coding style and be consistent with it throughout the project.
4. Meaningful Code Commenting
One thing I have learned over the years is code commenting is as important as the code written. I once had a Technical Architect in my project who always reviews the code comments more than the code, reason being the Einstein quote – “If you can't explain it to a six year old, you don't understand it yourself.”
In the initial years of my career, I use to get annoyed writing code comments as it is boring and takes time. You have to think of something meaningful every time, otherwise it is of no use. Over the years, it felt like a mandatory step before checking-in the code. If I take the above example and provide coding comments to the code, you will see it becomes very easy to read and understand.

Guest UserPosted Dec 29, 2020, 11:33 AM
Very Helpful Article
Dinesh Kumar SharmaPosted Jun 14, 2017, 7:50 AM
This is very good article.
SubashPosted Jun 5, 2017, 12:31 AM
Thank you sir, very much useful share
Thiruppathi RPosted Jun 4, 2017, 1:19 PM
Nice Article, Thanks for sharing.