DEV Community

Cover image for CS Trivia#6: Where did the spaceship operator come from?
Sylwia Vargas
Sylwia Vargas

Posted on

CS Trivia#6: Where did the spaceship operator come from?

Welcome to my little CS trivia series. Every week I post a new trivia piece.

Today's question is...


Where did the spaceship operator come from?

The spaceship operator looks like <=> and its official name is the “three-way comparison operator” or "Comparable" (in Ruby). Before we dive into its naming history, let's see what this operator does in programming languages. Currently, the programming languages that use this operator are: Perl, Ruby, Apache Groovy, PHP, Eclipse Ceylon, and C++. There were proposals to add it to other languages, e.g. Javascript, or Dart.

What does it do, really?

This logical operator checks a statement three fold:

  if a < b, return -1
  if a = b, return  0
  if a > b, return  1
  if a and b are not comparable return nil
Enter fullscreen mode Exit fullscreen mode

Therefore, instead of writing a few else/if statements, you can keep it short by just using the spaceship operator.

Where did the name come from?

In the Google Group devoted to Dart Committee Meeting, Perl expert Randal L. Schwartz mentioned that he had coined the name "spaceship operator":
Randal L. Schwartz's message

He explains:

In writing, you can just get away with saying "the <=> operator", but
when teaching, I got really tired of saying "the less-than, equal,
greater-than operator", so I noticed it reminded me of the spaceship in
an old HP BASIC "star trek" game, so I started using that, and it caught
on, even finding its way into an error message, and thus becoming
official.


Discussion (0)