Skip to content

Commit

Permalink
Avoid self
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed May 5, 2012
1 parent 39eea48 commit 1389452
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions README.md
Expand Up @@ -479,6 +479,55 @@ You can generate a PDF or an HTML copy of this guide using
end
```

* Avoid `self` where not required.

```Ruby
# bad
def ready?
if self.last_reviewed_at > self.last_updated_at
self.worker.update(self.content, self.options)
self.status = :in_progress
end
self.status == :verified
end

# good
def ready?
if last_reviewed_at > last_updated_at
worker.update(content, options)
self.status = :in_progress
end
status == :verified
end
```

* As a corollary, avoid shadowing methods with local variables unless they are both equivalent

```Ruby
class Foo
attr_accessor :options

# ok
def initialize(options)
self.options = options
# both options and self.options are equivalent here
end

# bad
def do_something(options = {})
unless options[:when] == :later
output(self.options[:message])
end
end

# good
def do_something(params = {})
unless params[:when] == :later
output(options[:message])
end
end
end

* Use spaces around the `=` operator when assigning default values to method parameters:

```Ruby
Expand Down

0 comments on commit 1389452

Please sign in to comment.