So, I was writing some code...

...and in the code was a part where I wanted to use the proper ordinal suffixes ("st", "nd", "rd", "th")--but in the clarity of a sleepy daze, I didn't want to use a function call or an if-statement; they seemed too clunky.

My tired mind settled on this:

(n<10?(n%10==1?("st"):(n%10==2?("nd"):(n%10==3?("rd"):"th"))):(n>20?(n%10==1?("st"):(n%10==2?("nd"):(n%10==3?("rd"):"th"))):"th"))

It's exactly as long as my IDE's text window (I'm using an old nightly build of Code::Blocks from shortly after C++11 came out).

As for the program, it was a simple test to see if I remembered how to use recursive calls effectively by finding the nth number of the Fibonacci sequence.
Last edited on
Definitely shorter than an if but, in terms of readability, would you pick this over an if statement?

Personally, I wouldn't but I'm not a big fan of using the conditional/ternary operator.
Ternary is a pretty slick operator, but it absolutely kills all readability when used like this. Just think if you were someone else reading this, would you be impressed by whoever the hell wrote this?
I thought it'd be easier to read for a second. And then that happened.

RB, I wouldn't dare do this to a program if someone else was going to read my source code. I'm pretty disappointed, actually. But, it is funny.
(n<10|n>20?(n%10==1?"st":(n%10==2?"nd":(n%10==3?"rd":"th"))):"th")
DRY Try to kill that last "th"
Can't argue with that logic.
White space and comments could go a long way with this one.
Topic archived. No new replies allowed.