A junior developer recently left the firm that Graham works for. In an act of great kindness and generosity, before officially departing, he sent to the developer who had inherited his work a file titled 'Useful_Bits.txt' - some code that he had lovingly written and could potentially be reused.

The file included, among many other things, the below gems. The last one had reportedly taken ages to write but the departing dev got upset when it was pointed out that you could do it in one line by overloading ToString() or using string.Format()...

In a related note, the developer responsible for maintaining the applications he wrote has since handed in his notice.

/// <summary>
/// 
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static DateTime ConvertToDateFromYYYYMMDD(this string str)
{
  int day = Convert.ToInt32(str[6].ToString() + str[7].ToString());
  int month = Convert.ToInt32(str[4].ToString() + str[5].ToString());
  int year = Convert.ToInt32(str[0].ToString() + str[1].ToString() + str[2].ToString() + str[3].ToString());
  DateTime dt = new DateTime(year, month, day);
  return dt;
}
   
/// <summary>
/// 
/// </summary>
/// <param name="dec"></param>
/// <returns></returns>
public static decimal GetVat(this decimal dec)
{
  return Math.Round((Convert.ToDecimal(dec) - Convert.ToDecimal(Convert.ToDecimal(dec) / Convert.ToDecimal(1.2))), 2);
}

/// <summary>
/// 
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static bool IsBankHoliday(this DateTime date)
{
  System.IO.StreamReader file;
  string line;
  bool check = false;
  bool returnVal = false;
  try
  {
    file = new StreamReader(@"\\*******\*******\*****\UK32Holidays.ics");
    while ((line = file.ReadLine()) != null)
    {
      if (line.StartsWith("DTSTART"))
      {
        if (line.Split(':')[1].ConvertToDateFromYYYYMMDD() == date)
        {
          check = true;
        }
      }
      if (line.StartsWith("SUMMARY") && check)
      {
        if (line.Split(':')[1] == "New Year's Day" || line.Split(':')[1] == "Good Friday" || line.Split(':')[1] == "Easter Monday" || line.Split(':')[1] == "Early May Bank Holiday" || line.Split(':')[1] == "Spring Bank Holiday" || line.Split(':')[1] == "Christmas Day" || line.Split(':')[1] == "Boxing Day" || line.Split(':')[1].StartsWith("Summer Bank Holiday"))
        {
          returnVal = true;
          break;
        }
        else
        {
          returnVal = true;
          break;
        }

      }
    }
    file.Close();
  }
  catch (Exception ex)
  {

  }
  return returnVal;
}

/// <summary> Adds number formatting to string
/// </summary>
/// <param name="str"></param>
/// <param name="includePounds">Include a £ at the beginning</param>
/// <returns></returns>
public static string ConvertToMoney(this string str, bool includePounds = false)
{
  switch (str.Split('.')[0].Length)
  {
    case 4:
      if (!str.StartsWith("-"))
      {
        str = str.Insert(1, ",");
      }
      break;
    case 5:
      str = str.Insert(2, ",");
      break;
    case 6:
      str = str.Insert(3, ",");
      break;
    case 7:
      str = str.Insert(4, ",");
      if (!str.StartsWith("-"))
      {
        str = str.Insert(1, ",");
      }
      break;
    case 8:
      str = str.Insert(5, ",");
      str = str.Insert(2, ",");
      break;
    case 9:
      str = str.Insert(6, ",");
      str = str.Insert(3, ",");
      break;
  }
  if (str.Split('.').Length == 1)
  {
    str += ".00";
  }
  if (str.Split('.')[1].Length == 1)
  {
    str += "0";
  }
  if (includePounds)
  {
    if (!str.StartsWith("-"))
    {
      str = "£" + str;
    }
    else
    {
      str = str.Insert(1, "£");
    }
  }
  return str;
}

 

photo credit: Sh4rp_i via photopin cc