Home > Refactorings > How to safely rename a public API member and mark it obsolete

How to safely rename a public API member and mark it obsolete

July 31st, 2012

There are times when public API members must be deprecated or its signature must be changed. In this case, we can not remove or modify public members, because other application that depend on these APIs may become broken and/or uncompilable. If you’d like to supersede a public member, you can use a special attribute called ‘Obsolete’. The Obsolete attribute marks an API member as one that is no longer recommended for use.

The Obsolete attribute is useful for refactoring and general management of your project’s source code over the development life cycle. Each use of a member marked obsolete will subsequently generate a warning or an error, depending on how the attribute is configured.

To create a new public member based on the old one, you can utilize the Safe Rename refactoring. The refactoring creates a copy of a non-private method or property in the class in a manner that will not break any dependent code, such as code in third-party applications that use that member.

Consider the following public member:

CodeRush Safe Rename sample code

Since you know that you will want to deprecate this code, marking the method with the Obsolete attribute will be a constant reminder for you and code consumers that its use is not recommended. The displayed message can also explain the reason why the member is obsolete as well as provide an alternative. The refactoring adds the corresponding message for you once you apply it:

CodeRush Safe Rename result

It also adds the EditorBrowsable attribute which specifies the browsable state of a property or method from within the code editor.

By providing a permanent reminder about unrecommended code, the refactoring helps in versioning software programs and coordinating the methods employed by different developers. This technique plays a vital role in the design of software that gets updated with newer versions and needs to be used by other consumers without breaking the existing applications. That is why it allows you to maintain the software’s backward compatibility and code maintainability throughout the development life cycle.

—–
Products: CodeRush Pro
Versions: 12.1 and up
VS IDEs: 2008 and up
Updated: Jul/31/2012
ID: R060

Similar Posts: