ruby编码规范 - oyaji's Blog

ruby编码规范

oyaji posted @ 2010年12月26日 03:48 in ruby with tags ruby coding rule , 2447 阅读

前言 

本blog是在ruby编程的基础上说明的。在实际的项目中,适当修改本规范使其更适合项目的需要。

source格式

1. 缩进
   为了容易阅读,需要适当的缩进。缩进的长度以2个space为准。请尽量使用space,不要使用tab。
 
if x > 0
  if y > 0
    puts "x > 0 && y > 0"
  end
end

2. 一行中的最大字符数 在一行中输入的字符数最大为80。

3. 空行 在class之间用空行分隔

class Foo
  ...
end
 
class Bar
  ...
end

class Foo
  ...
end
class Bar
  ...
end

另外,在class内部的各种组成单元间用空行分隔

class Foo
  attr :bar
 
  def baz
    ...
  end
 
  def quux
    ..
  end
end

4. 注释 方法定义的内部不追加注释。(当你认为有需要注释的代码的地发,请refactoring) 但class,module等的public方法的功能说明请在RDoc中注明

# 拆分逗号分隔的字符串、结果作为数组返回
def split_csv(str)
  return str.split(/,/)
end

构文规范 

1. class的构成要素 class的构成要素请按下面的顺序来依次进行 

1. include module 
2. 定义常数
3. 定义类变量,类的实例变量
4. 定义public属性的类方法
5. 定义accessor
6. 定义initialize
7. 定义public的实例方法
8. 定义protected的类方法
9. 定义protected的access
10.定义protected的实例方法
11.定义private的类方法
12.定义private的access
13.定义private的实例方法

2. 定义accessor

accessor是指attr_accessor,attr_reader,attr_writer这几个方法

3. 定义方法

定义方法时其参数要用()括起来。但没有参数的时候,不要加()

def foo(x, y)
  ...
end
 
def foo
  ...
end

4. 定义类方法

使用self来定义类方法

class Foo
  def self.foo
    ...
  end
end

class Foo
  def Foo.foo
    ...
  end
end

5. 方法调用

调用方法时要用()将参数括起来。但如果没有参数时不要加()。 
但在调用puts,print,p这几个方法时,参数不用() 

foo(1, "abc")
obj.foo(1, "abc")
bar
print "x = ", x, "\n"

foo 1, "abc"
obj.foo 1, "abc"
bar()

6. block 针对block基本原则是使用do...end

foo(x, y) do
  ...
end
 
x = bar(y, z) do
  ...
end

foo(x, y) {
  ...
}
 
x = bar(y, z) {
  ...
}

但如是行内嵌入的场合,请使用{}

s = ary.collect { |i| i.to_s }.join(",")

s = ary.collect do |i| i.to_s end.join(",")

7. return 要返回方法的值的时候,请使用明确使用return。但请省略()

def add(x, y)
  return x + y
end

def add(x, y)
  x + y
end
 
def add(x, y)
  return(x + y)
end

8. yield

通过yield来调用方法的写法,请参考方法调用

9. 条件分支

if后面的then可以省略。在if !x的判断时,请用unless x来代替,但unless的情况下不要使用else。

if x > 0
  puts "x > 0"
else
  puts "x <= 0"
end
 
unless x
  puts "x is false"
end
 
puts "x is true" if x

if x > 0 then
  puts "x > 0"
end
 
unless x
  puts "x is false"
else
  puts "x is true"
end
 
puts "foo && bar && baz && quux" if foo &&
  bar && baz && quux

10.case 在使用case语句时,省略then

case x
when 1
  ...
when 2
  ...
end

if x == 1
  ...
elsif x == 2
  ...
end
 
case x
when 1 then
  ...
when 2 then
  ...
end

11.不要使用条件分支的值

if x > 0
  msg = "x > 0"
else
  msg = "x <= 0"
end

msg = if x > 0
        "x > 0"
      else
        "x <= 0"
      end

12.循环处理

省略while的do。另在使用while !x的语句时,请用until x来代替

while cond
  ...
end
 
until cond
  ...
end

while cond do
  ...
end

13.无限循环时,请使用loop

while true
  ...
end

14.逻辑运算符

逻辑运算符有! && || (不要使用not and or)

15.三元运算符

条件复杂,或者需要换行的情况下,不要使用三元运算符

命名规范

1. 全体

1. 原则上不使用单词的缩略写法 
2. 作用域较小情况下的循环变量,使用i,j,k并按照这样的顺序来使用
3. 作用域较小情况下的变量密,使用class名称的简写形式也可以

2. 类名,模块名 

类名,模块名中每个单词的首字母大写,不要用’_'这样的分隔字符。 但在HTTP等这样的省略语的情况下,所有的字母都大写

ExampleClass  
HTTPClient

Example_Class
EXAMPLE_CLASS
HttpClient
HTTPclient
HTTP_Client

3. 方法名

方法名中全部的字母都要小写,单词之间用’_'这样的分隔字符。  

add_something

addsSomething
Add_Something

方法名中请使用动词的原形 返回的值是true/false的情况下,动词或者形容词后面加上?。不要用is_这样的写法

visible?

is_visible
is_visible?

如果,对可能改变传入参数的方法后面加上!

split
split! # split带有破坏性的版本

4. 常量名

类名模块名之外的常量定义时,全部字符大写,单词之间用’_'这样的分隔字符。

EXAMPLE_CONSTANT

5. 变量名

全部字符小写,单词之间用’_'这样的分隔字符。

tmp
local_variable
@instance_variable
$global_variable

6. 文件名

文件名全部小写,单词之间用’_'这样的分隔字符。 使用文件中主要的类名经变化之后,作为文件名

foo.rb # 定义foo类
foo-bar.rb # 定义foo-bar类
foo/bar-baz.rb # 定义Foo::BarBaz类(命名空间中的class)

 

Avatar_small
依云 说:
2010年12月26日 08:32

呃,没明白博主想表达什么。。。

Avatar_small
oyaji 说:
2010年12月26日 17:42

@依云: 昨晚没弄完:)


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee