Function naming Julia: When should I add a bit?

Julia's style guide says that functions that "change their arguments" should have their name on !

.

However, how about:

  • which change their arguments but reset them to their original state before returning?

  • which return Task

    which modifies the argument when executed?

  • which return such Task

    , but when it is done, will the arguments be restored to their original states?

If their names end in !

?


As an example, consider this module for finding exact covers using the Knuth Dance Reference Algorithm . It implements a type CoverSet

that can be populated with subsets and then queried for the first exact coverage:

set = CoverSet()
push!(set, [1, 2])
push!(set, [2, 3])
push!(set, [3, 4])
push!(set, [4, 1])

find_exact_cover(set) # returns [1, 3]

      

The function find_exact_cover

temporarily changes the data in set

while searching for a solution, but by the time it returns it find_exact_cover

set

will be in its original state. Should it be named find_exact_cover!

instead?

Likewise, exact_cover_producer

returns a Task

, which produces all exact skins, but when that Task

is done set

will be restored:

for cover in exact_cover_producer(set)
  println(cover) # prints [1,3] and [2,4]
end
# By now, set is restored.

      

Should it be exact_cover_producer!

?


I know this can be considered subjective, so let me clarify what I am asking about: I would like to know if there is a convention on this in the Julia community, and ideally also examples from the standard library or any packages that use either style.

+3


source to share


1 answer


See the discussion in Julia to commit e7ce4cba44fa3b508fd50e0c3d03f6bc5a7a5032 : the current convention is that the function mutates and therefore has an added !

one if it changes what the arguments will be ==

to.



(But there is some reason for broader definitions as well; see the above discussion.)

+2


source







All Articles