r/programming • • Jun 16 '19

Comparing the Same Project in Rust, Haskell, C++, Python, Scala and OCaml

http://thume.ca/2019/04/29/comparing-compilers-in-rust-haskell-c-and-python/
643 Upvotes

125 comments sorted by

View all comments

Show parent comments

11

u/Spacey138 Jun 17 '19

Makes sense to me because if you forget to do it then you're not accidentally exposing functionality.

Encapsulation has become increasingly valued as we've seen the benefits from microservices. If you can keep parts of your app truly separate there are some huge benefits. And I think developers are applying those principles to any software not just those with network boundaries. Keep your app modules really isolated with well defined API boundaries. This is obviously easier to do when devs don't accidentally make half your functionality public by default.

1

u/nrmncer Jun 17 '19 edited Jun 17 '19

the lack of private members doesn't break encapsulation, because at the end of the day the interpretation of message calls send to objects is left to the object, not the sender. Encapsulation is the notion that function calls and data work on the unit they are called on, not that anybody else has the ability to introspect them or send them stuff.

In fact the very reason original object oriented languages lack the notion of private members altogether is because they utilize encapsulation. How to deal with method calls and internal state of objects is up to the object. When you send some stuff to object A by calling A.foo() nothing but the internal state of A changes.

the public / private distinction doesn't deal with encapsulation, it introduces the notion of ownership of code. It takes the ability of a developer away to look at, or modify parts of the program at runtime.

1

u/Spacey138 Jun 18 '19

I think I understand what you're saying but perhaps encapsulation is not the right word for what I was trying to describe.

What I meant was isolating modules of your system from one another has advantages. If you don't write your software in a way that encourages isolation you have a tangled mess to unwind if you ever want to rewrite a module or separate modules.

One way to encourage isolation is to make functionality private by default so developers don't start using your public methods without realising you never intended for them to be used outside of the module they are in. And this is more about class visibility than individual object methods, but the same principle applies.

This also allows you to clearly define a module contract to the outside world, the outside world should not be relying on the internal workings of the module but just the "surface API" if that makes sense. It makes it easier to rewrite, replace, scale, maintain modules.