Monday, May 6, 2024

Why Uniform Function Call Syntax matters

Uniform Function Call Syntax (UFCS) is a computer language pattern that allows commands/procedures/functions to be called in visually different ways.  Why is this important?  Why should any programmer care?

Readability.

90% of programming time is reading what somebody wrote in the past, not writing new code.
90% of programming is bug fixing and refactoring.

Oh, wait, what is refactoring?  Changing the code to make it more readable.  Not faster.  Not more efficient.  More readable.

UFCS allows code to be more readable.  That's all it does.  That's all it has to do.

Which of these is more readable to you?

                                                                                                 Listack
1 add 2        Swift                    operator / infix style                1 add 2
add 1 2        Haskell                prefix style                               add: 1 2
add(1, 2)      C                         procedure call style                  add(1, 2)
(add 1 2)      LISP                    Lisp style                                 (add: 1 2)
1 2 add        Forth                    method call / postfix style       1 2 .add

These are all valid program snippets from different languages.  They are all readable to varying degrees.  They are all in different styles.  They are all equivalent.

UFCS, especially as implemented in Listack, allows you to use whichever one is more appropriate at the time.  Want to us standard function call?  Do it.  Want to chain together a bunch of functions in a pipeline on a single piece of data?  Do it.

Function chaining example - which is more readable?  They all do the same thing, resulting in 3.  (There are more possible variations.)

1 5 3 .sub .add
(add: 1 (sub: 5 3))
add: 1 sub(5, 3)
add(1, sub(5, 3))
1 add sub(5, 3)
1 (5 sub 3) .add

Listack has the additional advantage that infix operators (+, -, etc.) are not special.  So you can use a simple '+' instead of 'add' when creating your own procedures.

1 + 2     --> 3
"Hello " + "there!"     --> "Hello there!"
myCustomThing1 + myCustomThing2        --> myCustomThing3

No comments:

Post a Comment

I reserve the right to remove egregiously profane or abusive comments, spam, and anything else that really annoys me. Feel free to agree or disagree, but let's keep this reasonably civil.