Developers must see! KISS, DRY need to comply with the principles and coding


The full text 1500 words, when learning is expected to grow 5 Fenzhong

Source: Pexels

The first challenge encountered at the start of the programming is to write functional code. But after becoming a developer, programming skills will grow. Your code should function code from the ordinary development of simple, efficient, understandable and maintainable code.

This is the real challenge faced by developers.

 

This article will introduce five principles to help you achieve super status code.

 

1. Code glance

 

When increasing the size of the program, code complexity will increase. The code will become difficult to debug because debugging complex code is a daunting task. No one likes to maintain complex code. This principle states that you should always keep it simple code. If the code is complicated, please do your best to break down into smaller, more maintainable code.

 

Write clean code is more difficult than writing complex BS code. As a developer, along with the skills continues to mature, your code should be more clean, more meaningful.

 

2. You do not need it

 

Source: Pexels

Sometimes you should take precautions, but not in programming. People tend to write code may require future but now is not needed. These codes unnecessarily increases the size of the program, because the code is written never materialized. More importantly, most programmers will not use these codes in the future. Programmers of this habit will bloat unnecessarily.

 

The implementation of the principles laid down when necessary. This is a recommended every developer should follow.

 

3. Do not repeat

 

这一原则对于编写简单且易于修改的代码至关重要。重复的代码是程序员常犯的错误。这个原则指出,一段代码应该在源代码中的一个地方实现。如果注意到同样的代码块重复出现,说明违背了这个原则。

 

这一概念的反义词为WET代码:所有内容都重复一遍

 

可以创建一个公共函数或将代码抽象化,以避免代码中的任何重复。

 

4.关注点分离(SoC)

 

关注点分离原则:管好自己的事——就是字面意思。这个原则建议将复杂的代码划分为不同的部分或域。每个部分相互独立,因此每个部分可以独立处理。而且,维护、更新和重用代码也更加容易。

 

SoC一个很好的例子就是MVC架构。该架构将程序分成三个区域:数据(模型)、逻辑(控制器)和最终用户看到的内容(视图)。MVC在现代框架中大量运用。

 

图片来源:Wikimedia

 

5.避免过早优化

 

我们都希望优化自己的代码。但是该原则指出不应该在开发的早期阶段优化算法。

 

此原理与YAGNI原理非常相似。不同之处在于,YAGNI原则谈到了实现不必要函数的趋势,而该原则谈到了在必要之前加快算法速度的趋势。

 

过早优化的问题在于,直至出现问题之前,你永远无法真正知道程序的瓶颈在哪里。当然可以猜测,有时猜测甚至可能是对的。但是更常见的情况是,你会浪费宝贵的时间来尝试加速一个并不比预期慢的或者不像期望的那样经常被调用的函数。

 

结语

 

来源:Pexels

“编写代码的时候,永远要把维护代码的人当成一个知道你住在哪里的暴力精神病患者。”——马丁·戈尔丁

 

成为开发人员后,你会意识到项目的成功在很大程度上取决于你的团队。上面的原则可以帮助你编写可维护的代码——不仅是你自己,将来任何人都可以维护这些代码。毕竟,团结就是力量。

 

希望本文能让你有所收获。你有没有见过糟糕的代码?请在评论中告诉我们。

 

编程快乐!


推荐阅读专题

留言点赞发个朋友圈

我们一起分享AI学习与发展的干货

编译组:蒋馨怡、胡昕彤

相关链接:

https://medium.com/better-programming/kiss-dry-and-code-principles-every-developer-should-follow-b77d89f51d74

如转载,请后台留言,遵守转载规范

推荐文章阅读

ACL2018论文集50篇解读

EMNLP2017论文集28篇论文解读

2018年AI三大顶会中国学术成果全链接

ACL2017论文集:34篇解读干货全在这里

10篇AAAI2017经典论文回顾

长按识别二维码可添加关注

读芯君爱你

发布了768 篇原创文章 · 获赞 2447 · 访问量 38万+

Guess you like

Origin blog.csdn.net/duxinshuxiaobian/article/details/104177644