2018/9/18 Code Smells – CS 242 – Illinois Wiki https://wiki.illinois.edu/wiki/display/cs242/Code+Smells 1/3 页面 / Home 由 Triphol “Pao” Nilkuha (admin)创建于八月 20, 2014 Code Smells Code Smells What is a code smell? Duplicate Code Long Methods/Functions Large Classes Long Parameter List Message Chain Feature Envy Switch statements, nested ifs Temporary Fields Refused Bequest Too Many Bugs Too hard to understand Too hard to change Code Smells What is a code smell? According to Wikipedia, a code smell is “any symptom in the source code of a program that possibly indicates a deeper problem”. Code smells tend to be patterns that commonly show up in source code that when fixed, often lead to better, more maintainable, reliable, and cleaner code. The following is an incomplete list of common code smells with examples and suggested solutions for fixing them. Over the course of the semester, we encourage you to look for these smells in your code and other students code and do your best to fix them in your assignments. Duplicate Code What is it: When segments of source code are repeated throughout the program. How to fix it: Type Solution Duplicate Methods in subclasses Move code to superclass, create a superclass if needed Duplicate expressions in superclass Extract duplicates into their own methods Duplicate expressions in different classes Extract duplicates to a common component Long Methods/Functions What is it: When methods or functions are excessively long How to fix it: Type Solution Code that will not fit on a page Extract functions from long fragments Can’t think of the function all at once