4
\$\begingroup\$

I need to determine the status of a parent object based on 2 variables of each of its children. I came up with a working solution, but this includes a nested "if-else if-else". Needless to say, it doesn't look very elegant.

I was wondering if there is a way to simplify this. I have muddled around with some map/reduce code, but did not get to anything that is more elegant than the code below.

const parent = {
 children: [{
 connected: true,
 online: true
 },
 {
 connected: true,
 online: true
 }
 ]
}
// all online & all connected => connected
// all online & some connected => partially disconnected
// all online & none connected => disconnected
// some online => partially offline
// none online => offline
const onlineArr = parent.children.map(c => c.online);
const connectedArr = parent.children.map(c => c.connected);
let status;
if (!onlineArr.includes(true)) {
 status = 'Offline';
} else if (!onlineArr.includes(false)) {
 if (!connectedArr.includes(true)) {
 status = 'Disconnected';
 } else if (!connectedArr.includes(false)) {
 status = 'Connected';
 } else {
 status = 'Partially disconnected';
 }
} else {
 status = 'Partially offline';
}
console.log(status);

200_success
146k22 gold badges190 silver badges479 bronze badges
asked Mar 22, 2019 at 14:10
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

As a function

When you write code, even as an example, always write it as a function. A function is returnable, and thus can be written differently than non returnable flat execution.

Comment code mismatch

"Comments are just lies in waiting."
...by unknown guru.

Your comments do not match the code. The comments specify lowercase status, your code capitalists the status. Which is correct is anyone's guess. (削除) I will assume the comments are correct and that the status is formatted upon display. (It makes the solution simpler) (削除ここまで) Changed my mind and will assume the code has been tested and is correct.

Inefficiency === Inelegance

The nested statements are not inelegant (in a function it would not need any else statements), its the two Array.map and three Array.includes that are very inefficient for the task at hand, which to me is ugly inelegance.

Solution

It is the number of online, connected children that you need to know.

If the number of online children

  • is the same as the number of children then all are online.
  • is the less than as the number of children and not zero then some are online.
  • is zero then none are online

The same applies for connected children.

Thus count the two types and use the counts to return the status, as follows

function connectionStatus(clients) {
 const count = clients.length;
 var onC = 0, conC = 0; 
 for (const {connected, online} of clients) {
 conC += connected;
 onC += online;
 }
 if (onC === count) {
 if (conC === count) { return "Connected" }
 return conC ? "Partially disconnected" : "Disconnected";
 }
 return onC ? "Partially offline" : "Offline";
}
connectionStatus(parent.children);
answered Mar 22, 2019 at 16:46
\$\endgroup\$
3
  • \$\begingroup\$ I would replace var onC = 0, conC = 0; with const metrics = { connected: 0, online: 0 }; to gain additional readability. And a chained ternary operator to return the result string perhaps. \$\endgroup\$ Commented Aug 19, 2019 at 17:27
  • \$\begingroup\$ @dfhwze The line between personal preference and idiomatic style often finds me writing code I am not comfortable with. I prefer nested ternaries, but don't like the down votes. Obj metric to encap names would decrease readability, it just adds noise. Reassigning names in loop .{connected: con, online: on} of clients then vars connected += con; online += on rather than conC and onC but was unsure re browser support. Most annoying was the strings caps. All lowercase was is my pref, not really the functions role to format bio speak, hand that to CSS or higher level code \$\endgroup\$ Commented Aug 19, 2019 at 19:08
  • \$\begingroup\$ It's a pitty ppl downvote nested ternaries. They are compact and don't hurt readability at all :( \$\endgroup\$ Commented Aug 19, 2019 at 19:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.