Operation that may succeed / fail - return type and naming convention

What type of return is suitable for a method that can either succeed or fail because of its business logic? And based on this type of return, what would be the corresponding naming convention?

My instinct is that bool is most suitable for a simple pass / crash. I researched and found conventions for methods that display the trait - i.e., IsValid, HasFoo, ContainsBar, etc. But this is also the correct name for an action such as BuildHouse () or FlyKite () to clearly indicate whether the operation was successful

I tried this in several ways, but every time I keep thinking that it looks weird and there should be best practice ....

bool IsHouseBuilt() bool TryBuildHouse() void BuildHouse(out bool success) PassFailEnum BuildHouse() //seems a little excessive bool IsKiteFlying() bool TryFlyKite() void FlyKite(out bool success) PassFailEnum FlyKite() 
+4
source share
6 answers

All the built-in methods in .NET that try to parse something into something else (for example, int.TryParse ) are called TryParse and return the bool and out parameter.

So maybe:

 public static bool TryBuildHouse(T input, out House house) 
+4
source

TryBuildHouse seems to be the most correct of your examples.

+1
source

A commonly accepted format of a method that attempts to return a value is:

 bool TryBuildHouse(out object thing); 

The often accepted format for a parameterless method that tries to succeed in doing something is what you want, really ... Some people prefer the Try prefix, as it usually means that it may fail, but I like to think The return type bool also implies that it may fail. To each his own.

If the inability to build something is catastrophic (it absolutely SHOULD succeed), you should consider the void method, which throws an appropriate exception in rare cases of failure, and then catch or not catch it based on your unique situational and environmental requirements.

+1
source

You should consider creating a class that represents information that would be desirable for any type of call on your system, for example:

 public class ActionResult { public bool Success { get; set; } public List<string> Errors { get; } public ActionResult() { // Initialize whatever you want here Errors = new List<string>(); } } 

Then, when “bad” or “unexpected” things happen in your methods, you can populate the return type of the ActionResult type with error information that may be useful to the caller.

In your example, you will use it as follows:

 ActionResult BuildHouse() { } 
0
source

I would suggest TryParse , but you did not indicate whether the method really returns an object. Typically, TryParse assumes that there is a way out. If you look at the HashSet.Add method, it returns bool if successful, but does not return the actual value of out .

Therefore, from the sound of your case, I would go for something like

 /// <summary> /// Builds a house /// </summary> /// <returns>true if house was built; false if house failed to build</returns> public bool BuildHouse(); 

and document the fact that it returns bool success / fail. If you plan to return an object, such as a house, then the TryParse option makes sense, and your method will have an out parameter that will hold the newly created object.

0
source

Lots of features. One of those that I have not seen is BuildHouse (), which returns a House object if it succeeds and returns null if it does not work.

0
source

Source: https://habr.com/ru/post/1495033/


All Articles