-
Notifications
You must be signed in to change notification settings - Fork 949
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
refactor: use return fast to make code readable #2373
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2373 +/- ##
=========================================
+ Coverage 64.14% 68.2% +4.06%
=========================================
Files 275 265 -10
Lines 18261 18213 -48
=========================================
+ Hits 11713 12422 +709
+ Misses 5275 4361 -914
- Partials 1273 1430 +157
|
pkg/netutils/interface.go
Outdated
if err != nil { | ||
return nil, err | ||
} | ||
if memberOf(ip, family) { |
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.
According to the logic, I consider that there is an alternative method which I usually used to write:
if ! memberOf(ip, family) {
glog.V(4).Infof("%v is not an IPv%d address", ip, int(family))
continue
}
if ip.IsGlobalUnicast() {
glog.V(4).Infof("IP found %v", ip)
return ip, nil
}
glog.V(4).Infof("Non-global unicast address found %v", ip)
I recommend this code style because it can deduce the nested level, but it seems like that the readability perhaps has a decrease.
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.
That makes sense. @fengzixu
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.
LGTM. Could you also refactor memberOf
to fast return pattern ?
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.
@fengzixu Yes, it is more idiomatic 👍
fedc003
to
15f4d74
Compare
Signed-off-by: Allen Sun <[email protected]>
15f4d74
to
33c8abd
Compare
Thanks for your review. I have updated that part. @xiaoxubeii @zhuangqh @fengzixu |
LGTM |
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.
LGTM
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.
LGTM
Signed-off-by: Allen Sun [email protected]
Ⅰ. Describe what this PR did
use return fast to make code readable.
When I was reviewing pr #2367. I found that some code should be refactored. While the package
pkg/netutils
is imported by @YaoZengzeng in pr #2040.And I found that in this package, log package is using
"github.com/golang/glog"
ratherlogrus
. Do we need to make this package to use logrus? @YaoZengzengⅡ. Does this pull request fix one issue?
none
Ⅲ. Why don't you add test cases (unit test/integration test)? (你真的觉得不需要加测试吗?)
none
Ⅳ. Describe how to verify it
none
Ⅴ. Special notes for reviews
none