56
Horky SPD SW Apr/2011 http://blog.csdn.net/horkychen 1

程序员实践之路

Embed Size (px)

Citation preview

Page 1: 程序员实践之路

HorkySPD SW

Apr/2011

http://blog.csdn.net/horkychen

1

Page 2: 程序员实践之路

介绍程序员成长路上的那些小事情!

2

Page 3: 程序员实践之路

Agenda Pragmatic Programmer

Estimating (提升专业素养) DRY rule - Repeat or Reuse (提炼经验) Control Structures & Complexity (简洁就是美) Table Driven (善于运用算法) Design for CHANGE (完善设计、争取主动) Refactoring (追求卓越,勇于改进) Automation (一切都要自动化)

Resource Who is working for you (找个巨人的肩膀) Accessories for you

Process and Methods Process enhances confidence Thought Disorder(最容易欺骗的人是自己) Conceptual Blockbusting

Career anchors How to define the VALUE (价值不等式) Professionalism

Q&A

3

Page 4: 程序员实践之路

Pragmatic Programmer(注重实效)

4

Page 5: 程序员实践之路

Estimating (估算)

估算是专业能力的综合表现。

难点 需求分析

行政干预

精确度 PI

美国的议员曾提案要求将PI值定为3。

而NASA将需要12个小数位。

建议估算时间以周为单位。

5

Page 6: 程序员实践之路

估算是专业能力的体现!

新的系统

建立系统架构模型

分解为组件

设计组件的功能

依功能对各个组件进行估算

展开及细化

汇总

修改的系统

识别需要变更的组件

定义需变更的组件内容

依据变更的内容进行逐项估算

汇总

追踪估算,逐步修正误差!

Reference: http://www.cnblogs.com/badwps/archive/2009/08/

28/1555485.html

Estimating (估算)

6

Page 7: 程序员实践之路

DRY rule - Repeat or Reuse 开发者利器

Google & Baidu Sourceforge & CodeProject Ctrl+C & Ctrl+V

复用不是重复使用,而是如何重复使用!

思考: 物以稀为贵,越容易得到的也就越容易失去! 在如此开放的网络环境下,我们的知识还有多少价值?

面对变化时,维护成本变大且风险很高 知识是不稳定的!唯一不变的就是变化! BUG可能隐藏在仸何位置,那是噩梦的开始!

对个人知识积累不利 常常消化不良

7

Page 8: 程序员实践之路

Category and Count measure 类型:

无意的重复

无耐性的重复

对策

Simplify design

Cleaning Code

Coding Rule

Coding Review

Reference: Refactoring: Improving the Design of existing code

8

Page 9: 程序员实践之路

Control Structure Need a loop that executes a specified number of times

To use a for loop

If change the index value of a for loop force it to terminate, use a while loop instead.

Else to use a while loop.

Reference: Write Solid Code, Maguire, 1993

9

Page 10: 程序员实践之路

Controlling the loop Major principles

简化循环体相关连的因素(所引用的变量或数据),判

断或结束条件一定要简单。

循环也是另类的子程序,可以视其为Black box.

Entering the Loop

Put initialization code directly before the loop

Use while ( true ) for infinite loops

Don’t use a for loop when a while loop is more appropriate

10

Page 11: 程序员实践之路

Sample

Control statements vs. housekeeping statements

11

Page 12: 程序员实践之路

Processing the middle of the loop Use { and } to enclose the statements in a loop Avoid empty loops Keep loop-housekeeping chores at either the beginning or the

end of the loop Make each loop perform only one function

可以做,但不代表应该做。正确的代码不代表是好的代码!

12

Page 13: 程序员实践之路

Exiting the loop Existing loops early

According to an article in Software Engineering Notes, the software error that 7 brought down the New York City phone systems for 9 hours on January 15, 8 1990 was due to an extra break statement (SEN 1990)

break的分散代表逻辑上的分散,会带来维护上的风险!大量的return出现在函数

体,多个深层嵌套,变量中途转为它用,多处理重复功能相近的代码,都预示者开发者的逻辑可能已经出现问题!

13

Page 14: 程序员实践之路

Checking endpoints Willingness to perform this kind of check is a key

difference between efficient and inefficient programmers.

How to check

Hand calculations

Mental simulations

Calculate by Excel

What are benefits?

You understand how your code works rather than guessing about it!

14

Page 15: 程序员实践之路

How long should a loop be Make your loops short enough to view all at once.

Limit nesting to three levels

Move loop innards of long loops into routines

Make long loops especially clear.

Single exit.

15

Page 16: 程序员实践之路

Creating loop easily From the inside out

16

Page 17: 程序员实践之路

Control Structure and Complexity

The control flow is at least one of the largest contributors to complexity, if not the largest.

HP apply McCabe’s complexity metric, that is helpful to improve coding quality.

General guideline for reducing complexity

Improve your own mental juggling abilities

You can decrease the complexity of your programs and the amount of concentration required to understand them.

17

Page 18: 程序员实践之路

How to measure complexity Techniques for counting the decision points in a routine

Start with 1 for the straight pat through the routine Add 1 for each of the following keywords, or their equivalents: if

while repeat for and or Add 1 for each case in a case statement

Exercise

if ( ( (status = Success ) and done) or ( not done and ( numLines >= maxLines ) ) )

then …

*Cyclomatic Complexity18

Page 19: 程序员实践之路

Table Driven Advantage:

Simplify codes and enhance the performance

Enhance the system customization capability.

Weakness:

Not everyone know it well, it may bring extra efforts.

Virtually anything you can select with logic statements, you can select with tables instead.

19

Page 20: 程序员实践之路

Indexed Access Tables

20

Page 21: 程序员实践之路

Stair-Step Access Tables

21

Page 22: 程序员实践之路

Examplestatic unsigned int chooseDPI(unsigned int

original_dpi, int datatype)

{

int i, d, diff, k;

static Support_Mode support_mode[] = {

{4, 0}, //support two modes

{150, 2},

{300, 3}, //300DPI with color and gray

{600, 3}, //600DPI with color and gray

{1200, 2}

};

diff = -1;

k = 1;

for (i = 1; i <= support_mode[0].dpi; ++i){

if (support_mode[i].support_mode & datatype){

d = abs(support_mode[i].dpi - original_dpi);if (diff == -1){

diff = d;k = i;

}else{

if (d <= diff){

diff = d;k = i;

}else

break;}

}}

return support_mode[k].dpi;}

22

Page 23: 程序员实践之路

Design for Change• 应对变化:

– 解耦:得墨忒尔法则

– 元数据与动态配置

– 并发与次序

– 划分模块、分而治之

23

Page 24: 程序员实践之路

The law of Demeter 某个对象的任何方法都应该只能调用:

它自身

传入该方法的任何参数

它创建的任何对象

任何直接持有的组件对象

24

Page 25: 程序员实践之路

Meta data and Dynamic Configuration 将变化封装起来,使用元数据进行配置。

什么是变化的?

什么是不变化的?

抽象、抽象

Example:

Linux – Build your own Linux distribution

25

Page 26: 程序员实践之路

并发与时序 时间耦合(temporal coupling)

定义好你的工作流

用服务为并发进行设计

26

Page 27: 程序员实践之路

分而治之 观察者模式

27

Page 28: 程序员实践之路

Refactoring

重构最困难之处在于情感上,而不是技术上!

目的: 不改变“软件之可察行为”前提下,提高其可理解性,降低其维护成本.

权威著作:

Author: Martin Fowler

28

Page 29: 程序员实践之路

Example 1

分析switch(nBits)

以下两种为常用运算

+3)>>2)<<2

ConvertToMultipleOfFour

+7)>>3)+3)>>2)<<2

GetRowBytesForMonoImage

29

Page 30: 程序员实践之路

Example 2if/else两个分支中有部分重复代码

if(nDestResolution <= nFromSourceResX)

{

FlatTypeCfg.bitsPerPixel=1;

nScaleBitsPerPixel=1;

FlatTypeCfg.composition=scLineArt;

nDestWidth=nScanRight-nScanLeft+1;

nSourceWidth=FlatTypeCfg.bounds.right-FlatTypeCfg.bounds.left+1;

nSourceRowBytes=GetRowBytesForMonoImage(nSourceWidth);

scImage.rowBytes=nSourceRowBytes;

nDestRowBytes=GetRowBytesForMonoImage(nDestWidth);

}

else

{ // Use scan gray mode to simulate BW

FlatTypeCfg.bitsPerPixel=8;

FlatTypeCfg.composition=scGrayScale;

nScaleBitsPerPixel=1;

nDestWidth=nScanRight-nScanLeft+1;

nSourceWidth=FlatTypeCfg.bounds.right-FlatTypeCfg.bounds.left+1;

nSourceRowBytes=ConvertToMultipleOfFour(nSourceWidth);

scImage.rowBytes=nSourceRowBytes;

nDestRowBytes=GetRowBytesForMonoImage(nDestWidth);

}

30

Page 31: 程序员实践之路

Example 2. New codes

nDestWidth=nScanRight-nScanLeft+1;

nSourceWidth=FlatTypeCfg.bounds.right-FlatTypeCfg.bounds.left+1;

if(nDestResolution <= nFromSourceResX)

{

FlatTypeCfg.bitsPerPixel=1;

FlatTypeCfg.composition=scLineArt;

nSourceRowBytes=GetRowBytesForMonoImage(nSourceWidth);

}

else

{ // Use scan gray mode to simulate BW

FlatTypeCfg.bitsPerPixel=8;

FlatTypeCfg.composition=scGrayScale;

nSourceRowBytes=ConvertToMultipleOfFour(nSourceWidth);

}

nDestRowBytes=GetRowBytesForMonoImage(nDestWidth);

nScaleBitsPerPixel=1;

scImage.rowBytes=nSourceRowBytes;

31

Page 32: 程序员实践之路

Automation Release

Localization & Wording

Regression testing

Coding

32

Page 33: 程序员实践之路

Release Dynamic Languages

Perl

Python

Ruby

33

Page 34: 程序员实践之路

Regression testing Windows:

Python + Pywinauto

Mac OS (No full solution)

AppleScript

Sikuli

34

Page 35: 程序员实践之路

Code Generator 用模具来提高工作效率!

M4 -> Generate Info.plist

-> Generate HTML/XML file

flex -> Parser

35

Page 36: 程序员实践之路

Reference

36

Page 37: 程序员实践之路

Authors

Kent Beck Steve McConnell Andrew Hunt Dave Thomas Martin Fowler

37

Page 38: 程序员实践之路

Resource

38

Page 39: 程序员实践之路

Who is working for you More than 1,441,209 engineers are working for you:

SourceForge

Google Code

Code Project

Linux Community

39

Page 40: 程序员实践之路

Accessories Unit Test: CPPUnit, Google Test, PyUnit Static Code Checking: CPPTest, splint, Cyclomatic Complexity counting: cyclo LOC counting: Beyond Compare,Cloc in perl UML tools:Bouml, ArgoUML Drawing tool: yEd Mind mapping tool:Freemind VNC:TightVNC Editor: Notepad++ Python/Perl Editor: TextWrangler Schedule Utility on Linux/Mac OS: Taskjuggler Installation utility: Inno Setup MD5 Checksum on Windows: WinMD5 MD5 Checksum on Unix/Linux/Mac OS:md5sum Team Work : GForge, FusionForge Version Management: Subversion,Git,Bazaar

40

Page 41: 程序员实践之路

Process and Methodology

41

Page 42: 程序员实践之路

Process enhances confidence Follow the existing process well.

To reduce development risk.

42

Page 43: 程序员实践之路

Two critical issues DLL building failed

Customer get corrupted release package

Side-effect for function parameter with default value

43

Page 44: 程序员实践之路

Enhance the process with tool DLL building failed

Create one script to check version number for each DLL

Customer get corrupted release package while testing

Provides the MD5 Checksum

Side-effect for function parameter with default value

Define in coding rule

44

Page 45: 程序员实践之路

Thought Disorder

45

Page 46: 程序员实践之路

Conceptual Blockbusting

Reference: 突破思维的障碍

46

Page 47: 程序员实践之路

Methodology

大M: 公司定义的工作方法

(BMS, VM, CMMI, Agile Dev.)

小m: 个人的工作方法

(SVN, Coding style)

47

Page 48: 程序员实践之路

Key Problem in Project Schedule & Quality

No enough time to build best deliverables.

压缩开发时间,必须做好承担风险的准备!

加班或加人都不是面对风险的最佳途径!

48

Page 49: 程序员实践之路

Career anchors

49

Page 50: 程序员实践之路

Career anchors 充分认识自我,准确地“抛锚”于你钟情的职业,实现人生价值的最大

化。

我们不得不面对这样一个事实:知识经济时代要求从业者具备终身学

习能力,从业者能力的提高必然带来择业机会的增多,而从者直接导

致从业者在面对职业选择机会时困惑的加剧!职业困惑部分源于外在

因素对个人价值观的影响,更有一部分是因为困惑者对自身需求的不

了解。

50

Page 51: 程序员实践之路

Eight anchors 技术/职能型职业锚

管理型职业锚

创造/创业型职业锚

自主/独立型职业锚

安全/稳定型职业锚

服务型职业锚

挑战型职业锚

生活型职业锚 Edgar H. Schein

51

Page 52: 程序员实践之路

Your Knowledge Portfolio 经营你的资产

定期投资

多元化是长期成功的关键

周期性地重新评估和平衡资产

批判地评判你所看到的和听到的!

52

Page 53: 程序员实践之路

Study Plan for Programmer

Level 10

Code Complete 2nd Edition, Steve McConnellProgramming Pearls 2nd Edition, Jon BentleyRefactoring, Martin FowlerUML Distilled, Martin FowlerApplying UML & Patterns 2nd Ed, Craig LarmanTesting Computer Software, Cem Kaner et allConceptual Blockbusting, James AdamsSoftware Creativity, Robert GlassSoftware Project Survival Guide, Steve McConnell

Level 11

Mastering the Requirements Process, Robertson and RobertsonSoftware Requirements, Karl Wiegers"Manager's Handbook for Software Development", NASA Goddard Space Flight CenterWriting Efficient Programs, John BentleyWriting Solid Code, Steve MaquireSoftware Implementation, Michael MarcottyMore Programming Pearls, John BentleyA Practitioner's Guide to Software Testing Design, Lee CopelandRapid Development, Steve McConnell

Level 12

Design Patterns, Erich Gamma et allObject Oriented Software Construction, Bertrand MeyerObject Oriented Analysis and Design, Grady BoochSoftware Architecture in Practice, Bass et all"Manager's Handbook for Software Development", NASA Goddard Space Flight Center"Software Measurement Guidebook", NASA Goddard Space Flight Center

53

Page 54: 程序员实践之路

Level 10

Rapid Development, Steve McConnell"Manager's Handbook for Software Development", NASA Goddard Space Flight CenterMastering the Requirements Process, Robertson and RobertsonSoftware Requirements, Karl Wiegers

Level 11

Agile and Iterative Development: A Managers Guide, Craig LarmanBalancing Agility with Discipline, Barry Boehm and Richard TurnerCreating a Software Engineering Culture, Karl WiegersPeopleware, DeMarco and ListerA Practitioner's Guide to Software Test Design, Lee Copeland"Recommended Approach to Software Development, NASA Goddard Space Flight Center"Software Measurement Guidebook", NASA Goddard Space Flight Center

Level 12

Agile Modeling, Scott AdamsRequirements by Collaboration: Workshops for Defining Needs, Ellen GottesdienerScenarios, Stories, Use Cases, Ian AlexanderWriting Effective Use Cases, Alistair CockburnPeer Reviews, Karl WiegersThe Logic of Failure: Recognizing and Avoiding Error in Complex Situations, Domer DietrichCustomer Oriented Software Quality Assurance, Frank GinacCode Complete 2nd Edition, Steve McConnellApplying UML & Patterns 2nd Ed, Craig Larman

Study Plan for Manager

54

Page 55: 程序员实践之路

正當的力量,才是人生的王道!

人生不只是一場戰鬥,更是一趟有夢相隨的旅

程。

為你的夢想追根究柢,一切就從基本功開始!

◎每一個工作,都可以有計畫的幫自己尋找磨練的機會。

◎所有的成就都來自於基本功;而基本功來自於既深且廣

的歷練。

◎從事每一行都需要寬廣的視野,才能出類拔萃。

◎先求有,再求好。

55

Page 56: 程序员实践之路

THANKS

56