GitHub advocacy of Ruby Code writing style summary _ruby topic

Source: Internet
Author: User
Tags control characters hash instance method

source code layout aspect:
1. All source files are encoded in UTF-8
2, using a 2-space indentation
3. Use UNIX-style line breaks (\ n), Windows (\ r \ n). You can use git config--global core.autocrlf true to prevent Windows-style line breaks from being generated.
4, in ', ', '; ' After the operator (in addition to the exponential operation), ' {', '} ' before and after the addition of space to increase the readability of the code.
5. Do not add spaces after the ' (', ') ', ' [', '] ' sign.
6, case and when at the same level of code.
7, 2 def uses a blank line to separate
8, for a method has multiple parameters cause too long, as the following way to deal with

def send_mail (source) 
 Mailer.deliver (to: ' bob@example.com ', from 
         : ' us@example.com ', 
         Subject: ' Important message ', 
         Body:source.text] 
End 

9, use RDOC production system API documentation, there is no blank line between the annotation and def.
10, no more than 80 lines per line
11, the end of each line do not have white space characters.

Grammatical aspects:
1, a method if there is no argument to omit the parentheses, there are parameters to use parentheses
2, if not necessary, do not use for, and use each to do the loop
3, do not use then
4. Use three-dimensional operation? : instead of If,else.
5, not in if else's internal use? : operator
6, use when x then ..., because when x .... The ruby1.9 was deleted.
7. Use && and | | Do Boolean operation, use And,or to do process control.
8, unless and else do not have a
9, unless it is to determine the conditions involved assignment operations, or not need to surround
10, the multiline statement block using {} contains,
11, do not need to use the return time do not use
12, do not use ' \ ' to connect 2 lines of code
13. Use | | = To initialize a variable, but not to initialize a Boolean variable
14, do not use the Perl-style variable name, $1-9, etc.
15. When running Ruby, add "-w" to hint at the bad parts of our code.
16. Write lambda and hash using Ruby1.9 syntax

Naming conventions:
1, use lowercase + "_" named Variables and methods
2. Naming module and class using the first letter caps
3. Use all Capitals + "_" named constants
4, for the return value is a Boolean method to add a "? "Suffix
5, for some potential risk of the method plus "! "Suffix, for example, exit, modify self, or variables, etc.

Comments:
1, on the annotation on one, the code is a comment,

class:
1, conforms to the Liskov principle, the subclass can replace the parent class.
2, try to make the class do solid (single responsibility, open-closed, Liskov substitution, Interface segregation and Dependency inversion)
3. Write a to_s method for each class to see the state of the class.
4, use the Attr family method to do the class attribute the access control
5, consider adding new factory methods to do some meaningful initialization of the case.
6, use ducktyping rather than inheritance. Because the characteristics of dynamic language, not the need for polymorphism.
7, avoid using @@, global variables,
8, according to the access situation, reasonable use of access control characters.
9, use Self to define a single instance method, instead of using the class name.

Exception:
1, do not let go of some anomalies
2, do not use the exception to do process Control
3, do not capture exception, exception base class.
4. Arrange exceptions according to the coverage of the exception type
5, put all the external resources into the Exception capture module
6, priority to use the library from the exception, rather than create their own exceptions

Collection:
1, priority to use%w to create a string array
2. Create an array on demand
3, using set to remove the duplicate elements in the list
4. Use symbol to do hash key instead of string, do not use variable object to do hash key.
5, do not traverse a list at the same time, and change it.

Strings:
1, the use of "#{string} #{string}" is superior to string+string
2. When a string is not in the form of a #{}, use ' denotes '
3. Do not use {} When making connection to instance variable
4, using << instead of + do string concatenation

Regular expression:
1. Use named groups instead of $1-9 to facilitate tracking.
2, ' ^ ', ' $ ' to match the whole line, match the entire string should use ' \a ', ' \z '
3, use the ' x ' modifier to modify the complex regex statements, increase readability, but note that the removal of the space problem

% of Syntax:
1. Multi-use%w
2, the need for string inline expression when using% ()
3, the use of%r when the regular expression appears in multiple/
4, do not use%q,%q,%x,%w,%s these characters
5, in% after the first use () as a separator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.