编码的方法有几种_5种改善编码的简单方法

编码的方法有几种

I’ve been in the software industry for the past five years. I’ve worked with good coders as well as bad ones. The dominant quality of a good coder is their ability to improve their code.

在过去的五年中,我一直从事软件行业。 我曾经和优秀的编码器一起工作过,无论是不好的编码器。 好的编码员的主要素质是他们改进代码的能力。

We were taught to code to make things work. Beginners mostly focus on output. If their code is working fine, then they consider it done. Some develop this approach as a habit even later in their careers.

我们被教导编写代码以使事情正常进行。 初学者主要关注输出。 如果他们的代码运行良好,那么他们认为它已经完成。 有些人甚至在职业生涯后期就将这种方法养成习惯。

Today, I’m going to talk about five techniques to improve your code. These will help beginners as well as more experienced programmers to revamp their coding.

今天,我将讨论五种改进代码的技术。 这些将帮助初学者以及更有经验的程序员来改进他们的编码。

Every programmer codes in a different way. They develop a way to write the code that’s natural to them. It’s like handwriting — it develops with practice. We’ll learn how to improve that process.

每个程序员都以不同的方式编码。 他们开发了一种编写自然的代码的方法。 就像手写一样-它是随着实践而发展的。 我们将学习如何改进该过程。

1.花大部分时间进行分析 (1. Spend Most of Your Time in Analysis)

Most programmers directly jump into coding after looking at the requirements. This ignorance is pretty common among freshers in the software industry.

大多数程序员在看完需求之后直接跳入编码。 这种无知在软件行业的新生中非常普遍。

They usually believe that coding is a major part of the solution. That’s not true. As one of my senior developers once told me:

他们通常认为编码是解决方案的主要部分。 这不是真的。 正如我的一位高级开发人员曾经告诉我的那样:

Spend 70% of your time in analysis and do the coding in remaining.

花70%的时间进行分析,然后在剩下的时间里进行编码。

This saves you from having to spend time fixing bugs and optimizing your code at the last moment. Most people can code by Googling logic, but an actual programmer knows how to do the proper analysis before rushing toward coding.

这使您不必花费时间去修复错误并在最后一刻优化代码。 大多数人都可以使用Googling逻辑进行编码,但是实际的程序员会在着手进行编码之前知道如何进行适当的分析。

怎么做: (How to do it:)

Whenever you start working on anything, first analyze the existing code. Run and debug the existing functionality.

每当您开始进行任何工作时,请首先分析现有代码。 运行和调试现有功能。

Then you have to come up with a solution where you can add your feature without breaking the existing flow of the application. Once you’ve done that, start writing the code.

然后,您必须提出一个解决方案,在其中可以添加功能而不破坏应用程序的现有流程。 完成之后,开始编写代码。

2.寻找问题的多种解决方案 (2. Look for Multiple Solutions to a Problem)

Most programmers look for a solution. Once they achieve it, they move to different problems. However, spending more time finding multiple solutions to a problem will have many benefits:

大多数程序员都在寻找解决方案。 一旦实现,他们就会面临不同的问题。 但是,花费更多时间查找问题的多种解决方案将有很多好处:

  • Your logical thinking will enhance over time.

    随着时间的流逝,您的逻辑思维将会增强。
  • You have a choice to select the best solution from multiple options.

    您可以选择从多个选项中选择最佳解决方案。
  • Your approach toward solving a problem will improve.

    您解决问题的方法将会有所改善。

This method will help you to develop critical thinking as required in later stages of programming. The good programmers are expected to come up with multiple solutions so that the best can be selected based on speed, memory, and other factors.

这种方法将帮助您开发编程后期阶段所需的批判性思维。 优秀的程序员应提供多种解决方案,以便可以根据速度,内存和其他因素选择最佳的解决方案。

怎么做: (How to do it:)

If you’re unable to find more than one solution, then search online. You will definitely find alternative approaches to your problem on platforms like StackOverflow and CodeProject.

如果找不到多个解决方案,请在线搜索。 在诸如StackOverflowCodeProject之类的平台上,您肯定会找到解决问题的替代方法。

This way, you’ll develop a habit to look out for more solutions to a single problem.

这样,您将养成习惯,寻找针对单个问题的更多解决方案。

3.不要介意自己的事 (3. Don’t Mind Your Own Business)

Well, that’s true in the case of coding at least. You should read other people’s code. Try to do that on regular basis.

嗯,至少在编码的情况下是这样。 您应该阅读其他人的代码。 尝试定期执行此操作。

As I said, we all have a unique style of coding. Reading other people’s code will give us more insights that can be beneficial for us. I’ve learned a lot from reading other developers’ code on my project team.

正如我所说,我们都有独特的编码风格。 阅读其他人的代码将给我们带来更多的见解,这对我们可能是有益的。 通过阅读项目团队中其他开发人员的代码,我学到了很多东西。

This will help you understand the bigger picture as well. If you keep coding for yourself, then you won’t be familiar with other parts of the applications that you haven’t worked on.

这也将帮助您了解大局。 如果您继续自己编写代码,那么您将不熟悉尚未使用的应用程序的其他部分。

怎么做: (How to do it:)

Once you have completed your work, then start reading code committed by other developers. This will help you to understand their thought processes and you’ll learn more about your product.

一旦完成工作,然后开始阅读其他开发人员提交的代码。 这将帮助您了解他们的思考过程,并且将进一步了解您的产品。

4.测试您的耐心 (4. Test Your Patience)

Unit testing is way more important than most developers realize. Programmers think that the main purpose of unit testing is code coverage, but that’s not true.

单元测试比大多数开发人员意识到的重要。 程序员认为单元测试的主要目的是代码覆盖率,但是事实并非如此。

Unit testing helps you to eliminate the non-required lines of code. It helps you to improve your code and overall application performance.

单元测试可帮助您消除不需要的代码行。 它可以帮助您改善代码和整体应用程序性能。

怎么做: (How to do it:)

Always write unit test cases for your module. Try to achieve as high as 90% code coverage and remove the unnecessary parts during the process. If possible, discuss the scenarios with other developers so you won’t miss out on anything important.

始终为模块编写单元测试用例。 尝试实现高达90%的代码覆盖率,并在此过程中删除不必要的部分。 如果可能,请与其他开发人员讨论方案,以便您不会错过任何重要的事情。

5.在更新代码之前更新自己 (5. Update Yourself Before Updating the Code)

If you don’t keep yourself updated with the latest technological features, then how can you make sure your code is up-to-date?

如果您不了解最新的技术功能,那么如何确保代码是最新的?

The reason most software dies over time is because of developers neglecting to update the code for optimization. For instance, take a look at below C# code:

大多数软件会随着时间的流逝而死的原因是,开发人员忽略了更新代码以进行优化。 例如,看下面的C#代码:

// Old approachIf (sampleObject != null)
{
If (sampleObject.result != null)
{
string result = sampleObject.result;
}
}
// New approachIf (sampleObject != null)
{
string result = sampleObject?.result;
}

In the old approach, we first checked whether the object was null or not, and then we checked if a particular value inside that object was available. Only after that did we assign that value to a variable.

在旧方法中,我们首先检查对象是否为null,然后检查该对象内部的特定值是否可用。 之后,我们才将该值分配给变量。

In the new approach, we’re using the new C# feature, which was introduced in version 6.0. This lets us directly query the object properties, and in the case where a property has null, then instead of throwing a null reference exception, it returns the null value.

在新方法中,我们使用了6.0版中引入的新C#功能。 这使我们可以直接查询对象的属性,并且在属性具有null的情况下,它不会引发null引用异常,而是返回null值。

This is one example of how we can simplify and optimize our code with new features in the language.

这是一个示例,说明了如何使用语言的新功能来简化和优化代码。

怎么做: (How to do it:)

Follow tech blogs and read the documentation on new features. Use them in your pet projects, and if possible, apply them in your work projects as well.

关注技术博客,并阅读有关新功能的文档。 在宠物项目中使用它们,如果可能,也将其应用于工作项目中。

This helps you stay updated, and you can keep your code healthy and up-to-date.

这可以帮助您保持最新状态,并且可以使代码保持健康和最新。

Thanks for reading!

谢谢阅读!

翻译自: https://medium.com/better-programming/5-simple-ways-to-improve-your-coding-5194be0dcb33

编码的方法有几种

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值