News for hackers. Stuff that matters.
Re:Ask HN: Why do you make class members private? by swat535 on Monday July 25, 2022 @04:47PM (#32230699)
The following is a public API for a Car:

- Steering wheel

- Transmission

- Brakes and Gas pedal

- Doors and Windows, etc.

The following is a private API for a Car:

- The inner workings of the engine

- The inner workings of the battery

- Smart break system, internal wiring, etc.

All users of the Car only care about how to drive it with as little knowledge as possible.

Now Mazda can do a full recall and upgrade the smart breaking systems internally but millions of drivers around the world don't have to "learn" anything new, they can happily continue using the public API.

It makes refactoring much easier, reduces dependency and enables duct typing (in supported languages) via message passing for objects with a common public API. You also don't have to perform shotgun surgery[0] when changing classes..

[0] https://refactoring.guru/smells/shotgun-surgery

There are no comments on this comment yet.