编写优秀的程序的方法
编写优秀的程序的方法
微软软件项目开发方法--如何编写优秀的程序
主讲:林斌
主办单位:微软中国研究院
2001年12月03日
写好程序的两大要素:
1、设计
系统结构设计(由经验丰富的工程师设计Architecter )
-大型项目具有高度复杂性
Windows 2000
Exchange 2000
详细开发设计(由具体的工程师设计)
2、编程
写一流的源代码
---------
设计的特征:
占总开发时间的1/3 - 1/4
No major production code wirtten, just
- Prototype
- tools
- 常用数据结构
- Generic algorithms
没有详细设计的代码, 很可能要重写
数据结构,cache, 链表,等数据结构。
--------
(设计中最重要的是设计文档)
设计文档
Spec sampled template
- Summary Overview
- Design Goals (It's very important)
- Scenarios (用户用此软件的故事, 销售部门给用户宣传用)
- Dependecncy
- Design Requirements
Perfermance
Setup and Configuration
Re-use
Robustness
Security
- Test Requirments (测试人员是开发人员的1.5倍-2 倍)
list of difficult scenarios
Simulating Fail
Verify correctness
- Detailed Design
Design Pattern
Classes and header files (每个函数的arguments,很具体的写)
Algorithem Implementation
- Schedule
- Open issues
- Change history
--------------
Design. A case study
Design a scalable SMTP server
- Goal: Scalable
- 2-CPU, 4-CPU, 8-CPU machines
- Handle as many request as possible, with relatively fast response time.
A simple SMTP server
// Read SMTP commandds/date from sockets
IF (ReadFile( ...))
{// various housekeeping removed...}
//Parse SMTP recipicants and other headers
if (!ParseSMTPHeaders(..)){
// handle errors
}
//Parse bodies
If (!ParseSMTPBodies(..)){
// handle errors
}
//Local delivery or routing
IF (LocalDelivery(..)) {
Deliver(..);
} else {
Route(..);
}
// Send SMTP response through Socket
IF (WriteFile(..)) {
//?various housekeeping skips..
}
----------------
Traditonal Thread Architecture
1 thread to receive and dispatch SMTP request
64 worker threads doing:
- Parse SMTP headers
- Parse SMTP bodies
- Local delivery
- Routing
- All in the same the consequentially.
------------------
The Fact: Evolution of Hardware
CPU 的增长速度最快,Ram的增长速度是CPU的一半,而硬盘的速度几乎没有增长。
-----------------
The Fact:Bridge the Gap - Caches
CPU L1 cache
- 8K instruction cache, plus
- 8K data cache
- Closely coupled
- 3 clock/instruction
CPU L2 cache
- 512k static RAM
- Coupled with full clock-speed, 64-bit, cache bus
- Latency:4-1-1-1, -7 clocks/instruction
- I/O caches(RAM baesed file caches)
------------------------------
The Fact: the Price of Failure
let's look at the costs:
- Assume 1 second to zero a register
- L1 cache hit - 1 second (1x)
- L2 cache hit - 4 seconds (plus 3 seconds extra work - 7x)
- RAM hit - 25 - 150 seconds (24x - 150x)
- Disk or net hit - 3 weeks (2,000,000x)
------------------------
SMTP Server Architecture
1 thread to receive and dispatch SMTP request
2 worker threads per CPU
Event Loops(4 stages)
- Parse SMTP headers/bodies
- Local delivery
- Routing
- Socket send and file I/O
One queue for each event loops
--------------------
推荐的书:
1,Desing Pattetn , Erich、Gaxma. Richard Helon,
ISBN 0-201-63361-2
------------------------
一流源代码的特性
Solid and Robust Code
Maintainable and Simple Code
Fast Code
Small Code
Re-usable Code
Testable Code
Portable Code
(难点是如何在这些性质中平衡)
----------------------
1,麦当劳的经营 -Follow Basic Coding Standard
Your code may outlive you or your memory!
Think about the maintainer
Comment your code
File Header
Function Header
Inline Commenting
Pick a good name for your functions and varibales
naming convetions - Hunganan
Align your code
Less arguments
Use all uppercase with underscores for macro
Declare all your variables at the start of function.
Avoid hard coding constant, Use enum or macro
User braces for one line code
Limit the length of a single function
----------------------
What is the maximum length of a function
Watch out for functions > 200 lines!
Study in 1986 on IBM's OS/360: the most error-prone routines => longer than 500 lines.
Study in 1991 on 148,000-line of code: functions < 143 lines => 2.4 times less expensive to fix then longer functions
function's longth is less than 143 lines.
-----------------------
Naming Conventions
匈牙利命名法
-----------------------
不要偷懒 Practice Defensive Coding
Initialize variables
Check return values
Keep it simples
Usually, simplicity = performance
Keep it clean
Multi-thread clean
Minimize casts
Avoid miscalculatons
Divide by Zero
正负号
-----------------------------
放下“牛”架子
Test but don't stop there
在debugger里走一遍,两遍,三遍
Especially the error code paths
多走几遍 Test as much of your code as you can
从未走过 肯定不work
Code review
Have somebody else read it
And read somebody else's code
-------------------------
疑心重重写代码
Avoid Assumptions
不要相信任何人
There will be badly-written apps.
An interface may have
活用段落 -user but don't abuse ASSERT
---------------
最高境界,无招胜有招,Stop writting so much code.
More code is not better
Think twice before you cut-and-paster
------------------------
严谨的治学态度
It's all about attitude
Take pride in your code
Don't get your ego wrapped up in it .
------------------------
研究院网页:
http://www.microsoft.com/chinaresearch
林斌电子邮箱:
binlin@microsoft.com
---------------------
微软公司对工程师的要求
数据结构,基本算法,Patience,有信心,
-----------
软件开发的优先级:work -> robust -> performance -> ..
系统软件开发注重效率,性能,用C语言开发.
应用软件开发注重UI attractive, 启动的速度(应考虑用户的机子配置较低)
-----------
Program Manager的工作是对内对外的协调工作。他不仅对自己team的工作熟,也必须对其它team的工作熟。
熟悉一门语言,然后充分利用它,就足够了。
微软要公布WinCE的源代码,先改成能在桌面上运行。
公布Win Kernel的一些功能的包,来实现windows的一些功能。