Diaries of a non-consensual JavaScript developer, episode 2: my grand scheme to make my code style more readable has failed mildly

From https://gitlab.com/DoctorAjayKumar/sidekick/-/commit/8ac7cd86892882f57f84e5043041a0ed1bd55c95


my grand scheme to make my code style more readable has failed mildly

did you know that javascript is retarded

Idea: transform

async function foo_doo(bar: baz, quux: quuz, buzz: fuzz) : Promise<fuzz>
{
    ...
}

into

async function
foo_doo(bar  : baz,
        quux : quuz,
        buzz : fuzz)
    : Promise<fuzz>
{
    ...
}

I will concede that style looks a little bit weird. But your eyes learn the shape very quickly and you grow to like it within about 10 minutes

Of course this doesn’t work

Why doesn’t it work?

Because JavaScript was designed by Satan and we live in hell

That works fine for top-level functions

It doesn’t work for instance methods defined within classes

The important thing I think is there is no function keyword in the context of a class. So normally we would do

class MyClass
{
    instance_variable_1 = initial_value_1;
    instance_variable_2 = initial_value_2;
    // declared, not given a value:
    instance_variable_3;

    async foo_doo(bar: baz, quux: quuz, buzz: fuzz) : Promise<fuzz>
    {
        ...
    }
}

(notice the lack of function there)

So since semicolons are optional (in a syntax regime where they really shouldn’t be!), this is potentially ambiguous

class MyClass
{
    instance_variable_1 = initial_value_1;
    instance_variable_2 = initial_value_2;
    // declared, not given a value:
    instance_variable_3;

    async
    foo_doo(bar  : baz,
            quux : quuz,
            buzz : fuzz)
        : Promise<fuzz>
    {
        ...
    }
}

Of course (because Javascript was designed by Satan) you are allowed to name an instance variable async. So that line is interpreted as a variable declaration.

Why you can name a variable a keyword is beyond me. But you can. I have no idea what would happen if I did that. I currently have a good relationship with Satan and I don’t wish to sour that by pushing his buttons, so I will not be experimenting with this quirk of his language.

Just note that, for instance methods in a class, the style now is

class MyClass
{
    instance_variable_1 = initial_value_1;
    instance_variable_2 = initial_value_2;
    // declared, not given a value:
    instance_variable_3;

    async foo_doo(bar  : baz,
                  quux : quuz,
                  buzz : fuzz)
        : Promise<fuzz>
    {
        ...
    }
}

which I don’t like as much but will accept as a compromise for now.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.