makingsense / code-style-guides Goto Github PK
View Code? Open in Web Editor NEWCode style guides
Code style guides
Inside the JavaScript section, create folders for frameworks / libraries, include one for AngularJS.
This seems like a good start:
In the JS ES6 code style guidelines, we have the following recommendation:
23.2 Do not use JavaScript getters/setters as they cause unexpected side effects and are harder to test, maintain, and reason about. Instead, if you do make accessor functions, use getVal() and setVal('hello').
// bad
class Dragon {
get age() {
// ...
}
set age(value) {
// ...
}
}
// good
class Dragon {
getAge() {
// ...
}
setAge(value) {
// ...
}
}
However, I don't really see the problem. The hiccups I see with using ES6 getters and setters are:
this.age
will actually invoke the getter function and this.age = 15
will actually invoke the setter function. So in order to preserve a value there's a need for an internal (private! ๐) variable. This is also similar in typed languages.Outside of that, there are benefits to this approach (aside from the OOP class design I mentioned earlier): (taken from this SO question)
Proposal: remove that guideline. Allow developers to use getters and setters as much as they want.
Hi!
We currently have this guideline in the JS ES6 docs:
3.5 Group your shorthand properties at the beginning of your object declaration.
Why? It's easier to tell which properties are using the shorthand.
const anakinSkywalker = 'Anakin Skywalker';
const lukeSkywalker = 'Luke Skywalker';
// bad
const obj = {
episodeOne: 1,
twoJediWalkIntoACantina: 2,
lukeSkywalker,
episodeThree: 3,
mayTheFourth: 4,
anakinSkywalker,
};
// good
const obj = {
lukeSkywalker,
anakinSkywalker,
episodeOne: 1,
twoJediWalkIntoACantina: 2,
episodeThree: 3,
mayTheFourth: 4,
};
I think we should not make such recommendation, here's why:
All things being equal, any order will be the same. I don't think having shorthand properties at the top brings any particular value: you still need to read that particular line to understand that it is a shorthand property, regardless of it being in the beginning or the end of the object.
However, in the real world usage, there are certain affinities between methods / properties that have more cohesion than others, and we should allow developers to add them in an order that makes more sense for a mental model of the object.
For example:
const invoice = {
user: getUser(userId),
billingAddress,
shippingAddress,
shippingMethod: getUserChoice(Constants.SHIPPING_METHOD)
items: itemsForInvoice,
subtotal: subtotalForInvoice,
taxer: taxCalculatorFunction,
total: currentTotal
};
Note how certain properties belong together for the metal model that we have of an invoice. The blank spaces help construct that mental model. Properties being put before before shorthand / alphabetical order / being a function or object is actually a criteria that does not help code readability, but gives an arbitrary decision to follow.
So, my proposal is: let's just remove this guideline.
What do you think?
It seems there is a new official style guide from Microsoft: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions
It would be nice to add a reference at least.
We are currently adding the reference to the styles even when we prefer the opposite style. We should make it clear that are only a reference and the value we choose for that style is on the attached tooling files.
As is right now, it looks like an error.
Good suggestions to start with:
I know this could turn into a tabs vs. spaces debate but here goes:
The C# style guide links to SA1309 as a means to argue that you should name your private fields starting with underscore instead of using this.
prefix, however; SA1309 (which name is FieldNamesMustNotBeginWithUnderscore
) states that you should never use underscores for field names and you should in fact use the this.
prefix instead.
Also; the General Naming Convention for the .Net Platform Guide states: "X DO NOT use underscores, hyphens, or any other nonalphanumeric characters."
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.