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.mod
withgo mod tidy
because it contained some unneeded dependencies and the code has been linted and formatted withgofumpt
andgolangci-lint
.Some notes before the code:
examples/
were also formatted, linted and modified per the rules listed belowgo get -u
or anythingconnectContext.error
was renamed toconnectContext.err
(inwpa-connect.go
) because the linter didn't like itTop-level
if
check forself.Error
The function can just return early if
self.Error
is not nil, instead of having a big code block inside a hugeif
.becomes
if call := ...; call.Err = nil
with emptyif
blockSince
self.Error
will benil
anyways and we care only about the value ofcall.Err
, we don't need to check wether it'snil
or 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,
nil
s included.(This is more of a personal opinion, though).
can be simplified into
Removed iteration over
map
retrieved 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 ofvalue
andkey
)becomes
Use
defer
for closing connections and signal waitingUsing
defer
is 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.