Would you see any use of a Trilean (True, False, ??)

Posted by Sybiam on Programmers See other posts from Programmers or by Sybiam
Published on 2010-12-30T14:44:12Z Indexed on 2010/12/30 15:01 UTC
Read the original article Hit count: 146

Filed under:

I don't know if I'm alone but sometimes I have a function that should return true or false. But in some case there would be a third result that would make more sense.

In some language theses cases would be handled with integers or with exception.

For exemple you want to handle the age of a user if he is over 18 years old. And you have a function like this.

if(user.isAdult(country_code)){
     //Go On
}else{
     // Block access or do nothing
}

But in some case depending how your app is built I could see case where the birthday field is incomplete. Then this function should return something undetermined.

switch(user.isAdult()){
    case true:
        // go on
        break;
    case undetermined:
        //Inform user birthday is incomplete
    case false:
        //Block access
}

As i said we can handle that with Exceptions and Int, but I would find it quite sexy to have a true, false, undetermined embeded in the language instead of using some home defined constants.

© Programmers or respective owner

Related posts about syntax