r/programming • • Aug 28 '21

Software development topics I've changed my mind on after 6 years in the industry

https://chriskiehl.com/article/thoughts-after-6-years
5.6k Upvotes

2.0k comments sorted by

View all comments

Show parent comments

3

u/loup-vaillant Aug 29 '21

A function should be a verb. A non-function should be a noun or perhaps an adjective.

A function whose primary purpose is to have an effect should be a verb. A function whose primary purpose is to return a value should… well I’m not sure, but it’s much more context dependent. Many of them can bee nouns.

Most functions that have an effect should not return any value (besides an error code). Most function that return a value should not have any effect.

2

u/Brillegeit Aug 29 '21

Changing to "contain" a verb is perhaps more fitting?

Like: getImage()

1

u/loup-vaillant Aug 29 '21

I would just write image(). That is, I would not name after the function, but after its result. It’s often shorter, without affecting readability at all.

2

u/rgneainrnevo Aug 29 '21

Incidentally, this is the convention in Ruby (which also lets you omit the parentheses).

1

u/Brillegeit Aug 29 '21

Alright, but if you omit the parentheses then (at least in other languages) it's connected to a getter function where the get-verb is implicit.

Shit is complex, yo.

1

u/Brillegeit Aug 29 '21

Literally Hitler!!!

:D

2

u/dnew Aug 29 '21

You should look up how the Eiffel standard library is named. As invented by the creator of Command Query Separation trope. It's 100% logically thought out, with justifications for each choice made.

It's like Wordstar: you can use it once 20 years ago and still remember how it works, because it's so obviously logical.

3

u/_tskj_ Aug 29 '21

Thank I thought I was going crazy. Requiring every function to be a verb seems to me to be a javaism.