1

So my question is more about what I should do, not necessarily 'how' to implement what I'm doing. So, I have this static class that looks like this:

public static class CanImportFileType
{
   static Predicate<string> isSTQFile = f => f.ToLower().EndsWith(".stq") || f.ToLower().EndsWith(".stqt");
   static Predicate<string> isAPHFile = f => f.ToLower().EndsWith(".aph");


    public static bool IsValidQuoteFilePath(string[] FilePath)
    {
        bool IsValidQuoteFilePath = false;
        if(HasFile(FilePath))
        {
            if(isSTQFile(FilePath[0]))//we just look at the first index...we could extend this to look through all of the indices and find the first appropriate 
            {
                IsValidQuoteFilePath = true;
            }
        }
        return IsValidQuoteFilePath;
    }
    public static bool IsValidQuoteFilePath(string FilePath)
    {
        bool IsValidQuoteFilePath = false;
            if (isSTQFile(FilePath))
            {
                IsValidQuoteFilePath = true;
            }
        return IsValidQuoteFilePath;
    }


    public static bool IsValidAPHFilePath(string[] FilePath)
    {
        bool IsValidQuoteFilePath = false;
        if (HasFile(FilePath))
        {
            if(isAPHFile(FilePath[0]))
            {
                IsValidQuoteFilePath = true;
            }
        }
        return IsValidQuoteFilePath;

    }
    public static bool IsValidAPHFilePath(string FilePath)
    {
        bool IsValidQuoteFilePath = false;

        if (isAPHFile(FilePath))
        {
            IsValidQuoteFilePath = true;
        }

        return IsValidQuoteFilePath;
    }

    /// <summary>
    /// used only to determine if the string array being passed around is not null or void of any strings (count == 0)
    /// </summary>
    /// <param name="fileLocation"></param>
    /// <returns> whether or not there is a file type in the first index of a string array</returns>
    private static bool HasFile(string[] fileLocation)
    {
        bool hasfile = false;
        if (fileLocation != null)
        {
            if (fileLocation.Count() > 0)
            {
                hasfile = true;
            }
        }
        return hasfile;
    }
}

Now to implement this I would have something like this:

string[] foo = {"hello", "world"};
CanImportFileType.IsValidQuoteFilePath(foo) //returns false

Or, I could just extend the string[] and string types with adding a 'this' keyword at the beginning of the method arguments which would look something like this:

      public static bool IsValidQuoteFilePath(this string[] FilePath)
    {
        bool IsValidQuoteFilePath = false;
        if(HasFile(FilePath))
        {
            if(isSTQFile(FilePath[0]))//we just look at the first index...we could extend this to look through all of the indices and find the first appropriate 
            {
                IsValidQuoteFilePath = true;
            }
        }
        return IsValidQuoteFilePath;
    }

Then, all I would have to do to implement it would be to access that strings IsValidQuoteFilePath method like such:

    string[] foo = {"hello", "world"};
    foo.IsValidQuoteFilePath() //returns false

I guess to summarize my question it would be: How do you know when to just extend a method versus creating a static helper class?

Strohlaj
  • 21
  • 2
  • It sounds like you shouldn't be using arrays at all. – SLaks Mar 17 '14 at 19:22
  • well, this is for drag and drop functionality. you could potentially be dragging and dropping multiple files onto our app. In this case, we could make it look through the array of filepaths and find the correct file types, but for now I just want to look at the first one and then bail on it after that. – Strohlaj Mar 17 '14 at 19:36

2 Answers2

2

How do you know when to just extend a method versus creating a static helper class?

The two are basically the same - it's more of how you expect and want the items to be used.

My general rule of thumb is: Will this method be something every developer would (potentially) want to use on every single string or string[] instance? If so, consider an extension method. If not, use a normal static method.

In this case, it seems like the use case is very narrow, and not appropriate to an arbitrary string instance, which would suggest a normal, static helper method. This is almost always the case for core built-in types (object/string/etc), since it's very rare that a method is really appropriate for all uses of the type.

Also: Note that this doesn't extend the type, it merely provides a different syntax for accessing your static method.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
0

You're not extending the type in the second case. Your code is in no way modifying the string[] type. You are merely providing syntactic sugar to make it appear as if your method is an instance method. it is not; it is still a static method.

There is not real functional difference between the two; the latter is compiled to look just like the former. It is entirely a matter of personal preference which you use.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • wait, so the second option above isn't making an extension method for the string[] type? Here was my reference: [link] (http://stackoverflow.com/questions/4910108/how-to-extend-c-sharp-built-in-types-like-string) – Strohlaj Mar 17 '14 at 19:34
  • @Strohlaj It is making an extension methods, but *an extension method is not technically extending the type*. It is creating some syntactic sugar to make it look like it is, but it's not. The naming is somewhat unfortunate in that it implies that it does. – Servy Mar 17 '14 at 19:35