Monday, 16 March 2015

Javascript "let" keyword vs "var" keyword

One of the differences between JavaScript and Java, is regarding variable scope.

In JavaScript there are two kinds of scope, global scope and function scope. Any local variable declared in a function, is visible everywhere in the function.

In the example above, both secondCarName and thirdCarName are local variables, only accessable within the function.

Note that, though the thirdCarName is declared inside an if-then block, this matters naught.

A gobal variable is visible everywhere. In the example above, this would be firstCarName.


In contrast, in Java, scope can be within a method (sort of the equivalent of function), but also in block scope.

Therefore, netherlands is only available within the if statement.

let vs. var

Apparently, ECMAScript saw fit to provide JavaScript with a way to also support the Java-way of scope using the new let keyword.

But be wary, there will probably always be situations where Java and JavaScript will display different behaviour.

With let, it is possible to create a variable that obscures the variable in the higher block. This is in contrast with Java, where a error is thrown during compiling.


My experience with JavaScript is spotty at best. The intricacies of scope in Java and JavaScript are more subtle than the isolated examples described here. And the new let keyword makes the scope in JavaScript even more subtle.


Stackoverflow - Javascript - “let” keyword vs “var” keyword
JavaScript|MDN - let keyword
Hangar.Runway 7 - The Javascript Guide to Objects, Functions, Scope, Prototypes and Closures

Thursday, 12 March 2015

Enhanced For Loop in JavaScript

I am no expert in JavaScript, but I feel I should mention the possible flavours of for loops in here.

The forEach method is explained in [4].

The latter example is new in ECMAScript 6 (which should be out already).3

You can test it out in [5].


[1] Mozilla Developer Network - for...of
[2] Stackoverflow - For-each over an array in JavaScript?
[3] ECME-262 6h Edition, The 2015 ECMAScript Language Specification ("Harmony")
[4] forEach
[5] JSFiddle - example in action

Wednesday, 4 March 2015

Sequence Generators and Hibernate

Hibernate has a Hi-Lo Sequence Generator that maps to a Database Sequence (for example Oracle Sequence) with a multiplication.

What this means is, when you retrieve the next value from the Database Sequence (which returns 1), Hibernate will multiply that with a "allocationSize", the default for which is 50.

Now Hibernate can (stored at application level) use it to create PK numbers from 1 to 50.

The next time a Database Sequence value is retrieved (for example 2), Hibernate gets to create PK numbers 51 to 100.


This seems to be a valuable strategy, if your database sees a lot of inserted records and high loads and the sequence starts to be a bottleneck.

This can be a valid issue, for example when you run Batch jobs that cause a lot of inserts for every transaction.

Caveat Emptor

The reason for this blog is that a Colleague of mine at work, got a customer on the phone who was spooked, as his new PKs suddenly were a noticable factor bigger.

And, of course, it is a living hell to change this sequence schema afterwards to something more simple, as you run the risk of duplicate PKs.


“Only think of database sequences as unique - nothing more, nothing less.”
This means that the one task of a sequence generator is to provide you with values that have not been used yet, not will they be ever again.

It is an error, to assume that there is any kind of ordering in the numbers issued, or that there are no 'holes' in the sequence.


Vlad Mihalcea's Blog - The hi/lo algorithm
JavaDocs 7 - SequenceGenerator
StackOverflow - Hibernate, @SequenceGenerator and allocationSize
Fixing JPA2 Sequence Generator Problem With Hibernate 3.5