Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes How to remove old 'if' conditions programatically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to remove old Watch "How to remove old New topic
Author

How to remove old 'if' conditions programatically

Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi all,

We have a large code base and over time we have put lot of 'if' conditions to evaluate condition if a particular site feature should be turned on or off. Obviously after a while either feature was doing good and there was no way going back and 'if' condition always return true OR the feature was scrapped and we don't need the 'if' condition and want to keep 'else' logic. However the code is already in the mess of lot of 'if' (and corresponding 'else').

If it were just my code and few hundred files I'd have done it manually BUT I am talking about huge code base touching many developer's life (who are not even there anymore with the company ).

Is there any systematic way of doing this? Any external tools or research done to figure out and evaluate the runtime of the JVM and suggest which 'ifs' are useless now since they always evaluate to true?

I hope you get what I am asking for, otherwise please let me know and I'll try to be clearer.

Thanks
Maulin
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

Automatic way I do not think so.

I think you could remove one by one for properties configurations or so.


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

There's an old rule in the computing business which says "If it ain't broke, don't fix it".

Even if you are completely certain that a particular feature is no longer in use, there is still a risk in removing it from the code. You might, for example, remove the "if (...) {" part and its associated code, and then remove the wrong closing brace. You would probably have to run the code through the testing and acceptance process again (you said it was a large code base so I'm assuming you have that process).

As for automation: there isn't a program which can analyze a Java class and tell you whether a particular if-clause will be executed. However if you have the same code copied-and-pasted in many places (which does happen even in Java), you could conceivably scan the source code looking for that. And there might be tools to scan the source code to look for frequently-duplicated code. In fact I'm fairly sure there must be, although I can't name them.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hm... Thank you all for your responses.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Maulin Vasavada wrote:


Yes, I agree. Where I work we also have a large codebase with many unused and obsolete features.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2272
    
  28

You can run a code coverage tool and it will tell you which parts of your code aren't being executed. Or you can just use a text search tool like grep to find places where you have used a specific property

Having said that, probably the effort to remove dead code is going to be much larger that finding the dead code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to remove old 'if' conditions programatically
 
Similar Threads
where to use these things in good programming way
Ternary condition question
Non-programer scripting language for java?
unreachable code...
One alternative to Rules Engine when time is big money: Gurus please comment