Writing code is not hard but writing clean code is not easy either.I wrote trash code when I first started and hear me out that won't scale neither can that boost productivity.But now I write beautiful story book like code so today I will give you some tips to write clean well formated code.
::-
1. Use descriptive names
Make the names of the variables and the names of the functions must be as descriptive as they can be. For example, suppose you want to make some references to the character and last name. In that case, you might use a variable that calls "namePhysician" and "lastNamePhysician" instead of using "name." Even worse if you use "n." Why? Because with that syntax, anybody can understand what value the variable contains.
And what about a function? Well, the name of a function must start with a verb. For example, if I have a function that returns the physician's name, I can create a function that calls "getPhysicianName" instead of using a function that only calls "name."
//do this
let PhysiciansFirstName = "Deez";
let PhysiciansLastName = "Nuts";
//instead of this
let fn = "Deez";
let ln = "Nuts";
2. Use empty lines to create a readable code
With empty lines, we can add legibility to our code. An extra line will be beneficial to identify in an easy way where the functions end. Also, we can use empty lines to separate the variable's declaration from the function's operation. Finally, we might add an extra line before the returned value if you want.
public string Greetings = "こんにちは";
Debug.Log(Greetings);
3. Do not send more than three parameters into a function
Remember, we must make a readable function. It is easier to have three parameters and follow their logic inside the function than to have a bunch of parameters and try to find where the parameters are used.
If we need to use more than three parameters, we can send one object to the function and use the keys of the object as we need. Review the following point if you need to send many parameters into a function.
5. Functions must be small
If you need to do a function with many lines, you must consider that maybe it is more accurate to use a class instead of a function. Remember your function must do only one thing.Although I like functional programming more than OOP.
**6. Keep the number of characters in a line readable
Keep in mind that we want to create a code that is easy to read. Avoid having long lines of code. You can literally everything can be squeezed into a line. Just because you can doesn't mean you should. The size of a line must fit in your screen so that you don't need to do a horizontal scroll. There are a lot of tools like prettier that allow you to control the format of the code.
6. Reduce the number of characters in a line
Keep in mind that we want to create a code that is easy to read. Avoid having long lines of code. The size of a line must fit in your screen so that you don't need to do a horizontal scroll. Remember, there are a lot of tools like prettier that allow you to control the format of the code.
7. Avoid using comments
Sometimes it is difficult to maintain the code. Imagine if we have to maintain the comments too! How can we avoid using a comment? We can use descriptive names. If our code is understandable, we don't need a comment. If we create a comment and somebody has to change the code, we cannot confirm that this person updated the comment. Remember that if you are using a comment, it is because our code is not clear enough. But maybe you think it is sometimes necessary to add a comment, if that's the case, use comments but only in some exceptional cases.
8. Create a descriptive message when you create a commit
When we create a commit, we have to write a descriptive message. That message could be helpful if we want to remember what our code is doing some months later. Avoid messages that do not give us much information. If you only write messages like "refactoring," perhaps this could not be clear enough for the following developers.
9. The DRY Principle: Don’t Repeat Yourself
This is one of the first things that developers are taught. “Early on, I think it’s a really great rule for why we’re writing code in the first place and how to think about code,”. “We’re writing for loops and functions so we don’t have to write the same piece of code over and over again.”
That being said, applying the DRY principle too much can lead to over abstraction. Over abstraction can be especially common when developers get more advanced. “But early on, it’s helpful to think ‘Okay, am I doing similar code in 8 different places? If I am, I need to think about how to make this into a function.’”
10.Use Consistent Verbs per Concept
This is one of the important naming conventions. If we need a CRUD function, we use create, get, or update with the name.
If we need to get user info from the database, then the name of the function can be userInfo, user, or fetchUser, but this is not the convention. We should use getUser.
//Good pranctice
function getUser(){
//do something
}
so,
Reuse Code
Name variable greatly
Reuse code via functions
Avoid comments if possible
Use descriptive function names
That's it!
Happy coding.
Buy me a coffee