Is there a good way to find unused controllers/factories/services/etc?

724 views
Skip to first unread message

Casey Rodarmor

unread,
Sep 17, 2014, 10:38:58 AM9/17/14
to ang...@googlegroups.com
Hi all,

I have a large app and I'm worried that there might be unused controllers/constants/services/values/factories/etc. Is there a good way to determine if any of these are unused and can be removed?

Thanks!

Casey

Michael Bielski

unread,
Sep 17, 2014, 10:43:48 AM9/17/14
to ang...@googlegroups.com
Your unit tests should show you where your code coverage is low or even non-existant. Karma is easily configured to run a coverage report.

Casey Rodarmor

unread,
Sep 17, 2014, 10:46:36 AM9/17/14
to ang...@googlegroups.com
Unfortunately I may have unit tests which are testing code which is no longer used in non-test code. Will that work in this case?

On Wed, Sep 17, 2014 at 11:43 PM, 'Michael Bielski' via AngularJS <ang...@googlegroups.com> wrote:
Your unit tests should show you where your code coverage is low or even non-existant. Karma is easily configured to run a coverage report.

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/ESzkqU04djY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Michael Bielski

unread,
Sep 17, 2014, 11:21:43 AM9/17/14
to ang...@googlegroups.com
It will help with identifying code that has no tests against it, but that's about all. In a situation like this, I tend to resort to the Find function of my IDE. I start with modules that I suspect are not being used and search for their injection points. If I find none, then I comment out the file in the index.html page and run the application (this is where E2E testing can really help.) If everything runs correctly, I remove the line from the index.html file, delete the module and the tests, and move on to the next one. Having been developing the app that is my main project at work for just over 2 years, this scenario has been played out a few times.

Casey Rodarmor

unread,
Sep 19, 2014, 2:49:30 AM9/19/14
to ang...@googlegroups.com
I don't think it would be supported by the API, but might there be a way to:

1. Get all injectables (services, factories, constants, etc)
2. Shim them with something that looks like:

var used = {};

for (var name in injectables) {
  used[name] = false;
}

function shim(name, realValue) {
  used[name] = true;
  return realValue;
}

module.controller('main', shim.bind(undefined, 'main', realMainController));

3. And then once the app was booted and running, print out what was still false in the "used" dictionary?

This might be more work than just doing it manually, so it might not be worth it. Also, as written it would break function parameter introspection.

On Thu, Sep 18, 2014 at 12:21 AM, 'Michael Bielski' via AngularJS <ang...@googlegroups.com> wrote:
It will help with identifying code that has no tests against it, but that's about all. In a situation like this, I tend to resort to the Find function of my IDE. I start with modules that I suspect are not being used and search for their injection points. If I find none, then I comment out the file in the index.html page and run the application (this is where E2E testing can really help.) If everything runs correctly, I remove the line from the index.html file, delete the module and the tests, and move on to the next one. Having been developing the app that is my main project at work for just over 2 years, this scenario has been played out a few times.

--
Reply all
Reply to author
Forward
0 new messages