3

私は社内のニーズに合わせて API を作成しているため、使いやすさは最優先事項の 1 つです。次の例のように、押しすぎているのではないでしょうか。

逆測地問題を解く関数は、2 点の地理座標を取り込み、それらの間の距離と、各点から他の点までの方位 (角度) を計算します。

使いやすさを最適化した従来のソリューションは次のようになります (名前の長さは気にしないでください。改善の余地があることは承知しています)。

class Angle;
class GeoCoordinate;
...

struct InverseGeodeticOut
{
    Angle forwardAzimuth;
    Angle backAzimuth;
    double distance;
};

InverseGeodeticOut inverseGeodetic(const GeoCoordinate &firstPoint,
                                   const GeoCoordinate &secondPoint);
// or
void inverseGeodetic(const GeoCoordinate &firstPoint,
                     const GeoCoordinate &secondPoint,
                     InverseGeodeticOut *result);

私の質問は、ユーザーの入力を節約するためにさらに一歩進めることがどれほど合理的かということです。

class Angle;
class GeoCoordinate;
...

struct InverseGeodetic
{
    InverseGeodetic();
    InverseGeodetic(const GeoCoordinate &firstPoint,
                    const GeoCoordinate &secondPoint);

    Angle forwardAzimuth;
    Angle backAzimuth;
    double distance;
};

// so the usages would be
InverseGeodeticOut inverse = inverseGeodetic(firstPoint, secondPoint);

InverseGeodeticOut inverse;
inverseGeodetic(firstPoint, secondPoint, &inverse);

InverseGeodetic inverse(firstPoint, secondPoint);

おそらく、この特定の例では、違いが小さすぎて話す価値はありませんが、そのような構成が一般的に問題ないかどうか疑問に思います.

4

1 に答える 1

2

私はあなたの2番目のコード例が好きですが、パブリックコンストラクターは少し混乱しています. 特に . を構築する他の方法がある場合InverseGeodetic。静的ファクトリメソッドを使用して構築したいと思います。そうすれば、メソッドにより意味のある名前を付けることができます。

struct InverseGeodetic
{
    Angle forwardAzimuth;
    Angle backAzimuth;
    double distance;

    static InverseGeodetic FromTwoPoints(const GeoCoordinate &, const GeoCoordinate &);
};

// usage
auto inverse = InverseGeodetic::FromTwoPoints(a, b);

しかし、それは私のC#のバックグラウンドが浸透している可能性があります。

于 2013-05-29T20:30:50.087 に答える