immutability 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.coveralls.yml +2 -0
- data/.gitignore +9 -0
- data/.metrics +9 -0
- data/.rspec +2 -0
- data/.rubocop.yml +2 -0
- data/.travis.yml +24 -0
- data/.yardopts +3 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +7 -0
- data/Guardfile +14 -0
- data/LICENSE +21 -0
- data/README.md +201 -0
- data/Rakefile +34 -0
- data/config/metrics/STYLEGUIDE +230 -0
- data/config/metrics/cane.yml +5 -0
- data/config/metrics/churn.yml +6 -0
- data/config/metrics/flay.yml +2 -0
- data/config/metrics/metric_fu.yml +14 -0
- data/config/metrics/reek.yml +1 -0
- data/config/metrics/roodi.yml +24 -0
- data/config/metrics/rubocop.yml +71 -0
- data/config/metrics/saikuro.yml +3 -0
- data/config/metrics/simplecov.yml +6 -0
- data/config/metrics/yardstick.yml +37 -0
- data/immutability.gemspec +25 -0
- data/lib/immutability.rb +126 -0
- data/lib/immutability/rspec.rb +4 -0
- data/lib/immutability/version.rb +9 -0
- data/lib/immutability/with_memory.rb +70 -0
- data/spec/shared/user.rb +16 -0
- data/spec/spec_helper.rb +18 -0
- data/spec/support/immutable.rb +25 -0
- data/spec/unit/immutability/with_memory_spec.rb +101 -0
- data/spec/unit/immutability_spec.rb +63 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YTQ3MDdmODU2NDZkZmYxMzJjY2E2OTExYTAzMmRlNzY1NmU5NmU4Mw==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MTc4MTk4OGVkOWY5NmQzMTNjNGRlMGRmMzk1ZTQwNzdmYzI5MjhhMQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
Yzc0NTMyOTM1MmY4ZTllOTMyZmE0N2QzOGFhNzg1YjBiZTE2ZmE1ZGQ4OWU1
|
10
|
+
N2QzMzVmZjQ2NWU0YzUzYmU1YTU3MTM1ZDE3OTU3OTg0NzhlZDIwYTAyN2Yx
|
11
|
+
NWU4NzM0ZjI5YWQ5MTRkOWIyOGY2MWM5Y2UxZDliZDE2ZWJkYTE=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ODg1NDUxY2UzM2VhOGE0M2E2NDQ0ZjlmMzk4NTRiMmM2NzgxN2RjMGM3MTIw
|
14
|
+
MjdlY2Q4M2NlYzZkMzU5NzE3MzE3MTFmMWYzNWNkNzcxNTEyMjc5ZmUwM2I4
|
15
|
+
YjM0NmZhODQ3ODA1MDk3MDczYzBjNTQ4ZDRkYzRmMWVmYzFjNDM=
|
data/.coveralls.yml
ADDED
data/.gitignore
ADDED
data/.metrics
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# Settings for metric_fu and its packages are collected in the `config/metrics`
|
2
|
+
# and loaded by the Hexx::Suit::Metrics::MetricFu.
|
3
|
+
|
4
|
+
begin
|
5
|
+
require "hexx-suit"
|
6
|
+
Hexx::Suit::Metrics::MetricFu.load
|
7
|
+
rescue LoadError
|
8
|
+
puts "The 'hexx-suit' gem is not installed"
|
9
|
+
end
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
---
|
2
|
+
language: ruby
|
3
|
+
bundler_args: --without metrics
|
4
|
+
cache: bundler
|
5
|
+
script: bundle exec rake test:coverage:run
|
6
|
+
env:
|
7
|
+
global:
|
8
|
+
- JRUBY_OPTS="-Xcli.debug=true --debug"
|
9
|
+
rvm:
|
10
|
+
- '1.9.3'
|
11
|
+
- '2.0'
|
12
|
+
- '2.1'
|
13
|
+
- '2.2'
|
14
|
+
- ruby-head
|
15
|
+
- rbx-2 --1.9
|
16
|
+
- rbx-2 --2.1
|
17
|
+
- jruby-1.7-19mode
|
18
|
+
- jruby-1.7-21mode
|
19
|
+
- jruby-9.0.0.0
|
20
|
+
- jruby-head
|
21
|
+
matrix:
|
22
|
+
allow_failures:
|
23
|
+
- rvm: ruby-head
|
24
|
+
- rvm: jruby-head
|
data/.yardopts
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
guard :rspec, cmd: "bundle exec rspec" do
|
4
|
+
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
|
7
|
+
watch(%r{^lib/immutability/(.+)\.rb}) do |m|
|
8
|
+
"spec/unit/#{m[1]}_spec.rb"
|
9
|
+
end
|
10
|
+
|
11
|
+
watch("lib/immutability.rb") { "spec" }
|
12
|
+
watch("spec/spec_helper.rb") { "spec" }
|
13
|
+
|
14
|
+
end # guard :rspec
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2015 Andrew Kozin (nepalez), andrew.kozin@gmail.com
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Immutability
|
2
|
+
============
|
3
|
+
|
4
|
+
[![Gem Version](https://img.shields.io/gem/v/immutability.svg?style=flat)][gem]
|
5
|
+
[![Build Status](https://img.shields.io/travis/nepalez/immutability/master.svg?style=flat)][travis]
|
6
|
+
[![Dependency Status](https://img.shields.io/gemnasium/nepalez/immutability.svg?style=flat)][gemnasium]
|
7
|
+
[![Code Climate](https://img.shields.io/codeclimate/github/nepalez/immutability.svg?style=flat)][codeclimate]
|
8
|
+
[![Coverage](https://img.shields.io/coveralls/nepalez/immutability.svg?style=flat)][coveralls]
|
9
|
+
[![Inline docs](http://inch-ci.org/github/nepalez/immutability.svg)][inch]
|
10
|
+
|
11
|
+
Makes instances immutable (deeply frozen) and versioned.
|
12
|
+
|
13
|
+
Preamble
|
14
|
+
--------
|
15
|
+
|
16
|
+
The project is a clone of the [aversion][aversion] gem by [Josep M. Bach][txus] with some implementation differencies:
|
17
|
+
|
18
|
+
- it uses [ice_nine][ice_nine] gem to freeze instances deeply.
|
19
|
+
- instead of storing procedures that changed the instance, it stores reference to the previous state and the number of current version.
|
20
|
+
|
21
|
+
This approach to object's identity as a sequence of immutable snapshots is heavily inspired by 2009 year's brilliant talk ["Are We There Yet?"][are_we_there_yet] by [Rich Hickey][richhickey].
|
22
|
+
|
23
|
+
Synopsis
|
24
|
+
--------
|
25
|
+
|
26
|
+
### Immutable objects without memory:
|
27
|
+
|
28
|
+
Include the `Immutability` module to make the object immutable (deeply frozen).
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
require "immutability"
|
32
|
+
|
33
|
+
class User
|
34
|
+
include Immutability
|
35
|
+
|
36
|
+
attr_reader :name, :age
|
37
|
+
|
38
|
+
def initialize(name, age)
|
39
|
+
@name = name
|
40
|
+
@age = 44
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
young_andrew = User.new "Andrew", 44
|
45
|
+
young_andrew.name # => "Andrew"
|
46
|
+
young_andrew.age # => 44
|
47
|
+
|
48
|
+
# The instance is frozen deeply:
|
49
|
+
young_andrew.frozen? # => true
|
50
|
+
young_andrew.name.frozen? # => true
|
51
|
+
young_andrew.age.frozen? # => true
|
52
|
+
```
|
53
|
+
|
54
|
+
Use `update` with a block to create a **new instance** with updated values (other instance values remains the same):
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
elder_andrew = young_andrew.update { @age = 45 }
|
58
|
+
elder_andrew.name # => "Andrew"
|
59
|
+
elder_andrew.age # => 45
|
60
|
+
|
61
|
+
# The instance is frozen deeply:
|
62
|
+
elder_andrew.frozen? # => true
|
63
|
+
elder_andrew.name.frozen? # => true
|
64
|
+
elder_andrew.age.frozen? # => true
|
65
|
+
```
|
66
|
+
|
67
|
+
### Immutable objects with memory
|
68
|
+
|
69
|
+
Include `Immutability.with_memory` module to add `version` and `parent`:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
require "immutability"
|
73
|
+
|
74
|
+
class User
|
75
|
+
include Immutability.with_memory
|
76
|
+
|
77
|
+
attr_reader :name, :age
|
78
|
+
|
79
|
+
def initialize(name, age)
|
80
|
+
@name = name
|
81
|
+
@age = 44
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
young_andrew = User.new "Andrew", 44
|
86
|
+
young_andrew.name # => "Andrew"
|
87
|
+
young_andrew.age # => 44
|
88
|
+
|
89
|
+
# The instance is frozen as well:
|
90
|
+
young_andrew.frozen? # => true
|
91
|
+
young_andrew.name.frozen? # => true
|
92
|
+
young_andrew.age.frozen? # => true
|
93
|
+
|
94
|
+
# Now it is versioned:
|
95
|
+
young_andrew.version # => 0
|
96
|
+
young_andrew.parent # => nil
|
97
|
+
```
|
98
|
+
|
99
|
+
The method `update` stores reference to the `#parent` and increases `#version`:
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
elder_andrew = young_andrew.update { @age = 45 }
|
103
|
+
elder_andrew.name # => "Andrew"
|
104
|
+
elder_andrew.age # => 45
|
105
|
+
|
106
|
+
# Version is updated:
|
107
|
+
elder_andrew.version # => 1
|
108
|
+
elder_andrew.parent.equal? young_andrew # => true
|
109
|
+
```
|
110
|
+
|
111
|
+
Notice, than no instances in the sequence can be garbage collected (they still refer to each other).
|
112
|
+
|
113
|
+
Use `#forget_history` methods to reset version and free old instances for GC:
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
reborn_andrew = elder_andrew.forget_history
|
117
|
+
reborn_andrew.name # => "Andrew"
|
118
|
+
reborn_andrew.age # => 45
|
119
|
+
|
120
|
+
# History is forgotten
|
121
|
+
reborn_andrew.version # => 0
|
122
|
+
reborn_andrew.parent # => nil
|
123
|
+
```
|
124
|
+
|
125
|
+
RSpec helper
|
126
|
+
------------
|
127
|
+
|
128
|
+
Include `immutability/rspec` and use `be_immutable` RSpec matcher to check, whether an instance is deeply immutable (with all its variables):
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
include "immutability/rspec"
|
132
|
+
|
133
|
+
describe User, ".new" do
|
134
|
+
subject { User.new "Andrew", 44 }
|
135
|
+
|
136
|
+
it { is_expected.to be_immutable }
|
137
|
+
end
|
138
|
+
```
|
139
|
+
|
140
|
+
The matcher will pass if both the object and all its variables are immutable at any level of nesting.
|
141
|
+
|
142
|
+
Installation
|
143
|
+
------------
|
144
|
+
|
145
|
+
Add this line to your application's Gemfile:
|
146
|
+
|
147
|
+
```ruby
|
148
|
+
# Gemfile
|
149
|
+
gem "immutability"
|
150
|
+
```
|
151
|
+
|
152
|
+
Then execute:
|
153
|
+
|
154
|
+
```
|
155
|
+
bundle
|
156
|
+
```
|
157
|
+
|
158
|
+
Or add it manually:
|
159
|
+
|
160
|
+
```
|
161
|
+
gem install immutability
|
162
|
+
```
|
163
|
+
|
164
|
+
Compatibility
|
165
|
+
-------------
|
166
|
+
|
167
|
+
Tested under rubies [compatible to MRI 1.9+](.travis.yml).
|
168
|
+
|
169
|
+
Uses [RSpec][rspec] 3.0+ for testing and [hexx-suit][suit] for dev/test tools collection.
|
170
|
+
|
171
|
+
Contributing
|
172
|
+
------------
|
173
|
+
|
174
|
+
* Read the [STYLEGUIDE](config/metrics/STYLEGUIDE)
|
175
|
+
* [Fork the project](https://github.com/nepalez/immutability)
|
176
|
+
* Create your feature branch (`git checkout -b my-new-feature`)
|
177
|
+
* Add tests for it
|
178
|
+
* Run `rake mutant` or `rake exhort` to ensure 100% [mutant-proof][mutant] coverage
|
179
|
+
* Commit your changes (`git commit -am '[UPDATE] Add some feature'`)
|
180
|
+
* Push to the branch (`git push origin my-new-feature`)
|
181
|
+
* Create a new Pull Request
|
182
|
+
|
183
|
+
License
|
184
|
+
-------
|
185
|
+
|
186
|
+
See the [MIT LICENSE](LICENSE).
|
187
|
+
|
188
|
+
[are_we_there_yet]: http://www.infoq.com/presentations/Are-We-There-Yet-Rich-Hickey
|
189
|
+
[aversion]: https://github.com/txus/aversion
|
190
|
+
[codeclimate]: https://codeclimate.com/github/nepalez/immutability
|
191
|
+
[coveralls]: https://coveralls.io/r/nepalez/immutability
|
192
|
+
[gem]: https://rubygems.org/gems/immutability
|
193
|
+
[gemnasium]: https://gemnasium.com/nepalez/immutability
|
194
|
+
[ice_nine]: https://github.com/dkubb/ice_nine
|
195
|
+
[inch]: https://inch-ci.org/github/nepalez/immutability
|
196
|
+
[mutant]: https://github.com/mbj/mutant
|
197
|
+
[richhickey]: http://github.com/richhickey
|
198
|
+
[rspec]: http://rspec.org
|
199
|
+
[suit]: https://github.com/nepalez/hexx-suit
|
200
|
+
[travis]: https://travis-ci.org/nepalez/immutability
|
201
|
+
[txus]: https://github.com/txus
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rubygems"
|
5
|
+
|
6
|
+
# Loads bundler tasks
|
7
|
+
Bundler::GemHelper.install_tasks
|
8
|
+
|
9
|
+
# Loads the Hexx::RSpec and its tasks
|
10
|
+
begin
|
11
|
+
require "hexx-suit"
|
12
|
+
Hexx::Suit.install_tasks
|
13
|
+
rescue LoadError
|
14
|
+
require "hexx-rspec"
|
15
|
+
Hexx::RSpec.install_tasks
|
16
|
+
end
|
17
|
+
|
18
|
+
desc "Runs specs and check coverage"
|
19
|
+
task :default do
|
20
|
+
system "bundle exec rake test:coverage:run"
|
21
|
+
end
|
22
|
+
|
23
|
+
desc "Runs mutation metric for testing"
|
24
|
+
task :mutant do
|
25
|
+
system "mutant -r immutability --use rspec Immutability* --fail-fast"
|
26
|
+
end
|
27
|
+
|
28
|
+
desc "Exhort all evils"
|
29
|
+
task :exhort do
|
30
|
+
system "mutant -r immutability --use rspec Immutability*"
|
31
|
+
end
|
32
|
+
|
33
|
+
desc "Runs all the necessary metrics before making a commit"
|
34
|
+
task prepare: %w(exhort check:inch check:rubocop check:fu)
|
@@ -0,0 +1,230 @@
|
|
1
|
+
= Ruby Style Guide
|
2
|
+
|
3
|
+
Adapted from Dan Kubb's Ruby Style Guide
|
4
|
+
https://github.com/dkubb/styleguide/blob/master/RUBY-STYLE
|
5
|
+
|
6
|
+
== Commiting:
|
7
|
+
|
8
|
+
* Write descriptive commit messages, following the pattern:
|
9
|
+
|
10
|
+
[TYPE] name
|
11
|
+
|
12
|
+
The message, describing the changes being made
|
13
|
+
|
14
|
+
* Use the types below to mark commits:
|
15
|
+
|
16
|
+
- FEATURE - for adding new features, or backward-compatible changes;
|
17
|
+
- CHANGE - for backward-incompatible changes;
|
18
|
+
- BUG FIX - for fixing bugs;
|
19
|
+
- REFACTORING - for other changes of the code not affecting the API;
|
20
|
+
- OTHER - for changes in documentaton, metrics etc, not touching the code;
|
21
|
+
- VERSION - for version changes.
|
22
|
+
|
23
|
+
* Always separate commits of different types (such as FEATURE and CHANGE).
|
24
|
+
|
25
|
+
* Try to separate various features from each other.
|
26
|
+
|
27
|
+
* Include specification to the same commit as the code.
|
28
|
+
|
29
|
+
* Run all tests before making a commit.
|
30
|
+
Never commit the code that break unit tests.
|
31
|
+
|
32
|
+
* Use metric (run `rake check`) before making a commit.
|
33
|
+
|
34
|
+
* Do refactoring before making a commit. Best writing is rewriting.
|
35
|
+
|
36
|
+
* Follow semantic versioning.
|
37
|
+
|
38
|
+
http://semver.org/
|
39
|
+
|
40
|
+
* For versions name the commit after a version number, following the pattern:
|
41
|
+
|
42
|
+
VERSION 1.0.0-rc2
|
43
|
+
|
44
|
+
|
45
|
+
== Formatting:
|
46
|
+
|
47
|
+
* Use UTF-8. Declare encoding in the first line of every file.
|
48
|
+
|
49
|
+
# encoding: utf-8
|
50
|
+
|
51
|
+
* Use 2 space indent, no tabs.
|
52
|
+
|
53
|
+
* Use Unix-style line endings.
|
54
|
+
|
55
|
+
* Use spaces around operators, after commas, colons and semicolons,
|
56
|
+
around { and before }.
|
57
|
+
|
58
|
+
* No spaces after (, [ and before ], ).
|
59
|
+
|
60
|
+
* Align `when` and `else` with `case`.
|
61
|
+
|
62
|
+
* Use an empty line before the return value of a method (unless it
|
63
|
+
only has one line), and an empty line between defs.
|
64
|
+
|
65
|
+
* Use empty lines to break up a long method into logical paragraphs.
|
66
|
+
|
67
|
+
* Keep lines fewer than 80 characters.
|
68
|
+
|
69
|
+
* Strip trailing whitespace.
|
70
|
+
|
71
|
+
|
72
|
+
== Syntax:
|
73
|
+
|
74
|
+
* Write for 2.0.
|
75
|
+
|
76
|
+
* Use double quotes
|
77
|
+
|
78
|
+
http://viget.com/extend/just-use-double-quoted-ruby-strings
|
79
|
+
|
80
|
+
* Use def with parentheses when there are arguments.
|
81
|
+
|
82
|
+
* Never use for, unless you exactly know why.
|
83
|
+
|
84
|
+
* Never use then, except in case statements.
|
85
|
+
|
86
|
+
* Use when x then ... for one-line cases.
|
87
|
+
|
88
|
+
* Use &&/|| for boolean expressions, and/or for control flow. (Rule
|
89
|
+
of thumb: If you have to use outer parentheses, you are using the
|
90
|
+
wrong operators.)
|
91
|
+
|
92
|
+
* Avoid double negation (!!), unless Null Objects are expected.
|
93
|
+
|
94
|
+
http://devblog.avdi.org/2011/05/30/null-objects-and-falsiness
|
95
|
+
|
96
|
+
* Avoid multiline ?:, use if.
|
97
|
+
|
98
|
+
* Use {...} when defining blocks on one line. Use do...end for multiline
|
99
|
+
blocks.
|
100
|
+
|
101
|
+
* Avoid return where not required.
|
102
|
+
|
103
|
+
* Use ||= freely.
|
104
|
+
|
105
|
+
* Use OO regexps, and avoid =~ $0-9, $~, $` and $' when possible.
|
106
|
+
|
107
|
+
* Do not use Enumerable#inject when the "memo" object does not change between
|
108
|
+
iterations, use Enumerable#each_with_object instead (in ruby 1.9,
|
109
|
+
active_support and backports).
|
110
|
+
|
111
|
+
* Prefer ENV.fetch to ENV[] syntax.
|
112
|
+
Prefer block syntax for ENV.fetch to usage of the second argument.
|
113
|
+
|
114
|
+
|
115
|
+
== Naming:
|
116
|
+
|
117
|
+
* Use snake_case for methods.
|
118
|
+
|
119
|
+
* Use CamelCase for classes and modules. (Keep acronyms like HTTP,
|
120
|
+
RFC, XML uppercase.)
|
121
|
+
|
122
|
+
* Use SCREAMING_SNAKE_CASE for other constants.
|
123
|
+
|
124
|
+
* Do not use single letter variable names. Avoid uncommunicative names.
|
125
|
+
|
126
|
+
* Use consistent variable names. Try to keep the variable names close
|
127
|
+
to the object class name.
|
128
|
+
|
129
|
+
* Use names prefixed with _ for unused variables.
|
130
|
+
|
131
|
+
* When defining a predicate method that compares against another object of
|
132
|
+
a similar type, name the argument "other".
|
133
|
+
|
134
|
+
* Prefer map over collect, detect over find, select over find_all.
|
135
|
+
|
136
|
+
* Use def self.method to define singleton methods.
|
137
|
+
|
138
|
+
* Avoid alias when alias_method will do.
|
139
|
+
|
140
|
+
|
141
|
+
== Comments:
|
142
|
+
|
143
|
+
* Use YARD and its conventions for API documentation. Don't put an
|
144
|
+
empty line between the comment block and the def.
|
145
|
+
|
146
|
+
* Comments longer than a word are capitalized and use punctuation.
|
147
|
+
Use one space after periods.
|
148
|
+
|
149
|
+
* Avoid superfluous comments.
|
150
|
+
|
151
|
+
|
152
|
+
== Code structuring:
|
153
|
+
|
154
|
+
* Break code into packages, decoupled from the environment.
|
155
|
+
|
156
|
+
* Wrap packages into gems.
|
157
|
+
|
158
|
+
* Inject dependencies explicitly.
|
159
|
+
Leave all outer references on the border of any package. Inside
|
160
|
+
the package use internal references only.
|
161
|
+
|
162
|
+
* Follow SOLID principles.
|
163
|
+
|
164
|
+
http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)
|
165
|
+
|
166
|
+
* Only give a method one purpose for existing. If you pass in a boolean
|
167
|
+
to a method, what you're saying is that this method has two different
|
168
|
+
behaviours. Just split it into two single purpose methods. If you have
|
169
|
+
to use the words "AND" or "OR" to describe what the method does it
|
170
|
+
probably does too much.
|
171
|
+
|
172
|
+
* Avoid long methods.
|
173
|
+
Try to keep them at no more than 6 lines long, and preferably 4 or less.
|
174
|
+
|
175
|
+
If sections of a method are logically separate by blank lines, then
|
176
|
+
that's probably a sign that those sections should be split into separate
|
177
|
+
methods.
|
178
|
+
|
179
|
+
* Avoid hashes-as-optional-parameters. Does the method do too much?
|
180
|
+
|
181
|
+
* Avoid long parameter lists.
|
182
|
+
|
183
|
+
* Add "global" methods to Kernel (if you have to) and make them private.
|
184
|
+
|
185
|
+
* Use OptionParser for parsing complex command line options and
|
186
|
+
ruby -s for trivial command line options.
|
187
|
+
|
188
|
+
* Avoid needless metaprogramming.
|
189
|
+
|
190
|
+
* Always freeze objects assigned to constants.
|
191
|
+
|
192
|
+
|
193
|
+
== General:
|
194
|
+
|
195
|
+
* Code in a functional way, avoid mutation when it makes sense.
|
196
|
+
|
197
|
+
* Try to have methods either return the state of the object and have
|
198
|
+
no side effects, or return self and have side effects. This is
|
199
|
+
otherwise known as Command-query separation (CQS):
|
200
|
+
|
201
|
+
http://en.wikipedia.org/wiki/Command-query_separation
|
202
|
+
|
203
|
+
* Do not mutate arguments unless that is the purpose of the method.
|
204
|
+
|
205
|
+
* Try following TRUE heuristics by Sandi Metz
|
206
|
+
|
207
|
+
http://designisrefactoring.com/2015/02/08/introducing-sandi-metz-true/
|
208
|
+
|
209
|
+
* Do not mess around in core classes when writing libraries.
|
210
|
+
Namespace your code inside the modules, or wrap core classes to
|
211
|
+
decorators of your own.
|
212
|
+
|
213
|
+
* Do not program defensively.
|
214
|
+
|
215
|
+
http://www.erlang.se/doc/programming_rules.shtml#HDR11
|
216
|
+
|
217
|
+
* Keep the code simple.
|
218
|
+
|
219
|
+
* Don't overdesign.
|
220
|
+
|
221
|
+
* Don't underdesign.
|
222
|
+
|
223
|
+
* Avoid bugs.
|
224
|
+
|
225
|
+
* Read other style guides and apply the parts that don't dissent with
|
226
|
+
this list.
|
227
|
+
|
228
|
+
* Be consistent.
|
229
|
+
|
230
|
+
* Use common sense.
|