http://blog.csdn.net/KITTY_chenming/archive/2007/06/30/1672377.aspx
abject: utterly hopeless, miserable, humiliating, or wretched: abject poverty.
Inheritance is a way to retain features of old code in newer code. The programmer derives from an existing function or block of code by making a copy of the code, then making changes to the copy. The derived code is often specialized by adding features not implemented in the original. In this way the old code is retained but the new code inherits from it.
function getCustName(custID) { custRec = readFromDB("customer", custID); fullname = custRec[1] + ‘ ‘ + custRec[2]; return fullname; } function getCustEmail(custID) { custRec = readFromDatabase(”customer”, custID); fullname = custRec[1] + ‘ ‘ + custRec[2]; /*************** * 4/15/96 git : email address stored in * second fax field ***************/ return custRec[17]; }
getCustEmail
was inherited from getCustName
when email addresses were added to the application. Inheriting code in this way leverages working code with less risk of introducing bugs.A modular program is one that is divided into separate files that share a common header comment block. A module usually consists of:
- Copyright notice
- Legal disclaimers
- Three to five lines of asterisks
- Change history
- Description of what the module was originally supposed to do
- Three to five more lines of asterisks
- Big block of whitespace surrounded by asterisks or other character, including the name of each function or subroutine, the author’s name or initials, and the date originally written
- The code
Abject-oriented programming lends itself to using plug-in components — bits of code found in books or on the Internet. Using a search engine a smart programmer can save time by finding pre-made software components to do almost anything. The best components are black boxes: the programmer doesn’t know or care how the component works. Many large applications are built with a combination of inheritance from other applications and components gleaned from the web.
The idea behind encapsulation is to keep the data separate from the code. This is sometimes called data hiding, but the data is not really hidden, just protected inside another layer of code. For example, it’s not a good practice to scatter database lookups all over the place. An abject practice is to wrap or hide the database in functions or subroutines, thereby encapsulating the database. In the
getCustName
function above the database is not queried directly — a function is called to read the database record. All getCustName
and getCustEmail
(and the many other functions like them) “know” is where in the customer record to find the bits of data they need. How the customer record is read is encapsulated in some other module.Polymorphism
When learning abject-oriented techniques, programmers frequently get stuck on polymorphism. It sounds hard but the idea is simple and easy to implement. Code is polymorphic when it gives different outputs for different kinds of inputs.
function getCustData(custId, what) { if (what == 'name') { custRec = readFromDB("customer", custId); fullname = custRec[1] + ‘ ‘ + custRec[2]; return fullname; } else if (what == ‘email’) { custRec = readFromDB(”customer”, custId); fullname = custRec[1] + ‘ ‘ + custRec[2]; /*************** * 4/15/96 git : email address stored in * second fax field ***************/ return custRec[17]; } /* … etc. */ }
This is a subtlety of good abject-oriented design. When first learning abject principles programmers tend to do everything with inheritance (the is-a model). With more experience programmers find that the has-a relationship is often more appropriate. In the code example above, every customer has a name, but
custRec
is a database record.A virtual class or function is code that the application will eventually need, but it isn’t written yet. This is commonly accomplished by providing a base class that the final code will be based on:
function calcSalesTax(price, isTaxable, state) { /**************************************** * * TO DO: * * get tax rate for the customer state * eventually from some table * * ****************************************/ /** 02/07/99 git -- use WA rate for now **/ return price * (7.25 / 100.0); }
Overloading is when a module or chunk of code does more than one thing. An example would be a subroutine to get a customer’s name, email address, and state sales tax rate. Using overloaded functions cuts down on method dispatching, which is one of the reasons other programming styles can result in slow code.
It’s said that code should be written for people to read, so it follows that documentation is written for no one to read. Documentation should be written for every new module and then maintained as changes are put into production, or at least the next time there’s a lull in the workload.
Not really a programming practice per se, but abject shops tend to follow similar version control practices. Keeping previous versions of code around, and tracking changes to code, is important even if only one programmer works on the application. Experienced abject programmers follow a system similar to this:
- Always add your initials and the date of the last revision to the source file’s header.
- When you are editing a file and realize that your changes are big enough to make reverting hard, save a copy with a .bak extension.
- Keep multiple backups around by appending your name or initials and the date to the backup file name:
custdata_git20040321.bak
. - Always store backups in the same directory or folder as the original code, to make it easier to see each file’s history.
You’ll probably find that most established shops already follow some or all of these abject-oriented practices. Fads like agile and “extreme” programming come and go, but the abject style has stood the test of time. Managers are familiar with abject practices and will expect you to be able to work with their abject-oriented code base.
{
custRec = readFromDB("customer", custID);
fullname = custRec[1] + ‘ ‘ + custRec[2];
return fullname;
}
{
custRec = readFromDatabase(”customer”, custID);
fullname = custRec[1] + ‘ ‘ + custRec[2];
/***************
* 4/15/96 git : email address stored in
* second fax field
***************/
return custRec[17];
}
免责声明
三到五行星号
代码变更记录
对该模块本来打算做什么的描述
再三到五行星号
以星号或其他字符包含的大块空白,内含每一函数或子例程的名称,作者的名字或缩写,以及最初编辑时间
代码
模块通常保持在合理的大小以减少耦合度并增加强健性。若某模块变得太大,应将其分割为小块,并从原来的模块中复制版权声明、免责声明等等到各小块中。在衍生新的模块时,注释总是可以被安全地继承,所以复制所有注释最安全。
{
if (what == 'name') {
custRec = readFromDB("customer", custId);
fullname = custRec[1] + ‘ ‘ + custRec[2];
return fullname;
} else if (what == ‘email’) {
custRec = readFromDB(”customer”, custId);
fullname = custRec[1] + ‘ ‘ + custRec[2];
/***************
* 4/15/96 git : email address stored in
* second fax field
***************/
return custRec[17];
}
}
{
/****************************************
*
* TO DO:
*
* get tax rate for the customer state
* eventually from some table
*
*
****************************************/
return price * (7.25 / 100.0);
}
当意识到自己的修改大到足以导致回溯困难,就把源码做个.bak拷贝。
整理多个备份的方法是将自己的名字缩写以及日期附加到备份文件名后面: custdata_git20040321.bak。
所有备份文件都应放在与所修改的源代码相同的目录中,以便看到每个文件的历史。
结论
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/KITTY_chenming/archive/2007/06/30/1672377.aspx