Search code examples
optimizationcode-cleanup

Code cleanup and optimization, where to start?


I want to optimize my angularjs frontend application and cleanup the code to provide better code quality.

I thought about bringing in more abstraction, since I implemented a lot of similiar looking, but slightly different controllers.

My question(s) are the following:

  • Are there common techniques to recognize bad code and optimize it?
  • How can one determine if code is either good, bad or redundant?
  • Where should one start, when trying to provide better code quality in an existing software project?

Solution

  • To answer your questions:

    1. Yes there are: by looking at the code itself experienced programmers can tell if the code has certain characteristics or not. Some metrics exist that could indicate alarm signals in terms of quality like "many dots" in object-oriented languages (same in Javascript) which indicates close coupling. Here is a comprehensive list.
    2. By looking at it or as written before with static code analysis.
    3. As others stated don't optimize or refactor just for the sake to have a good looking code base. When you need to touch existing code again to e.g. add a feature or fix a bug then start to look for code redundancies and many other signals that might indicate to refactor the code. Martin Fowler wrote an excellent book about it with step-by-step examples which IMO is a must-read for every developer. Also a good starting point is Misko's site. He talks about testability but "good" code is well testable.

    What's really important before refactoring is to have a strong automated test base to rely on. If not add tests and go really slow to make sure you don't break existing functionality.

    The topic is really huge and impossible to work through in a post here but I think it's one of the most important ones that makes an experienced programmer.