Skip to content
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

Merged
merged 1 commit into from
Nov 1, 2018

Conversation

allencloud
Copy link
Collaborator

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" rather logrus. 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

@codecov
Copy link

codecov bot commented Oct 28, 2018

Codecov Report

Merging #2373 into master will increase coverage by 4.06%.
The diff coverage is 100%.

Impacted file tree graph

@@            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
Flag Coverage Δ
#criv1alpha1test 31.44% <50%> (?)
#criv1alpha2test 35.63% <50%> (-0.3%) ⬇️
#integrationtest 39.55% <0%> (-0.24%) ⬇️
#nodee2etest 32.98% <50%> (-0.35%) ⬇️
#unittest 25.53% <100%> (+0.02%) ⬆️
Impacted Files Coverage Δ
pkg/netutils/interface.go 95.87% <100%> (+6.93%) ⬆️
daemon/daemon.go 56.15% <0%> (-11.41%) ⬇️
daemon/mgr/snapshot.go 89.85% <0%> (-4.35%) ⬇️
apis/server/container_bridge.go 86.11% <0%> (-1.55%) ⬇️
cri/v1alpha2/cri_wrapper.go 60% <0%> (-1.21%) ⬇️
daemon/mgr/container.go 58.95% <0%> (-1.07%) ⬇️
ctrd/container.go 59.28% <0%> (-0.48%) ⬇️
cri/v1alpha2/cri_utils.go 90.57% <0%> (-0.24%) ⬇️
cri/v1alpha2/cri.go 68.07% <0%> (-0.22%) ⬇️
daemon/config/config.go 47.94% <0%> (ø) ⬆️
... and 32 more

if err != nil {
return nil, err
}
if memberOf(ip, family) {
Copy link
Contributor

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. @fengzixu

Copy link
Contributor

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 ?

Copy link
Contributor

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 👍

@allencloud
Copy link
Collaborator Author

Thanks for your review. I have updated that part. @xiaoxubeii @zhuangqh @fengzixu

@zhuangqh
Copy link
Contributor

zhuangqh commented Nov 1, 2018

LGTM

Copy link
Contributor

@fengzixu fengzixu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@fuweid fuweid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zhuangqh zhuangqh merged commit c835be8 into AliyunContainerService:master Nov 1, 2018
@allencloud allencloud deleted the return-fast branch November 4, 2018 05:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants