Refactoring Code
In the software companies, there will be times when old software code (also legacy code) needs to re-written with newer optimizations and syntax. This does not add a new feature but improves the working of the existing code. This process is called as refactoring code.
Advantages of refactoring
- Improved code readability
- Reduced code complexity
- Improved performance
- Coding style compliance
At then end the goal is to achieve simpler, cleaner, faster and code compliant code without changing user functionality.
Refactoring practical
Goal of the module is to fix problems with the existing code and refactor the code.
The module provides 2 sample example code in 2 coding languages (CPP & Python)
-
Get the workspace for refactoring
-
Download the ready to use workspace for refactoring - https://gitlab.iotiot.in/newbies/pre-office-office/refactoring-workspace/-/archive/master/refactoring-workspace-master.zip
-
Unzip the
refactoring-workspace-master.zip
-
Open VScode.
-
Press
Ctrl
+Shift
+p
in VScode. A bar will open up at the top. -
Click on "Remote-Containers: Open Folder in Containers".
-
In the pop up Window, Navigate to Downloads --> refactoring-workspace-master
-
Click on Open.
-
Vscode will take some time to start up the container, once done
-
That is it done you have successfully opened up the workspace. Now you can get to refactoring.
-
-
Fork the refactoring repository.
-
Go to link - https://gitlab.iotiot.in/newbies/pre-office-office/refactoring-trial
-
Click on Fork.
-
Select your username in the next window.
-
The repo will fork and you should be directed to your own copy of the original repository.
-
-
Clone the repository.
-
Choose a coding language Python or CPP to refactor code. (choose the language that you are most comfortable with)
- Type
cd codestyle/{language}
(for ex:cd codestyle/cpp
)
- Type
-
Choose any one sample code to refactor. There are 2 sample codes
- yhatzee-game - Click here to read code doc
- tennis - Click here to read code doc
-
Check coding style of the codes
-
For python
Click to see your steps!
- Make sure that you are in the folder codestyle/python, Check by running command
pwd
- Type
cd {your-chosen-example}
in VScode terminal. (for ex:cd tennis
) - Run command
- For Tennis
pycodestyle tennis.py
- For yhatzee-game
pycodestyle yahtzee.py
- For Tennis
- After running the command you will get a big list of errors in the coding style.
- Make sure that you are in the folder codestyle/python, Check by running command
-
For CPP
Click to see your steps!
-
-
Make changes to the code
-
For Python Example
Click to see your steps!
-
For CPP Example
Click to see your steps!
-
Lets try to solve first 4 errors thrown by the codestyle check
-
There seems to be errors, due brackets missing in
if
condition, Lets fix these errors -
After fixing the errors the codestyle does not throw the errors anymore
-
Yay! the code has been refactored, Now test it so that it clears all the tests.
-
-
-
Test if your refactored code passes all the tests.
-
For Python
Click to see your steps!
- Run command
pytest
- You will see your refactored code get tested. If anything fails the test will report the failure.
- Run command
-
For CPP
Click to see your steps!
- For Tennis example, run commands
make ./tennis1_tests
- For yhatzee-game, run commands
make ./yahtzee_tests
- You will see your refactored code get tested. If anything fails the test will report the failure.
- For Tennis example, run commands
-
-
Repeat the steps 6, 7, 8 till all the code style errors are solved.
-
Submit the changes.
-
Run commands
cd /home/shunya/refactoring-trial git config --global user.name "{Your-name}" #(for ex: `git config --global user.name "Yogesh Hegde"`) git config --global user.email "{your-email-id}" #(for ex: `git config --global user.email "yogesh@iotiot.in"`) git add . git commit -m "refactored code" git push
-
-
See your code Build, Test and Deploy via CI/CD.
-
Thats is it done, you have successfully Refactored your code and see it Built, Tested and Deployed via CI/CD.