云服务器代码调试代码_不存在的代码就是您不需要调试的代码

云服务器代码调试代码

作为开发人员,我们倾向于编写不必要的代码 (As developers, we tend to write more code than necessary)

As a developer, you’re in the business of managing complexity. And code is inherently complex.

作为开发人员,您需要管理复杂性。 而且代码本质上很复杂。

By writing as little code as necessary to solve the task at hand, you’ll have fewer concerns down the road.

通过编写尽可能少的代码来解决手头的任务,您将无需再担心任何事情。

Less code, less complexity.

更少的代码,更少的复杂性。

There are many examples that can illustrate this principle. For the purpose of this post, let’s say you want to test an existing function. It’s an untested legacy function that executes a network request and does something with its response. The important aspect is that the response contains more data than what the code actually uses.

有许多示例可以说明此原理。 出于本文的目的,假设您要测试现有功能。 它是未经测试的旧版功能,可以执行网络请求并对其响应进行某些处理。 重要的方面是,响应包含的数据比代码实际使用的更多。

The code showing a function that only requires the “street”, “number” and “suburb” properties from the “accountDetails”. See the line 27.

代码显示的功能仅需要“ accountDetails”中的“ street”,“ number”和“ suburb”属性。 参见第27行。

When testing that function, you want to stub the network request and provide a fixed dataset that simulates the original one. This way you can verify whether the function works as expected.

在测试该功能时,您想对网络请求进行存根并提供一个模拟原始请求的固定数据集。 这样,您可以验证该功能是否按预期工作。

The code stubbing the network request with the same dataset. See lines 37 to 47.

使用相同的数据集对网络请求进行存根的代码 。 参见第37至47行。

But the original dataset is huge and you don't really care about the rest of it. You can just provide the minimum response necessary to satisfy the requirements of the function you’re testing.

但是原始数据集非常庞大,您实际上并不在乎其余数据。 您可以只提供满足所测试功能要求所需的最小响应。

The code, stubbing just the minimum amount of properties from the dataset. See lines 37 to 41.

代码仅对数据集中的最少属性进行存根。 参见第37至41行。

There are a few benefits with the last example:

最后一个示例有一些好处:

  1. There’s no unused response in the test

    测试中没有未使用的响应
  2. The dataset won’t be huge, so it’s easier to reason about it (and that also makes the test smaller)

    数据集不会很大,因此更容易推理(这也使测试更小)
  3. If the code starts requiring more data from the response because of another test on the same function, the test will fail and you can start adding the rest of the response on an ad-hoc basis

    如果由于同一功能上的另一项测试而导致代码开始从响应中获取更多数据,则该测试将失败,您可以开始临时添加其余响应
  4. If you always change code by changing the tests first, when the code starts requiring less data, you’ll always be removing it from the tests first in order to keep the minimum amount of code necessary to test it

    如果您总是通过先更改测试来更改代码,那么当代码开始需要较少的数据时,您总是会首先从测试中删除它,以保持测试所需的最少代码量

Test Driven Development (TDD) forces you to write the minimum amount of code that satisfies a use case. In the last example, if you had used TDD, it would have forced you to write the minimum amount of code in the dataset until you needed more data (See number 3 above).

测试驱动开发 (TDD)迫使您编写满足用例的最少代码量。 在最后一个示例中,如果您使用过TDD,它将迫使您在数据集中写入最少的代码,直到需要更多数据为止(请参见上面的数字3)。

In a great article called You Are Not Paid to Write Code, Tyler Treat says:

泰勒·特雷斯特(Tyler Treat)在一篇名为《 您没有支付编写代码的报酬》的出色文章中说:

Every time you write code […] you are introducing the possibility of failure into your system.

每次编写代码[…]时,都会将失败的可能性引入系统。

A software system has a tendency to become more complex over time, increasing Software Entropy. The act of deleting code helps drive the system to a state where there’s only code that is necessary — a state where there's less Software Entropy. But project teams tend to ignore this principle and focus mostly on adding new things.

随着时间的流逝,软件系统趋于变得更加复杂,从而增加了软件熵 。 删除代码的行为有助于将系统驱动到只需要代码的状态—减少软件熵的状态。 但是项目团队倾向于忽略该原理,而主要关注于添加新事物。

To avoid that, I believe developers should be rewarded when they remove code. Perhaps the same way (or more) than when they add new features.

为避免这种情况,我相信开发人员在删除代码时应该得到回报。 可能与添加新功能时(或更多)相同。

Removing useless code should be rewardable.
删除无用的代码应该是有益的。

Besides complexity, useless code can also represent part of a functionality that doesn't provide any value. For every feature or change related to that functionality, you have more stuff to test, maintain, and support. That’s an unnecessary cost to the project, and a cost that doesn't go away unless explicitly removed.

除了复杂性之外,无用的代码还可以表示不提供任何价值的功能的一部分。 对于每个功能或与该功能相关的更改,您还有更多要测试,维护和支持的内容。 这对于项目来说是不必要的成本,并且除非明确删除,否则成本不会消失。

Evolution has shaped our minds to think of short term benefits. Adding features or fixing bugs to satisfy the project goal leads to short term benefit in the context of Software Entropy. This is essential, but shouldn’t come at the expense of adding or leaving code that is not necessary.

进化塑造了我们思考短期利益的思想。 添加功能或修复错误以满足项目目标可在软件熵的背景下带来短期利益 。 这是必不可少的,但不应以添加或保留不必要的代码为代价。

The characteristic that makes us humans is our ability to think ahead. Our ability to think about what really matters for a project. What we need is to reinforce the culture of removing code.

使我们成为人类的特征是我们超前思考的能力。 我们思考项目真正重要的能力。 我们需要的是加强删除代码的文化。

The code that doesn’t exist is the code you don’t need to worry about.

不存在的代码就是您无需担心的代码

After all, why having to worry about something you don’t need to?

毕竟,为什么要担心不必要的事情呢?

Thanks for reading. If you have some feedback, reach out to me on Twitter, Facebook or Github.

谢谢阅读。 如果您有任何反馈意见,请通过TwitterFacebookGithub与我联系。

翻译自: https://www.freecodecamp.org/news/code-that-dont-exist-is-the-code-you-don-t-need-to-debug-88985ed9604/

云服务器代码调试代码

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值