Skip to content

Gendarme.Rules.Design.ProvideTryParseAlternativeRule(git)

Sebastien Pouliot edited this page Mar 2, 2011 · 1 revision

ProvideTryParseAlternativeRule

Assembly: Gendarme.Rules.Design
Version: git

Description

This rule will warn on any type that provide Parse(string...) method(s) without supplying TryParse alternative method(s). Using a TryParse method is easier since it is less-error prone (no need to handle all possible exceptions) and remove the performance issue regarding throwing/catching exceptions.

Examples

Bad example:

public struct Int32Tuple {
    private int first;
    private int second;
    public Int32Tuple (int a, int b)
    {
        first = a;
        second = b;
    }
    // documented to throw only Argument[Null]Exception or FormatException
    static Int32Tuple Parse (string s)
    {
        if (s == null) {
            throw new ArgumentNullException ("s");
            } else if (s.Length == 0) {
                throw new ArgumentException ("s");
                } else {
                    string [] items = s.Split (':');
                    // without the try/catch it would be much harder to
                    // be 100% certain of all possible exceptions - like
                    // IndexOfOfRangeException and what Int32.Parse can throw
                    try {
                        int a = Int32.Parse (items [0]);
                        int b = Int32.Parse (items [1]);
                        return new Int32Tuple (a, b);
                    }
                    catch (Exception e) {
                        throw new FormatException ("Invalid data", e);
                    }
                }
            }
        }
        

Good example:

public struct Int32Tuple {
    private int first;
    private int second;
    public Int32Tuple (int a, int b)
    {
        first = a;
        second = b;
    }
    // documented to throw only Argument[Null]Exception or FormatException
    static Int32Tuple Parse (string s)
    {
        if (s == null) {
            throw new ArgumentNullException ("s");
            } else if (s.Length == 0) {
                throw new ArgumentException ("s");
                } else {
                    // re-implemented using the exception-less TryParse
                    Int32Tuple tuple;
                    if (!TryParse (s, out tuple))
                    throw new FormatException ("Invalid data");
                    return tuple;
                }
            }
            static bool TryParse (string s, out Int32Tuple tuple)
            {
                tuple = new Int32Tuple ();
                if (String.IsNullOrEmpty (s))
                return false;
                string [] items = s.Split (':');
                if (items.Length != 2)
                return false;
                int a;
                if (!Int32.TryParse (s, out a))
                return false;
                int b;
                if (!Int32.TryParse (s, out b))
                return false;
                tuple.first = a;
                tuple.second = b;
                return true;
            }
        }
        

Notes

  • This rule is available since Gendarme 2.8

Source code

You can browse the latest source code of this rule on github.com

Clone this wiki locally