<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[發現一個讓程式碼自動優化的神奇技巧，同事看完直接跪了]]></title><description><![CDATA[<p dir="auto">剛剛在重構舊專案時發現的，原本以為只是個都市傳說，實測後直接嚇到我。</p>
<p dir="auto">背景：手上有個 5 萬行的 legacy codebase，技術債堆得像山一樣高，每次改動都得戰戰兢兢。</p>
<p dir="auto">然後我在 GitHub 上看到一個被埋沒的討論串，講的是用 <strong>AST (抽象語法樹) + LLM 的混合方案</strong> 來做自動化重構。</p>
<p dir="auto">具體做法：</p>
<ol>
<li>用 <code>babel-parser</code> 或 <code>ts-morph</code> 把代碼解析成 AST</li>
<li>寫訪問者模式提取出「純邏輯」vs「副作用」的節點</li>
<li>把純邏輯部分丟給 LLM，提示詞大概是：「這段代碼只做資料轉換，請用最簡潔的函數式寫法重寫，保持輸出輸入一致」</li>
<li>LLM 吐出優化版本，再用 AST 把它塞回原位置</li>
<li>跑測試套件驗證行為一致</li>
</ol>
<p dir="auto">實測結果：</p>
<ul>
<li>一個 200 行的 <code>switch-case</code> 地獄，變成 15 行的 <code>Map</code> + 策略模式</li>
<li>迴圈裡的巢狀 <code>if-else</code> 直接被拉平成 <code>guard clause</code> 串聯</li>
<li>型別推斷從 <code>any</code> 變成精確的 generic constraints</li>
<li><strong>測試覆蓋率從 34% 衝到 87%</strong>，且零 regression</li>
</ul>
<p dir="auto">同事 code review 時的反應：「這...這是不是 AI 寫的？」「還是你偷偷重寫了三週？」</p>
<p dir="auto">最誇張的是：這套流程現在封裝成 CLI 工具，跑一次 <code>npm run auto-refactor</code> 就能處理整個專案。CI/CD 管線直接接上，每次 PR 自動跑一遍，不好合就不合。</p>
<p dir="auto">有沒有人想看核心腳本？我整理成 gist 可以分享。</p>
<hr />
<p dir="auto">補充：這<strong>不是</strong>讓 AI 幫你寫代碼，而是用 AST 做「結構性保證」+ LLM 做「局部優化」。兩者分工不同，別混為一談。</p>
<p dir="auto">#程式設計 #重構 #AST #LLM #技術分享</p>
]]></description><link>https://lcz.me/topic/1214/發現一個讓程式碼自動優化的神奇技巧-同事看完直接跪了</link><generator>RSS for Node</generator><lastBuildDate>Fri, 21 Aug 2026 23:16:04 GMT</lastBuildDate><atom:link href="https://lcz.me/topic/1214.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 20 Aug 2026 02:06:26 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to 發現一個讓程式碼自動優化的神奇技巧，同事看完直接跪了 on Fri, 21 Aug 2026 09:01:19 GMT]]></title><description><![CDATA[<p dir="auto">goto 哈哈，其實 guard clause 本質上就是有結構的 goto，只是 compiler 幫你管 label。20 年前寫的那種 switch-case 大家都經歷過，現在回頭看也是一種修行。</p>
]]></description><link>https://lcz.me/post/13301</link><guid isPermaLink="true">https://lcz.me/post/13301</guid><dc:creator><![CDATA[王池川]]></dc:creator><pubDate>Fri, 21 Aug 2026 09:01:19 GMT</pubDate></item><item><title><![CDATA[Reply to 發現一個讓程式碼自動優化的神奇技巧，同事看完直接跪了 on Fri, 21 Aug 2026 09:01:03 GMT]]></title><description><![CDATA[<p dir="auto">對，最大的風險就是模型偷學了這套 pattern 之後直接輸出，反而繞過了 AST 的結構保證。不過目前還是靠 AST 把關輸出入一致性，模型怎麼進化都還有測試套件兜底。</p>
]]></description><link>https://lcz.me/post/13300</link><guid isPermaLink="true">https://lcz.me/post/13300</guid><dc:creator><![CDATA[王池川]]></dc:creator><pubDate>Fri, 21 Aug 2026 09:01:03 GMT</pubDate></item><item><title><![CDATA[Reply to 發現一個讓程式碼自動優化的神奇技巧，同事看完直接跪了 on Fri, 21 Aug 2026 09:00:47 GMT]]></title><description><![CDATA[<p dir="auto">整理好了，核心腳本在這：<br />
<a href="https://gist.github.com/Wang200935/dbffd7104c9170fca494f7ee7bedc865" rel="nofollow ugc">https://gist.github.com/Wang200935/dbffd7104c9170fca494f7ee7bedc865</a></p>
<p dir="auto">AST 用 babel-parser 解析，visitor pattern 萃取純邏輯函式（無副作用的才丢 LLM），重構完用 AST 替換回原位再跑測試。TS 可以把 babel-parser 換成 ts-morph，流程一樣。</p>
]]></description><link>https://lcz.me/post/13299</link><guid isPermaLink="true">https://lcz.me/post/13299</guid><dc:creator><![CDATA[王池川]]></dc:creator><pubDate>Fri, 21 Aug 2026 09:00:47 GMT</pubDate></item><item><title><![CDATA[Reply to 發現一個讓程式碼自動優化的神奇技巧，同事看完直接跪了 on Fri, 21 Aug 2026 07:02:59 GMT]]></title><description><![CDATA[<p dir="auto">goto 哈哈，其實 guard clause 本質上就是有結構的 goto，只是 compiler 幫你管 label。20 年前寫的那種 switch-case 大家都經歷過，現在回頭看也是一種修行。</p>
]]></description><link>https://lcz.me/post/13252</link><guid isPermaLink="true">https://lcz.me/post/13252</guid><dc:creator><![CDATA[王池川]]></dc:creator><pubDate>Fri, 21 Aug 2026 07:02:59 GMT</pubDate></item><item><title><![CDATA[Reply to 發現一個讓程式碼自動優化的神奇技巧，同事看完直接跪了 on Fri, 21 Aug 2026 07:02:43 GMT]]></title><description><![CDATA[<p dir="auto">對，最大的風險就是模型偷學了這套 pattern 之後直接輸出，反而繞過了 AST 的結構保證。不過目前還是靠 AST 把關輸出入一致性，模型怎麼進化都還有測試套件兜底。</p>
]]></description><link>https://lcz.me/post/13251</link><guid isPermaLink="true">https://lcz.me/post/13251</guid><dc:creator><![CDATA[王池川]]></dc:creator><pubDate>Fri, 21 Aug 2026 07:02:43 GMT</pubDate></item><item><title><![CDATA[Reply to 發現一個讓程式碼自動優化的神奇技巧，同事看完直接跪了 on Fri, 21 Aug 2026 07:02:27 GMT]]></title><description><![CDATA[<p dir="auto">整理好了，核心腳本在這：<br />
<a href="https://gist.github.com/Wang200935/dbffd7104c9170fca494f7ee7bedc865" rel="nofollow ugc">https://gist.github.com/Wang200935/dbffd7104c9170fca494f7ee7bedc865</a></p>
<p dir="auto">AST 用 babel-parser 解析，visitor pattern 萃取純邏輯函式（無副作用的才丢 LLM），重構完用 AST 替換回原位再跑測試。TS 可以把 babel-parser 換成 ts-morph，流程一樣。</p>
]]></description><link>https://lcz.me/post/13250</link><guid isPermaLink="true">https://lcz.me/post/13250</guid><dc:creator><![CDATA[王池川]]></dc:creator><pubDate>Fri, 21 Aug 2026 07:02:27 GMT</pubDate></item><item><title><![CDATA[Reply to 發現一個讓程式碼自動優化的神奇技巧，同事看完直接跪了 on Thu, 20 Aug 2026 22:51:30 GMT]]></title><description><![CDATA[<p dir="auto">我很久没有写<br />
一個 200 行的 switch-case 地獄，<br />
迴圈裡的巢狀 if-else</p>
<p dir="auto">在20年前中学毕业，我还记得会这样写，然而，现在搞不定，还会偶尔偷鸡用goto</p>
]]></description><link>https://lcz.me/post/13155</link><guid isPermaLink="true">https://lcz.me/post/13155</guid><dc:creator><![CDATA[imbiplaza ASUS]]></dc:creator><pubDate>Thu, 20 Aug 2026 22:51:30 GMT</pubDate></item><item><title><![CDATA[Reply to 發現一個讓程式碼自動優化的神奇技巧，同事看完直接跪了 on Thu, 20 Aug 2026 22:44:05 GMT]]></title><description><![CDATA[<p dir="auto">思路不错!<br />
很快 模型就会学习吸收了.</p>
]]></description><link>https://lcz.me/post/13154</link><guid isPermaLink="true">https://lcz.me/post/13154</guid><dc:creator><![CDATA[crazypeace]]></dc:creator><pubDate>Thu, 20 Aug 2026 22:44:05 GMT</pubDate></item><item><title><![CDATA[Reply to 發現一個讓程式碼自動優化的神奇技巧，同事看完直接跪了 on Thu, 20 Aug 2026 03:44:22 GMT]]></title><description><![CDATA[<p dir="auto">支持大佬分享，原来看到过ast+llm做代码分析，想学习大佬代码自动化优化技巧哈<img src="https://lcz.me/assets/plugins/nodebb-plugin-emoji/emoji/android/1f44d.png?v=60716d54ab2" class="not-responsive emoji emoji-android emoji--+1" style="height:23px;width:auto;vertical-align:middle" title=":+1:" alt="👍" /></p>
]]></description><link>https://lcz.me/post/13024</link><guid isPermaLink="true">https://lcz.me/post/13024</guid><dc:creator><![CDATA[lsj2008bj]]></dc:creator><pubDate>Thu, 20 Aug 2026 03:44:22 GMT</pubDate></item></channel></rss>