A good program structure should be what?

Learn more Embedded Linux, Qt and embedded microcontrollers public concern number "playing embedded":

"Procedural framework is needed early in the construction program to complete the task, a program of infrastructure directly affect the probability of fluency and the emergence of bug realization of the whole project."

A relatively large project often requires many people to do, so many of coordination in general is a big problem, if the project initially build on a set of clear and complete framework, the latter will become smooth coordination. This involves the question of how are we going to build a beautiful code architecture to avoid low-level logic problems occur in stages to achieve the function of it ?

First person in charge of the code architecture must be clear, it must take into account the problems that may arise . For example, I would package the function to which the incoming parameters, passing in what form, I need to return value or other forms of this I need to pass variables outside variables; Another example, one of my function is definitions used in the document which it will be more convenient to consider the actual meaning of this function, the number used in the file which is defined in the multi-where; or we have a project to have a main interface, are we going in this document internal data transfer, or otherwise defined file to a data transfer, these are the places we have to consider.

Second function to a single function . A function is like a young man, must be single-minded, can not chop and change, but also to read the file, but also thinking to deal with data, to do so will only be a result, not only is this a function of reading bad files are also handled properly data, and other functions want to call up the file of raw data to otherwise deal with it is not easy to do. The best way is to function single function , the bug appears smaller probability, and error we can very easily find the problem, more importantly, this function can be called repeatedly, repeatedly calling it will not affect the project other features . We can look back, we each library function call, are very good code, also is not a single function of it?

再一个是要充分利用语言的优势拿c++来说,我们在写c++代码时一定要利用他的封装继承多态三大特性,这样我们写出来的代码更简洁高效。

1、封装

封装指的是我们可以在类中将我们不希望外界调用的函数或者成员变量设置成私有(private)或者保护(protected)成员的形式,而外界需要调用的设置成公共成员(public)的形式,这样不仅保护了我们的函数不被改变,并且也告诉后来者不必去动我的私有和保护成员的函数和变量。

2、继承

继承指的是一个类被定义了之后,可以去由其他有需要的类去继承父类的成员公共或者受保护成员的变量和函数,这样使得我们很多函数和变量不必去重复定义,减少代码量。

3、多态

多态指的是定义在父类内的虚函数,可以被子类重新定义,这个过程我们称之为“重写”,这样做的好处是我们可以不必多次在每个文件中去定义逻辑相同的函数,大大提高了函数的重用。

在其他的语言中我们也应注意到语言的优势,利用这种优势去完成任务。

总之,我们在构建代码初期一定要倍加小心,“合抱之木,生于毫末”,任何一个小的逻辑错误就会造成整个产品的失败。

发布了24 篇原创文章 · 获赞 35 · 访问量 1万+

Guess you like

Origin blog.csdn.net/Groot_Lee/article/details/104301910