views:

519

answers:

3

Here is my function (updated):

Public Shared Function shortenUrl(ByVal URL As String) As String
    Return shortenUrl(URL, 32)
End Function
Public Shared Function shortenUrl(ByVal URL As String, ByVal maxLength As Integer) As String
    If URL.Length > maxLength Then
        String.Format("{0}...{1}", URL.Substring(0, (maxLength / 2)), URL.Substring(URL.Length - ((maxLength / 2) - 3)))
    Else
        Return URL
    End If
End Function

I fixed the problem where it didn't return maxLength chars because it didn't take into account the ellipses.


It seems to me that it is too complicated; any suggestions, comments, concerns are more than welcome.

+1  A: 

Why not do this?

Public Shared Function shortenUrl(ByVal URL As String) As String
    Return shortenUrl(URL, 29)
End Function
Public Shared Function shortenUrl(ByVal URL As String, ByVal maxLength As Integer) As String
    If URL.Length > maxLength Then
        Return String.Format("{0}...{1}", URL.Substring(0, maxLength / 2),URL.Substring(URL.Length - (maxLength / 2)))
    Else
        Return URL
    End If
End Function

That at least gets rid of all of the tempoary declarations

Mitchel Sellers
thats why it seemed overly complicated, thanks!i blame it on brain fatigue!
Anders
It happens! Especially after working on a project for a while!
Mitchel Sellers
A: 
Public Shared Function shortenUrl(ByVal URL As String, Optional ByVal maxLength As Integer = 29) As String
    If URL.Length > maxLength Then       
        Return String.Format("{0}...{1}", URL.Substring(0, maxLength / 2), URL.Substring(URL.Length - (maxLength / 2)))
    Else
        Return URL
    End If
End Function

Not sure why people hate optional arguments so much. They do the exact same thing and expose to the user what value will be defaulted if they don't provide one.

tom.dietrich
The support in other languages is typically the main reason, different method signatures, are more common.
Mitchel Sellers
plus you can preprocess input if you overload (thats the technical term) a function rather than inside the function itself. cleaner code IMO
Anders
I suppose, but is still more code to write and maintain for what is only a tangible benefit in a small number of potential situations.
tom.dietrich
Optional arguments are not supported by the CLR. If you are trying to write code that is CLS-compliant, then you cannot write code that has optional arguments.
OwenP
Anders I'm not sure what you mean. Unless you need to do more than to simply call the polymorph with the default value you prefer, Optional parameters do the same thing. If you do need to do more than that, Then yes, of course overloaded functions are the way to go.
tom.dietrich
OwenP takes the win. If you are trying to write CLS complient code, then by all means, do it the harder way.
tom.dietrich
Optional values are also *compiled into client code* like a constant, which means changing them is an interface change - requiring clients to recompile.
Mark Brackett
+1  A: 

Well, I don't know if it's too complicated...but it is wrong. If I call shortenUrl(URL, 29) I'd expect the return would have a maximum length of 29 characters. Your code would give me 31. If I call it with a length of 30, I'll get back 33 characters. You're not including the inserted "...", and you're relying on rounding to get the substring lengths and dropping the remainder.

I'd add some param validation, and change it to:

Public Function shortenUrl2(ByVal URL As String, ByVal maxLength As Integer) As String
    Const middle as String = "..."
    If maxLength < 0 Then
       Throw New ArgumentOutOfRangeException("maxLength", "must be greater than or equal to 0")
    ElseIf String.IsNullOrEmpty(URL) OrElse URL.Length <= maxLength Then
       Return URL
    ElseIf maxLength < middle.Length Then
       Return URL.Substring(0, maxLength)
    End If

    Dim left as String = URL.Substring(0, CType(Math.Floor(maxLength / 2), Integer))
    Dim right as String = URL.Substring(URL.Length - (maxLength - left.Length - middle.Length))

    Return left & middle & right
End Function
Mark Brackett