代码简洁(注意事项)

internal class CleanCode
{
private CultureInfo m_CurrentCulture;
private CultureInfo m_CurrentUICulture;
private int m_Priority;
private string m_Name;

//提供有意义的命名

//一行语句也尽量使用大括号
private string FindElement(List<string> argElementList, string argDest)
{
if (argElementList == null || argElementList.Count == 0)
{
return "";
}
else
{
// return (from c in argElementList where c == argDest select c).First();

//return argElementList.FindAll((value) => { return value == "张三"; });
return argElementList.Find((value) => { return value == "张三"; });

}
}

#region 方法抽象级别在同一层次上

public void Init()
{
LocalInit();
RemoteInit();
}

private void LocalInit()
{
}

private void RemoteInit()
{
}
#endregion

/*单一职责原则:一个方法只做一件事
*
* 避免过长的方法或类:
* 应该考虑是否可以拆成几个小的方法
* 一般类型的代码超过300行则要考虑是否重构
* 一般方法的代码超过30行、一屏也要考虑是否重构
*
* 只对外公布必要的操作:
* 比如类、方法、属性,默认应该是private的
*
* 重构多个相关属性为一个类
*
* 不重复代码:
* 代码重复不利于维护
*
* 避免过长的if、switch分支:使用“表驱动法”(查字典)
*
*
*/


#region “表驱动法”(查字典)

public enum Week
{
Monday, Tuseday, Wednesday, Thursday, Friday, Saturday, Sunday
}
static string GetChineseWeekInTable(Week argWeek)
{
//调用:GetChineseWeekInTable(Week.Friday)

string[] chineseWeek = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
return chineseWeek[(int)argWeek];
}

/// <summary>
/// 调用方式
/// </summary>
void InvokeTest()
{
SampleClass sample = new SampleClass();
var addMethod = typeof(SampleClass).GetMethod((ActioInTable(Week.Monday)));
addMethod.Invoke(sample, null);
}
static string ActioInTable(Week argWeek)
{
string[] methods = { "Cleaning", "CleanCloset", "Working", "Working", "Working", "Shopping", "Shopping" };
return methods[(int)argWeek];
}

class SampleClass
{
public void Cleaning()
{
Console.WriteLine("Cleaning..");
}
public void CleanCloset()
{
Console.WriteLine("CleanCloset..");
}
public void Working()
{
Console.WriteLine("Working..");
}
public void Shopping()
{
Console.WriteLine("Shopping..");
}
}

#endregion

}

你可能感兴趣的:(代码)