LoginSignup
162

More than 3 years have passed since last update.

動詞を三人称単数化することでBoolを返すことを表せる

Last updated at Posted at 2019-08-11

iOS系の動画を扱うフレームワークの中に AVplayerViewController というクラスが存在します。

このクラスのメソッドを調べてみると 動詞を三人称単数化して Booleanを返すアクセッサメソッドがいくつか用意されていることに気づきました。

var showsPlaybackControls: Bool
var allowsPictureInPicturePlayback: Bool
var entersFullScreenWhenPlaybackBegins: Bool
var exitsFullScreenWhenPlaybackEnds: Bool
var updatesNowPlayingInfoCenter: Bool
var appliesPreferredDisplayCriteriaAutomatically: Bool
var requiresFullSubtitles: Bool

これ系のメソッド名でとても有名な exists が存在しますが、Appleのコーディング規約では動詞を三人称単数としてBool値を返すアクセサメソッドが許容されています。

If the property is expressed as a verb, the format is:
- (BOOL)verbObject;

- (void)setVerbObject:(BOOL)flag;

For example:

- (BOOL)showsAlpha;
- (void)setShowsAlpha:(BOOL)flag;

The verb should be in the simple present tense.

この時、動詞は三人称単数であるべきと説明されています。

これは一般的なのだろうかとググったところ以下の解説に出会いました。

...省略
Meanwhile, "can" more clearly indicates a capability, e.g. CanEdit, CanRead, CanSeek. In each of these cases, can is followed by a verb.

Since "Support" is a verb, I think in your case CanSupportContentType is better.

Shorter alternative

On the other hand, the conventions say the prefix is optional. What's more, it's kind of cheesy to include the argument type in the method name, since a developer can see the type of the argument in intellisense. So you could just name your method Supports and define it like this:

public bool Supports(System.Net.Mime.ContentType contentType)
...which is shorter and still clearly communicates the purpose.

グーグル翻訳

一方、「can」は、機能をより明確に示します。 CanEdit、CanRead、CanSeek。
これらの各ケースでは、canの後に動詞が続きます。

「サポート」は動詞なので、あなたの場合、CanSupportContentTypeの方が良いと思います。

より短い代替案

一方、慣例では、プレフィックスはオプションです。さらに、開発者はインテリセンスで引数のタイプを見ることができるため、メソッド名に引数のタイプを含めるのはちょっと安っぽいです。したがって、メソッドにSupportsという名前を付けて、次のように定義できます。

public bool Supports(System.Net.Mime.ContentType contentType)

三人称単数化する意味を考える

一般的にオブジェクト指向においてメソッド名に動詞の原形を使うことは、オブジェクトに対して 命令する という意味付けがあるためです。
参照 How should I name functions that return values in Python?

これはgitのコミットログを命令形にすることに通じると考えられます。

これをわざわざ三人称単数にすることで、今度は逆にオブジェクトを主語として読むことが出来ると考えられます。
つまりAVPlayerControllerの以下のメソッドは、主語がAVPlayerControllerとして読むことが出来、 「AVPlayerControllerが再生コントローラーを表示する」→ 「AVPlayerControllerが再生コントローラーを表示する(かどうか)」と補えると考えられます。

var showsPlaybackControls: Bool

大切なことはリーダブルであること

英語の変数名でとても重要なことは 英語としてリーダブル であるかどうかだそうです。

参照 Boolean method naming readability

The goal for readability should always be to write code the closest possible to natural language.
So in this case, userExists seems the best choice.
Using the prefix "is" may nonetheless be right in another situations,
for example isProcessingComplete

グーグル翻訳

可読性の目標は、常に自然言語に可能な限り近いコードを記述することです。
したがって、この場合、userExistsが最良の選択のようです。
それでも、isProcessingCompleteのように、接頭辞「is」を使用することは別の状況で正しい場合があります。

ちなみに isUserExists はネイティブからすると非常に奇妙に聞こえるようです。

first one sounds like isBabbyFormed

isBabbyFormed はググるとどうやらアメリカのヤフーの質問で How is babby formed? という質問が由来だそうです。
日本のゲームがやらかした All your base are belong to us に通じるものがあるのだろうと思います。

参照

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
162