C# Bad Code Examples

Date: 27 Aug 2010 Comments: 0

After the success of PHP Bad Code Examples, I decided to write another one about bad code examples, but this time it’s about C# :) Enjoy:

Example 1.

/// <summary>
/// Returns current UTC time
/// </summary>
/// <returns></returns>
public static DateTime GetCurrentTimeUtc()
{
       DateTime dt = DateTime.UtcNow;
       return dt;
}

Example 2.

bool value;
if(value.ToString.Length() == 4)
  return true;
else if(value.ToString.Length() == 5)
  return false;

else
  return !true && !false;

Example 3.

private void UpdateLabel(string str)
{
   if (str == "!0123clear3210!")
   {
       textBox1.Clear();
       return;
   }
   textBox1.AppendText(str);
}

Example 4.

if ((armid == "3504000036") || (armid == "3504000034"))
{
    if (armid == "3504000036")
    {
       PlaceSave = "3243100001";
    }
    if (armid == "3504000034")
    {
       PlaceSave = "2311030001";
    }
}

Example 5.

void delete_ServerClick(object sender, ImageClickEventArgs e)
{
  Parent.FindControl(ID).Visible = false;
}

Example 6.

int a=0;
if (a != 0)
{
 a=0;
}
else a=0;

Example 7.

bool exitstatus = true;
if (!extremalexit) { exitstatus = true; }

Example 8.

if (a == null == false) {
}

Example 9.

try
{
    ...
    if (obj == null)
    {
        LogManager.GetLogger("Log").Warn(msg);
        throw new SoapException("Object not found", SoapException.ServerFaultCode);
    }
}
catch (Exception ex)
{
    LogManager.GetLogger("Log").Error(msg, ex);
    throw new SoapException("Object not found, SoapException.ServerFaultCode, ex);
}

Example 10.

protected string TryToUpper(string text)
{
      StringBuilder sb = new StringBuilder();
      foreach(char t in text)
      {
      	try
            {
            	sb.Append(t.ToString().ToUpper());
            }
            catch
            {
            	sb.Append(t.ToString());
            }
      }
      return sb.ToString();
}

Leave a Reply