Artificial Intelligence

A Better Javascript

I've been thinking a lot about Javascript over the last few months. And not the "How can we architect a better system" type of thinking, but the "Why do people still write this?" type thinking. I guess it only recently occurred to me, it seems crazy that people still think it's a good idea.


Filed under:

This article is one of Metal Toad's Top 10 React JS Tips. Enjoy!

So...Javascript...

I've been thinking a lot about Javascript over the last few months. And not the "How can we architect a better system" type of thinking, but the "Why do people still write this?" type thinking. I guess it only recently occurred to me, it seems crazy that people still think it's a good idea. Javascript is the car you bought in 1992 that you drove until 2015, each week a new part breaking, each week a new "solution" to that broken part being glued on.  It's 2016, I think I've finally come to terms with the idea that it's time to upgrade.

But Browsers!

Yes. Browsers still use Javascript for tons of fun stuff. Yes, tons of programmers all over the world also are employed to write code specifically for the browser. Thing is, it's not 1992 anymore, there is a wide array of compile-to-js languages that allow you to both A.) Write better, more stable code, and B.) Work in the browser. The cool part is that many of them even out perform Javascript/JS libraries when compiled down. So again, why are we still writing this?

Elm

Elm is my personal favorite JS alternative, as it is a statically typed, functional compile-to-js language that is Blazing Fast. The compiler is written in Haskell, the language itself is very similar to Haskell, and the rendering implementation is similar to that of React/Angular2. Elm has a pretty decent following, and is often praised for its robust troubleshoot-ability. An example here.

Elm is great. I think Elm will be the tool I use to build my next personal application.

A quick code snippet to better understand what we're dealing with:

import Html exposing (Html, button, div, text)
import Html.App as App
import Html.Events exposing (onClick)
 
main =
  App.beginnerProgram { model = 0, view = view, update = update }
 
type Msg = Increment | Decrement
 
update msg model =
  case msg of
    Increment ->
      model + 1
 
    Decrement ->
      model - 1
 
view model =
  div []
    [ button [ onClick Decrement ] [ text "-" ]
    , div [] [ text (toString model) ]
    , button [ onClick Increment ] [ text "+" ]
    ]

Clojurescript

Do you love Clojure? Well why not write Clojure in the browser! Clojurescript is yet another compile-to-js language that enables a user to write non-js for the browser. For those that don't know, Clojure is a Lisp-like language that runs on he JVM. Clojurescript is meant to be a direct port of Clojure for the browser, so most of the Clojure features loved by Clojure developers are available. A Clojure example:

(defn say-hello
         [name]
         (println (str "Hello, " name)))
 
(say-hello "kim")

In Clojurescript there is a need for UI-Rendering libraries (as unlike Elm, Clojurescript does not ship with a concept of browser DOM), but a couple really smart people built a couple really smart libraries to solve those problems. David Nolen (now working at Cognitect, the developers and maintainers of Clojure) developed a library called Om, which is "a Clojurescript interface for Facebook's React." It can be found here.

Lastly, we have

Scala.js

Scala.js is yet another compile-to-js tool that compiles Scala code down to Javascript. Scala, like Clojure, runs of the JVM and, like Elm, is a statically typed, compiled, functional language. Scala.js offers type-safety and better-than-js compile-time error checking. Scala.js also has a React-to-Scala port in the works, which can be found here.

Your code sample:

package tutorial.webapp
 
import scala.scalajs.js.JSApp
 
object TutorialApp extends JSApp {
  def main(): Unit = {
    println("Hello world!")
  }
}

So please, consider alternatives

Javascript isn't the best. Even ES6 has its problems. In 2016 we do not have to be tied to a language as finicky and error-prone as Javascript. The conversation seems to have moved away from "what language will allow us to deliver the safest, most efficient, easiest to write software," and into "But I like javascript! I don't like other languages because they're not javascript." I think that this thought process is losing the forest through the trees. Elm, Clojurescript and Scala all provide better alternatives that solve the same problem as javascript (browser stuff), but with safer, more efficient development means.

Learn more about JavaScript in our JavaScript Blog Archive.

Similar posts

Get notified on new marketing insights

Be the first to know about new B2B SaaS Marketing insights to build or refine your marketing function with the tools and knowledge of today’s industry.