Over the 22 years of my software development career, I have seen many mistakes that developers repeat again and again. I made these mistakes too. I learned from my mistakes.
Here is my top 10 list and more.
1. Missing Documentation
I have seen developers who do not like to write documentation. Obviously, there are tight deadlines and deliverables but it does not take too much time to write about the functionality you are implementing. If you spend one hour for every seven hours of code you write, it will go a long way, and eventually it will save you a lot more time.
OK, let's try to understand this with an example.
In the code sample below, you can see a method called MessySample. I created two ArrayList objects and added some integer and string values to it. Once added, the code simply displays the output.
- private void MessySample()
- {
- ArrayList obj = new ArrayList();
- obj.Add(32);
- obj.Add(21);
- obj.Add(45);
- obj.Add(11);
- obj.Add(89);
- ArrayList obj2 = new ArrayList();
- obj2.Add("Mahesh Chand");
- obj2.Add("Praveen Kumar");
- obj2.Add("Raj Kumar");
- obj2.Add("Dinesh Beniwal");
- int bs = obj2.BinarySearch("Raj Kumar");
- Console.WriteLine(bs);
- foreach (object o in obj2)
- {
- Console.WriteLine(o);
- }
- }
Technically, there is nothing wrong with this code.
The only problem is, there is no proper documentation. Unless I go through the code, I don't know what this method is doing. Also proper naming conventions and readable variables can help. If another programmer writes code and uses the same name variables, obj1 and obj2, and at some point, you try to find all variable references with the same name, you may end up going through some unwanted code. Here is the same code but documented:
-
-
-
- private void MessySample()
- {
-
- ArrayList obj = new ArrayList();
- obj.Add(32);
- obj.Add(21);
- obj.Add(45);
- obj.Add(11);
- obj.Add(89);
-
- ArrayList obj2 = new ArrayList();
- obj2.Add("Mahesh Chand");
- obj2.Add("Praveen Kumar");
- obj2.Add("Raj Kumar");
- obj2.Add("Dinesh Beniwal");
-
- int bs = obj2.BinarySearch("Raj Kumar");
-
- Console.WriteLine(bs);
-
- foreach (object o in obj2)
- {
- Console.WriteLine(o);
- }
- }
2. Messy Code
Keep it clean. Don't be messy. Writing code is an art. Make it cleaner. Make it pretty. Format it. This part is more about focusing on naming conventions and proper representation of your methods, properties, and variables.
Here is my original code sample. As you can see from the code below, I have two ArrayList objects and the code adds some integer and string values to them.
- private void MessySample()
- {
- ArrayList obj = new ArrayList();
- obj.Add(32);
- obj.Add(21);
- obj.Add(45);
- obj.Add(11);
- obj.Add(89);
- ArrayList obj2 = new ArrayList();
- obj2.Add("Mahesh Chand");
- obj2.Add("Praveen Kumar");
- obj2.Add("Raj Kumar");
- obj2.Add("Dinesh Beniwal");
-
- int bs = obj2.BinarySearch("Raj Kumar");
- Console.WriteLine(bs);
-
- foreach (object o in obj2)
- {
- Console.WriteLine(o);
- }
- }
The following code is a clean code with proper comments and naming conventions. As one of the comments suggests, if you use the proper method, variable, and other object names, you will need very little or no documentation. From the code below, I can clearly see that numberList is an array of numbers and authorsArray is an array of author names.
-
-
-
-
- private void CleanSample()
- {
-
- ArrayList numberList = new ArrayList();
-
- numberList.Add(32);
- numberList.Add(21);
- numberList.Add(45);
- numberList.Add(11);
- numberList.Add(89);
-
-
- ArrayList authorsArray = new ArrayList();
-
- authorsArray.Add("Mahesh Chand");
- authorsArray.Add("Praveen Kumar");
- authorsArray.Add("Raj Kumar");
- authorsArray.Add("Dinesh Beniwal");
-
-
- Console.WriteLine("====== Binary Search ArrayList ============");
- int bs = authorsArray.BinarySearch("Raj Kumar");
- Console.WriteLine(bs);
-
-
- foreach (object author in authorsArray)
- {
- Console.WriteLine(author);
- }
- }
3. Copy, But With Love
Code sharing, code reusability, and open source are very common practices today. Thank Google, C# Corner, MSDN, CodeProject, StackOverflow and other online websites for providing tons of free code. It would be foolish for us not to use the same code that is already written and available.
So copy, but copy with love. The first thing you need to do is understand the code and verify it. There is so much code out there. Some code is written by experts. Some code is written by amateurs. You must test your code. Once tested, you may also want to check with the terms and conditions and licensing of the code. Sometimes, you may not realize it, but a person who has shared code may want you to use his copyright terms.
4. Think Outside of the Box
If you are involved in a project that was already developed by some other developers, do not just follow what other developers have written. Before you follow the same steps, think if the way the prior code was implemented is the right way to do it. I may have shared some code on C# Corner but that does not mean I have written the most efficient code. You may come up with better ideas.
For example, on many websites, you may find code that is written using C# 2.0. The same code is applicable today as well, but in C# 5.0, you may write the same code in an optimal way.
5. Testing! Testing! Testing!
This is one of the areas where I find most developers who are rushing to deliver their code are not testing it thoroughly. Not only must you functional test your code, but also stress test it. This I have seen over and over: When a new functionality is added to a project, there may be chances that the code may have affected other areas. You as a developer need to test that all areas are tested well before it is given to your Integration Manager or deployed on the Test Server.
Don't trust yourself unless you're an experienced programmer. Don't guess what your code would do unless you have already used that code before. Always debug the code before even running it. When I write a piece of code for the first time, I go line by line, add my debug variables and use debugger to step through line-by-line and variable-by-variable to see if the values of these variables are passing my tests. You can avoid this if you use #7.
7. Write Test Cases
Visual Studio 2010 and later versions come with a very powerful tool to write test cases for your project. Use it. You can also use third-party open source products such as Nunit.
8. One Thing at a Time
I have seen some programmers write code for one week straight and then do the testing. Write code based on a smaller unit of functionality and test it before you move to the next functionality.
9. Think Modular
I remember the days when a code file would have thousands of lines and just keep going and going. If possible, try to break down your code into chunks based on the function, and create a method or a class as applicable. Use proper Object Oriented Programming best practices. Use proper design patterns. Make a good use of libraries, classes, functions, and other modern programming language features that are available to you.
10. Do Not Trust Your Testing Team
Do not rely on your testing team and think that they will find all the bugs. You are the one who knows code and functionality more than anybody else. You test it and then hand it over to the testing team.
11. Good Team Player
Building software is teamwork. One person can build small software, but when you work on large projects, it is a team effort. A developer must be a good team player. It does not matter how smart or expert you are, if you are not helping your team and not sharing with others, the project will suffer.
12. Ask Questions. Ask Again and Again
Some developers (I was the same in my early career) think asking questions of a client or manager will make them look foolish or stupid. This is not true at all. I would rather explain the same thing four times than get something that is not right. So make sure you get the requirements right before starting to write code.
13. Be Ahead. Give 110 Percent
This is from my personal experience. I was named the "crack programmer," "coder on crack," and other names for solving problems instantly. Most of the time, I got the requirements and they were done before the deadlines. So when my manager would come ask, "Is that feature done?" My answer would be, "Yes, it is live already."
There are three ways to work:
1. Do what you were told to do.
2. Do less than what you were told to do and still be working on it.
3. Do more than what you were told to do.
Do not assume that your boss knows more than you. He/She may know more than you but it does not mean that you cannot give your ideas or suggestions. You may have better ideas. Even if they are not better, it does no harm to open up and let him/her know.