HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptModerate

Creating a status object based on the command type and action

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thecreatingstatusactiontypebasedandcommandobject

Problem

I need to create an object based on some properties of another given object. In my example setRules, add properties to object status.

Considering that the cases to be handled will increase increase I would like to figure out how to enhance readability.

I would like to know:

  • Could a switch statement increase readability (please scroll code below to see version 02 of the script)?



  • Any better way to write this from readability point of view?



```
// version 01
var status = {};
function setRules(cmd) {
if (cmd.type === 'category' && cmd.action === 'add') {
status = {
app: {
isEdit: false,
hasPriority: true
},
category: {
lock: false
}
};
} else if (cmd.type === 'category' && cmd.action === 'edit') {
status = {
app: {
isEdit: true,
hasPriority: true
},
category: {
lock: true
}
};
} else if (cmd.type === 'category' && cmd.action === 'delete') {
status = {
app: {
isEdit: true,
hasPriority: false
},
category: {
lock: false
}
};
} else if (cmd.type === 'category' && cmd.action === 'read') {
status = {
app: {
isEdit: false,
hasPriority: false
},
category: {
lock: false
}
};
} else if (cmd.type === 'users' && cmd.action === 'read') {
status = {
app: {
isEdit: false,
hasPriority: false
},
category: {
lock: false
}
};
} else if (cmd.type === 'users' && cmd.action === 'edit') {
status = {
app: {
isEdit: true,
hasPriority: true
},

Solution

If you know about the format of cmd you can merge the properties you're checking and check against both in one step.

status = and the structure of the Object it's being set to is repeated code, factor it out. When figuring out what to factor out you can almost imagine you're a dumb diff algorithm: what are the minimal text changes necessary to change between these forms? The answer is all the code that should be in that place, plus code to plug it into the thing that implements the commonalities of the form.

Don't be afraid to put code on one line when its complexity is low. Yes you'll need to refactor if the complexity grows, but worrying about possibilities without considering their likelihood is a path to insanity. Make the code look the best it can today.

A rewrite with those changes:

function setRules(cmd) {
  switch (cmd.type + '/' + cmd.action) {
    case 'category/add':    setStatus(false,  true, false); break;
    case 'category/edit':   setStatus( true,  true,  true); break;
    case 'category/delete': setStatus( true, false, false); break;
    case 'category/read':   setStatus(false, false, false); break;
    case 'users/read':      setStatus(false, false, false); break;
    case 'users/edit':      setStatus( true,  true,  true); break;
  }
}

function setStatus(edit, priority, lock) {
  status = {
    app: {isEdit: edit, hasPriority: priority},
    category: {lock: lock},
  };
}

Code Snippets

function setRules(cmd) {
  switch (cmd.type + '/' + cmd.action) {
    case 'category/add':    setStatus(false,  true, false); break;
    case 'category/edit':   setStatus( true,  true,  true); break;
    case 'category/delete': setStatus( true, false, false); break;
    case 'category/read':   setStatus(false, false, false); break;
    case 'users/read':      setStatus(false, false, false); break;
    case 'users/edit':      setStatus( true,  true,  true); break;
  }
}

function setStatus(edit, priority, lock) {
  status = {
    app: {isEdit: edit, hasPriority: priority},
    category: {lock: lock},
  };
}

Context

StackExchange Code Review Q#116204, answer score: 15

Revisions (0)

No revisions yet.