-
Notifications
You must be signed in to change notification settings - Fork 8
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
AG-NONE Cleanup destroy #1571
Closed
Closed
AG-NONE Cleanup destroy #1571
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dd2686b
Clean up memory management in ChartContext
olegat 8ede733
Fix SonarLint errors
olegat 132151c
CR: Add empty destroy() methods
olegat 3af3262
Specify that these implement destroy() as part of an interface
olegat 9c86fc5
nx format
olegat File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,15 @@ | ||
export type Destroyable = { destroy(): void }; | ||
|
||
export class ObjectDestroyer { | ||
private readonly objs: Destroyable[]; | ||
constructor(...objs: Destroyable[]) { | ||
this.objs = [...objs].reverse(); | ||
} | ||
destroy() { | ||
this.objs.forEach((o) => { | ||
if ('destroy' in o && typeof o.destroy === 'function') { | ||
o.destroy(); | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These inline assignment and returns are not very readable, can you adjust the pattern here to avoid this and the need for
// NOSONAR
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is readable.
Sonar argues that assignments in expressions are discouraged is because they can have an unexpected side-effect. For example:
In this scenario, it is not clear what the programmer meant:
isEnabled
istrue
?isEnabled
is true and x is assigned to a true-y value?==
/===
instead of=
?That can be confusing, because
x
might be assigned or it might not, anddoSomething()
might be called or it might not.However, this argument is not applicable in this case. How can
(this.annotationManager = new AnnotationManager(chart.annotationRoot)), // NOSONAR
possibly be misunderstood? There no other logical operators like&&
and||
so there is only 1 possible outcome from this expression.Also, it is abundantly clear the intent is to assign a value and we didn't intend
==
or===
(because this expression is part ofChartContext
's constructor and also why would we want to compare an unassigned value to a newly constructed object?).The aim objective of
ObjectDestroyer
is to follow a RAII model to ensure that:The only option to avoid the
// NOSONAR
could be to use lambdas methods thatObjectDestroyer
can call:However, this will violate objective 1). We could end up accidentally creating a circular dependency amongst constructors.
We could also apply the changes that Sonar suggests, which involve putting the assignments as separate expressions
However, this is not a good suggestion because objective 2) is to ensure that destructors are run in reverse. But with this approach we need to manually ensure the ordering is reversed, which is basically what we're doing now. So we might as well not bother adding
ObjectDestroyer
if we're going with this approach.It is a pain to have to write
// NOSONAR
in each line. I would much prefer multi-line enable/disable options but I can't find something like that. Open to other suggestions to disable the Sonar warnings, but Sonar is wrong in this case.