# Refactoring APIs with Deprecation : Kotlin

# What is Deprecation of APIs?

> If get to know something new by reading my articles, don't forget to endorse me on [LinkedIn](https://www.linkedin.com/in/rommansabbir/)

A deprecated **API** is one that you are no longer recommended to use, due to changes in the **API**. While deprecated classes, methods, and fields are still implemented, they may be removed in *future implementations*, so you should not use them in new code, and *if possible rewrite old code not to use them*.

## In sense of Java/Kotlin:

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

Okay, let's simplify this. Think about a class, we have a or some important business logic (APIs) which are working fine & currently in production. There is nothing wrong with it, but clients want some changes or addition to the business logic. So, what do we do? Just simply working on the APIs changes? No. We should keep the current APIs as it is. *\[ If it's working, then don't touch it (:p) \]*

Instead of changing the **APIs**, we work on the new **APIs** with client changes and simply **Deprecated** the old **APIs** and replace **Deprecated APIs** with new **APIs**. Simple, huh? Yes. Also, by keeping the **Deprecated APIs** for a certain version helps you to revert back to the working version if something goes wrong.... But keep in mind that `Deprecation` means it will be removed from the codebase (if required) in future version and no longer maintain properly.

### Let's take a look at the coding part.

```plaintext
class SDK {
    fun doSomething(): String = "Working. Working.. Working..."
}
```

We have a class call `SDK` which contain a single API `doSomething()`. But client came up with some changes and we have written a new API called `doSomethingNew()`.

```plaintext
class SDK {
    fun doSomething(): String = "Working. Working.. Working..."

    fun doSomethingNew(): String = "Working. Working.. Working... 24 hours working..."
}
```

So, we have two APIs for the same task and it's confusing. So, what to do? We can simply `Deprecated` the old API.

```plaintext
class SDK {
    @Deprecated("Use new API")
    fun doSomething(): String = "Working. Working.. Working..."

    fun doSomethingNew(): String = "Working. Working.. Working... 24 hours working..."
}
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1645371163618/7kDBBe-fU.png align="left")

If we are using `Deprecated APIs` in our codebase, **IDE** will show the **Deprecation Popup**.

Let's take a look at the `Deprecation` annotation in Kotlin.

```plaintext
public annotation class Deprecated(
    val message: String,
    val replaceWith: ReplaceWith = ReplaceWith(""),
    val level: DeprecationLevel = DeprecationLevel.WARNING
)
```

`Deprecated` Annotation takes a `message`, `replaceWith` to replace the old API with new **API** \[by using **IDE (Alt+E)**\], `level` Deprecation level.

```plaintext
public annotation class ReplaceWith(val expression: String, vararg val imports: String)
```

`ReplaceWith` takes `expression`, the new **API** name including params, and `imports` to import packages if required.

```plaintext
public enum class DeprecationLevel {
    /** Usage of the deprecated element will be reported as a warning. */
    WARNING,
    /** Usage of the deprecated element will be reported as an error. */
    ERROR,
    /** Deprecated element will not be accessible from code. */
    HIDDEN
}
```

Let's update our SDK class with:

```plaintext
class SDK {
    @Deprecated("Use new API", ReplaceWith("doSomethingNew()"))
    fun doSomething(): String = "Working. Working.. Working..."

    fun doSomethingNew(): String = "Working. Working.. Working... 24 hours working..."
}
```

We have added `ReplaceWith("doSomethingNew()")` to identify that, *to replace with which API*. Our **IDE** will take care of that.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1645371792922/jkGfgQy9v.png align="left")

Just click on **Replace with** and **IDE** will replace the API.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1645371810131/8JRGvk4G5.png align="left")

Another fun fact.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1645371905766/3U-ZKMh2F.png align="left")

If you press, **Ctrl+Shift+Alt+T** (on Windows) and enable `Optimize imports` and `Code cleanup`, **IDE** will automatically replace the `Deprecated APIs` with new defined `APIs` for us.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1645372055724/qVtUmLbqY.png align="left")

Fun right?

Happy Coding.

Follow me on LinkedIn:

%[https://www.linkedin.com/in/rommansabbir/]
