Jeetu Alex - IT Software Professional specialized in developing high-end, customized business solutions using Coldfusion and Java.

Developer's blog
web knowledge base

Coldfusion
Java
JQuery

An online resource for latest web technologies like Coldfusion, JRun, Pro*C, JQuery, HTML5, PHP, W3C, Java, J2EE, C, C++, ORACLE, PL/SQL, MySql, Ajax, Coldbox, Fusebox, UNIX, JavaScript, NodeJS and much more...

Tuesday, March 28, 2017

PHP Frameworks

Laravel

When speaking about Laravel, we are referring to Laravel version 4 and beyond. Laravel 4 was released in 2013 and represented a complete rewrite of the framework. The functionality of the framework was decoupled into separate components, which were managed with Composer, instead of everything being in one single huge code repository.

Laravel declares itself as a framework for rapid development with a simple and beautiful syntax which is easy to learn, read, and maintain. It is the most popular framework in 2016. According to Google trends, it is three times more popular than other frameworks, and on GitHub, it has two times more stars than competitors.

Symfony

Symfony 2 was released in 2011, but it must not be confused with Symfony 1, which was a totally different framework with different underlying principles. Fabien Potencier created Symfony 2, and the current version is 3.2, which is an incremental version of Symfony 2. Therefore, they are often called simply Symfony2/3.

Like Laravel 4, Symfony 2 is designed as a set of decoupled components. There are two benefits here: We can replace any component in a Symfony project, and we can take and use any Symfony component in a non-Symfony project. Symfony components can serve as great code examples and they are used in a lot of open source projects such as Drupal, phpBB, and Codeception. In fact, Laravel itself uses no less than 14 Symfony components. Understanding Symfony thus gives you many benefits when working with other projects.

See detailed comparisons in

  • Installation
  • Configurations
  • Routing and Controller
  • Template Engine
  • Dependency Injection
  • Object Relational Mapping (ORM)
  • REST API

And picking the Winner: Symfony or Laravel?

Friday, March 10, 2017

React, Redux and Immutable.js

React, Redux and Immutable.js are currently among the most popular JavaScript libraries and are rapidly becoming developers’ first choice when it comes to front-end development. In the few React/Redux projects that I have worked on, I realised that a lot of developers getting started with React do not fully understand React and how to write efficient code to utilise its full potential.
Some of the most common misuses of React and ways to avoid them.

Data Reference Problem
Any React app should mostly consist of small simple (or stateless function) components. They are simple to reason about and most of them can have shouldComponentUpdate function returning false.

shouldComponentUpdate(nextProps, nextState) { return false; }

Performance wise, the most important component lifecycle function is shouldComponentUpdate and if possible it should always return false. This ensures that this component will never re-render (except the initial render) effectively making the React app feel extremely fast.

Equality check for primitive data types like boolean, string and integer is very simple since they are always compared by their actual value:

1 === 1
’string’ === ’string’
true === true

On the other hand, equality check for complex types like objects, arrays and_functions_ is completely different. Two objects are the same if they have the same reference (pointing to the same object in memory).

const obj1 = { prop: ’someValue’ };
const obj2 = { prop: ’someValue’ };
console.log(obj1 === obj2);   // false

Even though obj1 and obj2 appear to be the same, their reference is different. Since they are different, comparing them naively within the shouldComponentUpdate function will cause our component re-render needlessly.

Handling References
This is not an easy task if we want to do it in a nice, clean, and performance optimised way. Facebook realised this problem a long time ago and called Immutable.js to the rescue.

import { Map } from ‘immutable’;

//  transform object into immutable map
let obj1 = Map({ prop: ’someValue’ });  
const obj2 = obj1;
console.log(obj1 === obj2);  // true

obj1 = obj1.set(‘prop’, ’someValue’);  // set same old value
console.log(obj1 === obj2);  // true | does not break reference because nothing has changed 

obj1 = obj1.set(‘prop’, ’someNewValue’);   // set new value
console.log(obj1 === obj2);  // false | breaks reference 
obj1 = obj1.set(...);.
See the full article here

How to get all possible combinations of elements in an array including order and lengths

Below is a recursive function which gives all possible unique combinations of elements in a array in all order and lengths.

Alternate 1

function getAllCombinations(arr) {
  var inputArray = arr;
  var resultArray = [];
  var combine = function() {
    var args = arguments;
    var temp = [];
    for (var i in args) {
      temp.push(inputArray[args[i]]);
    }
    if (temp.length > 0) {
      resultArray.push(temp);
    }
    for (i in inputArray) {
      temp = [];
      for (var j in args) {
        if (args[j] == i) {
          temp = false;
          break;
        } else {
          temp.push(args[j]);
        }
      }
      if (temp) {
        temp.push(i);
        combine.apply(null, temp);
      }
    }
    return resultArray;
  };
  return combine();
}
Alternate 2

function getAllCombinations(arr) {
  var inputArray = arr;
  var resultArray = [];
  var conditionNext = true;
  var combine = function() {
    var args = arguments;
    var temp = [];
    for (var i in args) {
      temp.push(inputArray[args[i]]);
    }
    if (temp.length > 0) {
      resultArray.push(temp);
    }
    for (i in inputArray) {
      conditionNext = true;
      if (args.length !== 0) {
        for (var j in args) {
          if (args[j] == i) {
            conditionNext = false;
          }
        }
      }
      if (conditionNext) {
        temp = [];
        for (j in args) {
          temp.push(args[j]);
        }
        temp.push(i);
        if (temp.length > 0) {
          combine.apply(null, temp);
        }
      }
    }
    return resultArray
  };
  return combine();
}

Alternate 3

function getAllCombinations(arr) {
  var inputArray = arr;
  var resultArray = [];

  var checkCondition = function(args, nextIndex) {
    if (args.length === 0) {
      return true;
    }
    for (var i = 0; i < args.length; i++) {
      if (args[i] == nextIndex) {
        return false;
      }
    }
    return true;
  };

  var applyFunc = function(args, index) {
    var temp = [];
    for (var i = 0; i < args.length; i++) {
      temp.push(args[i]);
    }
    temp.push(index);

    if (temp.length > 0) {
      v.apply(null, temp);
    }
  };

  var populateResult = function(args) {
    var temp = [];
    for (var i = 0; i < args.length; i++) {
      temp.push(inputArray[args[i]]);
    }
    if (temp.length > 0) {
      resultArray.push(temp);
    }
  };

  var v = function() {
    var args = arguments;
    populateResult(args);
    for (var i = 0; i < inputArray.length; i++) {
      if (checkCondition(args, i)) {
        applyFunc(args, i);
      }
    }
  };
  v();
  return resultArray;
}
See the complete solution discussion here

Friday, March 3, 2017

The Top 10 Most Common Mistakes That Java Developers Make

Java is a programming language that was initially developed for interactive television, but over time it has become widespread over everywhere software can be used. Designed with the notion of object-oriented programming, abolishing the complexities of other languages such as C or C++, garbage collection, and an architecturally agnostic virtual machine, Java created a new way of programming. Moreover, it has a gentle learning curve and appears to successfully adhere to its own moto - “Write once, run everywhere”, which is almost always true; but Java problems are still present. I’ll be addressing ten Java problems that I think are the most common mistakes.

Common Mistake #1: 

Neglecting Existing Libraries It's definitely a mistake for Java Developers to ignore the innumerable amount of libraries written in Java. Before reinventing the wheel, try to search for available libraries - many of them have been polished over the years of their existence and are free to use. These could be logging libraries, like log back and Log4j, or network related libraries,...

Common Mistake #2:

Missing the ‘break’ Keyword in a Switch-Case Block
These Java issues can be very embarrassing, and sometimes remain undiscovered until run in production. Fall-through behavior in switch statements is often useful; however, missing a “break” keyword when such behavior is not desired can lead to disastrous results. If you have forgotten to put a “break” in “case 0” in the code example below, the program will write “Zero” followed by “One”, since the control flow inside here will go through the entire “switch” statement until it reaches a “break”.

Common Mistake #3: Forgetting to Free Resources

Common Mistake #4: Memory Leaks

Common Mistake #5: Excessive Garbage Allocation

Common Mistake #6: Using Null References without Need

Common Mistake #7: Ignoring Exceptions

Common Mistake #9: Breaking Contracts

Common Mistake #10: Using Raw Type Instead of a Parameterized One

Tuesday, July 5, 2016

Collections & Threads in Java

package com.jeetu.app.util;

import java.util.*;

public class test {
    public static void main(String[] args) {

        List linkedList = new LinkedList();
        executeLogic(addContents(linkedList, "LinkedList"), displayContents(linkedList, "LinkedList")).start();

        List arrayList = new ArrayList();
        executeLogic(addContents(arrayList, "ArrayList"), displayContents(arrayList, "ArrayList")).start();

        Set hashSet = new HashSet();
        executeLogic(addContents(hashSet, "HashSet"), displayContents(hashSet, "HashSet")).start();

        Set treeSet = new TreeSet();
        executeLogic(addContents(treeSet, "TreeSet"), displayContents(treeSet, "TreeSet")).start();

    }

    static Thread executeLogic(final Thread t1, final Thread t2) {
        Thread t = new Thread(new Runnable() {
            public void run() {
                while (true) {
                    try {
                        t1.start();
                        t1.join();
                        t2.start();
                        break;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        return t;
    }

    static Thread addContents(final Collection e, final String type) {
        Thread t = new Thread(new Runnable() {
            public void run() {
                long end;
                long start = System.currentTimeMillis();
                long limit = 5000000;
                for (int index = 1; index <= limit; index++) {
                    e.add(index);
                }
                end = System.currentTimeMillis();
                System.out.println("Write[" + type + "]: " + (end - start) + "  ms.");
            }
        });
        return t;
    }

    static Thread displayContents(final Collection e, final String type) {
        Thread t = new Thread(new Runnable() {
            public void run() {
                long end;
                long start = System.currentTimeMillis();
                int total = 0;
                Iterator i = e.iterator();
                while (i.hasNext()) {
                    total = total + (Integer) i.next();
                }
                end = System.currentTimeMillis();
                System.out.println("Read[" + type + "]: " + (end - start) + " ms ,Total = " + total + ".");
            }
        });
        return t;
    }
}

Result:
Write[ArrayList]: 12997  ms.

Write[LinkedList]: 13144  ms.

Read[ArrayList]: 185 ms ,Total = 1647668640.

Read[LinkedList]: 69 ms ,Total = 1647668640.

Write[HashSet]: 29986  ms.

Read[HashSet]: 69 ms ,Total = 1647668640.

Write[TreeSet]: 38823  ms.

Read[TreeSet]: 67 ms ,Total = 1647668640.



Process finished with exit code 0

Saturday, June 18, 2016

JavaScript: Understanding the Weird Parts



Examples:

var p = {name: 'a', getName: function(){ return this.name;}}

function greet(p) {
    console.log(p.lastname);
}

console.log(p.getName());
greet(p);
greet({lastname:'mary'});



/*var a;

//a = 0;

if(a || a === 0) {
        console.log(a);
}*/


/*var b;
if((b = 2) * 2 === 4) {
    console.log(b);
}*/



/*function wait3mins() {
    var date = new Date().getTime() + 30000;
    while(new Date() < date) {
        
    }
    console.log("finished");
}

function clickHandler() {
    console.log("clicked");
}

document.addEventListener("click", clickHandler);

wait3mins();
console.log("final finished");*/




/*function a() {
    
    function b() {
        //var i;
        console.log(i);
    }
    
    var i = 10;
    console.log(i);
    b();
}

var i = 20;
console.log(i);
a();*/


/*function b() {
    //var i;
    console.log(i);
}

function a() {
    var i = 10;
    console.log(i);
    b();
}

var i = 20;
console.log(i);
a();*/

/*
var a = "hello";
function b() {
    
    console.log('hello jeetu');
    
}

b();
console.log(a);
*/

/*var a;

if (a === undefined) {
     console.log(a);
} else {
    console.log("defined");
}*/