• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to remove old 'if' conditions programatically

 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Automatic way I do not think so.

I think you could remove one by one for properties configurations or so.
 
Sheriff
Posts: 28328
96
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hm... Thank you all for your responses.
 
Paul Clapham
Sheriff
Posts: 28328
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maulin Vasavada wrote:



Yes, I agree. Where I work we also have a large codebase with many unused and obsolete features.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 am displeased. You are no longer allowed to read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic