Is this as dangerous as I think it is?

John A Meinel john.meinel at canonical.com
Thu Oct 22 12:42:20 UTC 2015


While doing a review, I came across this snippet of code:
asserts = append(isAliveDoc, asserts...)

It took me a while to understand why the thing that is being appended is at
the beginning. I realize it probably wants to insert it at 0, but I thought
it was an object, not a slice, so how could it go at the beginning. But
digging shows it is actually a slice:

https://github.com/juju/juju/blob/master/state/life.go#L36
var isAliveDoc = bson.D{{"life", Alive}}

and bson.D:
http://bazaar.launchpad.net/+branch/mgo/v2/view/head:/bson/bson.go#L118
type D []DocElem

Doesn't that mean that every time we call something like this, we are
potentially mutating the isAliveDoc statement?

I realize it probably "works" because isAliveDoc has capacity 1 and thus
must be reallocated everytime you append, and thus we don't end up with an
isAliveDoc that actually carries arround all the assertions from everything
else.

but that seems very "assume no bad side effects" rather than valid code.

Thoughts?

John
=->
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/juju-dev/attachments/20151022/25739d64/attachment.html>


More information about the Juju-dev mailing list