By Mark Wilston

Safely Deprecating APIs

By Mark Wilston
Last chance to win! You'll get a... FREE 6-Month Subscription to SitePoint Premium Plus you'll go in the draw to WIN a new Macbook SitePoint 2017 Survey Yes, let's Do this It only takes 5 min

Deprecation is a position in the software development lifecycle where a certain feature needs to be removed or avoided because it’s out of date. Deprecated features are not done away with immediately; rather they are retained but raise warning messages that recommend using alternative practices. This gives users time to update their code before the unwanted code is finally removed.

Deprecation can happen for various reasons – perhaps an API is no longer useful and has reached its end-of-life, or the refactoring of code to improve its reusability and testability obsoletes particular methods.

In this article I’ll share with you some key points that you should follow when deprecating APIs so you can continue to grow your code and provide fair warning to those who depend on it.

Prepare for Refactoring

Even though it is tedious, make sure you first document your code. This will assist you in better understanding what code is responsible for what functionality and how it functions as a whole. Use a standardized document format so it can be used by your IDE and easily compiled into readable documentation.

Then, to ensure your service or library continues to work after you deprecate parts of its API, test the original code with your unit tests. If the tests fail, you must fix the problems so you can distinguish between failures that were brought by deprecating the API and failures that were already there.

Employ the Single Responsibility Principle

Logically organize your code based on the purpose and function of various components, and employ the Single Responsibility Principle (SRP). The principle ensures the methods and classes that perform more than one job are refactored so each has a single task. This increases the reusability and testability of your code. Don’t worry if there are many classes and methods in your API as a result of applying the SRP; reusable and testable code allows deprecating changes to be targeted to a minimal amount of code.


Communicate with your Users

Depending on the size of your user base, establishing good communication can be difficult. A good solution is to establish a mailing list to which they can subscribe. The period of time that you can allow users to continue using old methods once you’ve announced the intended changes on the list depends on factors like the importance and why the change is being made.
Depending on your API and the resources available to your project, it might also be beneficial to host a project wiki where developers can share alternative strategies and workarounds to avoid deprecated methods calls.

For libraries, the methods and classes that provide your API should be updated to issue descriptive warning messages that can be logged when they’re used. If you’re providing a web service, define headers that do not affect the current implementation but which allow you to include metadata with the service call. These headers can allow you communicate with your users about service changes.

Remove the Old Code

After making the necessary changes, be sure to read through your code carefully to make sure the unneeded, unused, or old code, is safe to remove. Code analysis tools can be helpful as well.

When a suitable deprecation period has passed, you can go ahead and prune the old code. After removing code that is no longer necessary, it is important you perform tests to make sure the system is still functioning properly so as to avoid failures.


Deprecation can improve the internal consistency of a web service or library in the software code, its structure or the user interface. This makes it easier for users to understand how the system works reducing the cost of coming up with a new system.
Refactoring helps to improve the user experience of your API and makes future changes to your code easier to implement. Code becomes more reusable and testable. But the process of refactoring also means some code because unnecessary and should be removed to keep the project clean.

Deprecating features before they are done away or transformed gives users who depend on your code time to update their own code. By following the advice in this article, you will find it easier to keep your code fresh and your users won’t have any unhappy surprises.

Image via Fotolia

Login or Create Account to Comment
Login Create Account
Get the most important and interesting stories in tech. Straight to your inbox, daily.Is it good?