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

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
T
The Exploit Database - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
F
Fox-IT International blog
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
I
Intezer
P
Privacy & Cybersecurity Law Blog
B
Blog RSS Feed
Latest news
Latest news
小众软件
小众软件
A
Arctic Wolf
Attack and Defense Labs
Attack and Defense Labs
L
LINUX DO - 热门话题
博客园 - 聂微东
B
Blog
T
Troy Hunt's Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
Malwarebytes
Malwarebytes
爱范儿
爱范儿
Recorded Future
Recorded Future
Apple Machine Learning Research
Apple Machine Learning Research
人人都是产品经理
人人都是产品经理
D
Docker
T
Threat Research - Cisco Blogs
MyScale Blog
MyScale Blog
Martin Fowler
Martin Fowler
E
Exploit-DB.com RSS Feed
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
PCI Perspectives
PCI Perspectives
Scott Helme
Scott Helme
N
Netflix TechBlog - Medium
博客园 - 三生石上(FineUI控件)
T
True Tiger Recordings
C
Check Point Blog
Microsoft Azure Blog
Microsoft Azure Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
Security Latest
Security Latest
The Hacker News
The Hacker News
Microsoft Security Blog
Microsoft Security Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Stack Overflow Blog
Stack Overflow Blog
S
Security @ Cisco Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
M
Microsoft Research Blog - Microsoft Research

Nic Lin's Blog

謝明真 - 高效領導力的課後筆記 NFT 開發實戰!基礎智能合約入門 (3) NFT 開發實戰!基礎智能合約入門 (2) NFT 開發實戰!基礎智能合約入門 (1) 如何自我檢測 log4j CVE 漏洞 Rails 如何在資料寫入時記錄來源 IP 位置 如何經營工程師 Youtube 頻道 - Part 8 營收篇 如何經營工程師 Youtube 頻道 - Part 7 酸民文化篇 如何經營工程師 Youtube 頻道 - Part 6 演算法趨勢篇 如何經營工程師 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 這樣做(三) 2 個月擁有 6000 用戶 Side project 這樣做(二) 2 個月擁有 6000 用戶 Side project 這樣做(一) 如何讓自己成為失敗的軟體工程師 如何在 Jenkins 上用 Docker 跑 Rails + Rspec 做 CI 如何用 Rack::Attack 阻擋 DDOS / 惡意流量 用 OpenSSL 自簽開發用 HTTPS SSL 憑證 以 OOP 的角度提升 Ruby code 質量 不停機 migration 避免鎖表的幾種操作 為機器加上登入訊息,在 ubuntu 設置登入歡迎詞 Ruby Memoization 性能優化之記憶化 淺談 SSH agent forwarding 和 proxy command 的安全風險與應用 [Rails] Service / Library / Concern 的差異 Ruby conf Taiwan 2019 參與筆記 避免過度的 Defensive Programming 防禦性程式設計 Rails 要用 Time.zone.now 還是 Time.now Rails i18n 小技巧總匯 1:1 攪亂器,如何用 Ruby 做可逆推序號 [Rails] 用 puma-dev 作為本地開發伺服器 (支援 https 自簽憑證) 我的中高階 Rails 工作面試心得分享 讀書心得 - 「一流的人如何保持顛峰」 讀書心得 - 「窮查理的普通常識」 將 Rails 專案從手動部屬遷移使用 Capistrano 自動化部屬 工程師提昇自己的教學和簡報技術的方法 [筆記] Rails 3.2 升級 Rails 6.beta 經驗分享 Grape on Rails 實戰 101 Class method 氾濫帶來什麼問題 Rspec 中 let / let!(驚嘆號) / Instance variables / subject 的用法與差異 RDBMS 課程心得與筆記 常用的 Rails 開發規範 Rest-Client 如何做 Basic Authentication 驗證 用 ssh config 管理多台機器 [Rails] 實做批次操作的小技巧 [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)(一) if/unless 寫作不要用多重否定句啊 盡可能的減少使用具感染性的 Try 或是 lonely/safe navigation operator Load balance 負載平衡設計 ES6: export default 和 export 的差別 搞懂 React 中的 state 和 props
Rails 中的欄位及方法命名原則
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

參考資源