Thibauld - Imagination and Execution -

28Dec/080

Web application implementation step 4: Copying is not reusing

RT @thibauld Web application implementation step 4: Copying is not reusing

It is often said that "good coders code, great reuse" (see an example here). I definitely agree with this... unfortunately, too often, developers think that reusing code means copying code. How often did you see developers looking for code snippets on google to find a way to implement any given functionality, convinced that it will save them time... Unless the code snippet you're looking for is yours (= one you developed and, by extension, master), this behaviour will not help you much... Worse, it will even slow you down and here's why:

  1. you spend time on google, blogs, groups, forums... trying to find the code snippet that fits your needs
  2. you find something that is supposedly doing what you want but how to know exactly if it's ok ? It would require to carefuly examine the code... but it takes time... and you wanted to find a code snippet to save time so you decide it must be ok.
  3. you include the snippet in your own code but you have to spend some time adapting it because it can't be included "as is".
  4. finally you think it's ok and test your code. Not bad but it's not exactly what you expected so you're back hacking the snippet. As it is not your code, you're reluctant to study it in depth (and you're a bit lazy) so you treat it as a black box: you change a value here, a value there... "it should do it". And you spend time fine tuning it.
  5. you finally have the functionality you wanted. Too bad, the guy who wrote the snippet was not an expert and you're now facing crashing, security and/or performance issues with this code. You now have to spend time trying to fix a code that is not yours.

Now did you really save time ? To make things worse, you did not learn anything in the process... This method looks sexy at first sight but beware as it's a trap! When facing a new problem, you'll be better off understanding it and tackling it on your own and now! You have several solutions:

  1. Find a library, a webservice... which adresses the issue you're facing. What's the difference between this solution and using code snippets ? Simple: library, webservices etc... is code that is meant to be reused! They are tested, documented and maintained pieces of code... not a piece of code coming out of nowhere pasted on a blog around 2 photos from flickr.
  2. Use a code snippet you wrote. It is an issue you already tackled in the past and you coded something to address the issue. In this case, there is no problem in reusing your own code. You wrote it, you know it, know how it works, in which context it has been developed and which exact problem it solves.
  3. Code a solution by yourself. You can look for inspiration in others' code snippets but you should code the solution yourself. This way, you are sure to code something that adress your exact issue and you're improving your knowledge and skills. Plus, next time you'll be facing the problem, you'll be able to solve it in a few minutes.

It is often said that "good coders are lazy". This is true, but it's hard becoming a lazy coder and lazy coders are most of the time hard working people :)

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.