Skip to content

Refactor to decrease code duplication and complexity #301

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

Merged
merged 10 commits into from
Jun 3, 2020
Merged

Refactor to decrease code duplication and complexity #301

merged 10 commits into from
Jun 3, 2020

Conversation

ericcornelissen
Copy link
Contributor

This refactors the code base based on the issues from Code Climate regarding code duplication and code complexity. This should get us from a C to a (not perfect) A 😃

When reviewing this Pull Request, I'd suggest going through the commits one-by-one to see if you agree with every refactoring step that I took. I think that looking at the final diff may be a bit confusing and the motivation for certain refactoring choices may be unclear.

Simplify and reduce duplication in the tool-bar-button-view component, 
specifically in the `setEnabled` and `setSelected` methods by using 
`classList.toggle()` [1] instead of `classList.add` and 
`classList.remove` with an if-statement.

--
1. 
https://developer.mozilla.org/en-US/docs/Web/API/Element/classList#Examples
Update the `updatePosition()` method of the tool-bar-view to set the 
`tool-bar.position` configuration itself.

This presents any issue due to the caller forgetting to set it 
themselves and reduced code duplication when adding commands for the 
package.
Refactor the common logic out of the find function into a separate 
function, making the find logic easier to understand and reduce code 
duplication.
Add a method to the ToolBarView class to refresh the toolbar. This 
method is now used when the `tool-bar.position` and `tool-bar.fullWidth` 
configuration values changed.

This aims to reduce code duplication and make it explicit what happens 
in these anonymous functions.
Simply the `getTooltipPlacement` function by refactoring the nested 
ternary operators into an object keyed by the toolbar position value.
Refactor the constructor of the ToolBarButtonView class in order to 
reduce its length and cognitive complexity.
Update the implementation of raf-debounce to reduce it's length and 
reduce complecity. This is achieve primarily through the removal of 
if-statements that are "unnecessary" in the sense that everything still 
works when they're removed.

Most notably, there is no longer a check that the requestID is set, as 
it is safe to call `window.cancelAnimationFrame` with `null` or 
`undefined`.
Reduce the complexity of the `updatePosition` method of the ToolBarView 
class primarily by extracting the logic that creates a new panel into a 
separate method.

This already reduced the length of the `updatePosition` method. The 
length is further reduced by rewriting the logic that sets the classes 
for the new panel element.
@suda suda merged commit 48cd604 into atom-community:master Jun 3, 2020
@ericcornelissen ericcornelissen deleted the improve-codeclimate-score branch June 3, 2020 13:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants