Naming variables

pliroforikos

Active Member
Licensed User
Hi all
One of the worst think about Programming is the names of the variables or methods or subs etc. As application grows up it is difficult to handle so many names. So if you can share you naming tips it will be wonderful.
 

sfsameer

Well-Known Member
Licensed User
Longtime User
Hi all
One of the worst think about Programming is the names of the variables or methods or subs etc. As application grows up it is difficult to handle so many names. So if you can share you naming tips it will be wonderful.
I know how you feel, it's really important to name the variables + subs in a way that in case there is a team member working on the same project with you he won't be lost.

For me I always name the variable starting with 3 Letters that define it's type then I write the name, for example :
intUserID (integer user ID)
strUsername (string username)

And for the datebase tables :
tblUsers
(tbl as in table)

:)
 

hatzisn

Well-Known Member
Licensed User
Longtime User
1) Start every variable with a prefix specific to the type f.e.:
sName = String
iPos = int
lngPos = Long

2) Always use camel case f.e. (more readable):
sManufacturingCode
iActualPos

3) Never Assume you will remember always why you created a variable so name it in a way it will remind you its function (f.e. as seen above)

4) It is a good technic to encapsulate all the related code in a class or module

5) Now that I read the previous answer that was posted while i was typing a good technic I also use is to categorize the tables by number f.e.
001_XXXXXXXXXXXX base tables uniquely set.
070_XXXXXXXXXXXX many to many tables
090_XXXXXXXXXXXX supplementary tables needed for specific tasks.
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
Same as @sfsameer. Plus, for private module level variables I add "m" - for "module" and for public global variable a "g".
B4X:
Sub Class_Globals
    Public gintValue As Int
    Private mstrName As String

(Public variable first, usually).

For custom types, just a "t":
B4X:
Type tPerson(strName As String, strSurname As String)
but I should use "typ" :)
 

stevel05

Expert
Licensed User
Longtime User
For important things that will break if misspelt like table names or Map Keys (if they are not derived) I use Constants so that I don't have to waste an hour looking for a bug that is a spelling mistake. If you misspell the Constant value, at least it will always be that way and will still work.
 

aeric

Expert
Licensed User
Longtime User
to categorize the tables by number f.e.
001_XXXXXXXXXXXX base tables uniquely set.
070_XXXXXXXXXXXX many to many tables
090_XXXXXXXXXXXX supplementary tables needed for specific tasks.
Ya, sorting the names following computer is important too. Use the same prefix for the same group of subs or database tables.

I have another practice which I try to follow when possible. I group subs together at bottom while event handling subs at top (or vice versa). Maybe I am having OCPD.

B4X:
Sub BtnProcess_Click
    ' Code
End Sub

Sub TextField1_TextChanged (Old As String, New As String)
    ' Code
End Sub

Sub SendData As ResumableSub
    ' Code
End Sub

Sub ProcessJob
    ' Code
End Sub

Also for the variable declaration, I list the views first followed by variables.
B4X:
Sub Process_Globals
    ' Views
    Private Label1 As Label
    Private Label2 As Label
    Private swBuild As B4XSwitch
    Private TextField1 As TextField
    Private TextField2 As TextField
    Private TextArea1 As TextArea
    Private BtnProcess As Button
    ' Variables
    Dim strPath As String
    Dim intCount As Long
End Sub

Edit:
Try to avoid using generic naming for views like the example above. Give them more meaningful names.
B4X:
' Bad
Private Label1 As Label
Private Label2 As Label

' Good
Private lblFirstName As Label
Private lblLastName As Label
 
Last edited:

hatzisn

Well-Known Member
Licensed User
Longtime User
... is to categorize the tables by number f.e.
001_XXXXXXXXXXXX base tables uniquely set.
070_XXXXXXXXXXXX many to many tables
090_XXXXXXXXXXXX supplementary tables needed for specific tasks.

I use this technic so that the categorized tables are easy to find in a database management environment.
 

Sandman

Expert
Licensed User
Longtime User
if you can share you naming tips it will be wonderful
This might be interesting for you:
"Hungarian notation is an identifier naming convention in computer programming, in which the name of a variable or function indicates its intention or kind, and in some dialects its type. The original Hungarian Notation uses intention or kind in its naming convention and is sometimes called Apps Hungarian as it became popular in the Microsoft Apps division in the development of Word, Excel and other apps."

Several messages in this thread have already touched upon this also, but I thought it might be interesting to read more about the system.
 

emexes

Expert
Licensed User
"is sometimes called Apps Hungarian as it became popular in the Microsoft Apps division in the development of Word, Excel and other apps."
Hungarian notation named for Hungarian programmer Charles Simonyi, originator of WYSIWIG word processing at PARC and then moved to Microsoft initially to develop (Microsoft) Word. I imagine he was not too keen on reverting back to text mode displays, but you gotta work with what you've got.

I also remember reading somewhere that Hungarian notation had fallen out of fashion at Microsoft.
 

Sandman

Expert
Licensed User
Longtime User
I also remember reading somewhere that Hungarian notation had fallen out of fashion at Microsoft.
I wonder what they use instead. Knowing Microsoft, they probably name all variables as UUIDs that are parsed in 17 abstraction layers by 5 different systems. Joke aside, it would be interesting to know what they landed on.

Oooh, imagine the UUID variables... :)
B4X:
Sub Process_Globals
    ' Views
    Private 6f581b34-5087-4aa9-b1e5-830d4e257b55 As Label
    Private f79d084d-e363-4036-962a-3064c4a0d836 As Label
    Private 16d24b43-7194-4981-8904-c2cec2ca0105 As B4XSwitch
    Private 659f7abd-27e4-4002-bea1-05088635a7b9 As TextField
    Private e5798d37-0316-4813-8de2-59e3f44a3e2a As TextField
    Private a402b560-1a93-44a2-886f-11b739d3f649 As TextArea
    Private 74d745f3-ae35-4523-9492-71a4ee114bbc As Button
    ' Variables
    Dim 577ad97e-30c3-44ae-b08d-ef6e4a992e7d As String
    Dim 75816702-8696-4650-8df2-d3b6beb30c93 As Long
End Sub
 

emexes

Expert
Licensed User
I also remember reading somewhere that Hungarian notation had fallen out of fashion at Microsoft.

Joke aside, it would be interesting to know what they landed on.


The Great Rebellion hit its peak with the first release of .NET. Microsoft finally started telling people, “Hungarian Notation Is Not Recommended.” There was much rejoicing. I don’t even think they bothered saying why. They just went through the naming guidelines section of the document and wrote, “Do Not Use Hungarian Notation” in every entry. Hungarian Notation was so doggone unpopular by this point that nobody really complained, and everybody in the world outside of Excel and Word were relieved at no longer having to use an awkward naming convention that, they thought, was unnecessary in the days of strong type checking and Intellisense.

https://www.joelonsoftware.com/2005/05/11/making-wrong-code-look-wrong/

Search (Ctrl+F) for the word "infamous" to jump to the section about Hungarian notation at Microsoft.
 
Last edited:

klaus

Expert
Licensed User
Longtime User
I use significant variable names.
Instead of:
I = C * R / 100
I prefer:
Interest = Capital * Rate / 100

For objects I use a 3 character prefix which represents the object type like:
lbl for Label
pnl for Panel
edt for EditText
imv for ImageView
etc.
 

Cableguy

Expert
Licensed User
Longtime User
I like to add as many comments as I see needed to later understand why I did it that way.
Also, although requested a couple of times, but never implemented, a "converted" code module as a Note pad is also interesting to use
 

LucaMs

Expert
Licensed User
Longtime User
I like to add as many comments as I see needed to later understand why I did it that way.
A great feature: you can add comment lines to your methods (Subs) that contain code that you can copy and paste; even many lines of code.

B4X:
' This routine is useful to...
' Example:
'<code>
' here many "ready" code lines that can be easily copied and pasted by just one click.
'</code>
Public Sub DoSomething
End Sub

I love it!
 

pliroforikos

Active Member
Licensed User
Thank you all for this wonderful conversation

You gave me a lot of ideas
So lets gather all things and make a summary.
  • Use 1 to 3 letters as prefix to indicate variable kind with small letters and then continue with 1 Capital example: iAge or intAge
  • Keep variables of views together in Sub Process_Globals and then add other variables
  • Use As many as you can comments
  • Before subs: Use <code> </code> inside comment for easy copy paste from examples

And what about Constants are you using all Capital Letters?
And what about function names? Are you using any prefix indicating the return value or something else?
What about Classes, Modules, B4XPages, layouts etc.


:)
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
Before subs: Use \[ code] \[/code] inside comment for easy copy paste from examples
<code> ... </code>


And what about Constants are you using all Capital Letters?
The most practiced way is to write them in capital letters and separate words by underscores. Remeber that in B4A underscores are used to prevent the obfuscation of the name.


And what about function names? Are you using any prefix indicating the return value or something else?
Not me, because already writing the signature of the function you can see the return type.


What about Classes, Modules, B4XPages, layouts etc.
I use respectively:
clsName, modName, clspagName, layName.
 

ginkgo

New Member
I use many of the identifier prefixes mentioned above and also often use a_ / l_ as a prefixes for function arguments / local vars,
in addition to g_ / m_ for global / module scope. I will drop the underscores if the code is easily read.
I have found self-aware identifiers help me write more robust code.
 

William Lancee

Well-Known Member
Licensed User
Longtime User
Since B4X is a strongly typed language, I don't generally use type prefixes, however, like Klaus, I do use them for views.
I also use a type prefix if I have two type versions of the same object, for example:

B4X:
Dim mean As Float = sum / n
Dim strMean As String = NumberFormat2(mean, 1, 4, 4, False)

I also use an underline suffix for initialization parameters (they exist briefly but I like to know what they are from intellisense):

B4X:
Sub Class_Globals
    Private xui As XUI
    Private MP As B4XMainPage

    Private callBack As Object
    Private cv As B4XCanvas
    Public name As String
End Sub

Public Sub Initialize(name_ As String,  callBack_ As Object, cv_ As B4XCanvas)
    MP = B4XPages.MainPage
    name = name_
    callBack = callBack_
    cv = cv_
End Sub
 
Last edited:
Top