[EN] Copy-paste method is an evil and you should avoid it while you do coding

I am working at simple application for Decision Support Systems classes. I’ve decided to write it as a single page application with AngularJS. Yesterday I had a problem with angular.bootstrap method, thought the solution is pretty simple I’ve decided to put it here because I know that my memory tends to be volatile. Ok then, let’s explain what was the problem.

I use ui-map directive in application I mentioned above to work with Google Maps API. This directive requires to load angular application after Google Maps API is ready to use. That’s why I have to use angular.bootstrap method (I’ve noticed it’s not a common approach) instead of ng-app directive.

When I’ve used this approach for the first time the map was loaded correctly, but rest of the application didn’t work. In other case, when I’ve tried ng-app instead of angular.bootstrap, the map wasn’t loaded but the other parts worked as I expected. I gave the broken code I’ve been working with below.



It took me some time to find the solution. I have been injecting angular application into “map” element instead of the whole body! It was the reason of partially working scenario. All I had to do was a slight change in an invocation of application.bootstrap. Now the application is initialized in this way:

And it works correctly. I’ve prepared a simple working demo on plunker, which is available here. Be aware that manually refresh the preview might be necessary to run it.

After I had found the solution I was wondering why it took me more time to resolve the problem than it should. The answer was simple as well. I’ve used copy-paste method for the initialization part directly from github and I’ve made an assumption that it works and there is nothing to break. As you can see it’s a great example of why you should avoid copy-paste during development. It turns off thinking and brings the risk that you will spend more time for resolving bugs than in case you rewrite the code by hand. I would like you to remember this from today:

Copy-paste method is an evil and you should avoid it while you do coding.

Because as I showed, it can really run you into troubles.

Say something

Your email address will not be published. Required fields are marked with a grey bar.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Current ye@r *