Very early in my programming practice I realized that there is a difference in knowing how to write code and knowing to how to write a good and quality code.
So what is a good code anyways?
First requirement is for the code to work, obviously. But that’s not enough. Because you are usually not the only developer working on a project, and at some point someone might need to change or fix something you wrote. So it’s important that your code is easy to read and understand. It also should be easy to change and maintain so you can respond to clients requirement to change something faster. And it should be efficient and work fast.
It’s seems like a lot to achieve in usually very limited time.
So here are some tips to write code that is readable, maintainable and efficient.
(The following advice are related to programming generally but there are some examples for C# specifically)
Give variables a meaningful names
It makes code so much more readable and easier to understand.
Don’t use abbreviations
It’s better that the variables name is large than to be ambiguous and vaguely
Apply common casing rules
It helps to immediately recognize if that is a class or a variable and makes your code more readable too. In C# it’s recommended for classes to be written in PascalCase, and variables in camelCase.
Maintainability and simplicity
At the beginning I had tendency to overcomplicate solutions that could be done more easily. And here’s what helped me to get a little bit better in writing a simpler code.
When you get your task, don’t jump on code right away, but first think about it deeply. Write some pseudo code, describe step by step how your program will work or draw a flowchart. That will help you to realize your problem better and predict things that wouldn’t occur to you on first sight. When you done all thinking and drawing, the coding is the easiest part.
Break problem and code into smaller pieces. The smaller – the better. When you work with functions with a little code in it, and when every function has their own single purpose, your code will be much more reusable and easier to change also.
When it comes to efficiency and speeding up your program, here are few simple little tricks that is good keep in mind:
When optimizing your code, focus on parts that are called the most often.
Use switch rather than if, because in the IL, switch sometimes uses jump statements and an if will not use these optimizations.
When using if, put more likely output first to prevent program running unnecessary code more often.
If you need to append strings, use the StringBuilder class, because it can improve performance, but only if you work with large strings and appending them multiple times. For a single appending simply use string type.
When you work with collections, use array whenever you can, because they are the fastest collection types. Other types like List or Dictionary are more complex and have additional logic, so when you don’t need that logic, simply use an array.