Skip to main content
added 220 characters in body
Source Link
Adriano Repetti
  • 10.5k
  • 1
  • 23
  • 47

We're not using Rule class hierarchy but, in general, remember that in JavaScript there isn't a (easy) syntax to enforce an object with a specific base class then, unless, it adds something, you can simply drop it.

We're not using Rule class hierarchy but, in general, remember that in JavaScript there isn't a (easy) syntax to enforce an object with a specific base class then, unless, it adds something, you can simply drop it.

added 258 characters in body
Source Link
Adriano Repetti
  • 10.5k
  • 1
  • 23
  • 47
const ruleAddE = (text) => text + "E";
const ruleAddNumberOne = (text) => text + "1";

function produce(starterString) {
    const rules = [ ruleAddE, ruleAddNumberOne ];
    return rules.reduce((text, rule) => rule(text), starterString);
}
function produce(starterString, rules = [ ruleAddE, ruleAddNumberOne ]) {
    return rules.reduce((text, rule) => rule(text), starterString || "");
}
produce(starterString) {
    const rules = [ ruleAddE, ruleAddNumberOne ];
    return rules.reduce((text, rule) => rule(text), starterString);
}
produce(starterString, rules = [ ruleAddE, ruleAddNumberOne ]) {
    return rules.reduce((text, rule) => rule(text), starterString || "");
}
const ruleAddE = (text) => text + "E";
const ruleAddNumberOne = (text) => text + "1";

function produce(starterString) {
    const rules = [ ruleAddE, ruleAddNumberOne ];
    return rules.reduce((text, rule) => rule(text), starterString);
}
function produce(starterString, rules = [ ruleAddE, ruleAddNumberOne ]) {
    return rules.reduce((text, rule) => rule(text), starterString || "");
}
added 258 characters in body
Source Link
Adriano Repetti
  • 10.5k
  • 1
  • 23
  • 47

1. You have classes but you do not effectively need them because they do not keep any state (even the applied field is actually unused). In C# you do not have much options (unless they're local) but in JavaScript you can use functions anywhere:

2. In short, your code might be:

3. To answer the question about "do you need a class for Producer?": if you don't need to call produce() multiple times on the same object then you don't even need a class and everything may be reduced to:

4. If you're using a class only to give a chance to the caller to specify a different set of rules:

produce(starterString, rules = [ ruleAddE, ruleAddNumberOne ]) {
    return rules.reduce((text, rule) => rule(text), starterString || "");
}

You have classes but you do not effectively need them because they do not keep any state (even the applied field is actually unused). In C# you do not have much options (unless they're local) but in JavaScript you can use functions anywhere:

In short, your code might be:

To answer the question about "do you need a class for Producer?": if you don't need to call produce() multiple times on the same object then you don't even need a class and everything may be reduced to:

If you're using a class only to give a chance to the caller to specify a different set of rules:

produce(starterString, rules = [ ruleAddE, ruleAddNumberOne ]) {
    return rules.reduce((text, rule) => rule(text), starterString);
}

1. You have classes but you do not effectively need them because they do not keep any state (even the applied field is actually unused). In C# you do not have much options (unless they're local) but in JavaScript you can use functions anywhere:

2. In short, your code might be:

3. To answer the question about "do you need a class for Producer?": if you don't need to call produce() multiple times on the same object then you don't even need a class and everything may be reduced to:

4. If you're using a class only to give a chance to the caller to specify a different set of rules:

produce(starterString, rules = [ ruleAddE, ruleAddNumberOne ]) {
    return rules.reduce((text, rule) => rule(text), starterString || "");
}
added 258 characters in body
Source Link
Adriano Repetti
  • 10.5k
  • 1
  • 23
  • 47
Loading
Source Link
Adriano Repetti
  • 10.5k
  • 1
  • 23
  • 47
Loading