Don’t be afraid to delete code
Today I did something drastic! I deleted almost all the code I had been working on over the last week or so and started again.
I’m currently working on a new application at work being written in AngularJS. This is the first real experience I’ve had with AngularJS and I’m learning lots every day. I am developing the application using TDD and thoroughly enjoying it however the last day or so I’ve started to notice certain aspects of the code aren’t as clean as I would like.
I put the lapse in code quality down to the fact that when I started work on this application I knew A LOT less about Angular than I do now. I didn’t really understand how to mock the controller dependencies properly or how the injection worked and as a result there were big chunks of code that were in there for some reason but I wasn’t sure why. Another issue I wasn’t happy with, was how the structure of the application was forming. This was due to not knowing or understanding the best practices for structuring AngularJS applications, as well as certain coding standards that are specific to AngularJS in the beginning.
I have read a few resources over the past week or two, which have been very useful in terms of learning how to structure and style the application and if I’d have read these sooner perhaps I wouldn’t have thrown away so much code. John Papa Style Guide is a fantastic resource detailing good practices for coding in Angular and I believe this is a MUST read for anyone working in Angular. Another couple of resources that I found very useful are as follows:
- GoCardless Blog – Building a large angular application
- Tom Seldon – Unit testing in Angular: Behaviour over Implementation
- Scotch.io – AngularJS Best Practices: Directory Structure
- Stackoverflow – “Thinking in AngularJS” if I have a jQuery background?
Although throwing away a week or mores work may seem scary or unnecessary I don’t see it as wasted effort. I’ve learnt a huge amount from writing that code, and whether I keep it or not won’t change what I have learnt. Throwing it away however, and starting again will enable me to utilise the new understanding I have gained and ensure I know it as well as I think I do. In the long term it also means that I should have a more maintainable code base to continue working on, not only increasing my motivation and satisfaction but also saving time.
How much time did I have to spend rewriting the code? Not as long as you would expect, and certainly a lot less than I initially thought. I managed to rewrite 90% of what I had previously within less than 4 hours, and I am also a lot happier with the quality. The bottom line is deleting code shouldn’t be scary and besides if you’re worried about it you can always create a copy before deleting it, either create a branch before deleting your code or just copy the files elsewhere for reference.