-
Notifications
You must be signed in to change notification settings - Fork 20.4k
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
swarm/network/simulation: fix New function for-loop scope #18161
Changes from all commits
06e9562
f0da48c
ae922ff
f4958c0
ea62076
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -205,3 +205,16 @@ func (t *noopService) Start(server *p2p.Server) error { | |
func (t *noopService) Stop() error { | ||
return nil | ||
} | ||
|
||
// a helper function for most basic noop service | ||
// of a different type then noopService to test | ||
// multiple services on one node. | ||
func noopService2Func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) { | ||
return new(noopService2), nil, nil | ||
} | ||
|
||
// noopService2 is the service that does not do anything | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not just embed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch, changed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, agree, nicer. |
||
// but implements node.Service interface. | ||
type noopService2 struct { | ||
noopService | ||
} |
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.
Good catch. How did that ever work with multiple services...?
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.
I suppose that we never started all services up until now, and newly created TestAddNodeMultipleServices was a missing test case from start.
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.
I don't know about the others, but I find this notation confusing. I would prefer different names in the inner scope. I believe I even deleted something similar to this somewhere else in the code a while ago, because I didn't realize what it did. (I wish now I remember what and where that was)
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.
I totally disagree. Are you saying that we should change a common approach in go because you are not familiar with it? There is even a comment above. I am also interested what others think.
With different variable names, the original ones are still available and can be used instead the new ones which opens the possibility for the same issue that should be solved with scoping. With the same variable names there is no such risk.
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.
No, that's not what I'm saying. You are right: I didn't even know it was a common approach in
go
. I'm sure I should have.What I am saying is I actually deleted code like this before because it looked nonsensical, and I'm pretty sure it even slipped through code review, which it shouldn't have if it was glaringly obvious. So let's make sure everyone in the team recognizes this trick. Maybe the comment is enough, I dunno.
Apologies for my ignorance.
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.
Thanks @nloash. If you have any suggestions how the comment can be improved, please provide it.
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.
I've posted in hq to raise awareness.
I understand the convenience, but I really don't like the style. I believe we should explicitly know the vars we use. A comment would serve the same purpose for the contrary argument.
But then, as you say, who am I to contend with the perfect coder conditioning oracle that is Google :)
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.
Great, thanks. :)
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.
I think comment is fine for this notation. We should be aware of this construct and just raise awareness of it. I personally would not add a comment if I write code that needs to scope the loop vars.
https://github.com/golang/go/wiki/CommonMistakes