DEV Community

loading...

Discussion on: What programming best practice do you disagree with?

Collapse
jckuhl profile image
Jonathan Kuhl

C# Style guidelines. I don't like having the opening bracer on its own line:

Don't like

public static void DoTheThing()
{
   Thing theThing = new Thing();
   theThing.do();
}

Do like

public static void DoTheThing() {
   Thing theThing = new Thing();
   theThing.do();
}

Also, I refuse to have a dangling comma.

const favoriteThings = [
   "whiskers on kittens",
   "bright copper kettles",
   "warm woolen mittens",
   "brown paper packages tied up with strings",
];

Ugh. No. Gross. I think internally it makes my brain expect something to follow the last item, but nothing does.

Collapse
michaeldober profile image
Michael Ober

For functions and methods, including anonymous lambda functions, I keep the opening brace on the next line by itself. For objects and everything I put the opening brace on the line with the declaration.

Will 'favoriteThings' even compile with that final comma?

Collapse
jckuhl profile image
Jonathan Kuhl

In JavaScript, the final comma is optional. In many other languages, that might not be the case.

Collapse
metruzanca profile image
Samuele Zanca

Oof, 6 months old buuuuttt. Yes, it does compile. Just like js, it's there for convenience when editing. You can't however do that in object initializers e.g.:

var obj = new MyClass
{
  item1, //shorthand if same name
  item2 = item2, //<—this comma gives error
};

(on mobile, fingers crossed for formatting)

Collapse
256hz profile image
Abe Dolinger

Old thread, but in JS, the dangling comma is actually mandatory in our style guide. It makes edits easier - you can reorder or delete the last item without changing other lines. I think your point about expecting something else is valid.