Open
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
First things first: this PR does not introduce any change in functionality. I just found the code hard to read because of the many nested
if's and other non-idiomatic code choices, so I simplified the following snippets of code (these are examples). I also cleaned upgo.modwithgo mod tidybecause it contained some unneeded dependencies and the code has been linted and formatted withgofumptandgolangci-lint.Some notes before the code:
examples/were also formatted, linted and modified per the rules listed belowgo get -uor anythingconnectContext.errorwas renamed toconnectContext.err(inwpa-connect.go) because the linter didn't like itTop-level
ifcheck forself.ErrorThe function can just return early if
self.Erroris not nil, instead of having a big code block inside a hugeif.becomes
if call := ...; call.Err = nilwith emptyifblockSince
self.Errorwill benilanyways and we care only about the value ofcall.Err, we don't need to check wether it'snilor not and just assign it straight toself.Error.becomes
Function named return simplification
In several cases, using unnamed return values can improve readability of a function, because it requires all the returned values be explicitly on a single line,
nils included.(This is more of a personal opinion, though).
can be simplified into
Removed iteration over
mapretrieved from DBusSelf explanatory. Since the code iterates over all the keys of a map with a nested
if, and maps can only have a single value bound to a specific key, the code can be simplified to a straight map-value-ok check. (Variables have also been renamed to proper names instead ofvalueandkey)becomes
Use
deferfor closing connections and signal waitingUsing
deferis very good practice when handling connection cleanup and similar boilerplate: a deferred function will always run, even on early return. This prevents memory leaks and also makes the code somewhat cleaner.becomes
Types and
New*function moved to beginning of the fileSelf explanatory. Makes type definition easier to find just by opening the file.