o9s
is a microblog
home archives search feed blogroll


Stop Comma-leading

What is it?

The comma-leading syntax is used to make diffs shorter and simpler.

function myFunction(
  x,
  y,
  z
);

In this case, if the parameter a is added to myFunction, the diff might look like this.

function myFunction(
  x,
  y,
- z
+ z,
+ a
);

Now let’s write the function this way.

function myFunction(
  x
  ,y
  ,z
);

Once again, let’s add the parameter a to myFunction.

function myFunction(
  x
  ,y
  ,z
  ,a
);

Here’s the new diff.

function myFunction(
  x
  ,y
  ,z
+ ,a
);

The diff is shorter. It fixes an issue, but creates an ugly syntax. There is also an argument that states that it makes it easier to point out missing commas. I don’t agree that it’s ever an issue.

The new alternative

There is a proposal for trailing function commas in JavaScript. We will be able to append an extra comma to the last parameter of a function. It will look like this. Notice the comma after the z.

function myFunction(
  x,
  y,
  z,
);

If we add the parameter a. The diff will look like this.

function myFunction(
  x,
  y,
  z,
+ a,
);

The diff is short and simple and the syntax is not as ugly.

Posted on 2018-01-23   #javascript  






← Next post    ·    Previous post →