Advantages of using extension methods in functional programming

Method chaining in functional programming relies on extension methods. As we have already discussed in Chapter 1, Tasting Functional Style in C#, method chaining will make our code easier to read since it can decrease the lines of code. For the sake of code readability in the extension method, let's take a look at the following code, which we can find in the CodeReadability.csproj project:

using System.Linq; 
namespace CodeReadability 
{ 
  public static class HelperMethods 
  { 
    public static string TrimAllSpace(string str) 
    { 
      string retValue = ""; 
      foreach (char c in str) 
      { 
        retValue +=!char.IsWhiteSpace(c) ?c.ToString() :""; 
      } 
      return retValue; 
    } 
    public static string Capitalize(string str) 
    { 
      string retValue = ""; 
      string[] allWords = str.Split(' '); 
      foreach (string s inallWords) 
      { 
        retValue += s.First() 
        .ToString() 
        .ToUpper() 
        + s.Substring(1) 
        + " "; 
      } 
      return retValue.Trim(); 
    } 
  } 
} 

The preceding code is the static method inside the static class. It is not an extension method since we don't use the this keyword in the method argument. We can find it in the HelperMethods.cs file. The use of the TrimAllSpace() method is to remove all white space characters from the string, while the use of the Capitalize() method is to make the first letter of a string uppercase in the sentence. We also have exactly same methods as HelperMethods, which we can find in the ExtensionMethods.cs file. Let's look at the following code, in which we declare TrimAllSpace() and Capitalize() as the extension methods:

using System.Linq; 
namespace CodeReadability 
{ 
  public static class ExtensionMethods 
  { 
    public static string TrimAllSpace(this string str) 
    { 
      string retValue = ""; 
      foreach (char c in str) 
      { 
        retValue +=!char.IsWhiteSpace(c) ?c.ToString() :""; 
      } 
      return retValue; 
    } 
    public static string Capitalize(string str) 
    { 
      string retValue = ""; 
      string[] allWords = str.Split(' '); 
      foreach (string s inallWords) 
      { 
        retValue += s.First() 
          .ToString() 
          .ToUpper() 
          + s.Substring(1) 
          + " "; 
      } 
      return retValue.Trim(); 
    } 
  } 
} 

Now, we will create code that will trim all the whitespace in the given string and then capitalize each string in the sentence. The following is the code implemented in the HelperMethods class:

static void Main(string[] args) 
{ 
  string sntc = ""; 
  foreach (string str in sentences) 
  { 
    string strTemp = str; 
    strTemp = HelperMethods.TrimAllSpace(strTemp); 
    strTemp = HelperMethods.Capitalize(strTemp); 
    sntc += strTemp + " "; 
  } 
  Console.WriteLine(sntc.Trim()); 
} 

We also declare a string array named sentences, as follows:

static string[] sentences = new string[] 
{ 
  " h o w ", 
  " t o ", 
  " a p p l y ", 
  " e x t e n s i o n ", 
  " m e t h o d s ", 
  " i n ", 
  " c s h a r p ", 
  " p r o g r a m mi n g " 
}; 

The preceding code will give the following output:

Advantages of using extension methods in functional programming

We can, if we want, simplify the preceding Main() method, which uses HelperMethods, using extension methods we have already created, as follows:

static void Main(string[] args) 
{ 
  string sntc = ""; 
  foreach (string str in sentences) 
  { 
    sntc += str.TrimAllSpace().Capitalize() + " "; 
  } 
  Console.WriteLine(sntc.Trim()); 
} 

If we run the preceding Main() method, we will get eactly the same output on the console. However, we have refactored the following code snippet:

string strTemp = str; 
strTemp = HelperMethods.TrimAllSpace(strTemp); 
strTemp = HelperMethods.Capitalize(strTemp); 
sntc += strTemp + " "; 

Using the extension method, we just need this one-line code to replace the the four lines of code:

sntc += str.TrimAllSpace().Capitalize() + " "; 

The point is that we have reduced the line of code so it now becomes simpler and more readable and the flow of the process is clearer.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset