4 ms·
In the past five years, I've written C#, Ruby, and Javascript in a style I'd describe as "OO, but with a heavy functional accent"; it might even be fair to call
by eldavido 10y ago
In the past five years, I've written C#, Ruby, and Javascript in a style I'd describe as "OO, but with a heavy functional accent"; it might even be fair to call it "object-functional".
A few little things I've identified that I do, that aren't totally mainstream:
- Don't reassign parameters, in fact, don't reassign things at all, if you can avoid it; show your work. It makes for more expressive code, and you'll have fewer bugs.
- Think of your code as doing "evaluation", not "computation". Code should take a bunch of inputs, do some stuff to them, and then return an "evaluated" response, vs. the viewpoint of "doing a procedure".
- Try not to make functions side-effecting. Avoid "void" unless you really need it, and really, really avoid distant side-effects.
- Don't be afraid to return a new object that seems "large" from a method. If you do it right, you'll mostly be copying references anyway which is pretty cheap.
- Share data liberally by reference all over your code.
- Don't be afraid to use a bit of recursion.
On the other hand, I'm not afraid of a little grubby OO now and then, especially when dealing with I/O, file handles, network streams, etc.
All to say, I think the two styles are about 90% compatible but there are some parts that don't mesh well. I'm not afraid to mix them, though.
- jowiar 10y agoThe introductory Scala Coursera teaches this style -- and to a large extent, writing code in this style is a significant reason why Martin Odersky created Scala. For me, it's a definite "accent" that I have when I write code in other languages.
- hutzlibu 10y ago"-Share data liberally by reference all over your code. -Try not to make functions side-effecting" Aren't those 2 points contradictory?
- rjeli 10y agono, the shared data is not modified
- Smudge 10y agoImmutable data is very easily shared.
- mywittyname 10y agoDoesn't "by reference" imply mutability and allow for side-effects (as opposed to by value, which cannot side-effects by design).
- AnimalMuppet 10y agoNo. In C++, for instance, you can share data either by a const reference (or pointer), or by a non-const reference (or pointer). The latter allows mutation; the former does not. Now, that means that the language is not forcing you to share without mutability, because you can leave off "const". That is, you have to be disciplined. But if you put "const" on the reference, the compiler won't permit you to modify the data through that reference.
- hutzlibu 10y agoOk, I understood "sharing data" as equal to read and write ...
- manmal 10y agoI agree with you on all points. Regarding side effects, it might even be possible to create almost-side-effect-free server code; but the state of most native client frameworks are just not ready for this. State can be banished/abstracted into the lower levels, but it doesn't go away. (Speaking as an iOS dev. There is React Native, but this only handles the view layer; good luck with stateless CoreData)
- coreload 10y agoNote that this was the advice of developers of distributed OO systems in the early 1980s. See in the HOPL... "The Development of the Emerald Programming Language"