Why do so many developers get DRY wrong?

栏目: IT技术 · 发布时间: 4年前

内容简介:Dan Abramov’sWe’ve also looked at the reaction to various parts of the book, and discovered that we weren’t really communicating as well as we thought we were some of the ideas that we had. A classic one is DRY. DRY has come to mean “Don’t cut and paste”,

Dan Abramov’s excellent Goodbye, Clean Code post (wherein he learned it was wise to walk back an overzealous refactoring) reminded me of somethingDave Thomas said when we interviewed him and Andy Hunt about the Pragmatic Programmer’s 20th edition:

We’ve also looked at the reaction to various parts of the book, and discovered that we weren’t really communicating as well as we thought we were some of the ideas that we had. A classic one is DRY. DRY has come to mean “Don’t cut and paste”, butthe original “Don’t repeat yourself” was nothing to do with code, it was to do with knowledge. So we’ve had to go through and update that…[:headphones:]

Young Dan’s misguided refactoring began the same way so many of ours do. With these four little words:

But it was repetitive.

I’m not picking on Dan here, by any means. I’ve fallen into this trap countless times in my career and I even heard Feross talking about the same thing on a recentJS Party episode about refactoring.

This got me thinking: why do we so often get DRY wrong? I have a few thoughts on the subject…

Why do so many developers get DRY wrong?

Names really matter

One of the reasons why DRY is a thing that most of us think about is it’s so easy to remember! It’s a great acronym. Except.

“Don’t Repeat Yourself” as a name is, itself, a leaky abstraction .

Lets refer back to what Dave Thomas said above:

the original “Don’t repeat yourself” was nothing to do with code, it was to do with knowledge

The trouble with DRY is it has no reference to the knowledge bit, which is arguablythe most important part! I can’t blame the guys for going with DRY, though. DRK just doesn’t have the same ring to it…

We’re good at pattern matching

Humans are pattern matchers. Developers, especially so.

The more code you write (and read), the more patterns just pop out of your editor and stare you in the face.

I don’t know about you, but I appreciate patterns so much that I want to pull them out, give them a name, (the best ones even get their own file), and refer to them.

Why do so many developers get DRY wrong?
Photo by Patrick Hendry on Unsplash

Lowest common refactoring

Another reason why we’re so eager to DRY up our code is that itfeels so goodto do it.

Think back to your early days of programming. You likely copy/pasted code from one area of your program to another. You did this out of necessity, because subroutines/functions/etc just weren’t a tool you knew how to wield yet.

Once you eeked out enough XP to reach Level 2 , condensing that copy pasta down felt amazing . Suddenly your code looked more impressive. Efficient! Clean!Simple! This is like the lowest common form of refactoring. But it feels good

Those are a few of my thoughts on the matter. And to be clear, I’m not pro code duplication. I’m a Rule of Three person. I just find it interesting thatone of the most widely cited best practices in software is so misunderstood.

If you enjoyed this, you should definitely check out theMaster feed of all Changelog podcasts. Because have you heard? Monoliths areback in style. While you’re at it, subscribe toChangelog Weekly and join our globalhacker community all for the price of a free pretzel.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

计算统计

计算统计

Geof H.Givens、Jennifer A.Hoeting / 王兆军、刘民千、邹长亮、杨建峰 / 人民邮电出版社 / 2009-09-01 / 59.00元

随着计算机的快速发展, 数理统计中许多涉及大计算量的有效方法也得到了广泛应用与迅猛发展, 可以说, 计算统计已是统计中一个很重要的研究方向. 本书既包含一些经典的统计计算方法, 如求解非线性方程组的牛顿方法、传统的随机模拟方法等, 又全面地介绍了近些年来发展起来的某些新方法, 如模拟退火算法、基因算法、EM算法、MCMC方法、Bootstrap方法等, 并通过某些实例, 对这些方法的应用进行......一起来看看 《计算统计》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具