-1

I need to remove a particular function or a class from my Java code when it is being converted into .jar file using Maven. But the caveat is the function or class should stay inside the source code.

Is there any such way in which I can achieve this using Maven and/or any Java utilities?

(there are a lot of functions ~400 and their implementations are very large as well therefore commenting the code is not an option)

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 2
    What exactly do you want to achieve with this? What gain do you have over commenting out the code yourself? – sm3sher Nov 16 '22 at 08:34
  • Actually I am working on a project where we have to depricate some functions. But we do not want to remove them from source because they have some important practices which will be required in further release but at the same time, we do not want the code to reach the end user. – Harsh Paradkar Nov 16 '22 at 08:40
  • https://meta.stackexchange.com/a/66378 – Mike Nakis Nov 16 '22 at 08:45
  • @MikeNakis Actually, I suggested the same thing to my team. But, they told me there are a lot of functions ~400 and their implementations are very large as well. – Harsh Paradkar Nov 16 '22 at 08:59
  • And also I have been told to do so, therefore I have to implement this I just need to figure out how, therefore any help would be appreciated. – Harsh Paradkar Nov 16 '22 at 09:02
  • Simply this class is wrongly written. 400 lines of code is too long.. That needs to be refactored. You need unit tests. Commenting out for running or alike is wrong... and no there is no such plugin/utilitiy.. – khmarbaise Nov 16 '22 at 09:07
  • @khmarbaise there are not 400 lines there are around 400 functions and classes that are needed to be removed. And commenting all of them out would take me days if not weeks. – Harsh Paradkar Nov 16 '22 at 09:13
  • Why remove these methods? What is the reason? – Thorbjørn Ravn Andersen Nov 16 '22 at 09:15
  • One idea: https://binkley.blogspot.com/2014/12/writing-your-own-lombok-annotation.html (not sure if that allows to *remove* code). – assylias Nov 16 '22 at 09:18
  • Why not just delete the code? If you need to refer to it later, you can always use your git (or other source control system) revision history to find it again. – Mark Rotteveel Nov 16 '22 at 09:35
  • @HarshParadkar even worse. That means the code sounds wrong..??? – khmarbaise Nov 16 '22 at 09:57

1 Answers1

2

Okay, so the real problem is this:

We have a code base which includes certain parts that are not currently being used, but they may be used in the future, so we want to keep them in the code base, but we do not want them to be shipped to customers. (Due to, uhm, reasons.) What are the best practices for achieving this? Note that commenting them out would be impractical.

The proper way to achieve this is to extract all those parts into a separate module, and refrain from shipping that module.

The hacky way is to use a hard-coded feature flag.

A normal (non-hard-coded) feature flag is a boolean which controls a certain aspect of the behavior of our software. For example, if you build an mp3 player application, and you want to add support for the aac file format, but you do not want to ship support for it yet, then you might want to create a boolean supportAacFeatureFlag() method, and have all code that pertains to the aac file format invoke that method and check the return value before doing anything. It is important to note that this must be a method, not a constant, so that its value is not known at compilation time, because every single if statement that checks the value of a constant is bound to yield a "condition is always true" or "condition is always false" warning. The great benefit of feature flags over commenting-out code is that the code controlled by a feature flag must still compile, so it must be semantically correct. The problem with feature flags is that they do not eliminate the code; the code still gets shipped, it just does not get executed.

A hard-coded feature flag is a feature flag which is implemented using a constant. The constant condition warning will be issued in every single use of that flag, so it will have to be globally disabled. (That's why this approach is hacky: we normally want all warnings enabled.) The benefit of using a constant is that its value is known at compilation time, so even though the compiler will still compile the controlled code, it will refrain from emitting any bytecode for it, so the code essentially does not get shipped to customers. Only empty functions get shipped.

Note that this is documented behavior of the Java compiler. In other languages like C++ and C# the compiler always emits all code, and you have to use other means of controlling code generation, like #defined symbols, which, in my opinion, are also very hacky.

An alternative way which I am sure some people will opt for but I would strongly advice against is to keep the unused code in a separate feature branch and remove it from the master branch. I would strongly advise against this, because any refactorings applied to the master branch will not affect the feature branch, so the code will diverge, so it will be a nightmare to integrate it in the future.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142