Imagine that a method call doesn't consist of a method name and a list of its arguments. On the contrary, it looks more like a natural language sentence, is more agile and readable, but still well structured. Does it sound interesting?
In order to get closer to this idea, I like to use named arguments, i.e match(text='abcd', regex='\w+')
instead of match('abcd', '\w+')
. Particularly, it makes a lot of sense in case of deeply nested constructions, where an order of method arguments gets non-obvious and simply unshapely because of a cumbersome structure:
match(
stripped_text(
file(
file_path(
['tmp', 'xxx.txt']
),
'r'
)
),
'10$'
)
Here, one wouldn't so easily understand, what's going on. Really, what do these 'r'
and '10$'
mean? They are already out of context. Here, named arguments would come in very handy.
Let's get back to the first example: match(text='abcd', regex='\w+')
vs match('abcd', '\w+')
. The need to use named arguments here is much more controversial. They are still useful in case one mixes up an order of arguments, but make the code twice as verbose.
What would be the compromise?
I suggest to make argument naming required and eliminate the difference between a method name and argument names.
Simply put, I'd change match('w+', 'abcd')
to match: '\w+' text: 'abcd'
. Makes sense?
Take a look at our current code outline:
create fibonacci(1) as Number:
Number n
bytes asBytes():
if:
lessThan: @n, 2
1,
plus:
@n
fibonacci:
minus: @n, 1
Indeed, as we got rid of the brackets, it isn't verbose at all. But the readability suffers just like in the example above. I believe, if the if
had more code inside, it wouldn't be readable anymore.
What about something like this?
create fibonacci(1) as Number:
Number n
bytes asBytes():
if:
less: @n than: 2
then:
1
else:
plus:
@n
and:
fibonacci:
minus: @n and: 1
Here, the if
isn't a constructor with 3 unnamed arguments anymore. It has two named arguments: good old then
and else
are back in the game, but now in an object oriented way!
What do you think?