TOC

Generating TOC...

2.3. Refactoring Safely with AI

The key to refactoring is for people to take the initiative in breaking down tasks into smaller pieces, repeating this process multiple times, and creating code that works exactly as intended. Here, three refactoring methods for this purpose will be discussed.

1. Human-driven development (HDD)

Collaboration with AI is rapidly changing the landscape of development. AI-generated code may look appealing, but the key to successful refactoring still lies with people. โ€œHuman-Driven Development (HDD)โ€ refers to the principles and approaches for creating better software by proactively utilizing AI as a tool. In this chapter, specific methods for successfully refactoring while maintaining clear control and not being led by AI will be examined.

Safety net for refactoring

The basic premise of refactoring is always the same: โ€œEnsure that existing code continues to work.โ€ If the legacy code is good, automated testing and a testing environment will have been established. In such cases, refactoring can be completed by reinforcing existing tests.

Unfortunately, there are many legacy codes that do not have tests for various reasons. In such cases, it is recommended to write test code based on working code before refactoring. By writing test code first, it can be ensured that the code works and refactoring can proceed smoothly. The operation of existing services can be ensured through testing at any time, which is extremely helpful in understanding legacy code. Of course, this is also an excellent example of working with AI.

## ๐Ÿ›  ์ง€์‹œ ํ•ญ๋ชฉ ๋ฐ ์ถœ๋ ฅ ํ˜•์‹
1. **ํ…Œ์ŠคํŠธ ๊ฐ€๋Šฅ ์˜์—ญ ์‹๋ณ„**
- ์ฝ”๋“œ์—์„œ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๊ฐ€ ๊ฐ€๋Šฅํ•œ ๋ถ€๋ถ„์€ ์–ด๋–ค ๊ฒƒ์ธ๊ฐ€?
- ์กฐ๊ฑด ๋ถ„๊ธฐ, ์ˆœ์ˆ˜ ํ•จ์ˆ˜, ํ•ต์‹ฌ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์ด ๋ช…ํ™•ํžˆ ๋ถ„๋ฆฌ๋˜์–ด ์žˆ๋Š”๊ฐ€?
- ๋งŒ์•ฝ ๋ถ„๋ฆฌ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด, ํ…Œ์ŠคํŠธ ๊ฐ€๋Šฅ์„ฑ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด ์–ด๋–ป๊ฒŒ ๊ตฌ์กฐ ๊ฐœ์„ ์ด ํ•„์š”ํ•œ๊ฐ€?
- ๐Ÿ” ์ถœ๋ ฅ ํ˜•์‹:
	โœ… ํ…Œ์ŠคํŠธ ๊ฐ€๋Šฅ: [์˜ˆ: validateEmail ํ•จ์ˆ˜ โ€“ ์ˆœ์ˆ˜ ํ•จ์ˆ˜]
	โŒ ํ…Œ์ŠคํŠธ ๋ถˆ๊ฐ€: [์˜ˆ: ๊ฒฐ์ œ ์ฒ˜๋ฆฌ ๋กœ์ง โ€“ DB ์ ‘๊ทผ๊ณผ ๋กœ์ง์ด ์„ž์—ฌ ์žˆ์Œ]
	โ†’ ๊ฐœ์„  ์ œ์•ˆ: [์˜ˆ: DB ์ ‘๊ทผ ๋ถ€๋ถ„์„ Repository๋กœ ์ถ”์ถœ]
2. **ํ…Œ์ŠคํŠธ ์šฐ์„ ์ˆœ์œ„ ํŒ๋‹จ**
- ์ฝ”๋“œ ์ค‘ ํ…Œ์ŠคํŠธ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ์˜์—ญ์€ ์–ด๋””์ธ๊ฐ€?
- ๋กœ์ง ๋ณต์žก๋„, ๋ณ€๊ฒฝ ์œ„ํ—˜๋„, ์™ธ๋ถ€ ์˜์กด๋„(API/DB ๋“ฑ) ๊ธฐ์ค€์œผ๋กœ ํŒ๋‹จ.
- ๐Ÿ” ์ถœ๋ ฅ ํ˜•์‹:
	๐Ÿ”บ ์šฐ์„ ์ˆœ์œ„ ๋†’์Œ: [๊ฒฐ์ œ ์Šน์ธ ๋กœ์ง] โ€“ ์™ธ๋ถ€ ๊ฒฐ์ œ API์™€ ๊ธด๋ฐ€ํžˆ ์—ฐ๊ด€๋จ
	๐Ÿ”ป ์šฐ์„ ์ˆœ์œ„ ๋‚ฎ์Œ: [UI ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ „ํ™˜] โ€“ ๋ณ€๊ฒฝ ์œ„ํ—˜๋„ ๋‚ฎ๊ณ  ์‚ฌ์šฉ์ž ๊ฐ€์น˜ ์˜ํ–ฅ ์ ์Œ
3. **ํ…Œ์ŠคํŠธ ์–ด๋ ค์›€์˜ ์„ค๊ณ„์  ์›์ธ ๋ถ„์„**
- ํ…Œ์ŠคํŠธ ์ž‘์„ฑ์„ ์–ด๋ ต๊ฒŒ ๋งŒ๋“œ๋Š” ์„ค๊ณ„์  ๊ฒฐํ•จ์€ ๋ฌด์—‡์ธ๊ฐ€?
- ์ „์—ญ ์ƒํƒœ, ๊ฒฐํ•ฉ๋„, ์˜์กด์„ฑ ๋“ฑ ๊ตฌ์กฐ์  ์žฅ์• ์š”์†Œ๋ฅผ ์ง„๋‹จํ•˜๋ผ.
- ๐Ÿ” ์ถœ๋ ฅ ํ˜•์‹:
	โ— ์ „์—ญ ์ƒํƒœ: globalConfig๊ฐ€ ๋ชจ๋“ˆ ์ „์ฒด์— ์˜ํ–ฅ์„ ์คŒ
	โ— ํ•˜๋“œ์ฝ”๋”ฉ๋œ ์˜์กด์„ฑ: FileWriter๊ฐ€ ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์ง์ ‘ ์ƒ์„ฑ๋จ
4. **ํ…Œ์ŠคํŠธ ๋ ˆ๋ฒจ ํŒ๋‹จ**
- ๊ฐ ์˜์—ญ์— ๋Œ€ํ•ด ์–ด๋–ค ํ…Œ์ŠคํŠธ ๋ ˆ๋ฒจ(Unit / Integration / E2E)์ด ๊ฐ€์žฅ ์ ์ ˆํ•œ๊ฐ€?
- ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ์šฐ์„  ๊ณ ๋ คํ•˜๋˜, ์ œํ•œ์ด ์žˆ์„ ๊ฒฝ์šฐ๋„ ๋ช…์‹œํ•˜๋ผ.
- ๐Ÿ” ์ถœ๋ ฅ ํ˜•์‹:
	[๊ฒฐ์ œ ๋กœ์ง]
	- ์ถ”์ฒœ ํ…Œ์ŠคํŠธ: ์œ ๋‹›
	- Stub ๋Œ€์ƒ: ์™ธ๋ถ€ PG API
5. **Mock/Stub/Test Double ์ „๋žต ์ˆ˜๋ฆฝ**
- ์–ด๋–ค ๋ถ€๋ถ„์— Test Double์ด ํ•„์š”ํ•œ๊ฐ€? ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ๊ตฌ์กฐ ๊ฐœ์„ ์œผ๋กœ mocking ํšŒํ”ผ ์œ ๋„.
- ๊ฐ Double์— ๋Œ€ํ•ด ๊ฐ„๋‹จํ•œ ๊ตฌํ˜„ ์˜ˆ์‹œ์™€ ๋ชฉ์ ์„ ์ œ์‹œํ•˜๋ผ.
- ๐Ÿ” ์ถœ๋ ฅ ํ˜•์‹:
	Stub: PaymentGateway โ†’ ์˜ˆ์‹œ: class FakePaymentGateway { ... }
	๋ชฉ์ : ๊ฒฐ์ œ ์Šน์ธ ์‘๋‹ต์„ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ํ•˜์—ฌ ์‹ค์ œ API ํ˜ธ์ถœ ์ œ๊ฑฐ
6. **ํ…Œ์ŠคํŠธ ๋ณด์žฅ ์ˆ˜์ค€ ๋ฐ ํ•œ๊ณ„**
- ์œ„ ์ „๋žต์œผ๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ–ˆ์„ ๋•Œ, ๋ฆฌํŒฉํ† ๋ง ์‹œ ๋ณด์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ”์œ„๋Š” ์–ด๋””๊นŒ์ง€์ธ๊ฐ€?
- ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ์ปค๋ฒ„๋ฆฌ์ง€์˜ ๊ฐ•์ ๊ณผ ํ•œ๊ณ„๋Š” ๋ฌด์—‡์ธ๊ฐ€?
- ๐Ÿ” ์ถœ๋ ฅ ํ˜•์‹:
	โœ… ๋ณด์žฅ ๊ฐ€๋Šฅ: ๋‚ด๋ถ€ ๋กœ์ง ๋ณ€๊ฒฝ ์‹œ ๊ธฐ๋Šฅ ์•ˆ์ •์„ฑ ์œ ์ง€
	โš  ๋ณด์žฅ ํ•œ๊ณ„: ์™ธ๋ถ€ API ์‘๋‹ต ํ˜•์‹์ด ๋ณ€๊ฒฝ๋  ๊ฒฝ์šฐ ํƒ์ง€ ๋ถˆ๊ฐ€
---
## ๐Ÿ’ก ์ฃผ์˜์‚ฌํ•ญ
- ํ…Œ์ŠคํŠธ ๋ ˆ๋ฒจ์€ โ€˜๊ฐ€๋Šฅํ•œ ์ตœ์†Œ ๋‹จ์œ„์—์„œ ์‹œ์ž‘โ€™ํ•˜๋Š” ๊ฒƒ์„ ์šฐ์„  ์›์น™์œผ๋กœ ํ•  ๊ฒƒ
- ๊ตฌ์กฐ ๊ฐœ์„ ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ **๋‹จ๊ณ„์  ๋ถ„๋ฆฌ ์ „๋žต**์„ ์ œ์•ˆํ•  ๊ฒƒ (ex: ์ถ”์ถœ โ†’ ์ถ”์ƒํ™” โ†’ ์ธํ„ฐํŽ˜์ด์Šค ์ „ํ™˜)
- ๋‹ต๋ณ€์€ ์ฝ”๋“œ ์˜ˆ์‹œ, ํŒ๋‹จ ๊ทผ๊ฑฐ, ์˜ˆ์ƒ ๋ฆฌ์Šคํฌ๋ฅผ ํฌํ•จํ•˜์—ฌ ์‹ค๋ฌด ์—”์ง€๋‹ˆ์–ด๊ฐ€ ๋ฐ”๋กœ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋„๋ก ์ž‘์„ฑํ•  ๊ฒƒ

The phrase โ€œwithout TDDโ€ may seem puzzling. Due to the nature of analyzing legacy code, development is not done in a test-driven manner. This section has been intentionally added to minimize unnecessary suggestions.

It is also effective to research and specify a testing framework in advance in order to add tests effectively. If there is a testing framework that is familiar, or if the legacy code itself is old and requires specification of the testing framework version, AI can be asked for specific suggestions and appropriate recommendations received.

If the first test has been successfully completed, the next cycle can be moved to by submitting documents such as planning documents and policy documents along with the context to reinforce testing of business logic. There is no legacy code as reliable as legacy code that has been thoroughly tested.

Plan and review repeatedly

In AI-powered code writing, refactoring is an area that is as useful as new development. In particular, legacy code refactoring is an area where AI can be utilized more effectively, as it involves modifying code that is already functioning. You can give the entire project to AI and have it refactor it. However, in this case, it may work well, but the results may differ from what you expected. This is likely to result in an overwhelming amount of changes in response to your request.

It is recommended to go through the stages of planning and reviewing rather than entrusting refactoring to AI right away. Plans should be reviewed as if talking to a colleague, and AI will gladly review them.

<ํ”„๋กฌํ”„ํŠธ>
# ์—ญํ• 
๋„ˆ๋Š” ๋น„ํŒ์ ์ด๋ฉด์„œ๋„ ๋…ธ๋ จํ•œ ์‹œ๋‹ˆ์–ด ์†Œํ”„ํŠธ์›จ์–ด ์—”์ง€๋‹ˆ์–ด์ด์ž ์•„ํ‚คํ…ํŠธ๋‹ค. ๋‚˜๋Š” ๋„ˆ์™€ ํ•จ๊ป˜ ์ฝ”๋“œ๋ฅผ ๋ฆฌํŒฉํ† ๋งํ•˜๋ ค๊ณ  ํ•œ๋‹ค.

# ์ ˆ์ฐจ (๋ชจ๋“  ๋‹จ๊ณ„๋ฅผ ๋ฐ˜๋“œ์‹œ ์ˆœ์ฐจ์ ์œผ๋กœ ๋”ฐ๋ฅด๋„๋ก ํ•ด)
1.  **์ฝ”๋“œ ๊ตฌ์กฐ ๋ถ„์„**
	- ๋‚ด๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์•„ํ‚คํ…์ฒ˜ ๊ด€์ ์—์„œ ๊ตฌ์กฐ์ ์œผ๋กœ ๋ถ„์„ํ•ด์ค˜.
	- ๋ชจ๋“ˆ ๊ตฌ์„ฑ, ๋ ˆ์ด์–ด ๊ตฌ๋ถ„, ์˜์กด ๊ด€๊ณ„ ๋“ฑ์„ ํŒŒ์•…ํ•ด.
2.  **๊ตฌ์ฒด์ ์ธ ๋ฌธ์ œ์  ์ง„๋‹จ**
	๋‹ค์Œ ํ•ญ๋ชฉ๋ณ„๋กœ **๊ตฌ์ฒด์ ์ธ ๋ฌธ์ œ์ **์„ ์ง€์ ํ•ด์ค˜:
	- ์„ค๊ณ„ ๋ฐ ์ฑ…์ž„ ๋ถ„๋ฆฌ (SRP ์œ„๋ฐฐ ์—ฌ๋ถ€ ๋“ฑ)
	- ์ถ”์ƒํ™” ์ˆ˜์ค€๊ณผ ์ค‘๋ณต๋„
	- ๋„ค์ด๋ฐ ์ผ๊ด€์„ฑ๊ณผ ์˜๋„ ์ „๋‹ฌ๋ ฅ
	- ํ…Œ์ŠคํŠธ ์šฉ์ด์„ฑ ๋ฐ ๊ฒฐํ•ฉ๋„
	- ์œ ์ง€๋ณด์ˆ˜์„ฑ๊ณผ ํ™•์žฅ์„ฑ
3.  **๋ฆฌํŒฉํ† ๋ง ๊ณ„ํš ์ˆ˜๋ฆฝ (์ฝ”๋“œ ๋ณ€๊ฒฝ ์ „ ๋‹จ๊ณ„)**
	- ๋‹จ์ˆœ ์ž‘์—… ๋ชฉ๋ก์ด ์•„๋‹Œ, **์ „๋ฐ˜์ ์ธ ๋ฆฌํŒฉํ† ๋ง ๋ฐฉํ–ฅ์„ฑ๊ณผ ๋‹จ๊ณ„๋ณ„ ์ „๋žต**์„ ์ œ์‹œํ•ด์ค˜.
	- ๊ฐ ๋‹จ๊ณ„์˜ ์šฐ์„ ์ˆœ์œ„, ์˜ˆ์ƒ ๋ฆฌ์Šคํฌ๋„ ๊ฐ„๋žตํžˆ ์•Œ๋ ค์ค˜.
4.  **์„ค๊ณ„์  ๊ทผ๊ฑฐ ์ œ์‹œ**
	- ์œ„ ๊ณ„ํš์ด ์™œ ํƒ€๋‹นํ•œ์ง€ ์•„๋ž˜ ์›์น™๋“ค์„ ๋ฐ”ํƒ•์œผ๋กœ ์„ค๋ช…ํ•ด์ค˜:
		- SOLID, KISS, DRY
		- ํด๋ฆฐ ์•„ํ‚คํ…์ฒ˜ / ๊ณ„์ธตํ˜• ๊ตฌ์กฐ / ๋„๋ฉ”์ธ ์ค‘์‹ฌ ์„ค๊ณ„
		- ํ…Œ์ŠคํŠธ ์ „๋žต ๋ฐ ์˜์กด์„ฑ ๋ถ„๋ฆฌ
5.  **๋Œ€์•ˆ์  ๋ฐฉํ–ฅ ์ œ์‹œ (์„ ํƒ ์‚ฌํ•ญ)**
	- ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ๋‹ค๋ฅธ ์„ค๊ณ„ ์ ‘๊ทผ ๋ฐฉ์‹๋„ ์ œ์•ˆํ•ด์ค˜.
	- ๊ฐ๊ฐ์˜ ์žฅ์ /๋‹จ์ ์„ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์š”์•ฝํ•ด์ค˜.
6.  **๋‚ด ํ™•์ธ ํ›„ ๋‹ค์Œ ๋‹จ๊ณ„ ์ง„ํ–‰**
	- ๋‚ด ๋™์˜ ์—†์ด ๋‹ค์Œ ๋‹จ๊ณ„๋กœ ๋„˜์–ด๊ฐ€์ง€ ๋งˆ. ๋ฐ˜๋“œ์‹œ "์ง„ํ–‰ํ•ด๋„ ๋ ๊นŒ์š”?"๋ผ๊ณ  ๋ฌผ์–ด๋ด.

# ๋ฆฌํŒฉํ† ๋ง ์ฒ ํ•™ (๋ชจ๋“  ํŒ๋‹จ์˜ ๊ธฐ์ค€์œผ๋กœ ์‚ผ์„ ๊ฒƒ)
- ๋น„์ฆˆ๋‹ˆ์Šค์™€ ๋„๋ฉ”์ธ์— ๋งž๋Š” ์ฑ…์ž„ ๋ถ„๋ฆฌ
- ํ…Œ์ŠคํŠธ ๊ฐ€๋Šฅํ•œ ๊ตฌ์กฐ์™€ ์˜์กด์„ฑ ๋ถ„๋ฆฌ
- ์˜๋„๊ฐ€ ๋ช…ํ™•ํžˆ ๋“œ๋Ÿฌ๋‚˜๋Š” ์ฝ”๋“œ
- ๋ฏธ๋ž˜์˜ ์š”๊ตฌ ๋ณ€๊ฒฝ์— ์œ ์—ฐํ•˜๊ฒŒ ๋Œ€์‘ํ•  ์ˆ˜ ์žˆ๋Š” ์„ค๊ณ„

If the scope of the code to be refactored is provided using the prompt above, the AI will respond kindly. Based on the first response, further review is necessary. Itโ€™s only been a few seconds since the refactoring code was given to AI.

It is recommended to repeat the process of discussing the reasons for refactoring and the expected risks with AI until the desired level of design is achieved. If code proposals based on hasty decisions are received, more time will be spent reviewing the proposed code. If any problems are found during review, the cause should be clearly explained and AI requested to review again.

The productivity that AI brings can sometimes seem like magic. However, it is safer to treat AI that assists with refactoring as an enthusiastic new employee rather than an experienced senior employee. This is because if there are no restrictions, people tend to start tinkering with things and end up changing the structure. AI provides probabilistic answers, so the proposed structure may seem good at first glance.

However, unless the structural changes are part of the original plan, they should be approached conservatively. This is because unplanned structural changes can disrupt the harmony of the entire system. As conversations with AI are repeated, things that should and should not be done will emerge. These should be reflected in the prompt and the discussion continued.

While exchanging messages, it is important to remember to make clear requests to the AI. It is beneficial to ask AI questions as if you were asking yourself what parts you would like to refactor in order to refine your thinking, but if vague requests are made, only plausible suggestions will be received. An example of an ambiguous request is โ€œPlease improve this code.โ€

Like TDD (Test Driven Development), HDD (Human Driven Development) must be practiced. Since accepting code suggestions is also done by humans, how AI is handled depends entirely on humans.

2. Repeat refactoring

Refactoring with AI is not like moving a huge rock all at once, but rather like breaking it into smaller pieces and moving them several times. The key to this process is โ€œiteration.โ€

Request in small units and commit

After reviewing the refactoring plan, a clear understanding of what needs to be changed and where to start should be had. The next step in the refactoring plan is to address requests such as โ€œPlease fix this,โ€ which can lead to too many changes and require complex review of multiple contexts at once.

Instead of saying, โ€œPlease refactor this class,โ€ it is much safer and more effective to request specific changes in small, clear units, such as โ€œPlease improve the readability of this method (specifying the specific method)โ€ or โ€œPlease remove duplicate code from this function.โ€ If the code suggested by AI is satisfactory and passes testing, the refactoring details should be committed. There is always a possibility that AI will ruin the code beyond the scope of the request.

Refactor again at a faster cycle

What does it mean to refactor again at a faster cycle? Even if AI is asked to refactor code that has been carefully refactored, it will gladly comply. This means that the implementation details can be quickly reviewed while reviewing the newly implemented results. This also means that lessons learned through refactoring can be applied again without having to wait too long. At this point, it is useful to compare specific commits with the current version, reconfirm requirements, and make new requests.

By revisiting the initial purpose of refactoring and incorporating the technical insights gained during the refactoring process into the initial purpose, a better plan can be created from scratch. Without AI, these tasks would have required a significant investment of time and mental energy. โ€œRefactor what was just refactored from scratch?โ€ AI kindly optimizes this process, which can be exhausting for humans.

3. Adjusting the size of the task

By communicating clear intentions to AI and exchanging feedback, a huge amount of code can be generated in no time. However, the essence is not to create a lot of new code, but to maintain the quality of the service and the code itself so that it works exactly as intended.

Trust productivity and avoid over-engineering

Once you experience how well refactoring works after a few conversations, you may develop more advanced desires. An example of this would be pursuing engineering beauty beyond code that works well. Requirements that go beyond the scope planned at the beginning of refactoring, such as adding unnecessary design patterns or considering unnecessary scalability, should be avoided. Such changes not only undermine the consistent design of the entire service code, but also increase uncontrolled complexity wherever refactoring is performed. If something is realized while refactoring, it should not be reflected spontaneously, but rather refactored while considering harmony with the entire service code.

Refactoring based on PR

Talking to AI isnโ€™t just about getting code. It should be proceeded by clearly stating the intent and details of the changes, as if writing a PR (Pull Request) to send to a colleague. The proper role of AI is to assist developers in writing code, not to boast about the superiority of the model it uses to fellow developers who are reviewing the code.

<ํ”„๋กฌํ”„ํŠธ>

# [๋ฆฌํŒฉํ† ๋ง PR ๋ฆฌ๋ทฐ ์š”์ฒญ]
์•„๋ž˜ GitHub Draft PR์˜ ์„ค๋ช…๊ณผ ์ฝ”๋“œ diff๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ๋‹ค์Œ์„ ๋„์™€์ค˜:
1. PR ์„ค๋ช…์„ ๋ฐ”ํƒ•์œผ๋กœ **๋ฆฌํŒฉํ† ๋ง์˜ ๋ฒ”์œ„์™€ ํ•ต์‹ฌ ์˜๋„**๋ฅผ ๊ฐ„๊ฒฐํžˆ ์š”์•ฝํ•ด์ค˜.
2. ์ฝ”๋“œ diff๋ฅผ ๊ตฌ์กฐ์ ์œผ๋กœ ๋ถ„์„ํ•˜๊ณ , ๋‹ค์Œ ๊ด€์ ์—์„œ ํ‰๊ฐ€ํ•ด์ค˜:
	-  **์ฑ…์ž„ ๋ถ„๋ฆฌ (SRP)**
	-  **์„ค๊ณ„ ์›์น™ ์ค€์ˆ˜ ์—ฌ๋ถ€ (์˜ˆ: OCP, DRY, KISS)**
	-  **์ฝ”๋“œ ์ผ๊ด€์„ฑ๊ณผ ํ…Œ์ŠคํŠธ ๊ฐ€๋Šฅ์„ฑ**
3. ๊ฐœ์„ ์ด ํ•„์š”ํ•˜๋‹ค๋ฉด ์•„๋ž˜ ํ•ญ๋ชฉ์„ ํฌํ•จํ•ด ๊ตฌ์ฒด์ ์œผ๋กœ ์ œ์•ˆํ•ด์ค˜:
	-  **๋ฆฌํŒฉํ† ๋ง ๊ณ„ํš (๋‹จ๊ณ„๋ณ„ ์ œ์•ˆ ํฌํ•จ)**
	-  **์„ค๊ณ„์  ๊ทผ๊ฑฐ (SOLID, ํด๋ฆฐ ์•„ํ‚คํ…์ฒ˜ ๋“ฑ ๊ธฐ๋ฐ˜)**
	-  **๋Œ€์•ˆ ์ „๋žต**๊ณผ ๊ทธ **์žฅ๋‹จ์  ์š”์•ฝ**
4. ๋ฆฌํŒฉํ† ๋ง์˜ ์•ˆ์ •์„ฑ์„ ๋†’์ด๊ธฐ ์œ„ํ•œ **ํ…Œ์ŠคํŠธ ๋ณด์™„ ํ•„์š” ์—ฌ๋ถ€**์™€ ๊ทธ์— ๋Œ€ํ•œ **ํ…Œ์ŠคํŠธ ์ „๋žต**์„ ์•Œ๋ ค์ค˜.
โ€ป ์ด PR์€ ๋ฆฌํŒฉํ† ๋ง์˜ ์ดˆ๊ธฐ ๋‹จ๊ณ„์ด๋ฉฐ, ์ „์ฒด ๊ตฌ์กฐ ๊ฐœ์„ ๊ณผ ํ™•์žฅ์„ฑ ํ™•๋ณด๋ฅผ ๋ชฉํ‘œ๋กœ ํ•œ๋‹ค๋Š” ์ ์„ ๊ณ ๋ คํ•ด ๋ถ„์„ํ•ด์ค˜.

---
# ๋‹ค๋ฅธ ํ”„๋กฌํ”„ํŠธ์™€ ํ•จ๊ป˜ ์“ธ ์ˆ˜ ์žˆ๋„๋ก ์ง€์นจ์„ ๊ฐ„์†Œํ™” ํ•ด๋†จ๋‹ค.

If refactoring criteria and scope are discussed with colleagues in advance, a draft PR written, and refactoring proceeded based on the draft PR information, it will be clearer when refactoring should be completed.

The Essence of Refactoring with AI

While it is true that AI reduces the time spent writing code, it does not eliminate the time spent thinking and reviewing. A significant portion of the time spent writing code is spent reviewing code. It must be carefully checked that the request has been properly reflected, that there are no logical contradictions, and that there are no potential side effects. In order to critically accept the code generated by AI, additional time is required to ask questions and check whether it has been implemented correctly.

Including these times, the time spent on refactoring may not have decreased significantly. Therefore, it is not recommended to place excessive trust in AI productivity and set refactoring periods that are too short or plan an unmanageable amount of refactoring.

Refactoring with AI is certainly an attractive development method, but in order to fully utilize AIโ€™s potential, it is necessary to recognize its clear limitations and approach it wisely. AI cannot read minds, nor can it put the results it has produced into heads. Even if the code is generated by AI, the final ownership and responsibility for the code lies with the developer.

AI-generated code must be able to understand every line and explain the design intent just as well as code written by oneself. That is why it is important to review the refactoring design process several times and devote a lot of time to it.

If AI is given even the slightest ambiguous request, it will only give plausible answers. It is important to remember that the system provides results with the highest probability based on the userโ€™s request. Without clear goals, specific instructions, and critical review, AI responses will remain at the level of functioning wrong answers.

Although AI can assist in the process of deploying refactoring results and in operations, troubleshooting can be difficult due to unrecognized environmental complexities. There are other reasons why refactoring time cannot be kept short. In such cases, traditional analysis and experience may still be necessary.

Despite these limitations, AI makes it easier to overcome the biggest psychological barrier to refactoring: getting started. When developers are faced with complex legacy code and donโ€™t know where to start, AI can help analyze the entire project and diagnose and propose solutions to problems that were not previously identified. AI is also extremely helpful in performing repetitive tasks.

Ultimately, refactoring in the AI era boils down to the essence of โ€œcollaboration between humans and AI.โ€ Rather than relying on one side, developers should take on the role of โ€œarchitects,โ€ providing direction at a higher level, making important design decisions, and taking responsibility for the quality of the final product. AI is a passionate partner that provides multiple high-potential alternatives and never shies away from repetitive tasks.