Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Ternary operator (there's no Trinary operator )

by davido (Archbishop)
on Jul 19, 2006 at 07:04 UTC ( #562203=perlmeditation: print w/ replies, xml ) Need Help??

So... which is it?

As of July 2006, Google shows over 420k possible hits for the search term ternary operator. The first 20 hits include Wikipedia: ternary operator, and links to the official documentation for several languages (PHP, Java, and Python). Perl's own perlop also refers to the ?: operator as a "ternary operator". Wikipedia tells us that the term 'ternary operator' comes from the mathematical term 'ternary operation'. It's sometimes (according to wikipedia) called a tertiary operator, as it takes three arguments.

If you google for the search term trinary operator, you get (as of today) almost 28k hits, along with the familiar Google helper, "Did you mean: ternary operator." There are no Wikipedia hits for it, though there is a wikipedia hit for 'trinary' (the word, not an operator).

Given that there are nearly 28 thousand references to "trinary operator", versus 420k hits for 'ternary operator' turned up by Google, it's obvious that there is a significant propensity for name confusion. Here at the monastery we occasionally see posts about the ternary operator, where the node's author mistakenly calls it the trinary operator. Occasionally such nodes get retitled as a hazard to navigation and search-ability.

The purpose of this meditation is to clarify which term is correct in the context of Perl's ?: operator, as well as to provide a quick-reference node title that will be found anytime someone searches for either ternary or trinary here at the Monastery. This should serve as a pre-emptive aid to navigation, reducing the temptation to retitle 'trinary' nodes. I post this as a Meditation, despite its strong "PerlMonks Discussion" flavor because I believe the discussion has merit beyond the scope of only pertaining to the Monastery's goings-on.

So, what's the verdict? Perl's POD, Google's number of hits returned, Wikipedia's blurbs, and the documentation for other popular languages all seem to agree that with regards to the ?: operator, it's called a ternary operator

From a research perspective, those searching for information on the Ternary operator, here at the Monastery, as well as on Google, would probably find additional beneficial posts by searching for the "misspelled" term, Trinary. But the existence of nodes with both 'spellings', and the associated ambiguity shouldn't detract from the fact that Perl has a ternary operator, not a trinary operator.

...or maybe I'm just confused, this is a meditation, after all. ;)


Comment on Ternary operator (there's no Trinary operator )
Download Code
Replies are listed 'Best First'.
Re: Ternary operator (there's no Trinary operator )
by GrandFather (Sage) on Jul 19, 2006 at 07:15 UTC

    From The Camel (Programming Perl (Third edition): section 3.16. Conditional Operator):

    As in C, ?: is the only trinary operator.

    The Camel also comments in the introduction to Chapter 3:

    Perl operators come in three arities: unary, binary, and trinary (or ternary, if your native tongue is Shibboleth).

    Update: in light of some later replies to the OP perhaps I should note that as Perl has only one terniary/trinary operator, and given The Camel's use of 'trinary', it is reasonable in the context of Perl to refer to the conditional operator ?: as 'the trinary operator'. This may not be "correct" in a wider sense, but is certinally fitting in a Perl context. :)

    DWIM is Perl's answer to Gödel

      Heh. Just because it's in the Camel book doesn't mean it's right!

      I started writing C code over 20 years ago and as far as I can recall, I have never heard it called anything else than the ternary operator outside Perl. I've lost my copies of K&R, both editions 1 and 2, so I can't look it up. Stroustrup side-stepped the issue in C++ and called it a conditional statement.

      My own take on the matter is that calling it trinary is never going to go anywhere; the other terminology is too well entrenched, much the same way that programmers jump up and down in anger when the mainstream media refers to black-hats as "hackers", instead of the more semantically precise term "crackers". It's a lost cause.

      • another intruder with the mooring in the heart of the Perl

        Stroustrup side-stepped the issue in C++ and called it a conditional statement.

        I call it the conditional operator (or the ternary conditional operator to avoid confusion). "Ternary operator" (or "trinary operator") is a description, not a name. Calling it the ternary operator is like calling me "Laurie Brine's child" in favour of using my name.

        Update: Hey! perlop calls it that too!

      Lol, good job on uncovering a reliable and trusted source that manages to further the ambiguity and confusion. I should have guessed the Camel would discredit my pontification, though I'm not sure if it discredits or instead, illustrates my point. Let's get it all out on the table and arm wrestle for the rights to pick which term is going to survive. There can be only one. (Highlander)


        I see trinary somewhat as a shibboleth among Perl cognoscenti in much the same way that Perl itself is, or indeed as stumbit is among Perl Monks. It is these small things that distinguish us from users of other run of the mill boring languages and that enrich our use and immersion in our chosen language, that provides part of the solution and will prevent Perl from dying out.

        DWIM is Perl's answer to Gödel

        There can be only one

        I disagree - there can only be three. The third will encompass all, and be the ultimate truth.

        -=( Graq )=-

        This is not just a perl issue, though. ?: is most often called the ternary operator in several programming languages. I don't even see the Camel as cited as disagreeing; it just says that the operator whose name is "the ternary operator" is a member (and the only one, in perl) of the class of trinary operators.

      In my copy of the 2d edition of Programming Perl, it says this: "Trinary ?: is the conditional operator, just as in C." (p 91).

      It's also indexed under "trinary", not "ternary". At least one of the authors or editors of the book thinks "trinary" is a perfectly good word.


      e(π√−1) = −1
Re: Ternary operator (there's no Trinary operator )
by Aristotle (Chancellor) on Jul 19, 2006 at 07:15 UTC, when asked for definitions of “trinary”, finds nothing and helpfully suggests: did you mean “urinary”? I’ll let that fact stand as its own commentary.

    Makeshifts last the longest.

Re: Ternary operator (there's no Trinary operator )
by swampyankee (Parson) on Jul 19, 2006 at 10:13 UTC

    It sounds like "trinary" is joining "irregardless" as a Word That Shouldn't Be.

    I can see why trinary shows up: it looks like the logical result of (binary++). Incidentally, you will also see monoary as the logical result of (binary--) in the world of polymer chemistry.

    binary++ = ternary

    ternary++ = quaternary


    e(π√−1) = −1
Re: Ternary operator (there's no Trinary operator )
by jdhedden (Deacon) on Jul 19, 2006 at 17:07 UTC
    From perlop ,we have:
    Conditional Operator
        Ternary "?:" is the conditional operator, just as in C.
    Which says that ?: is a ternary-type operator (meaning it has 3 components), and that its name is "the conditional operator".

    "Trinary" is just not a word - it's a lexical error.

    "Tertiary" is a description of ordinality, and means "the third position". Thus, for ?:, the tertiary argument is returned when the conditional is false.

    Remember: There's always one more bug.
      Trinary certainly is a word; in this context is a synonym for ternary.

      Be well,

Re: Ternary operator (there's no Trinary operator )
by ambrus (Abbot) on Jul 19, 2006 at 08:42 UTC

    I think someone should look what the operator is called in the English original of K&R, and we should probably stick to that term. (If it's always referred to as something like, say, "conditional operator" then we're out of luck.)

      My copy of K&R (second edition) says 'ternary operator':

      "The conditional expression, written with the ternary operator ``?:'', provides an alternate way to write this and similar constructions."
      from section 2.11
Re: Ternary operator (there's no Trinary operator )
by rinceWind (Monsignor) on Jul 19, 2006 at 10:20 UTC

    I've also heard it called the "tertiary operator", which has 1.6M hits on Google.


    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

      You have to put quotes around the words in Google to get a correct count on the phrase as a whole. With quotes, "tertiary operator" only returns 598 hits. (Without quotes you're getting hits on any page with both words in it, but not necessarily next to each other.)

      Remember: There's always one more bug.

      Do these same people call binary + a "secondary" operator and unary - a "primary" operator?


      e(π√−1) = −1
Re: Ternary operator (there's no Trinary operator )
by spiritway (Vicar) on Jul 19, 2006 at 18:42 UTC

    You've got a good point. I don't know what would be involved, nor how practical it would be, but putting something like Google's "do you mean foo?" in Super Search might be helpful. If that's impractical, then maybe a sort of Wiki-like "disambiguation page" for the more common mistakes. Ternary/Trinary would be a good one. Can't think of anything else right off, but surely there are plenty more.

    OTOH, I suppose that if I did enter something about trinary operators, I'd encounter questions using that term, and see the corrections offered by the Monks - so I'm not sure that it would be worthwhile to go to the trouble of making such changes. I wonder whether this is a big enough problem to "fix" anything.

Re: Ternary operator (there's no Trinary operator )
by ysth (Canon) on Jul 19, 2006 at 20:39 UTC
    For fun, go through perlop and see how many punctuation (punctual?) operators have actual English names given. Operators are usually described like
    Binary "-" returns the difference of two numbers.

    The closest thing we have to a canonical list of names might be the PL_op_desc array in opcode.h in perl.git (amusingly enough, not in opnames.h in perl.git).

Re: Ternary operator (there's no Trinary operator )
by ww (Bishop) on Jul 19, 2006 at 15:50 UTC

    So if my toddler (would that I were still of an age to have cookie-crumblers and rug-rats) calls the kitten "Dawg," does that make references to felines as "canines" correct?

    I think NOT!


    Time and cultural changes encourage shifts and changes in language, but usages which obfuscate meaning (or distinctions among meanings) impoverish our ability to be precise.

Ternary operators
by pemungkah (Priest) on Jul 25, 2006 at 23:12 UTC
    Just to add to the confusion:

    The ternary operator, ?: has a binary value: one for true, another for false.

    The binary operators <=> and cmp have ternary values: -1 (left less than right), 0 (arguments equal), and +1 (left greater than right).

    That's really a lovely symmetry.

Re: Ternary operator (there's no Trinary operator )
by jdporter (Canon) on Jul 19, 2006 at 18:13 UTC

    "Trinary" is Just Plain Wrong. Are operators taking two arguments called "binary" for that fact? No.
    Binary ops deal with binary values (either at the boolean or bit-wise level). A trinary operator would deal with three-state values — for example, in a logic system that allows 0, 1, and undef/null.

    "Tertiary" also doesn't make sense, since it means "third". Clearly, :? isn't "the third operator".

    I guess that leaves "ternary" as the best — if not ideal — choice.

    Update: D'oh!

    We're building the house of the future together.

      Are operators taking two arguments called "binary" for that fact? No.

      Uh, yes. Yes, they are.

      Just check perlop, if you don't believe me.

      Or, if you don't find the Perl documentation clear enough, check Wikipedia on binary operation: "an operation whose arity is two".

      (In my experience, operators dealing with binary values are commonly refered to as either boolean or bit operators.)

      print "Just another Perl ${\(trickster and hacker)},"
      The Sidhekin proves Sidhe did it!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://562203]
Approved by cog
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2015-11-25 01:17 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (666 votes), past polls