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.