Ruby Coding Convention (Phần 4)
Trong phần này, tôi sẽ tiếp tục giúp mọi người tìm hiểu về Class, Modules, Exceptions, Collections
Classes & Modules
- Sử dụng một cấu trúc thống nhất cho các class mà bạn định nghĩa.
1234567891011121314151617181920212223class Personextend SomeModuleinclude AnotherModuleSOME_CONSTANT = 20attr_reader :attribute_namevalidates :attribute_name# public classesdef some_methodend# protected and private methosprotecteddef some_protected_methodendprivatedef some_private_methodendend - Sử dụng
module
thay choclass
nếu chỉ định nghĩa funtions.class
chỉ nên dùng khi chúng ta tạo đối tượng mới khi dùng
123456789101112131415161718192021# badclass SomeClassdef self.some_method# body omittedenddef self.some_other_methodendend# goodmodule SomeClassmodule_functiondef some_method# body omittedenddef some_other_methodendend - Sử dụng
attr_reader
vàattr_accessor
khi có thể
12345678910111213141516171819202122232425# badclass Persondef initialize(first_name, last_name)@first_name = first_name@last_name = last_nameenddef first_name@first_nameenddef last_name@last_nameendend# goodclass Personattr_reader :first_name, :last_namedef initialize(first_name, last_name)@first_name = first_name@last_name = last_nameendend - Thiết kế class dựa theo nguyên lý SOLID
- Tránh sử dụng
@
khi không cần thiết phải sử dụng ở bên ngoài class - Tránh sử dụng
@@
khi không cần thiết vì nó là biến toàn cục
123456789class Parent@@class_var = 'parent'def self.print_class_varputs @@class_varendendclass Child "child" - Định nghĩa lại các hàm
to_s, as_json
nếu bạn cần
1234567891011121314151617class Personattr_reader :first_name, :last_namedef initialize(first_name, last_name)@first_name = first_name@last_name = last_nameenddef to_s"#{@first_name} #{@last_name}"enddef as_jsonfirst_name: first_name,last_name: last_nameendend - Sử dụng
def self.method
để định nghĩa singleton methods.
1234567891011class TestClass# baddef TestClass.some_method# body omittedend# gooddef self.some_other_method# body omittedendend
Exceptions
- Chỉ sử dụng
raise
khi bắt một exception. Còn nếu không, hãy sử dụngfail
12345beginfail 'Oops'rescue => errorraise if error.message != 'Oops'end - Không return trong
ensure
block, Khi bạn sử dụng return trong đó, sẽ không exception nào được raise. - Sử dụng tích hợp
begin
blocks nếu có thể
123456789101112131415# baddef foobegin# main logic goes hererescue# failure handling goes hereendend# gooddef foo# main logic goes hererescue# failure handling goes hereend - Không sử dụng exceptions khi nó đóng vai trò trong luồng xử lý chính
12345678910111213# badbeginn / drescue ZeroDivisionErrorputs 'Cannot divide by 0!'end# goodif d.zero?puts 'Cannot divide by 0!'elsen / dend - Không rescuing
Exception
class.
1234567891011121314# badbeginexitrescue Exceptionputs "you didn't really want to exit, right?"# exception handlingend# goodbegin# code obmittedrescue => e# exception handlingend - Nếu bạn có nhiều rescues, hãy sắp xếp chúng hợp lý.
1234567begin# some coderescue StandardError => e# some handlingrescue Exception => e# some handlingend