Methods prefixed with 'Try' should return a boolean

Rule ND2016 Naming Conventions

A method prefixed with Try is expected to return a boolean (or Task<bool> if async) reflecting success or failure, usually with the result in an out parameter, e.g. System.Int32.TryParse(int,out string):bool

How to fix

Rename the method, or transform it into an operation that can fail.

CQLinq source

This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.

// <Name>Methods prefixed with 'Try' should return a boolean</Name>
// <Id>ND2016:MethodsPrefixedWithTryShouldReturnABoolean</Id>

warnif count > 0
from m in Application.Methods where
  m.SimpleNameLike("^Try") &&
  m.ReturnType != null &&
  // Accept boolean or value tuple (eventually nullable) as return type
 !m.ReturnType.FullNameEqualsAny("System.Boolean","System.Nullable<T>") &&
  m.ReturnType.SimpleName != "ValueTuple" &&
 !m.ReturnType.Name.Contains("Task")
select new { 
   m, 
   m.ReturnType,
   Debt = 10.ToMinutes().ToDebt(),
   Severity = Severity.Medium
}

//<Expl>
// The *{0}* returns the *{1}*.
// Usually a method whose name starts with *Try* returns a *bool* value 
// to allow client code to test whether its execution succeeded or not.
//</Expl>

//<Desc>
// A method prefixed with **Try** is expected to return a *boolean* (or
// *Task<bool>* if async) reflecting *success* or *failure*, usually with
// the result in an *out parameter*, e.g.
// *System.Int32.TryParse(int,out string):bool*
//</Desc>

//<HowToFix>
// Rename the method, or transform it into an operation that can fail.
//</HowToFix>

Naming Conventions: all 23 items