December 19th, 2009, 06:38 PM
Design Pattern suggestion: Neutral Element instead NULL
i had the idea that this might be an interesting approach especially in handling numerical database null values. -> Tell the app that numerical null values (this are e.g. null values in integer, float, decimal, .. database columns) should be handled as neutral elements regarding to any mathematical operations (e.g. for addition this would be a 0 (zero), for multiplication a 1 (one) and so on).
Of course this is not always what you want, so there are still the cases where you want an exception to be thrown (so take it as null value) or to have it some default value (e.g. 0 _or_ 1 or any other value of course).
I guess there would be needed some more input, especially on how to apply this to string values (are there neutral strings? Maybe that is just the same as the empty string? Whats the neutral string for numerical null values -> probably the empty string as well.
What do you think? Interesting approach or not?
but more important: How could I implement this the best way? Maybe by extending the 'Null Value Pattern' to behave like this, (maybe NeutralNull) ?
December 20th, 2009, 04:36 AM
Re: Design Pattern suggestion: Neutral Element instead NULL
I see how this is conceptually related to the Null Object pattern, where you have an object that implements the common interface but all its methods are neutral (effectively do nothing). For this to be effective, you need objects and polymorphism.
I don't really see how this would be done for your database values - unless their behavioural scope was very limited. You could wrap the values in objects implementing a common 'Numeric' interface (what would that look like?) and wrap the null values in a NullObject, but surely, in a sensible design, numeric values that are 'null' have 'no value' rather than 0 or 1, which means they should not be involved in arithmetic expressions. It seems to me that null values in general cannot and should not participate in mathematical operations.
If certain null database values should take some default value, then give them that value when they are read from the database - isn't that what 'default' means?
Perhaps you have some particular application in mind, but to me, using null numeric values in operations just smells bad. Can you suggest a practical example where it would be useful?
The cheapest, fastest, and most reliable components of a computer system are those that aren't there...
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Tags for this Thread
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.