[RUBY] When will you do the refactoring?

I usually write Rails, but I was wondering if refactoring should be done.

I've noticed a lot while reading Refactoring: Ruby Edition, so here's a summary.

What is refactoring?

It's trivial, but I'll forget it, so I'll write it down.

Organize the internal structure of the source code without changing the behavior seen from the outside of the program.

[wiki: Refactoring 2020/11/15](https://ja.wikipedia.org/wiki/%E3%83%AA%E3%83%95%E3%82%A1%E3%82%AF%E3% 82% BF% E3% 83% AA% E3% 83% B3% E3% 82% B0_ (% E3% 83% 97% E3% 83% AD% E3% 82% B0% E3% 83% A9% E3% 83 % 9F% E3% 83% B3% E3% 82% B0))

When to refactor?

It is often said that refactoring is always done while developing, but when will you do it concretely? I didn't really understand.

First, consider modifying the existing code and adding functions as an example.

When implementing the above, I thought that refactoring would be done like implementation → test code → refactoring.

When you actually add functionality, you start by understanding the existing code.

Refactoring also includes making the code easier to read, so refactoring may be the right choice when you first understand your existing code.

Therefore, I think it is better to implement in the flow of Understanding existing code → Refactoring → Implementation → Test code → Refactoring.

This way, the first refactoring will make the code easier to understand, and at this point a Pull Request will help other developers understand it.

If you have test code, I think the first refactoring will work, and if you don't have tests, I think you should write it at that time, including understanding the code.

Summary

When doing refactoring, it seems good to do it at the following timing.

Understanding existing code → Refactoring → Implementation → Test code → Refactoring

Recommended Posts

When will you do the refactoring?
When you get lost in the class name
When you want to use the method outside
What do you use when converting to String?
What to do when The SSL certificate has expired
Do you know Decorator?
Do you need dotenv-rails?
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
What to do when you launch an application with rails
When you want to change the MySQL password of docker-compose
What to do if you can't use the rails command
What to do when you want to know the source position where the method is defined in binding.pry
Summarize the words you learned
When reading the source code
If you do not call shutdownNow when the transfer is completed in the Java SDK of AWS S3, threads will continue to remain
In Java 10, when you do gradle eclipse and JavaSE-1.10 comes out, ...
What to do when the changes in the Servlet are not reflected
When you want to ZIP download the image data saved locally
When the processing after conditional branching is redundant-12 [C # refactoring sample]
Why you should add the Override annotation when overriding a method
The first thing to do before you can use Rails6 Ajax
What to do when you become a Problem During Content Assist
What to do when you run into a docker-compose node_modules problem
What to do if you forget the root password in CentOS7
What to do if you cannot roll back the migration (UnknownMigrationVersionError)
The first thing to do when you want to be happy with Heroku on GitHub with Eclipse in Java
What to do if Operation not permitted is displayed when you execute a command in the terminal