6 ms·
My methodology for avoiding this confusing “functional” programming: 1. Write: function add (a, b) { return a + b; } 2. Refactor to object: class Accumu
by braythwayt 11y ago
My methodology for avoiding this confusing “functional” programming:
1. Write:
function add (a, b) { return a + b; }
2. Refactor to object:
class AccumulatingIntegerWidget {
constructor (...numbers) {
this.accumulation = 0;
this.accumulate(...numbers);
}
accumulate (...numbers) {
for (const n of numbers) {
this.accumulation = this.accumulation + n;
}
}
getAccumulation () {
return this.accumulation;
}
}
Presto! It’s stateful and object-oriented and handles future cases like adding three numbers, even though WeArentGonnaNeedIt!
If this does not satisfy, I progress to making my classes observe each other and update themselves automatically. “We’re doing two-way binding!” I exclaim proudly, while leaving debugging as an exercise for my colleagues.
- defiblep 11y agoYour first example is actually procedural. Needs more lambdas.
- braythwayt 11y agoIt never lasts long enough to get complex, it appears in a virtual functional/anti-functional pair and then they annihilate each other. Only in the presence of a powerful gravitational force, like the edge of a corporate programming style guide, will one of the pair be sucked into the maw, leaving the other to accumulate cruft. For example, the second can incorporate two-way binding as noted, logging, synchronization via Operational Transforms, and so much more...
- BerislavLopac 11y ago> Needs more lambdas. I'll be using this in every code review from now on.
- dudul 11y agoYour design lacks a `Factory` I think. You don't use design patterns enough.
- braythwayt 11y agoIt’s factories all the way down.
- Lawtonfogle 11y agoSo if you have some object named Obj, and you have some interface for it, and you have a factory that creates objects matching that interface, and that factory has an interface, then what do you call the factory that builds implementations of that interface? A.) IObjFactoryFactory B.) IIObjFactory C.) IIObjFactoryFactory D.) An abomination upon mankind. E.) IObjFactoryIFactory
- onnoonno 11y agoJavaProxyEnterpriseBeanConnectorAbstractInterfaceFactoryImplementationMockup or something.
- strmpnk 11y agoWon't pass code review till you can use dependency injection to pass a + operator mock for testing.
- goalieca 11y agoThis is one reason I hate our industry. No one engineers anything. They all just abuse OOP so that code is packaged together in some arbitrary heirarchy and then refactor it every single time a user story is added.
- pekk 11y agoIf we forced everyone to switch to Haskell, do you think this would really be any different? People would still do the bare minimum to implement the latest user stories, because that is literally what the boss is paying for.
- davexunit 11y agoI think the point is to not force any single paradigm. OOP is what is being forced today, but it would also be bad if the industry forced functional programming. Large programs typically call for multiple styles of programming. One piece may suit an imperative style, another functional, another logic/relational, another object-oriented, etc. In general, I think OOP is nearly always the wrong tool, though.
- emerongi 11y agoI'm in university right now and there is an object-oriented programming course (which is required by most other advanced courses), but no functional programming course. That shows how skewed the industry is. It's a pretty forward-thinking university as well. OOP is sometimes the right tool, but most times it definitely is not. It's important to be able to know your tools and choose the right one. The current situation is disappointing though.
- johan_larson 11y agoWe force everyone to switch to Haskell. OK. Even the fair-to-mediocre programmers? Yes. Who tend to program by copypasta and change stuff until it gets the right answer? Yes. Run away. Run far, far away. You don't want to see what "design patterns" and urban legends these people layer on top of Haskell to make it intellectually manageable.
- jksmith 11y agoI'll need an interface for that so I don't have to read into the code to see what it does.
- Cthulhu_ 11y agoI think you need to pass it a NumberFactory instead of this magic dot-dot-dot syntax, I can't read it.
- merpnderp 11y agoI love this so hard. I've been a dev for over a decade and just recently discovered functional programming (in a now undergraduate academic sense) and it has changed all my code. Things are so much simpler, easier to follow, and far far less code as I focus on data structure as state, and functions as things that operate on the data structures. Everything is just so much simpler now.
- xyzzy4 11y agoBut where's your specs?
- mavelikara 11y ago-1; please add a logging/tracing call in your add method. Since that introduces impurity, make an algebra for effects you can do with logging, construct the program as a free monad in that algebra and evaluate it.
- ruggeri 11y agoWith great comedy, you never know if they're being serious...
- JanneVee 11y agoLike "functional" programming can't be abstracted so it becomes way too generic.