goodbye,-clean-code

It was a late evening.

My colleague has just checked in the code that they’ve been writing all week. We were working on a graphics editor canvas, and they implemented the ability to resize shapes like rectangles and ovals by dragging small handles at their edges.

The code worked.

But it was repetitive. Each shape (such as a rectangle or an oval) had a different set of handles, and dragging each handle in different directions affected the shape’s position and size in a different way. If the user held Shift, we’d also need to preserve proportions while resizing. There was a bunch of math.

The code looked something like this:

let Rectangle = {
  resizeTopLeft(position, size, preserveAspect, dx, dy) {
    
  },
  resizeTopRight(position, size, preserveAspect, dx, dy) {
    
  },
  resizeBottomLeft(position, size, preserveAspect, dx, dy) {
    
  },
  resizeBottomRight(position, size, preserveAspect, dx, dy) {
    
  },
};

let Oval = {
  resizeLeft(position, size, preserveAspect, dx, dy) {
    
  },
  resizeRight(position, size, preserveAspect, dx, dy) {
    
  },
  resizeTop(position, size, preserveAspect, dx, dy) {
    
  },
  resizeBottom(position, size, preserveAspect, dx, dy) {
    
  },
};

let Header = {
  resizeLeft(position, size, preserveAspect, dx, dy) {
    
  },
  resizeRight(position, size, preserveAspect, dx, dy) {
    
  },  
}

let TextBlock = {
  resizeTopLeft(position, size, preserveAspect, dx, dy) {
    
  },
  resizeTopRight(position, size, preserveAspect, dx, dy) {
    
  },
  resizeBottomLeft(position, size, preserveAspect, dx, dy) {
    
  },
  resizeBottomRight(position, size, preserveAspect, dx, dy) {
    
  },
};

That repetitive math was really bothering me.

It wasn’t clean.

Most of the repetition was between similar directions. For example, Oval.resizeLeft() had similarities with Header.resizeLeft(). This was because they both dealt with dragging the handle on the left side.

The other similarity was between the methods for the same shape. For example, Oval.resizeLeft() had similarities with the other Oval methods. This was because they all dealt with ovals. There was also some duplication between Rectangle, Header, and TextBlock because text blocks were rectangles.

I had an idea.

We could remove all duplication by grouping the code like this instead:

let Directions = {
  top(...) {
    
  },
  left(...) {
    
  },
  bottom(...) {
    
  },
  right(...) {
    
  },
};

let Shapes = {
  Oval(...) {
    
  },
  Rectangle(...) {
    
  },
}

and then composing their behaviors:

let {top, bottom, left, right} = Directions;

function createHandle(directions) {
  
}

let fourCorners = [
  createHandle([top, left]),
  createHandle([top, right]),
  createHandle([bottom, left]),
  createHandle([bottom, right]),
];
let fourSides = [
  createHandle([top]),
  createHandle([left]),
  createHandle([right]),
  createHandle([bottom]),
];
let twoSides = [
  createHandle([left]),
  createHandle([right]),
];

function createBox(shape, handles) {
  
}

let Rectangle = createBox(Shapes.Rectangle, fourCorners);
let Oval = createBox(Shapes.Oval, fourSides);
let Header = createBox(Shapes.Rectangle, twoSides);
let TextBox = createBox(Shapes.Rectangle, fourCorners);

The code is half the total size, and the duplication is gone completely! So clean. If we want to change the behavior for a particular direction or a shape, we could do it in a single place instead of updating methods all over the place.

It was already late at night (I got carried away). I checked in my refactoring to master and went to bed, proud of how I untangled my colleague’s messy code.

The Next Morning

… did not go as expected.

My boss invited me for a one-on-one chat where they politely asked me to revert my change. I was aghast. The old code was a mess, and mine was clean!

I begrudginly complied, but it took me years to see they were right.

It’s a Phase

Obsessing with “clean code” and removing duplication is a phase many of us go through. When we don’t feel confident in our code, it is tempting to attach our sense of self-worth and professional pride to something that can be measured. A set of strict lint rules, a naming schema, a file structure, a lack of duplication.

You can’t automate removing duplication, but it does get easier with practice. You can usually tell whether there’s less or more of it after every change. As a result, removing duplication feels like improving some objective metric about the code. Worse, it messes with people’s sense of identity: “I’m the kind of person who writes clean code”. It’s as powerful as any sort of self-deception.

Once we learn how to create abstractions, it is tempting to get high on that ability, and pull abstractions out of thin air whenever we see repetitive code. After a few years of coding, we see repetition everywhere — and abstracting is our new superpower. If someone tells us that abstraction is a virtue, we’ll eat it. And we’ll start judging other people for not worshipping “cleanliness”.

I see now that my “refactoring” was a disaster in two ways:

  • Firstly, I didn’t talk to the person who wrote it. I rewrote the code and checked it in without their input. Even if it was an improvement (which I don’t believe anymore), this is a terrible way to go about it. A healthy engineering team is constantly building trust. Rewriting your teammate’s code without a discussion is a huge blow to your ability to effectively collaborate on a codebase together.

  • Secondly, nothing is free. My code traded the ability to change requirements for reduced duplication, and it was not a good trade. For example, we later needed many special cases and behaviors for different handles on different shapes. My abstraction would have to become several times more convoluted to afford that, whereas with the original “messy” version such changes stayed easy as cake.

Am I saying that you should write “dirty” code? No. I suggest to think deeply about what you mean when you say “clean” or “dirty”. Do you get a feeling of revolt? Righteousness? Beauty? Elegance? How sure are you that you can name the concrete engineering outcomes corresponding to those qualities? How exactly do they affect the way the code is written and modified?

I sure didn’t think deeply about any of those things. I thought a lot about how the code looked — but not about how it evolved with a team of squishy humans.

Coding is a journey. Think how far you came from your first line of code to where you are now. I reckon it was a joy to see for the first time how extracting a function or refactoring a class can make convoluted code simple. If you find pride in your craft, it is tempting to pursue cleanliness in code. Do it for a while.

But don’t stop there. Don’t be a clean code zealot. Clean code is not a goal. It’s an attempt to make some sense out of the immense complexity of systems we’re dealing with. It’s a defense mechanism when you’re not yet sure how a change would affect the codebase but you need guidance in a sea of unknows.

Let clean code guide you. Then let it go.

83 comments

  1. I’m not sure exactly why but this weblog is loading extremely
    slow for me. Is anyone else having this problem or is it a issue on my end?
    I’ll check back later on and see if the problem still exists.

    my web site :: Rapid Keto Cut

  2. Please let me know if you’re looking for a article writer for your
    site. You have some really good posts and I believe I would be
    a good asset. If you ever want to take some of the load off,
    I’d really like to write some content for your blog in exchange for a link back
    to mine. Please send me an e-mail if interested.
    Thank you!

    Look at my blog: IQ SuperCharged Supplement

  3. I simply needed to thank you very much once more. I’m not
    certain the things that I would’ve followed in the absence
    of the actual secrets discussed by you relating
    to my theme. Entirely was a frightening setting in my circumstances,
    but being able to view a professional approach you
    treated the issue forced me to weep for delight. I will be happier for your work and trust you find out what an amazing job that you are providing teaching the mediocre ones
    all through your website. Most probably you’ve never met any of us.

    my site :: Blosum CBD Review

  4. I would like to thnkx for the efforts you’ve put in writing this site.

    I’m hoping the same high-grade site post from you in the upcoming as well.
    Actually your creative writing skills has encouraged me to get my own blog now.
    Actually the blogging is spreading its wings quickly.
    Your write up is a good example of it.

    my website – Vinyasa Anti Aging Cream

  5. Hey I am so happy I found your blog page, I really found you
    by accident, while I was browsing on Digg for something else,
    Regardless I am here now and would just like to say kudos for a incredible post and a all round exciting blog (I also love the theme/design),
    I don?t have time to browse it all at the moment but I
    have saved it and also included your RSS
    feeds, so when I have time I will be back to read a lot more, Please do keep up the awesome jo.

    My page – cs.yyqyt.com

  6. Excellent post. I was checking continuously
    this blog and I’m impressed! Extremely useful info particularly the last part :
    ) I care for such info much. I was looking for this particular info for a long time.

    Thank you and good luck.

    Feel free to visit my blog: Oracle Leaf CBD

  7. This is a great tip particularly to those
    new to the blogosphere. Simple but very accurate info?

    Thanks for sharing this one. A must read article!

    my website: D-Fine8

  8. I loved as much as you will receive carried out right here.

    The sketch is attractive, your authored subject matter stylish.
    nonetheless, you command get bought an impatience over that you
    wish be delivering the following. unwell unquestionably come
    further formerly again as exactly the same nearly very often inside
    case you shield this hike.

    Here is my web-site; Keto Lean X Review; Gilda,

  9. Yesterday, while I was at work, my cousin stole my apple ipad and tested to see if it can survive
    a twenty five foot drop, just so she can be a youtube sensation. My
    iPad is now destroyed and she has 83 views. I know this
    is totally off topic but I had to share it with someone!

    Feel free to visit my blog post … Amellia Cream

  10. Wow, fantastic blog structure! How lengthy have
    you ever been running a blog for? you make blogging glance easy.
    The full look of your web site is fantastic, as well as the content material![X-N-E-W-L-I-N-MosQiller S-P-I-N-X]I simply couldn’t
    leave your web site prior to suggesting that I really enjoyed the usual
    info an individual provide on your visitors? Is gonna be back incessantly in order
    to check out new posts.

  11. Hello there, just became alert to your blog through Google, and found that it’s really informative.

    I’m going to watch out for brussels. I will appreciate if you continue this in future.

    Many people will be benefited from your writing.
    Cheers!

    Review my blog; Spore Mens Vitality Mix Review (https://www.lgbt.gr/)

  12. Thank you for all your effort on this web site.

    Kim loves participating in research and it’s simple to grasp
    why. Most of us hear all relating to the lively method you offer both useful and interesting solutions via the
    blog and in addition welcome response from the others on this area and our girl is without a doubt learning a whole lot.
    Take advantage of the remaining portion of the new year.
    You’re doing a first class job.[X-N-E-W-L-I-N-S-P-I-N-X]I am really inspired together with your writing skills as smartly as with the format
    in your weblog. Is that this a paid subject
    or did you customize it yourself? Either way stay up the nice quality
    writing, it’s rare to see a nice weblog like this one today.

    Here is my site: Wawza Apple Cider Gummies Reviews

  13. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.

    I’ve been looking for a plug-in like this for quite some time and was hoping maybe you
    would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

    Feel free to visit my web-site :: Total Keto 365 Reviews

  14. Hey! I just wanted to ask if you ever have any issues with hackers?
    My last blog (wordpress) was hacked and I ended up
    losing many months of hard work due to no back up.
    Do you have any solutions to prevent hackers?

  15. hello there and thank you for your info – I’ve
    certainly picked up something new from right here.
    I did however expertise some technical points using this site,
    as I experienced to reload the web site lots of times previous to I could get it to load correctly.
    I had been wondering if your web host is
    OK? Not that I am complaining, but slow loading
    instances times will very frequently affect your placement in google and could damage your quality score if ads and marketing with Adwords.
    Anyway I am adding this RSS to my e-mail and could look out for much more of your respective
    exciting content. Ensure that you update this again soon..

    my web site: http://www.comptine.biz/modules.php?name=Your_Account&op=userinfo&username=TregurthaSusie

  16. I got this web site from my pal who told me concerning this web site and at
    the moment this time I am visiting this web page and reading very informative articles
    or reviews here.

  17. Greetings from Carolina! I’m bored to death at work so I decided to browse your
    website on my iphone during lunch break. I really like the info you provide here and can’t wait to take a
    look when I get home. I’m surprised at how fast your blog loaded on my mobile
    .. I’m not even using WIFI, just 3G .. Anyhow, awesome site!

  18. This is really interesting, You’re a very skilled blogger.
    I’ve joined your feed and look forward to seeking more of your magnificent post.
    Also, I have shared your website in my social
    networks!

  19. Undeniably believe that which you said. Your favorite justification appeared to be on the net the simplest thing to be aware of.

    I say to you, I certainly get annoyed while people
    think about worries that they just do not know about.
    You managed to hit the nail upon the top as well as defined out the whole thing without having side-effects
    , people can take a signal. Will likely be back to get more.
    Thanks

  20. First of all I want to say great blog! I had a
    quick question in which I’d like to ask if you do not
    mind. I was curious to find out how you center yourself and clear your mind prior to writing.

    I’ve had a tough time clearing my thoughts in getting
    my ideas out there. I truly do take pleasure in writing however it just seems like the first 10 to 15 minutes
    are usually wasted simply just trying to figure out how to begin.
    Any recommendations or hints? Cheers!

    Feel free to surf to my blog post – http://networking.drbarbara.pl/index.php?action=profile;u=915794

  21. My developer is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of the expenses.

    But he’s tryiong none the less. I’ve been using WordPress on various websites for about a year and
    am concerned about switching to another platform. I have heard
    fantastic things about blogengine.net. Is there a way I can import all my wordpress content into it?
    Any help would be greatly appreciated!

    my web page :: Far East XL Male Enhancement Ingredients

  22. I was recommended this blog by my cousin. I am not sure whether this post is written by him as
    nobody else know such detailed about my trouble.
    You are wonderful! Thanks!

  23. Great blog! Is your theme custom made or did you
    download it from somewhere? A design like yours with a few simple adjustements
    would really make my blog stand out. Please let me know where you got your design. Many thanks

    Also visit my page – Clean Cut Keto

  24. Right here is the right website for everyone who wishes to understand this topic.

    You know a whole lot its almost tough to argue with you (not that I actually will need to…HaHa).
    You definitely put a brand new spin on a topic that has been discussed
    for years. Excellent stuff, just excellent!

    Here is my blog post: Terra Xtract CBD Oil

  25. Hello would you mind sharing which blog platform
    you’re using? I’m planning to start my own blog soon but I’m having a difficult time deciding between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design and style seems different then most blogs and I’m
    looking for something unique. P.S Apologies for being off-topic but I
    had to ask!

    Also visit my page :: quality treatment

  26. Pingback: erythritol keto

Leave a Reply

Your email address will not be published.