Browse Source

New chapter: no booleans on parameters

master
Julio Biason 5 years ago
parent
commit
474d174da2
  1. 1
      content/books/things-i-learnt/_index.md
  2. 32
      content/books/things-i-learnt/boolean-parameters/index.md
  3. 2
      content/books/things-i-learnt/future-trashing/index.md

1
content/books/things-i-learnt/_index.md

@ -25,3 +25,4 @@ template = "section-contentless.html"
* Writing code * Writing code
* [Be Ready To Throw Your Code Away](throw-away) * [Be Ready To Throw Your Code Away](throw-away)
* [Future Thinking Is Future Trashing](future-trashing) * [Future Thinking Is Future Trashing](future-trashing)
* [Don't Use Booleans As Parameters](boolean-parameters)

32
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.
<!-- more -->
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") }}

2
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 _abstraction_ you're looking for and _then_ you'll be able to solve it in a
simple way. 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") }}

Loading…
Cancel
Save