Today’s subject gets a small refactor, as the existing code is already in good shape. As I have been going through the code base of AngularJS I have been noticing a majority has been written with care for future maintenance. The document file was selected to show method extraction; this is something that doesn’t get done because of JavaScript’s anonymous functions. Anonymous functions are great for making your code condense, but that also leaves more work for the reader of the code to understand its real intent.

The first method extraction is to extract the anonymous function for the root scope on event handler and make it named function. Giving this function a name was a little confusing, as I had to read all the code to try and gain enough understanding of the intent.

Our final method extraction is pulling out the inline function from the dependency injection array for $get. This part of best practices for AngularJS, and it is really for readability; without this, it is hard to see where the array ends.

That is it, a shortcode splitting exercise. The code is closer to best practices for AngularJS and is cleaner so that others can more clearly understand it. Lesson learned, become aware of the best practices for the framework you are writing in so that others can quickly help you when you have any issues using it.

Remember, if you have a snippet of code that you would like refactored on split code leave a comment below with the code snippet or link to where to obtain it.