TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

When to refactor code

19 pointsby mainguyabout 12 years ago

7 comments

jameskiltonabout 12 years ago
Always. The development cycle should be Test, Code, Refactor, as one atomic block of work. Sarah Mei explains why this is vitally important to creating good code in her talk "Work, Play &#38; Code", I highly recommend checking it out.<p><a href="http://confreaks.com/videos/2333-mwrc2013-work-play-code" rel="nofollow">http://confreaks.com/videos/2333-mwrc2013-work-play-code</a><p>Also, if you don't have a test suite to prove your refactoring didn't change functionality, IMO you aren't refactoring, you are simply rewriting. This is the point many people miss about TDD. TDD is first and foremost about letting you refactor your code; doing TDD right gives you the best environment for writing great code. On the other hand, doing TDD wrong makes it more likely your entire code base will be very difficult to maintain.
评论 #5718860 未加载
评论 #5719126 未加载
InclinedPlaneabout 12 years ago
100 lines, hah....<p>Anyway, I don't think you need a big, complicated checklist like this. Writing code by checklist is not going to solve your problems. The more important questions are these:<p>- Do you find yourself spending more time than you think you should making changes and additions (or bug fixes) to the same sections of code?<p>- Do you often run into difficulty figuring out or remembering what a given section of code does?<p>If either of those things are true then refactoring might be a good idea. Use extract method until it hurts. Use extract method on complicated logic tests as well. Figure out where the control flow is wonky and switch to using flow based on method calls and returns to simplify things. Be ruthless. Don't be lulled into the "meaty method" trap, don't be afraid of short methods, even if they're only one line. Make your code so that it looks almost <i>too</i> simple, as though even a novice could look at it and tell you what it does. Push until you get to a state where making changes seems almost like cheating because it's so easy. That's when you know you're in the sweet spot. Programming is about making things easier, even future programming, not about proving how incredibly skilled you are at maintaining complex code.
评论 #5720080 未加载
Hansiabout 12 years ago
I have a 1134 line function with zero tests defined for it sitting open in the background which most likely scores something like 32 out of 11 on that list. I'm afraid of touching that monster...
评论 #5718847 未加载
评论 #5718919 未加载
h2sabout 12 years ago
<p><pre><code> &#62; "this 10 item checklist." &#62; 11 items in the checklist </code></pre> Heh, it's not often you see an off-by-one error in prose!
评论 #5718462 未加载
npsimonsabout 12 years ago
<i>You probably just want to rename variables because you don't like the previous developer's naming convention</i><p>If the last developer named his or her variables along the lines of 'a', 'b', 'c', you're damn right I have an issue with their naming convention and I'm going to fix it.
columboabout 12 years ago
&#62; Did you completely understand what cyclomatic complexity was without following that link?<p>Seems confusing that answering "No" to this (meaning you did NOT know what cyclomatic complexity was) would be a net positive for refactoring?
评论 #5720227 未加载
MostAwesomeDudeabout 12 years ago
This seems like a checklist for when to factor, not when to refactor; he appears to be describing massive mutant code that has never actually been split into manageable chunks.
评论 #5720444 未加载