Function Name Convention - General or Specific names?

Hey there!

I’ve just started using Julia, so I’m super new and overwhelmed by so much new information. So sorry if my question is superfluous.

What if I have two modules / “classes”:

GameBoard and GameRules

Both can have randomly generated “behavior” - so a randomly generated Board and randomly generated game rules.

Should I give them function names like: generateBoard() and generateRules() or generateBoard!(rules::GameRules) and generateRules!(rules::GameRules) ( I think style guide says generateboard() but I love lowerCamelCase so much. I’m German I appreciate capital letters :smiley: )

Normally I would name them kind of this, because I like names that are very specific, even with “normal” OOP - call me wrong, I’m still a CS student and don’t have too much experience. But I’m going to start reading “Clean Code”, so don’t give up hope on me! :smiley:

But for Julia I thought, I just could use generate() for all my “classes”/modules.
generate!(rules::GameRules)
generate!(board::GameBoard)

Probably you could do this in the constructor function of the struct, but my question is intended to be general.

I appreciate super-specific names, but with the method-oriented approach of Julia I see advantages of using very general/generic function names. What would you say, do the advantages outweigh the disadvantages?

Are there any good ressources for such kind of questions?
I haven’t found anything in this forum.

Thank you for your precious time

Best Regards
Peter

First I would say choose a style you like and stick with it. Quite frankly I love snake case for function names, so that is what I use. You might like something else, whatever, use it!

One of the things that annoyed me about GO is they defined how the GO code should look and the tools kept bitching at me. To make matters worse the atom plugin kept re-enabling code formatting so I’d save a file and it would “pretty” it up for me and ignore how I wanted it to look, very annoying. I will say having a common coding style when multiple people are working on the same code does make sense. However in this situation I prefer to put people on different modules define the APIs between those modules and then let people own their code.

As for naming, I would recommend going for the shortest name that still describes what the function is doing. I think creating a generate() function sounds fine. As for the ! suffix on functions, I’ve found that wonky. Why write doesn’t have the suffix but push does, I’ll never know. Both functions are “adding” something to the underlying “object”. So I’d say use it or don’t use it, just be consistent on when you use it.

Really all your naming conventions are to help you read the code (and possibly other people). Try to ensure that if you walk away from the code for a year when you come back, your code will still make sense. For me that really means adding comments at the top of each function that document what the function does, what it expects for input, what it returns, and what state it changes. Julia really helps with this and their triple quote comments for method, I would highly recommend using that feature.

3 Likes

Thank you so much - you helped me tremendously!
I really like your opinion about this topic and will heed your tips.
You cannot imagine how much I appreciated your advice!

Did I understand you right that it’s generally recommended to use and reuse generic function names, so you can reuse them for as much “objects” as possible? …as a result of using Julia, because of multiple dispatching / method oriented approach?
In other words: How far you would use different names in Julia than you would use for Python/Java/C#?

As a result I could use generate!() for many things:

generate!(level)
generate!(visualMap)
generate!(rules)
generate!(board)

or even put all the relevant structs/variables in a collection and then iterate over it with generate().

I guess what I would say is don’t worry about reusing a function name if it makes sense. :slight_smile: Just don’t try to “force” the function name if it doesn’t really fit. If you find yourself trying to justify that it’s a “generate” function to yourself, then it probably needs another name.

An example of this if it generate! for board created the board but also started the game. In that case generate! for the board would be different than generate! for the other things, and in that case may deserve a different name.

1 Like

That makes total sense to me, thanks! :slight_smile: