Change default access level of class members

Share this topic:



Link to this posting

Postby Ursego » 19 Feb 2013, 22:22

Don't leave the class members PUBLIC as they are created by default - change the access level to reflect their real practical use.

Remember, that PowerBuilder's instance variables and functions are public by default. Methods access can be changed to private or protected using the dropdown in the declaration section, and instance vars must be "privatized" by adding the private or protected keyword before each var, or the same keyword with a semicolon before a group of vars. Pay attention - if no scope identifier is mentioned explicitly then the vars are public (in contrast to C#, where "no scope identifier" means "private")! IMHO, it was a bad solution of PB authors (everything should be private by default and be changed only if a need arise!), but that's what we have... So, when you are creating a new instance variable or a function, and your brain is busy thinking about the functionality being developed, don't forget to change the access level to what it should be!

Why is this advice important? We can see many PB applications with all functions public, and nothing bad is happening... I would say, it is not too important in the development stage, but when the maintenance time comes... For example, I have to add and even change functionality of an existing object. If the stuff I am touching is private then I know that my changes will not cause straightforward damage in other (sometimes unexpected) parts of the application - for example, I can change the number of parameters or even the name of a private function without having a chance to do any harm to other objects. But if the function is declared as public then I need to perform the Global Application Search (which is extremely slow in PowerBuilder, especially in serious apps) to make sure the function is not called outside the object (and if it is called then, may be, to change the technical solution).

There are more reasons to keep the access in the CORRECT level - for example, you can read "Private and public affairs".
User avatar
Ursego
Site Admin
 
Posts: 112
Joined: 19 Feb 2013, 20:33

Return to Elegant Code

Who is online

Users browsing this forum: No registered users and 1 guest


Change default access level of class members

Share this topic:


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





cron
free counters

eXTReMe Tracker