- C# and .NET Core Test Driven Development
- Ayobami Adewole
- 302字
- 2021-06-25 22:00:26
Code smell
Code smell is a term that was first used by Kent Beck, which indicates deeper issues in the source code. Code smell in a code base can come from having replications in the source code, use of inconsistent or vague naming conventions and coding styles, creating methods with a long list of parameters, and having monster methods and classes, that is methods or classes that know and do too much thereby violating the single responsibility principle. The list goes on and on.
A common code smell in the source code is when a developer creates two or more methods that perform the same action with little or no variation or with program details or facts that ought to be implemented in a single point replicated in several methods or classes, leading to a code base that is not easy to maintain.
The following two ASP.NET MVC action methods have lines of code that create a strongly-typed list of strings of years and months. These lines of code, that could easily have been refactored into a third method and called by both methods, have been replicated in these two methods:
[HttpGet]
public ActionResult GetAllTransactions()
{
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());
ViewBag.Transactions= GetTransactions(years,months);
return View();
}
[HttpGet]
public ActionResult SearchTransactions()
{
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());
ViewBag.Years = years;
ViewBag.Months = months;
return View();
}
Another common code smell occurs when developers create methods with a long list of parameters, as in the following method:
public void ProcessTransaction(string username, string password, float transactionAmount, string transactionType, DateTime time, bool canProcess, bool retryOnfailure)
{
//Do something
}
- jQuery Mobile Web Development Essentials(Third Edition)
- Instant Testing with CasperJS
- 數(shù)據(jù)庫程序員面試筆試真題與解析
- C++ Builder 6.0下OpenGL編程技術(shù)
- Java Web及其框架技術(shù)
- Instant 960 Grid System
- Ray分布式機(jī)器學(xué)習(xí):利用Ray進(jìn)行大模型的數(shù)據(jù)處理、訓(xùn)練、推理和部署
- Python應(yīng)用輕松入門
- Mastering Ext JS
- Python之光:Python編程入門與實戰(zhàn)
- Visual Basic程序設(shè)計(第三版)
- Qlik Sense? Cookbook
- C++ System Programming Cookbook
- Python大規(guī)模機(jī)器學(xué)習(xí)
- Python物理建模初學(xué)者指南(第2版)