Skip to content

Instantly share code, notes, and snippets.

@paulghaddad
Created February 5, 2015 23:06
Show Gist options
  • Save paulghaddad/9e29699b04323b9e47ed to your computer and use it in GitHub Desktop.
Save paulghaddad/9e29699b04323b9e47ed to your computer and use it in GitHub Desktop.
LevelUp 7: Understands the Principle of Least Surprise
1. Explain several conventions in ruby we use to conform to the Principle of Least Surprise.
Ruby adheres to the Principle of Least Surprise in many ways. Here are several instances, in my opinion:
- Well named methods whose function are obvious from their names, such as exists?, select and reject.
- The use of ? and ! methods. If used properly, programmers understand ahead of time what will happen when they are used (a Boolean will be returned or something destructive will occur).
- The IO and File class have several methods and parameters that borrow from Unix (open, close, r, w, etc) that aren't a surprise to users of the Unix command line.
- Ruby's duck typing allows the language to more adhere to the Principle. When a method is performed on an object, as long as it "quacks like a duck", the method will work. This principle would in turn be violated if an object, such as an ActiveRecord object from a returned query, didn't respond to #each.
- Ruby's implementation of blocks reduces the likelihood of surprise when programmers need to iterate over a collection. There is no need to know the number of elements in the array beforehand, and there are many methods such as #collect, that perform everyday functions on collections.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment