-2
\$\begingroup\$

I have a repetitive set of instructions that I would like to optimize. Is there a better pattern to achieve the same goal but with less repetition ?

var assigned = this.state.data ? this.state.data.assigned : 1,
delivered = this.state.data ? this.state.data.delivered : 1,
unassigned = this.state.data ? this.state.data.unassigned : 1,
pending = this.state.data ? this.state.data.pending: 1,
total = this.state.data ? this.state.data.total: 1,
failed = this.state.data ? this.state.data.failed : 1;
\$\endgroup\$
9
  • \$\begingroup\$ I dont think why people's are downvoting without proper explanation \$\endgroup\$
    – rajesh
    Feb 5 '19 at 13:34
  • \$\begingroup\$ I don't know how this lacks context. This is the code - a series of assignments that are each a ternary expression. The goal is to have this effect without being that verbose. \$\endgroup\$
    – VLAZ
    Feb 5 '19 at 14:22
  • \$\begingroup\$ @VLAZ It's all explained in the help center. We don't review snippets. There's a long list of reasons for that, which can all be found on meta. \$\endgroup\$
    – Mast
    Feb 6 '19 at 11:46
  • \$\begingroup\$ @Mast I still don't get it. As far as I can see, this example answers "yes" to each of the criteria listed. There is code, OP is presumably the author or maintainer, seems to be from an actual project, it works as intended, OP wants this to be "good code" as in improved, all facets can be discussed and I've indeed made suggestions not directly related to OP's request. Sure, it's a short code but I don't see length being mentioned. The only thing I see it maybe failing is the "Do not ask 'How do I best do X'" but if it's that reason there could have been an edit or comment. \$\endgroup\$
    – VLAZ
    Feb 6 '19 at 11:54
  • 1
    \$\begingroup\$ The problem is that the code is presented without context. It is hard to give the proper advice unless we see where the state values come from and how they are used. \$\endgroup\$ Feb 6 '19 at 18:53
1
\$\begingroup\$

First of all, you can use the OR operand. It's idiomatic to mean "either get the value of a variable or a fallback value, if falsey"

assignment = someVariable || "fallback value"

Since you're checking this.state.data every time, it's better to either check it once

const data = this.state.data || {}

and then use that

var assigned = data.assigned || 1,
    delivered = data.delivered || 1,
    unassigned = data.unassigned || 1,
    pending = data.pending || 1,
    total = data.total || 1,
    failed = data.failed || 1;

This will leave this.state.data untouched, you are just working with a different variable called data.

Alternatively, you can directly check and possibly initialise this.state.data, assuming that doesn't lead to problems with any other potential initialisation.

this.state.data = this.state.data || {}

and then you can check var assigned = this.state.data.assigned || 1 and so on.

Note that this style will give you the fallback if the current value is falsey. This includes an empty string or the number zero. If those are valid values, then you should not be using the OR operator to get them. For example, with this.state.data.failed = 0 the expression failed = this.state.data.failed || 1 will give you 1. In that case, you might need to write a custom function to get the value or get a default value. If you have Lodash, you can use _.get() for that purpose.

If you are using ES6, then this can be drastically shortened using a destructuring assignment with default values

const data = { assigned: 5, delivered: 10, total: 42 };

let { 
  assigned = 1, 
  delivered = 1, 
  unassigned = 1, 
  pending = 1, 
  total = 1, 
  failed = 1 
} = data;

console.log("assigned", assigned);
console.log("delivered", delivered);
console.log("unassigned", unassigned);
console.log("pending", pending);
console.log("total", total);
console.log("failed", failed);

\$\endgroup\$
1
  • \$\begingroup\$ It would be useful to know what is wrong with this question/answer if we are to improve them. \$\endgroup\$
    – VLAZ
    Feb 5 '19 at 11:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.