Monday 29 June 2015

Too Much Typing


One of the most dangerous tools available to a developer is Ctrl+C Ctrl+V, with those two short-cuts a developer can do a lot of damage to a code base.
The duplication it causes reduces the readability, maintainability and the integrity of the code, we should implement things once and only once and not repeat ourselves because who enjoys typing.
Don't Repeat Functionality
For every piece of functionality their should be a single authoritative implementation within the code base.
Don't duplicate functionality by implementing your own function or method to provide functionality already available in the code. If the current implementation doesn't quite fit your needs consider how it could be re-factored to meet your requirements.
Remember that if you need this functionality the chances are someone else will need it in the future too.
If we all implement functionality ourselves soon the code base will be nothing but date formatting functions and string utilities.
If you can't adapt the method to do what you want this should be conceded a smell, it could be that the code you need hasn't been built for change or could be that what you want to do with it just isn't practical or desirable. 
Don't Duplicate Implementation
Don't duplicate implementation by copy and pasting code from one location to another.
If you can't just call the code that already exists the chances are something is structured badly, either your code or the code you need to call. It could be behind a bad abstraction, tied up with too many other concerns or it could be that your code is trying to do something it really shouldn't be doing and sticking its nose in where its not wanted.
Either way the solution to the problem isn't to duplicate the mistake into multiple locations in the code.
Once again if you've encountered this problem at some point someone else will, help them out by dealing with the situation not by replicating the problem.
Don't Duplicate Information
Make sure every piece of information within your system is stored in only one place, this should apply to dynamic data and constants alike, their should be a single source of truth for all data.
Once again if you can't get to the data you need the chances are either you shouldn't need to know that data or its being stored in the wrong place. The class that exposes the data should have a close association with that data, this is how we ensure the code makes sense and that we abstracting things properly.
Having data defined in multiple places creates the possibility that the different sources won't agree on the value of the data, this creates a situation that isn't easily solved, you shouldn't have to decide which source of data you trust more.
Just Don't Duplicate
What I'm trying to get across is that when duplication appears to be the answer your probably solving the wrong problem.
This is nearly always indicative of a structural problem in the code base, it is these structural issues that should be addressed.
Every time something is duplicated in a system your not only duplicating the code, your duplicating the effort required to maintain the code, the likelihood of bugs and the amount of code a reader has to understand.
Because of its need to be maintained and fixed code is a liability we should be looking to end up with as little of it as is practically possible, duplicating it does the opposite.
Writing classes isn't about providing example source code documenting to someone this is a solution for this problem, its about providing a structure so that they don't have to care how the machine is working they just trust you and make use of the implementation you've already provided.
Now its not always possible to know all the variations of a piece of functionality that may be needed in the future, but its for exactly this reason that good code is built for change, so when tweaking is necessary its clear how that can be achieved and we don't just start again by duplicating.
A good developer is a lazy developer who doesn't want to type the same thing over and over again, he or she likes to do a really good job of it once and then stop their. 

No comments:

Post a Comment