Often I come across what I like to call “Misplaced Code”, that is, code which should be refactored to a specific, independent concern rather than mistakenly being defined in an incorrect context.
For instance, consider the following example to get a better idea of what I mean:
1 2 3 4 5 6 7 8 9 10 11 12 | var url:String = Application.application.url; if ( url.indexOf( "localhost" ) { ... } else if ( url.indexOf( "dev" ){ ... } else if ( url.indexOf( "staging" ){ ... } etc... |
Taking the above example into a broader context, it is quite common to see code such as this scattered throughout a codebase; particularly in the context of view concerns. At best this could become hard to maintain and, at worst, it will result in unexpected bugs down the road. In most cases (as in the above example) the actual code itself is not necessarily bad, however it is the context in which it is placed which is what I would like to highlight as it will almost certainly cause technical debt to some extent.
Considering the above example, should code such as this become redundantly implemented throughout a codebase it is quite easy to see how it can become a maintenance issue as, something as simple as a change to a hostname would require multiple refactorings. A much more appropriate solution would be to encapsulate this logic within a specific class whose purpose is to provide a facility from which this information can be determined. In this manner unnecessary redundancy would be eliminated (as well as risk) and valuable development time would be regained as the code would need only be tested and written once – in one place.
So again, using the above example, this could be refactored to a specific API and client code would leverage the API as in the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | switch( DeploymentContext.host ) { case DeploymentContext.LOCAL_HOST : ... break; case DeploymentContext.DEV: ... break; case DeploymentContext.STAGING: ... break; } |
This may appear quite straightforward, however, I have seen examples (this one in particular) in numerous projects over the years and it is worth pointing out. Always take the context to which code is placed into consideration and you will reap the maintenance benefits in the long run.
Hi man : )
Related: totally agree and more on technical debt:
http://www.codinghorror.com/blog/2009/02/paying-down-your-technical-debt.html
Unrelated: is there any reason why you don’t offer full posts on RSS? Sometimes leaving RSS reader is not an option (ie: while reading on the train).
Cheers!
J