标签 English 下的文章

那些代码中的“中国式”命名

10月中旬,有人在Quora网站上发起一个调查:“程序员职业生涯中最难的事是什么?”,调查结果让人实感意外。世界范围内的程序员同胞们普遍认为: “命名是让大家感觉最困难的事情”。对于主流的欧美程序员尚且如此,对于英文非母语的中国程序员来说,苦逼程度可想而知了:(。

虽说中国程序员大多也都学了10年以上的英语了,但能“地道”的表达和书写甚至是选词的程序员们比例却不高。而在编写程序的过程中,给变量、常量以及函数命名即意味着我们要选择恰当地道的词汇或缩写。

事实证明,任何事情经过中国人民的加工,就会呈现出意想不到的效果,于是我们有了Chinglish,有了“中国式”的命名。就此,我特地花了些时间翻阅了下面几个项目组的源码,总结出了一些“中国式”命名的门道,这里也说道说道^_^。

一、万能的动词

通过阅读代码中的一些命名,我发现了代码中经常出现的一些动词堪称“万能”,这些动词包括:handle、do、process、check、collect等。无论对象是什么,你只要像如下这么命名,中国程序员基本都能看懂或猜个八九不离十:

    process_xx  处理xx
    do_yy       做yy
    handle_zz   处理zz
    check_xx    检查/校验xx
    collect_xx 收集xx

二、标志/类型代言人

在项目源码文件中,你会变量声明或结果体声明中看到太多的xx_flag或xx_type。xx_flag被大家“公认为”各种标志的代言人了;xx_type也好不到哪去。

    route_flag
    protocol_flag
    msg_flag
    session_flag

    msg_type
    check_type
    command_type
    session_type

三、字面意直译式

比如我们要声明两个变量,一个的意思是“是否打开消息过滤”,另外一个“是否关闭消息过滤”。对于中国人来说,打开/关闭最直接对应的就是open/close,于是就有了这样的变量命名:

    is_message_filter_open
    is_message_filter_close

其实一般认为更地道的方式应该是:

    is_message_filter_on
    is_message_filter_off

再举个例子,我们的系统要处理一种中文名为“长短信”的对象,于是直译过来就是:long_sms。但其真正的英文术语应该是Concatenated SMS

四、中文拼音式

在国内做行业解决方案,不可避免会遇到各种领域词汇,比如我们处理的一种业务称为农信通。估计是开发人员没能找到其对应的英文翻译,于是乎用上了我们的看家本领 – 拼音。

    nxt_url  农信通地址

这样的例子是否似曾相识呢!

五、单词“选秀”

采用这种方式的程序员应该是更具“责任心”的,至少他/她知道去查查英文词典^_^。

于是乎有了dispense_message、cache_overstock等变量命名。显然他是在字典的单词选秀中随意找到一个“顺眼”的、意思还算相近的单词就挑了出来。显然dispatch 、overflow要比dispense、overstock更地道和准确。

六、尾声

以上的“实例”确是从我们项目的代码中“挖掘”出来的,希望只是个案。面对这样的命名bad smell,我们是否有解决方法呢?没有捷径,语言的感觉是要一点点的去找的,个人觉得一个最行之有效的方法就是去看那些英语地道的程序员编写的代码,留 意他们是如何命名的。比如看一些著名欧美程序员的开源项目代码。另外对于行业特定领域,尽量用经过认可的英文翻译结果来命名。

不可否认,英语是编程领域的主流语言,包括中文在内的以其他语言为编码语言的编程语言十分罕见。对于欧美程序员来说,这是天然的优势;而对于其他非欧美国家的程序员,尤其是中国程序员来说,在走向“地道”的路上还要付出很多才行。

'Manna' – An extremely attractive fiction

Our British English teacher Alex recommended a book called ‘Manna’ to us for its simple grammar and vocabulary. After reading it, we all agreed on that it was an extremely attractive fiction.

The author of ‘Manna’ is Marshall Brain, who is a writer, a well-known national speaker and a consultant in U.S. In his fiction, he tells us his point of view on robots’ coming out in near future. It seems to be a science fiction, maybe a political fiction, who knows, because almost any book could be associated with politics.

The following is the link of this fiction:
Manna by Marshall Brain

If you have your own options after reading the book, I am looking forward to talking with you about that.

Harry Potter and the Sorcerer's Stone读书笔记Part2

CHAPTER TWO – THE VANISHING GLASS
1.The photographs showed a large blond boy riding his first bicycle, on a carousel at the fair, playing a computer game
with his father, being hugged and kissed by his mother. The room held no sign at all that another boy lived in the house, too.

Carousel -喧闹的酒会
Fair – 展览会
The room held no sign at all that another boy lived in the house. 没有任何迹象显示有另一个男孩儿也住在这个屋子里。

2."Are you up yet?" she demanded.
"Nearly," said Harry.
"Well, get a move on, I want you to look after the bacon. And don't you dare let it burn, I want everything perfect on
Duddy's birthday."
Harry groaned.
"What did you say?" his aunt snapped through the door.
"Nothing, nothing…"

从这段对话来看Harry的境况并不是很好哟。
Do you dare do something?
Groan -呻吟着说, 叹息

3.Harry was used to spiders, because the cupboard under the stairs was full of them, and that was where he slept.
Be used to … 常用的一种表达方式,习惯于…
Used (adj) -习惯的
The astronauts soon got used to the condition of weightlessness.
I am used to the weather here.
You must get used to getting up early.

4、The table was almost hidden beneath all Dudley's birthday presents.
表达如此到位,佩服佩服。

5.Exactly why Dudley wanted a racing bike was a mystery to Harry, as Dudley was very fat and hated exercise — unless of course it involved punching somebody. Dudley's favorite punching bag was Harry, but he couldn't often catch him.

Punch sb – 用拳猛击猛击

6.Harry had a thin face, knobbly knees, black hair, and bright green eyes. He wore round glasses held together with a lot of Scotch tape because of all the times Dudley had punched him on the nose. The only thing Harry liked about his own appearance was a very thin scar on his forehead that was shaped like a bolt of lightning.
这是一段关于Harry长相的描写,通过这段描写我们可以在脑中为Harry画出一幅肖像。

Scar – 伤疤

7.It was a very sunny Saturday and the zoo was crowded with families.
你写日记时可以常用的一句表达方式。

8.Everybody knew that Dudley's gang hated that odd Harry Potter in his baggy old clothes and broken glasses, and nobody liked to disagree with Dudley's gang.

Gang –(一)伙, (一)群, 帮派
a gang of criminals
a gang of young men

CHAPTER THREE – THE LETTERS FROM NO ONE
1.The escape of the Brazilian boa constrictor earned Harry his longest-ever punishment.
Boa Constrictor – 大蟒,两个单词都有蟒蛇的意思,但是就把他们连在一起表示蟒蛇的意思。
2.This was why Harry spent as much time as possible out of the house, wandering around and thinking about the end of the
holidays, where he could see a tiny ray of hope.
That is why + 正常语序;
A tiny ray of hope 一丝希望

3.As he looked at Dudley in his new knickerbockers, Uncle Vernon said gruffly that it was the proudest moment of his life.

Aunt Petunia burst into tears and said she couldn't believe it was her Ickle Dudleykins, he looked so handsome and grown-up.
As – At the same time that; while 在本小说中有很多as引导的时间状语从句。
Gruffly – 粗暴地
The proudest moment – 最骄傲的时刻
Burst into tears – 可以理解为喜极而泣

4.They heard the click of the mail slot and flop of letters on the doormat.
与信函有关的几个声音,
The click of mail slot 将信放入信箱的声音
Flop of letters 信件落在地上的声音

Doormat – (放于门前的)擦鞋垫

5.Harry was on the point of unfolding his letter, which was written on the same heavy parchment as the envelope, when it was jerked sharply out of his hand by Uncle Vernon.

Parchment – 羊皮纸,给小说增加了神秘感

Jerk – 猛拉
She jerked the rope but it wouldn't move.
She jerked out the knife that was stuck in the wood.

6.His face went from red to green faster than a set of traffic lights. And it didn't stop there. Within seconds it was the grayish white of old porridge.
关于人脸色变化的描写

Grayish – 浅灰色的
Porridge – 麦片粥

7.Five minutes to go. Harry heard something creak outside. He hoped the roof wasn't going to fall in, although he might be warmer if it did.
Four minutes to go. Maybe the house in Privet Drive would be so full of letters when they got back that he'd be able to
steal one somehow.

Five minutes to go;
Four minutes to go;

倒计时

Creak 吱吱作响

附录:
Harry Potter and the Sorcerer's Stone读书笔记Part1




这里是Tony Bai的个人Blog,欢迎访问、订阅和留言!订阅Feed请点击上面图片

如果您觉得这里的文章对您有帮助,请扫描上方二维码进行捐赠,加油后的Tony Bai将会为您呈现更多精彩的文章,谢谢!

如果您希望通过比特币或以太币捐赠,可以扫描下方二维码:

比特币:


以太币:


如果您喜欢通过微信App浏览本站内容,可以扫描下方二维码,订阅本站官方微信订阅号“iamtonybai”;点击二维码,可直达本人官方微博主页^_^:



本站Powered by Digital Ocean VPS。

选择Digital Ocean VPS主机,即可获得10美元现金充值,可免费使用两个月哟!

著名主机提供商Linode 10$优惠码:linode10,在这里注册即可免费获得。

阿里云推荐码:1WFZ0V立享9折!

View Tony Bai's profile on LinkedIn


文章

评论

  • 正在加载...

分类

标签

归档











更多