编写更好的C#代码

我们先来看一个 FizzBuzz 示例。FizzBuzz 要求编写一个程序,遍历从 1 到 100 的数字。其中如果某数字是 3 的倍数,则程序输出 “Fizz”。如果某数字是 5 的倍数,则输出 “Buzz”。如果某数字即是 3 的倍数也是 5 的倍数,则输出 “FizzBuzz”。如果数字既不是 3 的倍数也不是 5 的倍数,则只需输出该数字本身。

 

public static void Test()
    {
      for (int i = 1; i < 101; i++)
      {
        if (i % 3 == 0 && i % 5 == 0)
        {
          Console.WriteLine("FizzBuzz");
        }
        else if (i % 3 == 0)
        {
          Console.WriteLine("Fizz");
        }
        else if (i % 5 == 0)
        {
          Console.WriteLine("Buzz");
        }
        else
        {
          Console.WriteLine(i);
        }
      }
    }

 

什么感觉?这段代码需要改进吗?

 

public static void Check()
    {
      for (int i = 1; i <= 100; i++)
      {
        string output = "";
        if (i % 3 == 0) { output = "Fizz"; }
        if (i % 5 == 0) { output = output + "Buzz"; }
        if (output == "") { output = i.ToString(); }
        Console.WriteLine(output);
      }
    }

现在感觉如何?还能不能进一步改进?

好,让我们来尝试改进下。代码命名对所有软件开发人员来说都是件非常困难的事情。我们花费了大量的时间来做这件事,而且有太多的需要被命名的元素,例如属性、方法、类、文件、项目等。不过我们的确需要花费一些精力在这些命名上,以使代码中的名称更有意义,进而可以提高代码的可读性。

public void DoFizzBuzz()
    {
      for (int number = 1; number <= 100; number++)
      {
        var output = GetFizzBuzzOutput(number);
        Console.WriteLine(output);
      }
    }

    private static string GetFizzBuzzOutput(int number)
    {
      string output = string.Empty;
      if (number % 3 == 0)
      {
        output = "Fizz";
      }
      if (number % 5 == 0)
      {
        output += "Buzz";
      }
      if (string.IsNullOrEmpty(output))
      {
        output = number.ToString();
      }
      return output;
    }

 

一些命名约定示例

在互联网上你可以找到足够多的资源,我只是推荐几个其中我最喜欢的:

这里我展示了一些最基本的示例,但就像我上面已经提到的,找到一个适合你的规范,然后坚持使用。

使用 Pascal Casing 为类和方法命名。

    public class Product
    {
      public void GetActiveProducts()
      {
        //...
      }
      public void CalculateProductAdditinalCost()
      {
        //...
      }
    }

使用 Camel Casing 为方法的参数和局部变量命名。

    public class ProductCategory
    {
      public void Save(ProductCategory productCategory)
      {
        // ...
      }
    } 

不要使用缩写语。

    // Correct
    ProductCategory productCategory;

    // Avoid
    ProductCategory prodCat;

不要在标示符中使用下划线。

    // Correct
    ProductCategory productCategory;

    // Avoid
    ProductCategory product_Category;

在接口名称前使用字母 I 。

    public interface IAddress
    {
    } 

在类的顶端定义所有成员变量,在最顶端定义静态变量。

  public class Product
  {
    public static string BrandName;

    public string Name { get; set; }
    public DateTime DateAvailable { get; set; }

    public Product()
    {
      // ...
    }
  }

使用单数的词汇定义枚举,除非是BitField枚举。

  public enum Direction
  {
    North,
    East,
    South,
    West
  }

不要为枚举名称添加Enum后缀。

  //Avoid
  public enum DirectionEnum
  {
    North,
    East,
    South,
    West
  }
原文链接:http://www.cnblogs.com/gaochundong/archive/2013/06/03/3114489.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值