In any case it’s probably better to use setVariable1.add
instead of using +=
. That way there is no confusion about +=
in the first place.
set1 + set2
is ok for sets since it clearly creates a new set. set1 += set2
is less clear. Does it modify set1
or does it create a new set and assign it to the variable set1
. For readonly sets it’s obviously the first but unless the type declaration is right next to it, it’s still sometimes hard to tell. Sure it’s actually forbidden for mutable sets but that’s a less well known fact.
Therefor I tend to not use +=
for collections. It’s bad for readonly collections since it will always create a copy which can have very bad performance impacts (especially in loops) and it’s bad for mutable collections because the code is harder to read.