10 Tips for Writing Better Code

Good code can be defined as easy to read, easy to understand, easy to debug, easy to change, and most importantly, less buggy. Obviously, it takes a lot of time to knock out good code, but it makes sense in the long run because you can spend less time and effort maintaining and reusing your code. 

In fact, we can equate good code with reusable code, which is one of the important principles mentioned below. The code may just accomplish a specific function of the short-term goal of a programming job, but if no one (including yourself) wants to reuse your code, it's arguably insufficient and flawed in a way. Either it's too complex, it's too specific, or it's very likely to break under different circumstances, or other programmers may not trust your code.

Regardless of your experience level, if you consistently apply the following tips to your code (including your experiments or prototypes), good code is at your fingertips. 

1. Follow the Single Responsibility Principle Functions are the single most important form of abstraction in a programmer's library. The more opportunities that can be reused, the less code you have to write and the more reliable that code is. Small functions that follow the Single Responsibility Principle are more likely to be reused. 2. Minimize shared state Implicit shared state between functions should be minimized, whether it is a file-scoped variable or a member field of an object, in favor of explicitly taking the desired value as a parameter. When it is clear that the function achieves the desired result, the code becomes easier to understand and reuse. A conclusion can be drawn from this, you should prefer static stateless variables to member variables of objects. 3. Localized side effects Ideally side effects (such as printing to the console, logging, changing global state, filesystem operations, etc.) should be placed in separate modules, rather than scattered throughout the code. Functional side effects often violate the single responsibility principle. 4. Prefer immutable objects 



 





 



 

If an object's state is set once in its constructor, and not changed again, debugging becomes much easier because it remains valid once constructed correctly. This is one of the easiest ways to reduce the complexity of a software project. 

5. Use more interfaces and less classes . Functions that accept interfaces (or template parameters or concepts in C++) are more reusable than functions that operate on classes. 6. Apply good principles to modules Break software projects into smaller modules (such as libraries and applications) for modular reuse. Some of the key principles of modules are:  



 

  • Minimize dependencies
  • Every project should have a single clear function
  • don't repeat

你应该努力让你的项目保持小巧和明确。 

7.避免继承 

在面向对象编程中,继承,特别是虚拟函数在可重用性方面往往是一个死穴。我很少能成功地使用能覆盖类的库。 

8.同设计和开发一样进行测试 

我并不是测试驱动开发的铁杆拥护者,但在你开始编写测试代码时,编写测试自然遵循了许多指导方针。它也有助于早点将错误暴露出来。避免编写无用的测试,良好的编码意味着更高级的测试(例如,单元测试中的集成测试或功能测试)在显示缺陷方面更有效。 

9.优先选择而不是手写标准库 

我无法告诉你需要多久才能看到一个 std :: vector 或 std :: string 更好的版本,但它几乎总是浪费时间和精力。除了一个显而易见的事实,那就是你正在把 bug 引入一个新的地方。(见技巧10)其他程序员不太可能重用您的代码,而不是那些被广泛理解、支持和测试的代码。 

10.避免写新代码 

最重要的一点是,每位程序员应遵循:“The best code is the code that isn’t written”(最好的代码是不用被复写的代码)。你的代码越多,缺陷就越多,找到并修复 bug 就越困难。 

在编写一行代码之前先问问自己,有没有一个工具,函数或库已经做了你所需要的功能?你真的需要自己去实现这个功能,而不是调用另一个已经存在的功能吗? 

总结 

编程就好比是一种艺术形式或者一项运动,你只有通过不断地练习,不断地向他人学习,才能不断地提高代码的质量,这些都将有利于你成为更加高效的程序员。

 

 

http://www.iteye.com/news/32627

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326433416&siteId=291194637