让代码更好_长代码与短代码:什么对您的用例更好?

让代码更好

In order to successfully program an application, you need to make a number of small decisions while trying to solve a greater set of problems.

为了成功地对应用程序进行编程,您需要做出一些小决定,同时尝试解决更多问题。

How wisely you make those decisions, and whether you write long lines of code or short lines of code, relies more on your preferences, skills, and expected outcomes.

您做出这些决定的明智程度,以及编写的是长代码行还是短代码行,更多地取决于您的偏好,技能和预期结果。

So you might be wondering: between long code vs. short code, which is better?

因此,您可能想知道:在长代码与短代码之间,哪个更好?

Here are some factors to consider before deciding whether to use many or fewer lines of code.

在决定是使用多行还是少行代码之前,请考虑以下因素。

1.可读性 (1. Readability)

Martin Fowler, an expert software developer, once said, “Any fool can write code that a computer can understand. Good programmers write code that humans can understand”.

资深软件开发人员Martin Fowler曾经说过:“任何傻瓜都可以编写计算机可以理解的代码。 好的程序员编写人类可以理解的代码。

Create your code with other people in mind. On the one hand, it will be processed by a machine, which doesn’t care whether you use a lot or a little code. But your source code may evolve in other people’s hands who need to understand how the code operates and what improvements they should make.

在考虑其他人的情况下创建您的代码。 一方面,它将由机器处理,这与您使用大量代码还是无关紧要。 但是您的源代码可能会在其他人的手中发展,他们需要了解代码的运行方式以及应该进行哪些改进。

So, when programming, the readability of the source code will often be more important than the number of lines of code.

因此,在编程时,源代码的可读性通常比代码行数更为重要。

Here is an example of some JavaScript code that uses the ternary operator syntax for an if..else statement:

这是一些JavaScript代码的示例,该代码对if..else语句使用三元运算符语法:

const firstNumb = 100;

let secondNumb;

const secondNumb = firstNumb > 50 ? "Number is greater than 50" : "Number is less than 50";

Here is the same code written in the traditional long-form format:

这是用传统的长格式书写的相同代码:

const firstNumb = 100;

let secondNumb;

if (firstNumb > 50) {
  secondNumb = "Number is greater than 50";
} else {
  secondNumb = "Number is less than 50";
}

The second programming style consists of many more lines of code than the first one. But you could argue that it's easier to read and interpret—especially for novice programmers.

第二种编程样式比第一种包含更多的代码行。 但是您可能会争辩说,它更易于阅读和解释,尤其是对于新手程序员而言。

Still, the first version with the ternary operator syntax could be a great time saver if you want to write an if..else statement in just a single line.

不过,如果您只想在一行中编写if..else语句,则具有三元运算符语法的第一个版本可能会节省大量时间。

If you are working on a project with other programming teams, make sure your code is readable. Especially if you care for the long-term sustainability of the project.

如果您正在与其他编程团队一起进行项目,请确保您的代码可读。 特别是如果您关心项目的长期可持续性。

You need to consider that other developers may not be able to easily interpret your source code. So you need to make your code easily and quickly understandable.

您需要考虑其他开发人员可能无法轻松解释您的源代码。 因此,您需要使您的代码易于快速理解。

For example, if you are collaborating with other developers in building an app that fetches Netflix content, then it is better to use long, clear lines of code. In that case, short lines of code may only show your peers that are you are “smart,” and their input may not be useful.

例如,如果您正在与其他开发人员合作构建可获取Netflix内容的应用程序,则最好使用清晰的长代码行。 在这种情况下,短代码行可能只会显示您的同龄人“很聪明”,而他们的输入可能没有用。

2.可维护性 (2. Maintainability)

Short and confusing code can be difficult to maintain. It may result in problems like bugs and higher overhead costs during quality maintenance. It can also cause motivational issues and plain hullabaloo for you as a developer.

简短而混乱的代码可能难以维护。 在质量维护过程中,可能会导致诸如错误和更高的间接费用之类的问题。 对于开发人员来说,这也可能会引起激励性问题并给您带来麻烦。

What if you write a short piece of code and discover that you are unable to interpret it six months from now? Longer, more detailed code might assist you in reacquainting yourself with what you wrote and why you wrote it in that particular style.

如果您编写了一段简短的代码却发现六个月后无法解释该怎么办? 更长,更详细的代码可以帮助您重新了解自己编写的内容以及为什么以这种特定风格编写。

With longer code, debugging a program becomes much easier since you will have unrelated variables to scrutinize and more places to insert breakpoints.

使用更长的代码, 调试程序变得更加容易,因为您将有不相关的变量要检查,并且有更多的位置插入断点。

On the other hand, short and unclear code can waste time and money while developers re-factor or rewrite the existing code to include new features that are easier to maintain in the long run.

另一方面,短而不清楚的代码可能会浪费时间和金钱,而开发人员会重构或重写现有代码以包含更易于长期维护的新功能。

This code is shorter:

该代码更短:

let a, b, c= 50;

However, it is easier to maintain the code below:

但是,维护以下代码更加容易:

let a;

let b;

let c = 50;

3.效率 (3. Efficiency)

Arguably, using shorter lines of code is more efficient than spreading the code over several lines. If you have more lines of code, there are more places for bugs to hide and finding them might be more of a hassle.

可以说,使用较短的代码行比将代码分散在几行上更为有效。 如果您有更多的代码行,则有更多的地方可以隐藏错误,发现错误可能更麻烦。

Fewer lines of code can achieve the same results (and probably better) than many lines of code. If you reduce the amount of code in a task, you will lower the bug count, especially if the source code is clear, readable, and maintainable.

与许多代码行相比,更少的代码行可以达到相同的结果(并且可能更好)。 如果减少任务中的代码量,则将减少错误计数,尤其是在源代码清晰,可读且可维护的情况下。

Also, writing long lines of code may require you to include too many local variables, since you have to formulate names for them. All those different names can lead to confusion and inefficient programs.

另外,编写较长的代码行可能会要求您包含过多的局部变量,因为您必须为其指定名称。 所有这些不同的名称都可能导致程序混乱和效率低下。

If you want to build efficient apps with fewer bugs to trouble you, then using fewer code lines may be your best solution.

如果您想用更少的错误来构建高效的应用程序,那么使用较少的代码行可能是最佳的解决方案。

4.预期工作量 (4. Expected workload)

Shorthand coding enables you to achieve more with less, and therefore drastically reduces the number of hours you spend developing your apps. With sufficient training and experience, you can learn how to do more quickly and with fewer lines of code.

速记编码使您可以用更少的钱实现更多的目标,因此可以大大减少开发应用程序所花费的时间。 借助足够的培训和经验,您可以学习如何以更少的代码行数更快地执行操作。

As you probably know, long lines of code are sometimes demanding to write and can make you put in long hours.

如您所知,有时需要编写很长的代码行,这会使您花费很长时间。

With short code, you can lower the amount of code needed for repetitive statements and string manipulation. This way, instead of using verbose code, you can conveniently combine many steps into single steps and greatly reduce your workload and other associated costs.

使用短代码,可以减少重复语句和字符串操作所需的代码量。 这样,您无需使用冗长的代码,就可以方便地将许多步骤组合为一个步骤,从而大大减少了工作量和其他相关成本。

Here is some JavaScript code written using many lines:

这是使用多行代码编写的一些JavaScript代码:

function myFunc(foo) {
  console.log("Hello World", foo);
}

setTimeout(function () {
  console.log("Upload completed");
}, 3000);

mylist.forEach(function (foo) {
  console.log(foo);
});

And, here is the same code in a shorter format written using the JavaScript arrow function syntax:

并且,以下是使用JavaScript箭头函数语法编写的较短格式的相同代码:

myFunc = (foo) => console.log("Hello World", foo);

setTimeout(() => console.log("Upload completed"), 3000);

mylist.forEach((foo) => console.log(foo));

Clearly, the first example requires more time to write than the second example.

显然,第一个示例比第二个示例需要更多的时间来编写。

If workload is important for you and you have sufficient programming skills, then using shorter code is probably the way to go.

如果工作量对您很重要,并且您具有足够的编程技能,那么使用较短的代码可能是可行的方法。

结论 (Conclusion)

Ultimately, the choice whether to use long code vs. short code does not matter. Instead, what matters is writing the code in a manner suitable for its intended use. Source code that is longer than it needs to be will result in more errors, increased unnecessary load, and wasted time and resources.

最终,选择使用长代码还是短代码都没有关系。 相反,重要的是以适合其预期用途的方式编写代码。 源代码长于所需的时间,将导致更多的错误,不必要的负载增加以及时间和资源的浪费。

On the other hand, if you are writing shorter code by replacing numerous simple lines of code with one complex line of code, or verbose statements with vague ones, or straightforward operations with strange hacks, then the loss in overall usefulness will normally outweigh the gain in conciseness.

另一方面,如果您通过用一个复杂的代码行替换许多简单的代码行,或者用模糊的代码替换冗长的语句,或者使用奇怪的hacks进行简单的操作来编写较短的代码,则总体上的损失通常会超过收益简明扼要。

So, when building an application, you should make sure that every line serves its intended purpose. And whether you use long lines of code or short lines of code should not be your key motivator.

因此,在构建应用程序时,应确保每一行都达到其预期目的。 无论您使用长代码行还是短代码行都不应该成为您的主要动机。

Your real objective for each case should be to achieve high standards of readability, maintainability, efficiency, and cost-friendliness—normally in that order. And the number of lines of code should not have a place in that list.

对于每种情况,您的真正目标应该是达到可读性,可维护性,效率和成本友好性的高标准-通常按​​此顺序进行。 并且代码行数在该列表中不应占位。

翻译自: https://www.freecodecamp.org/news/long-code-vs-short-code/

让代码更好

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值