3 ms·
> So like, I guess the thing I don't understand is this: why do people think writing: > some_array.has_my_thing() is better than has_my_thing(some_array). ? Th
by Mikeb85 5y ago
> So like, I guess the thing I don't understand is this: why do people think writing:
> some_array.has_my_thing() is better than has_my_thing(some_array). ?
The latter is in the global namespace (or you have to create a module/namespace for it). The former is encapsulated in a class. Encapsulation is nice.
- zer0-c00l 5y agoExcept here the encapsulation comes at the cost of extending a core library at runtime, which has all the tradeoffs mentioned in the article.
- jshen 5y agoOne benefit is discoverability. I coded a ton of Ruby/rails back in the day and it was nice to be able to jump into a repl and see what methods were on an object. Also, you could often find the source of the method through the repl. I think a lot of these debates come down to repl development versus ide development. I’ve done a lot of both, but many complaints come down to people on one side not understanding the way the other side works.
- matsemann 5y agoIn other languages (like Kotlin), that extension is actually not done on the object, but is a statically imported function and it's only syntactic sugar. This avoids all the pit falls (no collision, knows where the function comes from etc)
- dragonwriter 5y ago> Except here the encapsulation comes at the cost of extending a core library at runtime No, it doesn't. Pervasive OO style and monkey-patching core classes are orthogonal. Ruby favors the former strongly, and supports the latter, and Rails rather heavily leverages the latter (to the extent that it has been harshly criticized for it by much of the non-Rails Ruby community), but there is no necessary relationship between them.
- zer0-c00l 5y ago“Here” in my comment was referring specifically to Rails’ use of the latter method, sorry for the confusion
- dgb23 5y agoThe level of encapsulation seems to be the same. It’s just a different notation in that case.
- WallowingInShit 5y agohas_my_thing(some_array) can be encapsulated too, using modules. For example, in elixir you have functions like List.to_string(some_list) and Map.values(some_map). This way, you get encapsulation and don't have to have a class.