Naming: how to name method overloads which return nullable result? #87752
Replies: 3 comments 1 reply
-
I think it depends on what method existed first and if nullability was designed into the API from the get go. Personally, I've been using GetFoo and GetRequiredFoo. |
Beta Was this translation helpful? Give feedback.
-
Sometimes I just use Try* prefix. Yeah it's kinda occupied by bool + out T pattern but pattern matching wise it's much nicer to return T? Also it's somewhat inspired by F# where Try* thingies return Option of T The problem is that if you want to be consistent with api that is already there you kinda have to adhere to the naming used there or else it will look ugly. So maybe all of these are good options. |
Beta Was this translation helpful? Give feedback.
-
When it comes to the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In LINQ, we have
First
andFirstOrDefault
, etc., whereFirst
throws on empty collection andFirstOrDefault
returns null. I actually created 2 other extensions,FirstOrNull
andFirstOrNullStruct
, because I don't like the "default" semantics and prefer "real"T?
for value types, so I get(int?)null
instead of(int)0
.In other places, I have other methods, like
IConfiguration.GetResolvedValueRequired
andIConfiguration.GetResolvedValue
, which access config settings (and do some other logic on top of the regularGetValue
), and the "required" one throws, and the other one returns null, if the config setting is missing.Yes in other places, I have methods like
IContainer.Resolve
andIContainer.ResolveOptional
(for dependency injection), whereResolve
throws andResolveOptional
returns null.Today, I realized that I have 3 different naming schemes for essentially the same pattern.
I was curious if other members encountered this naming challenge, what they went with, - any thoughts on this subject would be welcome. Thanks in advance.
PS. I'm currently leaning towards standardizing on the
Foo
andFooOrNull
naming scheme, but wanted to hear other opinions before I commit to it.Beta Was this translation helpful? Give feedback.
All reactions