Skip to main content

I prefix my variables with their type!

Why 'Systems Hungarian' makes sense.

As a developer, if you prefix the variable type you are clearly in the minority - I for one am a proponent of Systems Hungarian and belong to that group myself.

The merits become apparent when reading other people's code, or when you look at your own code months/years after. Properly naming variables can be hard, the type is not always obvious from the name itself and left to individual interpenetration. Prefixing completely removes any uncertainty, it immediately reveals the type of the variable, and the developer reading the code does not have to go through the mental friction of inferring from either the name, or from how it is being used in the context, or traversing to where the variable was defined.

For weakly typed languages like JS/Python it has the huge additional benefit of making debugging that much easier, when one treats the prefix as the intent of what should be the value stored it reveals possible issues immediately when stepping through code.

When the end goal is the benefit to 'other' developers reading your code Systems Hungarian also makes perfect sense. Yes, it does mean some time getting used to it, but it's no different then any other coding pattern you habituate yourself to.

We end up gravitating towards the familiar and end up finding other patterns ugly, this in my opinion is the root cause of the negativity against type prefixing, or any naming conventions you don't use everyday. In the end I prefix because it has increased my own code readability to me.

JavaScript and Apex variable prefixing

Variables are prefixed using letters from primitive types, the base 'Object', or `f` for functions. Only exception are arrays that are prefixed with `a` even though the base type is an Object - this keeps the rule list small:

// Note: you can use `Object.prototype.toString.call(variable)` to get the primitive type of it. 

var nYear; // for Numbers 
var bGender; // for Booleans
var sName; // strings
var oTable; // objects
var aScores; // arrays or oScoresArray if you wish
var fAge = function () {}; //gets age of the person

I was happy to find at least one other person thinks the same: what-do-you-mean-by-the-initial-letter-on-javascript-variables-functions.

Apex primitive types variable are prefixed as follows

Blob blBlobieFoo; 
Boolean bActionVisible;
Date daDateOnly
Datetime dtStartDateAndTime;
Decimal deAgeOfUniverise;
ID idUserRecord;
Double dBiggerNumber;
Integer iUserAge;
Long lTurlleAge;
Object oUserRecord;
Object oContentList;
Object oContentMap;
String sFirstName;
Time tAlarm;

mUserAge(...); // undecided on this one

What are you thoughts on this? But please no hating.

Comments

Popular posts from this blog

Opera SOCKS Proxy Setup Issues

SOCKS error: Connection closed by remote server When setting up SOCKS only proxy in Opera web browser make sure to specify the IP address instead of your fully qualified domain name (FQDN). Using a host name will not work and Opera simply fails to connect to the proxy server. Sample working setup screen shot with a Dante proxy server , note that HTTP, HTTPS, FTP, SSH options are left blank. Bug DSK-364301 has been filled with Opera, if it has been a while since this post and the issue still not fixed feel free to add your word of encouragement by emailing DSK-364301@bugs.opera.com :)

Storing passwords in PuTTY

How to save SSH username/password for auto login in PuTTy The answer is you can't do it...at least in plain PuTTy. However there is an awesome fork with that let's you store the username and password and other additional features called KiTTy.



So grab yourself a copy and +1 this if you do, thanks.

How to child proof a fireplace

DIY - Do it yourself fireplace child guard Our wonderful 8.5 month old Sofia has become a crawling race car with an untamed thirst for exploration. And so with the cold nights approaching we needed to child proof the fireplace. This however proved to be more difficult than would reasonably expect, I've checked the local Toys "R" Us, Walmart, and even a Canadian Tire with no success for a ready to use product. Internet search was more fruitful and returned a few online stores one could order from, however in all honestly they didn't look too sturdy to me. So I build my own relatively quickly and inexpensively. Materials needed is a privacy plastic lattice - the smallest hole pattern - a few screws and anchors; tools needed are a drill, and a handsaw if you don't have the lattice cut at the store - that’s it. The construction consits of screwing the lattice into the wall and the final product is easiest explained through following pictures. Feel free to +1 this po…