Code Refactoring Flashcards
(10 cards)
What is refactoring and why is it beneficial?
An activity by which we modify code but don’t change its functionality. Generally, the purpose of refactoring is to improve readability / understandability or reduce the number of lines of code which is ultimately done to simplify future code changes and reduce the amount of code that has to be maintained.
What is meant by “code smells”?
This is a term used to describe indications of bad design or bad internal quality in code.
What should be refactored?
- Code smells (since they’re an indication of bad design)
- Code that is hard to read or hard to understand
- Code that does not adhere to conventions
- Code that is unnecessary or in the wrong place
What is the most common code smell?
Duplicate code! Two techniques to eliminate duplicate code are extract method and extract superclass.
Extract method
A refactoring pattern used to address duplicate code in which we pull out, or extract, the code that’s duplicated and put it in a new method. Then that method is invoked wherever the code was previously duplicated.
Extract superclass
A refactoring pattern in which a new common superclass is created to contain methods that were previously duplicated across classes. This way the code can be written once in the superclass and then used in multiple subclasses.
Dictionary words
Words that occur in everyday language. These are good to use as variable names, as they tend to be easier to read / recognize in comparison to words that are made up.
Syntactic purpose
For a given token, this is it’s function in that particular position in the statement of code.
What are the 4 primary factors that impact code readability?
Code readability is the ease with which the reader can identify and differentiate tokens and their syntactic purpose. It is important because people need to read code in order to fix bugs, add features, etc. A few of the primary ways to improve readability include:
- Use of whitespace
- Identifier length
- Use of dictionary words
- Variation among identifiers
What are the 4 primary factors that impact code understandability?
Code understandability is the ease with which a reader can identify the semantic meaning of code. A few of the primary ways it can be improved include:
- Use meaningful identifier names
- Use indentation and spacing
- Use comments
- Use punctuation