DEV Community

Discussion on: Compact or Verbose Codestyle?

Collapse
 
nicolus profile image
Nicolas Bailly • Edited

I'm not a js developer, but I would use this middle ground (hoping it actually does the same thing) :

const get = (store, key) => {
    if (!Reflect.has(window[store], key)) {
        throw new Error(`Key "${key}" not found in window.${store}.`);
    }

    return JSON.parse(window[store][key])
};
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jsn1nj4 profile image
Elliot Derhay

Can confirm it would run.

Collapse
 
vdedodev profile image
Vincent Dedo

I was going to make a similar code suggestion, the variables at the end don't add anything.

Collapse
 
bernhardwebstudio profile image
Bernhard Webstudio

Well, one little use case of the variables for newbies: easier to debug (slide in a console.log(var) easily).

Collapse
 
f1lt3r profile image
F1LT3R • Edited

One thing you get from The verbosity of the ending variables, is that you don't have to break apart the line to debug the code. You could identify whether your bug resides in the JSON parse or the store getter without changing any lines of code.

I think generally, developers are more comfortable compacting things we understand well, especially when they are stable and behave in expected ways (like JSON.parse and window.localStorage).

I believe I observe people compacting calls to their own code more often, (because they understand it well), and use more verbosity calling their peers' code (because they understand it less).

Personally I lean more towards universal verbosity for the ability to debug without changing lines of code. In my opinion, this also fosters self documenting code, especially when calling internal code and not common environment/library APIs.