Page 1 of 1

Private and public affairs in functions

PostPosted: 21 Feb 2013, 21:26
by Ursego
If a function of a class will be accessed only from inside the class then declare that function as private (if also from descendants then as protected).

This approach is not only consistent with the object-oriented theory (data protection), but has additional advantages which are very practical.

Developers, using the class, immediately see the outer world interface of the class. They don’t want to concentrate on implementation details (private functions) if the class is used as the "black box". Imagine that you want to utilize a class which has 5 public and 30 private functions. You explore the public functions and get an idea how to use the class. But what if all the 35 functions would be public? Your task would become much more difficult.

If a function, practically used as private, is mistakenly declared as public, then:

1. Developers, consuming the class, can think they can (or even should!) call that function to get services.
2. Developers, changing the class itself (interface or internal implementation), cannot be sure that the function is not called from outside. So, they are forced to use global search not to damage other modules.