编写代码的软件用什么编写的_有效编写代码的9种技巧

编写代码的软件用什么编写的

终极编程指南 (ULTIMATE PROGRAMMING GUIDE)

It’s really easy to write efficient and faster code. Efficient code, not just only improves the functionality of the code but it can also reduce the time and space complexity of the programming.

编写高效快速的代码真的很容易。 高效的代码,不仅可以改善代码的功能,还可以减少编程的时间和空间复杂性。

Speed is one of the major factors in deciding the quality of the code, for instance, your code might be producing the required result but it takes some time to execute then it will not be considered a quality code. An alternative approach to the same problem producing faster results will be considered better.

速度是决定代码质量的主要因素之一,例如,您的代码可能会产生所需的结果,但是执行需要花费一些时间,因此它不会被视为质量代码。 对于产生更快结果的相同问题的替代方法将被认为是更好的。

The code should be clean i.e. comprehensible and readable so that it can be reused (saving the efforts of rewriting the whole program from scratch), adding new features, and making the process of debugging more easier.

该代码应该是干净的,即易于理解可读的,以便可以重复使用(节省了从头开始重写整个程序的工作),添加了新功能以及使调试过程更加容易。

In this article, I will cover some simple tips and techniques which we can easily apply to make our code more elegant and efficient.

在本文中,我将介绍一些简单的技巧技术 ,我们可以轻松地应用这些技巧技术 ,以使我们的代码更加优雅和高效。

There is always more than one method to solve the problem.

解决问题的方法总是不止一种。

如何有效地编写代码 (How to write code efficiently)

Now let me show you how we can make our code efficient and faster.

现在,让我向您展示如何使我们的代码高效和快速。

1.创建功能 (1. Creating function)

If your block of code is being repeated or it has a similar structure and it can be parameterized, generalize it by creating a function for the same.

如果您的代码块被重复或具有相似的结构并且可以对其进行参数化,则可以通过为该代码块创建函数来对其进行概括。

Creating a function will be computationally cheaper and will create a much cleaner (readable) code as well.

创建函数将在计算上更便宜,并且还将创建更清晰(可读)的代码。

def function(a,b,c):
#write your code here

Rather than writing the same code again and again, now we will just have to call the function to execute the code block.

不必一次又一次地编写相同的代码,现在我们只需要调用函数来执行代码块即可。

2.消除不必要的操作 (2. Eliminate unessential operations)

Processing of the same data, again and again, can be avoided by saving the required value in variables (for example- calculating the length of string, conversion of integer to string, etcetera). The data can be stored in a dictionary or an array for faster execution of code (elimination of recalculation of values). For example:

通过将所需的值保存在变量中(例如,计算字符串的长度,将整数转换为字符串等),可以避免一次又一次地处理相同的数据。 数据可以存储在字典或数组中,以加快代码执行速度(消除对值的重新计算)。 例如:

l = 'abc'
for i in range(100):
print(i * len(l))

In the code above, the length of l will be calculated 100 times because of the for loop. This unnecessary calculation can be eliminated by storing the value in a variable.

在上面的代码中,由于for循环, l的长度将被计算100次。 通过将值存储在变量中,可以消除这种不必要的计算。

l= 'abc'
k = len(abc)
for i in range(100):
print(i * k)

Now, as we can see the calculation of length multiple times is eliminated. This method can be very effective for large codes.

现在,如我们所见,消除了多次长度计算。 此方法对于大型代码可能非常有效。

3.避免声明不必要的变量 (3. Avoid declaring unnecessary variables)

In a function, if we want to return a certain value, instead of storing it in a variable we should directly return it. For example:

在函数中,如果我们要返回某个值,而不是将其存储在变量中,则应直接返回它。 例如:

def function(b,c):
a = sum(b,c)
return a

Instead of writing the above-mentioned code this way, we can write it as:

不用这样写上面的代码,我们可以这样写:

def function(b,c):
return (sum(b,c))

This simple technique can make our code look cleaner.

这种简单的技术可以使我们的代码看起来更简洁。

4.使用适当的算法 (4. Use appropriate algorithms)

Selecting the correct algorithm for solving the programming problem is very necessary. Only an efficient coding style won’t make your code efficient and fast. Think of an algorithm that works with low time and space complexity even for large values.

选择正确的算法来解决编程问题是非常必要的。 只有高效的编码风格才能使您的代码高效而快速。 想一想即使对于较大的值也可以以较低的时间和空间复杂度工作的算法。

5.学习动态编程的概念 (5. Learn the concept of dynamic programming)

Dynamic programming is an approach that can be applied to a certain class of problems (the problems which require the calculation of certain values multiple times, resulting in high time complexity of the code). This technique significantly optimizes the code and make it much more efficient.

动态编程是一种可以应用于特定类别问题的方法(这些问题需要多次计算特定值,从而导致代码的时间复杂度很高)。 该技术极大地优化了代码,并使代码效率更高。

Here the concept is to break the problem into sub-problems and save the result (store the values in a list/array or dictionary) for the future so that we will not have to compute that same problem again.

这里的概念是将问题分解为子问题,并保存结果(将值存储在列表/数组或字典中)以备将来使用,这样我们就不必再次计算相同的问题。

Check out our piece on Dynamic Programming below.

在下面查看我们有关动态编程的文章。

6.尽量减少使用If-Else (6. Minimize the use of If-Else)

If-else approach is one of the most common and easy approaches to solving code-branching problems.

If-else方法是解决代码分支问题的最常见且最简单的方法之一。

However, it results in less readable code and increases the complexity with any new conditional requirement implemented using If-Else.

但是,如果使用If-Else实施任何新的条件要求,它将导致代码可读性降低,并增加复杂性。

The appropriate strategy should be applied to obtain an efficient code. Few of them are:

应该应用适当的策略以获得有效的代码。 其中很少有:

  • Eliminate unnecessary else block- Understand this with the example.

    消除不必要的else块-通过示例理解这一点。

def function():
if a == True :
#do this
else:
#do this

Now remove else block from this code.

现在,从此代码中删除else块。

def function():
if a == True :
#do this
return value
#do this

This code will work the same as the one above but here the else block is eliminated.

此代码将与上面的代码相同,但是在此取消了else块。

  • Storing If-Else in a dictionary- This method can be used in the case where we have to perform an operation based on some condition and some more operations will be added later.

    在字典中存储If-Else-如果我们必须根据某些条件执行某个操作,并且稍后会添加更多操作,则可以使用此方法。

variable_one = 'variable'
if variable_one == 'a' :
variable_two = 1
elif variable_one == 'b' :
variable_two = 2
elif variable_one == 'b' :
variable_two = 3
else:
variable_two = 4

Here, if we want to perform another operation then we will have to add another elif(else-if)! This does not look like a nice approach. Now let’s try the same problem by using a dictionary.

在这里,如果要执行其他操作,则必须添加另一个elif (else-if)! 这看起来不是一个好方法。 现在,让我们使用字典尝试相同的问题。

variable_one = 'variable'
dict_ = {'a' = 1, 'b' = 2, 'c' = 3}
variable_two = dict_.get(variable_one,4)

This method eliminates the need for the If-Else statement!

这种方法消除了对If-Else语句的需要!

In the above code, if and elif (else-if) conditions are stored in the dictionary and the value of variable_two are easily obtained using .get() (where the second parameter of .get() the function acts as else statement).

在上面的代码, ifelif (否则-IF)条件被存储在字典中和的值variable_two使用容易获得.get()其中的第二个参数.get()函数作为else语句)。

This approach makes the code look cleaner and easily readable.

这种方法使代码看起来更整洁并易于阅读。

7.必要时中断循环 (7. Break the loops when necessary)

The number of iterations of the loops should be reduced as much as possible. The loop should break when the required value is calculated instead of running all the iterations. For example:

循环的迭代次数应尽可能减少。 当计算出所需值而不是运行所有迭代时,循环应该中断。 例如:

for i in range(x):
#write the code
if required_value == True:
break

As we can see in the above example, instead of running over all the iterations, the loop ends immediately after the required result is obtained. This will make the code efficient and computationally cheaper to execute.

正如我们在上面的示例中看到的那样,循环没有结束所有迭代,而是在获得所需结果后立即结束。 这将使代码高效且执行起来在计算上更便宜。

8.避免在全局范围内声明变量 (8. Avoid declaring variables in the global scope)

When you have to write a very large code (thousands or millions of lines), avoid creating variables and loops in the global scope, as they can create noise and influence code desirable.

当您必须编写非常大的代码(成千上万的行)时,请避免在全局范围内创建变量和循环,因为它们会产生噪声并影响所需的代码。

We should write code inside functions or methods. And these functions and methods should be defined inside the class definitions.

我们应该在函数或方法内部编写代码。 这些函数和方法应该在类定义中定义。

This will make code clean and easy to maintain. Moreover, we should always add comments wherever necessary to make the code easily understandable and reusable for later use.

这将使代码干净并且易于维护。 此外,我们应该始终在必要的地方添加注释,以使代码易于理解和重用,以供以后使用。

9.继续练习! (9. Keep practicing!)

Somethings can only be learned by practicing. So just keep coding, you will definitely learn many things through experience that no one will ever teach you.

有些东西只能通过练习来学习。 因此,只要继续编码,您肯定会从经验中学到很多东西,这是没人会教您的。

结论: (Conclusion:)

These are some essential tips and techniques for any developer who wants to achieve mastery in their field. Implementing these basic rules at the right time can make your code much better and professional at the same time.

对于那些想要在其领域内精通掌握的开发人员来说,这些是一些必不可少的技巧和技术。 在正确的时间实施这些基本规则可以使您的代码同时变得更好,更专业。

So now it’s the time to put an end to old practices and try executing new techniques to make your code efficient, readable, and maintainable to work with.

因此,现在是时候结束旧的做法,并尝试执行新技术,以使您的代码高效,易读且可维护。

If you have any questions or comments, please post them in the comment section.

如果您有任何问题或意见,请将其张贴在评论部分。

Originally published at: www.patataeater.blogspot.com

最初发布于: www.patataeater.blogspot.com

翻译自: https://medium.com/swlh/how-to-write-efficient-and-faster-code-67567e74ef87

编写代码的软件用什么编写的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值