From 474d174da2b13ca3cbccc8c8edc92f40165613f5 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Sun, 23 Jun 2019 14:42:54 -0300 Subject: [PATCH] New chapter: no booleans on parameters --- content/books/things-i-learnt/_index.md | 1 + .../boolean-parameters/index.md | 32 +++++++++++++++++++ .../things-i-learnt/future-trashing/index.md | 2 +- 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 content/books/things-i-learnt/boolean-parameters/index.md diff --git a/content/books/things-i-learnt/_index.md b/content/books/things-i-learnt/_index.md index cd15eb7..c534eca 100644 --- a/content/books/things-i-learnt/_index.md +++ b/content/books/things-i-learnt/_index.md @@ -25,3 +25,4 @@ template = "section-contentless.html" * Writing code * [Be Ready To Throw Your Code Away](throw-away) * [Future Thinking Is Future Trashing](future-trashing) + * [Don't Use Booleans As Parameters](boolean-parameters) diff --git a/content/books/things-i-learnt/boolean-parameters/index.md b/content/books/things-i-learnt/boolean-parameters/index.md new file mode 100644 index 0000000..622ce01 --- /dev/null +++ b/content/books/things-i-learnt/boolean-parameters/index.md @@ -0,0 +1,32 @@ ++++ +title = "Things I Learnt The Hard Way - Don't Use Booleans As Parameters" +date = 2019-06-23 + +[taxonomies] +tags = ["en-au", "books", "things i learnt", "booleans", "functions", "parameters"] ++++ + +When you're designing a function, you may be tempted to add a flag. Don't do +this. + + + +Here, let me show you an example: Suppose you have a messaging system and you +have a function that returns all the messages to an user, called +`getUserMessages`. But there is a case where you need to return a summary of +each message (say, the first paragraph) or the full message. So you add a +flag/Boolean parameter called `retrieveFullMessage`. + +Again, don't do that. + +'Cause anyone reading your code will see `getUserMessage(userId, true)` and +wonder what the heck that `true` means. + +You can either rename the function to `getUserMessageSummaries` and have +another `getUserMessagesFull` or something around those lines, but each +function just call the original `getUserMessage` with true or false -- but the +interface to the outside of your class/module will still be clear. + +But _don't_ add flags/Boolean parameters to your functions. + +{{ chapters(prev_chapter_link="/books/things-i-learnt/future-trashing", prev_chapter_title="Future Thinking is Future Trashing") }} diff --git a/content/books/things-i-learnt/future-trashing/index.md b/content/books/things-i-learnt/future-trashing/index.md index 5b0fe8d..bd1f902 100644 --- a/content/books/things-i-learnt/future-trashing/index.md +++ b/content/books/things-i-learnt/future-trashing/index.md @@ -23,4 +23,4 @@ solutions and _then_ you'll find your "solve everything". This pattern is the _abstraction_ you're looking for and _then_ you'll be able to solve it in a simple way. -{{ chapters(prev_chapter_link="/books/things-i-learnt/throw-away", prev_chapter_title="Be Ready To Throw Your Code Away") }} +{{ chapters(prev_chapter_link="/books/things-i-learnt/throw-away", prev_chapter_title="Be Ready To Throw Your Code Away", next_chapter_link="/books/things-i-learnt/boolean-parameters", next_chapter_title="Don't Use Booleans As Parameters") }}