3

I have Java background and in Java when programmer call method with wrong parameters, exception will be thrown. How Ruby programmers treat wrong method arguments?

Two opposite examples from core classes:

irb(main):009:0> "" * (-2)
ArgumentError: negative argument
        from (irb):9:in `*'
        from (irb):9
        from C:/Ruby/Ruby193/bin/irb:12:in `<main>'


irb(main):013:0> (-2).times { puts 'hello' }
=> -2
Cœur
  • 37,241
  • 25
  • 195
  • 267
guest
  • 1,696
  • 4
  • 20
  • 31
  • 1
    Actually in `(-2).times { puts 'hello' }` I think there is really no argument type problem : you just cannot do something a negative amount of time, so doing nothing seems sensible to me and I don't think an error ought to be raised here – djfm Dec 30 '12 at 01:26
  • Besides, -2 is Fixnum, so argument type wise -2.times is valid! – djfm Dec 30 '12 at 01:33
  • `1.5.times {puts "hi"}` will fail though ;) – djfm Dec 30 '12 at 01:41
  • 1
    @djfm Note that the OP did not write argument **type** problem but wrote "wrong arguments". Validation is sometimes not just a type problem, as you correctly point out. Same thing with, e.g., zero division error. You cannot distinguish zero by type. – sawa Dec 30 '12 at 06:50

1 Answers1

3

I generally tend to avoid argument validation in Ruby.

However, it really depends on what your primary purpose is. If you are concerned about reliability, then act defensively and raise exceptions whenever the arguments will lead to an unrecoverable error.

Also it depends on your use case:

  • Are they public APIs? You may strongly consider validation.
  • Are they private APIs/methods? Well, I would generally trust the programmer.

A valid approach could be the usages of argument checking in the debug phase, useful to detect bugs and misuses of the APIs, then - once tested - remove the checks.

Bottom line there's no perfect answer to this tricky question. It's very dependent both on the context and on the personal judgment of the developer.

sawa
  • 165,429
  • 45
  • 277
  • 381
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • @djfm: I don't want type safety, but I want to find bugs quickly. Like: sqrt(-1) should crash. – guest Dec 30 '12 at 01:40
  • @guest that's why I suggested that you may introduce argument checking only in debugging phase, then removing it in production ;) – Gabriele Petronella Dec 30 '12 at 01:41
  • irb(main):004:0> Math.sqrt(-1) Math::DomainError: Numerical argument is out of domain - "sqrt" from (irb):4:in `sqrt' from (irb):4 from /usr/bin/irb:12:in `
    ' QED
    – djfm Dec 30 '12 at 01:42
  • @guest if the answer helped you out, you may consider accepting it. If you need more clarification, just ask. – Gabriele Petronella Jan 02 '13 at 13:30
  • @Gabriele Petronella: Private methods aren't so important for me. I am mainly interested in treating parameters in public methods. Could you tell me how main 3rd party Ruby libraries deal with public method parameters validation? – guest Jan 03 '13 at 11:37