Wednesday, June 08, 2005

Copy -> Brain -> Paste

People say that all that software engineers do is "Copy Paste". Here's a real life example why the Brain component is important.
I once wrote a code to get some data and cache it to make second time retrieval faster. Here's what the code for retrieval looked like that I wrote:

public ShowDetail getShowDesc(String territoryId,String showId) {
HashMap cmap = (HashMap)cache.get("somekey");
if(cmap==null) {
cmap = new HashMap();
}
ShowDetail showDesc = (ShowDetail) cmap.get(showId);
if(showDesc == null) {
logger.debug(showId+" not found in Cache ... Getting from Webservice");
showDesc = pwm.getShowDetail(territoryId,showId);
if(showDesc != null) {
cmap.put(showId,showDesc);
cache.put("somekey",cmap);
}
}
return showDesc;
}


Basically checking if the data exists in cache, if not, getting it from webservice and putting in cache.
Another fella needed to reuse the entire code (translation: copy paste everything). He probably didn't understand the concept of cache, or probably he did understand but didn't want to use it. Whatever be the case, here's the code that finally came out:

public ShowDetail getShowDesc(String territoryId,String showId) {
HashMap cmap = null;
if(cmap==null) {
cmap = new HashMap();
}
ShowDetail showDesc = (ShowDetail) cmap.get(showId);
if(showDesc == null) {
log.debug(showId+" not found in Cache ... Getting from Webservice");
showDesc = pwm.getShowDetail(territoryId,showId);
}
return showDesc;
}


The first 2 lines itself are amusing enough for starters. I used to laugh on such things some time back, but it not funny anymore.

No comments:

Post a Comment