Avoid aligning keys and values in source code unnecessarily
You are reviewing a PR. You see this diff. What has changed?
user := &User{
- ID: uuid.NewString(),
- Username: username,
- EmailAddress: emailAddress,
- PasswordHash: bcrypt(password),
- IsActive: true,
- LoginCount: 0,
- LastLogin: time.Now(),
- Role: defaultRole,
- CreatedAt: time.Now(),
- EmailVerified: false,
+ ID: uuid.NewString(),
+ Username: username,
+ EmailAddress: emailAddress,
+ PasswordHash: bcrypt(password),
+ IsActive: true,
+ PwLastRotatedAt: time.Now(),
+ LoginCount: 0,
+ LastLogin: time.Now(),
+ Role: defaultRole,
+ CreatedAt: time.Now(),
+ EmailVerified: false,
}
This diff only adds a single line. With a single “+” line that’d be easy to see.
By formatting the code like this, adding or removing a single long field causes “write amplification”: all the other lines needlessly change as well.
This causes:
- PRs to be more difficult to review, as you can’t as easily see what’s been added or removed, and
- More merge conflicts, as one author adding a line and another changing an unrelated line in the block can’t be merged by Git automatically any more.
Aligning values is the convention in Go (golang), enforced by the Go formatter (gofmt).
There is no readability benefit to this. Java and other languages get by just fine without doing this. Even if there were a readability benefit, it doesn’t outweigh the increased difficulty/mistakes during review, and the increased effort/mistakes during merging.
If you are creating a programming language, or code formatter, don’t do this.