Default values for primitives

The use of specific JADE features and proposals for new feature suggestions
Stokes
Posts: 66
Joined: Wed Oct 13, 2010 2:06 pm
Location: QLD, Australia

Default values for primitives

Postby Stokes » Thu Dec 08, 2016 11:02 am

I'm not sure if this has been discussed before but I think a great new feature would be adding default values for persistent primitives when they are created.

If we add a Boolean to a class it would be nice if we could make the default TRUE in some cases. The easy way out of this is to just reverse the Boolean name which I don't like at all e.g. isNotAllowedAccess

I'm thinking about adding this to Parsys as an NFS but just wanted to raise a discussion topic first to see what else has been discussed in the past.

allistar
Posts: 156
Joined: Fri Aug 14, 2009 11:02 am
Location: Mount Maunganui, Tauranga

Re: Default values for primitives

Postby allistar » Thu Dec 08, 2016 3:21 pm

The standard (and slightly icky) way of doing this is to implement the create method and set the default value there. That doesn't help when instances of the class already exist and you're adding in a new property; in that case a script will be needed to set the values. I have seen many times the meaning of a Boolean be negated to get around this. Then you have code like: "if (not inactive) then".

I think it's a nice idea, but for me it would be near the bottom of a list of other nice ideas for Jade enhancements :-)

murray
Posts: 144
Joined: Fri Aug 14, 2009 6:58 pm
Location: New Plymouth, New Zealand

Re: Default values for primitives

Postby murray » Thu Dec 08, 2016 7:02 pm

I don't see anything "icky" about using the create() constructor method. It is good OO practice to initialise new objects to a valid default state, and that method fulfills the purpose. What's more, it's a declarative and expected pattern (no surprises).

I think @Stoke's suggested NFS could result in introducing some "surprising" or "unexpected" behaviour further down the line. While it does solve the problem with a one-off re-org, every new object created will now have a non-standard default value in that property. How many programmers, even in other languages, expect a Boolean to default to 'true'?.

I personally have employed the negatively-named Boolean for adding a property, but with careful choice of name, e.g. isDeniedAccess, instead of isNotAllowedAccess. It's easier with enumerated integer constants - just never use zero as a valid value. You can then add logic that detects the uninitialised zero and updates it to the required default.
Murray (N.Z.)


Return to “Feature Discussions”

Who is online

Users browsing this forum: No registered users and 5 guests

cron