4 ms·
Is there any real harm though? Free functions pollute the global namespace, which is something I tend to avoid (at least in Ruby where everything shares the sam
by hamandcheese 6y ago
Is there any real harm though? Free functions pollute the global namespace, which is something I tend to avoid (at least in Ruby where everything shares the same namespace).
- cerberusss 6y agoWhat's the difference between polluting the global namespace with a class name, or with a function name? I get it, I also experience some reticience when adding free/bare functions in Swift. But maybe it's because of 15 years of OOP programming.
- aarong11 6y agoMaybe not so much of a problem in C++ but look at JavaScript. With the millions of dependencies / libraries you import, if they all used free standing functions you would inevitably end up with collisions.
- wruza 6y agoIn c++ functions can be marked ‘static’ to make them translation-unit local, or you can put them in a library namespace, if they’re public. In a particular js bundler/runtime usually a module system is used (or just iife/umd wrap) which prevents function names from populating globals.
- entha_saava 6y agoThe problem is unqualified imports though, not free functions.
- mcv 6y agoMaybe the problem here is the global namespace. I really like modern javascript where you have to explicitly import everything.
- cgrealy 6y agoJust put the function in a namespace?
- ClikeX 6y agoIn Ruby, that basically comes down to creating a static class.
- risorect 6y agoIn terms of ruby, we may be splitting hairs when it comes to namespaces vs classes. Modules, which are typically used for namespacing, can have state and extend themselves, making them effectively singleton classes.
- ratww 6y ago> Modules, which are typically used for namespacing, can have state But there's nothing about state that makes it specific to classes. Namespaces in C++ can hold (static) state, for example. So it's actually the other way around: singleton classes can effectively be namespaces/modules.
- cessor 6y agoObject without state but with functions is just a namespace. But that depends on how the language implements it.
- hamandcheese 6y agoIn Ruby that looks a lot like what the article says is an anti-pattern.