Introduction
My last post triggered quite a few questions. I had no idea so many people read my blog. The emails mostly asked "Why Node?" or "What's good about Node?".In my previous blog I expressed my frustration learning Node and how I overcame my issues. Perhaps it's time to explain why I'm using Node even when I had every possible option of technologies for the project I just started. So… here I'll try to explain why I use Node and what I like about Node.
NOT an advantage: Wide Recruiting Base
Before I start, I'll make address what I think is a common misconception. I've seen this argument made in multiple places and I could not disagree with it more. The argument for node goes something like this:Node is great because we can use JavaScript. JavaScript is an easy language that is widely spread and recruiting will become much easier.If this is your reason to go to Node, I suggest you don't!
Here is my opinion. If you are going to transition your organization to Node, you probably need better programmers than you currently have. It is correct that you can find lots of resumes with JavaScript on them, but making a widget pop-up with JavaScript, validating fields in a form, that is the typical tasks of a client-side JavaScript programmer is quite different from maintaining a set of complex backend services.
If you are planning to use Node to build backend services, get the best programmers you can find. For large scale project, Node (and JavaScript) requires more professional engineering discipline. The freedom of Node/JavaScript invites clever and brilliant solutions, but at the same time opens up Pandora's Box for the worst possible engineering practices. Believe me, I've seen them...
If I were interviewing someone to work on my Node server, I would ensure that:
- They have a solid Computer Science background
- They fully understand the asynchronous model
- They are fluent in Test-Driven-Development and embrace it
Advantage 1: Code easily be migrated to and from the browser/server
Apart from using the Google Web Toolkit, there is really no way you can avoid JavaScript in a modern web application (and even if you use GWT, you most often end up doing a fair amount of JavaScript). If you have adapted the Web 2.0 paradigm, you would want to distribute some of the computing to the client side.Using Node, the algorithms and data structures that you build can in theory run both on the client and on the server. With a bit of cleverness (remember, the best programmers) you can make modules context free and play around with where they run.
Using this advantage I can easily optimize performance by taking advantage of all the browsers accessing my application and fine-tune it as I go. This often significantly outweighs the performance disadvantage running my server algorithms through an interpreter.
Advantage 2: Consistently asynchronous
Everything is Node is asynchronous. This is quite powerful as it allows a single incoming request to (conceptually) fork off a set of threads at the same time. In most other environments you program every incoming request serially. This is a performance issue in many cases where the one may easily parallelize the algorithms. Since JavaScript has closures as first-class-citizens of the language, programming asynchronously is straight forward.I have to say that most node applications, when complex, can be rather cumbersome to read because all of its nested functions, but I found the async.js library to pretty much eliminate this issue (this library is well worth a few hours of study). With async, the parallelism is clear to see and the code reads like prose...
Advantage 3: It breaks down the boundaries between front- and back-end development
Because both the client-side and server-side development is using the same language, we don't have to separate the front-end developers and back-end developers. This means that a developer (or two developers if you use pair-programming) can work on a feature from start-to-end. This eliminates the waiting for front/back-end to finish. This is of course because we have no impedance mismatch between the two environments (you may still have some that prefer to work on U/I's and there is of course some advantage to have experts here… You still have to deal with browser idiosyncrasies, CSS, HTML, etc., but the boundary can be quite fluid).This simplifies project management and helps spread knowledge.
Advantage 4: [Context specific] JavaScript is a dynamic language
For many problems, the ability to load code and execute it dynamically can significantly simplify the design. You can store code/functions as first-class objects and execute them upon need. You may even have customer produce JavaScript to be executed by your engine (this of course opens up security holes, but the option is there).A few things I used to fear, but no longer do
Java has a tremendous number of open source components that you can learn download, learn and integrate. Node.js is rather fresh, so there are not that many components out there. Node is a newcomer and it doesn't have nearly as many battle tested components. This is of course an issue, but I've stop fearing it. It seems the Node community is very vibrant and productive. You can almost always find a suitable component for whatever domain independent problem that these open source components typically solve.I had little hope that Node would be able to handle cross-open-source-dependencies (Maven does an excellent job of this for Java). However, it seems to me that the package manager of Node (npm) is almost as good (and sometimes better).
Hello Peter,
ReplyDeleteGreat post. Can you comment on node's utility for using code between platforms (web, iOS, android)? I am starting a company and my front runner for CTO is great with Java and .net, but not a fan of JavaScript (we have worked on multiple projects together). I want one code and we will be heavily mobile focused. I want one code base so we can focus on quality (and it might price more cost effective too). I have reads that LinkedIn is doing all its mobile development in node. Can you comment (as a Java guy) on why node for front end and mobile development? Does my logic of one code base make sense or am I missing something that is obvious to a techie? Thanks!
Hi Mike,
ReplyDeleteTo answer your question properly, I would probably have to write another blog post, but here is some quick answers.
When you build some software for the web, there is no practical way to avoid JavaScript today. As more and more of the processing can be pushed to your customer's browser (either for performance or usability reasons), JavaScript is the best alternative (one may argue the only alternative unless you count Flash and GWT).
You may of course have programmers that are fluent on both JavaScript and whatever backend language you choose or you may hire developers that focus primarily on the front end and others that focus on the back end. I've seen both strategies work well. They do have obvious disadvantages as you either have two teams working on the same problem or recruit from a smaller pool.
With respect to the reuse of code between platforms, the Node.js option provides some advantages. It is possible to reuse code between a browser and the backend, however, you were asking specifically about iOS and Android. If you are providing native applications, the two platforms prefer ObjectiveC and Java respectively, hence, reuse is difficult. However, if you can get away with a PhoneGap solution, yes, there are some great potentials for reuse.
The popularity of Node.js is only caused by the potential for reuse . There are other reasons:
- Asynchronous model. One can achieve this Java/Scala also using the Akka or Vert.x framework also, but it is not as common
- Startup cost. It is ridiculously easy to get started with Node.js. A few steps and you have a running web server. This is not necessarily representative for how projects continue as they get more complex.
To your friends scepticism to JavaScript... I used to be a sceptic and to some extent I'm still a sceptic (for reasons mentioned in my blog), however, there is no way around JavaScript. It's here to stay and if it is the best match for the problem I'm trying to solve, I'll use it!
-- Petter
Node js frameworks a JavaScript runtime. It is actually behaves as a wrapper around the V8 JavaScript runtime. Many people use the JavaScript programming languages extensively for programming the interfaces of Web sites.
ReplyDeleteUnfortunately, node and its malevolent offspring npm (node package manager) have been taken onboard to the detriment of sensible software development. Even without the yeoman generator on top, node/npm install very many thousands of files.
ReplyDeleteAs an example, loading a basic demo app for angular 2.0 which used npm to install various support libraries, led to 31,000 files! (Actually, ls -R | wc -l showed 31,059, but that included counting each subdirectory as a file). The application itself - plus a sibling simplified application - gave a count off 119, including its subdirectory entries.
There is no way any responsible developer should be happy with this. How many developers could tell you what even a fraction of these were needed for? Could you understand and explain what a thousand support files were needed for when developing an application? Let alone 30,000.