function names ending with !

393 views
Skip to first unread message

Mark Volkmann

unread,
Oct 16, 2009, 10:44:51 AM10/16/09
to clo...@googlegroups.com
What's the rule of thumb for deciding whether a function name should
end with an exclamation point? I thought maybe it was when the
function modifies its first argument, but it seems there are functions
that do that and do not have such names.

For example, set-validator! and add-watch. Why don't they either both
end with ! or both not do that.

--
R. Mark Volkmann
Object Computing, Inc.

Jarkko Oranen

unread,
Oct 16, 2009, 12:09:26 PM10/16/09
to Clojure
On Oct 16, 5:44 pm, Mark Volkmann <r.mark.volkm...@gmail.com> wrote:
> What's the rule of thumb for deciding whether a function name should
> end with an exclamation point? I thought maybe it was when the
> function modifies its first argument, but it seems there are functions
> that do that and do not have such names.
>
> For example, set-validator! and add-watch. Why don't they either both
> end with ! or both not do that.

add-watch doesn't really affect the object itself, while set-
validator! certainly does, so I think the naming is consistent.
I think it might be a good rule of thumb to add the ! if the operation
either mutates the object (set!) or invalidates old values/references
to it, like persistent! or set-validator!. However, even core.clj
doesn't seem to follow this...

The exceptions seem to be the "alter" operations for reference types;
alter, commute, alter-var-root, and for some reason, ref-set.

--
Jarkko

Meikel Brandmeyer

unread,
Oct 16, 2009, 12:44:15 PM10/16/09
to clo...@googlegroups.com
Hi,

Am 16.10.2009 um 18:09 schrieb Jarkko Oranen:

> add-watch doesn't really affect the object itself, while set-
> validator! certainly does, so I think the naming is consistent.
> I think it might be a good rule of thumb to add the ! if the operation
> either mutates the object (set!) or invalidates old values/references
> to it, like persistent! or set-validator!. However, even core.clj
> doesn't seem to follow this...
>
> The exceptions seem to be the "alter" operations for reference types;
> alter, commute, alter-var-root, and for some reason, ref-set.

I also use ! for functions, which modify resources. Eg. creating a
database table or such.

Sincerely
Meikel

Laurent PETIT

unread,
Oct 16, 2009, 1:08:41 PM10/16/09
to clo...@googlegroups.com


2009/10/16 Meikel Brandmeyer <m...@kotka.de>

I'm currently trying to follow the rule that every function with "side-effect" (e.g. non-pure) has an ended '!'.
Not sure I'll stick to it in the long run, though ...

Mark Volkmann

unread,
Oct 16, 2009, 2:22:02 PM10/16/09
to clo...@googlegroups.com
On Fri, Oct 16, 2009 at 11:09 AM, Jarkko Oranen <chou...@gmail.com> wrote:
>
> On Oct 16, 5:44 pm, Mark Volkmann <r.mark.volkm...@gmail.com> wrote:
>> What's the rule of thumb for deciding whether a function name should
>> end with an exclamation point? I thought maybe it was when the
>> function modifies its first argument, but it seems there are functions
>> that do that and do not have such names.
>>
>> For example, set-validator! and add-watch. Why don't they either both
>> end with ! or both not do that.
>
> add-watch doesn't really affect the object itself, while set-
> validator! certainly does, so I think the naming is consistent.

set-validator! sets the validator field in the object, so that's a modification.

add-watch replaces the watches map in the object with a new map
containing the new watch function, so that's a modification.

You could say there's a difference because validators can prevent
future changes to an object whereas watchers are just notified.

> I think it might be a good rule of thumb to add the ! if the operation
> either mutates the object (set!) or invalidates old values/references
> to it, like persistent! or set-validator!. However, even core.clj
> doesn't seem to follow this...
>
> The exceptions seem to be the "alter" operations for reference types;
> alter, commute, alter-var-root, and for some reason, ref-set.

So the situation seems to be that the presence of ! in a name
indicates something, but the lack of it doesn't necessarily mean
anything.

Reply all
Reply to author
Forward
0 new messages