Which language is better: Java or JavaScript?

In one of my blogs a person asked me, “Can you teach a person to be a programmer within 6 months?”  I answered, “I can make a programmer out of any person within two weeks, but there is a chance that he’ll be asking questions like this: http://m.hotpot.hk/story.php?id=15689”.

I shared the above link with our software developers in the Skype chat. Some people laughed. One person responded with a popular link to a presentation that makes fun of JavaScript:

Another guy responded with this question:

var a=0.1
a=a+a+a
(a - 0.3== 0)  // false or true ?

 
After years on Wall Street, this was an easy one, “Of course, false!” Floating numbers precision makes the results unpredictable. We use BigDecimal. I’ve created a little fiddle for you. Just follow this link and press Run to see for yourself: http://jsfiddle.net/4nwdv/

For those who after running this fiddle say “WTF!”, here are the some details – I ran the same code in JavaScript console in Chrome Developers Tool:

addition

So Google Chrome’s Java Script engine truly believes that
0.1 + 0.1 + 0.1 = 0.30000000000000004

Maybe if you’ll run it in Firefox, the result will be different? Nope, I ran it in Firebug’s JavaScript console, which confirmed, that 0.1 + 0.1 + 0.1 = 0.30000000000000004.

addingff

By now, only the person who forgot to take his morning pill wouldn’t agree that this is a language problem and JavaScript is bad. What’s good then? This is another easy question: “Java and only Java!” Most of the Wall Street applications are written in Java and do the number crunching real well! Let’s see what will be the result of the same arithmetics in Java. I wrote this little program, ran it in the debugger and put a breakpoint right after the variable got the new value. Man, the result is the same as in JavaScript!

addingjava1_1

Just to complete the program I pressed the green button Resume to see the result of a-0.3 on the console. Well, it’s not exactly what I was expected to see, but pretty damn close, isn’t it?

addingjava2_3

This little experiment shows that the demand in software developers will only be increasing, because while regular Joe believes that (0.1 + 0.1 + 0.1) – 0.3 = 0, the savvy software developer would not be so sure cause it depends…

I’d appreciate if you’d run the same tests in other programming languages and share your findings. Together we can make the world a better (or at least more definitive) place!

Four chapters submitted to O’Reilly

We have submitted the drafts of four chapters of the book “Enterprise Web Development: from Desktop to Mobile” to O’Reilly for review and editing. This is a pretty hands-on book as we are developing an application Save Sick Child while explaining various techniques and technologies. The readers will get a new independent working application every time we add a new piece of functionality (e.g. login, donation form, video, Google maps, charts, auction, etc.). These projects will be published on the dedicated Web site savesickchild.org. For example, this is a sample application illustrating modularization of the JavaScript-based UI.

ssc15

The following chapters are submitted to the publisher, but the raw texts are available for you to read now on the github at https://github.com/Farata/EnterpriseWebBook:

Chapter 2. Advanced Intro to JavaScript
Chapter 3. Mocking up Save Sick Child
Chapter 4. Using AJAX and JSON
Chapter 9. Replacing HTTP with WebSockets

Currently we are working on the chapters on responsive design, test-driven development and modularization of the JavaScript applications. We’re making these unpolished chapters available for you to read hoping to get valuable technical feedback from you. Please do let us know if you see some bugs or inefficient solutions in the code samples. We really really appreciate it.

Talking to Publisher About New Book

I’ve contacted a publisher regarding possible publication of our new book. We’ve been working with these publishers in the past and they seem to be interested, which means the publisher started asking certain questions to see if such a book will have a market value. This blog is our response to these questions.

The question was caused by the proposed book title: “Enterprise HTML5: Desktop and Mobile”. What the word enterprise means in this title and is this book about HTML5? The title of the book can be changed to “Enterprise Web Development. From Desktop to Mobile”. IMO, HTML5 became a buzzword meaning a lot more than HTML. This book is more about Web development than HTML. The targeted audience is intermediate developers and the knowledge of HTML basics is assumed.

This book is mostly about developing for desktop and mobile in JavaScript and related technologies. Many enterprise development managers are facing (or will be soon) the need to make their applications or Web sites available on the plethora of devices used for accessing Internet. They need some kind of a roadmap for going mobile. There are different approaches for doing this. One approach is to create separate teams developing native applications for popular operational environments (e.g. iOS, Android, etc.) This approach can produce good results as long as they can afford creating and maintaining several versions of the same application. Another approach is to create an HTML5 application, which is will use modern versions of HTML, JavaScript, and CSS. This approach has different flavors though. One is to have the same codebase for both mobile and desktop version (this approach is also known as Responsive Design). Another – have two versions of the HTML5 code – one for desktops and the other for mobile devices. If you go with the latter approach, the next question arises – which JavaScript framework to use that will allow reusing as much of the code as possible and to minimize the learning curve. This book will allow you to compare JQuery and Ext JS – popular JavaScript frameworks for desktop development, and their mobile versions: JQuery Mobile and Sencha Touch. Is this an intermediate to advanced book? Which other books will compete with this one?

So let’s profile the books that are currently available on the Web market.

Most of the available books on the HTML/JavaScript market can be divided by three categories:

1. Tutorials on HTML, JavaScript, CSS or particular JavaScript frameworks. For example, Larry Ullman’s “Modern JavaScript. Develop and Design” or “JavaScript 24-hour trainer” are good JavaScript tutorials.

2. Cookbooks or design patterns. These books show code fragments helping to accomplish certain isolated tasks. O’Reilly’s “HTML5 Cookbook” or “JavaScript Cookbook” are good representatives in this category.

3. Advanced books. These cover advanced topics of a programming language (e.g. “Secrets of the JavaScript Ninja” by John Resig) or explain a certain aspect of development, for example “High Performance JavaScript” by Nicholas Zakas.

These are definitely useful books. But our book won’t perfectly fit into any of these categories cause it’ll have a little bit of everything. We’ll structure the materials around designing, developing, and re-developing a sample Web site using HTML, JavaScript, CSS, and popular frameworks. While working on this Web site the reader will learn the language/tools and compare different approaches to cross-platform development. Of course, if you didn’t work with JavaScript before, a chapter on JavaScript will be quite useful for you. But it’s an advanced introduction to JavaScript – we assume that you know what the loops are about and won’t give you a coverage of all types of loops in JavaScript. If you know JavaScript already – just skip this chapter.

On the other hand, such advanced concepts as callbacks or closures deserve better coverage, so we’ll spend some time explaining them. While developing a mobile version of the sample Web site in jQuery Mobile (and then in Sencha Touch), we’ll explain a bare minimum of this framework – just so you can complete this Web application. We are also planning to compare these JavaScript frameworks and highlight the issues that will allow larger Web sites to be written in an efficient and modular fashion.

So we assume that a reader of our new book has some background in programming, but it won’t be overly complicated to be included in the category of advanced books. I’d say that we’d like to write a practical book that will cover the entire cycle of the Web application development using the modern tools and techniques. This book will be written by software engineers from Farata Systems all of which work on real world Web projects.