Java: Is it wrong to use deprecated methods or classes?

Let’s look at the documentation of @Deprecated:

A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists.

The authors doesn’t say it’s wrong but it’s discouraged. Wheather or not it’s reasonable to continue using a deprecated API even though it’s discouraged boils down the reason for the deprecation.

When it’s wrong

If the API has been discovered to be broken by design you should do your best to avoid using it. An example from the standard API is Thread.stop which has been deemed dangerous due to memory consistency issues. Using this method is indeed wrong.

When it’s not

If the API has been deprecated simply because there’s a newer and shinier way of solving the task, the existing API will most likely continue to function as advertised and you may deal with it according to your own discretion. Sooner or later you may want to address it, but it’s it’s definitely not wrong to prioritize other tasks.

To give an example on the low end of the scale: The FontMetrics.getMaxDecent. Reason for deprecation: Spelling error.

Deprecated. As of JDK version 1.1.1, replaced by getMaxDescent().

Comments

Be the first to comment!