Skip to main content
added 2 characters in body
Source Link

There is an inconsistency - negative values of maxLength would be forgiven for value being null, but cause an exception for every other input (since Substring would throw an ArgumentOutOfRangeException).

There's two possible approaches that remove this consistencyinconsistency - fail fast

public static string WithMaxLength(this string value, int maxLength)
{
    if (maxLength < 0)
    {
       throw new ArgumentOutOfRangeException("maxLength must be equal to or greater than 0");
    }
    // ...

Or ignore negative values for both cases:

public static string WithMaxLength(this string value, int maxLength)
{
    if (value == null)
    {
        return null;
    }
    if (maxLength < 0)
    {
        return "";
    }
    return value.Substring(0, Math.Min(value.Length, maxLength));
}

Personally I'd be inclined to choose the former - maxLength being smaller than 0 could be a symptom of a problem (implementation error) that we wouldn't want to sweep under the carpet. It's also consistent with the way Substring itself behaves.

There is an inconsistency - negative values of maxLength would be forgiven for value being null, but cause an exception for every other input (since Substring would throw an ArgumentOutOfRangeException).

There's two possible approaches that remove this consistency - fail fast

public static string WithMaxLength(this string value, int maxLength)
{
    if (maxLength < 0)
    {
       throw new ArgumentOutOfRangeException("maxLength must be equal to or greater than 0");
    }
    // ...

Or ignore negative values for both cases:

public static string WithMaxLength(this string value, int maxLength)
{
    if (value == null)
    {
        return null;
    }
    if (maxLength < 0)
    {
        return "";
    }
    return value.Substring(0, Math.Min(value.Length, maxLength));
}

Personally I'd be inclined to choose the former - maxLength being smaller than 0 could be a symptom of a problem (implementation error) that we wouldn't want to sweep under the carpet. It's also consistent with the way Substring itself behaves.

There is an inconsistency - negative values of maxLength would be forgiven for value being null, but cause an exception for every other input (since Substring would throw an ArgumentOutOfRangeException).

There's two possible approaches that remove this inconsistency - fail fast

public static string WithMaxLength(this string value, int maxLength)
{
    if (maxLength < 0)
    {
       throw new ArgumentOutOfRangeException("maxLength must be equal to or greater than 0");
    }
    // ...

Or ignore negative values for both cases:

public static string WithMaxLength(this string value, int maxLength)
{
    if (value == null)
    {
        return null;
    }
    if (maxLength < 0)
    {
        return "";
    }
    return value.Substring(0, Math.Min(value.Length, maxLength));
}

Personally I'd be inclined to choose the former - maxLength being smaller than 0 could be a symptom of a problem (implementation error) that we wouldn't want to sweep under the carpet. It's also consistent with the way Substring itself behaves.

added 1 character in body
Source Link

There is an inconsistency - negative values of maxLength would be forgiven iffor value being null, but cause an exception for every other input (since Substring would throw an ArgumentOutOfRangeException).

There's two possible approaches that remove this consistency - fail fast

public static string WithMaxLength(this string value, int maxLength)
{
    if (maxLength < 0)
    {
       throw new ArgumentOutOfRangeException("maxLength must be equal to or greater than 0");
    }
    // ...

Or ignore negative values for both cases:

public static string WithMaxLength(this string value, int maxLength)
{
    if (value == null)
    {
        return null;
    }
    if (maxLength < 0)
    {
        return "";
    }
    return value.Substring(0, Math.Min(value.Length, maxLength));
}

Personally I'd be inclined to choose the former - maxLength being smaller than 0 could be a symptom of a problem (implementation error) that we wouldn't want to sweep under the carpet. It's also consistent with the way Substring itself behaves.

There is an inconsistency - negative values of maxLength would be forgiven if value being null, but cause an exception for every other input (since Substring would throw an ArgumentOutOfRangeException).

There's two possible approaches that remove this consistency - fail fast

public static string WithMaxLength(this string value, int maxLength)
{
    if (maxLength < 0)
    {
       throw new ArgumentOutOfRangeException("maxLength must be equal to or greater than 0");
    }
    // ...

Or ignore negative values for both cases:

public static string WithMaxLength(this string value, int maxLength)
{
    if (value == null)
    {
        return null;
    }
    if (maxLength < 0)
    {
        return "";
    }
    return value.Substring(0, Math.Min(value.Length, maxLength));
}

Personally I'd be inclined to choose the former - maxLength being smaller than 0 could be a symptom of a problem (implementation error) that we wouldn't want to sweep under the carpet. It's also consistent with the way Substring itself behaves.

There is an inconsistency - negative values of maxLength would be forgiven for value being null, but cause an exception for every other input (since Substring would throw an ArgumentOutOfRangeException).

There's two possible approaches that remove this consistency - fail fast

public static string WithMaxLength(this string value, int maxLength)
{
    if (maxLength < 0)
    {
       throw new ArgumentOutOfRangeException("maxLength must be equal to or greater than 0");
    }
    // ...

Or ignore negative values for both cases:

public static string WithMaxLength(this string value, int maxLength)
{
    if (value == null)
    {
        return null;
    }
    if (maxLength < 0)
    {
        return "";
    }
    return value.Substring(0, Math.Min(value.Length, maxLength));
}

Personally I'd be inclined to choose the former - maxLength being smaller than 0 could be a symptom of a problem (implementation error) that we wouldn't want to sweep under the carpet. It's also consistent with the way Substring itself behaves.

Source Link

There is an inconsistency - negative values of maxLength would be forgiven if value being null, but cause an exception for every other input (since Substring would throw an ArgumentOutOfRangeException).

There's two possible approaches that remove this consistency - fail fast

public static string WithMaxLength(this string value, int maxLength)
{
    if (maxLength < 0)
    {
       throw new ArgumentOutOfRangeException("maxLength must be equal to or greater than 0");
    }
    // ...

Or ignore negative values for both cases:

public static string WithMaxLength(this string value, int maxLength)
{
    if (value == null)
    {
        return null;
    }
    if (maxLength < 0)
    {
        return "";
    }
    return value.Substring(0, Math.Min(value.Length, maxLength));
}

Personally I'd be inclined to choose the former - maxLength being smaller than 0 could be a symptom of a problem (implementation error) that we wouldn't want to sweep under the carpet. It's also consistent with the way Substring itself behaves.