JavaScript & Java; Object-Orientated vs. Class-Orientated

A software developer can sometimes choose the language they use to program with, but often the decision is already made. When developing software using a language a developer is not familiar with they can go one of two ways, either learn the new language with its accompanying paradigms and idioms or seek to leverage their current programming style into the new language.

In many cases the transition between them is not especially difficult, Java and C# are reasonably close cousins for example, and a competent programmer can switch between them without much cognitive dissonance. Sometimes the distance between the two paradigms is substantial.

When I think of the conceptual differences between languages I am often reminded of Java and JavaScript (recently christened the “worst invention ever“). Owing to its name and curly braces style JavaScript is often though of as very similar to Java, at least by people who’ve never programmed seriously with it. When these developers do begin using JavaScript for serious work they soon stumble into the differences. We then come back to the learn or adapt choice.

“Leveraging their current programming style”, the word “Leveraging” comes from the word “Lever” which can be described as a bar or rod used in combination with a pivot point to transmit force. The goal of a software development is what’s at the other end of the lever. How we choose to apply the force, or our time and mental energy, to reach out goals is my area of interest. Leveraging our existing knowledge to achieve our goals is often a good idea, we learn a lot on the way to becoming the kind of developers we are after all. Discarding that and starting from scratch on each development is not a pragmatic approach. Another aspect of pragmatism however is recognising when our existing experience and skills are not currently a good match for the current problem domain. When our skills are a bad fit we can go through a lot of motion to “force” the fit. An example of this is when people try to make JavaScript into a class orientated programming language (aka a conventional OO language).

Coming from a background in Java, I’ve spent many hours trying to figure out how to make JavaScript objects act more like Java objects in every way. – Nicholas C. Zakas

Should we really be spending many hours trying to work against a language? While I am sure Nicholas is an accomplished developer I don’t believe his approach to JavaScript is the most effective. Concentrating your efforts on overcoming the perceived weaknesses of a language by coercing it to act like another language is not a productive use of time. Seeking to more fully understand that language and its underlying paradigms is a more productive use.

“Embracing the new style”, this alternate approach is also about leverage, however what we are leveraging here is not our current capabilities, but our capacity to improve our current capabilities. As well as the goal of achieving our development target we should also be focused on achieving our longer term targets. One of my longer term targets is to improve my software development skills. One way to do this is to continually improve our current knowledge, reinforce it with experience and expand it with learning and experimentation.

Knowing other systems and paradigms expands the world inside your brain. – Yukihiro Matsumoto.

As with all decisions we make there are tradeoffs, when considering the lever we choose to apply we need to weigh up the effort required to achieve the desired result. Remember there are always external constraints, such as deadlines for example! Remember though that the greater up front effort often has a better payoff in the end. In Steven R Covey speak “Sharpening the Saw” may have more overhead initially but subsequent productivity is increased. As far as JavaScript is concerned cast off any misplaced prejudices and come toappreciate that most misunderstood of languages called JavaScript, be prepared for an interesting world of closures and prototypical objects, and oh yeah you can use a string in a switch statement!

Some related reading:

  1. Emphasising the Role of the “Object” in Object Orientated Design.
  2. Evolution of a Haskell Programmer