Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good idea to shorten property paths by assigning new variables?

I was ask why I would introduce a variable to shorten the property path yesterday? For me it was just the subjective feeling that it is easier to read. I'm now wondering if there are any objective reasons to choose one of the two following options (memory consumption, popular convention, ...)?

Option 1:

var errors        = require('../errors'),
    NotFoundError = errors.NotFoundError;

function example() {
    ...
    new NotFoundError('Item was not found');
}

Option 2:

var errors = require('../errors');

function example() {
    ...
    new error.NotFoundError('Item was not found');
}
like image 928
sebgie Avatar asked Nov 23 '25 08:11

sebgie


1 Answers

Doing this loses context, both in the programming sense (if NotFoundError relies on this being error, it will fail with Option 1 because the context will be different), and the literal sense (averted in this case because Error is in the class name itself, but a more ambiguous name could lead to confusion)

like image 146
Niet the Dark Absol Avatar answered Nov 24 '25 20:11

Niet the Dark Absol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!