r/fsharp • • Jul 10 '19

Object-Oriented Programming — The Trillion Dollar Disaster

https://medium.com/@ilyasz/object-oriented-programming-the-trillion-dollar-disaster-%EF%B8%8F-92a4b666c7c7
0 Upvotes

15 comments sorted by

View all comments

20

u/[deleted] Jul 11 '19

I mean, I love FP as much as anyone can, and I do agree that it’s a better way to write software, but there are a LOT of assertions in this article with no evidence to back them up. Bad OOP is bad not because it’s OOP, but because it’s bad.

4

u/RiPont Jul 11 '19

For example...

Testing Private Methods

Some people say that private methods shouldn’t be tested… I tend to disagree, unit testing is called “unit” for a reason — test small units of code in isolation. Yet testing of private methods in OOP is nearly impossible. We shouldn’t be making private methodsinternal just for the sake of testability.

In order to achieve testability of private methods, they usually have to be extracted into a separate object. This, in turn, introduces unnecessary complexity and boilerplate code.

Nobody says "you shouldn't unit test private methods". You should be able to test your private methods by calling your public methods. If you have to make your private methods internal to test them, then that's a code smell that your design breaks modularity and/or encapsulation. Bad OOP, as you said.

Mutable state is definitely problematic and should be avoided as much as possible. However, I've never seen anyone write a GPU driver in a pure FP language. Sometimes, mutable state is an inescapable fact. The CPU and memory architecture of a computer is a giant pile of mutable state. Of course, I do think the world would be better off if every programmer at least practiced with an FP language and learned how to push dealing with mutable state off to the edge cases where it belongs.

8

u/AngularBeginner Jul 11 '19

I tend to disagree, unit testing is called “unit” for a reason — test small units of code in isolation. Yet testing of private methods in OOP is nearly impossible.

The equivalent in F# would be to test sub-functions. No one would do that either.

3

u/ipv6-dns Jul 11 '19

Absolutely strange passage. If you want to test "private" functions in Haskell you can either:

- make them "public" or

- extract them in special module which will export anything (usually called "Internal") and test them through it. And import it in another one module and re-export only some "public" functions/types to client side. So, most canonical FP language (ie, Haskell):

introduces unnecessary complexity and boilerplate code.

By the way, no such thing as private/public and other accessibility levels in Haskell at whole, only optional list of exported symbols lol.

But in C# I can just use "internal" access modifier (https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers). How it's intelligent. Bravo, Microsoft!

1

u/Slypenslyde Jul 22 '19

State of the art OO philosophies (as of about 2005?) don't even have this problem. Large-scale OO projects tend to deal with what used to be private methods via composition and DI today. So where I used to would have a CalculateWidgetTax() private method, now my class depends on IWidgetTaxCalculator. That is tested separately, and can be controlled in the context of whatever is using it.

I'm pretty sure if I sat down and wrote a lot of F# using only anti-patterns and smells, I could write an interesting article about how much FP costs. It'd be bad faith.

If I had to write an essay on this topic, my personal premise is history's greatest and most common programming disasters boil down to failures to manage complexity. The reason we make new languages is to try and handle that complexity better than the existing languages. But every approach is a tradeoff between different kinds of complexity and if we aren't careful we can use a perfect language to write a garbage pile.

Lots of atrocities were committed when OO was still very new. I don't think we really learned how to write good OO software until the mid 2000s. We learned good practices on top of the ashes of the bad practices. I think we also rediscovered a healthy amount of software engineering FP devs had known all along. But I find people still use examples of 1990s OO design as a condemnation of OO in the 2010s. That's like using Windows 95 to criticize Windows 10. I hate how often I find when someone writes, "This is what's wrong with {OO | FP | whatever}" what I'm actually reading is, "This is what was wrong with it when I changed jobs 10 years ago."

I don't have to write 1000 words to support, "Failure to properly maintain project complexity leads to unmaintainable code." And I don't care what language you give me, if I treat my project like a storage shed instead of a garden, pretty soon I'm going to have to move the lawnmower and the Christmas decorations to get at the grill and I'm going to ask why I created the mess.

1

u/RiPont Jul 22 '19

Very well said. In the end, we're dealing with complex issues, even if they can sometimes be stated simply. We try and manage that complexity, but as you said, there are tradeoffs.

Cargo-cult programming leads to using patterns badly and can overcomplicate things that shouldn't be quite as complicated. And then there is outright malice by contracting firms and charge-you-for-customization vendors that profit from complexity.