You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

请问通过动态调整空格让属性起始列对齐的代码格式叫什么?

Aligned Assignment (Column-Aligned Key-Value Pairs)

Hey there! That formatting style you're asking about—where you add spaces before attributes so all assignment markers line up in the same column—is commonly called aligned assignment (or sometimes column-aligned key-value pairs).

The core idea here is to boost code readability: by vertically lining up characters like : (or = in other languages), you can quickly scan and match each key/property to its corresponding value, which is especially helpful when working with blocks of multiple entries.

Example 1: Object Literal

Your original unaligned snippet:

Object: {
  name: "Jason",
  age: 28,
  gender: "male"
}

After aligned formatting:

Object: {
  name  : "Jason",
  age   : 28,
  gender: "male"
}

Example 2: Ruby FactoryBot Definition

Your original unaligned code:

FactoryBot.define do
  factory :company do
    email { FFaker::Internet.email }
    phone { FFaker::PhoneNumber.short_phone_number }
    status { :processing }
    name { FFaker::Company.name }
    identity_no { FFaker::Identification.ssn }
  end
end

With dynamic alignment based on the longest key:

FactoryBot.define do
  factory :company do
    email         { FFaker::Internet.email }
    phone         { FFaker::PhoneNumber.short_phone_number }
    status        { :processing }
    name          { FFaker::Company.name }
    identity_no   { FFaker::Identification.ssn }
  end
end

Lots of code formatters (like RuboCop for Ruby, or Prettier with custom config for JavaScript) can automatically apply this style, adjusting spacing dynamically based on the longest key/property name in the block—exactly like the FactoryBot example you shared.

内容的提问来源于stack exchange,提问作者Hegwin

火山引擎 最新活动