Slashdot Mirror


Refactoring: Improving the Design of Existing Code

kabz writes "Refactoring (as I'll refer to the book from here on in) is a heavy and beautifully produced 418 page hardback book. The author is a UK-based independent consultant who has worked on many large systems and has written several other books including UML-Distilled. Refactoring is a self-help book in the tradition of Code Complete by Steve McConnell. It defines its audience clearly as working programmers and provides a set of problems, a practical and easily followed set of remedies and a rationale for applying those techniques." Read below for the rest of Johnathan's review. Refactoring: Improving the Design of Existing Code author Martin Fowler with Kent Beck, John Brant, William Opdyke and Don Roberts. pages 431 publisher Addison-Wesley rating 9/10 reviewer Jonathan Watmough ISBN 0201485672 summary expands and formalizes the idea of applying explicit refactorings Code refactoring is the process of restructuring code in a controlled way to improve the structure and clarity of code, whilst maintaining the meaning of the code being restructured. Many maintenance problems stem from poorly written code that has become overly complex, where objects are overly familiar with each other, and where solutions implemented expeditiously contribute to the software being hard to understand and hard to add features to.

Typically refactorings are applied over a testable or local scope, with existing behavior being preserved. Refactoring as defined in this book is not about fixing bad designs, but instead should be applied at lower levels.

Testing a la Extreme Programming is emphasized as a control for ensuring that program meaning is not changed by refactoring. It is not over emphasized, and this is not a book about testing, but it is often mentioned and stays in the background through the book.

The refactorings presented in the book are not intended as a comprehensive solution for all problems, but they do offer a means to regain control of software that has been implemented poorly, or where maintenance has been shown to simply replace old bugs with newer ones.

The book is divided into two main sections, introductory material that introduces and discusses refactorings, and a lengthy taxonomy of refactorings that includes both examples and further discussion. The introductory material consists of a long worked example through simple Java code that implements printing a statement for a video store. Despite the simplicity of the code, Fowler shows in clear detail where improvements can be made, and how those improvements make the code both impressively easy to understand, and easy to maintain and add features.

Several key refactorings are demonstrated in the opening chapter including Extract Method, Move Method and Replace Conditional with Polymorphism. This is a book about programming in the object oriented paradigm, so as you might expect, the first two refactorings refer to extracting and moving object methods either into new methods, or between objects. The third example provides a means to replace special cased behavior in a single object type by deriving a sub type of the object and moving type specific code to the sub types. This is a fundamental technique in object oriented programming, and is discussed here in practical terms.

Now that several actual refactorings have been introduced, Fowler provides a solid and well thought-out discussion of the why's, when's and when not's of refactoring. For example, code can decay as features are added, and programmers special-case, or bodge additional functionality into existing objects. Fowler argues that the bitrot and decay makes software more unreliable, leads to bugs and can accelerate as the problem gets worse. Faced with these problems, refactoring should be used to improve local design and clean up and improve code, leading to better software, that is easier to maintain, easier to debug, and easier to improve with new features as requirements change.

However, there is a caveat, in that since software functionality should remain unchanged during refactoring, the process of refactoring consumes resources, but provides no easily measurable value. Fowler confronts this issue in a section that discusses how to communicate with managers, that you are performing refactoring work. He denies being subversive, but his conclusion is that refactoring should essentially be folded in with normal work as it improves the overall result.

This is a bit like goofing off on the basis that you'll think better after 20 minutes of fooseball. I'd definitely subscribe to that theory, but many others may not.

Kent Beck guests in Chapter Three for a review of the issues in typical software that suggest a refactoring may be needed. This chapter is entitled Bad Smells in Code, and most of the smells presented will be familiar to any reasonably experienced programmer, and they will be a great learning experience for less experienced programmers. I got the same feeling reading this chapter as I did when I first read Code Complete. Here was someone writing down names and describing problems that I had a vague unease about, but was too inexperienced to really articulate or do something about. Typically the refactorings address the same kind of issues that a code review with a skilled experienced programmer would address. Long parameter lists, too long methods, objects delving about in each others private variables, case statements, related code spread across different objects etc. None of these problems are debilitating in themselves, but added up, they lead to software that can be prone to error and difficult to maintain.

Most of the remaining substance of the book, 209 pages, is given over to a taxonomy of refactorings. These 72 refactorings are covered in detail with comprehensive simple examples presented in Java. Each refactorings is given a clear name, a number and a line or two of descriptive text. The motivation for the refactoring is then discussed, often including caveats and cautions. The mechanics of implementing the refactoring are then listed, with 1 or more (and often more) examples of implementing the refactoring. Refactorings range from the very simple to more complex examples such as Convert Procedural Design to Objects.

Due to the difficulties of reproducing large and complex sections of code, Fowler sticks with relatively simple examples. These seem to grate on him more than the reader, and he can come across as somewhat embarrassed when we look at the employee, programmer, manager pay example for the tenth time. I certainly didn't have a problem with it though.

This is a very well written and fun to read book. I personally feel that much of the material is implied by from Code Complete, but Fowler does a fantastic job of expanding and formalizing the idea of applying explicit refactorings. Much like Code Complete gave a motivation for keeping code well commented and laid out, this book presents the case for care and feeding of how to structure software. To fight bitrot and technical debt, poorly structured and unclear code should be targeted and refactored to improve structure and clarity. This gives a very real payback in terms of less required maintenance, and ease in adding features later on down the line.

Despite the fact that all the examples are in Java, the ideas are easily transferable to C++ or any procedural object oriented language. I highly recommend this book.

You can purchase Refactoring: Improving the Design of Existing Code from amazon.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

184 comments

  1. Awful description by Anonymous Coward · · Score: 5, Insightful

    This is a bit like goofing off on the basis that you'll think better after 20 minutes of fooseball. I'd definitely subscribe to that theory, but many others may not.

    It's not at all like that. If you really need an analogy to understand this very simple concept, it's like seeing that your desk is overflowing with paperwork and spending some time filing everything properly. A little time invested can make it a lot quicker to find what you are looking for and help you deal with one thing at a time.

    1. Re:Awful description by Anonymous Coward · · Score: 1, Funny

      Plus, this is Slashdot, so a bad analogy should involve a car somehow.

    2. Re:Awful description by dubl-u · · Score: 5, Insightful

      I agree that this is a terrible analogy:

      This is a bit like goofing off on the basis that you'll think better after 20 minutes of fooseball. I'd definitely subscribe to that theory, but many others may not.

      The analogy I usually draw is to cooking.

      Everybody's thrown a big dinner party and then left the dishes for a few days. Or even worse, had a roommate who did. Turns out this sucks, because if you just want to make yourself a little breakfast, then it's harder to cook. You have to scrape out a pan and chip off a plate just to get started. And cooking is twice as much work because you have to shift the mess around just to get at the counter, and then shift it again to get at the stove.

      Then when you're done, you sure aren't going to wash up properly; the sink's filled with dishes already. So you just toss your dishes on the top of the pile, saying you'll get to it "later". Of course, that ever-growing mound of fuzzy dishes is the real-world analogy of half the code bases I've seen. And the giant rewrites that inevitably follow are like tearing down your kitchen and building a new one because that's the cheapest way out of the mess.

      Instead, good chefs work clean. They clean as they go. Always. Not because they're uptight freaks, but because if they don't, the mess slows them down and makes them sloppier, eventually resulting in a giant clusterfuck, with all their colleagues yelling at them.

      Refactoring is just a way for programmers to clean as they go. I picked up the habit years ago, and now would never go back, as it's the fastest way I've found to get good work done.

    3. Re:Awful description by greg_barton · · Score: 3

      I wish I could mod you higher than 5. :) Most fuckin' awesome description of refactoring I've ever read.

    4. Re:Awful description by severoon · · Score: 1

      If you're ever in need of a Java guy to work on a side project, ping me. I'll work with guys like you any day.

      --
      but have you considered the following argument: shut up.
    5. Re:Awful description by Anonymous Coward · · Score: 1, Funny

      Agreed, very nice. And it's inspired me to clean my dishes immediately ;-)

    6. Re:Awful description by hotdiggitydawg · · Score: 2, Funny

      I dunno... the analogy was conspicuously lacking in cars...

    7. Re:Awful description by m.precursor · · Score: 1

      You forgot add the profit step. That alone might convince my roommates to clean up after themselves more.

    8. Re:Awful description by The_reformant · · Score: 1

      Yeah but it was the worst car analogy ever

      --
      I have discovered a truly remarkable sig which this post is too small to contain.
  2. Old news. by Kaz+Kylheku · · Score: 4, Informative
    1. Re:Old news. by corby · · Score: 1

      That is ridiculously insensitive. Everybody knows that samzenpus is unfrozen from the year 1999, and is struggling to regain his relevance as a critic.

      Don't listen to Kaz, I'm behind you all the way, samzenpus. Hey, man, can you give us a sneak preview of that movie American Beauty? I hear it's a real sleeper!

    2. Re:Old news. by $RANDOMLUSER · · Score: 3, Interesting

      It is a really good book. It got me thinking about objects in ways that GoF never did. Maybe it's time to introduce a new generation of /.ers to it.

      --
      No folly is more costly than the folly of intolerant idealism. - Winston Churchill
    3. Re:Old news. by The+Ultimate+Fartkno · · Score: 2, Funny

      Goblet of Fire was pretty awesome, but it's no Deathly Hallows.

    4. Re:Old news. by spleen_blender · · Score: 4, Funny

      I'm not really into Pokemon.

    5. Re:Old news. by hansamurai · · Score: 1

      Looks like the assigned score hasn't been refactored since then. I suppose that's a good sign the book is actually good and doesn't focus on specific technologies.

    6. Re:Old news. by Anonymous Coward · · Score: 0

      you must be new here. oh wait...!

    7. Re:Old news. by Blakey+Rat · · Score: 1

      Also, it's spelled "Foozball." Since we're being critical and all.

    8. Re:Old news. by JerkBoB · · Score: 1

      Also, it's spelled "Foozball."

      Fooseball. From Fußball, which is a shortening of Tischfußball.

      Fuß == foot. ball == ball. Football (soccer, for us yanks).

      Nyah.

      --
      A host is a host from coast to coast...
      Unless it's down, or slow, or fails to POST!
    9. Re:Old news. by Anonymous Coward · · Score: 0

      Although the tabletop game (what I assume the review writer was referring to) is commonly spelled fusball.

    10. Re:Old news. by StarvingSE · · Score: 1

      Yes, this book has been out for a number of years now and is concept is not new. It is (or should) be taught in any decent software engineering curriculum.

      What gets me is that this book was published back in 1999, yet there hasn't been that much progress in terms of automated refactoring tools since then. Most IDE's have a handful of the simple ones available, but nothing complicated such as extract class (which I use frequently and is prone to errors).

      --
      I got nothin'
    11. Re:Old news. by Anonymous Coward · · Score: 0

      Sorry to mod you 'overrated', I meant 'underrated'.. damn javascript moderating without undo.

    12. Re:Old news. by Anonymous Coward · · Score: 0

      > Also, it's spelled "Foozball." Since we're being critical and all.

      It's sad that you have the whole internet in front of you and you didn't even bother to check how wrong you are.

    13. Re:Old news. by Blakey+Rat · · Score: 0, Flamebait

      It's sad that you have the whole internet in front of you and you didn't even bother to check how wrong you are.

      I checked the internet, jackass.

      Foozball: 60,100 http://www.google.com/search?hl=en&q=foozball&btnG=Google+Search

      Obviously I figured that was enough. But I unfortunately didn't check all the alternatives:

      Fooseball: 78,000 http://www.google.com/search?hl=en&q=fooseball&btnG=Google+Search

      Foosball: 3,310,000 http://www.google.com/search?hl=en&q=foosball&btnG=Search

      So for the goddamned record, the "most" correct spelling is "foosball" and both me and the original reviewer are wrong.

    14. Re:Old news. by Anonymous Coward · · Score: 0

      Everyone, stop working! Something similar was done 9 frigging years ago.

      Shouldn't you be on imdb posting things like "They used a Bicycle Card Co. deck in a scene that was set in 1955 but EVERYONE knows that deck wasn't made until 1956! This totally ruined the movie for me."?

    15. Re:Old news. by Hoi+Polloi · · Score: 1

      Foozball is probably a brand name spelled that way so they could trademark it.

      --
      It is by the juice of the coffee bean that thoughts acquire speed, the teeth acquire stains. The stains become a warning
    16. Re:Old news. by Anonymous Coward · · Score: 0

      > So for the goddamned record, the "most" correct spelling is "foosball" and both me and the original reviewer are wrong. ... which was my point, genius.

    17. Re:Old news. by LizardKing · · Score: 1

      Kaz Kylheku? Late of comp.lang.c? My god it's a small world.

    18. Re:Old news. by Anonymous Coward · · Score: 0

      both me and the original reviewer are wrong.
      Both I and the original reviewer are wrong.
    19. Re:Old news. by laejoh · · Score: 0

      Just to make sure everybody understands :)

  3. Change, we love it! by Anonymous Coward · · Score: 0

    Typically refactorings are applied over a testable or local scope, with existing behavior being preserved.

    IOW: Refactoring changes shit around without making a difference. Ever heard of "never touch a running system"? Especially if you expect it to do exactly the same after you're done with it?

    1. Re:Change, we love it! by magical_mystery_meat · · Score: 3, Interesting

      If the application is "done" and no major components are to be added, then by all means leave it alone.

      However, most systems aren't like that. They need to be changed due to changes in requirements, new functionality, business rules, etc., and that's where refactoring helps you - it helps isolate functionality and help the system evolve. (The app should have been designed with that in mind from the beginning, but you and I both know that it never is.)

      The main problem I have with refactoring (both the book and the concept) is that it's way too easy to go overboard with it because it presents a Right Way of writing code. Sometimes methods need to be 20 lines, sometimes classes need to do more than one thing, sometimes inheritance just gets in the way. Fowler respects these ideas, but I've known people who read this book and take refactoring to its logical extreme, which results in overly fragmented code.

      I also despise the XP directive to not comment code, which Fowler promotes.

      In all it's a good book but it's best to read it when after you've had a few years of real world experience and you can tell what should and shouldn't be taken seriously.

    2. Re:Change, we love it! by mr_mischief · · Score: 1

      Even when you plan for expansion and extension, you usually plan for extension in the wrong direction. Changing business requirements, new users, and brilliant but unexpected new features often require changes to your plans for change (or extensions to your extension mechanism). Let's face it, breakthrough ideas that simply must be worked into a program aren't anticipated fully. That's what makes them breakthrough ideas.

    3. Re:Change, we love it! by Peaker · · Score: 3, Insightful

      I also despise the XP directive to not comment code, which Fowler promotes. Consider the goal, not the means. The goal is documenting the code. Those who go against comments (me included) are not against documentation of the code. We are against documentation that may easily lose sync with the code, where better forms of documentation exist.

      The other forms of documentation are meaningful names. If you want to document a sub-expression, which does not have a name, give it a name, by assigning it into a variable before using it. If a piece of code does not have a name, then give it a name by putting it in a separate function.

      Variable and function names are much less likely to go out of sync with the meaning of the code than comments are, are more concise and less redundant (so they avoid violating the DRY principle).

      Remember, bad/wrong documentation is worse than no documentation.
    4. Re:Change, we love it! by StarvingSE · · Score: 1

      What about "proactive refactoring." Before coding your new feature into a system, you can do some analysis and identify ways to refactor the existing code so that your change is easier to implement and will have the least amount of impact on the rest of the system.

      --
      I got nothin'
    5. Re:Change, we love it! by gnasher719 · · Score: 1

      The main problem I have with refactoring (both the book and the concept) is that it's way too easy to go overboard with it because it presents a Right Way of writing code. Sometimes methods need to be 20 lines, sometimes classes need to do more than one thing, sometimes inheritance just gets in the way. Fowler respects these ideas, but I've known people who read this book and take refactoring to its logical extreme, which results in overly fragmented code. That is misunderstanding what "refactoring" means. The whole book is about ways to change how your code looks without changing what the code does. It doesn't say when to do it, that has to be a judgement call. There are refactoring methods that do exactly the opposite of each other: Like extracting code into a separate function, or inlining a function call into your code. It would be idiotic to just apply all the methods in the book blindly, because you would extract, inline, extract, inline and so on forever. So overly fragmented code as a result is not taking refactoring to its logical extreme, it is stupidly applying refactoring to make code worse instead of improving it.
    6. Re:Change, we love it! by Oligonicella · · Score: 1

      Uh, do both.

    7. Re:Change, we love it! by magical_mystery_meat · · Score: 1

      Consider the goal, not the means. The goal is documenting the code. Those who go against comments (me included) are not against documentation of the code. We are against documentation that may easily lose sync with the code, where better forms of documentation exist.

      Yes, maintaining documentation in comments requires a certain amount of discipline. But if you don't have the discipline necessary to maintain comments, you're not going to have the discipline to write code that is as clear as XP thinks it should be.

      Of course, most developers think they're perfect and infallible, so they will never admit that they don't have this discipline, and continue with their bad habits.

      Furthermore, what is clear in code to one programmer isn't necessarily going to be clear to another. Human-language comments provide a lowest common denominator of clarity.

      Remember, bad/wrong documentation is worse than no documentation.

      Documentation is for intent. Documentation that is out of date can at least explain the intent of a piece of code, which will help a good developer deduce what is really going on.

      Then, being a good developer, they will fix the documentation.

    8. Re:Change, we love it! by Anonymous+Brave+Guy · · Score: 1

      Those who go against comments (me included) are not against documentation of the code. We are against documentation that may easily lose sync with the code, where better forms of documentation exist.

      And what if commenting is the best form of documentation?

      No offence, but if you often found your comments and code got out of sync, you were probably writing the wrong kind of comments. Good comments don't just repeat what the code does. They explain why and perhaps how it does it. They warn of subtleties not immediately obvious from the code (because sometimes, the appropriate algorithms or data structures just aren't obvious to someone unfamiliar with the code). They refer to other documentation with more detailed background information.

      In short, they tell you the stuff the code can't, because sometimes code is too low level to represent the conceptual understanding of the programmer who wrote it. It's pretty hard for that sort of thing to get out of sync with the code without it being pretty darn obvious.

      --
      If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
    9. Re:Change, we love it! by Peaker · · Score: 1

      Its not that _my_ code and comments go out of sync. Its that it is rare that I can't get the code to speak for itself, without comments being necessary.

      It does happen, and I do sometimes have to write some comments because communicating something via the naming or other coding artifacts is impossible, but only very rarely.

      Discipline is nice, and you should have it, but a system that requires less of it is better, because discipline is not boolean. Whenever discipline is required, some percentage of the time, it will not be adhered, and if there's an alternative that requires less discipline, its a better one.

    10. Re:Change, we love it! by DMUTPeregrine · · Score: 1

      You have the key. Documentation is for intent. The code describes how "it" gets done, the documentation describes why. The code should be self-describing, but the documentation links the various functions together.

      --
      Not a sentence!
    11. Re:Change, we love it! by try_anything · · Score: 1

      I also despise the XP directive to not comment code, which Fowler promotes.


      I think Martin Fowler (and everybody else with a lick of sense) actually agrees with you. As I understand it, the XP objection to writing comments has been overblown by a few XP proponents and a horde of XP detractors; what you find in the mainstream XP writings is a twofold argument that writing comments should not be the first response to unclear code:

      Clarify your code before resorting to comments. If a section of code is complicated enough to need comments, it was probably written suboptimally the first time through, when the programmer's understanding is incomplete. If you write comments before cleaning up the code, the comments end up being an apology for a convoluted first-stab implementation. This (as I understand it) is Fowler's main complaint about lengthy code comments: they're a bad code smell in themselves and often indicate poorly organized code that should be scrapped, not documented. A quick rewrite with the benefit of hindsight will greatly improve the code clarity, perhaps eliminating the need for comments. Unfortunately, this bit of advice pisses programmers off because it assumes that they don't clean up code as they go along. Some programmers do so instinctively, and the rest are fed up with being told to.

      Why write a comment when you can express it as a test and kill two birds with one stone? This tends to piss people off because it assumes, as many other part of XP do, that the programming group is following XP wholeheartedly. That means that every programmer who works on a piece of code is familiar with all the tests for that code and will refer to them when he needs information about the code. This is actually a very uncommon way of working, so the advice that depends on it is useless to most programmer. In the context of a full commitment to extreme programming, though, the advice makes perfect sense.

      After applying these two principles (and ignoring the second if it doesn't apply,) I think XPers are happy to add whatever comments are useful. There are some XPers who take the "comments are bad" principle and make it EXTREME!!!!!!! (insert image of snowboarding Mountain Dew drinker, which dates XP pretty accurately,) and eXtreme Programming is an unfortunate name that positively *invites* confusions like these, where the most extreme statements made by any self-proclaimed XPer are taken to be the true XP way. The strict "no comments" rule is a minority viewpoint and not one advocated (AFAIK) by the originators of XP and the most common XP resources (the Extreme Programming Explained series, extremeprogramming.org, etc.)
  4. who doesn't know about Refactoring? by StandardDeviant · · Score: 3, Interesting

    I don't mean to be a wet blanket, but the book has been out for quite some time -- checking Amazon, July of 1999. It is pretty great, and I would recommend it to those who somehow managed to miss it up to this point, but a review almost nine years later? Slow news day much?

    On the other hand, maybe periodically prodding towards the direction of higher internal quality (to be distinguished from external quality, that which is perceived in a black-box fashion by your customers, the relationship between these two qualities is of course a matter of much friction and debate between the managing and laboring classes) isn't a bad call. Lord knows any extra ammo to convince people that this is worthwhile is appreciated. As much as we like to think of ourselves as poets, I sometimes think the traditional profession most software development resembles is "butcher" or "janitor", just one messy hack-job and sweep under the rug after another after four or five decades of which you can retire and grumble on the beach about putting cyanide in the guacamole, fondly reminiscing about your red swingline or asr-33 or what have you.

    1. Re:who doesn't know about Refactoring? by gbjbaanb · · Score: 2, Insightful

      I've just been reading a nice blog about how the modern world is chucking more and more stuff at us (yes, I'm looking at you Microsoft C#/.NET teams) so we barely have time to learn something before its obsoleted; cannot learn enough about all of the new features that are being pushed at us; stress out that we think we need to learn in order to keep up-to-date with modern development practices; etc.

      This review comes as a pleasant reminder that you don't have to chuck your old code away and rewrite it all in the latest, coolest tech-fashion. Keep the old stuff working, refactor it so it can still be maintained, enjoy producing applications that your customer wants and that work well for your customer.

      for example, I had a meeting with some of my american colleagues early this week where they wanted to "throw away all our old SQL access code and rewrite it all using LINQ". Please, lets not.

    2. Re:who doesn't know about Refactoring? by kevin_conaway · · Score: 1

      I don't mean to be a wet blanket, but the book has been out for quite some time -- checking Amazon, July of 1999. It is pretty great, and I would recommend it to those who somehow managed to miss it up to this point, but a review almost nine years later? Slow news day much?

      I see where you're coming from but I think this book is so fundamental that the article will be worth it if one new person reads the book and takes its advice to heart.

      The book is 9 years old and its concepts are still relevant and practical today. That says a lot about a tech book

    3. Re:who doesn't know about Refactoring? by j_edge · · Score: 1

      As a matter of fact I do tend to think of myself as a "janitor".. at least in my current position, when more than 1/2 my time is spent refactoring poorly-written code from CIS majors or techs who learned a language, got hired as a programmer and thought that's all that was needed in Software Development. No understanding of data structures, algorithms, etc.. much less more recent concepts like design patterns or unit testing. Feh!

      -jE

    4. Re:who doesn't know about Refactoring? by Anonymous Coward · · Score: 1, Funny

      You are right. 418 pages of Bullshit for this... Select your code in Eclipse IDE, Go to Refactor Menu, and select Extract method, Extract interface, etc. Done! Must be some Agile/XP "guru" trying to show off!

  5. How is this news? by Anonymous Coward · · Score: 0

    The book came out in 1999, that's almost nine years ago. And yet, a review finally appears here. Well, at least the book was good, in the the Amazon reviews the average rating is almost five stars.

  6. MakeWork by iBod · · Score: 1

    How this can be compared to McConnell's epic(s) I don't know.

    1. Re:MakeWork by stg · · Score: 1

      I've read both - while Code Complete is pretty good too, I think Refactoring is a bit better, in a practical sense.

    2. Re:MakeWork by iBod · · Score: 1

      How is it better?

      It seems to me like doing a quick makeover as opposed to getting it right the first time around.

      In my experience, and I don't thing I'm a coding God or anything, most code is generated by idiots (or to be kind, people who just don't understand the task at hand).

      If the design of an application is fubared from the start, you can't refine the mistakes out of the result.

      You need to understand the application requirement and code correctly through the entire process. There's no going back.

    3. Re:MakeWork by chromatic · · Score: 2, Funny

      If the design of an application is fubared from the start, you can't refine the mistakes out of the result.

      There's an interesting book you should read. I forget the name, but it's about how to improve the design of existing code.

    4. Re:MakeWork by stg · · Score: 3, Informative

      First - you do realize the 2nd edition of Code Complete has a whole chapter on Refactoring, right? AND it recommends the other book for more details?

      Have you actually read the book or are you just going from the review? Most of the methods are designed to work in a very small scale.

      Examples - have you...
      - Moved some code in the middle of a method to a separate method to make your code clear or re-usable?
      - Renamed a variable with an unclear name?
      - Added an intermediate variable for a complex calculation?
      - Moved a complex boolean expression into a well-named boolean function?
      - Added/removed a parameter to/from an existing method?

      These are all refactorings, although some of the simplest. You might say that is just common sense, but that is kind of the point of it, and the particular steps are designed so that you won't screw up the code with common mistakes. Many refactoring tools exist too, and they are quite useful - for example, when adding a parameter to a method, one of my refactoring tools show me where that was used and I can edit any with a click (and also note if they all need it, or if I should give it a default value). If I change the name of a method, it can replace everywhere it is used (a search and replace can do the same, except if another object has a method with the same name it will screw things up - so you have to watch each replace carefully).

      You might also note that many refactorings match other concepts in Code Complete.

      None of these are supposed to "fix" a broken design by itself. They are meant to slowly improve the code so that it is easier to keep working on it.

    5. Re:MakeWork by Kelvin · · Score: 1

      In any real project, application requirements change.

      It's just not realistic to think that you can absolutely get it right the first time around except for the most trivial applications.

      One important reason to refactor code is that when application requirements change, sometimes the best way to keep an existing code base clean is to refactor some of the internal interfaces in order to support the new requirements without tacking on ugly special case crap.

      Anyone who works on software should have a good understanding of the refactoring process and I highly recommend this book.

    6. Re:MakeWork by jamie(really) · · Score: 1

      Ah, the dreams of up-front design. The miracle of "getting it right the first time". This desire of yours to "understand the application requirement and code correctly through the entire process", ignores the fact that when you start programming, you do *not* understand the requirements that you will have when you *finish* because they will change constantly.

      I haven't read the second edition of Code Complete, because, frankly, the first edition was a great book on "how to fuck up, by the numbers". Sure he was only espousing current "establishment wisdom", but anyone with any experience would know that the book doesn't generate the results. I don't want to read books by people who talk about what the establishment thinks works. I want to read books by people writing good software that ships. Unfortunately, of course, most of these people are writing good software that ships instead of fucking books.

    7. Re:MakeWork by jamie(really) · · Score: 1

      Indeed. Code Complete 1 is a "how to fuck up, by the numbers", and Code Complete 2 is "oh shit, its obvious that everything I said in the last book doesn't actually work, and so proves that I've never actually done any of that, so lets try some new stuff and see if that sticks".

      Whereas refactoring is so completely accepted and useful that its built into any modern programming tool you can find, such as Eclipse or Visual Studio.

    8. Re:MakeWork by MetalPhalanx · · Score: 2, Insightful

      What if you're not the one who does it "The first time around"? You mention that most code is generated by idiots... Don't you think that there's a good chance that many programmers might find themselves in a new position, being asked to implement new features into an old system that your predecessor (one of the idiots you mention) managed to somehow bludgeon together?

      Another scenario (this time it's personal experience): A co-worker and I were asked if we could design an in house resource management system. Management wanted to keep this very low budget, meaning we had to use the tools available (VERY LIMITED) and nothing which would take more resources than a couple of co-op students knocking the idea about in their spare time (i.e. no resources at all). Although both of us have coding experience, neither of us had used the language available to us, nor do we do any sort of programming in our daily jobs.

      We started discussed a couple of different approaches and we started knocking out bits of code that were really only meant for testing ideas about the language and putting together a rough sketch of the program. Unfortunately, one of the bosses saw a nearly-almost-halfway functional version out of this, showed it to his boss, who got excited, and then handed the code off to another employee to finish (actual resource allocation now that upper management approved). He unfortunately was a sloppy programmer and did a mediocre job at finishing the project. The system that resulted from that is still in use today, although over the last few months has started to show aberrant behaviour, reacting slowly, and any time a new feature is added it gets worse. "Tutorial" code we originally would have scrapped became a "functional" system, and it was made worse by a sloppy programmer who didn't understand the need for commenting (He actually deleted most of my comments out, because they "distracted" him).

      In either of these situations, does it not sound like refactoring may do this code some good?

  7. My coworkers are insane. by RandoX · · Score: 5, Funny

    Does your book cover that?

    1. Re:My coworkers are insane. by Scrameustache · · Score: 1

      Does your book cover that? "a heavy and beautifully produced 418 page hardback book."

      Coworker's head, meet hardcover book...
      --

      You can't take the sky from me...

    2. Re:My coworkers are insane. by CodeBuster · · Score: 1

      Perhaps not, but Antipatterns: Identification, Refactoring, and Management does. See Antipattern for a partial listing and watch out for the Cage Match Negotiator, sometimes it really is better just not to play that game.

  8. Java-Specific by justasecond · · Score: 1, Interesting

    One point about the review: The note about the examples being transferrable to C++, etc. is a little off; some of the refactor techniques are basically workarounds for Java quirks. In fact, I've always felt that the book should have been called "Refactoring: Improving the Design of Existing Java Code".

    Not a bad read overall, but it could have been made better by presenting examples in different languages, a la the GOF Design Patterns book.

    1. Re:Java-Specific by nanter · · Score: 1

      The GOF Design Patterns book didn't present examples in multiple languages either. Its patterns were consistently universally applicable, though, I'll give you that.

    2. Re:Java-Specific by cyberkreiger · · Score: 3, Informative

      It presented examples in Smalltalk and C++.

      --
      Stumbling in the dark
      I hear slavering of jaws
      Eaten by a grue.
    3. Re:Java-Specific by Anonymous Coward · · Score: 1, Informative


      GoF presents examples in both SmallTalk and C++, and most of the patterns are not directly applicable to C++, in the sense that if you just implement them as-written terrible things will happen.

      An obvious example of this is the publish/subscribe (Observer) pattern. I once worked with an incompetent developer who had the fixed belief that you could just take GoF examples and use them without modification. So he did, and the first time an observer removed itself from the subscriber list during a notification call his crappy code fell over due to an invalid iterator. And then there were the fun things that happened when any subscriber threw an exception during notification...

      The GoF patterns are excellent, and any good developer should know and use them. But simply taking them verbatim out of the book without thinking at least a little about language-specific implementation issues can land you in a big, hard-to-debug mess. No amount of rote copying will ever replace actual competency.

    4. Re:Java-Specific by AmberBlackCat · · Score: 1

      This was one of my textbooks in college. We used it for Squeak.

    5. Re:Java-Specific by Just+Some+Guy · · Score: 1

      By a fluke of Slashdot's discussion system, your post appeared as a reply to:

      My coworkers are insane. Does your book cover that?

      We've all had bad experiences with C++, but I wouldn't call it insane.

      --
      Dewey, what part of this looks like authorities should be involved?
  9. See Chapter XII - 'Kill Them' by iBod · · Score: 3, Funny

    And bury them deep.

  10. Love refactoring but primary problem is legal by Maxo-Texas · · Score: 4, Interesting

    "new" code is a capital investment and gets very high tax benefits.
    "refactored" code is a pure cost.

    I do a lot of refactoring but i always have to sneak it in under the cover of a "new" project instead of a "bug fix" project.

    --
    She was like chocolate when she drank... semi-sweet at first and then increasingly bitter.
    1. Re:Love refactoring but primary problem is legal by Surt · · Score: 2, Interesting

      Refactored code is new code to do an existing job. Does Microsoft not get to write down its investment in each new version of windows because it does the same job?

      --
      "Who is the Journal of Quantum Physics going to believe?" --Stephen Hawking
    2. Re:Love refactoring but primary problem is legal by kevin_conaway · · Score: 1

      The primary problem is not legal, but your management.

      Improving your software is always a benefit, whether that benefit is visible to the customer or not is an exercise for your management to handle. By improving your code, you make it easier to maintain which in turn allows more NEW features to be added to it with less headaches.

    3. Re:Love refactoring but primary problem is legal by cbcanb · · Score: 4, Insightful

      Not true. not true.

      Refactoring code is like paying off debt. When you add new code without thinking about the design, the internal quality goes down. This makes the code harder (read "more expensive", "slower", etc) to work with in the future. And since most of the cost of software is in maintenance rather than the initial development, that's a bigger issue than you think.

      Carrying a little bit of technical debt is inevitable, and a good thing. But if you allow too much to build up (by not refactoring debt-heavy areas of code), the interest payments can become crippling. Your code sucks, you can't understand it, and making changes takes far more work than they should. That directly impacts the bottom line.

    4. Re:Love refactoring but primary problem is legal by Maxo-Texas · · Score: 2, Interesting

      I do not think any of you guys got my point so I'll clarify.

      From here:
      http://www.pkftexas.com/pkf/NewsBot.asp?MODE=VIEW&ID=15&SnID=2
      The entrepreneurial owner of a growing business would like to expand into other cities and states, but is hesitant. He does not believe his current accounting software will be able to handle the increased transaction load and produce the type of management reports he needs to make effective decisions, and the cost of the next tier software has always been a little out of reach. Under the 2003 Act, the business could expense the first $100,000 of the cost of the next tier accounting software and could immediately save $34,000 in taxes, using a 34% tax rate.

      In other words, "new" software is 2/3 the price of the same code done as maintenance code.
      So the ROI on your 100,000 maintenance change must be at least 34,000 to be equally justified to replacing the code with a new 100,000 project.

      I know that there are huge benefits to refactoring code- but there are huge tax advantages (that translate to large real amounts of money) to writing it new.

      --
      She was like chocolate when she drank... semi-sweet at first and then increasingly bitter.
    5. Re:Love refactoring but primary problem is legal by dubl-u · · Score: 2, Interesting

      Refactoring code is like paying off debt.

      Agreed! And that's the best way to explain it to execs.

      On a new project, you refactor to keep debt low. On a legacy project, you refactor to keep "interest" costs low. The interest on technical debt comes out as less reliable systems, buggier code, increased maintenance cost, and higher costs for new features. All of those cost cash money, which in my experience is a lot more money than you'd pay to do things right.

    6. Re:Love refactoring but primary problem is legal by Maxo-Texas · · Score: 2, Interesting

      Refactored code is existing code maintained. It is not legally new code. Microsoft has big lawyers but they probably have to track their software the same way every company I've ever worked for in the last 25 years has. New code is like buying a new truck. Refactored code, bug fixed code, etc. is like fixing the truck or spending extra time tuning it up. You do not get the tax benefits.

      I'm not sure where "reusable" code falls in this mix. Here you reuse existing code for a new project.

      --
      She was like chocolate when she drank... semi-sweet at first and then increasingly bitter.
    7. Re:Love refactoring but primary problem is legal by shlashdot · · Score: 1

      I think you ex. is flawed. If the entrepreneur spent $100,000.00 for someone to maintain his software, do you not think they could expense that?

      There may be special cases that revolve around an R&D tax *credit*, which requires building something new to get a credit, which is much different than a deductible expense.

      IANAA

      --
      Additional plugins are required to display all the media on this page.
    8. Re:Love refactoring but primary problem is legal by Maxo-Texas · · Score: 1

      Sigh.

      I've been doing this for 23 years. It's a fact not an opinion. It has come up easily two dozen times while working on projects. But think what you want. Or research "capital expense" and deduction and so on. Or ask a CPA the next time you talk with one.

      No point in beating the horse any more.

      --
      She was like chocolate when she drank... semi-sweet at first and then increasingly bitter.
    9. Re:Love refactoring but primary problem is legal by Surt · · Score: 1

      That's pretty interesting ... none of the companies i've worked for in the last 20 years (6) have done what you're suggesting, none of my friends at Microsoft have mentioned this, and I've never heard of this before. Maybe this is something unique to your state.
      There's basically no way the company I currently work for even could track this, not without major infrastructure changes, and we refactor code all the time.

      --
      "Who is the Journal of Quantum Physics going to believe?" --Stephen Hawking
    10. Re:Love refactoring but primary problem is legal by Anonymous+Brave+Guy · · Score: 1

      I think your basic problem is that a lot of us have been doing this stuff for a living for years too, yet you seem to be the only one who's ever heard of this.

      And yes, the US arm of my employer has accountants who deal with R&D tax provisions. And no, we don't track every line of code as "new" or "refactored".

      --
      If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
    11. Re:Love refactoring but primary problem is legal by shlashdot · · Score: 1

      You've been doing what for 23 years? Not answering questions? Misinterpreting what accountants say? Explaining things poorly?

      Yes thank you I will think what I want.

      --
      Additional plugins are required to display all the media on this page.
  11. Jazz can't be taught by Daniel+Phillips · · Score: 1, Troll

    At one time not so long ago the common wisdom was, jazz can't be taught, it is just something you have in you or not. Today, jazz is commonly taught by formal methods at schools like Julliard, producing many fine studio and live jazz musicians. So much for common wisdom.

    Today, in computer science, it is commonly thought that there are star developers and there are normal developers, and that being a star developer is just something you have in you. Well. It smells like deja vu all over again.

    --
    Have you got your LWN subscription yet?
    1. Re:Jazz can't be taught by rk · · Score: 1

      I agree with what you're saying, but it's not like anyone studies jazz because they can get a steady career out of it with a regular paycheck, health care, and a 401k. My wild-ass guess is there is a higher percentage of people studying jazz at music conservatories because they love it than we will find studying programming and software engineering. There are many people in software development because they perceived it as a good steady career. It doesn't mean that they can't be competent or even great at it, but someone who does something because they have a love for it is more likely (though not certainly) to be more knowledgeable, productive and capable.

      Put another way, I'd guess that there are more software engineers who really want to be jazz musicians than jazz musicians who really want to be software engineers. Though I do love what I do for a living, I know I'm one of them. :-) (Un)fortunately, I'm a better engineer than I am a musician.

    2. Re:Jazz can't be taught by slim-t · · Score: 1

      Jazz is simple, you must be thinking of the Blues. The Blues must be a part of you, unless you sell your soul to the devil. God is the ultimate bluesman, of course.

  12. Another Excellent Book, The Pragmatic Programmer: by lyapunov · · Score: 3, Insightful

    THe Pragmatics Programmer: From Journeyman to Master" was published as well in 1999 and is written by Andrew Hunt and David Thomas. I just got it for Christmas and have been enthralled with it. They spend some time dealing with the refactoring of code as well as wonderul insight into a wealth of other areas.

    This is yet another book that I wish I had read years ago. Working a few years in industry really makes you realize how much you can learn from other people. But alas, the problem of youth is that you always think you're the exception.

    --

    Either give it away or get top dollar, but never sell yourself cheap.
  13. A problem with 'refactoring' by w3woody · · Score: 4, Interesting

    At several places where I worked management was always happy to allow cycles to be spent on the process of 'refactoring' the code.

    Unfortunately, in my experience, the process of 'refactoring' involved making code more complex by adding to it. In one case, I saw the product of the 'refactoring' process wrap two pieces of functionality into two separate EJBs (with a whole 'dto-pojo' conversion scheme for data "isolation"). In another, I saw some functionality wrapped into a collection of beans--which was later wrapped in another layer of beans, and so forth, until 20 lines of code which set up a call into the javax.xml.translate package (for performing an XSLT transformation) into something like 8 bean layers. The 20 lines of code was at the heart of an 8-layer onion, each layer added by someone else's "refactoring" operation.

    In Java, because modern IDEs allow you to write code without thinking, the problem with code is not that there isn't enough code (to prevent incestuous classes from being overly familiar with each other), but that there is too much code as programmers unfamiliar with the problem decided to add beans and interprocess communication and multiple threads without properly sizing the problem. (Right now I'm looking at an internal system which may need to process 1 transaction a second, tops, built in an inter-cooperating network of 8 EJBs, which someone thought would help improve transactional performance. Eight? A second system essentially replicates an in-memory SQL system rolled in-house: the system has been buggy because the reverse index processing had a race condition. Um, why wasn't that built in a dozen classes on top of MySQL instead of built with a couple of hundred classes that reinvent the wheel poorly?)

    While I'm glad someone wrote a book on refactoring methodology, in my experience what we need is a book which describes how to write "simple" code: code that is just as complicated as it needs to be, and no more. And a book which also describes how to simplify overly-complicated code, how to pick simpler techniques, and how to manage programmers who have an "itch" so they go scratch it somewhere else--I think that would be a much more useful book.

    It's easy to add complexity. It's hard to simplify.

    1. Re:A problem with 'refactoring' by Anonymous Coward · · Score: 0

      code that is just as complicated as it needs to be, and no more

      As long as there are code metrics like LOC, that will not happen. Overengineered code is the standard, even in popular open source projects, because creating byzantine frameworks is easier than writing elegant and efficient code, and people won't appreciate your work if it looks easy (and elegant code usually has this "why didn't I think of it, it's so simple" quality.)

    2. Re:A problem with 'refactoring' by kevin_conaway · · Score: 1

      While I'm glad someone wrote a book on refactoring methodology, in my experience what we need is a book which describes how to write "simple" code: code that is just as complicated as it needs to be, and no more. And a book which also describes how to simplify overly-complicated code, how to pick simpler techniques, and how to manage programmers who have an "itch" so they go scratch it somewhere else--I think that would be a much more useful book. It's easy to add complexity. It's hard to simplify.

      If you read the book, you'll find that Mr. Fowler agrees with you.

      Read the TDD book by Beck. In it, he describes that TDD methodology in terms of making the code simple in terms of doing only what it should and no more.

    3. Re:A problem with 'refactoring' by Bob9113 · · Score: 1

      While I'm glad someone wrote a book on refactoring methodology, in my experience what we need is a book which describes how to write "simple" code: code that is just as complicated as it needs to be, and no more.

      Your point has much merit. Bear in mind, though, that refactoring is intended to facilitate the redesign of active code. While the book you mention would have value, it doesn't mean refactoring is somehow less valuable. Refactoring teaches a good designer how to steer poorly designed code, it would be a different book that would teach good design.

      It's easy to add complexity. It's hard to simplify.

      And that's the key to the refactoring book. In my eyes (and apparently yours), good design often implies reducing complexity. And good design is hard. But even assuming you see a truly better design option, it is also hard to shift existing code with reasonable cost and risk. This book deals with the latter issue.

      Method and outcome can sometimes be separate problems, treated separately. Refactoring is a method which does not necessarily imply that the outcome will be good.

    4. Re:A problem with 'refactoring' by computational+super · · Score: 3, Insightful

      Amen. I'm getting to the point these days where I'm slowly becoming wary of anything that appears, to me, to be a good idea. In the mid-90's or so, I came across this new paradigm called "Object Oriented Programming". At the time, I was maintaining monolithic Cobol systems, and the appeal of OO was intuitive. I saw right away how it could be used to simplify code reuse, or even make reusable code that had not previously been reusable.

      These days, OO means every function is encapsulated in a class, and every class has an ISomething interface, a SomethingImpl implementation (which has no data members) and an AbstractSomething base class (that's not extended by anything else), and a SomethingFactory that creates instances of SomethingImpl's and hands back a pointer to an ISomething... and if you ever float the concept of implementing ISomething again (you know, taking advantage of all that framework?), you'll be scolded for violating the purity of the OO design.

      XP was another thing that, when I first saw it, looked like a great idea. It seemed to me that XP was essentially a description of what productive programmers were already doing while they were pretending that Gantt charts were meaningful and that project managers were useful.

      Now XP is called "Agile" and companies hire "Agile Mentors" to show us how to do precisely the meaningless project management exercises that XP was meant to replace... When I see how much paperwork I have to fill out to be "agile", I long for the days of Gantt charts and Microsoft project.

      Refactoring, too. When I first saw Fowler's book, it was like a lightning bolt. The intro talks about how he spent a few days at a client site "cleaning up" a bit of code to simplify his next task. I had had that same experience too many times to count - I had been tasked with, say, implementing lot tracking in an inventory system. It was obvious (to me) that if I first made some modifications to the existing system, I could insert the desired feature without disturbing everything else - but I could never seem to explain that to a nonprogramming manager (in the end, I always ended up doing it and pretending I hadn't because I didn't want to be responsible for breaking the entire thing). And here was a book dedicated to the technique!

      Yet, nowadays, refactoring seems to mean going through working code and wrapping every function in a SomethingImpl class, giving it an ISomething interface, letting it extend an AbstractSomething base, and creating a SomethingFactory to create SomethingImpls...

      --
      Proud neuron in the Slashdot hivemind since 2002.
    5. Re:A problem with 'refactoring' by dubl-u · · Score: 1

      It's easy to add complexity. It's hard to simplify.

      Something where Refactoring's author, Martin Fowler, agrees strongly with you.

      A lot of what you described isn't refactoring, which is defined as making design improvements that don't change behavior. Instead, those guys were just doing developer goldplating.

      I think if you read the book, you'll see how a lot of the techniques in Refactoring are intended to be used in the service of simplification.

    6. Re:A problem with 'refactoring' by Cederic · · Score: 2, Informative


      XP is an Agile methodology, but there are many others. Not all methodologies work for all organisations, so someone that knows agile principles can help a company adopt an appropriate methodology without forcing them to follow any specific one. The principles behind them all are much the same. And if someone is requiring reams of documentation/paperwork as part of an agile methodology then either their methods are not agile, or the customer is demanding the documentation (in which case they should hire technical writers and not developers).

      These days OO means much the same as it always used to. Basic OO principles are still very valid. ISomething interfaces are very strongly a Microsoft thing, no languages I've worked with have ever needed that. The use of Abstract base classes, factories, etc results from the use of design patterns, and if you think those are bad then you're in disagreement with every software engineer I know.

      Refactoring as a technique is very sensible. Fowler's book is exceedingly well written, and the approaches he recommends very sensible. The people factors (explaining things to project managers) are complicated, but the underlying refactoring mechanisms are sound. Don't confuse the tools and techniques with the difficulties of getting them accepted within an organisation.

      If you think that nowadays refactoring means going through turning everything into an interface+impl+factory then frankly you're not following the book, even remotely.

      Sounds to me like you've entered a Microsoft shop, you're receiving very poor guidance, and you're attempting to use technology/languages that you aren't sufficiently trained/expert in. You have my sympathy, but don't let that scare you away from the very real benefits that OO programming, design patterns, agile development activities and refactoring can provide. At the very least make sure the people around you stop using incorrect terms to describe the things they're doing.

    7. Re:A problem with 'refactoring' by rk4n3 · · Score: 1

      > If you think that nowadays refactoring means going
      > through turning everything into an interface+impl+factory
      > then frankly you're not following the book, even remotely.

      I think he was more complaining that it seems that's what OTHERS
      think... I tend to agree - the hordes of wanna-be's blindly
      applying what they *think* conforms to tauted "best practices",
      application of patterns being one of the most abused, is
      staggering.

    8. Re:A problem with 'refactoring' by eurasian · · Score: 1

      what we need is a book which describes how to write "simple" code: code that is just as complicated as it needs to be, and no more.

      I think I've found my /. sig.

      Amen brother.

    9. Re:A problem with 'refactoring' by mAx7 · · Score: 1

      Excellent points; it's often hard to judge when code is too complicated and whether the refactoring is moving in the right direction. My project to visualize technical dept aims help discussions in this area. See:

              http://complexitymap.com/

      "A designer knows he has achieved perfection, not when there is nothing left to add, but when there is nothing left to take away."
      Antoine de Saint-Exupery

    10. Re:A problem with 'refactoring' by lena_10326 · · Score: 1

      ...It's easy to add complexity. It's hard to simplify.
      I have always believed refactoring was simplifying. I looked at it in the same way editors look at books--omit needless words. If the work isn't smaller after refactoring, then it wasn't refactored.

      --
      Camping on quad since 1996.
    11. Re:A problem with 'refactoring' by Anonymous Coward · · Score: 0

      Are you saying there should be a book on how to write elegant code?!!? My god sir that is mad!!

      Oh how I wish there was book like this, I wish I could have had this book in my days as a CS tutor/ta. Oh I wish I could have it now to give to the code monkeys I work with.

      I have a word for the process used to write elegant simple code... I call it Prefactoring. It requires that a person stop for at least a moment and think about a problem, I usually have to sketch a little diagram out on paper and write some pseudo-code. Then BAM! simple perfect code. and if things do not look as good on screen I have at least thought about the problem enough to quickly know a good solution to refactor to.

      I am not exactly sure what this book would even look like, in a way it is like trying to teach people to be clever or tasteful. Maybe it would be something like a cross between The Design of Everyday Things and Why's Poignant Guide to Ruby... something to teach people how to tastefully design simple elegant code.

    12. Re:A problem with 'refactoring' by TuringTest · · Score: 1

      While not exactly what you ask for, you could give a try to The Little Schemer.

      Although that book is tailored to the teaching of fundamental computing concepts, it does so in a very elegant, logical, one-step-at-a-time improvement of programs. Don't let the fact that it's LISP-based scare you: Lisp is the reason that this book can "teach people to be clever AND tasteful".

      --
      Singularity: a belief in the "God" idea with the "demiurge" relation inverted.
    13. Re:A problem with 'refactoring' by computational+super · · Score: 1

      Thanks, that's exactly what I was getting at.

      --
      Proud neuron in the Slashdot hivemind since 2002.
  14. Code refactoring is the process of... by iBod · · Score: 0

    ...breaking working code!

    By all means refactor the code you're working on but please leave working production code alone!

    Why refactor?

    1) Too much time on your hands
    2) Big Ego - you can do what the original programmer did better
    3) Someone has a ton of money to waste and some of it is on programming

    I've seen refactoring in action - Boy! have I seen it!!

    1. Re:Code refactoring is the process of... by Dan667 · · Score: 1

      Actually, if you are in a situation like mine and take the 20 2k line block of code that are sprinkled all over the place and tweaked into one refactored lib that all these apps can use, your work load goes down considerably. Refactoring is a very good idea when you have maint problems.

    2. Re:Code refactoring is the process of... by iBod · · Score: 1

      Dan,

      Yes, but that's more like encapsulation than refactoring.

      I totally agree that complex functions should be contained in a 'black box' with well-defined interfaces (like your big 202k block of code).

    3. Re:Code refactoring is the process of... by A+beautiful+mind · · Score: 2, Insightful

      I have seen refactoring in action aswell.

      It decreased the size of the program code, made the code more modular, faster, made possible to write good unit tests for it and decrease bug count per LOC, all in all rewarding the developer and user with a more stable, smaller, faster and more maintainable codebase.

      --
      It takes a man to suffer ignorance and smile
      Be yourself no matter what they say
    4. Re:Code refactoring is the process of... by Surt · · Score: 1

      Where I work, we refactor only when fixing a bug, and since we have tests of everything that works in production code, you're guaranteed not to break anything.

      There are frequently situations where an improved utility/helper class can handle a task that is currently re-implemented in the class you are editing. Removing duplicate code reduces bugs.

      --
      "Who is the Journal of Quantum Physics going to believe?" --Stephen Hawking
    5. Re:Code refactoring is the process of... by Anonymous Coward · · Score: 2, Insightful

      Two great reasons to refactor:

      (1) Your requirements have changed.
      (2) You are dealing with a piece of buggy code that is a nightmare to debug and test.

    6. Re:Code refactoring is the process of... by iBod · · Score: 1

      You were most fortunate, and no doubt you were the guy doing the refactoring in question.

      Stick around a while and see what happens when some asshat 'refactors' your code and you are called in to clean up the mess.

    7. Re:Code refactoring is the process of... by cromar · · Score: 1

      You took the words right out of my mouth. I have refactored a lot of the last coder's projects because the code just sucked balls. (It really did - only a little ego involved :) Now I have hardly anything to do at work and my (non-coder) colleagues wonder why there are so few errors reported now!

    8. Re:Code refactoring is the process of... by iBod · · Score: 1

      Do you mean by that that your refactoring reduced reportable errors? I so, I salute you Sir!

      Thing is, REFACTORING should exactly replicate the original, bugs and all.

    9. Re:Code refactoring is the process of... by dgatwood · · Score: 1

      Yeah, well, replicating the bugs is usually not a virtue unless you have to maintain compatibility with somebody else's code that you can't change. My refactoring work frequently reduces bugs in the code as well. The reason for that is that 99% of the refactoring that is worthwhile consists of consolidating nearly identical code into a single copy with special case code where needed.

      It's often the case that software has special case behavior for... say a dozen very similar tasks. If you refactor those almost identical twenty versions of that function (the twelve versions you knew about plus the eight versions every previous coder missed because the word "process" had a capital 'P' in eight of the function names) into a single version of a function with special case logic, you A. reduce the number of places you have to change when you add a feature or fix a bug that affects all of those functions, B. significantly reduce the odds of forgetting to change one of them when adding features or fixing bugs, and C. almost invariably find places where the functions unintentionally differed in behavior slightly, generally as a result of missing one version of the function in prior feature additions or bug fixes.

      I've found that this redundancy tends to happen far more in object-oriented programming than in procedural programming. I think that this is because programmers often override entire methods in the superclass without bothering to break out the minimum portions with variant behavior into their own methods. In procedural programming, by contrast, the tendency is to modify the existing function, though in some cases, I've still seen multiple copies of near-identical functions. *sigh*

      In any case, whenever you increase code reuse through refactoring, you almost invariably reduce existing bugs and reduce the odds of new bugs creeping in. With the exception of cases where it reduces code volume, though, I wouldn't generally recommend refactoring a working app.

      --

      Check out my sci-fi/humor trilogy at PatriotsBooks.

    10. Re:Code refactoring is the process of... by iBod · · Score: 1

      Yes I know that replicating bugs is not a virtue, but strictly speaking, refactoring should neither introduce bugs nor eliminate them.

    11. Re:Code refactoring is the process of... by Anonymous Coward · · Score: 0

      iBod, I am starting to think you HAVE worked in a production environment that was poorly setup. Take for example my last project, the company (after making agreements to wait 2 years) told the developers to finish the entire project in 3 months. No wonder it was such ship shod work, we spent 6months fixing bugs till 2 colleagues and I spent 3 months per section completely redoing them while implementing new technology (so instead of working around, just scraping what was there before and starting from anew). Now the application uses 1/10th of the memory, has caching implemented, and instead of taking 40seconds to load different parts it flows without any noticeable delay. This is refactoring, turning bad code that typically ends out being more expensive (server load) for the company into cheaper, better, faster, less buggy, code. If your refactoring is breaking and not fixing, then you are not refactoring you are breaking what is already working and lying to your customer that you are "making it better".

      Ray

    12. Re:Code refactoring is the process of... by Kelvin · · Score: 1

      Encapsulation is a form of refactoring.

    13. Re:Code refactoring is the process of... by Oligonicella · · Score: 1

      Oh, please. At no time should you replicate errors. Refactoring is a method for finding and correcting errors as well as providing structure and legibility.

    14. Re:Code refactoring is the process of... by Anonymous Coward · · Score: 0

      ....and with both of those reasons refactoring is at most a side-effect, since you're not changing program behaviour when you refactor (right).

      When it is needed, and done right, refactoring is a blessing. Most of the 'refactoring sux!11!one!' comments are from bad experiences when it is not done right. That's like saying 'design before code sux' because you once had a project that went dead because the initial design was made by a two-headed monkey missing the smart head.

    15. Re:Code refactoring is the process of... by Anonymous+Brave+Guy · · Score: 1

      This is refactoring, turning bad code that typically ends out being more expensive (server load) for the company into cheaper, better, faster, less buggy, code.

      No, it isn't.

      By definition, refactoring does not change behaviour, at all. Not for the worse. Not for the better.

      Now, once you've finished refactoring the code into a more manageable shape, it should be easier to fix bugs or add new features. You might even discover new bugs in the course of the refactoring, and be able to address those when you do whatever you came to do in the first place. But these things are not themselves refactoring. It simply isn't what the word means.

      --
      If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
    16. Re:Code refactoring is the process of... by dgatwood · · Score: 1

      In theory, you are correct. In practice, though, such perfectly pure refactoring almost never happens and probably should never happen.

      Refactoring frequently turns up glaringly obvious inconsistencies that nobody noticed before---usually because nobody was looking at those pieces of code before. If you don't fix the bugs (or at a minimum, log those inconsistencies for later review) while you do the refactoring, you are missing a great opportunity; odds are, those bugs will never get noticed again once you are no longer touching that piece of code and looking for inconsistencies....

      Second, refactoring is often for the purpose of making it easy to add a feature. The easiest way to make sure that the change is practical is to make it while you are refactoring the code so that if you run into an "Oh, crap, I can't do this like that" moment, you can rethink the way you are refactoring the code and won't have to go back and do it all over again. As such, this sort of refactoring tends not to reproduce the original behavior, but rather some superset of the original behavior. A pure refactoring approach is more likely to waste a lot of time with a new architecture that almost works but fails to account for one edge case that requires rewriting the whole bloody module. :-)

      --

      Check out my sci-fi/humor trilogy at PatriotsBooks.

    17. Re:Code refactoring is the process of... by Anonymous Coward · · Score: 0

      A few months ago I was dealing with a FORTRAN code originally written back in the early 80s. Written by a scientist, the original code was a tangled, uncommented mess with no documentation and horrid variable and function names (and a few gotos for good measure). Being automatically converted to C via the f2c tool a decade ago didn't help things either. On the whole, the code worked but with occasional subtle bugs.

      So I refactored the code. About 3 days of hard work. Now the code is fully documented with references back to the original research paper it's based on. Function and variable names make sense, and things are readable.

      So is refactoring just a "side-effect"?

      Only afterwards was it possible for a normal human being to track down the rogue bugs (there were a couple that lurked in the original logic for almost two decades). Better yet, understandable code led to the opportunity for clever algorithmic improvements, speeding things up by an order of magnitude.

    18. Re:Code refactoring is the process of... by upside · · Score: 1

      Refactoring makes most sense in XP/Agile/$buzzword development where the code has good unit test coverage and continous integration. When you commit the refactored code it's immediately unit tested. If it passes you know it's not broken. Caveat being you've got good tests. :)

      --
      I'm sorry if I haven't offended anyone
  15. Refactoring sucks by Hal_Porter · · Score: 4, Insightful
    I've worked at a load of places where there's insufficient resources to do things that customers actually want, but an endless program to refactor away the ugliness of code. And the thing is, it's bullshit. Customers don't care how ugly the code is, so long as it works. And good programmers can deal with ugly code - it's just the sort of people who are obsessed with refactoring that can't. So next time you find a thousand line function, or code full of #ifdefs ask yourself how much of that complexity is there because some customer demanded it. Will your rewritten 'pretty' version duplicate all features that the ugly version has? Do you even understand which ones are features and which ones are bugs? If so, why do you want to refactor it? And if not, how can you expect to get it right first time and not provoke howls of protest from the people that use it.

    And if anyone whines about how old code needs to be rewritten, point them at this

    http://www.joelonsoftware.com/articles/fog0000000027.html

    Old code doesn't rust, it gets better, as bugs are fixed. Lou Montulli again: "I laughed heartily as I got questions from one of my former employees about FTP code the he was rewriting. It had taken 3 years of tuning to get code that could read the 60 different types of FTP servers, those 5000 lines of code may have looked ugly, but at least they worked." That's just in a PC application. Try refactoring the 'ugliness' out of an embedded system and see how long your employer still has customers, and how long you still have a job. And it's interesting that evolution, an unconscious process that far outperforms human 'intelligent' designers doesn't have any concept of ugliness at all. Maybe that concept is just an artifact of your limited ability to deal with complexity.
    --
    echo -e 'global _start\n _start:\n mov eax, 2\n int 80h\n jmp _start' > a.asm; nasm a.asm -f elf; ld a.o -o a;
    1. Re:Refactoring sucks by iBod · · Score: 1

      Absolutely Hal

      The code that runs most of the world as we know it is pig-ugly. But it doesn't matter because it works, day-in, day-out. Now I'm not advocating ugly code (I try to make mine as pretty as I can) but the fact is, ugly code can be workable and efficient.

      There seems to be a mindset here that only beautiful, elegant code can do the job. Not so!

    2. Re:Refactoring sucks by east+coast · · Score: 1

      I've worked at a load of places where there's insufficient resources to do things that customers actually want

      As a curiosity, do you mean the companies lack talent or is there some other reasons that they fall short?

      --
      Dedicated Cthulhu Cultist since 4523 BC.
    3. Re:Refactoring sucks by halivar · · Score: 1

      The problem comes when the old code doesn't "just work," and you have to fix it. It's one thing to deal with code that has multiple paths for special cases; I'm talking about poorly designed, poorly documented, and poorly written code, with 8 years of patch-work for all the bugs in it. If the code is bad enough, if really is simpler to rewrite it than to figure out what is wrong with the spaghetti. I don't have time to waste on crap that, at some point in the future, is going to make me waste even more time.

      I do a lot of maintenance work, and I've got a 5-strikes rule: if I have to revisit the same spaghetti 5 times to patch critical design flaws, I chuck it and rewrite it. So far, I have yet to regret this policy.

    4. Re:Refactoring sucks by Anonymous Coward · · Score: 0

      Just wait till you have to fix the ugly code in a timely manner.

      Especially if you didn't write the ugly code.

      Then we'll see if you change your tune on ugly code.

      Saving one hour of writing time isn't worth the hundred hours of extra maintenance time.

    5. Re:Refactoring sucks by Hal_Porter · · Score: 1

      You're missing the point, I'm advocating writing ugly code. I'm talking about code that has become complex through years of bug fixes and feature additions. That code is very valuable to the company that owns it. It's a compressed solution to problems customers have complained about its life. Refactoring throws away most of that.

      --
      echo -e 'global _start\n _start:\n mov eax, 2\n int 80h\n jmp _start' > a.asm; nasm a.asm -f elf; ld a.o -o a;
    6. Re:Refactoring sucks by Zet · · Score: 1

      Refactoring is not rewriting. If done correctly it preserves the semantics of
      the original code. Code that has become unwieldy is broken; in my view the
      ugliness and unmaintainability is itself a bug (even if it operates correctly).

      Refactoring for "purity" is not an end in itself, it is a means to keeping
      code readable and maintainable. The need to refactor needs to be weighed
      against other engineering goals, including meeting near-term schedule
      constraints.

      But it is a process that must be done to a certain degree, continually,
      as code matures. It allows the actual underlying design to remain clear,
      even as it evolves gradually in the face of features, bug fixes and fine
      tuning.

      Dealing with complexity is a fact of life. Allowing unnecessary complexity
      to fester is foolish.

    7. Re:Refactoring sucks by mr_mischief · · Score: 1

      That mindset happens, but that's not the idea of refactoring. The right mindset is that making your code more beautiful and more elegant before your next major functionality change will help with that. If you're not going to change anything other than the code layout, that's pretty silly.

      If you inherit code that's laid out badly (even from yourself when you were tired or rushed) and need to fix a bug or add a feature, the time to clean it up plus fix the bug in the cleaner version might not be much more than tracking down and fixing the bug alone. It might even be faster if the poor code layout was harboring the bug spread across too many lines. Then, the next time you need to make a change, that section of code will be more tidy.

      Doing whole programs just for the sake of cleaning them up is only meaningful for two reasons. One is to practice your refactoring. The other is if you're about to do an overhaul of the whole project and are likely to touch most of those parts. Otherwise, just refactor the parts you're going to change for your bugfix or new feature anyway.

      Don't forget unit testing and project testing for continuity are important, either. If you're causing regressions when refactoring, you're doing something wrong.

    8. Re:Refactoring sucks by Hal_Porter · · Score: 1

      By resources I mean programmers. If the programmers spend all their time removing features from code, they won't be available to add them. Even worse, they might not want to anymore.

      --
      echo -e 'global _start\n _start:\n mov eax, 2\n int 80h\n jmp _start' > a.asm; nasm a.asm -f elf; ld a.o -o a;
    9. Re:Refactoring sucks by rk · · Score: 4, Insightful

      But that's not what refactoring really is. Refactoring is not about making code "purdy", it's about making it so it's easier to add those layers of bugfixes that all successful production systems have. What you're referring to is a bunch of lazy, vain chuckleheads who don't want to read code. Refactoring is noticing that an enhancement or bugfix that requires you to make a similar change in umpteen places in the code and working to make it so the next enhancement/fix to it doesn't take so much time in the future. Refactoring is noticing that venerable function with all the layers of complexity isn't even used anymore by anything (and KNOWING that, not just assuming it) and giving it a dignified retirement. It's not going through software like the Code Gestapo and looking for everything that doesn't conform to "The Right Thing As I See It."

    10. Re:Refactoring sucks by kevin_conaway · · Score: 1

      You seem to be making the mistake of thinking that refactoring means "making ugly software prettier" when a closer meaning is "making bad software better"

    11. Re:Refactoring sucks by Rycross · · Score: 5, Insightful

      In the original article, Joel explicitly states that refactoring is fine and may even be needed. What he's opposed to is rewriting, which is an entirely different thing than refactoring (and, to disagree with Joel, may be completely necessary in certain fringe cases).

      Good programmers can deal with ugly code, but if your code base takes two months to make a developer marginally productive and breaks in unpredictable ways from simple code changes, then you probably need to refactor. If you're spending tons of man hours treading water because the code is such an unbelievable mess that its nearly impossible to make a solid change without breaking something, then you're going to get better productivity if you bite the bullet and come up with a plan for refactoring.

      Also, I always have to laugh at the whole "well a great developer can work with messy code..." Unless you're working in a small, tight team, its completely impossible to make sure that you're working with a good developer, much less a great one. A lot of developers out there are poor or average, and companies, despite their best efforts to avoid doing so, do hire these people.

    12. Re:Refactoring sucks by iBod · · Score: 1

      I've fixed plenty of ugly code in my time - and learned from it too.

      Rather that turning my nose up at it, I tried to understand the intention of the original programmer. Sometimes I learned a trick or two.

    13. Re:Refactoring sucks by Fujisawa+Sensei · · Score: 1

      Customers don't care how ugly the code is, so long as it works.

      You've obviously never written a customer facing API.

      And good programmers can deal with ugly code [msdn.com]

      We "can" but I would rather not spend 8 hours grepping through trash, because some idiot used copy-paste coding rather than reusable functions and classes.

      --
      If someone is passing you on the right, you are an asshole for driving in the wrong lane.
    14. Re:Refactoring sucks by BagMan2 · · Score: 5, Insightful

      There is no reason to refactor code that works and requires little to no maintenance. However, if you are working on a living code base, refactoring is critical to keeping things sane. I am not a fan of rewriting something just because it is ugly, but if ugly code requires significant modifications to support new functionality that is being added, then refactoring should be highly considered as part of the process. Hacking features into existing systems can be very time efficient in the short term, but almost without fail, the price paid is more difficult maintenance down the road, particularly if additional new features are to be added.

      Obviously refactoring needs to be carefully considered on a case by case basis. How ugly will hacking in the new feature make the code? How likely is it that other new features will also be added to this code down the road? How many other programmers have to deal with this portion of the code? How much time do you have to make the change? How much work is it to refactor the code and do it the right way, versus just hack in the feature? These and many other questions need to be answered before one determines whether refactoring is a worthwhile effort.

      As somebody who works primarily on server-software that is in a continuous state of changing and adding features, where stability is critical and expected lifetime is measured in decades, I personally have found that refactoring is almost always worthwhile in the end. I think the biggest problem with refactoring is not whether there is a need, but whether there is competence to get it done. Often times programmers will refactor something to work differently to their liking, but in reality will not have actually improved the flexibility of maintainability of the code at all.

    15. Re:Refactoring sucks by Marsell · · Score: 1

      Is this a general rant about some arbitrary definition of refactoring, or that advocated by the book?

      If it's about the book, you haven't read it. One of the very first things it raises -- and repeats often -- is that you should have tests covering the functionality before refactoring. Guess why?

      If you don't have good tests, jumping in and changing willy-nilly is idiotic.

      Also, I should note that Joel's article mentions from scratch three times. How do you refactor a codebase from scratch exactly?

      I suppose such (obvious) nuances are beyond the hoards of "I've worked at a load of places" Slashdot.

    16. Re:Refactoring sucks by Anonymous Coward · · Score: 0

      You are an idiot and the people who modded you up as insightful are idiots. Embedded software can be and is refactored. The only difference between refactoring embedded code and application code is that the constraints are different. This means that is takes someone with a brain and some education to refactor embedded code in such a way as to produce reliable and readable code. Your company should fire two of your bunch and hire a good programmer at 1.5 times the salary thus producing better code and saving money. idiot.

    17. Re:Refactoring sucks by starfishsystems · · Score: 1
      We "can" but I would rather not spend 8 hours grepping through trash

      Yep. What the parent pretends not to get is that every software developer is posessed of finite cognitive bandwidth. You can expend that bandwidth on the mental bookkeeping necessary to make sense of trashy code, or you can put resources into cleaning up the code so that it is no longer a needless cognitive drain.

      Furthermore, once cleaned up, the code stays cleaned up. The next developer to come along will not have to waste time making sense of bad code. Considering the stats on how much developer time goes into reading code written by others, refactoring and other forms of code cleanup is time well spent.

      Of course, the code should have been well structured and readable to begin with, but even the best developers have their good days and bad days, and often good developers have to tolerate the product of mediocre developers. I just don't see the need to tolerate it more than once.

      --
      Parity: What to do when the weekend comes.
    18. Re:Refactoring sucks by brewstate · · Score: 1

      After many years of code and fix (more code than fix) approach is applied to a project refactoring is sometimes the only thing you can do. Unless you want only a handful of programmers to be able to read it. I have hit so many problems with the my most recent refactor that I am amazed it even worked. For instance they had booleans in for loops to keep code out of embedded recursions instead of looping to call the code from another function. There were instances of goto points that were never hit and were kept active just in case. There were 4 forms that were used to hold 400+ global variables. It was, and to some degree still is a nightmare. Anyway what I am trying to say is refactoring is a necessary evil. It doesn't always have to do with egos. In our case one of our programmers moved on and he left the state of the program, over the course of 5-8 years, in a state of confusion. It is difficult to bring in a new programmer and get them to read something that the original author has difficulty reading. BTW the programmer was one of the owners so there wasn't a lot of checking up behind him.

    19. Re:Refactoring sucks by dcollins · · Score: 1

      "... it's interesting that evolution, an unconscious process that far outperforms human 'intelligent' designers..."

      Won't agree with that. An interesting topic for debate or research, perhaps, but no way in hell can you toss that out like everyone automatically agrees with it.

      --
      We know where leadership by an anti-intellectual "strongman" who scapegoats minorities and likes boisterous rallies goes
    20. Re:Refactoring sucks by Anonymous Coward · · Score: 0

      Copy-paste is the best way to ensure the code looks uniform and tidy in all places. All text editors provide these features, and they are really useful! The 'refactor' shortcut does not even exist :-)

    21. Re:Refactoring sucks by Anonymous Coward · · Score: 0

      This is why TDD is so important. If you write unit tests for every instance where a real-world situation caused a bug in your code, then you can refactor code while still ensuring that you're not losing all that accumulated experience.

      The other thing that people miss about refactoring is that they consider it at too low a level. Refactoring a method because it looks ugly or is difficult to read can be a nice to have, and if the code is important enough to the application, it may make sense to refactor it. But the refactorings that are really important are when you re-examine how your application is architected with the added hindsight of previous experience and changing business concerns. It's the "is this code being used for something it was never intended to be used for and, if so, would I build it differently if I know it will be used in this new way?" type questions that should lead you to refactor your code.

      If you waste time refactoring out every ugly for loop or awkward if,elseif,elseif,...,else construct, you can end up getting very little done. But if you take a higher level approach to realizing when a significant shift in your design is necessary, the few refactorings you do do will make life a lot easier and will lead you to write fewer of the ugly code snippets that you'd want to refactor out eventually.

    22. Re:Refactoring sucks by Krunch · · Score: 1

      That's just in a PC application. Try refactoring the 'ugliness' out of an embedded system and see how long your employer still has customers, and how long you still have a job.

      Nearly three months now. Some of my code should go into production within one or two months, I'll tell you how it goes :)

      But I agree that you have a point.

      --
      No GNU has been Hurd during the making of this comment.
    23. Re:Refactoring sucks by clampolo · · Score: 1

      If you have to fix up code, then it means it was poorly thought out to begin with.
      The only way to write good, bug-free code is to use formal methods. But from what I've seen, maybe 1 in 1000 coders know how to do this. Therefore people keep buying these books on refactoring, extreme programming, and design patterns to try to get a quick fix for their lack of fundamental knowledge.
      It reminds me of how people keep buying these diet books, diet shakes, and diet pills to lose weight when everyone knows that exercise + less calorie intake is 100% effective for weight loss. Mathematically checking your code produces lean and bug-free code all the time. But the engineers universities crank out now are SO BAD that they have to search through these silly books in the hopes of making their code less horrible. The books are almost comical in their "great" insights: choose descriptive variable names, test your code, don't have functions that are hundreds of lines long, etc. If a person needs a book to tell them that instead of common sense, they are in the wrong business.

    24. Re:Refactoring sucks by Aidtopia · · Score: 2

      I've worked at a load of places where there's insufficient resources to do things that customers actually want, but an endless program to refactor away the ugliness of code.

      If the design is clear and appropriate, it takes much less time to implement what the customers need and want. Refactoring is about getting a design back on track so you can deliver more in less time. You should have a motivation for each refactoring step besides making the code prettier. You should undertake refactoring that makes what you're trying to do easier.

      And good programmers can deal with ugly code.

      Yup. And better programmers find and/or build tools to make ugly code more tractable. The best programmers keep the code from becoming ugly in the first place. Even if you're totally comfortable dealing with a rat's nest, it's nice to have the flexibility to delegate some tasks to other developers who may not cope with the mess as well.

      Will your rewritten 'pretty' version duplicate all features that the ugly version has?

      Rewriting is not refactoring. Refactoring is applying well-defined transforms to improve the design without changing the functionality (and checking to ensure that it indeed does not change the functionality). If you're "pretty" version doesn't duplicate the features, you're not refactoring correctly.

      Do you even understand which ones are features and which ones are bugs? If so, why do you want to refactor it? And if not, how can you expect to get it right first time and not provoke howls of protest from the people that use it.

      By using test scripts that know the correct behavior and checking my work. If you don't have test scripts and the current version "defines" correct behavior, then you have to build test scripts that will thoroughly exercise the code and check for differing results.

      And if anyone whines about how old code needs to be rewritten, point them at [Joel Spolsky's famous article against rewriting old code].

      Joel makes good points. You don't want to throw away what you've invested in the code. But code does rot as changes are made and as bugs are half-fixed because the design doesn't allow for a correct fix. True refactoring lets you shed the handicaps of clumsy design without throwing away the knowledge and experience of existing code. As I read it, Joel wasn't ranting against refactoring, he was ranting against throwing code away and starting from scratch.

      Try refactoring the 'ugliness' out of an embedded system and see how long your employer still has customers, and how long you still have a job.

      Done. I've worked on code embedded in medical devices. Since the FDA people read your source code and must understand it, you better make sure the design is clear and correct. (Hint, they're not the best programmers.) Oh yeah, the design better be right or people die, too.

      I've also worked on firmware for a disk drive. I inherited a mess that could not function correctly because the design was so wrong. I spent six months fixing bugs (without trying to rewrite or refactor the code) until the company shut down the division and laid everyone off.

    25. Re:Refactoring sucks by clampolo · · Score: 1

      I have a strong suspicion that you have never worked on an embedded system of any size.

      There are a lot of heinous-looking things that pop up in embedded software that are absolutely necessary to get around hardware problems, timing issues, etc.

      Try refactoring that stuff out and see how fast the whole system will crash into a pile of rubble.

    26. Re:Refactoring sucks by chromatic · · Score: 1

      The only way to write good, bug-free code is to use formal methods.

      Have you ever seen this work? Me neither.

      Mathematically checking your code produces lean and bug-free code all the time.

      Oh. Hm. Have you ever seen this work on anything more (let's say) pragmatic than a binary heap?

    27. Re:Refactoring sucks by Oligonicella · · Score: 1

      I have done just that. Taken a system that works, which not a damned person could read except the original author and restructured it from the inside out so that it became legible and malleable and worked correctly at each stage. This, on a system that recorded and aged telephone billing transactions in real time.

      Perhaps your rant is no more than that of someone who enjoys having code designed for job security.

    28. Re:Refactoring sucks by Kelvin · · Score: 1

      You're not refactoring if you're changing essential parts of functionality.

      And as gross as some of the hardware quirks you need to deal with are, that doesn't mean the code that implements them has to be crap.

    29. Re:Refactoring sucks by jcnnghm · · Score: 1

      I've noticed this with some of my code. I was paging through the source of an application that I've been using internally for about 10 years now the other day, and I noticed that a good portion of the code base dealt with error handling and bug workarounds that were never present in the original release, but were slowly integrated in as problems crept up over the years, things like leap year bugs that you wouldn't necessarily test for. While I noticed all of that code and all of the different error messages possible, I mentally noted that I can't recall the last time I had actually seen the application throw an error message. As the code aged its stability and ability to negotiate edge cases seems to have gone up substantially, to the point that it hardly ever needs maintenance today.

      Some of the code is a bit sloppy and could be refactored, but why would I want to risk introducing new bugs into an application that operates more or less bug free today.

      --
      You don't make the poor richer by making the rich poorer. - Winston Churchill
    30. Re:Refactoring sucks by clampolo · · Score: 1

      I do this all the time on decent sized projects. I am mostly working on control loops. For most things I will do informal proofs. For some very critical sections, I will go the full route and do a full proof. And yes, it does work. Spending an extra couple hours proving things is time well-spent when a full circuit compile will take me 2 hours. And yeah, if you use formal methods, except for syntax errors 90% of stuff works perfectly on the first run.
      One section of code was taking up too much chip area. I used a formal method and cut the circuit size 80% with no loss in speed. So finally, formal methods do produce leaner and more bug-free code. More success stories from a simple google search http://shemesh.larc.nasa.gov/fm/fm-what.html.

    31. Re:Refactoring sucks by chromatic · · Score: 1

      If, as you initially claimed, the only way to write bug-free code is to use formal methods, why don't you use them all of the time? Don't you want to write bug-free code?

      Which is it then, "bug-free" or merely "more bug-free"? "Formal methods" or "informal proofs"? "Bug-free" or "90% works perfectly on the first run"?

    32. Re:Refactoring sucks by Hal_Porter · · Score: 1

      Umm, congratulations. You took something you didn't understand and rewrote it so that you did. My guess is that no one else will be able to understand your code either. Now you've got the ultimate job security of being the only person who can add back in all the features that you removed that people really needed. The irony of your last comment is no doubt completely lost on you.

      --
      echo -e 'global _start\n _start:\n mov eax, 2\n int 80h\n jmp _start' > a.asm; nasm a.asm -f elf; ld a.o -o a;
    33. Re:Refactoring sucks by mr_mischief · · Score: 1

      So you rewrote a badly designed section of code to do the same work in a fraction of the space? You refactored it. That's all the word means -- to improve code and leave the same functionality in place. Just because it's a fancy jargon word somebody puffed up and because their idea of "better code" might mean something different doesn't mean you can't apply the same principles.

      Most of these books on how to do software development aren't being written for you anyway. Hardware and firmware people have your own books, don't you? Most of this XP, refactoring, design patterns, and agile stuff is for people doing high-level applications where different rules apply. It's no wonder you think so little of it if you were seeing people try to apply it to stuff being embedded in a chip.

    34. Re:Refactoring sucks by CBravo · · Score: 1

      I can deal with bad code and 5000 lines is not a lot. 500.000 lines of inconsistent code, which intertangles, is hard to debug.

      It is questions like "where does that UTF-8 character change to garble in my ajax application?" is when I like to rearrange code.

      --
      nosig today
    35. Re:Refactoring sucks by Anonymous Coward · · Score: 0

      My guess is that no one else will be able to understand your code either.

      Your guess is based on effectively zero knowledge of the orignal and new code and is therefore worthless and renders your entire comment valueless.

    36. Re:Refactoring sucks by thaig · · Score: 1

      Can't do this in many cases because the tests would need to create real-world situations e.g. a remote server that you're talking to doesn't support POP3 properly. It's written by people at China Telecom and they:

      a) Wont' give you a copy
      b) Will not change to meet the RFC
      c) Are a big customer

      It's not always possible to simulate the outside world in a test.

      In this case you can try to write a dummy POP3 server that repeats the odd behaviour (in as far as you understand it) and make able to behave badly in every conceivable way in different tests so that all the error paths in your code are tested. It's hard work but incredibly good for your level of quality.

      What if this hasn't been done from the beginning, though? It's very hard to justify the time out to do something like this retrospectively and what's worse is that you haven't got all the possible use cases. Your bug tracking system might but it's a mountain of data, all highly technical and mostly redundant.

      It is often safest to leave code as it is for fear of making someone's "odd" use case fail. There is always change and regression, of course, but not limiting it can be unwise.

      --
      This is all just my personal opinion.
    37. Re:Refactoring sucks by Malachias · · Score: 1
      Refactoring is not initiated in a vacuum: bugs are discovered, new features are required, performance is unsatisfactory, and so on. In short, I already have the need to change something. Refactoring implies that I expand the scope of the change beyond what is absolutely necessary in order to improve or maintain code quality. For example, if the functionality I need is buried in another method, I am not going to cut and paste the lines of code or create another version that does the same thing. That's just bad programming. I extract the method and use it in both places. In the work that I do, I also maintain a large body of automated unit tests. Because these test are there, I have a great deal of freedom when it comes to refactoring code. When I make changes that go beyond what it required, I know whether I have broken something.

      Ultimately, I want the freedom to correct my mistakes. I want to be proud of every piece of code I write (even if that pride is short lived). Refactoring is one approach to correcting those mistakes. When developers let management dictate when and where refactoring is to be done, they have abdicated their responsibilities. Management is not responsible for technical content. As the developer or the lead of a development team, I am responsible for technical content. As long as I meet my commitments or take the blame when I fail to do so, management is reasonably happy. Management tends to be risk adverse, resistant to change, and short sighted. They see safety in maintaining the status quo, because they are often unable to appreciate the risks the status quo represents.

      Sure I won't have customers if I am forever refactoring and never delivering, but I won't have customers for very long if the quality sucks, the code is unstable, and I cannot cover my development costs with license fees. Disciplined refactoring avoids both outcomes.

    38. Re:Refactoring sucks by Raenex · · Score: 1
      You've said two things:

      Mathematically checking your code produces lean and bug-free code all the time. and

      And yeah, if you use formal methods, except for syntax errors 90% of stuff works perfectly on the first run. If you're making syntax errors after having "proved" your code then clearly there's a step that isn't formal that introduces bugs.
  16. Re:Another Excellent Book, The Pragmatic Programme by EricR86 · · Score: 1

    I just got it for Christmas and have been enthralled with it.
    That's actually a dead-on seconded from me.
    Only because of slashdot, can I feel slightly less ashamed :)
  17. Not quite the same by Moraelin · · Score: 3, Insightful

    The fallacy you're doing there is shifting the scales in that comparison. One side is learning Jazz at all, the other is being a _star_ in programming.

    You'll find that being a star in _any_ discipline, Jazz included, isn't just a matter of being given a crash course in music notation. There are many who can learn Jazz, but there are few which are stars at it.

    And there are a ton of people who just aren't any good at Jazz, no matter how much you teach them. There are those who are born tone deaf, or lack the coordination, or those who just aren't interested in a musical career, or those who find that learning uber-boring, or a dozen other cases which you just can't turn into _stars_ in any kind of music.

    I'll further go and say that in Jazz (or any other kind of music) it's a relatively low pay career and with very few oportunity to become a super-star. So I'd wager that any music school gets an already filtered set of candidates. It's already those who love that genre, are sure that that's what they want to do, etc. It's already the people passionate and motivated.

    In programming, we get a ton of drooling burger-flippers who think they can just get a quick training in Java and earn the big bucks. Not because they like it, not because it fits their personality type, not because they showed any kind of aptitude or inclination, just because they think they can fake their way to the big bucks. And it shows.

    If you want a more apt comparison, compare them to the gang wanting to be a rock star in high school and college. The prospect of glamour and big bucks is there, and every other high-school boy wants to be in a rock band. The problem is that most suck, and will _never_ be even an acceptable player with any instrument. And aren't particularly motivated to train hard either. How many actually become a _star_? I'll say that's lost in the decimals.

    So basically to sum up the comparison to Jazz:

    1. Yes, you can teach Jazz. You can teach CS too. That's why we have that kind of colleges, you know.

    2. You can't just take any guy off the street and turn him into a _star_ musician. If you went and took random people laid off by McDonald and tried to turn them into musicians, very few would even make an acceptable musician, and _extremely_ few will ever be considerable a _star_. Same as programming.

    But, of course, that won't stop idiot PHBs from trying.

    --
    A polar bear is a cartesian bear after a coordinate transform.
    1. Re:Not quite the same by The_reformant · · Score: 1

      Clearly someone hasnt been watching the endless american idol style programs. In which they take an idiot off the street and make them a star.

      --
      I have discovered a truly remarkable sig which this post is too small to contain.
    2. Re:Not quite the same by Just+Some+Guy · · Score: 1

      Clearly someone hasnt been watching the endless american idol style programs.

      In defense of American Idol (did I just say that?), the premise is that there are many talented people who haven't been "discovered" because of random chance. The idea isn't to take untalented hacks and train them to be talented, but to find the rare gem who would be famous if they weren't squirreled away on a farm or washing dishes somewhere. I think the idea is pretty sound regardless of what you might think of the implementation. And as far as the implementation, if 100 million people think that Joe Prettyboy is a good singer, then it's kind of hard to argue that he's not. You and I might prefer something a bit more niche, but you can't say that any of the Idol finalists are objectively bad.

      --
      Dewey, what part of this looks like authorities should be involved?
  18. Nice to see by coldtone · · Score: 1

    It's nice to see comments about the pitfalls of refactoring. (I'm sure it would be different on reddit :) ) In my experience it is very rare that any refactoring will actually improve an application.

    The worst part of refactoring, it breaks merges.

    1. Re:Nice to see by Anonymous+Brave+Guy · · Score: 1

      The worst part of refactoring, it breaks merges.

      Congratulations. I believe you've just given the first legitimate criticism of refactoring in any post I've read so far in this discussion. The merge issue is a genuine problem, and counsels a little hesitation before changing too many trivial things. In any case, it's almost certainly a good idea to check in any substantial refactoring work separately from the follow-up work that motivated it and actually changes behaviour.

      That said, I don't think even the merge issue is that big a problem. If you only refactor code when it's useful preparation before doing some bug fixing or new development, some of that code is going to change anyway and you're going to be at risk of conflicts with any other developers working in the same area anyway.

      Wouldn't it be cool if these folks who keep coming up with refactoring IDEs could come up with a language-sensitive, refactoring-aware diff/match tool to match? Now that would be a tool worth having for developers.

      --
      If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
  19. Theory and Reality by FromTheAir · · Score: 1
    There is a difference, and in any complex system, not your typical address database, you can only guess some of the design necessary to meet the requirements. The requirements are never complete because there is so much one does not know or discover until they try to do it and people use it.

    Most clients I have dealt with are unable to define their own requirements properly, about all they can do is express their intentions and answer questions, they need to be guided

    This is why I think design and development need to happen concurrently starting out with an intention, and various anticipated outputs and functionality that is needed.

    One also needs to be comfortable with chaos and unknowing, while the design unfolds perfectly if one goes with the flow. The other thing that affects the quality of code is budget constraints and the egoic mind.

    Polishing and improvement come after one figures out what they needed which only comes from doing.

    --
    "an infinite player that has lost his finite mind" ~Infinite Play the Movie (it blends with reality)
  20. PROTIP: Don't look to slashdot for advice by Anonymous Coward · · Score: 0

    I mean really. It's not 1987 anymore

  21. WTF? by Anonymous Coward · · Score: 0

    "Refactoring as defined in this book is not about fixing bad designs, but instead should be applied at lower levels."

    Um. The title of the book is "Refactoring: Improving the Design of Existing Code"

    So if it's not about improving bad designs, maybe the book should get a bad review, no?

    Maybe it should have been called "Code Refactoring: Chewing the Staples Cause You Just Can't Let Sleeping Dogs Lie"

    In any attempt at Refactoring, refactoring the design should be the first thing you consider, not the last.

    Get it working -->(refactor design)-->Get it working right-->(refactor code)-->Get it working fast.

    You might just find that as a side benefit of your redesign, a lot of crufty code gets pulled out by happy coincidence.

  22. Fundamental Misunderstanding of Refactoring by iBod · · Score: 1

    A lot of folks here just don't understand what 'REFACTORING' means.

    Wikki says:

    "A code refactoring is any change to a computer program's code which improves its readability or simplifies its structure without changing its results."

    It has nothing to do with the size source code or the functionality of the program.

    A properly refactored program will have the exact same characteristics and bugs as the original.

    Kids nowadays!

    1. Re:Fundamental Misunderstanding of Refactoring by Oligonicella · · Score: 1

      So, if you've got a program that shits itself and brings down the system every once in a blue moon and you discover the reason for this during refactoring -- you just keep that bug right in there and bring down the system? Hardly.

    2. Re:Fundamental Misunderstanding of Refactoring by anomalous+cohort · · Score: 2, Informative

      I'm surprised that the companion web site for the book hasn't been posted here yet. It's the catalog that will be of most interest.

    3. Re:Fundamental Misunderstanding of Refactoring by gnasher719 · · Score: 1

      So, if you've got a program that shits itself and brings down the system every once in a blue moon and you discover the reason for this during refactoring -- you just keep that bug right in there and bring down the system? Hardly. You are misunderstanding what refactoring does. Refactoring changes the appearance of code without changing what the code does. Sometimes, changing the appearance of code changes what was a hard to find bug to an obvious bug. Fixing the bug is not part of refactoring. Once the code is refactored, you can then look at the bugs and fix them. Very carefully. If a function doesn't do what it is supposed to do, how do you know things don't break if it does what it supposed to do?
  23. Why all the negativity over refactoring? by MobyDisk · · Score: 1

    I see lots of comments about how old code is solid and reliable and should not be refactored. This group must be working in a very different environment than the ones I am accustomed too. All too often, I come into a project where the same bugs keep re-asserting their heads over and over again, and they keep getting fixed over and over again. Usually, it is because the original code contains some routines that were copied and pasted over and over again, and by refactoring you take the best block of code and re-use it. The result is smaller, more readable, and less error-prone.

  24. Re:Main problem with the book... by Anonymous Coward · · Score: 0

    No, the main problem is that it was written by a fictional character in a soap opera.

  25. Wow! Now I'm All Fired Up and Ready to Refactor! by aquatone282 · · Score: 1

    First project: Tony's nifty custom MS Access financial-tracking application!

    First step: Rename all 122 tables from table1, table2, table3, etc to REAL names!

    --
    What?
  26. Martin Fowler is a self promotionalist by Anonymous Coward · · Score: 0

    Martin Fowler isn't that smart if you listen to them, consultants are self promotionalists.

    This particular book, despite being old, is full of a lot of stupid ideas.

    For instance, that the refactoring process is, "move a line, see if this compiles, move a line, see if this compiles" as Fowler advocated at some lecture I went to in college.

    Mechanically distilling things down like that is inane -- though XP is perhaps just as oddball and having Kent Beck and his pair programming absurdity on the author list does not bode well for anything.

  27. Refactoring MVS by iBod · · Score: 1

    Beautifying code is a subjective business.

    I used to work for IBM on the MVS (now z/os) operating system. At the time it was all written in s/370 assembler, but got rewritten in PL/S (a cousin of PL/I but with lower-level functions like direct register/memory access).

    I spent 3 years of my life (mid to late 80's) refactoring the MVS operating system. It was a mammoth exercise, and exacting too. There were 28 of us on the team, all top IBM sysprogs, based in the US (Armonk NY) and UK (Portsmouth). We had 5 IBM 3090-4 mainframes in various states of disaster while we carefully recreated decades of s/370 OS code.

    It was a bit like archeology. You analyzed and pealed away layer after layer of BXLE and MVCL instructions (mostly uncommented) and tried to grasp what the original programmer's intention was. There was self-modifying code in some modules (changing a BR branch destination!) and some of the code was actually lost, and only available on microfiche!

    Now that's refactoring!

    1. Re:Refactoring MVS by Kayyham · · Score: 1

      And we had to code uphill, both ways, in the snow!

  28. That's a problem with java by johannesg · · Score: 1

    ...and not with refactoring. Whenever I come across java code it always seems needlessly complex, with endless layers upon layers, unneeded abstractions, and patterns applied far beyond what would make for useful architecture.

    Before you start flaming, I'd like to state that this is probably more a problem with java *programmers* than with the language itself, although its design has certainly caused too many people to follow down that path. So the language is probably ok, but the surrounding libraries and the attitude and inexperience of many of its users are the real problem.

    Donning asbestos underwear... Now!

  29. Assembler Code Documentation by Sanat · · Score: 1

    I once had to take over the support of a series of 10,000 line assembler programs that did not have a single comment. these programs would communicate through the printer port with a black box device that was use for testing monitoring equipment.

    Each different monitor had its own program so to make an across the board change to the codebase one had to change each program that was similar but different from the other.

    I took a couple days out and first added comments and dividing lines as to what test was being run. This also involved figuring out what the code was even suppose to be doing at times.

    This simple documentation task literally found hundreds of errors when comparing each individual test among that same test in all of the codebase.

    The original code was written by an old professor in his 70's who was really sharp but never bothered to learn "C" so the code was written in assembler instead. As new monitors were added to be tested then the snapshot of the present code was modified and named for that monitor thus an ever growing quantity of code in various states of being updated was created.

    documentation, standardization, refactoring all permitted this mess to be fixed. It actually was a fun project.

    --
    And in the end, the love you take is equal to the love you make
  30. Time Warp? by Altec+Lansing · · Score: 1

    For the love of Christ, the book is more than eight years old! It's just getting slashdotted now?

  31. When to refactor, in order of decreasing urgency: by Anonymous Coward · · Score: 0

    1) To fix bugs. You realize the same bug is in two places. Refactor so the fix is in only one place. If there's another bug in that code, you'll only have to fix it in one place, too. If you find the same bug in a third place, you'll be equipped to refer to the previous fix rather than fix it again. Refactoring to fix a bug gives you power to fix more bugs.

    2) To add features. You made a second thing work by copy-pasting from the first thing. To make the next 20 things work, refactor your duplicated code so you don't have to copy-paste and worry about duplicated bugs. The third thing takes a little longer, but the fourth thing takes less time. If you're serious about expanding the feature set, refactor. Refactoring to add features gives you power to add more features.

    3) To optimize. The bug fix logic applies here: refactoring puts less-than-ideal code in one place so you can fix it once and for all. It can also make your compiled code smaller, which can be an important optimization goal by itself.

    Don't just refactor on principle. Refactoring is a means to an end, and before you decide to do it, you should be able to see clear, specific benefits.

  32. Refactoring by jamie(really) · · Score: 2, Interesting

    There's lots of posts about how Refactoring is a waste of time, broke something or leads to bad code. I would put it to you that taking a saw and a hammer, one can do a lot to damage a perfectly good house. On the other hand, they can be used to build a nice new deck. I imagine that since I have no experience with a hammer that I would do a lot of damage. Why does everyone with no experience at Refactoring, and who tries it with poor results, assume that Refactoring is bad?

    What to about this? Some options:

    1. Quit and join a company with an established Agile practice.
    2. Form a small team to develop some new small product unit using Agile practices.
    3. Work on a small project at home using Agile.
    4. Read Feather's Working with Legacy Code and tough it out.

    Refactoring (properly) leads to improved productivity and contentment, but learning any tool requires more than reading a book.

  33. Comment removed by account_deleted · · Score: 1

    Comment removed based on user account deletion

  34. Re:Another Excellent Book, The Pragmatic Programme by kraut · · Score: 1

    "The Mythical Man Month" by Fred Brooks was first published in 1975, and republished in 2000 as a 25th anniversary edition.

    And it's still one of the most insightful books about commercial software development out there.

    The Pragmatic chaps aren't bad either, but I'm not convinced they'll get republished in 2024 ;)

    --
    no taxation without representation!
  35. Gedanken for those who dismiss refactoring by Anonymous Coward · · Score: 0

    the process of refactoring consumes resources, but provides no easily measurable value Suppose I offered to pay you to do the inverse of refactoring to your code for you. I would change no functionality, just eliminate the use of inheritance and copy-and-paste large chunks of code in place of methods, bloating it many times over. I believe I could generate at least 1 kloc/hour. What is the least I would have to offer to pay? I'm sure it's between $1 and $1M/hour, so at least there is range for the value of refactoring.
  36. You forgot... by big+ben+bullet · · Score: 2, Insightful

    Write tests!

    The only way you can make sure while refactoring an application that at the end of the process it will still behave the same way as it did before the refactoring is to write unit tests. Never (heavily) refactor untested code.

  37. Re:Main problem with the book... by Thwomp · · Score: 1

    +1 Obscure