Daily Pattern (Software Architecture and Design, Refactoring)
882 subscribers
32 photos
39 links
Software design patterns and principles, tips, code quality, software architecture, refactoring

Java, Scala, Kotlin

Feedback, реклама: @dailypatternfeedbackbot
Download Telegram
#refactoring Decompose Conditional

Problem:
You have a complex conditional (if-then/else or switch).

Solution: Decompose the complicated parts of the conditional into separate methods: the condition, then and else.
Replace Type Code with Subclasses #refactoring

Problem
:
You have a coded type that directly affects program behavior (values of this field trigger various code in conditionals).

Solution:
Create subclasses for each value of the coded type. Then extract the relevant behaviors from the original class to these subclasses. Replace the control flow code with polymorphism.

https://sapanparikh18.github.io/Refactoring-Part-2/
Split Temporary Variable #refactoring

Problem
You have a local variable that’s used to store various intermediate values inside a method (except for cycle variables).

Solution
Use different variables for different values. Each variable should be responsible for only one particular thing.

Why Refactor

If you’re reusing variables inside a function for various unrelated purposes, you’re sure to encounter problems as soon as you need to make changes to the code containing the variables. You will have to recheck each case of variable use to make sure that the correct values are used.

Benefits

- Each component of the program code should be responsible for one and one thing only.

- Code becomes more readable.

- This refactoring technique is useful if you anticipate using Extract Method later.

P. S. In Scala and Kotlin it's best practice to use val (final in Java) instead of var as much and possible so you will never have this problem.
Extract Variable #refactoring

Problem: you have an expression that’s hard to understand.

Solution: place the result of the expression or its parts in separate variables that are self-explanatory.