Tuesday, August 21, 2012

Custom Objects in Javascript

I have been using Java for a long time, and for quite some time I was doing web development using GWT (Google Web Toolkit). The beauty of it was that I had my Java object oriented constructs and would not worry about how it gets translated to GWT - let Google take care of it. My knowledge of Javascript was sufficient but not to the extent that I could do heavy web development with it. Later I decided that I have to get a deeper and more thorough understanding of Javascript and this has been a real roller coaster - just at a time where I would think I got something, something came and proved me that I was wrong - that I simply misunderstood.
The culprit for me - Javascript's flexibility, or should I say over-flexibility. Bees that have honey in their mouths, have stings in their tails and he who will gather honey must bear the sting of the bees. Javascript's flexibility is like a wild horse - you have to understand it, so you can play with it. What one needs in Javascript is strong disciplined code conventions and constructs. What I needed from Javascript - a set of code conventions and constructs that will mimic the most popular Java OO concepts.
This post is the first of a series of posts to come and it will cover Custom Objects in Javascript.

In an Object Oriented Programming language like Java, an object is an instance of a class. The class defines member variables and methods that operate on that data. Objects represent instances of the class, and a class represents a blueprint of how a new object is created. In Javascript, this is not the case. Javascript is a prototype-based language and there is no class statement. So, how do we mimick the class behaviour popular in object oriented programming languages like Java? How do we instantiate a new object in Javascript? What is the equivalent of the new keyword in Javascript?

Monday, July 23, 2012

Just make a jsfiddle

Very often, web developers find themselves stuck with a script written in javascript or jquery and they need to ask for help. Asking for help like this can be a tedious process considering the fact that your code is a combination of the powerful trio: CSS, HTML, JS. So, instead of zipping your project and sending it over to me, just make a jsfiddle. jsfiddle has editors for HTML, CSS and JS and a result section - being able to see the result is crucial with web development.

Friday, July 6, 2012

Strahler Number of a Binary Tree

This post is going to cover how to find a strahler number of a binary tree. We are going to start by explaining what a strahler number is, the algorithm for finding a strahler number and I will also provide a nice little program to demonstrate this. I provide no source code for the program, but I will be more than happy to assist you with any problems you might encounter writing a program for a strahler number.

1. Strahler Number - Description and Algorithm

The Strahler Number is the branching complexity of a tree. It has been introduced by hydrologists for studying the morphology of the river networks and was later used in computer science regarding some optimization problems. The Strahler number can be defined by the following recursive rule:

Recursive rule for strahler number

The notation that we use is strahler(L, r, R) - L refers to the left subtree, r is the root, and R is the right subtree. The way the algorithm works is that all leaf nodes are assigned a strahler number of 1. If a node has only one child, it is assigned the strahler number of its child. This is also defined by the recursion above which checks whether strahler(L) == strahler(R).

Thursday, July 14, 2011

Generation of Multitaskers

People today have loads of sources to choose from when it comes to entertainment: thousands of TV channels, weekly shows, high-speed Internet, social networks like Facebook and Myspace etc. These sources have become very easily available and with the latest technological improvements we can give ourselves some entertainment while doing other tasks: listening to music while running, browsing the Internet while attending class, watching movies while doing our homework (hope you are not doing this one, but you can) and many other multitasking activities. We have become so “connected” to the technological world, that sometimes we feel even more disconnected. People are becoming very effective at multitasking and we are becoming a generation of multi-taskers suffering from attention deficit disorder, thinking that multitasking is the only way to cope with the increasing demand of tasks to be finished every day. But do we really finish more tasks while multitasking ? Latest research has shown that it takes about 50 percent more time to complete two tasks when they are performed in a multitasking manner than if those two tasks were done separately, one task at a time. It seems to be that the major reason people give in support of multitasking is not really true. Another study has shown that today’s workers do not spend more than three minutes of their attention on a single task before letting their attention escape to other matters.

The multitasking state of mind is really dominant in younger generations and it happens to have serious consequences on students’ performance in their academic endeavors. As Simpson says in her article "Multitasking state of mind", “students indeed have little tolerance for anything that does not have the split-second, image-splashing pace of a Spike TV commercial or an Internet pop-up ad. Antsy with mere existence, they are often tuned everywhere but where they actually are.” Students are always on the lookout for sensational stories and worthless irrelevant newspaper items that excite the impulsive and emotional faculties - thus weakening their power of concentration. To be able to focus the mind on the object at hand in a conscious manner leads to concentration. The strong influence of technology and the increased pace of modern life have led to significantly shorter attention spans, decreased power to concentrate and there seems to be a constant craving for information that is “exciting”. 
Where does this desire for this constant input of information come from ? Why do students escape the task at hand by distracting themselves with useless information ? The answer points to another popular problem among students : procrastination. Students find it that it is far more amusing and interesting to spend their time on exciting news than to listen to a long, boring lecture. It gives them a temporary escape from the task at hand. Procrastination is also very handy when the current task is demanding and students want to lessen the pressure and anxiety caused by the immediate task. This however seems to be a recipe for disaster. Distracting themselves and procrastinating, students fall deeper in their addiction to a constant flow of exciting information.

Monday, July 11, 2011

Setting up log4j in your Java Project

For those that are wondering about the need for a logging utility in their project when there are already debuggers, you might want to read the following passage from the book "The Practice of Programming": 
As personal choice, we tend not to use debuggers beyond getting a stack trace or the value of a variable or two. One reason is that it is easy to get lost in details of complicated data structures and control flow; we find stepping through a program less productive than thinking harder and adding output statements and self-checking code at critical places. Clicking over statements takes longer than scanning the output of judiciously-placed displays. It takes less time to decide where to put print statements than to single-step to the critical section of code, even assuming we know where that is. More important, debugging statements stay with the program; debugging sessions are transient.
Log4j is a Java-based logging utility and is a project of the Apache Software Foundation.
To add log4j to your project, first you will have to download it to your lib folder in your project. Make sure to add it to your classpath. If you are using Eclipse, just right click on the jar file in the lib directory, and go to Build Path -> Add to Build Path.
This tutorial is going to cover configuring log4j via a properties file called "log4j.properties". Configuring can also be done with a different technique using XML. 
It is considered a good coding practice to include your log4j.properties file in a "config" directory. 
Another library that we will be using for the purpose of this tutorial is the DatedFileAppender library. 

Tuesday, May 17, 2011

Intelligent Visions

Our fine arts were developed; their types and uses were established, in times very different from the present, by men whose power of action upon things was insignificant in comparison with ours. But the amazing growth of our techniques, the adaptability and precision they have attained, the ideas and habits they are creating, make it a certainty that profound changes are impending in the ancient craft of the Beautiful. In all the arts there is a physical component which can no longer be considered or treated as it used to be, which cannot remain unaffected by our modern knowledge and power. For the last twenty years neither matter nor space nor time has been what it was from time immemorial. We must expect great innovations to transform the entire technique of the arts, thereby affecting artistic invention itself and perhaps even bringing about an amazing change in our very notion of art.
[PAUL VALERY, PIECES SUR L'ART, “Conquest of Ubiquity”]

The history of art is marked with incessant search for excellence in realistic paintings and sculptures. As artists learned about materials, perspective, anatomy, nature, math, geometry, art became not only a means to please but also “pure and faultless workmanship”. This meant that the virtuosos of each generation took more extensive liberties to embrace the latest established technological breakthroughs of their time to perfect their art. In this way, often, new standards were set for the new generations to come. All this resulted with art having new radical movements, set of revolutionary thoughts or so called “isms”: Classicism, Romanticism, Realism, Neo-Romanticism, Cubist-Realism, Futurism, Constructivism, Surrealism, Abstract Expressionism, Minimalism etc . Art cannot accommodate the emergence of new art “isms” due to the fact that it has used up the possibility of introducing new revolutionary developments in terms of structure. This however does not deny the individual creativity of the artist: one can combine the already existing notions of different “isms” and link them in a very intriguing way. But that is not enough to be qualified as an innovative visual idiom.

The visual idioms of traditional art are limited in introducing new ways of representing threedimensional material. Even the most skilfulartists are constrained in terms of depth and precision of delineating complicated differences of complex objects. With the help of computers, which are famous for their precision and speed of executing instructions, art can reach new heights by offering extraordinary precision in directing decisive structural factors, image manipulation, and algorithmic automatic generation of objects. It can also be further developed for different interaction scenarios in forms of software.

Tuesday, March 29, 2011

Adding Code Syntax Highlighting to your Website

Any coder posting source code on a blog, or on his website, needs a proper syntax highlighting tool that will make his code readable. I have been using the HTML <code> tag for some time, but it does nothing more than rendering the text in a fixed width font. So after some time of researching to remove this handicap from my source code containing pages, I came across a way to put some beauty to my code: google-code-prettify. I was googling for a code beautifier, but Google decides to name it a prettifier. Oh, well. Never mind.
Let's give it a try and see if it works.
int answerToLife(String question) { 
  return 42; 
In this post you will find simple steps that will lead you to set up syntax highlighting on your site.


First, go ahead and download the distribution. This will allow you to make changes to the CSS and play with the design if you don't like what you see. Then add the following lines of code before the opening <head> tag to your HTML document.
<link href="prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="prettify.js"></script>