Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions chapters/arrays/shuffling-array-elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ you are able to run it on any array you wish, in a much more direct manner.

{% highlight coffeescript %}
do -> Array::shuffle ?= ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the do makes sense.

for i in [@length-1..1]
j = Math.floor Math.random() * (i + 1)
[@[i], @[j]] = [@[j], @[i]]
if @length > 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My personal preference is to avoid placing over 50% of a function in a conditional. Instead I like to handle that logic with a short circuit not check:

return this unless @length > 1

for i in [@length-1..1]
j = Math.floor Math.random() * (i + 1)
[@[i], @[j]] = [@[j], @[i]]
@
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since in CoffeeScript @ and this are synonymous if I'm referring to the this object directly I use the this keyword and use the @ symbol when it is attached to a property/method (@foo, @bar()).

I feel it is more obvious what is happening. Too easy to read some code and miss the one character dangling at the bottom of indented code which really changes the return value quite drastically.


[1..9].shuffle()
Expand Down