Good programming practice How to JavaScript

JS: IIFE(Immediately-invoked function expression ) and safe code

Sometimes we need to declare the function and call it immediately. Below code does that, it can also accept the parameter:

So we will see “hello John”. What happens here is that we wrapped the anonymous function inside the (); brackets. This allows to declare the anonymous function. Because JS compiler treats everything inside () as declaration. Then we add function and call it

second () actually invokes the function. The rest is easy we just pass the variable that we need. You can see the first example.

My understanding is that this approach is used in many frameworks.  It also possible to put the execute brackets () outside the function wrapper. Both are ok:

The beauty of the above code is that it’s safe. By that I mean that this code will run inside it’s own execution context and not global one.

So, variable “greetings” will not clash with variables with same name, but from other places in code.

If, however, we want to overwrite some existing global variable we can do it this way:

File 1, has this code:

File 2, has this code:


Good programming practice How to

JS: good practices

  1. Be careful when returning some values


function test(){



cacheable: true;



console.log(test()) ;



// console.log will return undefined, all of this is happening because the js compiler puts ; (semicolon) after the return statement. Therefore, it’s good practice to have curly braces directly after the return statment

ex: return {

cacheable: true;


2. Writing comments

The JS compiler will ignore the white spaces and comments so we can do something like this:


// this is the name of the user


// this is the surname of the user



// this should be always more than 0



JS: storing function inside array and calling that function

We can store function inside the array and call it.


var arr = []; // note this creates array in js

var obk = {} // this creates empty ojbect


var arr = [ “1”,



{ name = “James”,

address = “Mulholland Drive”



var greeting = “hello “;




Now we have different variables inside our array, we even have another array and function!

To call the stored function:

Note: to call the method use arr[4]()

arr[4](arr[3].name);  // this will get the name from the array under index 3 and pass it to the function

// output will be: hello James


JS function parameters: arguments and Spread

Unlike Java, JS will not throw any errors if the way function is declared and called is different. So there are 2 scenarios when developer passes wrong number of parameters. Either too many or too less

If too many:

Ex: function xyz(x, y,z){




// output will be undefined

So, to tackle this potential issue when other developers pass more arguments to a method than it can accept we can use either arguments or spread.


function abc(a, b,c){



// arguments is keyword and will return a,b,c or whatever arguments passed to abc function.

Works a bit similar to this keyword can access it through arguments[n]

Another way is using spread:


function restParam(parameter1, …restArgs){
console.log(restArgs.length); // Logs the number of arguments that do not have a corresponding parameter
console.log(restArgs[2]); // Logs the 3rd argument after the number of arguments that do not have a corresponding parameter

// Log would be as follows
// 4
// 4

// 4 is the number of arguments that do not have a corresponding parameter
// 4 is the 4th 3rd argument after the number of arguments that do not have a corresponding parameter

Second way is preferred, because arguments only return array like collection. The keyword is “like”, so some array methods won’t work. (arguments.length and arguments[n] will still work )

Oh, yeah. In the case if there are less parameters passed

If too less:

just assign the functions parameters to a default variable:

fuction zzz(a, b,c){

a= a || “a”;

b=b || 7;

c = c || 10;



2 Years anniversary!

I created and maintaining this blog for more than 2 years now! YAhoooo!!!

This blog was initially created on 

I’m thinking to move this blog to a new place, so that it will be easier to post the posts with code inside, because currently the posts with code don’t look too good.

How to

How to: Missing commit (error: unpack failed: error Missing commit) on Smargit

Go to the folder where your project is stored, then open the Git bash and run below commands:

  1. git gc
  2. git pull –rebase
  3. git push

In case you have this error:

error: cannot pull with rebase: You have unstaged changes.
error: please commit or stash them.

Then, just commit the files first. You can do it in the smart git.


How to

How to: Show proper unit test coverage

Note: make sure that Code Coverage tab has correct package name for coverage.

Also, can enable the coverage in testing folders

Python Uncategorized

Python: Revere string

Probably the easiest and close to the fastest way to reverse a string is to use Python’s extended slice syntax. This allows you to specify a start, stop and step value to use when creating a slice. The syntax is: [start:stop:step].

s = "String to reverse."
print s[::-1]

If start is omitted it defaults to 0 and if stop is omitted it defaults to the length of the string. A step of -1 tells Python to start counting by 1 from the stop until it reaches the start.

When working with large strings, or when you just don’t want to reverse the whole string at once, you can use the reversed() built-in. reversed() returns an iterator and is arguably the most Pythonic way to reverse a string.


s = "String to reverse."
print ("".join(reversed(s)))

Java utils collections: Map

Difference between HashMap, LinkedHashMap and TreeMap

  • HashMap makes absolutely no guarantees about the iteration order. It can (and will) even change completely when new elements are added.
  • TreeMap will iterate according to the “natural ordering” of the keys according to their compareTo() method (or an externally supplied Comparator). Additionally, it implements the SortedMap interface, which contains methods that depend on this sort order.
  • LinkedHashMap will iterate in the order in which the entries were put into the map


taken from:

Company related How to

Check the tomcat app.log in case of unexpected error

If you encounter some unexpected error with your web app, the best place to check what’s wrong is Tomcat’s app.log file.

1. Firstly, ssh to the server containing your app:

$ssh -A ( you can set shortcuts like in this file  ~/.ssh/config)

2. Get the root access

$sudo su –

3. Get the tail of the log. Tail command is useful to see the latest log entries and -f helps to keep refreshing it.

$tail -f app.log

Then replicate the bug, you should see the error message there. It can be null pointer exception for instance.