Posts

Showing posts from 2015

Looping & Closure

Image
Here is simple code to dynamically create set of buttons that greet in different languages. document.body.onload = createButtons; function createButtons() { var greetings = [ {lang:'English', msg:'Good Morning !!'}, {lang:'German', msg:'Guten Morgen !!'}, {lang:'French', msg:'Bonjour !!'}, {lang:'Finnish', msg:'Huomenta !!'} ]; document.createElement('h1'); console.log(greetings); for(var i=0;i<greetings.length; i++){ var greet = greetings[i] var input = document.createElement("button"); input.appendChild(document.createTextNode(greet.lang)); input.setAttribute('class','btn btn-default btn-lg'); input.onclick = function(event){ alert(greet.msg); }; document.body.appendChild(input); } } Pretty simple, looping through array of objects and adding buttons with lan

NPE as well as Null check free code ... Really ?

Image
NPE as well as Null check free code … Really ? Is this how your Java code review start ? NPE is always nightmare for a Java developer. Lets not discussion to much and jump on an usual Java code snippet public interface Service { public boolean switchOn(int timmer); public boolean switchOff(int timmer); //Other controls } public class RefrigeratorService implements Service { // ... } public class HomeServices { private static final int NOW = 0; private static HomeServices service; public static HomeServices get() { //Null Check #1 if(service == null) { service = new HomeServices(); } return service; } public Service getRefrigertorControl() { return new RefrigeratorService(); } public static void main(String[] args) { /* Get Home Services handle */ HomeServices homeServices = HomeServices.get(); //Null Check #2 if(homeServices != null) { Serv

Java doesn't have "Diamond" problem : Really ..?

Java doesn’t have “Diamond” problem : Really ..? We have been confidently telling that Java doesn’t have diamond problem even if it support multiple inheritance. In object-oriented programming languages with multiple inheritance and knowledge organization, the diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. But do you that the diamond problem is now there in newly introduced Java 8. It is because of the introduction of default methods in Interface. In Java8 Interfaces not only have method declaration but their default implementation. Any class implementing can either use default implementation from interface or provide its own implementation. Since Java class can implement multiple interfaces it is possible that two interfaces has same default methods, that brings diamond problem. How do we overcome diamond problem ? Consider following code snippet public interface InterfaceA {

Overloaded function in Javascript

Coming from Java background, it is happened to be searched java language constructs while coding javascript. One of such construct is overloaded methods/function. Take a look following code snippet   //Normal sum var intergerSum = sum ( 10 , 20 ); //Expecting item price sum var objectSum = sum ( {item_id: '01' ,name: 'abc' , price: 100 }, {item_id: '02' ,name: 'xyz' , price: 200 } ); As per above code snippet, implementation of ‘sum’ has been abstracted from the user. In the world of type safe language like Java, it is very straight forward, but in case of javascript we need to have our custom implementation. Lets see how can we achieve above in javascript. Lets start with defining a constructor function which introduce a common function to all our custom object so that we can extract integer value for summation. //constructor function to introduce new function to custom object function item (options) { var instance = Object

Do you know Javascript … Try these puzzle

Do you know Javascript … Try these puzzle Javascript is one the most miss-understood language among developers. Everyday I learn new lesson about it.While going through few articles I thought of following JS language puzzles. Handling Array var days = [ 'Monday' , 'Tuesday' , 'Wednesday' , 'Thursday' , 'Friday' , 'Saturday' ]; var length = days. length ; console. log ( 'No of days are ' + days. length ); //Opps we missed Sunday, lets add it days[++ length ] = 'Sunday' ; console. log ( 'Final no of days are ' + days. length ); It is obvious that no of days are 7…. is it ? Written with StackEdit . StackEdit is a full-featured, open-source Markdown editor based on PageDown, the Markdown library used by Stack Overflow and the other Stack Exchange sites. ↩ Here is the text of the footnote . ↩