9 ms·
I don't think you understand functional programming. function makeAppropriateCar(cargo) { if(cargo == "people") return new Automobile(); else i
by tomp 13y ago
I don't think you understand functional programming.
function makeAppropriateCar(cargo) {
if(cargo == "people") return new Automobile();
else if(cargo == "boxes") return new Truck;
else ...
}
function makeLocalCar(settings) {
var car = new Car({'color': settings.get('new_car_color')});
car.set_origin(settings.get_hostname());
return car;
}
function makeDebuggingCar() {
if(global_settings.DEBUG && !global_settings.PRODUCTION)
return new Car({'loglevel': LOGLEVEL_DEBUG});
else if(global_settings.DEBUG && global_settings.PRODUCTION) // testing
return new Car({'loglevel': LOGLEVEL_INFO});
else // production
return new Car({'loglevel': LOGLEVEL_WARNING});
}
- rafekett 13y agoOk, now try maintaining that when there are 15 types of cars and cars are used in dozens of services. You're imperatively expressing a dependency graph that you could (and would benefit from) defining declaratively at a larger scale. I'd recommend reading the user guide for Guice for balance before you respond. It's worth seeing how nice DI can be.
- zoomerang 13y agoWhat does your example have to do with functional programming? You've written two factories, pretty much exactly how AngularJS uses them, in an imperative style. Regardless of which, this doesn't in any way solve the issue Angular Services are designed to solve (IoC).
- yesiamyourdad 13y agoThis example actually really sucks. Advocating for case statements shows that either you've never worked on a truly large project or you've never had to go back and modify your own code 2 years after the fact. If you really understood functional programming, you'd see an if/case statement as an opportunity to replace conditional with a function. I started to write up an example, but since we're talking about the imaginary abstract Vehicle implementation, it's not worth dignifying. If programmers didn't switch jobs every 12-18 months and didn't see 100K LoC as a "large project", these "all frameworks suck" rants might turn into useful discussions of how one can organize code such that it's understood by more than the original author. That goes for frameworks too. If you have to rely on one of the core developers responding to a post on Google Groups or SO as your support mechanism, you've made the world less simple.
- Kroem3r 13y agoIt's a fine example. Are you saying that 3 case statements is too many? Why optimize the code to support cases that don't actually exist yet? Or even better, why optimize the code in ignorance to how it will need to be optimized in 2 years?
- hackNightly 13y agoNone of these are examples of functional programming except that you wrote some functions.