- C# and .NET Core Test Driven Development
- Ayobami Adewole
- 258字
- 2021-06-25 22:00:27
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.
- TypeScript入門與實戰
- Oracle 11g從入門到精通(第2版) (軟件開發視頻大講堂)
- MySQL 8從入門到精通(視頻教學版)
- 認識編程:以Python語言講透編程的本質
- 程序員面試算法寶典
- 編寫高質量代碼:改善Python程序的91個建議
- Python高級機器學習
- Hands-On Automation Testing with Java for Beginners
- Hands-On Full Stack Development with Go
- Learning FuelPHP for Effective PHP Development
- 機器學習與R語言實戰
- Express Web Application Development
- JavaScript程序設計(第2版)
- Beginning C++ Game Programming
- Hands-On JavaScript for Python Developers