代码写臭_您的代码很臭。 这是您可以做的。

代码写臭

by Jonathan Solórzano-Hamilton

乔纳森·索洛萨诺·汉密尔顿(JonathanSolórzano-Hamilton)

您的代码很臭。 这是您可以做的。 (Your code stinks. Here’s what you can do about it.)

Humans stink. It’s nothing to be ashamed of: it is the inevitable consequence of our animal nature.

人类发臭。 没什么可羞耻的:这是我们动物天性的必然结果。

But humans aspire to be more than animals.

但是人类渴望超越动物。

Humanity’s singular endeavor throughout history has been the journey to eliminate stink. We’ve always hated it. Even in antiquity, we sent people on ridiculous journeys around the world in rickety sailboats, and across deserts on tiny horses, to find some remedy for it.

人类在整个历史上的独特努力一直是消除臭味的旅程。 我们一直讨厌它。 即使是在上古时期,我们也派人乘坐摇摇欲坠的帆船在世界各地进行荒谬的旅行,并乘坐小马穿越沙漠,以寻求补救

We have iterated for thousands of years to reach a stink-free world. From the invention of soap and toothpaste to the Yankee Doodle Candle Co. and Febreze — our soaps, candles, and sprays now give us a way to make food waste and industrial garbage smell like mountain raindrops and sugar cookies.

我们已经进行了数千年的迭代,以达到一个无臭的世界。 从肥皂和牙膏的发明到Yankee Doodle Candle Co.和Febreze-我们的肥皂,蜡烛和喷雾剂现在为我们提供了一种方法,可以使食物残渣和工业垃圾产生气味,例如山上的雨滴和糖饼干。

And yet with each innovation, we find a new way to stink.

但是,通过每一项创新,我们都找到了一种新的臭味处理方式。

Like our battle between antibiotics and bacteria, we have launched an endless arms race between ourselves and our environment. Now we’re moving beyond the era of physical products.

就像我们在抗生素和细菌之间的斗争一样,我们在我们与环境之间发起了无休止的军备竞赛。 现在,我们正在超越物理产品的时代。

With our advancement, we have brought stink along with us — just as we always have. We created the information age.

随着我们的进步,我们也一如既往地带来了臭味。 我们创造了信息时代。

And we created information stink.

我们创造了信息恶臭。

我应该喝这个吗? (Should I drink this?)

Garbage comes cloaked in a wonderful variety of smells. As a new father, a door opened for me into an undiscovered country of stink. Previously, I had only heard rumors of this world from the few parents who survived it. Rumors always accompanied by a thousand-yard stare.

垃圾掩盖了各种各样的气味。 作为新父亲,我进入了一个未被发现的臭味之门。 以前,我只从幸存下来的少数父母那里听说过这个世界的传闻。 谣言总是伴随着一千码的凝视。

Just so, there are many types of stinky information. Teenagers in central Europe write up fake news articles for social media clicks. There is always one more weird trick that doctors don’t want you to know.

就是这样,有很多类型的臭味信息。 中欧的青少年为社交媒体点击撰写虚假新闻文章 。 总是有另外一种怪异的技巧 ,医生不希望您知道。

Today, in this article, we’re going past all that. We’re diving into the very seams of our new information era to learn about the stinks that we Morlocks are creating in our very own products. I’m talking about code smell.

今天,在本文中,我们将超越所有这些。 我们正在深入研究新信息时代的缝隙,以了解我们Morlocks在自己的产品中正在制造的恶臭。 我说的是代码异味。

Kent Beck and Martin Fowler define code smell as “a surface indication that usually corresponds to a deeper problem in the system.

肯特·贝克(Kent Beck)和马丁·福勒(Martin Fowler)将代码气味定义为“ 表面指示,通常对应于系统中的更深层问题。

Woof.

纬。

A simpler version is that smelly code causes you to say “that doesn’t seem right” when you glance at it. Code that has begun to smell resembles milk on the cusp of spoiling. It looks like it might be okay, but it seems to be “off” just a bit.

一个更简单的版本是,臭气熏天的代码使您一看便会说“似乎不正确”。 已经开始闻起来的代码就像牛奶在变质的风口浪尖上。 它看起来像它可能是好的,但它似乎是“关”只是有点。

And — just like with smelly milk — if you want to be sure you’ll be at work the next day, you may not want to settle for smelly code. So are you ready to come to grips with your code smell?

而且-就像用臭牛奶一样-如果您要确保第二天上班,您可能不想满足于臭代码。 那么,您准备好处理代码气味了吗?

学习闻 (Learning to smell)

Acceptance is the first step to killing code stink. I know your code stinks. Maybe not all of your code, but certainly some of your code. Like I said, it’s a fact of nature — animals create stink, and as coders we are just techno-animals. Technanimals?

接受是消除代码臭味的第一步。 我知道您的代码很臭。 也许不是您的所有代码,但肯定是您的某些代码。 就像我说的,这是自然的事实-动物会散发出臭味,而作为编码者,我们只是技术动物。 技术名词?

You have to accept that your code isn’t always going to smell like roses. Especially your first draft. Your first draft is never going to be your best.

您必须接受的是,您的代码并不总是闻起来像玫瑰。 特别是您的初稿。 您的初稿永远不会是最好的。

That’s right, your “draft.” You need to start treating coding the way we treat other endeavors. You need to plan to iterate from the beginning.

是的,您的“草稿”。 您需要开始像对待其他工作一样对待编码。 您需要计划从头开始进行迭代。

You have probably spent your entire education since kindergarten learning about outlines, first drafts, rewrites, editing, peer editing, and the other steps of writing. Do you want to forget it all and treat coding like a stream-of-consciousness exercise?

自幼儿园以来,您可能已经花了整整时间学习有关大纲,初稿,重写,编辑,同伴编辑和其他写作步骤的知识。 您是否想将所有内容都忘记,将编码视为意识流练习?

Don’t. Code like you would write an argument: with a clearly planned conclusion in mind, an outline of the logical steps needed to get there, and a series of drafts. Call it “refactoring” if you want. Rubbing your own nose in your code will teach you to make it less smelly in the first place.

别。 像您这样的代码将编写一个论点:考虑到明确计划的结论,达到此目的所需的逻辑步骤的概述以及一系列草稿。 如果需要,可将其称为重构 ”。 首先,在代码中揉自己的鼻子会教您如何减少气味。

Like all smells, you’ll get better at detecting code smells once you’re sensitized to them. You will find it worthwhile to learn the most common smells and how to identify them. There are many useful articles about specific code smells and how to treat them.

像所有气味一样,一旦对代码气味敏感,就会更好地检测代码气味。 您会发现值得学习最常见的气味以及如何识别它们。 有很多关于特定代码气味以及如何处理它们的有用 文章

Technology can also help save you from smells. Do you have deodorant? Your IDE may not have a plug-in deodorizer today. But it could! Static code analysis and linting tools give your code a shower to help keep it from getting too smelly.

技术还可以帮助您避免气味。 你有除臭剂吗? 您的IDE可能今天没有插入式除臭器。 但是可以! 静态代码分析整理 工具为您的代码提供了淋浴,以防止其变得太臭。

If you’re not already habitually unit testing, this is a good reason to start. Writing unit tests forces you to go back over your code to figure out where and what to test. This is a much harder task if you’re holding your nose at the same time. Reviewing code for testing provides you with both an opportunity and an incentive for better (and less smelly) coding.

如果您还没有习惯进行单元测试,那么这是一个很好的开始。 编写单元测试会迫使您回顾代码以找出要测试的位置和内容。 如果您同时holding鼻,这是一项艰巨的任务。 复查测试代码为您提供了更好的(且气味更少)编码的机会和动机。

谁闻到… (Whoever smelt it…)

Let’s face it. Other people are going to notice your stink long before you do. It’s basically the first rule of society. Humans first started coalescing into groups for the opportunity to mock other humans. It’s an archaeological fact™️. My wife is an archaeologist, and I am 100% sure she face-palmed upon reading that.

面对现实吧。 其他人会比您早发现您的臭味。 这基本上是社会的第一法则。 人类首先开始联合成群,以便有机会嘲笑其他人类。 这是考古事实™️。 我的妻子是一位考古学家,我100%确信她读完之后会面色苍白。

The best way to find and fix the stinky spots in your code is to review it with other people. Peer code review has a lot of benefits. The first benefit is that you get to aggravate your co-workers. You’ll immediately find out how poorly written your first effort was by the groans of dismay wafting across the office after you click “submit pull request.”

在代码中查找和修复臭点的最佳方法是与其他人一起检查它。 同行代码审查有很多好处。 第一个好处是您会加重您的同事。 单击“提交请求”后,您会立即发现沮丧地在办公室里四处徘徊的gro吟声使您的第一笔努力写得多么糟糕。

Another great way to reduce stink is to review other people’s code. Take delight in pointing out their failings and flaws! Then quietly realize that you make the same mistakes all the time. The back-and-forth of a good peer review system will help you become a better developer faster than just about anything else.

减少臭味的另一种好方法是查看其他人的代码。 高兴地指出他们的缺点和缺陷! 然后静静地意识到自己一直在犯同样的错误。 好的同行评审系统的来龙去脉将帮助您比其他任何事物更快地成为更好的开发人员。

But what if you don’t have co-workers or friends and spend your days writing code alone in a dark basement? Well, dear friendless freelancer, there is no better time to start participating in open source. freeCodeCamp is a great place to start.

但是,如果您没有同事或朋友,却花一天时间在黑暗的地下室里独自编写代码,该怎么办? 好吧,亲爱的无朋友自由职业者,没有更好的时间开始参与开源freeCodeCamp是一个很好的起点。

结论 (Conclusion)

Nobody wants to stink. And you don’t have to. Coding stink is like all others: with a little hygiene it can be managed.

没有人想臭。 而且您不必。 编码臭味与其他所有臭味一样:只要保持一点卫生就可以解决。

It’s time to examine your coding process. Is it iterative? Do you seek feedback from others? Are you using free tools to check your code for “grammatical” mistakes? Are you testing?

现在该检查您的编码过程了。 迭代吗? 您是否正在寻求他人的反馈? 您是否正在使用免费工具来检查代码中的“语法”错误? 在测试吗?

It’s also time to learn about the common code smells and how to avoid them. A good coding process is like a good writing process, with lots of drafts and editing. So, too, is coding skill like writing skill: there are rhetorical devices that are known to be effective, and fallacies that are known to fail.

现在也该学习常见的代码气味以及如何避免它们了。 一个好的编码过程就像一个好的写作过程,需要大量的草稿和编辑。 同样,编码技能就像写作技能一样:有一些修辞手法是有效的,而谬论是失败的。

You don’t have to discover them all on your own. Read up on known code smells and refactoring techniques.

您不必自己全部发现它们。 阅读已知的代码气味和重构技术。

Finally, keep practicing to hone your skills. Do side projects and open source to get even more and better feedback.

最后,继续练习以磨练自己的技能。 进行辅助项目和开源,以获得更多,更好的反馈。

Your nose will thank you.

你的鼻子会感谢你的。

Please ? to let me know if you enjoyed this article!

请 ? 让我知道您是否喜欢这篇文章!

翻译自: https://www.freecodecamp.org/news/your-code-stinks-heres-what-you-can-do-about-it-3f9650653447/

代码写臭

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值