A Case for Hungarian Notation in JavaScript
Tags: hungarian, javascript
Published: 2024-03-25 21:54:25

The most lowly newbie JavaScript programmer knows that JavaScript, for good or bad, is a dynamically typed language and that a variable can take on any value. Many attempts have been made to somehow rectify this situation so that type errors occur less often. A seasoned (I taste like seasoning salt) JavaScript programmer also knows that type errors are not actually that common. I do not intend to denigrate (much) any of those approaches here (TypeScript, JSDoc etc.) but instead I will make a case for a JavaScript specific Hungarian Notation. It you don't know what Hungarian notation is, here is the Wikipedia page...[Hungarian Notation](https://en.wikipedia.org/wiki/Hungarian_notation)

Full disclosure; I have not used this in anything but small test programs but I have used Hungarian Notation before (when it was all the rage...was it ever all the rage?). I think it has two qualities that elevate it above other solutions. 

1. It gives immediate knowledge as to the type of a variable.
2. It is low impact in regards to typing (both kinds).

Lets look at JavaScript types.

String
Number
Bigint
Boolean
Symbol
Object
Array
Date
Set

Undefined and Null are technically types but I will not consider them for this purpose. I intend to keep this system simple. For the most part I think the letter choices seems simple. Our major complication is String,Symbol and Set. We could use "c" for strings (characters) but I think we should stick with "s". Maybe sets should be "se". We don't prefix Symbols because maybe that doesn't make much sense? So here is what our prefixes will be. It's kinda weird that Bigint literals are suffixed with n. But then again Bigints are big, stupid and largely useless. Oh well, they didn't ask me.

|Type|Prefix|
|-------|-------|
|String |s      |
|Number |n      |
|Bigint |i      |
|Boolean|b      |
|Symbol |N/A    |
|Object |o      |
|Array  |a      |
|Date   |d      |
|Set    |se     |

Let's give it a try. Here is a quicksort and I am using it to sort a string of letters. 

```javascript
let sNotWritingJustTyping = 'thequickbrownfoxjumpsoverthelazydog';

function swap(aItems, nLeftIndex, nRightIndex){
    let vTemp = aItems[nLeftIndex];
    aItems[nLeftIndex] = aItems[nRightIndex];
    aItems[nRightIndex] = vTemp;
}
function partition(aItems, nLeft, nRight) {
    let nPivot   = aItems[Math.floor((nRight + nLeft) / 2)], //middle element
        i       = nLeft, //left pointer
        j       = nRight; //right pointer
    while (i <= j) {
        while (aItems[i] < nPivot) {
            i++;
        }
        while (aItems[j] > nPivot) {
            j--;
        }
        if (i <= j) {
            swap(aItems, i, j); //sawpping two elements
            i++;
            j--;
        }
    }
    return i;
}

function quickSort(aItems, nLeft, nRight) {
    let nIndex;
    if (aItems.length > 1) {
        nIndex = partition(aItems, nLeft, nRight); //index returned from partition
        if (nLeft < nIndex - 1) { //more elements on the left side of the pivot
            quickSort(aItems, nLeft, nIndex - 1);
        }
        if (nIndex < nRight) { //more elements on the right side of the pivot
            quickSort(aItems, nIndex, nRight);
        }
    }
    return aItems;
}
// first call to quick sort
let sSorted = quickSort([...sNotWritingJustTyping], 0, sNotWritingJustTyping.length - 1).toString().replaceAll(',','');
console.log(sSorted); //prints abcdeeefghhijklmnoooopqrrsttuuvwxyz
```
"Wait!" you say. You notice a few things. What's with the "v"? Well sometimes we really don't know what a variable will contain so it is a variant type. What about function names and index variables? You could, if you wanted to go nuts with this, and denote what a function returns. Swap is a void function so it would return undefined so it would be uSwap...ah crap looks like we have to have undefined after all. Partition returns a number so it would be nPartition and quickSort would be aQuickSort. I am of two minds about this. I think published public interfaces and class names should not be Hungarian so as to not impose that style on a third party's code. I think the standard one letter indexing variables should just stay their good ole Fortran'y selves. So here is our new list

|Type|Prefix|
|---------|-------|
|String   |s      |
|Number   |n      |
|Bigint   |i      |
|Boolean  |b      |
|Symbol   |N/A    |
|Object   |o      |
|Array    |a      |
|Date     |d      |
|Set      |se     |
|Undefined|u      |
|Variant  |v      |

I think this method of naming helps with an issue that concerns programmers without introducing anything more complicated than prefixing variables and without yat (Yet Another Transpiler). I am also of the opinion that when possible, variables should be initialized with a value of their intended type even if that value is never used. Hungarian Notation and good initialization practice should be sufficient for other programmers on your project(s) to glean your intentions as to variable type. 

Thanks for reading! 

BTW I yoinked the quick sort from [QuickSort Algorithm in JavaScript](https://www.guru99.com/quicksort-in-javascript.html)