以下の内容はhttps://bleis-tift.hatenablog.com/entry/20090113/1231832839より取得しました。


特定の値しか許さないんだったらintじゃなくてenum

今日発見したこんなコード。

/// <summary>
/// ほげほげを設定する
/// </summary>
/// <param name="hoge">ほげほげ</param>
/// <param name="location">0:左、1:中央、2:右</param>
public void SetHoge(string hoge, int location)
{
    switch (location) {
        case 0: leftHoge = hoge; break;
        case 1: centerHoge = hoge; break;
        case 2: rightHoge = hoge; break;
        default: return;
    }
}


こんな、特定の値しか許さない場合は、積極的にenumを定義すべき。

/// <summary>
/// ほげほげの位置を表す列挙型。
/// </summary>
public enum HogeLocationKind { Left, Center, Right }

/// <summary>
/// ほげほげを設定する
/// </summary>
/// <param name="hoge">ほげほげ</param>
/// <param name="locationKind">ほげほげを設定する位置</param>
public void SetHoge(string hoge, HogeLocationKind locationKind)
{
    switch (locationKind) {
        case HogeLocationKind.Left: leftHoge = hoge; return;
        case HogeLocationKind.Center: centerHoge = hoge; return;
        case HogeLocationKind.Right: rightHoge = hoge; return;
    }
}


あと、真偽値だけど何を表す真偽値なのかはっきりさせたいだけのときでも、booleanの代わりにenumを定義すべき。

// fileはなんか自分で作ったクラスのオブジェクト
file.Create(@"C:\out.txt", true);   // このtrueは何?
file.Create(@"C:\out.txt", CreateMode.DoNothingIfExists);   // こうなってれば一目瞭然



以上の内容はhttps://bleis-tift.hatenablog.com/entry/20090113/1231832839より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14