primary constructor naming convention
Hi!
I was wondering if there is a naming convention for primary constructor parameters on a class. Let's say I have
class Point(int x, int y)
and now I want to add a method to this class with an int x
parameter. AFAICT I then can't really resolve the name conflict between the two in the method body, so I was wondering if I should rename the primary constructor parameters to e.g. _x
and _y
or if I should instead rename the method parameter to avoid the conflict.
Thanks!6 Replies
hmm? I'm a bit confused. There should not be any conflict between a primary ctor (or any ctor) and any member function.
Something like a
Point
is a great candidate for a record struct though.
It is a silly example which would be better done with records, as you say. I was just trying to use something simple to convey the question.
okay I see what you are talking about better now. I would only rely on the "auto fields" when there is no obvious conflict. If there is, I would just do like:
especially in a case like
Point
where you are likely to want to expose the X and Y.I see. Thanks! I was just wondering if there was some established convention to resolve these kinds of conflicts.
I would say that "upgrading" them to properties (private or public) is probably the way to resolve these.
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View