views:

228

answers:

6

I have a class with the fields "deletionDate" and "experiationDate" which could both be undefined, what would mean that the object is whether deleted nor has an expiration date.

My first approach was:

private Date deletionDate = null; // null means not deleted

Having the book "Clean Code" in mind I remember to better use expressive names instead of comments. So my current solutions is:

private static final Date NEVER = null;
private Date deletionDate = NEVER;

I could user a wrapper class around Date, but that would complicate JPA mapping.

What do you think of it? How would you express "never"?

A: 

I would just choose a far-future date as the value for the constant NEVER. Then to check for deletion/expiry, just compare against NEVER.

Roy Tang
That would be year in the 8099.
Aleksi
The possible issue with that is if you're doing any range or ordering operations then having a large but real value may do something unexpected.
GaryF
Noooooo! Don’t use a future date! Haven’t you learned anything from Y2k or SpamAssassin? :(
Bombe
A: 

Let the default value be treated as "never"

Sri Kumar
+9  A: 

well never is never, not the 1/1/2999.

I would stay with your 1st solution. a Null date means it has not yet happened.

maybe you can wrap it with something like :

boolean isNeverDeleted(){
    return deletionDate == null;
}
chburd
+1. Never is never. ^
KB22
Also +1 for never is never, but note that the problem with using null to mean "set to never" is that it means you don't then have a way to indicate "not yet set". It's an application question whether that matters, but it's quite easy to imagine the OP later discovering a need to keep track of whether the value has been set, leading to needing a second "boolean hasBeenSet" variable for each, which would be icky.
CPerkins
+3  A: 

You can think about null date as "not available" or "not applicable". If that's the case "NO DATE" is fine for "never".

Don't subtype Date only for a very exquisite style requirement.

A better option is to add semantic to your model object. If your have a Thing object with a deletionDate property you can do:

class Thing
+ deletionDate
+ isNeverDeleted: boolean { return deletionDate == null; }

and it will be practical and documentative, both in the class and in your client code:

if(myThing.isNeverDeleted())
helios
+3  A: 

I consider null appropriate. It clearly indicates "not set".

Depending on how complicated you want to get, though, you could have a Enum and have some state like 'NeverExpires' as the 'UserState' (or whatever it is you're representing). This is probably preferable, but could be uselessly complex, depending on what your system.

Noon Silk
A: 

I would not use Date but timestamps, using -1 for never and 0 for immediately;

public static final long IMMEDIATE = 0;
public static final long NEVER = -1L;
private long expires = NEVER;

interpretation of the attribute should be in a getter, like:

public boolean isExpired() {

    return (NEVER == expires) ? false : (expires < System.currentTimeMillies());
}

Deletion follows the same pattern.

Update I know that 0 and -1 are valid timestamps, but as expiration and deletion of files and other resources rarely (never say never :-)) happen in 1970 or before, it is a useful constant, imho.

rsp
-1 is a valid timestamp (23:59:59.999 UTC on December 31st, 1969).
jarnbjo