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.
99 comments
Right away I am going to do my breakfast, once having my breakfast coming over again to read additional news.
Very nice post. I just stumbled upon your blog and wanted to say that
I’ve truly enjoyed browsing your blog posts.
After all I will be subscribing to your feed and I
hope you write again very soon!
My blog post … Hempizor CBD Review
What a stuff of un-ambiguity and preserveness of precious knowledge on the topic
of unexpected emotions.
Feel free to surf to my web page :: Cognitive IQ Pills
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
Hi i am kavin, its my first time to commenting anywhere, when i read this paragraph
i thought i could also make comment due to this
sensible post.
My web-site :: learn.medicaidalaska.com
What’s up to all, the contents present at this website
are in fact awesome for people experience, well,
keep up the nice work fellows.
Visit my website … Blosum CBD Gummies Reviews
You are my inhalation, I possess few web logs and
sometimes run out from post :).
Look into my site … http://www.meteoritegarden.com
Wow! Thank you! I always wanted to write on my website something like that.
Can I implement a fragment of your post to my site?
Feel free to surf to my homepage :: Bio Wellness CBD Gummies Reviews
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
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
Do you have any video of that? I’d care to find out more details.
my page … Slimy Vita Vital Effect
Hello, Neat post. There is a problem along with your site in web explorer, may test this…
IE nonetheless is the marketplace leader and a large part of other people will miss your excellent writing
because of this problem.
Also visit my web site: https://talk-video.com/
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
It is not my first time to visit this site, i am browsing this site dailly and take pleasant data
from here every day.
Feel free to visit my web blog – Virectin Loaded Side Effects
Some really nice stuff on this internet site, I like it.
Also visit my web-site – IQ SuperCharged Review
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
Hi there, all the time i used to check webpage posts here early in the dawn, because i enjoy
to find out more and more.
Feel free to visit my site; DFine8 Reviews
Its superb as your other blog posts :D, thanks for putting up.
Here is my web blog – Summer Valley CBD Gummies Price
I’m not sure where you’re getting your information, but good topic.
I needs to spend some time learning much more or understanding more.
Thanks for magnificent info I was looking for this info for my mission.
My blog post – Sion Air Conditioner
Wow, superb blog layout! How long have you ever been running a blog for?
you made running a blog look easy. The entire glance of your site is great, as neatly as the content!
Also visit my website – Fast Dash Keto Pills
Right now it looks like BlogEngine is the top blogging platform available right
now. (from what I’ve read) Is that what you’re using on your blog?
My website librarius.main.jp
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
My relatives always say that I am wasting my time here
at web, except I know I am getting familiarity everyday by reading
such good posts.
Here is my website – SynerSooth CBD
I was very pleased to find this great site.
I need to to thank you for your time for this particularly fantastic read!!
I definitely liked every little bit of it and I have you saved to fav to see new things on your site.
Also visit my homepage :: Royal Derma Cream Review
Excellent read, I just passed this onto a colleague who was doing some research on that.
And he just bought me lunch since I found it for him
smile Thus let me rephrase that: Thank you for lunch!
Stop by my web page; Ardent Male Enhancement Ingredients
But wanna state that this is very useful, Thanks
for taking your time to write this.
Also visit my blog; Amellia Cream – Grant,
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
What’s up to all, how is all, I think every one is
getting more from this web page, and your views are good designed for new users.
Feel free to surf to my website Green Earth CBD Gummies Price
Of course, what a magnificent website and educative posts, I surely will bookmark your website.All the Best!
Look at my web site; Millie
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,
I as well believe thence, perfectly pent post!
My website; Nutri Blendx Keto
What’s up, just wanted to tell you, I liked this post. It was
practical. Keep on posting!
Feel free to surf to my web-site … Nutri Blendx
Some times its a pain in the ass to read what blog owners
wrote but this internet site is rattling user friendly!
my web-site :: Xtreme Shred Keto Reviews
Thanks for sharing your thoughts about coping with eczema.
Regards
Visit my website – Hempizor CBD
There is noticeably a bundle to realize about this. I assume you made
various nice points in features also.
Feel free to visit my page – Glacier Air Conditioner
Hey there! I’ve been following your website for some time
now and finally got the courage to go ahead and give you a
shout out from Porter Tx! Just wanted to tell you keep
up the fantastic job!
My web site :: http://www.1stanapa.ru/
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
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.
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/)
I don’t ordinarily comment but I gotta say thank you for the post on this special one :
D.
my site … vip5.moisait2021.ru
Loving the information on this web site, you have done outstanding job
on the articles.
Also visit my web blog :: Bio Wellness CBD Gummies
I love the efforts you have put in this, regards for all
the great articles.
Feel free to visit my website; Testo Bull
I just like the valuable info you supply to your articles.
I’ll bookmark your blog and take a look at again right here regularly.
I am fairly sure I will be informed lots of new stuff proper right here!
Good luck for the next!
my page Fast Dash Keto Ingredients
Hi there, I enjoy reading through your article post. I wanted to
write a little comment to support you.
Feel free to surf to my site – Rapid Keto Cut Reviews
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
Loving the information on this site, you have done great job on the articles.
my webpage … Paulina
It’s amazing designed for me to have a web page, which is good for
my knowledge. thanks admin
Look into my blog post :: Summer Valley CBD Gummies Review
Amazing! Its genuinely remarkable post, I have got much clear idea about from
this article.
Also visit my homepage; http://www.fles.hlc.edu.tw
I went over this site and I think you have a lot of superb info, saved to my bookmarks (:.
Also visit my site; ski-roues.net
I’ll immediately seize your rss as I can not in finding your e-mail subscription link or e-newsletter service.
Do you’ve any? Please permit me realize in order that
I may just subscribe. Thanks.
my web page :: forum.adm-tolka.ru
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
Enjoyed studying this, very good stuff, thanks.
Feel free to surf to my web-site :: fenshuajiang88.com
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?
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
I was studying some of your blog posts on this internet site and I think this site is really instructive!
Retain posting.
Also visit my web page :: Ardent Male Enhancement Reviews
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.
Whoa! This blog looks just like my old one!
It’s on a entirely different topic but it has pretty
much the same layout and design. Excellent choice of
colors!
My web-site :: Tacoma Farms CBD Cost
Aw, this was a very good post. Taking the time and actual effort
to make a very good article? but what can I say? I hesitate a lot and never seem
to get anything done.
My website; Rapid Fire Keto
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!
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!
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
Some truly nice and useful information on this website, besides I think the pattern has good features.
Have a look at my web site: astravo.net.ru
Hello, I enjoy reading through your article.
I like to write a little comment to support you.
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
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
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!
Superb, what a webpage it is! This website gives helpful facts to us, keep it up.
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
Hi there i am kavin, its my first time to commenting anyplace,
when i read this article i thought i could also make comment
due to this sensible paragraph. quest bars http://j.mp/3jZgEA2 quest bars
Wow! This blog looks exactly like my old one!
It’s on a totally different subject but it has pretty much the same page layout
and design. Outstanding choice of colors! asmr https://app.gumroad.com/asmr2021/p/best-asmr-online asmr
It’s actually a nice and helpful piece of information. I’m
happy that you simply shared this helpful information with us.
Please keep us up to date like this. Thank you for sharing.
cheap flights http://1704milesapart.tumblr.com/ cheap flights
This piece of writing is truly a pleasant one
it helps new internet users, who are wishing for blogging.
scoliosis surgery https://0401mm.tumblr.com/ scoliosis
surgery
Highly energetic blog, I loved that bit. Will there be a
part 2? ps4 https://j.mp/3nkdKIi ps4 games
I got this site from my pal who shared with me concerning this site and at the
moment this time I am browsing this website and reading very informative content at this
place. scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis
surgery
Great delivery. Sound arguments. Keep up the good work.
quest bars https://www.iherb.com/search?kw=quest%20bars quest bars
Aw, this was an extremely good post. Spending some time and actual effort to produce a good article?
but what can I say? I hesitate a lot and don’t seem to get nearly
anything done.
Look into my site; Libido Build Rx Pills
It’s an awesome post designed for all the web people; they will take benefit from it I am sure.
Here is my web site; hatched seeds
Would love to constantly get updated great web blog!
Feel free to visit my homepage :: Libido Build Reviews
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
I savour, cause I discovered exactly what I was looking for.
You’ve ended my 4 day lengthy hunt! God Bless you man.
Have a nice day. Bye
my web blog; http://sylvbuster.free.fr
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
Hello.This article was really interesting, particularly since I was searching for thoughts
on this issue last Saturday.
Also visit my page :: astravo.net.ru
Appreciate the recommendation. Will try it out.
Review my site; drug rehab centres
This is very interesting, You are a very skilled blogger.
I have joined your rss feed and look forward to seeking more of your fantastic post.
Also, I have shared your web site in my social networks!
my web page; night skin care
always i used to read smaller posts which as well clear their motive,
and that is also happening with this piece of writing which I am reading at this time.
Also visit my site; best portable air conditioner
Every weekend i used to pay a visit this website, because i want
enjoyment, for the reason that this this web site conations genuinely good funny data too.
Feel free to visit my page – http://benjamindinh.fr/
I’m curious to find out what blog system you happen to be using?
I’m having some small security issues with my latest
site and I’d like to find something more risk-free.
Do you have any solutions?
Feel free to surf to my homepage whole foods
I am extremely impressed together with your writing abilities and also with the format to your weblog.
Is this a paid subject matter or did you modify
it yourself? Either way keep up the excellent high quality writing, it is uncommon to see a great weblog like this one today.
Here is my web site :: weed doctor
My developer is trying to convince me to move to
.net from PHP. I have always disliked the idea because of the
costs. But he’s tryiong none the less. I’ve been using Movable-type on numerous websites for about a year and
am anxious about switching to another platform. I have heard good things about blogengine.net.
Is there a way I can import all my wordpress content into it?
Any help would be greatly appreciated!
Feel free to visit my page … diet plans
Hi it’s me, I am also visiting this website regularly, this web page is truly pleasant and the users are in fact sharing
pleasant thoughts.
Also visit my web site :: coping with eczema
If you desire to improve your know-how just keep visiting this web site and be updated with the
latest information posted here.
Feel free to visit my page :: http://www.a913.vip
I think this is one of the such a lot important info for
me. And i’m glad reading your article. But want to observation on few
normal things, The site taste is great, the articles is in reality
nice : D. Good process, cheers
my blog post – medical cannabis
Hi there, You’ve done a great job. I’ll definitely digg it and
personally recommend to my friends. I am confident they’ll be benefited from this site.
My homepage – healthy meal plans
Good blog you have here.. It?s hard to find high quality writing like
yours nowadays. I really appreciate people like you!
Take care!!
Here is my website; calories eating
Hello, Neat post. There is an issue along with your web site in web explorer, could check this?
IE still is the market leader and a large part of other folks will
omit your excellent writing because of this problem.
Feel free to surf to my web-site: upper back pain relief
Very good post! We will be linking to this great post on our website.
Keep up the good writing.
Greetings! Very useful advice in this particular post! It is the little changes that will
make the greatest changes. Thanks a lot for sharing!
This website was… how do you say it? Relevant!! Finally I have found something that helped me.
Kudos!