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 .