Android Question Private - Why?

craigc

Member
I am very, very, new to B4X - working on my first non-tutorial application. My programming knowledge is PHP and some Basic a long time ago. I do not know Java.

I have searched books, the booklets, the Forum, Google search. I might have missed it but, I have not seen this explained anywhere.

What is the purpose of the Private access modifier? I know what it does. But, why is there a need to hide access from other variables? Is it a memory issue? Is it a security issue? Is it an issue related to "lifetime" of the objects? Would there be a problem with all subs, modules, activities, etc. being Public?

I apologize if this question is too fundamental for the Forum, but knowing the "why" would help me a lot. Thanks in advance for assistance with this.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Is it a memory issue?
No.
Is it a security issue?
No.
Is it an issue related to "lifetime" of the objects?
No.
modules, activities, etc. being Public?
No.
I apologize if this question is too fundamental for the Forum
Not at all.

Why?
Encapsulation - https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)
If a variable or sub are private then you know for sure that they are only used in the current module. 3 years from now you will want to improve a feature or fix a bug in that module. If there is a public variable then it might be accessed from many other places. Any change that you make becomes more difficult as you need to go over a lot of code and understand all possible states and side effects.
By default you should make everything private unless it needs to be public. It will encourage you to think about the module external interface and minimize it.
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
I can confirm that what Erel says is a very good description.

A couple of years ago I was in pretty much your exact situation, craigc. The first version of my app was a complete mess, like knotted spaghetti, where code from different places read and modified variables all over the place. It was insanely difficult to find and fix bugs, which frustrated me to no end.

By default you should make everything private unless it needs to be public. It will encourage you to think about the module external interface and minimize it.
Follow this advice, for your own sake. This is what I do now, and it helps so much in creating cleaner code where it's a lot easier to fix bugs.

I apologize if this question is too fundamental for the Forum, but knowing the "why" would help me a lot.
No need to apologize, it's an excellent question!
 
Upvote 0

craigc

Member
Thanks for the responses. I understand and it all makes sense. I have a lot of spaghetti code to clean up, but that is easier to make myself do now that I explicitly have the reasons. I appreciate having the rationale behind best practices.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…