神刀安全网

Why you should enforce Dangling Commas for Multiline Statements

We all have different opinions on what code should look like. Most of the time it’s a matter of preference based on good arguments. When it comes to dangling commas I noticed most people didn’t like them until they understood the benefits. I didn’t either, until recently …

Before we dig into the benefits let’s briefly explore what I mean with dangling commas.

Code without dangling commas:

const fruits = [
'apples',
'banana' // <- no comma before the closing bracket
];
const godzilla = {
teeth: 213,
kind: 'giant monster' // <- no comma before the closing brace
};

Code with dangling commas:

const fruits = [
'apples',
'banana', // <- comma before the closing bracket
];
const godzilla = {
teeth: 213,
kind: 'giant monster', // <- comma before the closing brace
};

Benefits

At the first glance, it might look a bit weird. Let’s dig into it and find out why so many people use dangling commas.��

1. Clean Diffs

Clean diffs become increasingly more important as your codebase & team grows. Especially while reviewing Pull-Requests.

Why you should enforce Dangling Commas for Multiline Statements
Adding ‘pineapple’ to the fruits array

We want to add an entry to an array.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Why you should enforce Dangling Commas for Multiline Statements

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮