• +43 660 1453541
  • contact@germaniumhq.com

When More Is Less


When More Is Less

This time we’re going to look at a simple example of well written, compact code, and make it larger. And better.

If we have code like this is it’s pretty good:

export function setMobile(context: any, mobile: boolean): void {
    context.commit('setMenuHidden', mobile);
    context.commit('setMobile', mobile);
}

Personally I don’t like it.

Not surprisingly I like this more:

export function setMobile(context: any, mobile: boolean): void {
    if (mobile) {
        context.commit('setMenuHidden', true);
        context.commit('setMobile', true);
        return;
    }

    context.commit('setMenuHidden', false);
    context.commit('setMobile', false);
}

Why? Because it reads so much better.

I’m not setting the menu hidden to the "mobile" value. If I’m in the mobile mode, then I’m setting the menu to be hidden.

It’s just common sense.