Find unused code in a Rails app

后端 未结 11 1355
南方客
南方客 2020-12-01 02:35

How do I find what code is and isn\'t being run in production ?

The app is well-tested, but there\'s a lot of tests that test unused code.

相关标签:
11条回答
  • 2020-12-01 03:20

    This isn't a very proactive approach, but I've often used results gathered from New Relic to see if something I suspected as being unused had been called in production anytime in the past month or so. The apps I've used this on have been pretty small though, and its decently expensive for larger applications.

    I've never used it myself, but this post about the laser gem seems to talk about solving your exact problem.

    0 讨论(0)
  • 2020-12-01 03:20

    It is not the perfect solution, but for example in NetBeans you can find usages of the methods by right click on them (or press Alt+F7).
    So if method is unused, you will see it.

    0 讨论(0)
  • 2020-12-01 03:21

    mark suspicious methods as private. If that does not break the code, check if the methods are used inside the class. then you can delete things

    0 讨论(0)
  • 2020-12-01 03:24

    You got already the idea to mark suspicious methods as private (what will maybe break your application).

    A small variation I did in the past: Add a small piece code to all suspicious methods to log it. In my case it was a user popup "You called a obsolete function - if you really need please contact the IT". After one year we had a good overview what was really used (it was a business application and there where functions needed only once a year).

    In your case you should only log the usage. Everything what is not logged after a reasonable period is unused.

    0 讨论(0)
  • 2020-12-01 03:25

    I had the same problem and after exploring some alternatives I realized that I have all the info available out of the box - log files. Our log format is as follows

    Dec 18 03:10:41 ip-xx-xx-xx-xx appname-p[7776]:   Processing by MyController#show as HTML
    

    So I created a simple script to parse this info

    zfgrep Processing production.log*.gz |awk '{print $8}' > ~/tmp/action
    
    sort  ~/tmp/action | uniq -c |sort -g -r > ~/tmp/histogram
    

    Which produced results of how often an given controller#action was accessed.

    4394886 MyController#index
    3237203 MyController#show
    1644765 MyController#edit
    

    Next step is to compare it to the list of all controller#action pair in the app (using rake routes output or can do the same script for testing suite)

    0 讨论(0)
提交回复
热议问题