2.6 Variable naming conventions

The concept

In our previous posts, we mentioned that you can pretty much name your variable anything you want. But it doesn’t mean you should give them just random names. For Apex compiler, it really doesn’t make any difference. But remember, our programme is written for human to read, not for compilers.

So the following code:

Decimal personalIncome = 5000;
String firstName = 'Lance';
Boolean isWorkingDay = true;

and

Decimal a = 5000;
String b = 'Lance';
Boolean c = true;

are the same from a compiler’s point of view. However, you should definitely follow the first code sample’s naming conventions to make your variable names meaningful.

Imagine you are explaining to your support team:

“I am trying to insert a record into p and z is giving me a validation error. I tried to update a but c would not let me. Do i need to update y first, not sure cause I know the aa is set properly.”

I can almost hear the crying from the support team. It is the same thing for variable name. You should make the variable names make sense so that it will save so much time and effort in the future to look into and understand this code.

Common Variable Name Conventions

Your variable name conventions is usually pre-defined in the dev team. It doesn’t always need to be the same. As long as it consistent and make sense, it should be fine. As we are learning, I am going to introduce the general naming conventions for Java, as currently we don’t have on for Apex yet:

Identifier type Rules for naming Examples
Classes Class names should be nouns in UpperCamelCase, with the first letter of every word capitalised. Use whole words — avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).
  • class Raster;
  • class ImageSprite;
Methods Methods should be verbs in lowerCamelCase or a multi-word name that begins with a verb in lowercase; that is, with the first letter lowercase and the first letters of subsequent words in uppercase.
  • run();
  • runFast();
  • getBackground();
Variables Local variables, instance variables, and class variables are also written in lowerCamelCase. Variable names should not start with underscore (_) or dollar sign ($) characters, even though both are allowed. This is in contrast to other coding conventions that state that underscores should be used to prefix all instance variables.Variable names should be short yet meaningful. The choice of a variable name should be mnemonic — that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary “throwaway” variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.
  • int i;
  • char c;
  • float myWidth;
Constants Constants should be written in uppercase characters separated by underscores. Constant names may also contain digits if appropriate, but not as the first character.
  • static final int MAX_PARTICIPANTS = 10;

This table is copied from wikipedia.

As we are introducing Apex in our course and some variables don’t have much actual usage, I will still use some simplified variable names like s or d in the future. But make sure you use the proper variable name if you are programming something meaningful.

Next Post

3.1 Define a custom object using class

Subscribe to Sfdcinpractice

Subscribe to get the latest blogs and tutorials of sfdcinpractice. No spam, no trash, only the awesome posts from sfdcinpractice. 

Comments

  1. http://tenuousmecca8068.hazblog.com - May 28, 2017 @ 5:17 pm

    It’s very straightforward to find out any topic on web as compared to textbooks, as I found
    this piece of writing at this site.

Leave a Reply

Your email address will not be published / Required fields are marked *