Private and public affairs in functions

Share this topic:



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 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.
User avatar
Ursego
Site Admin
 
Posts: 112
Joined: 19 Feb 2013, 20:33

Return to Managing Functions

Who is online

Users browsing this forum: No registered users and 1 guest


Private and public affairs in functions

Share this topic:


If you think that this site is not too bad, please LIKE it in Facebook. Thanks!





free counters

eXTReMe Tracker