B4J Question (Closed) Good practices when creating classes

aeric

Expert
Licensed User
Longtime User
1. How do you avoid creating class with name that won't conflict with your code?
Example, I create a class name ID.
When I declare a variable
B4X:
Dim id As Long
This will cause a warning:
B4X:
Variable name is the same as a module name. This can cause problems during debugging. (warning #30)

2. Use prefix?
Example, use ClsID instead of ID.
It is a solution but for me the class name is not elegant.

3. Is there any way that we can use namespace?
Example,
B4X:
Dim id As Invoice.ID
where ID is a class.
Maybe the class can be put inside a folder or nested folders?

I am trying to create a b4xlib that contains 100+ of classes with getters and setters to use in my project.
 

Alexander Stolte

Expert
Licensed User
Longtime User
1. How do you avoid creating class with name that won't conflict with your code?
Simple... with a prefix, also has the additional advantage that you can immediately see what type of module it is.

Example:
B4X:
clsId
mFunctions
frmMain
b4xpSecondPage
B4X:
cls_Id
m_Functions
frm_Main
b4xp_SecondPage

3. Is there any way that we can use namespace?
would be new to me
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Simple... with a prefix
That is what I am not fond of.

Then we will see something like
ClsInvoice.ClsDocument.ClsCustomer.ClsIdentification.ClsPayment.ClsTotal.ClsAmount...

or

Cls_Invoice.Cls_Document.Cls_Customer.Cls_Identification.Cls_Payment.Cls_Total.Cls_Amount

and I always avoid using underscores.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I 'll try to convert all the Classes to Type and see how.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
That 'Variable name is the same as a module name. This can cause problems during debugging. (warning #30)"

is a tad misleading as in reality the variable is named _id and the class is named id.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Then we will see something like
ClsInvoice.ClsDocument.ClsCustomer.ClsIdentification.ClsPayment.ClsTotal.ClsAmount...

or

Cls_Invoice.Cls_Document.Cls_Customer.Cls_Identification.Cls_Payment.Cls_Total.Cls_Amount
No you won't unless you also give the instantiated variable's name a cls / cls_ prefix. More likely, you will see something like:

B4X:
Invoice.Document.Customer.Indentification.Payment.Total.Amount
Because Invoice is the variable name of an instance of the ClsInvoice class, Document is the variable name of the instance of ClsDocument, and so on.

For a larger B4XLib, note that you will have two types of classes, public and private, yet ALL classes are exposed. What you could do, is prefix the public classes with your initials and prefix all private classes with something like zzz_. Then when the lib is used, make users aware that the zzz_ are not for public consumption and by prefixing the classes with your initials (or whatever), they have a good chance of not clashing with other classes named by other people
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
public and private
Please note that B4X does not give you public and private classes. What I mean with this is that some classes are there for public consumption (outside of the B4Xlib) and others are support classes for the code within the B4XLib and are not really there to provide any meaningful support outside of the B4Xlib.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I am not sure I have explained clearly.
The b4xlib contains like 100+ class file like:
ID.bas
TaxTotal.bas
TaxAmount.bas

When I use this b4xlib in my new project, there are conflict.

There are a few times in the past that I cannot use the variable name same as the class.
e.g Object, Signature (B4XEncryption) which I wanted to use to generate my e-Invoice. So I have to use another names.

Edit: The problem is the class names may be very "common" and usually can be "good candidates" for variable names.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I am not sure I have explained clearly.
You have. For a large lib that I did, I faced the same situation. I actually faced two
1) I had classes that were "public", and I wanted to make sure that they would not conflict with any other classes that someone may have. My solution was to use a prefix that may not be as ubiquitous and therefore allow the lib to be used without issue in (hopefully) 99% of the time
2) There were many classes in the library that were there only to be used by the library, yet they are exposed by the B4Xlib. In order to distinguish between the public classes and the "private" classes, I used a zzz_ prefix, which a) would show up last in an alphabetical listing within the IDE and b) clue a user in that they should not use them outside of the lib (nothing stopping someone to do it, but then it's up to them if things go BOOM).

This is just a limitation of how name-spacing in B4X works and this is just one way of coping with that issue.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Then we will see something like
ClsInvoice.ClsDocument.ClsCustomer.ClsIdentification.ClsPayment.ClsTotal.ClsAmount...
No, you will never have a line like that, as those will be objects (or class members) not classes.

clsInvoice -->Invoice (object - instance)
clsDocument -->Document(object - instance)

Dim ...

Invoice.Document.XXX......


P.S. I only now see that there were other posts I still have to read!
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
Personally, I NEVER use prefixes in code and it drives me me crazy when others do. I like suffixes because I want to start typing the name, not the prefix. It also keeps related items together in an alpha list.
IDCls
IDFrm
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I like suffixes because I want to start typing the name, not the prefix. It also keeps related items together in an alpha list.
What if you don't remember the name exactly? What if you suppose that the name of the variable you need starts with "e" and instead it starts with "a"?

If you don't remember the name of a page or a form b4j, etc. by writing the prefix they are listed:

pagProduct
frmDetails

(often)
lblImport
txtImport
btnStart
clvProducts
...
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
What if you don't remember the name exactly? What if you suppose that the name of the variable you need starts with "e" and instead it starts with "a"?

If you don't remember the name of a page or a form b4j, etc. by writing the prefix they are listed:
personal preference:
MyMod
MyFrm
MyMap
MyInt

All related objects are together.
 
Upvote 0
Top