Cocoaのプログラミングスタイル
この2日間、Error Handling Programming Guide For Cocoaを読んでいるのですがちょっと耳の痛いことが書いてありました。
It is important to keep in mind the difference between NSError objects and NSException objects, and when to use one or the other in your code. They serve different purposes and should not be confused.
Exceptions (represented by NSException objects) are for programming errors, such as an array index that is out of bounds or an invalid method argument. User-level errors (represented by NSError objects) are for runtime errors, such as when a file cannot be found or a string in a certain encoding cannot be read. Conditions giving rise to exceptions are due to programming errors; you should deal with these errors before you ship a product. Runtime errors can alway occur, and you should communicate these (via NSError objects) to the user in as much detail as they require.
要するに例外は配列範囲外へのアクセスだとか実装のバグをキャッチするもので、出荷時には不要なんだよって言っています。ユーザにエラーを教える必要があるならNSErrorを使ってねと。
これまでこういうことを意識してコードを書いたことがないので反省しまくり。Javaとかでウェブアプリだと単純にこういう風にはいかないかもしれないけれど、少なくともCocoaでデスクトップアプリを書く上ではこういったガイドラインは大切ですね。少なくとも、何も考えないで例外をスローするよりは100倍マシ。かつてヒューマンインターフェースガイドラインを読んで感動したあことがありますが、そのときのことをちょっと思い出してグッっときました。僕がマックを使い続ける理由のひとつは”(ハードであれソフトであれ)インタフェースを徹底的に考え抜く”というアップルの哲学に惹れているというところにもあるんだなぁっていまさらながら自己分析したりして...
そんな風に考えながら「Error Handling Programming Guide For Cocoa」を読んでいると手抜きはできないぞって気持ちになりますね。


最近のコメント
14 weeks 4 days ago
49 weeks 2 days ago
1年 14 weeks ago
1年 14 weeks ago
1年 31 weeks ago
1年 31 weeks ago
1年 31 weeks ago
1年 43 weeks ago
1年 43 weeks ago
2 years 23 weeks ago