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 `` 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.


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 :)

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…

Duplicate value found: duplicates value on record with id: <unknown>.

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: <unknown> duplicates value on record with id: <unknown>.The above error is triggered in the database layer and caused by a trigger or workflow outside of your main code of block that is bubbling this exception. This is rather difficult to track down especially if you are unfamiliar with the code, I am sharing my procedure in the hopes this saves you time - if you find this helpful drop me a line or follow me on twitter @danielsokolows. This error is caused by unique field constraint on the object, so the first step is to examine the object and locate the API names of all unique fieds. You can do this through SF direclty 'Setup < Customize &lt <object being inserted> &lt Fields' or by downloading the `src/objects` metadata information and searching for <unique>; I preffer the latter and actually download ALL matadata information f…