The following are some best practices to follow when programming.
1. Comment Your Code
2. Have Measurable Goals
3. Don’t Repeat Yourself (DRY)
4. Name Your Variables Intuitively
5. Consider Reusability When Writing Code
6. Be Consistent With Indentation, Naming Conventions, and Braces Placement
When it comes to coding, there are best practices, and then there are practices that programmers generally find themselves doing simply because of a lack of knowledge in the area. There are also some practices that some programmers follow out of laziness. These are all things that should be avoided and taken care of by the programmer to keep code clean and correct.
In this article, I’ll go over some of those practices that we need to do away with while keeping in mind the importance of efficiency and productivity.
1) Don’t Repeat Yourself
This is probably the biggest thing that I see programmers getting lazy on. Sometimes they will just take one piece of code and copy it over to another section without changing the variables or functions involved. That’s not good practice at all! If you find yourself having to do this, you should look into using functions and classes to help you along with your programming. By doing this, your code will be much easier to maintain and debug since you can change one function instead of several different pieces of code. Using functions also helps make your code more modular, which makes it cleaner as well as easier to understand what is going on at a glance.
2) Use comments
Another thing that I see programmers leave out nowadays is comments. They either think
There are a lot of things to keep in mind when coding: object orientation, encapsulation, readability, and security to name a few.
As a web developer, I spend a lot of time writing and reviewing C
Code readability is essential. It doesn’t matter how smart you are or how good your code may look to you, if it is not readable, the likelihood of maintaining or updating it goes down significantly.
“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” – Martin Fowler
You have to ensure you have comments in your code. Even if it is self-documenting code, comments make it much more readable to the next person who might come along and need to maintain it. In addition to that, you should be writing code in a style that makes it easy for someone else to read and understand easily.
Also, a good practice would be putting the most important lines of code at the top and the least important at the bottom, so they can be found more easily.
Another area you need to pay attention to is naming conventions. The name of a variable or method should define what it does, what type of data it contains, etc. It should not just be some random word or phrase that has nothing to do with its functionality or content. I have seen many instances where there was no consistency in naming conventions and this made programs very difficult to read and maintain.
In addition to that, you should
Creating a Variable:
When declaring a variable, you want to make sure that you declare it in the most logical place in the code. There are 3 main categories of scope for variables:
1. Global – these are declared outside any functions or classes and can be accessed by multiple functions/classes. They are generally used for constants, such as PI or GRAVITY_ACCELERATION.
2. Local – these variables are defined locally inside a function and cannot be accessed outside that function. They generally only exist within the duration of the function call.
3. Class-level – these variables exist for the lifetime of a class object and can be accessed by any member functions (methods) of that class object.
Another thing to watch out for is the naming standard for your variables. For example, if you’re using C
var bestPractices = new BestPractices();
bestPractices.Add(new BestPracticeItem()
{
Name = “Using the ‘var’ keyword”,
Description = “Use the ‘var’ keyword to declare variables.”,
