To be a good programmer, we should follow some easy to adopt habits to keep our programming career long-lasting.
In this article, we’ll look at the best practices to apply to make us better developers.
Duplication is Bad
If we have to write the same thing 3 or more times, then we should definitely consolidate them into one place and reference that one place only.
Duplicating things men’s that we have maintained the same thing in different places.
This means more work for us at the end just by copying and pasting in one place.
When we have to perform maintenance, we’ve to find and change things.
If we duplicate things, then we have a maintenance nightmare.
It starts before whatever code we have is shipped.
Therefore, we should stick to the do not repeat yourself, or DRY, principle.
Instead of having the same thing in 3 or more places, we should consolidate them into one place.
2 of the same thing isn’t quite enough for them to be needed to be consolidated since we don’t if we need them again.
If we know that they’re needed a 3rd time, then it makes sense to move them out into their own component.
This doesn’t only apply to code. It includes any kind of duplication.
There’re a few kinds of duplication. They include imposed duplication from environments that makes us keep duplicating things because we’re forced to.
Also, we may be inadvertently duplicating things because we may not that something like what we have already existed for example.
We can also duplicate because we just didn’t both to abstract out things that are duplicated.
Duplication may seem easier at the beginning but it’ll make our lives difficult later.
Interdeveloper duplication may also occur if multiple people on one or more teams duplicate something.
We may be forced to top duplicate something because of requirements and.
Programming languages may require certain structures to be duplicated.
Multiple representations of something may occur if we have to create the same thing in different languages for example.
This kind of duplication is hard to avoid since whatever we need to implement is needed in 2 different programming languages, so we can’t just apply the DRY principle straight up.
Instead, we may have to stick with duplication if it’s a one-off change or we can make a code generator to create the code for us if we have to change the same thing multiple times.
Documentation in Code
Documentation in code may duplicate what we have in our code.
Since our code is supposed to be mostly self-documenting, we don’t need to put in comments in addition to having our code in most cases.
Unless something really needs explaining like if it’s not completely obvious why we’re writing a piece of code to do something, then we need to comment on it.
Otherwise, we just make sure that our variable and function names are clear and following the usually clean code principles like dividing our code into small pieces.
Comments are easily ignored and become out of date, so that’s another reason not to have so many comments that are duplicate of what we convey in our code.
Documentation Outside Of Our Code
We may also be writing documentation outside of our code.
We can also remove most of those if they’re only for internal documentation and let our code explain themselves.
Also, we also have tests to document our code to tell us what the business requirements are, so that’s also great documentation.
Therefore, we really only need documentation for external users of our API or app so that they’ll know how to use it.
This is because they are the only people that can’t read our stuff. There’s no way that they can access our code in most cases and even if they can, it’s not practical for them to read every line of code to understand how to use our API.
Therefore, we need documentation for them.
Programming languages may be making us duplicate things. We may have the same method in different objects or namespaces for example.
This is a form of duplication that we can’t avoid, but since it’s namespaced, we won’t get confused so easily.
Duplication is something that may come in different forms. We may be forced to duplicate something, we may not know that we’re duplication something, or we may be doing it on purpose.
Duplication doesn’t only apply to code, it may also apply to other things like documentation and anything else that can be duplicated.