Open resinten opened 4 years ago
In Ruby, methods that involve mutation of values (those with side effects) commonly end with !. This is a really nice way to look at a series of methods and know whether the method might modify state in some way.
I think this is a really good description of bang methods: http://rubylearning.com/satishtalim/writing_own_ruby_methods.html
The way I interpret it is that the method is "dangerous" in some way and that one should probably avoid using this method unless they have a specific reason to. "Dangerous" is totally subjective and dependent on the context the method is being defined in.
The Ruby standard library typically uses bang methods that modify the object they are acting on instead of returning a new object, but other libraries use it for other reasons. For example, in the ActiveRecord
library, which is the standard Rails library for interacting with a database, bang methods are used to raise errors when an attempt to modify the database fails validations.
But in a game, nearly all methods are likely to involve mutation of state in some way: ... It seems like it loses its meaning if everything has !, so I would like to form some best practices for how the library will use this suffix.
You make a great point. If everything is special then nothing is really special, in which case I would personally drop all of the bangs. If changing state is expected behavior and is something that should be done regularly, then there isn't anything dangerous or unexpected happening.
Since this is your library, you get to decide what "dangerous" means and when to define bang methods. That could be when a method has effects that persist over multiple frames (if that is unexpected behavior) or something else entirely.
In general, I like to limit bang methods to a couple different use cases:
I hope this helps!
Yes! This is very helpful. I've always looked at it as indicating "side-effect-y" behavior, since I'm coming from a Rust and Scala background, but "dangerous" seems like another good way to look at this. For instance, an exception to "side effects" is that the array .push
method mutates the underlying array (a side effect) but doesn't have an exclamation mark. My current thinking then is that methods that perform output or last multiple frames would be considered dangerous because they have potential ramifications that extend outside the current frame.
In Ruby, methods that involve mutation of values (those with side effects) commonly end with
!
. This is a really nice way to look at a series of methods and know whether the method might modify state in some way. But in a game, nearly all methods are likely to involve mutation of state in some way:It seems like it loses its meaning if everything has
!
, so I would like to form some best practices for how the library will use this suffix. Two options come to mind:!
and use it with its normal meaning!
as indicating an effect that persists over multiple frames (the frame serves as the mutation boundary)In the second option,
Draw.load_spritesheet!
,Draw.create_sprite!
andrun!
would retain the!
indicator, as the spritesheet loading affects a list of globally available spritesheets, andrun!
initiates a coroutine of code that will run over many frames.Draw.text
andupdate
(maybe notupdate
?) would lose the!
as they have a natural frame mutation boundary. Possible exception here is thatupdate!
could last over multiple frames since it might callrun!
.