Posted in Interesting, Programming, Web Tech

IIFE : Immediately Invoked Function Expression

What is IIFE?

As the name says, IIFE, Immediately Invoked Function Expression is a function which is immediately invoked as soon as it is defined in javascript programming. It is a very popular way of handling javascript programs where the interpreter runs it only once.

Different syntax:

  1. ( function( ){ //statements })( );
  2. ( function( ){ //statements }( ));
  3. ( ( )=>{ //statements })( );

It has two parts: One is the function enclosed within () group operator, helps in accessing variables within function and second part which directly creates the immediately invoked function expression () which the javascript engine interprets. This whole concept of design pattern is called as Self Executing Anonymous Function.

As the function name is not used, they are called as anonymous function.

When is IIFE important?

As IIFE is executed once it is defined and interpreted once by javascript engine.

It maintains encapsulation and avoids polluting of variables which are not required for global access.

The code which does not have reusability can also be a part of IIFE. Hence this function is also an anonymous function.

Posted in Interesting, Programming, Softwares, Web Tech

Because…”this” is not that

Yes! You read it right. This is not that in Javascript and otherwise.

The this keyword in javascript can be confusing for a naive JS learner. Its behaviour varies depending on the execution context. In general, this refers to the object under current execution.

  • this, when used alone refers to a global object.
var a = this;
console.log(x);

A browser window

  • this, when used in an object method, refers to owner of the object method.
var car = {
 model: BMW,
 colour: black,
 details: function(){
    return this.model + " " + this.colour;
 }
};

this refers to the car object. The car object is owner of its properties and methods.

console.log(car.details());
BMW black
  • this, when used in a function refers to a global object
function testFunction() {
  return this;
}

returns the global object [object Window]. However, in strict mode, the above testFuntion returns undefined

  • this, when used in event handling

refers to the html DOM element on which the event listener is placed

Click here!

alerts with button tag name.

Posted in Interesting, Open Source, Programming, Web Tech

Spaghetti code

Imagine a bowl of cooked spaghetti and you manage to take a few of its strands on a fork. Unfortunately, few fall back to bowl. Now, trying to search for which is the longest string or the start point of it. Quite a task. Isn’t it? Analogically, this is what happens when we have a spaghetti code.

What is spaghetti code?

Spaghetti code, as the name says it is an unstructured, complex and tangled nature of  source code which is troublesome to maintain and comprehend for fellow programmers.

How does it get introduced?

Not implying the design patterns expected for a programming language per se.

The code starts simple but as and when new features get appended for the sake of scalability not understanding the existing code can introduce redundant code and looping/jumping of program from one to other, back and forth. Also if multiple developers work simultaneously problem can be worse.

Overuse of workarounds to serve the purpose of functionality for time being and never refactoring it.

Is spaghetti code avoidable?

Yes. It is definitely avoidable if as a code developer we use more readable variables and provide comments to make it comprehensible.

Before beginning writing new feature understand what the existing code does and is it possible to tweak it a little bit to improvise new feature.

If there is a reusable code block its a good practice to isolate it in a function construct and make a call from other code block on need basis.

Posted in Interesting, Open Source, Programming, Web Tech

Boilerplate code

What is a boilerplate code ? 

In computer programming is a section of code that has to be repetitively used with a little or no alteration . This term is interchangeably used with bookkeeping code.

Can we avoid the usage of boilerplate code ?

This seems to be debatable question. We can’t avoid the use of it all the time as not all the programming languages have the the non-verbosity. Other means to avoid its use is application of meta programming concepts, abstraction, inheritance, keeping the business logic in a separate globally accessible module and reusing it on need basis.