惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
I
InfoQ
The Register - Security
The Register - Security
L
LangChain Blog
H
Help Net Security
The GitHub Blog
The GitHub Blog
S
Schneier on Security
博客园 - 【当耐特】
W
WeLiveSecurity
Attack and Defense Labs
Attack and Defense Labs
IT之家
IT之家
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
The Cloudflare Blog
H
Heimdal Security Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Y
Y Combinator Blog
雷峰网
雷峰网
N
Netflix TechBlog - Medium
Security Archives - TechRepublic
Security Archives - TechRepublic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
Lohrmann on Cybersecurity
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Exploit Database - CXSecurity.com
P
Privacy & Cybersecurity Law Blog
G
GRAHAM CLULEY
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
V
Visual Studio Blog
博客园 - 聂微东
PCI Perspectives
PCI Perspectives
Last Week in AI
Last Week in AI
A
Arctic Wolf
宝玉的分享
宝玉的分享
T
The Blog of Author Tim Ferriss
S
Secure Thoughts
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
云风的 BLOG
云风的 BLOG
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
SegmentFault 最新的问题
SecWiki News
SecWiki News
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
Schneier on Security
Schneier on Security
P
Proofpoint News Feed
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
AI
AI
Engineering at Meta
Engineering at Meta

Nic Lin's Blog

謝明真 - 高效領導力的課後筆記 NFT 開發實戰!基礎智能合約入門 (3) NFT 開發實戰!基礎智能合約入門 (2) NFT 開發實戰!基礎智能合約入門 (1) 如何自我檢測 log4j CVE 漏洞 Rails 如何在資料寫入時記錄來源 IP 位置 如何經營工程師 Youtube 頻道 - Part 8 營收篇 如何經營工程師 Youtube 頻道 - Part 7 酸民文化篇 如何經營工程師 Youtube 頻道 - Part 5 設備器材篇 如何經營工程師 Youtube 頻道 - Part 4 後製剪輯篇 如何經營工程師 Youtube 頻道 - Part 3 文案企劃篇 如何經營工程師 Youtube 頻道 - Part 2 設備器材篇 如何經營工程師 Youtube 頻道 - Part 1 制訂頻道方向篇 如何經營工程師 Youtube 頻道 - Part 0 Rails 中避免 race condition 的最佳實踐(二) Rails 中避免 race condition 的最佳實踐(一) 10 分鐘整合 google sheet 做自動化開發功能週報 經營 Side Project 300 天所帶來的收穫及挑戰 我的 Youtube 影片製作流程 API 設計時必須注意的 HTTP header 底線問題 如何提升你的程式可讀性之實務技巧(三) 如何提升你的程式可讀性之實務技巧(二) 如何提升你的程式可讀性之實務技巧(一) Ruby 中使用 freeze 優化效能的時機 避免 React 中的 useEffect 無限 render 在 Rails 內輕量使用 Vue Component 的最佳實踐 如何在區域網路用 Docker 架設有 SSL 的 Gitlab 從被問到問人,那些我常問的面試問題 [Rails] 如何漂亮寫出可維護的 query (Maintainable Rails Query) 在已知長度情況下優化 slice 的性能 [ReactNative] 如何在 iOS APP 上主動要求用戶評分 Rails 的 scope 為什麼用 lambda? Proc 與 lambda 不同之處 淺談 Active Record 的 Lazy load 特性 Rails 專案搭配 Github Actions 進行 RSpec 自動化測試 JavaScript 中 require, import 的差別及效能 React 效能優化基本招 ES6 箭頭函式 (Arrow functions) 2 個月擁有 6000 用戶 Side project 這樣做(一) 如何讓自己成為失敗的軟體工程師 如何用 Rack::Attack 阻擋 DDOS / 惡意流量 用 OpenSSL 自簽開發用 HTTPS SSL 憑證 為機器加上登入訊息,在 ubuntu 設置登入歡迎詞 Ruby Memoization 性能優化之記憶化 淺談 SSH agent forwarding 和 proxy command 的安全風險與應用 [Rails] Service / Library / Concern 的差異 避免過度的 Defensive Programming 防禦性程式設計 1:1 攪亂器,如何用 Ruby 做可逆推序號 [Rails] 用 puma-dev 作為本地開發伺服器 (支援 https 自簽憑證) 將 Rails 專案從手動部屬遷移使用 Capistrano 自動化部屬 工程師提昇自己的教學和簡報技術的方法 [筆記] Rails 3.2 升級 Rails 6.beta 經驗分享 Class method 氾濫帶來什麼問題 RDBMS 課程心得與筆記 常用的 Rails 開發規範 Rest-Client 如何做 Basic Authentication 驗證 [Rails] 何為 tld_lebgth? 遵循 Semantic Versioning 軟體開發語意化版本管理 請直接在 MySQL 裡面直接用 utf8mb4 取代 utf8 如何解決在 awesome print 中遇到 ActionController::Parameters unable to convert unpermitted 如何在 Mac 上升級 PostgreSQL 並遷移資料 如何解決 Mysql2::Error: Incorrect string value 讀書心得 - 「信任因子:信任如何影響大腦運作、激勵員工、達到組織目標」 我是如何寫部落格筆記的 讀書心得 - 「先問,為什麼?:顛覆慣性思考的黃金圈理論,啟動你的感召領導力」 [Rails] 解決 Reset Password 帶來的 token 洩漏問題 我的軟體工程師生涯:如何挑選適合你的公司 Rails 中的 delegate 用法 淺述 SSR SPA 優缺點 Rails 非同步工作請用 Global ID [React] Class Component 傳遞 props 的 2 種方式 好用的隱私權政策 URL 自動生成 Rails 5.1 之後的 tag helper Rails 5.2 Encrypted Credentials 最近面試被給的建議和書單 一般架構需要用到 K8S 嗎 透過 commit SHA 找 github Pull request 從零搭建,如何讓 Rails 跑在 Kubernetes(k8s)(二) 從零搭建,如何讓 Rails 跑在 Kubernetes(k8s)(一) React Stateless Functional Components 搞懂 React 中的 state 和 props 物件導向基本原則 SOLID (Ruby Sample) 在以太坊智能合約上是可以預測隨機數的 在台灣租屋必須注意的事 Rails 5 簡單雙向加解密 如何用 ABA 培養自律型員工 不要在 rake task 中定義 method, 請用 RAKE::DSL rails 非hash只想用array輸出page 如何處理陣列裡有重複的值 [Rails] 如何重設你的專案名稱 Ruby on Rails install on Mac 安裝步驟 使用 Friendly_id 與 Babosa 美化你的Rails 網址 Junior Rails 兩個月實戰心得 Devise使用Google實作登入 [iterm2] 如何新增alias 一個新鮮人找尋Rails工作的面試經驗 如何讓兩個資料表建立關聯 routing 的 namespace strong parameter user story 的格式 user story 是什麼?
Rails 中的欄位及方法命名原則
Nic Lin · 2019-05-03 · via Nic Lin's Blog

Computer Science 中最難的兩件事情

  1. cache invalidation
  2. naming things

不過 Ruby / Rails 本身就有一些基本的慣例及約定在,只要熟悉這個規則通常都能夠寫出易懂易維護的程式碼。

就像我們呼叫 created_at 欄位時,腦袋會默認他就是 datetime,而不會是 string

以下是一些常用的慣例,分別是

  • Database column name
  • Method name
  • Library name

Column 欄位命名

使用過去分詞 + “at”

時間欄位請使用 Date / Datetime,嚴禁使用 String

被驗證的時間不應該使用 activated_time 而應該使用 activated_at。 因為 time 是表示時間(時間是指什麼時候還是花多久時間?),不是「什麼時候被驗證」。

example:

  • submitted_at
  • paid_at
  • done_at
  • created_at
  • updated_at

使用 “is” + 形容詞

欄位請使用 Boolean,這樣一來 Rails 可以直接使用帶有 ? 的 method

假設欄位名稱為 is_admin 屬性為 Boolean 時,可以直接呼叫 user.is_admin?

The #{attribute}? method is a method defined automatically by Active Record

example:

  • is_admin
  • is_activated
  • is_hidden
  • is_locked
  • is_enterprise

Method 方法命名

使用動詞 + 名詞表示要做的事情

把該做的事情放到對的地方,不要都在 controller 做

# Bad

class UsersController < ApplicationController
  def action
    user.token = SecureRandom.hex(5)
    user.save
  end
end

應該放在 model 裡面用 method 包起來,並敘述是做什麼事

# Grate

class User < ApplicationRecord
  def generate_token
    update_attribute(:token, SecureRandom.hex(5))
  end
end

請注意,名詞 method 不要有小動作,嚴禁偷動手腳,要操作資料務必另外寫

# Bad
class User < ApplicationRecord
  def full_name
    name = "#{first_name} #{last_name}"

    update_column(:fullname, name)

    name
  end
end

method 是名詞時,就讓他回傳單純的東西就好,例如 Array, String, Integer, Object

# Great

class User < ApplicationRecord
  def full_name
    "#{first_name} #{last_name}"
  end
end

使用 ! 結尾的 method 表示會做一些改變

  • Ruby method 會預期改變原有的 object 狀態
    • String 的 gsub! 與 gsub 是不同的結果與作用。
  • Rails 中的 ActiveRecord method (create!, save!) 失敗會 throw exception
class Cart < ApplicationRecord
  def clean!
    item.delete_all
  end
end

Class 命名

一律採用中性命名

Library

Library 是提供一個介面讓其他人可以呼叫

例如常用的第三方服務 OneSignal 推播通知

用 OneSignal(one_signal.rb) 是比較好的

不需要用

  • OneSignalSender(one_signal_sender.rb)
  • OneSignalWrapper(one_signal_wrapper.rb)

或是常用的 Telegram 機器人

用 TelegramBot(telegram_bot.rb) 是比較好的

不需要用

  • TelegramBotSender(telegram_bot_sender.rb)
  • TelegramBotWrapper(telegram_bot_wrapper.rb)

Wrapper

wrapper 應該是包裝了一些方法讓人呼叫

class OMDBWrapper
  include Singleton

  class << self
    delegate :get_movie_by_title, to: :instance
  end

  def initialize
    @omdb_client = OMDBClient.new
  end

  def get_movie_by_title(title)
    movie_attributes = omdb_client.
      get_movie(t: title).
      deep_transform_keys(&:underscore)
    Movie.new(movie_attributes)
  end

  private

  attr_reader :omdb_client
end

# OMDBWrapper.get_movie_by_title('The Rock') # =>
# <OMDBWrapper::Movie:0x00007fc6f5b7b290 @runtime="136 min", @title="The Rock", @year=1996>

Sender

sender 的職責是呼叫其他的 services wrapper,像是 Rails mailer 一樣,透過 mailer 去呼叫其他外部程式

例如

module NotificationSender
  def send_to_user
    # code
  end

  def send_to_vendor
    # code
  end
end

參考資源