Private and public affairs in functions

Link to this posting

Postby Ursego » 21 Feb 2013, 21:26

If a function of a class will be accessed only from inside the class then declare that function private (if also from descendants then 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 to prevent damage in other modules.
User avatar
Site Admin
Posts: 128
Joined: 19 Feb 2013, 20:33


free counters

eXTReMe Tracker