どんな場合のときだとNullを返したほうがいいか、深く考えたことがないなと思ったのでいろいろ調べてみた。 Is it better to return NULL or empty values from functions/methods where the return value is not present?にとても有意義な情報がちりばめられていた。その中でも一番上の回答をしているJW8さんが下の内容を投稿していてとても参考になった。

Returning null is usually the best idea if you intend to indicate that no data is available. An empty object implies data has been returned, whereas returning null clearly indicates that nothing has been returned. Additionally, returning a null will result in a null exception if you attempt to access members in the object, which can be useful for highlighting buggy code – attempting to access a member of nothing makes no sense. Accessing members of an empty object will not fail meaning bugs can go undiscovered



Related Contents

Pickup Contents