Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
dev-guide
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
VeNtura
dev-guide
Commits
2990f26d
Commit
2990f26d
authored
Dec 26, 2014
by
Giang Tran
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Internal coding rule
parent
8b1aa6bc
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
237 additions
and
0 deletions
+237
-0
ruby/InternalCodingRule.md
+237
-0
No files found.
ruby/InternalCodingRule.md
0 → 100644
View file @
2990f26d
# Internal Coding Rule
## Các qui tắt chung về indent, space
-
Dùng encoding UTF-8
-
Dùng 2 khoảng trắng (whitespaces) cho indent (Lưu ý: không dùng tab)
-
Dùng space trước và sau các toán tử +,-,x,/, {, }, =, dấu phẩy, chấm phẩy
-
Không dùng space sau dấu (,
[
và trước dấu ),
]
```
ruby
some
(
arg
).
other
[
1
,
2
,
3
].
size
```
-
Không dùng ; khi kết thúc dòng code.
```
ruby
# bad
puts
'foobar'
;
puts
'foo'
;
puts
'bar'
# good
puts
'foo'
puts
'bar'
puts
'foo'
,
'bar'
```
-
Không dùng space giữa biểu thức range
```
ruby
# bad
1
..
3
'a'
..
'z'
# good
1
..
3
'a'
..
'z'
```
-
Thêm dấu _ ở các số lớn.
```
ruby
# bad - how many 0s are there?
num
=
1000000
# good - much easier to parse for the human brain
num
=
1_000_000
```
-
Trong biểu thức case when, indent when bằng với case
```
ruby
# good
case
when
song
.
name
==
'Misty'
puts
'Not again!'
when
song
.
duration
>
120
puts
'Too long!'
else
song
.
play
end
```
-
Đặt 1 dòng trắng (blank line) giữa 2 định nghĩa method
```
ruby
def
some_method
#aaaa
end
def
other_method
#bbbb
end
```
-
Thêm khoảng trắng vào trước và sau = khi gán giá trị mặc định trong biểu thức hàm
```
ruby
# bad
def
some_method
(
arg1
=
:default
,
arg2
=
nil
,
arg3
=
[])
# do something...
end
# good
def
some_method
(
arg1
=
:default
,
arg2
=
nil
,
arg3
=
[])
# do something...
end
```
-
Không viết quá 80 kí tự trên 1 dòng lệnh
-
Không được để kí tự trắng cuối mỗi dòng (traling whitespace)
-
Phải để 1 dòng trắng cuối mỗi file
-
Không dùng block comment
```
ruby
# bad
=begin
comment line
another comment line
=end
# good
# comment line
# another comment line
```
## Cú pháp
-
Dùng (, ) ở khai báo hàm có truyền tham số, không dùng (, ) trong trường hợp hàm không nhận tham số
### Các câu lệnh điều kiện
-
Không dùng and, or. Dùng &&, ||
-
Dùng ''unless'' thay cho ''if not''
-
Không dùng ''unless ... else''
-
Không dùng (, ) ở điều kiện trong câu if, unless
-
Dùng các hàm số có sẵn như ''x.even?, x.odd?, x.nil?, x.zero?'' thay cho các câu so sánh ''x % 2 == 0, x % 2 == 1, x == nil, x == 0''
-
Trong trường hợp thân câu if/unless/while/until chỉ có một dòng, dùng inline format.
```
ruby
do_something
if
some_condition
thay
cho
if
some_condition
do_something
end
```
-
Canh chỉnh indent cho các câu gọi method dài dòng như sau.
```
ruby
# bad (line is too long)
def
send_mail
(
source
)
Mailer
.
deliver
(
to:
'bob@example.com'
,
from:
'us@example.com'
,
subject:
'Important message'
,
body:
source
.
text
)
end
# good
def
send_mail
(
source
)
Mailer
.
deliver
(
to:
'bob@example.com'
,
from:
'us@example.com'
,
subject:
'Important message'
,
body:
source
.
text
)
end
# good (normal indent)
def
send_mail
(
source
)
Mailer
.
deliver
(
to:
'bob@example.com'
,
from:
'us@example.com'
,
subject:
'Important message'
,
body:
source
.
text
)
end
```
-
Canh chỉ indent cho các thành phần của array trên nhiều hàng như sau.
```
ruby
# bad - single indent
menu_item
=
[
'Spam'
,
'Spam'
,
'Spam'
,
'Spam'
,
'Spam'
,
'Spam'
,
'Baked beans'
,
'Spam'
,
'Spam'
,
'Spam'
,
'Spam'
,
'Spam'
]
# good
menu_item
=
[
'Spam'
,
'Spam'
,
'Spam'
,
'Spam'
,
'Spam'
,
'Spam'
,
'Spam'
,
'Baked beans'
,
'Spam'
,
'Spam'
,
'Spam'
,
'Spam'
,
'Spam'
]
# good
menu_item
=
[
'Spam'
,
'Spam'
,
'Spam'
,
'Spam'
,
'Spam'
,
'Spam'
,
'Spam'
,
'Baked beans'
,
'Spam'
,
'Spam'
,
'Spam'
,
'Spam'
,
'Spam'
]
```
### Câu lệnh lặp
-
Hạn chế dùng câu ''for'', nên dùng câu 'each'
-
Dùng ''until'' thay cho ''while not''
### Khai báo và gọi hàm
-
Bỏ (, ) khi gọi các hàm không tham số
-
Nếu hàm có tham số, để các tham số trong (). Ví dụ:
`foo(param1, param2)`
.
-
Không cần dùng return ở dòng cuối cùng trong khai báo hàm. Ví dụ:
```
ruby
def
some_method
(
some_arr
)
some_arr
.
size
end
```
-
Không dùng space giữa tên hàm và dấu (
-
Dùng -> thay cho từ khóa lambda
-
Dùng các câu inline if thay cho các câu if lồng nhau. Ví dụ:
```
ruby
# Bad
def
compute_thing
(
thing
)
if
thing
[
:foo
]
update_with_bar
(
thing
)
if
thing
[
:foo
][
:bar
]
partial_compute
(
thing
)
else
re_compute
(
thing
)
end
end
end
# Good
def
compute_thing
(
thing
)
return
unless
thing
[
:foo
]
update_with_bar
(
thing
[
:foo
])
return
re_compute
(
thing
)
unless
thing
[
:foo
][
:bar
]
partial_compute
(
thing
)
end
```
-
Khi khai báo biến array, hash, dùng cấu trúc:
`a_arr = [], a_hash = {}`
thay cho
`a_arr = Array.new, a_hash = Hash.new`
## Qui tắc đặt tên
-
Tên biến, tên hàm, symbol:
`a_var_name`
,
`get_prime`
,
`a_symbol`
.
-
Tên class, module: SomeClass, SomeModule
-
Hằng số: SOME_CONST
## Class, Module
-
Dùng include, extend ngay sau khai báo class
-
Dùng
`attr_reader`
,
`attr_writer`
,
`attr_accessor`
thay cho các hàm set, get
-
Hạn chế dùng
`self << class`
, nên dùng
`def self.a_func`
## Collection
-
Dùng %w để khai báo mảng các chuỗi
-
Dùng %i để khai báo mảng các symbol
-
Nên dùng symbol làm key trong hash. Ví dụ: {:one => 1, :two => 2}
## String
-
Dùng
`#{}`
thay cho nối chuỗi. Vd:
`email_with_name = "#{user.name} <#{user.email}>"`
-
Dùng %Q để khai báo chuỗi có ', ", #{}
-
Dùng + để cắt khi chuỗi quá dài. Ví dụ:
```
ruby
long_str
=
'a very long long long long long long'
+
'long long long long string'
```
# More reference
https://github.com/bbatsov/ruby-style-guide
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment