官术网_书友最值得收藏!

Preventing and detecting code smell

Programming styles and coding formats that result in code smell should be avoided. By adequately paying attention to the details, bad code pointers discussed in the Code smell section should be avoided. The replicated lines of code in the two methods of the source code mentioned in the Code smell section can be refactored to a third method. This avoids replication of code and allows for easy modifications:

[HttpGet]
public ActionResult GetAllTransactions()
{
var yearsAndMonths=GetYearsAndMonths();
ViewBag.Transactions= GetTransactions(yearsAndMonths.Item1,yearsAndMonths.Item2);
return View();
}

[HttpGet]
public ActionResult SearchTransactions()
{
var yearsAndMonths=GetYearsAndMonths();
ViewBag.Years = yearsAndMonths.Item1;
ViewBag.Months = yearsAndMonths.Item2;
return View();
}

private (List<string>, List<string>) GetYearsAndMonths(){
List<string> years = new List<string>();
for (int i = DateTime.Now.Year; i >= 2015; i--)
years.Add(i.ToString());
List<string> months = new List<string>();
for (int j = 1; j <= 12; j++)
months.Add(j.ToString());
return (years,months);
}

Also, the method with a long list of parameters in the Code smell section can be refactored to use C# Plain Old CLR Object (POCO) for clarity and reusability:

public void ProcessTransaction(Transaction transaction)
{
//Do something
}

public class Transaction
{
public string Username{get;set;}
public string Password{get;set;}
public float TransactionAmount{get;set;}
public string TransactionType{get;set;}
public DateTime Time{get;set;}
public bool CanProcess{get;set;}
public bool RetryOnfailure{get;set;}
}

Development teams should have guidelines, principles, and coding conventions and standards developed jointly by the team members and should be constantly updated and refined. These, when used effectively, will prevent code smell in the software code base and allow for the easy identification of potential bad code by team members.

主站蜘蛛池模板: 汝州市| 武宁县| 黄梅县| 辛集市| 新民市| 鄄城县| 明水县| 石嘴山市| 中卫市| 淮南市| 承德县| 新竹市| 利辛县| 太仆寺旗| 电白县| 阳泉市| 三明市| 互助| 汝城县| 黎城县| 吴川市| 铁力市| 秦安县| 寿光市| 汉中市| 湟源县| 冀州市| 石楼县| 那曲县| 青铜峡市| 都兰县| 罗城| 崇信县| 稻城县| 平阴县| 资溪县| 南澳县| 砀山县| 客服| 玛多县| 刚察县|