1. 4

  2. 7

    This is a perfect example of why type systems save time. It’s also why I prefer multiple return values over exceptions. Dynamic languages plus exceptions are 2 out of 3 ingredients of a recurring nightmare I have. The 3rd ingredient is, of course, callback/errbacks.

    Imagine if instead the method was:

    func save() {
    if save() { // compiler error

    Or even

    public void save() throws ValidationException {
    if save() { // compiler error because save has no return 
    // value AND you're not handling ValidationException
    1. 2

      Maybe I’m too familiar with rails, but I didn’t get more than a second into skimming the code before seeing the bug.

      Is this just a problem with “unfamiliar things are hard”?

    2. 3

      Pretty unusual to see camel cased variables in a Rails tutorial.

      1. 1

        I think the article could gain to be augmented with links to the appropriate documentation, for example save vs save! as seen here: http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-save

        If I recall correctly, this isn’t a new behavior; I believe most of the “!”-ended methods in Ruby idiomatically do “destructive” things to the underlying object instead of working on copies (see “String.reverse” vs “String.reverse!”). Well done, otherwise.

        1. 3

          The sad part of the save! name is that the bang doesn’t indicate destructive behavior, it means “this method will raise an error if the save fails”, which is different semantics from the idiomatic Ruby meaning. Another “fun” Rails quirk.

          1. 2

            I wouldn’t call it a quirk; it’s fairly consistent with the meaning of bang according to Matz : https://www.ruby-forum.com/topic/176830#773946

            The bang (!) does not mean “destructive” nor lack of it mean non destructive either. The bang sign means “the bang version is more dangerous than its non bang counterpart; handle with care”. Since Ruby has a lot of “destructive” methods, if bang signs follow your opinion, every Ruby program would be full of bangs, thus ugly.

            1. 1

              At my job we end up treating the bang version of save with less care, because we know it will raise an error if things go wrong and not silently propagate a bug.

            2. 1

              Agreed. I expressed myself poorly; I meant to suggest that someone familiar with Ruby would surely have some form of alarm bell ringed in his mind if he saw “!” at the end of a method name; it’s a bit sad that it potentially raises errors, though.

            3. 2

              I’ve just added the links. :)

            4. 1

              Is it just me or does this basically boil down to “test all the control paths in your models?”