On Wed, Dec 13, 2017 at 12:18:16AM +0100, Ancor Gonzalez Sosa wrote:
> This is probably the less useful mail ever, since it just goes over a
> topic that has been explained over and over and still pops us in almost
> every single meeting.
>
> Let me say it again: 98% of the "undefined method `xxx' for
> nil:NilClass" errors are NOT related AT ALL to Ruby being dynamically
> typed and would NOT be prevented by the usage of a statically typed
> language.
>
> And the other side of the same recurring coin - we don't write unit
> tests as a replacement for the compiler checks. That's not the point,
> the goal or the intention of the unit tests.
>
> This time I will try to explain it with an example (something that
> already has been done, but I just felt impelled to do it one more time).
>
> Let's say you have this Ruby method that returns an object of type
> Device or nil if the device was not found
>
> find_device_by_name(name)
>
> And then you write this code
>
> dev = find_device_by_name("/dev/sda")
> puts dev.size
>
> If there is no /dev/sda that will result in one of those "undefined
> method `size` for nil:NilClass" that some people hate so much. Did it
> happen because Ruby is dynamically typed? NOT AT ALL. It happened
> because you didn't check for the situation in which the device is not
> there with some code like this.
>
> dev = find_device_by_name("/dev/sda")
> if dev
> puts dev.size
> else
> puts "No such device"
> end
>
> Let's do the same in an hypothetical typed Ruby in which there is
> nothing like nil. Now your find_device_by_name method will raise a
> NotFound exception instead of returning nil if the device is not there
> (the approach we use in statically typed languages).
>
> The first example will still fail, now with an uncaught NotFound
> exception. The static type checking performed by the compiler didn't
> save us from THE DEVELOPER ERROR and the result is pretty much the same
> - an exception thrown in the user's face.
I think you are mixing static typing and static code analysis.
ciao Arvin
--
Arvin Schnell, <[email protected]>
Senior Software Engineer, Research & Development
SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284
(AG Nürnberg)
Maxfeldstraße 5
90409 Nürnberg
Germany
--
To unsubscribe, e-mail: [email protected]
To contact the owner, e-mail: [email protected]