凪瀬 Blog
Programming SHOT BAR

目次

Blog 利用状況
  • 投稿数 - 260
  • 記事 - 0
  • コメント - 46892
  • トラックバック - 192
ニュース
広告
  • Java開発者募集中
  • 経歴不問
  • 腕に自信のある方
  • 富山市内
  • (株)凪瀬アーキテクツ
アクセサリ
  • あわせて読みたい
凪瀬悠輝(なぎせ ゆうき)
  • Java技術者
  • お茶好き。カクテル好き。
  • 所属は(株)凪瀬アーキテクツ
  • Twitter:@nagise

書庫

日記カテゴリ

 

continueをどう使うかという話。

ループ内で特定の条件の場合だけ処理をするようなケースで

for (Hoge hoge : hogeList) {
    if (hoge.条件1()) {
        if (hoge.条件2()) {
            if (hoge.条件3()) {
                // 実際の処理
            }
        }
    }
}

というようにifを沢山入れ子にするよりも、

for (Hoge hoge : hogeList) {
    if (!hoge.条件1()) {
        continue;
    }
    if (!hoge.条件2()) {
        continue;
    }
    if (!hoge.条件3()) {
        continue;
    }

    // 実際の処理
    // ...
}

というように、いわゆるガード句を利用して順次処理対象外のモノを外していく方法の方が、 ミスが少なくなるという話。

なぜミスが少なくなるのか、について考察して見ましょう。 ifの入れ子方式の場合、暗黙のelse側のルートを書き漏らすケースが出やすい。 なぜそのようなヒューマンエラーが起きやすいのか。 ひとつにはブロックが大きくなると条件式を書く場所とelseの位置が離れていくため、 対応付けを誤りやすくなるのではないかと思われます。

ガード句を利用する場合、対象外となるものを順次はずしていきますから、 ロジックを考える際の余計なものを思考から順番に外していけるため、 脳への負荷が小さくなのではないかと思います。

Iteratorパターンを使う方法

ガード句を使うほかにGoFデザインパターンのItaratorパターンを使う方法があります。

public Iterable<Hoge> hogeListFilter(List<Hoge> hogeList)

というように、Itearbleを返すメソッドなどを作り、その実装内で不要なものを除外する方法。

ちょっとしたロジックを書くには大掛かりですが、フィルタリングするロジックが あちらこちらで同様のものを使っている場合、フィルタリング処理を分離してまとめることが出来ます。

投稿日時 : 2007年12月14日 18:40
コメント
  • # re: ガード句 x continue x Iteratorパターン
    melt
    Posted @ 2007/12/16 4:21
    >ちょっとしたロジックを書くには大掛かり
    C#3.0 なら Where 区を使って簡単に書けますね。

    foreach (Hoge hoge in
      hogeList.Where(h => h.条件1() && h.条件2() && h.条件3()))
    {
      // 実際の処理
      // ...
    }
  • # re: ガード句 x continue x Iteratorパターン
    凪瀬
    Posted @ 2007/12/17 15:26
    へぇ。便利な機能ありますねぇ。
    C#は文法的に複雑化しすぎの感があるのですけどw
    このWhereってのはコレクションAPIのListの機能なんでしょうか。
    =>の部分がラムダ式ですよね。
  • # re: ガード句 x continue x Iteratorパターン
    melt
    Posted @ 2007/12/17 17:05
    >このWhereってのはコレクションAPIのListの機能なんでしょうか。
    C#3.0 の新機能に拡張メソッドというのがあって、それのおかげで IEnumerable(多分 Java の Iterable と同じようなもの)を実装してるクラスならみんな Where を持っています(持っているように見えます)。


    ところで全く関係ないんですけど、イテレート可能なクラスって Iterable って書くんですね……ずっと Iteratable だと思ってましたorz
  • # Hello All and assorted!
    GrieriaAnaesy
    Posted @ 2011/05/05 3:50
    Hello everyone. I'm ahttp://www.buysale.ro/anunturi/auto/autoturisme/romania.html?Marca=Opel - [anunturi opel - jejune member.http://www.buysale.ro/anunturi/auto/autoturisme/romania.html?Marca=VW - [anunturi VW - Looking enthusiastic to erudition from this valuable resource. http://www.buysale.ro/anunturi/auto/autoturisme/romania.html?Marca=Mercedes%20Benz - [anunturi mercedes benz -
    http://www.buysale.ro/anunturi/auto/autoturisme/romania.html?Marca=Audi - [anunturi audi - Will-power do my print relating to donating and sharing from sometimes to time. http://www.buysale.ro/anunturi/auto/autoturisme/romania.html?Marca=BMW - [anunturi BMW -

    Thanks.
  • # radio
    bogemi
    Posted @ 2011/08/15 19:55

    http://www.buysale.ro/anunturi/vestimentatie-si-accesorii/ceasuri-si-bijuterii/neamt.html - neamt
  • # OwvIiolGvisV
    http://crorkz.com/
    Posted @ 2014/08/07 1:00
    vLx21H Im grateful for the blog article.Thanks Again. Will read on...
  • # WZgrccvbFiY
    http://www.nobisca.com
    Posted @ 2014/09/10 19:44
    There is visibly a lot to identify about this. I assume you made various good points in features also.
  • # JjmulRLoOxlv
    http://www.youtube.com/watch?v=6eoaR-4GvzQ
    Posted @ 2014/09/12 20:42
    whoah this weblog is wonderful i love studying your posts. Keep up the good paintings! You already know, a lot of people are hunting round for this information, you can help them greatly.
  • # CPiKtvLZsuo
    http://track.buyzytax.com/product/Zytax/?uid=612&a
    Posted @ 2014/09/13 18:57
    Excellent website. Lots of useful info here. I am sending it to some friends ans also sharing in delicious. And of course, thanks for your effort!
  • # UlJCtNEfmvHOAqqP
    http://www.1818-888.com
    Posted @ 2014/09/17 17:17
    Thanks for some other wonderful article. Where else could anybody get that kind of info in such an ideal way of writing? I have a presentation next week, and I am on the look for such information.
  • # Hello friends, pleasant post and fastidious urging commented at this place, I am in fact enjoying by these.
    Hello friends, pleasant post and fastidious urging
    Posted @ 2019/04/22 19:39
    Hello friends, pleasant post and fastidious urging commented at this place, I am in fact enjoying
    by these.
  • # Howdy! I could have sworn I've been to this blog before but after browsing through some of the post I realized it's new to me. Anyhow, I'm definitely happy I found it and I'll be bookmarking and checking back often!
    Howdy! I could have sworn I've been to this blog b
    Posted @ 2019/08/01 3:52
    Howdy! I could have sworn I've been to this blog before but after browsing through
    some of the post I realized it's new to me. Anyhow, I'm definitely happy I found it
    and I'll be bookmarking and checking back often!
  • # Howdy! I could have sworn I've been to this blog before but after browsing through some of the post I realized it's new to me. Anyhow, I'm definitely happy I found it and I'll be bookmarking and checking back often!
    Howdy! I could have sworn I've been to this blog b
    Posted @ 2019/08/01 3:53
    Howdy! I could have sworn I've been to this blog before but after browsing through
    some of the post I realized it's new to me. Anyhow, I'm definitely happy I found it
    and I'll be bookmarking and checking back often!
  • # Howdy! I could have sworn I've been to this blog before but after browsing through some of the post I realized it's new to me. Anyhow, I'm definitely happy I found it and I'll be bookmarking and checking back often!
    Howdy! I could have sworn I've been to this blog b
    Posted @ 2019/08/01 3:54
    Howdy! I could have sworn I've been to this blog before but after browsing through
    some of the post I realized it's new to me. Anyhow, I'm definitely happy I found it
    and I'll be bookmarking and checking back often!
  • # Howdy! I could have sworn I've been to this blog before but after browsing through some of the post I realized it's new to me. Anyhow, I'm definitely happy I found it and I'll be bookmarking and checking back often!
    Howdy! I could have sworn I've been to this blog b
    Posted @ 2019/08/01 3:55
    Howdy! I could have sworn I've been to this blog before but after browsing through
    some of the post I realized it's new to me. Anyhow, I'm definitely happy I found it
    and I'll be bookmarking and checking back often!
  • # I have read some just right stuff here. Certainly price bookmarking for revisiting. I wonder how a lot effort you set to create the sort of great informative website. plenty of fish natalielise
    I have read some just right stuff here. Certainly
    Posted @ 2019/08/01 10:27
    I have read some just right stuff here. Certainly price bookmarking
    for revisiting. I wonder how a lot effort you
    set to create the sort of great informative website. plenty of fish natalielise
タイトル
名前
Url
コメント