When and why use the Obsolete attribute?

Asked

Viewed 135 times

5

At least in the projects I’ve worked on, I see no point in using the attribute Obsolete in methods that are no longer used. What I do, and I see people doing is removing or commenting on codes that are no longer used/obsolete.

When should I use the Obsolete attribute?

1 answer

6


In simple, internal things (no one outside your team or yourself will use your code) do not need to use it anyway. You have full control over the use and if that was used, you can quickly with the help of the IDE, and who knows any plugin, change everything that uses what was deprecated.

In codes that are used extensively and/or are made available to third parties this type of marking is essential. You cannot remove something that may be being used by third parties. It has to show that its use is not ideal, make the compiler indicate it to him. It may be that in future versions you can even remove, but the normal is to never remove, unless you launch a completely redesigned product.

This attribute is semantic documentation to indicate that that code should not be used in future projects and if possible should change to something more modern where it has already been used.

It is common to use a short message indicating what to use instead.

It is possible to indicate whether it will generate a Warning or build error. Ideally starting with a Warning and document when it will go wrong. It is good to release at least one version of the software giving obsolescence error before removing the code. When removing the error it will be another and will not give any indication that it has become obsolete. Probably adopting Semver.

Documentation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.