Style Guide

Version 12 (Brian Ford, 01/19/2009 03:38 PM)

1 1
h1. Style Guide
2 1
3 2 Brian Ford
Generally, "RSpec":http://rspec.rubyforge.org specs describe the expected behavior of code. While RSpec is fairly young, there are some conventions for writing specs. The RubySpecs cover a wide variety of components, so we have developed some pragmatic conventions to handle the various situations. As noted below, some conventions are more rigid than others.
4 1
5 1
These conventions apply to all specs. Existing specs that deviate from these conventions need to be fixed. Consistency is the principle that will almost always trump other conventions. Consistency aids understanding and readability. There are many thousands of lines of code in the spec files, so the value of consistency cannot be overstated.
6 1
7 1
The specs uniformly use @describe@ not @context@. The use of @it@ is preferred over @specify@ except in situations when the first word of the string is not a verb. The word "should" is unnecessary noise in the spec description strings and is not used. (The rationale is this: the spec string describes the expected behavior unconditionally. The code examples, on the other hand, set up an expectation that is tested with the call to the _should_ method. The code examples can violate the expectation, but the spec string does not. The value of the spec string is as clearly as possible describing the behavior. Including "should" in that description adds no value.)
8 1
9 1
Whenever possible, the spec strings should be written to conform to very basic English sentence structure: _subject + predicate_. The spec strings also uniformly use double-quotes, not single-quotes. The minimum number of words should be used to describe the behavior. Only make distinctions when they add significant value to understanding the behavior. This is explained further below. The general rule across all the specs is to use the least amount of detail to unambiguously describe behavior. Add to the detail conservatively. This is conceptually consistent with doing the simplest thing that could work.
10 1
11 5 Brian Ford
Ruby is a beautifully expressive language with _optional_ parentheses. There is a distinct preference for omitting parentheses in the specs whenever they are not needed. In other words, parentheses should _not_ be used unless necessary to make an expression syntactically or semantically correct.
12 1
13 1
h2. 1. Core and Standard Library
14 1
15 1
The specs for the Ruby core and standard libraries use one @describe@ block per method. For particularly complex methods, such as Array#[], more than one @describe@ block may be used according to the nature of arguments the method takes.
16 1
17 1
The @describe@ string should be "Constant.method" for class methods and "Constant#method" for instance methods. "Constant" is either a class or module name. For subclasses or submodules, the "Constant" name should be "Super::Sub". The @describe@ string should not include arguments to the methods unless absolutely necessary to describe the behavior of the method. Keep in mind that in Ruby duck-typing is a deeply embedded concept. Many methods will take any object that responds to a particular method or acts like an instance of a particular class.
18 1
19 6 Brian Ford
Nested @describe@ blocks should not be used. Various automated process scripts depend on the @describe@ string having the format explained above. Also, nested @describe@ blocks complicate the structure of the specs. If a particular situation appears to greatly benefit from nested blocks, open a discussion about it on the "mailing list":http://groups.google.com/group/rubyspec.
20 6 Brian Ford
21 1
22 1
  # This is correct
23 1
  describe "String#eql?" do
24 1
    it "returns true if other has the same length and content" do
25 1
      ...
26 1
    end
27 1
  end
28 1
29 1
  describe "Array#[]= with [index, count]" do
30 1
    it "returns non-array value if non-array value assigned" do
31 1
      ...
32 1
    end
33 1
  end
34 3 Brian Ford
35 1
36 4 Brian Ford
Contrast the _good_ example above with the one below. The following example deviates from the conventions for @describe@ strings and uses "should" and single-quotes for the descriptions.
37 4 Brian Ford
38 3 Brian Ford
39 1
  # This is NOT correct
40 1
  describe "String#eql?(string)" do
41 4 Brian Ford
    it 'should return true if other has the same length and content' do
42 1
      ...
43 1
    end
44 1
  end
45 1
46 4 Brian Ford
  describe 'Array#[]=(index, count)' do
47 4 Brian Ford
    it 'returns non-array value if non-array value assigned' do
48 1
      ...
49 1
    end
50 1
  end
51 1
52 1
53 1
The vast majority of the spec files for the core library have already been created. To create template files for the standard library classes, refer to the "mkspec":/wiki/mspec/Mkspec documentation.
54 1
55 1
h3. 1.1 Utility Classes
56 1
57 1
Many spec code examples refer to a particular class. To prevent name clashes with these different class definitions across all the specs, the classes should be scoped to a module. The convention is as follows:
58 1
59 1
60 1
module ObjectSpecs
61 1
  class SomeClass
62 1
  end
63 1
end
64 1
65 1
66 1
The module is named after the class for which the specs are being written. So, for the specs for _Object_, the module name is ObjectSpecs.
67 1
68 1
These utility classes are also referred to as _fixtures_. In the directory for each class, there is also a @fixtures@ directory. Refer to the existing files for examples.
69 1
70 1
h3. 1.2 Aliased or Identical Methods
71 1
72 1
Ruby has a significant number of aliased methods. True aliases are identical methods, so the specs should be exactly the same for each aliased method. The following illustrates the convention for specs for aliased methods (or just otherwise identical interfaces.)
73 1
74 1
In @rubyspec/1.8/core/array/shared/collect.rb@
75 1
76 1
77 1
shared :array_collect do |cmd|
78 1
  describe "Array##{cmd}" do
79 1
    it "returns a copy of array with each element replaced by the value returned by block" do
80 1
      a = ['a', 'b', 'c', 'd']
81 1
      b = a.send(cmd) { |i| i + '!' }
82 1
      b.should == ["a!", "b!", "c!", "d!"]
83 1
    end
84 1
85 1
    it "does not return subclass instances" do
86 1
      MyArray[1, 2, 3].send(cmd) { |x| x + 1 }.class.should == Array
87 1
    end
88 1
  end
89 1
end
90 1
91 1
92 1
In @rubyspec/1.8/core/array/collect_spec.rb@
93 1
94 1
require File.dirname(__FILE__) + '/../../spec_helper'
95 1
require File.dirname(__FILE__) + '/shared/collect.rb'
96 1
97 1
describe "Array#collect" do
98 5 Brian Ford
  it_behaves_like :array_collect, :collect
99 1
end
100 1
101 1
102 1
In @rubyspec/1.8/core/array/map_spec.rb@
103 1
104 1
require File.dirname(__FILE__) + '/../../spec_helper'
105 1
require File.dirname(__FILE__) + '/shared/collect.rb'
106 1
107 1
describe "Array#map" do
108 5 Brian Ford
  it_behaves_like :array_collect, :map
109 1
end
110 1
111 1
112 7 Brian Ford
h3. 1.3 Floating Point Values
113 7 Brian Ford
114 8 Brian Ford
Writing specs that use floating point values poses a problem because two values that look the same when rendered to a string may not actually be bitwise equal. Also, floating point operations can result in a value that differs based on the way the FPU carried out the operations.
115 8 Brian Ford
116 8 Brian Ford
Specs that compare floating point values should use @#should_be_close@ with the TOLERANCE constant. For floating point values that are exact, but larger than the precision formatted with #to_s (e.g. 1093840198347109283720.00), use the expanded float literal not the truncated precision format that #to_s provides (e.g. don't use 1.09384019834711e+21).
117 1
118 9 Brian Ford
h3. 1.4 Private methods
119 9 Brian Ford
120 11 Brian Ford
Generally, no specs are written for private methods. A notable exeception are the specs for #initialize on some classes. These specs are primarily written to illustrate the behavior of #initialize for subclasses, where the subclass #initialize behavior is contrasted with the superclass's. Another exeception is #initialize_copy.
121 9 Brian Ford
122 9 Brian Ford
h3. 1.5 Ruby Ducktyping Interface
123 9 Brian Ford
124 12 Brian Ford
Ruby method dispatch behavior calls #method_missing if an instance has no method corresponding to a particular selector.  Ruby also defines a number of methods, for example, #to_ary, #to_int, #to_str, that form an interface to Ruby's ducktyping behavior. String methods, for instance, may call #to_str when passed an argument that is not a String.
125 9 Brian Ford
126 9 Brian Ford
The point of the RubySpecs is to describe behavior in such a way that if two different implementations pass a spec, Ruby code that relies on behavior described by the spec will execute with the same result on either implementation.
127 9 Brian Ford
128 11 Brian Ford
If a spec asserts that a method calls #to_int on an object, it is immaterial to the final outcome whether an implementation calls #to_int and handles the possibility that the method is missing in some way, or first calls #respond_to?(:to_int) and then calls #to_int. There are only two significant aspects to this from the perspective of user code (i.e. code using the interface, not code implementing the behavior): 1) #to_int is called and performs some action; or 2) #to_int is not called.
129 9 Brian Ford
130 9 Brian Ford
It is conceivable that user code like the following exists:
131 9 Brian Ford
132 9 Brian Ford
133 9 Brian Ford
  class Silly
134 9 Brian Ford
    def method_missing(sym, *args)
135 9 Brian Ford
      return 1 if sym == :to_int
136 9 Brian Ford
    end
137 9 Brian Ford
  end
138 9 Brian Ford
139 9 Brian Ford
140 9 Brian Ford
In such case, the behavior of the following code would be different:
141 9 Brian Ford
142 9 Brian Ford
143 9 Brian Ford
  # The implementation calls #to_int without checking #respond_to?
144 9 Brian Ford
  [1, 2].at(silly) # => 2
145 9 Brian Ford
146 9 Brian Ford
  # The implementation calls #respond_to? first
147 9 Brian Ford
  [1, 2].at(silly) # => TypeError
148 9 Brian Ford
149 9 Brian Ford
150 11 Brian Ford
In the second case, the expected behavior is restored if the Silly class is modified to implement a #respond_to?(:to_int).
151 9 Brian Ford
152 11 Brian Ford
The point is that it really is not sensible to implement an object that provides an interface but does not let the world know about it by either 1) defining the method properly, or 2) defining #respond_to? to indicate that the object provides the interface.
153 9 Brian Ford
154 11 Brian Ford
If real-world code exists that _depends_ on this silly implementation (i.e. cannot be coded in a more realistic way), then we can revisit the utility of specs that require #respond_to? to be called. Otherwise, these specs are too tied to the implementation and impose an unrealistic burden on implementations that may exhibit perfectly compatible behavior but not call #respond_to?.
155 9 Brian Ford
156 1
h2. 2. Language
157 1
158 1
For the language specs, there is nothing as convenient or as concrete as a particular method to spec. Review the discussion of the "organization":/wiki/rubyspec/Organization of the language specs. The general conventions apply here: use simple English to describe the behavior of the _language entities_ and only add detail as needed. Use a single @describe@ block initially and add distinguishing @describe@ blocks as necessary. Use @it@ rather than @specify@ whenever possible.