Consistency / Ease of refactoring / Commit lines discussion
See original GitHub issueThis is expanding on #74. Sorry if this is bike-shedding or if this has already been discussed in-depth. Feel free to close for any reason.
I think priority should be given to consistency (no surprises / reading and understanding code is “easier”), ease of refactoring, and reduced git-commit lines at the expense of what initially could be viewed as “looking” better.
Some of the changes below are probably too strict and / or out of scope.
Here are some examples / proposed changes.
-
Always expand objects / arrays. it is easier and more consistent to add/remove/update fields when objects are forced into always being on a new line. Similar to the reason to always include a trailing comma, it reduces commit messages making changes cleaner to review.
-
Do not omit parens with single input arrow functions. Added for consistency / ease of refactoring. It required when used with Flow / Typescript to add types and also hinders refactoring to add fields / change to deconstructing.
-
Ternary always on their own line
-
Deconstructed exports
-
If statements with && or || operators split into multiple lines
Exceptions (possible bike-shedding?): The following would, I think, get in the way of reading and writing code.
- import deconstructing
- Function params
- if statements without && / || operators
Given the following input (default prettier options + trailingComma):
import SomeModule, { test } from 'some-module';
const arr = ['printWidth', 'tabWidth'];
const obj = { first: 'one' };
const arrObj = [{ second: 'two' }, { third: 'three' }];
const otherObj = [
{ other: 1 },
{ o: 1 },
{ other: 1 },
];
const arrowFn = one => { return one };
function fn(one) {
return one;
}
if (false) {
console.log('never');
}
if(true && 'false' === 'false') {
console.log('true');
}
const tern = obj ? '1' : '2';
export { otherObj };
export default arrowFn;
Would output:
import SomeModule, { test } from "some-module";
const arr = [
"printWidth",
"tabWidth",
];
const obj = {
first: "one",
};
const arrObj = [
{
second: "two",
},
{
third: "three",
},
];
const otherObj = [
{
other: 1,
},
{
o: 1,
},
{
other: 1,
},
];
const arrowFn = (one) => {
return one;
};
function fn(one) {
return one;
}
if (false) {
console.log("never");
}
if (
true &&
"false" === "false"
) {
console.log("true");
}
const tern = obj
? "1"
: "2";
export {
otherObj,
};
export default arrowFn;
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:16 (9 by maintainers)
For reference, here is a diff example (although for objects, not for arrays): http://eslint.org/docs/rules/object-property-newline
@jlongster You used the words “much nicer” a couple times. This is exactly what I was trying to address with this issue. I think stylistic choices should decided on what is best for long-term code quality / maintenance instead of what is easier to initially write / nicer to initially look at.
You are right, adding an element is just as easy. What is more difficult is:
/* */
), or you have to modify the element to be on a separate line.I think that is a lot to give up when the only reason to keep it one line is so it initially looks better, which is subjective / personal preference / whatever you are used to.
I agree. The majority of exceptional cases should be avoided.
For why expanded exports and not imports: I think it is much more likely for an export to be added/removed/changed/commented than an import. I think import lines are typically static, and aren’t exactly considering the “meat” of the javascript file. But this was an initial response and possible “bike-shedding” / personal preference / experience.
It is marginally “nicer” to look at, which is really just personal preference. It is confusing for people that are unfamiliar with arrow functions.
It is not consistent / easier to change later. Every other way to write that function requires parens. If parens were added, you can still write the function without parens. Less keys to start, and have prettier add them when formatting. Win-win
To recap, I think stylistic choices should be made to promote consistency, refactoring, and code sharing/reviewing over what could be considered as looking nicer.