2 ms·
When I write a private class, I'm declaring to the world: These are the methods now available, their expected inputs, and their return types. Anything I do ins
by pwinnski 4y ago
When I write a private class, I'm declaring to the world:
These are the methods now available, their expected inputs, and their return types.
Anything I do inside that class that isn't marked public is an implementation detail, and might change. Given time, likely will change! But if it's not marked public, then if you want to access something in there, you'll need to think through the implications of making it public, either a new method of by adding to an existing method.
If I come along later and want to change things about those methods, I can do that freely, so long as I maintain the same input types and return types.
Internally I can change a counter from an Int to an Long, and it doesn't matter to anybody. Internally I can change the way I'm assembling a String, and that's fine. Internally I can move the definition for an interim variable to a separate function with its own tests, and that's fine. So long as I maintain the input types and return types, all is good.
If the fields and methods were public, on the other hand, then other classes might be accessing them directly, and now any change has to be negotiated with all callers. The counter needs to be changed from an Int to a Long? If it's public, you'll need to make sure that no other classes anywhere are referring to that counter and expecting it to be an Int.
Side effects are generally unwanted, and marking things as public is making everything in an implementation a side effect.