Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a code with expiration date?

I just had this idea for something that I'd love to be able to use:

Let's say I have to fix a bug and I decide to write an ugly code line that fixes the immediate problem - but only because I promise myself that I will soon find the time to perform a proper refactoring.

I want to be able to somehow mark that code line as "Expired in" and add a date - so that if the code is compiled some time after that date there will be a compilation error/warning with a proper message.

Any suggestions? It must be possible to perform - maybe using some complicated #IF or some options in visual studio? I'm using VS 2005 - mainly for C#.

Thanks!

[EDIT]: Wow - never expected this question to raise so much interest :) Thank you all for your answers and for turning this into an interesting debate. I know it's hard to justify using anything like this - and I probably won't use it - but sometimes, when you have to ship a version YESTERDAY and you find yourself compromising on a patchy fix instead - you want to force yourself to fix it in the near future.

I chose MartinStettner's suggestion as the answer because it met my needs - no error on runtime - only during compilation, no need to define new types just for this goal - and it's not limited to a scope of an entire method. Cheers!

like image 621
Doron Zavelevsky Avatar asked Mar 07 '11 16:03

Doron Zavelevsky


People also ask

Can codes have an expiration date?

What About Those Can Codes? According to the Canned Food Alliance, canned foods don't have expiration dates. The codes on the back of your cans, which usually include a series of letters and numbers, refer to the date and place of canning.

How do you write the expiration date?

Expiry dates can come in these forms: DDMMYY, MMDDYY or YYMMDD. Some packaging use four digits to reflect the year while others use only the last two numbers. Such discrepancies are confusing, especially for the less discerning. Besides the format, where the expiry date is displayed also leaves much to be desired.

How do you read a 4 digit date code?

(AIC) uses a 4 digit date code label either directly on the product if it fits, or on the packaging if the product is too small. The code has two parts with the first 3 digits signifying the day of the year and the last digit signifying the year.

How do you read the 5 digit expiration date?

For canned food and ready-to-eat meals, check the packaging for a 5-digit code that signifies when the food was manufactured. In this code, the first 2 numbers stand for the year, while the last 3 represent the day of the year.


2 Answers

Mark the code with the System.ObsoleteAttribute attribute, you'll get a compiler warning, which will nag you to fix the code

[Obsolete("You've an ugly hack here")] public void MyUglyHack() { ... } 

Alternatively . . .

Write your own attribute, passing it an expiration date on the constructor, in the constructor throw an exception if DateTime.Now >= expirationDate.

The compile will fail until you fix the code (or more likely increase the expiration date, or far more likely you just remove the Attribute.

like image 88
Binary Worrier Avatar answered Sep 28 '22 00:09

Binary Worrier


oooohhh - this is 'orrible. try this for a giggle:

[AttributeUsage(AttributeTargets.All)] public class BugExpiryAttribute : System.Attribute {     // don't tell 'anyone' about this hack attribute!!     public BugExpiryAttribute(string bugAuthor, string expiryDate)     {         DateTime convertedDate = DateTime.Parse(expiryDate);         Debug.Assert(DateTime.Now <= convertedDate,              string.Format("{0} promised to remove this by {1}",                  bugAuthor, convertedDate.ToString("dd-MMM-yyyy")));     } } 

then, decorate your method/class etc:

[BugExpiryAttribute("Jack Skit", "2011-01-01")] public static void Main(string[] args) { ... } 

... nasty :-)

[DISCLAIMER] - created in the name of academic interest, not production code finese!!

[edit] - just to clarify, code compiled and in production will continue to run on/after the 'bugExpriryDate'. only once the code is run in the compiler (on/after the date), will the warning message be raised (debug.assert). just thought it worth making that distinction - cheers MartinStettner.

[caveat] - if used in classes/methods etc would need to be read via reflection. however (and this is interesting) will work straight off in the compiler if used on sub Main(). how strange!! (thanks for the nod Hans...)

like image 34
jim tollan Avatar answered Sep 28 '22 00:09

jim tollan