-
Notifications
You must be signed in to change notification settings - Fork 78
[DX] Add code style guide #12359
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
Open
Luke-Oldenburg
wants to merge
3
commits into
main
Choose a base branch
from
lro-add-code-style-guide
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[DX] Add code style guide #12359
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| # Code Style Guide | ||
|
|
||
| This document serves to instruct on how to style code for things the CI does not enforce. If the CI does not enforce a particular style and it is not mentioned here feel free to write it how you would like. | ||
|
|
||
| ## Partials | ||
| There are some rules we are trying to enforce for partials. You may come across partials that do not follow these rules. Feel free to leave those alone but any new partials you write should follow by these rules. | ||
|
|
||
| ### Render calls should use the full path | ||
|
|
||
| This makes it both easier to understand what is being rendered and also improves performance by reducing the amount of searching Rails needs to do. | ||
|
|
||
| **GOOD:** | ||
| ``` | ||
| <%= render "events/nav" %> | ||
|
|
||
| <%= render "stripe_cards/stripe_card", stripe_card: @stripe_card %> | ||
| ``` | ||
| **BAD:** | ||
| ``` | ||
| <%= render "nav" %> | ||
|
|
||
| <%= render @stripe_card %> | ||
| ``` | ||
|
|
||
| ### Use strict locals instead of local_assigns or instance variables | ||
|
|
||
| Strict locals provide an easy to reference comment at the top of the file of which variables are needed. This will also raise an error if you forget to pass a variable, increasing the reliability of code written. If you have an optional local, you should still use strict locals but pass nil as the default value. | ||
|
|
||
| This is superior to instance variables because of the added rigidity to your code that it will fail immediately if a local is not passed. When you use instance variables, you don't always know that it will be defined depending on where the partial is rendered from. | ||
|
|
||
| **GOOD:** | ||
| ``` | ||
| <%= locals: (example_local: "") %> | ||
|
|
||
| <%= example_local %> | ||
Luke-Oldenburg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``` | ||
| **BAD:** | ||
| ``` | ||
| <%= example_local %> | ||
| ``` | ||
|
|
||
| *Why use strict locals over local_assigns?* | ||
|
|
||
| Strict locals with nil defaults function similarly to local_assigns with the added benefit of raising an error if you forget to pass a local when no default is set. | ||
|
|
||
| ## Presence | ||
| `presence` is a super useful function to return the object if it is present (not nil or blank); otherwise, it returns nil. | ||
|
|
||
| It is advised to use this function over using a ternary with `present?` to improve readability. | ||
|
|
||
| **GOOD:** | ||
| ``` | ||
| @event_id = params[:event_id].presence | ||
| ``` | ||
| **BAD:** | ||
| ``` | ||
| @event_id = params[:event_id].present? ? params[:event_id] : nil | ||
| ``` | ||
|
|
||
| ## Capitalization | ||
| Most of the UI uses sentence case. | ||
|
|
||
| **GOOD:** | ||
| ``` | ||
| "Get reimbursed" | ||
| ``` | ||
| **BAD:** | ||
| ``` | ||
| "Get Reimbursed" | ||
| ``` | ||
|
|
||
| ## Tables | ||
| If you have a column with an empty `<th>` tag, for example, a logo column, please put a comment inside of the `<th>` explaining what it is for. | ||
|
|
||
|
|
||
| **GOOD:** | ||
| ``` | ||
| <tr> | ||
| <th><%# icon %></th> | ||
| <th>Status</th> | ||
| <th>Date</th> | ||
| <th>To</th> | ||
| <th>For</th> | ||
| <th class="text-right">Amount</th> | ||
| <th><%# details button %></th> | ||
| </tr> | ||
| ``` | ||
| **BAD:** | ||
| ``` | ||
| <tr> | ||
| <th></th> | ||
| <th>Status</th> | ||
| <th>Date</th> | ||
| <th>To</th> | ||
| <th>For</th> | ||
| <th class="text-right">Amount</th> | ||
| <th></th> | ||
| </tr> | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.