-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] Vuex naming conventions #2069
Comments
I'm not sure about the following set of rules:
The rest is fine with me; @filrak |
In my case 100% agree with @patzick . PLUS: Our naming should be predictive and (if possible) very similar across modules for same tasks (fetching data from server/cache, filtering products on category/search, fetching single category/product etc) so by invoking similar action/getter on other module you more or less know how it works and learning curve is natural. This should be true especially for actions/getters params. Regarding your concerns
Usually js vars and properties aren't snake-cased. Maybe we can just keep the state like this but use proper camelCase for getters?
Imho we should do this anyway. If sth can be updated in a minute via simple search&replace i don't think backward-compatibility will be a problem there. @patzick point was to avoid conflicting actions under the same namespace when you use |
Okay, to finish this up we established two things during our last talk:
And most important fact, which i think was missed here - this is not a plan for refactor. This is naming convention, which we'll use for future reference, when we'll be planing refactor or adding new features. @pkarw, @filrak guys, is it everything to close this? Where do we want to publish it after it's finished? |
@patzick: positive. Please go on with such a naming convention. I believe we should update docs including the coding guidelines there + we should mention it with link in CONTRIBUTING.md |
I've updated docs with PR #2049 |
This is nice and all but, forgive my frustration, @vue/typescript/recommended is ENFORCING using camelCase in state variable names. Can't do anything about this, so right now I'm pondering should I either drop ESLint or TypeScript entirely. Or both. I'm really sorry but it's 3 in the morning and I spent last hour trying to sort this out. |
@patzick then how can i access state of module named including dash sign? |
Vuex conventions
We need to establish some rules for vuex. It’s a proposition, which after some discussion will be added to our docs for possible referencing in future.
Module
Vuex module should be created for specific set of functionalities. Should also have only absolutely necessary dependencies to other modules.
Name of module should be short, quite clear about it’s destination and has words separated by dash.
Good examples:
Bad examples:
State
State properties should be simple and their structure should not be nested. Their names are written in camelCase notation and indicates what they are containing.
We should avoid to have more than one instance of object, even between modules. In the vast majority of cases they can be referenced by it’s unique id property. Example:
Good examples:
Bad examples
Getters
Vuex state, except of mutations, should always be accessed by getters. Including actions.
Getter should:
is
when returns Boolean, orget
otherwisewhat am I returning?
So for example we have module
category
and in stateavailableFilters
. Sowhat am I returning?
->available Filters
. And this filters arecategory filters
. Its not a Boolean, it’s array or map so we’re starting withget
->getAvailableCategoryFilters
Good examples:
Bad examples:
Actions
It’s a heart of logic for module. Every state change from outside of module should be invoked as an action. Actions are meant to:
Their names should most possibly be unique and in simple way says what specific action is doing. Almost every action should return promise
Good examples:
fetchProduct
, mutate it to productsMap and mutate its id to currentProductId. Also if productId is null then it removes currentProduct.Bad examples:
Mutations
Finally we have mutations. Only mutations can change state of module. They should be synchronous (never returns promise), not contain any logic (be extremely fast) except one needed to keep state as it should be (for example sets default value for state). Mutations should be invoked only by actions from the same module. In most cases it should be only a single action which invokes specific mutation.
Types of mutation:
Good examples:
Bad examples:
Thats it ;)
It’s a first draft proposition. It’ll be updated after discussions and at the end we will add it to our docs.
Please share your thoughts, every input is welcome and needed. We need to establish this ground rules to get clear and common vision for where are we're going with Vuex.
The text was updated successfully, but these errors were encountered: