Vertically Aligning Roman Numerals in Code

I have a PHP function which uses Roman Numerals. It looks like this: $romanNumerals = [ "Ⅿ" => 1000, "ⅭⅯ" => 900, "Ⅾ" => 500, "ⅭⅮ" => 400, "Ⅽ" => 100, "ⅩC"

I have a PHP function which uses Roman Numerals. It looks like this:

$romanNumerals = [ “Ⅿ” => 1000, “ⅭⅯ” => 900, “Ⅾ” => 500, “ⅭⅮ” => 400, “Ⅽ” => 100, “ⅩC” => 90, “Ⅼ” => 50, “ⅩⅬ” => 40, “Ⅹ” => 10, “Ⅸ” => 9, “Ⅷ” => 8, “Ⅶ” => 7, “Ⅵ” => 6, “Ⅴ” => 5, “Ⅳ” => 4, “Ⅲ” => 3, “Ⅱ” => 2, “Ⅰ” => 1 ];

The problem is, the operators don’t line up and the whole thing looks messy. Why? Because the Unicode Roman Numerals (https://shkspr.mobi/blog/2023/03/unicode-roman-numerals-and-screen-readers/) are not monospaced! ⅭⅯ is a different width to ⅩC and Ⅷ is only a single character! Copy the above to a text editor and see if you can get neat columns. I bet you can’t!

I’m obsessed with vertically aligning my code (https://shkspr.mobi/blog/2014/11/why-i-vertically-align-my-code-and-you-should-too/). So how to solve this ugly problem?

The answer was simple (https://phpc.social/@Crell/115329116036130430). Assign keys to the values and then flip the array!

$romanNumerals = array_flip([ 1000 => “Ⅿ”, 900 => “ⅭⅯ”, 500 => “Ⅾ”, 400 => “ⅭⅮ”, 100 => “Ⅽ”, 90 => “ⅩC”, 50 => “Ⅼ”, 40 => “ⅩⅬ”, 10 => “Ⅹ”, 9 => “Ⅸ”, 8 => “Ⅷ”, 7 => “Ⅶ”, 6 => “Ⅵ”, 5 => “Ⅴ”, 4 => “Ⅳ”, 3 => “Ⅲ”, 2 => “Ⅱ”, 1 => “Ⅰ” ]);

There! Doesn’t that look much neater!

As was written long ago (https://libraries.mit.edu/150books/2011/05/11/1985/):

A computer language is not just a way of getting a computer to perform operations but rather … it is a novel formal medium for expressing ideas about methodology. Thus, programs must be written for people to read, and only incidentally for machines to execute.

Write a comment
No comments yet.