Getter and setter naming #7874
Replies: 3 comments 1 reply
-
It's trivial to manually add properties in python, and we already do this for some of the geometry classes. For unit-specific properties, the convention I've been using is the 'normal' accessor is the SI unit, and then other properties are suffixed with the converted unit. Here's how rectangle 2d has extra properties/unit methods: https://github.com/robotpy/mostrobotpy/blob/main/subprojects/robotpy-wpimath/gen/geometry/Rectangle2d.yml#L33. The resulting docs are at https://robotpy.readthedocs.io/projects/robotpy/en/stable/wpimath.geometry/Rectangle2d.html#wpimath.geometry.Rectangle2d.xwidth |
Beta Was this translation helpful? Give feedback.
-
Copying my thoughts from the issue: The most intuitive solution would be to use a public member variable, a la The ideal solution then would be allowing that syntax but still with custom setter/getter functionality. The best example is Qt (C++) which lets you define a "property" as such: private:
int m_value = 0;
Q_PROPERTY(value int READ value WRITE setValue)
public:
int value() const { return m_value; }
void setValue(const int newValue) { m_value = newValue; } // and any custom stuff can be done here And then in QML, which can access C++ containers: customClass.value = 10
console.log(customClass.value) Of course this isn't really easy to implement. In C++ you could probably do some weird stuff with In my experience teaching brand-new programmers, most seem to pretty equally understand both Additionally, depending on where this discussion ends up, another discussion about what method casing to use would be good--particularly with C++ which has three competing standards that are used in all sorts of different libraries, and in maintaining parity between C++, Java, and Python. |
Beta Was this translation helpful? Give feedback.
-
I think what wpimath currently does, where the methods that return raw doubles have the unit name baked into the method name (eg |
Beta Was this translation helpful? Give feedback.
-
Forking from the conversation in #7821.
Java records generate
x()
accessors (there's nox(value)
setters because records are immutable).Will entry-level (middle school level) students understand how Java records work? Will they understand
x()
better thangetX()
? Will they understand why it'sx()
and not justx
?More generally, currently WPILib uses
getX()
andsetX(value)
. Would it make sense more broadly to change tox()
getter andx(value)
setter naming (orx()
getter andsetX(value)
)? ThegetX
/setX
naming is very common historically in Java; there are a few standard library classes that use justx()
andx(value)
but the extraget
andset
prefix may make usage easier to understand for brand-new programmers?Unfortunately Java as a language doesn't support properties, which would be the cleanest solution to this by allowing variable-like access. Python does, however, and we might want to consider that, although it may be challenging to generate PyBind wrappers that do that without a lot of custom work.
Relatedly, in cases where we want additional getters for unit types, how should those be named? Setters can be overloaded if desired, but getters need unique naming. Should they both be uniquely named?
Beta Was this translation helpful? Give feedback.
All reactions