category | cover | created | tags | title |
---|---|---|---|---|
Practice |
/assets/tips/avoid-boolean-parameters.png |
2021-05-13 |
JavaScript |
Avoid boolean parameters |
Let's consider a situation where we have a function that writes a string to a file. It allows user to append the content to file, or override the content via the override
parameter:
const writeToFile = (content: string, file: string, override: boolean) => {
...
};
With that signature, the function will be invoked as following
// Append the content to file
writeToFile(content, file, true);
// Override the file
writeToFile(content, file, false);
If you are not the one who creates the function, you have to question what the boolean value represents until looking at the implementation.
It is worse if the function has a lot of boolean flags. Using boolean flags makes the core harder to read and maintain.
There are a few ways to get rid of the issue.
appendToFile(content, file);
overrideFile(content, file);
writeToFile(content, file, { override });
If you're using TypeScript, then you can use enum
to represent the possible values of a boolean flag.
enum SaveMode {
Append,
Override,
}
writeToFile(content, file, mode: SaveMode);
It's confident for consumers to call the method:
writeToFile(content, file, SaveMode.Append);
// Or
writeToFile(content, file, SaveMode.Override);