News for hackers. Stuff that matters.
Re:Ask HN: Relationship between OO and functional programming? by eldavido on Tuesday May 31, 2016 @02:49PM (#11809023)
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.

Re:Ask HN: Relationship between OO and functional programming? by manmal on Tuesday May 31, 2016 @03:49PM (#11809592)
I 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)

Re:Ask HN: Relationship between OO and functional programming? by coreload on Wednesday June 01, 2016 @10:32AM (#11814601)
Note 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"

Re:Ask HN: Relationship between OO and functional programming? by hutzlibu on Tuesday May 31, 2016 @03:34PM (#11809474)
"-Share data liberally by reference all over your code. -Try not to make functions side-effecting" Aren't those 2 points contradictory?
Re:Ask HN: Relationship between OO and functional programming? by Smudge on Tuesday May 31, 2016 @03:40PM (#11809528)
Immutable data is very easily shared.
Re:Ask HN: Relationship between OO and functional programming? by mywittyname on Wednesday June 01, 2016 @04:51PM (#11818058)
Doesn't "by reference" imply mutability and allow for side-effects (as opposed to by value, which cannot side-effects by design).
Re:Ask HN: Relationship between OO and functional programming? by AnimalMuppet on Wednesday June 01, 2016 @04:54PM (#11818074)
No. 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.

Re:Ask HN: Relationship between OO and functional programming? by rjeli on Tuesday May 31, 2016 @03:38PM (#11809499)
no, the shared data is not modified
Re:Ask HN: Relationship between OO and functional programming? by hutzlibu on Tuesday May 31, 2016 @06:41PM (#11810848)
Ok, I understood "sharing data" as equal to read and write ...
Re:Ask HN: Relationship between OO and functional programming? by jowiar on Tuesday May 31, 2016 @03:23PM (#11809359)
The 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.