Sunday 10 November 2013

Web Storage

Web storage is the simplest form of data storage. Data storage is a way to store data on user's hard drive. There are some non critical data such as user's preferences, recent searches etc. that do not need to be sent to the server or received from the server at every request. These non critical data can be stored at client side to boost the performance of the web site. There are three ways to store data on the client side: Web Storage, Web SQL Database and IndexedDB. In this post we will talk about Web storage.

Web Storage

In web storage a key is associated with a value. Though it looks like cookies, but it is more secure and faster than cookies. Unlike cookies, it allows to define a sessionStorage that only refers to the particular window/tab the user is currently in. Once window/tab is closed, the data disappears. Data doesn't stick around until the entire browser is closed.

There are two forms of Web storage: sessionStorage and localStorage.

When data is created in sessionStorage, it is available only to that window until the window is closed (or session is ended). So when you open another window on the same domain, previous session data will not be available. This way leak of session data is avoided across different windows. Let's see some examples of sessionStorage:

 sessionStorage.itemSelected = 'Game';
 alert("You have selected : "+ sessionStorage.itemSelected);

You can set data in sessionStorage using setItem method. The setItem method takes a key and a value.

 sessionStorage.setItem('itemSelected', 'Reading');
 alert("You have selected : "+ sessionStorage.itemSelected);

Data can be retrieved from sessionStorage using getItem method. The getItem method takes the key and returns the value related to that key.

sessionStorage.setItem('itemSelected', 'Reading');
alert(sessionStorage.getItem(itemSelected));

You can remove data from sessionStorage using removeItem, clear method or directly using deleter. The removeItem method takes the key and deletes the entry, where as clear removes all entries.

sessionStorage.removeItem('itemSelected');
sessionStorage.clear();

When data is put in localStorage, it becomes available on any other window on the same domain. Data in localStorage remains available until it is explicitly deleted either by the web application or by the user. You can get back your saved data in localStorage even if you close the browser or reboot your machine. This way you can persist data without the need of cookies. localStorage has the exact same API like sessionStorage. So if you want to keep data in localStorage, you can use it this way:

localStorage.setItem('itemSelected', 'Reading');
alert(localStorage.getItem(itemSelected));

Web storage is supported on all modern browsers. You can see it here. API signatures are simple. Web storage typically has a limit of 5 MB. If data size goes beyond 5 MB, browsers generally ask the users whether they want to permit the website to go beyond this limit. Another thing to remember when using Web storage, it only supports string-to-string mapping at present. You can use JSON.stringify and JSON.parse to overcome this difficulty.

Sunday 27 October 2013

Exciting features of Java 8

Java 8 is scheduled to be released in Q1 next year. It looks really promising with many exciting features such as uniform platform, Lambda, enhancement of core libraries with Lambda, new Date and Time API, compact profiles, Nashorn JavaScript Engine and many more. These new features in Java will bring a paradigm shift by providing many great possibilities of writing good, concise and elegant code.

In Java 8 we are going to see the platform unification in terms of code portability, commonality of APIs, common tooling. Nandini Ramani, VP of Java Development of Java Platform for Oracle in her keynote speech in JavaOne Strategy Keynote 9-22-2013 mentioned that Java 8 will focus on bringing together different implementations of Java under one platform. Language features of Java SE 8 and Java ME 8 will be similar. Peter Utzschneider, VP of Java Product Management for Oracle also mentioned in that talk that there will one type of Java Developer in future. Beyond Java 8, Java SE will be shrink enough to work on embedded and mobile devices.

Lambda is the another most exciting feature of Java 8, bringing closures to Java. As per Mark Reinhold, Chief Architect of the Java Platform Group:

Lambda is the single largest upgrade to the programming model. Ever. It's larger even than Generics. It's the first time since the beginning of Java that we've done a carefully coordinated co-evolution of the virtual machine, the language and the libraries, all together. Yet the result still feels like Java.

Lambda will reduce lot of boilerplate code, make the code concise and easy to understand. Core libraries of Java are enhanced by Lambda.

There will be new date/time API in Java 8. All the classes in java.time package are immutable and thread-safe. The date and time types include Instant, LocalDate, LocalTime, LocalDateTime and ZonedDateTime. There are also the Duration and Period types and additional value types include Month, DayOfWeek, Year, Month, YearMonth, MonthDay, OffsetTime and OffsetDateTime.

Compact profile of Java 8 will allow applications to use a subset of Java SE to run on resource-constrained devices. For example: an application that does not use the Swing/AWT/2D graphics stack can achieve considerable space savings by running on top of a Profile that does not include those APIs. Compact profile replaces CDC.

Nashorn is the new, lightweight, high-performance implementation of JavaScript integrated into the JDK. Nashorn is the successor to Rhino, with improved performance and memory usage. Nashorn will be made available to Java applications via the existing javax.script API. It will define a new command-line tool, jjs.

There are many other features, bug fixes and performance improvement in Java 8. Features list of Java 8 is available here. Java 8 Developer Preview is released and can be downloadable from here.

Sunday 20 October 2013

Introduction to Real Time Web

Wiki definition of Real Time Web is:

The real-time web is a set of technologies and practices that enable users to receive information as soon as it is published by its authors, rather than requiring that they or their software check a source periodically for updates.

The real-time web is really cool and all the massive social platforms like Facebook, Twitter and Google+ are using real-time web technologies for their instant notifications and interactive experiences. In this blog post I will give an introduction to two technologies that can be used to add real-time aspect to your web application: WebSockets and Server-Sent Events.
WebSocket
WebSocket is used to create persistent connection between the client and the server and both parties can use this connection to start sending and receiving data at any time. It is a protocol that provides full-duplex communication channels over a single TCP connection.
The connection you create using WebSocket is real time and is permanently open until you close it explicitly. When you open a socket, the server can push data to these connected sockets. WebSockets help to you achieve low latency. Since the socket is always open and listening, data will arrive to your browser as soon as data is pushed from the server.
WebSocket API is simple and easy to work with. This API contains methods for creating the connection, sending data, receiving data, and closing the socket. It also has an error handler and a state flag. This flag tells the client about the state of the socket whether it is connecting, open, closing, or closed.
This is how you can create a new WebSocket:
var socket = new WebSocket('ws://theserver.com/mynews:8080');
Now send some data to the server when it is opened:
socket.onopen = function () {
  socket.send('Hello Server'); 
};
Receive data from the server:
socket.onmessage = function (e) {
  console.log('Server: ' + e.data);
};
Logging error:
socket.onerror = function (error) {
  console.log('WebSocket Error ' + error);
};
Server-Sent Events
Sometimes you need simple push-based data from the server. Server-Sent Events are just for this. This enables one-way information flow from your server to the browser. EventSource object is used to manage Server-Sent Events. The browser immediately tries to establish a connection when you create a EventSource object, passing it a URL to connect to.
var es = new EventSource('/mydata');
Now setup handler for message, open and error event:
es.addEventListener('message', function(e) {
  console.log(e.data);
}, false);

es.addEventListener('open', function(e) {
  // Connection was opened.
}, false);

es.addEventListener('error', function(e) {
  if (e.readyState == EventSource.CLOSED) {
    // Connection was closed.
  }
}, false);
Use cases for Server-Sent Events are: Sending notification to browser, social media feed updating, live price updating, stock ticker streaming etc.

Saturday 12 October 2013

Characteristics of clean code

Writing clean code matters because it not only makes your programming life much easier, also help others to understand it when you are not available. Think about a nice song you hear recently. Think about the happiness while listening that song. Reading clean code is like this. It will fill you with joy because the author of this code has taken lot of care in writing it. He not only thinks about the computer that executes it, but also thinks about the future reader of his code. He has made his intention clear in his code. Martin Fowler has rightly said about clean code in his refactoring article : "Any damn fool can write code that a computer can understand, the trick is to write code that humans can understand."

So what are the characteristics of clean code?

Clean code is simple. It should be easy to read. Anyone can understand what the code does. It has a story to tell. It is not ambiguous. It is easy to maintain and enhance.

Clean code does one thing and does it well. Trying to do too many things is a sign of bad code. It makes your code messy. Clean code does not do this.

Clean code does not have duplication. Duplication increases the chance of introducing bugs in your code. When you have duplicated code and you change one thing then you need to change others as well. There is strong possibility you may forget to do this. Focus on reusability and remember DRY (Don't Repeat Yourself) principle: "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system."

Clean code must have a good set of tests. Code without unit and acceptance test is unclean. So write your tests. Tests are your live documentation. Care must be taken in writing tests.

These are the main characteristics of clean code. But there are many other attributes that make your code clean such as consistent and meaningful naming, good error handling, completeness as per requirement etc.

Bad code increases time to complete your product. Every change in your bad code breaks many other parts of the code. Productivity of the team continues to decrease due to bad code. It hurts your nerve and project is bound to fail. So try to avoid writing bad code and write clean code.

Saturday 5 October 2013

New Courses

We have recently launched three courses at Rainbow. These courses focus on computational thinking and programming. In our earlier blog we have outlined why computational thinking is important. Computational thinking helps us to understand the problems better. We also believe that learning programming is fun and helps us to bring our ideas into reality. Our industry experts at Rainbow have developed and deliver these courses to make you understand the programming topics in an easy and engaging manner.

Programming Day

This course is designed to introduce you to the fundamentals of computer programming. In this course you will learn how computer works, how to analyze, what code is all about, how to code and how learning to code will help you now and also in future.

Computing for School Teachers

The aim of this course is to provide guidance and practical advice and support current ICT transformation to align with the new Computer Science programme published in September 2013 for teaching from September 2014. This course has been designed to increase the effectiveness of teaching computer science in the classroom. At the end of this course you will have knowledge of computational thinking and programming.

Java Courses

Java course is designed to give an introduction to the Java programming language and object oriented programming (OOP) concepts. After completing these courses, students will have necessary skills for analyzing, designing, and developing Java applications.

All these courses include both theory and practice session. For more information about these courses and book your place please visit our website http://www.rainbowsolution.co.uk .

Sunday 29 September 2013

What is Computational Thinking and why is it important?

Jeannette M. Wing used the term "computational thinking" in an ACM Communication article to present her vision to the computer science community. In this article, she mentioned that anyone can benefit from computational thinking and  she also suggested that computation thinking should be a fundamental skill for everyone, not just for computer scientists.
So what is computational thinking?
"Computational thinking is the thought processes involved in formulating problems and their solutions so that the solutions are represented in a form that can be effectively carried out by an information-processing agent." - Cuny, Snyder, Wing

Computational thinking involves a set of problem-solving skills and techniques that includes:
  • Collecting, logically organizing and analyzing data
  • Representing data through abstractions (such as models and simulations)
  • Problem decomposition (breaking down tasks into smaller manageable parts)
  • Automating solutions through algorithmic thinking (series of ordered steps taken to solve a problem)
  • Identifying, analyzing, and implementing possible solutions in order to achieve the most efficient and effective combination of steps and resources
It also includes pattern recognition (the ability to notice similarities or common differences that will help us to make predictions or lead us to shortcuts); generalizing (the ability to represent an idea or a process in general terms) and transferring this process to other problem domains
Computational thinking is applicable to any subject such as biology, medicine, economics, finance, archaeology, journalism, law, social science, arts. Computational thinking influences research in nearly all disciplines. It brings together different disciplines under one platform. For example, in Human-Computer interaction ,  researchers from computer science, designs and psychology working together to understand how human can interact with computer effectively, Computational Biology or Bioinformatics involves people from computer science and biology.
Why Computational Thinking is important?
We are in 21st Century. Computer technologies are used in every part of our life. We must understand and learn computational thinking because it changes the way we think. When we will apply computational thinking in our problems, we will understand them better. We will know what strategies are applicable to solve them. With the understanding of computational thinking we will have the ability to evaluate the match between computational tools and techniques and a problem. We will be able to explain problems and solutions in computational terms. We will know how to think abstractly to manage complexity.
References: