Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse of Deprecated / Obsolete

Tags:

c#

I have a functionality in my code, but it will be available from next version. Is there any tags to notify that it is not in use, but will be available in future. I just have one way ie, [Obsolete] with custom message. Expecting other than this.Thanks in advance.

like image 221
N K Avatar asked Jul 05 '13 11:07

N K


4 Answers

If it is not available in the current version, don't make it part of the public API. Don't expose stuff that shouldn't be used by consumers of your library.

And even when it's not a library, don't add code that you don't yet need. That violates the YAGNI principle. Add the code only when it is actually needed.

like image 61
Steven Avatar answered Sep 28 '22 11:09

Steven


You can create throw a CustomError message from that method saying "This method is not available for the current version. It is for future" something like that.

Or else you can make the method as Obselete and specify the reason in the message. [Obsolete("I have written this method for future use. Do not use it in the current version.")]

like image 37
Sreejith V.S Avatar answered Sep 28 '22 12:09

Sreejith V.S


Judging from your question, I assume you want to generate a compiler warning when someone attempts to call this method, stating that it has not yet been implemented.

ObsoleteAttribute is hard coded into the C# compiler as something that should generate a warning message. Therefore there is no way to mark a method as 'to be implemented in the future' without modifying the compiler itself.

From the C# Spec:

The attribute Obsolete is used to mark types and members of types that should no longer be used. If a program uses a type or member that is decorated with the Obsolete attribute, the compiler issues a warning or an error. Specifically, the compiler issues a warning if no error parameter is provided, or if the error parameter is provided and has the value false. The compiler issues an error if the error parameter is specified and has the value true.

I suggest to not expose methods that are not yet implemented, since it makes no sense to use them. If this is part of a team development effort, specifying API contracts before developing specific modules, you should consider throwing a NotImplementedException or provide a dummy implementation.

like image 22
Bas Avatar answered Sep 28 '22 13:09

Bas


Reviving this question as there is now a real solution thanks to Roslyn: Code analyzers.

Just add this Nuget package to your project and you'll be able to create attributes that will be picked up by the compiler!

The GitHub repo readme has the instructions, and it's really simple.

like image 24
gregsdennis Avatar answered Sep 28 '22 11:09

gregsdennis