I'm sorry, but JavaScript is ugly.

I'm sorry, but JavaScript is ugly.

·

3 min read

Someone had to say it. I've written JavaScript since 2017. It was the first real programming language I learned to the extent of being able to find a job and work using it.

Around 2019, I decided that I don't ever want to go back to JavaScript. I found Ruby and I love it. I will write JavaScript when I have to, just like I write SQL when I have to, and I use any other tool when I have to.

But I'm not happy to use JavaScript. It doesn't please me.

It turns out that Ruby is just... more enjoyable to write code in. I don't mean it as a dig at JavaScript or its developers; it works, but it's ugly.

Ruby is concise. Beautiful. Elegant.

JavaScript: ugly, ferocious, annoying.

Configuration everywhere. Every app is structured slightly differently. Dependencies are a nightmare to manage.

The best part of JavaScript is TypeScript, though TypeScript is uglier and requires even more configuration and more tooling. I never thought I'd prefer a statically typed language, but JavaScript is easy to beat.

I've taken the liberty of writing some code snippets detailing what I mean. Naturally, production examples would be better

Let's say we want to print out "Hello world" five times in JavaScript:

for (let i = 0; i < 5; i++) {
    console.log("Hello, World!");
}

Not too bad. We have to set up a for loop, create a variable, create a constraint, and an iterator function. Then we can call .log on console and pass in the string. It's fine.

But look how much nicer it is in Ruby:

5.times { puts "Hello, World!" }

Alright. You're not convinced. I get it. We can use a more complicated example.

Let's see how Node and Express might approach something like making an HTTP request and parsing it with JSON, with an error catch:

const express = require('express');
const fetch = require('node-fetch');

const app = express();

app.get('/', async (req, res) => {
    try {
        const response = await fetch('https://api.example.com/data');
        const data = await response.json();
        res.send(data);
    } catch (error) {
        res.status(500).send({ error: 'An error occurred' });
    }
});

app.listen(3000, () => console.log('Server running on port 3000'));

Again. It's fine. It works, even! But for something as simple as making a basic HTTP request, we're writing a significant chunk of code.

Note all the line breaks and {} usage. It becomes difficult for the eyes to track.

And Ruby:

require 'open-uri'

class MyController < ApplicationController
  def index
    begin
      data = JSON.parse(open('https://api.example.com/data').read)
      render json: data
    rescue
      render status: 500, json: { error: 'An error occurred' }
    end
  end
end

That's clean, what can I say? The indentations and line breaks are easy to follow and it doesn't require as much non-text formatting.

I use JavaScript regularly, but Ruby will always take the cake for me. Another piece to come soon highlights those beautiful elements I find.

What do you think? What language speaks to you?

As always, thanks for reading!