I'm currently working on an enormous, enterprise level project. There are about 130 other devs on said project, and I aim to document my code in the clearest way, so others may easily see what I'm doing and why. I'm using JSDoc style comments to summarize my functions, but sometimes I feel the need to break it down to a more granular level.
A particular method, using a decently obtuse function chain, comes to mind. To provide a bit of background... this method takes an integer which is the sum of various powers of two, where each power represents a string "rule". It derives a subset of rules from master list using this sum.
What is a cleaner, more straightforward way to document "steps" within a function, giving line-by-line descriptions if necessary?
...
},
intToRuleset: function(subset) { // ex: intToRuleset(40);
var rules = this.rules, // the master list of rules
mask = (subset) // 40
.toString(2) // '101000'
.split('')// ['1','0', '1','0','0','0']
.reverse(); // ['0','0','0','1', '0','1']
return rules.filter(function(a, i) {
return +mask[i];
});
},
...
As you can see, I'm only crappily explaining what I'm doing; it seems mostly redundant to me as I'm only providing the result of each function... any fool can guess that the reverse of ['foo', 'bar']
is ['bar', 'foo']
.
What I want to do is describe both what I'm doing and why/my thought process.
I've tried a few things: verbose inline commentary both above and below lines, labeled each with a number as a key/legend sol'n, etc. I think all attempts have been ugly and made the code harder to read overall.
...
},
intToRuleset: function(subset) { // ex: intToRuleset(40);
var rules = this.rules, // the master list of rules
mask = (subset) // 40
/* 1 - convert into base 2 to form mask -- we'll exploit
truthy/false-y 1s and 0s to filter out the associated
rules. */
.toString(2) // '101000'
/* 2 - split into array so it can be reversed */
.split('')// ['1','0', '1','0','0','0']
/* 3 - reversing the "mask" array so it aligns with */
.reverse(); // ['0','0','0','1', '0','1']
return rules.filter(function(a, i) {
/* 4 - coerce string into number into "boolean";
if 1, rule stays; otherwise, it's filtered out.
*/
return +mask[i];
});
// example return:
// ['034 - 50000lb waffle != fuzzball',
// '222 - steeple-people === dentists]
},
...
What's a better way to comment/document steps like this?