Motivation: When I write a math-intensity code, I want to write it as fast as possible.
Problem: Oftenly-used math classes such as Vector3, Rect, Color, etc, have underscore-postfixed public field names. Instead of writing just “x” or “min”, in addition I have to press a key once or even twice. It’s not big issue, but annoying thing. It slows me down and final code looks ugly little bit.
For example, the code
closest_corner.x = dmin_abs.x < dmax_abs.x ? rect.min.x : rect.max.x;
closest_corner.y = dmin_abs.y < dmax_abs.y ? rect.min.y : rect.max.y;
closest_delta.x = dmin_abs.x < dmax_abs.x ? dmin_abs.x : dmax_abs.x;
closest_delta.y = dmin_abs.y < dmax_abs.y ? dmin_abs.y : dmax_abs.y;
looks mush better for me than
closest_corner.x_ = dmin_abs.x_ < dmax_abs.x_ ? rect.min_.x_ : rect.max_.x_;
closest_corner.y_ = dmin_abs.y_ < dmax_abs.y_ ? rect.min_.y_ : rect.max_.y_;
closest_delta.x_ = dmin_abs.x_ < dmax_abs.x_ ? dmin_abs.x_ : dmax_abs.x_;
closest_delta.y_ = dmin_abs.y_ < dmax_abs.y_ ? dmin_abs.y_ : dmax_abs.y_;
There are many reasons to eliminate underscores in public field names of widely-used core math classes:
- The code is shorter
- Beautiful
- Faster writing and refactoring
- Conventional naming (Most of engines and libraries are using simple notation: lowercased without underscores. Examples: Unity, glm, glsl, hlsl)
So I found simple solution for me:
In Urho3D’s class headers I’ve cover the public fields by union statements, e.g. Vector3 member definitions look like this:
/// X coordinate.
union
{
float x_;
float x;
};
/// Y coordinate.
union
{
float y_;
float y;
};
/// Z coordinate.
union
{
float z_;
float z;
};
download: http://gdurl.com/AamL (modified headers only)
Now I can write math code in Unity-style without using external library! Happy coding