The Art of Naming The Art of Naming agentzh ( ) 2006.9 Why - - PowerPoint PPT Presentation

the art of naming the art of naming agentzh
SMART_READER_LITE
LIVE PREVIEW

The Art of Naming The Art of Naming agentzh ( ) 2006.9 Why - - PowerPoint PPT Presentation

The Art of Naming The Art of Naming agentzh ( ) 2006.9 Why naming matters ? Good hackers poets and wordsmiths A poetry in Pugs' Main.hs


slide-1
SLIDE 1

The Art of Naming

slide-2
SLIDE 2

The Art of Naming 命名的艺术 ☺agentzh (章亦春)☺

2006.9

slide-3
SLIDE 3

Why naming matters? 为什么命名很重要?

slide-4
SLIDE 4

Good hackers ➥ poets and wordsmiths 好的黑客 ➥ 诗人和词汇大师

slide-5
SLIDE 5

A poetry in Pugs' Main.hs ⇒ Pugs 的 Main.hs 中的英文诗歌 ⇒

slide-6
SLIDE 6

A ship then new they built for him Of mithril and of elven-glass With shining prow; no shaven oar Nor sail she bore on silver mast; The Silmaril as lantern light And banner bright with living flame To gleam thereon by Elbereth Herself was set, who thither came...

slide-7
SLIDE 7

众人为彼造新舟, 铸以秘银精灵璃。 船首闪耀何需桨, 银桅未有风帆系。 无双宝钻作灯炬, 旗帜辉煌展生焰。 映照燃星雅碧绿, 神祇乘梭下九天。

  • - 唐凤
slide-8
SLIDE 8

Perl 6 translation ⇒ 翻译到 Perl 6 编程语言 ⇒

slide-9
SLIDE 9

use v6; for $*Larry {

  • ur Ship $pugs .= new(:of<mithril elven-glass>);

given $pugs { $.prow does Shine; Silver $.mast but none(Oar::Shaven, Sail); Light $.lantern := $*Silmaril; Bright $.banner := Flame.bless:{}; when $*Elbereth.gleam { .sail(...); } } }

slide-10
SLIDE 10

Top programmers take naming issues very seriously 顶级的程序员对命名问题 非常认真。

slide-11
SLIDE 11

as seen on the #perl6 channel

  • f irc.freenode.net...

正如在 irc.freenode.net 上的 #perl6 通道里 所看见的那样……

slide-12
SLIDE 12
slide-13
SLIDE 13

<agentzh> TimToady++ # JIT syn fixes <TimToady> thanks <TimToady> I looked at several hundred words before picking START. <章亦春> (Larry Wall)++ # 即时 Perl 6 文档修改 <Larry Wall> 谢谢 <Larry Wall> 我在挑选出 START 这个名字之前查看了 几百个单词。

slide-14
SLIDE 14
slide-15
SLIDE 15

<gaal> defer? slothlike <audreyt> I like "defer" <audreyt> gaal++ * audreyt deletes Data::Thunk and uploads Data::Defer <audreyt> ...and it's now Scalar::Defer <audreyt> naming takes more time than tests+doc+code <高浪> defer 怎么样? 有些惰性的味道 <唐凤> 我喜欢“defer” <唐凤> 高浪++ * 唐凤删除了 Data::Thunk 并上传了 Data::Defer <唐凤> ……现在它叫做 Scalar::Defer 了 <唐凤> 命名所花费的时间比测试 + 文档 + 实现代码还要多

slide-16
SLIDE 16

☆ Follow the naming convention

  • f the language you are using

and try to be consistent. 遵循你所使用的语言的命名约定, 并努力保持一致性。

slide-17
SLIDE 17

The problem with being consistent is that there are lots of ways to be consistent, and they're all inconsistent with each other.

  • - Larry Wall

保持一致性的困难在于存在许许多多种不同方式 来实现一致性,而且它们彼此之间都是不一致的。

  • - Larry Wall
slide-18
SLIDE 18

// C++ STL naming style vector<string> list; list.push_back("hello"); while (!list.empty()) {...}

slide-19
SLIDE 19

// Java naming style List list = new ArrayList(); list.add("hello"); while (!list.isEmpty()) {...}

slide-20
SLIDE 20

// C# naming style ArrayList myAL = new ArrayList(); myAL.Add("hello"); Console.WriteLine(myAL.ToString());

slide-21
SLIDE 21

☆ Use meaningful names 使用有意义的名字

slide-22
SLIDE 22

? Button button1 = new Button("New"); ? Button button2 = new Button("Open"); ? Button button3 = new Button("Save");

slide-23
SLIDE 23

? Button button1 = new Button("New"); ? Button button2 = new Button("Open"); ? Button button3 = new Button("Save"); ☹ This is bad.

slide-24
SLIDE 24

? Button button_New = new Button("New"); ? Button button_Open = new Button("Open"); ? Button button_Save = new Button("Save");

slide-25
SLIDE 25

? Button button_New = new Button("New"); ? Button button_Open = new Button("Open"); ? Button button_Save = new Button("Save"); ☹ This is ugly.

slide-26
SLIDE 26

Button btnNew = new Button("New"); Button btnOpen = new Button("Open"); Button btnSave = new Button("Save");

slide-27
SLIDE 27

Button btnNew = new Button("New"); Button btnOpen = new Button("Open"); Button btnSave = new Button("Save"); ☺ This is good.

slide-28
SLIDE 28

The Ugly, the Bad, and the Good 丑的,坏的,好的

slide-29
SLIDE 29

☆ Choose nouns for your class names 从名词中为你的类取名

slide-30
SLIDE 30
slide-31
SLIDE 31

class Evaluator { ... } class Solver { ... } class Withdrawal { ... }

slide-32
SLIDE 32

class Evaluator { ... } class Solver { ... } class Withdrawal { ... } ☺ These are good.

slide-33
SLIDE 33

? class Evaluate { ... } ? class Solve { ... } ? class Withdraw { ... }

slide-34
SLIDE 34

? class Evaluate { ... } ? class Solve { ... } ? class Withdraw { ... } ☹ These are bad.

slide-35
SLIDE 35

# The following class/module names are # also good: package CGI::Simple; package XML::Smart; package Class::DBI::Sweet; package Test::Easy;

slide-36
SLIDE 36

☆ Choose active verbs for your method names 从主动动词中为你的方法取名

slide-37
SLIDE 37
slide-38
SLIDE 38

$dbh = DBI.connect('dbi:odbc:qqbase'); $dbh.commit(); $account.update(balance => 100);

slide-39
SLIDE 39

$dbh = DBI.connect('dbi:odbc:qqbase'); $dbh.commit(); $account.update(balance => 100); ☺ These are good.

slide-40
SLIDE 40

? $dbh = DBI.connection('dbi:odbc:qqbase'); ? $dbh.committing(); ? $account.updated(balance => 100);

slide-41
SLIDE 41

? $dbh = DBI.connection('dbi:odbc:qqbase'); ? $dbh.committing(); ? $account.updated(balance => 100); ☹ These are bad.

slide-42
SLIDE 42

☆ Choose nouns or adjectives for your property names 从名词或者形容词中为你的属性取名

slide-43
SLIDE 43

@list.length $persion.name if $dbh.available { ... } die if $set.is_empty;

slide-44
SLIDE 44

@list.length $persion.name if $dbh.available { ... } die if $set.is_empty; ☺ These are good.

slide-45
SLIDE 45

☆ Use namespaces to split your verbose class names. 使用命名空间来分割你冗长的类名。

slide-46
SLIDE 46

? class Makefile_Parser_AST_Element { ? ... ? } ? Makefile_Parser_AST_Element elem();

slide-47
SLIDE 47

? class Makefile_Parser_AST_Element { ? ... ? } ? Makefile_Parser_AST_Element elem(); ☹ This is ugly.

slide-48
SLIDE 48

namespace Makefile::Parser::AST { class Element { ... } } Makefile::Parser::AST::Element elem();

slide-49
SLIDE 49

namespace Makefile::Parser::AST { class Element { ... } } Makefile::Parser::AST::Element elem(); ☺ This is good.

slide-50
SLIDE 50

☆ Use short names for common things. 为常用的东西取短名字。

slide-51
SLIDE 51

Common operations should be "Huffman coded". That is, frequently used operators should be shorter than infrequently used ones.

  • - Larry Wall (Perl 6 Apocalypse 3)

对常见的操作应该进行“哈夫曼编码”。也就是说,频繁 使用的运算符的名称应该比那些不经常使用的要短。

  • - Larry Wall (Perl 6 启示录 3)
slide-52
SLIDE 52
slide-53
SLIDE 53

// The Java way: System.out.println("Computing the sum..."); for (int i = 1; i <= n; i++) { sum += i; System.out.println(i); } System.out.println("Sum: " + sum);

slide-54
SLIDE 54

// The Java way: System.out.println("Computing the sum..."); for (int i = 1; i <= n; i++) { sum += i; System.out.println(i); } System.out.println("Sum: " + sum); ☹ This is ugly.

slide-55
SLIDE 55

# The Perl 6 way: say "Computing the sum..."; for 1..$n -> $i { $sum += $i; say $i; } say "Sum: $sum";

slide-56
SLIDE 56

# The Perl 6 way: say "Computing the sum..."; for 1..$n -> $i { $sum += $i; say $i; } say "Sum: $sum"; ☺ This is good.

slide-57
SLIDE 57

☆ Use short names for locals. 为局部变量取短名字。

slide-58
SLIDE 58

int len = list.length; for (int i = 0; i < len; i++) System.out.println(list[i]);

slide-59
SLIDE 59

int len = list.length; for (int i = 0; i < len; i++) System.out.println(list[i]); ☺ This is good.

slide-60
SLIDE 60

int length = list.length; for (int index = 0; index < length; index++) System.out.println(list[index]);

slide-61
SLIDE 61

int length = list.length; for (int index = 0; index < length; index++) System.out.println(list[index]); ☺ This is ugly.

slide-62
SLIDE 62

☆ Use descriptive names for globals. (long names are okay.) 为全局结构取描述性的名字。 (长名字也可以。)

slide-63
SLIDE 63

? # Globals in smartlinks.pl: ? my ($count, $broken_count);

slide-64
SLIDE 64

? # Globals in smartlinks.pl: ? my ($count, $broken_count); ☹ This is bad.

slide-65
SLIDE 65

# Globals in smartlinks.pl: my ($link_count, $broken_link_count);

slide-66
SLIDE 66

# Globals in smartlinks.pl: my ($link_count, $broken_link_count); ☺ This is better.

slide-67
SLIDE 67

☆ Don't repeat yourself. (The DRY principle) 不要重复你自己 (DRY 原则)

slide-68
SLIDE 68

? // The Java way: ? double value = Double.parseDouble("3.14");

slide-69
SLIDE 69

? // The Java way: ? double value = Double.parseDouble("3.14"); ☹ This is ugly.

slide-70
SLIDE 70

// The C# way: double value = double.Parse("3.14");

slide-71
SLIDE 71

// The C# way: double value = double.Parse("3.14"); ☹ Just a bit better.

slide-72
SLIDE 72

// The Perl 6 way: my Double $value = "3.14";

slide-73
SLIDE 73

// The Perl 6 way: my Double $value = "3.14"; ☺ This is good!

slide-74
SLIDE 74

// The C/C++ way: double value = atof("3.14");

slide-75
SLIDE 75

// The C/C++ way: double value = atof("3.14"); ☺ This is good too, though a bit fuzzy.

slide-76
SLIDE 76

? class UserQueue { ? int noOfItemsInQ; ? int frontOfTheQueue; ? int queueCapacity; ? public int noOfUsersInQueue() { ... } ? }

slide-77
SLIDE 77

? class UserQueue { ? int noOfItemsInQ; ? int frontOfTheQueue; ? int queueCapacity; ? public int noOfUsersInQueue() { ... } ? } ☹ This is ugly.

slide-78
SLIDE 78

class UserQueue { int nitems; int front; int capacity; public int nusers() { ... } }

slide-79
SLIDE 79

class UserQueue { int nitems; int front; int capacity; public int nusers() { ... } } ☺ This is good!

slide-80
SLIDE 80

☆ Tell me more! (Don't be handwaving.) 多告诉我一些! (不要遮遮掩掩。)

slide-81
SLIDE 81

? if (check_even(num)) { ... }

slide-82
SLIDE 82

? if (check_even(num)) { ... } ☹ Check if it is an even number?

slide-83
SLIDE 83

? if (check_even(num)) { ... } ☹ Check if it is an even number? ☹ Check if it is not an even number?

slide-84
SLIDE 84

if (is_even(num)) { ... }

slide-85
SLIDE 85

if (is_even(num)) { ... } ☺ Now it is good!

slide-86
SLIDE 86

☆ Improper abbreviations can be very confusing. 不恰当的缩写名可能会让人 非常迷糊。

slide-87
SLIDE 87

<Sal> what is "JQL"? <clkao> Jabberwocky Query Language? <TimToady> Just Quack Loudly? <audreyt> Junctional Quantum Library? <仲伟祥> 什么是“JQL”? <高嘉良> 废话查询语言? <Larry Wall> 不过是高声吹嘘? <唐凤> 联结性量子库?

slide-88
SLIDE 88

☆ Widely-used abbreviations are recommended. 提倡选择 广泛使用的缩写。

slide-89
SLIDE 89

var ← variable val ← value init ← initialize / initialization elem ← element id ← identifier len ← length eval ← evaluate / evaluation func ← function sub ← subroutine AST ← Abstract Syntax Tree ...and many more

slide-90
SLIDE 90

☆ Chinese Pinyin abbreviations can be extremely hateful. 汉语拼音缩写会非常令人讨厌。

slide-91
SLIDE 91

http://.../xs_main.aspx?xh=3030602110# http://.../xscj_gc.aspx?xh=3030602110&xm=章亦春 http://.../xscxbm.aspx?xh=3030602110&xm=章亦春

slide-92
SLIDE 92

http://.../xs_main.aspx?xh=3030602110# http://.../xscj_gc.aspx?xh=3030602110&xm=章亦春 http://.../xscxbm.aspx?xh=3030602110&xm=章亦春 xs == 学生 (student) ?

slide-93
SLIDE 93

http://.../xs_main.aspx?xh=3030602110# http://.../xscj_gc.aspx?xh=3030602110&xm=章亦春 http://.../xscxbm.aspx?xh=3030602110&xm=章亦春 xs == 学生 (student) ? xh == 学号 (student id) ?

slide-94
SLIDE 94

http://.../xs_main.aspx?xh=3030602110# http://.../xscj_gc.aspx?xh=3030602110&xm=章亦春 http://.../xscxbm.aspx?xh=3030602110&xm=章亦春 xs == 学生 (student) ? xh == 学号 (student id) ? xm == 姓名 (name) ?

slide-95
SLIDE 95

http://.../xs_main.aspx?xh=3030602110# http://.../xscj_gc.aspx?xh=3030602110&xm=章亦春 http://.../xscxbm.aspx?xh=3030602110&xm=章亦春 xs == 学生 (student) ? xh == 学号 (student id) ? xm == 姓名 (name) ? xscj == 学生成绩 (student grades) ?

slide-96
SLIDE 96

http://.../xs_main.aspx?xh=3030602110# http://.../xscj_gc.aspx?xh=3030602110&xm=章亦春 http://.../xscxbm.aspx?xh=3030602110&xm=章亦春 xs == 学生 (student) ? xh == 学号 (student id) ? xm == 姓名 (name) ? xscj == 学生成绩 (student grades) ? xscxbm == 学生重修报名 (XXX) ?

slide-97
SLIDE 97

http://.../xs_main.aspx?xh=3030602110# http://.../xscj_gc.aspx?xh=3030602110&xm=章亦春 http://.../xscxbm.aspx?xh=3030602110&xm=章亦春 xs == 学生 (student) ? xh == 学号 (student id) ? xm == 姓名 (name) ? xscj == 学生成绩 (student grades) ? xscxbm == 学生重修报名 (XXX) ? gc == ???

slide-98
SLIDE 98

I'm a Chinese, but I still find it hard to understand. 我是中国人, 但我发现理解它们仍然很困难。

slide-99
SLIDE 99

☆ Avoid potential ambiguity and confusion. 避免可能的歧义和混淆。

slide-100
SLIDE 100

To get the number of elements in an array, use the .elems method. You can also ask for the total string length of an array's elements, in bytes, codepoints or graphemes, using these methods .bytes, .codes or .graphs respectively

  • n the array. The same methods apply to strings

as well. There is no .length method for either arrays or strings, because length does not specify a unit.

  • - Larry Wall (The Perl 6 Synopsis 2)
slide-101
SLIDE 101

想得到数组中的元素数目,可以使用 .elems 方法。 你也可以利用 .bytes、 .codes 或者 .graphs 分别 以字节、编码点、或者字形为单位来查询数组元素 的总长度。字符串也拥有这些方法。 对数组和字符串而言,都不再有 .length 方法了, 因为 length 并没有指明长度单位。

  • - Larry Wall (Perl 6 纲要 2)
slide-102
SLIDE 102

☆ DWIM? Do what you mean! 言行一致!

slide-103
SLIDE 103

? class Array { ? // print out the contents: ? public void init() { ? for (int i = 0; i < this.length; i++) ? System.out.println(this.get(i)); ? } ? }

slide-104
SLIDE 104

? class Array { ? // print out the contents: ? public void init() { ? for (int i = 0; i < this.length; i++) ? System.out.println(this.get(i)); ? } ? } init? emit?

slide-105
SLIDE 105

Get the slides today! ♨ http://agentzh.org/misc/slides/naming.pdf http://agentzh.org/misc/slides/naming_recap.pdf

slide-106
SLIDE 106

Contact me on the web! ♨ agentzh@gmail.com

slide-107
SLIDE 107

These slides are powered by Sporx and Takahashi++

slide-108
SLIDE 108

Thank you! ☺

slide-109
SLIDE 109