1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Rep Power

    Automated code modification for adherence to coding guidelines.


    I have this task to restructure and clean huge amounts of code, including removal of junk code, changing functions/variables names to adhere to naming conventions and similar changes.
    Can you please let me know of any tools which can help me automate it as much as possible.

    I came across coccinelle. Just wondering what are the other better options ?

  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Rep Power
    While there might be something out there, I haven't seen anything and I have done this a time or twain in the past. Cleaning up code is a huge pain in the ***, I don't envy you your task. If you know unambiguously that the code is built into a finite (and small) number of end products (libraries, executables, etc) then you could try what I have done a number of times and simply start removing chunks of code from the code base and rebuilding. Tedious, I know, particularly if the code base is huge. Once, several years ago, I tried to do some semi-automated analysis by using 'ctags' and 'nm' to find code in the repo that wasn't also in any of the binaries with a bit of success. As I recall (it was some 3 years ago at a different company) I took the output of one (nm, I believe) and 'diffed' it with the other to reveal the bits that were not the same, then started to remove those chunks from the repo and rebuilding. Of course, if no one is using some of your binaries (as was the case for me and ultimately why I stopped bothering) then this 'trick' won't really help.

    As for formatting, there are 'beautifiers' that can handle that aspect, I seem to recall something called 'bcpp' (or 'bc' for C code). I _think_ it would also modify comments for you as well, but it was quite a while ago when I played with it regularly.

    As for bringing variables, etc. into compliance, find and replace is your only friend here and it is quite possibly only a fair weather friend. If past developers didn't provide any sort of scope tags in their naming conventions (I personally _like_ that aspect of Hungarian Notation, though have since given up taking the data type to the name, for the most part, since modern IDEs will identify the type for you with mouse hover) then a wide-scale find and replace will likely cause more heartburn than it treats. While some IDEs will allow for find and replace in a selected block of code, another alternative is to copy the function/method to a temporary file, do find/replace there, then copy back. Again, quite tedious.

    An important note here: doing any sort of wholesale changes like this _without_ a comprehensive suite of regression tests is just asking for problems. Just because something compiles does _not_ mean that it functions correctly!

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo