C代码发音的最佳实践

时间:2021-11-14 20:07:46

As my formal education in programming pre-dates C, I learnt C from K&R and other texts.

我在编程之前接受的正规教育是C,我从K&R等教材中学习了C。

Are there helpful conventions for vocalising C code when reading and writing it?

在阅读和编写C代码时是否有一些有用的规范?

For example, in

例如,在

 d = emalloc(sizeof(*d));
 d->d_name = estrdup(name);

is '=' best read as 'is set to' or 'equals' or something else?

'=' best read as 'is set to' or 'equals' or something else?

Would '==' then be read as 'equals' or 'is' or 'has the same value as'?

那么'= '将被读为'equals'或'is'或'has the same value as'?

Similarly with "*d", "d->d_name", and then "&d", " & ", " && ", and so on.

类似与“* d”,“d - > d_name”,然后“研发”、“&”、“& &”,等等。

I know what all these mean and what they do, the question relates to what you say (out loud or internally) when reading the symbol, especially if you think this helps understanding and minimises confusion.

我知道这些都是什么意思,他们做什么,这个问题和你读这个符号时(大声或内心)说什么有关,尤其是如果你认为这有助于理解和减少困惑。

11 个解决方案

#1


3  

I hadn't really thought about it before, but I now realise that I don't intermally read programming language statements like those above as English words. I just look at them and know what they do.

我以前并没有想过这个问题,但是现在我意识到我并没有把上面的编程语言语句当做英语单词来读。我只是看着他们,知道他们在做什么。

And interestingly, this works even when I don't really know what a function does. For example, I've never come across emalloc() before, but my brain still tells me what the statement (probably) does.

有趣的是,即使我不知道函数的作用。例如,我以前从来没有遇到过emalloc(),但是我的大脑仍然告诉我这个语句(可能)的作用。

If for some reason (war? national security?) I had tb read something like:

如果出于某种原因(战争?国家安全?)我让结核病患者读到以下内容:

 d->d_name = estrdup(name);

over the phone, I'd say:

在电话里,我会说:

"thing pointed to by d's d_name member gets assigned the result of calling estrdup with name as the parameter"

" d的d_name成员所指向的东西被指定为调用estrdup的结果,并以名称作为参数"

But that is emphactically not what is going through my head when I read the code.

但这并不是我读代码时脑子里想的东西。

#2


10  

ASCII Pronunciation Rules for Programmers has a large list of, well, the ASCII pronunciation rules... not so much the best way to pronounce a pointer, necessarily, but at least how to verbalize symbols.

对于程序员来说,ASCII发音规则有一大堆,嗯,ASCII发音规则……不一定是发音指针的最佳方式,但至少是如何表达符号。

#3


9  

I pronounce this as:

这个是我发音:

d = emalloc(sizeof(*d));

dee equal ee malloc size of star dee

d级的大小,大小相等。

d->d_name = estrdup(name);

dee arrow dee underscore name equal ee stir dupe name"

dee箭头下划线名称相等ee搅拌dupe名称"

But I wouldn't call any of that best practice. I don't pronounce the parenthesis except when it would be ambiguous, such as with operator precedence. I do pronounce almost all other punctiation. I actually read == as "equal equal"

但我不认为这是最好的做法。我不发括号,除非它是模棱两可的,比如运算符优先级。我确实发过几乎所有其他的穿刺音。== == == == == == == == == == == == == == == ==

#4


4  

I am not an English native, but those are the terms I see being used the most:

我不是英语母语人士,但我觉得这些词用得最多:

'=' - assign to
'==' - equals
d->d_name member of a struct
&d - address of d
& - bitwise and
&& - logical and (or just "and" for short)
*d - dereference d, or in case of *d = x "assign x to what is being pointed by d"

As for your example:

至于你的例子:

d = malloc(sizeof(*a));

Allocate (malloc) an amount of memory equal to the size of what is pointed to by a and assign the result to d.

分配(malloc)与a所指向的大小相等的内存量,并将结果分配给d。

d->d_name = estrdup(name);

Assign to the member d_name of d the value returned by the call to estrdup with name as the parameter.

将调用estrdup返回的值(名称为参数)赋给d的成员d_name。

#5


3  

(Speaking slowly and clearly.) I am going to read some text to you, one character at a time. I will read it slowly and clearly, pausing slightly after each character. I'm going to ask you to read it back to me when I'm finished, so listen carefully, but just keep going if you make a mistake. All of the letters are lower case and it contains punctuation throughout. Are you ready? (Wait for confirmation.)

(说得很慢很清晰)。我要给你们读一些文字,一次一个字。我将慢慢地、清晰地读它,在每个字符后稍作停顿。我要让你们在我写完后把它读给我听,所以请仔细听,如果你犯了错误,请继续读下去。所有的字母都是小写字母,并且自始至终都有标点符号。你准备好了吗?(等待确认。)

(Adjust speed between slow and extremely slow based on audio feedback.) d space equals space e m a l l o c left-parenthesis s i z e o f left-parenthesis asterisk d right-parenthesis right-parenthesis semi-colon new-line d dash greater-than d underscore n a m e space equals space e s t r d u p left-parenthesis n a m e right-parenthesis semi-colon

之间(调整速度缓慢,极其缓慢的基于音频反馈。)d =空间e m z l l o c左括号s i e o f左括号星号右括号右括号分号换行d '大于d强调n m e =空间e s t r d u p左括号n m e右括号分号

Ok, please read it back to me slowly and one character at a time. (Confirm that they received the transmission correctly. Retransmit if necessary.)

好的,请慢慢读给我听,每次一个字。(确认他们正确地接收了传输。必要时重新发送。)

#6


2  

I normally use the word/group of words in which are already used for the C syntax elements if i need to pronounce/read aloud the C code-

如果我需要大声读出C代码,我通常使用C语法元素中已经使用的单词/组

e.g.

如。

*d = value at memory address in d

*d = d中内存地址的值

d->d_name = value at memory address pointed at some offset from base address in d (this value could be a integral value or another address value!)

d->d_name =内存地址的值,指向与d中的基地址的某个偏移量(该值可以是一个整数值,也可以是另一个地址值!)

&d = memory address of value d

&d = d值的内存地址

x=y = value of x updated and changed with value in y.

x=y = x值随y值的变化而变化。

x == y =compare value of x with value of y

x = y = x值与y值的比较

When i was doing this i found that 'pronouncing' following C language constructs would be tricky/interesting:

当我这样做的时候,我发现C语言结构后面的“发音”会很棘手/有趣:

union
{
  int a;
float b;
}

function pointers,

函数指针,

typecasts,

类型转换,

-AD.

广告。

#7


2  

IMO, here is the best way to read the following (insofar as PHP is concerned):

在我看来,以下是最好的阅读方式(就PHP而言):

= "is (now) set to"

= "is (now) set to"

== "is (now) equal to"

== "is (now) = "

=== "is (now) identical to"

=== "is (now) same to"

  • Calling = "is set to" helps to avoid using it incorrectly.
  • 调用= "is set to"有助于避免使用不正确。
  • The word "now" is optional but reminds programming novices (like myself) of the dynamic nature of values.
  • “现在”这个词是可选的,但它提醒编程新手(像我一样)价值观的动态性。

#8


1  

Hmm, never thought about this directly before.

嗯,以前从没想过这个。

For words and identifiers, I just pronounce them as they are written.

对于单词和标识符,我只是按它们的书写方式发音。

For symbols, I don't think there is a hard and fast rule. If the operator pronounced as it's written makes sense then I do that. For example, == I would pronounce as equals equals. There's no ambiguity there and for most programmers it flows.

对于符号,我认为没有硬性规定。如果运算符读起来是有意义的,那么我就这么做。例如,== I的发音为等于。这里没有歧义,对于大多数程序员来说,它是流动的。

Operators like -> though I would just say member access. Saying "dash greater than" is correct but I'm guessing I'd get a few funny stares.

操作符像->,我只说成员访问。说“dash greater than”是对的,但我猜我会得到一些有趣的眼神。

#9


1  

I'm not a native english speaker and neither do I read out code, but here's how I'd do it:

我的母语不是英语,也不读代码,但我的做法是:

= -> equals
== -> is equal to
& -> bitwise and
&& -> and
&d -> address of

= -> == -> = -> / >和&d ->地址。

I would read *d, d->d_name, d.d_name as "d"/"d d_name", as it should be clear from the context.

我会读*d, d->d_name, d。d_name为“d”/“d d_name”,从上下文应该很清楚。

#10


1  

 d = emalloc(sizeof(*d));

dee equals ee malloc sizeof star dee

dee等于ee malloc sizeof star dee

 d->d_name = estrdup(name);

dee ref dee name equals ee stir dup name

dee ref dee name = ee stir dup name

 if ( d -> d_name == NULL )

if dee ref dee name is null

如果dee id名称为null

or

if dee ref dee name is equal to null

如果dee dee dee name等于null

#11


1  

I usually pronounce both "=" and "==" as "equals".

我通常把"="和"="都读成"等于"。

There's little ambiguity, since I very rarely use "=" in a context where its result is used, or "==" in a context where its result is not used. If the former came up, perhaps in code I was reading that I didn't write, I might say "if a single-equals b", or "for i equals j semi-colon i single-equals j semi-colon plus-plus j. I hope that's not a bug".

这里几乎没有歧义,因为我很少在使用结果的上下文中使用“=”,或者在不使用结果的上下文中使用“==”。如果出现前者,也许在我读到的代码中我没有写,我可能会说“If a single-equals b”,或者“for I = j分号I single- = j分号+ j,我希望这不是一个错误”。

In any case, it's pretty rare for me to speak code out loud in a context where ambiguity really matters. So how I normally say it is different from what I'd do in a situation where I needed to be really pedantic about conveying text accurately.

在任何情况下,我都很少在模棱两可的环境下大声说出代码。所以我通常会说,这和我在这种情况下所做的事情是不同的,在这种情况下,我需要对如何准确地传达文本进行真正的学究。

If I was dictating a lot of code (pair programming, for example) then I'd normally expect to read it as it was typed, so any errors can be spotted. If my ambiguity was frequently confusing the typist and wasting time, then I'd expect just to agree a convention on the spot. Far easier than forming a consensus among all C/C++/Java/C# programmers in the world, ever.

如果我在口述大量的代码(例如,结对编程),那么我通常希望在输入时读取它,这样就可以发现任何错误。如果我的模棱两可经常让打字员感到困惑和浪费时间,那么我希望我能当场同意一个约定。这比世界上所有的C/ c++ /Java/ c#程序员达成一致要容易得多。

#1


3  

I hadn't really thought about it before, but I now realise that I don't intermally read programming language statements like those above as English words. I just look at them and know what they do.

我以前并没有想过这个问题,但是现在我意识到我并没有把上面的编程语言语句当做英语单词来读。我只是看着他们,知道他们在做什么。

And interestingly, this works even when I don't really know what a function does. For example, I've never come across emalloc() before, but my brain still tells me what the statement (probably) does.

有趣的是,即使我不知道函数的作用。例如,我以前从来没有遇到过emalloc(),但是我的大脑仍然告诉我这个语句(可能)的作用。

If for some reason (war? national security?) I had tb read something like:

如果出于某种原因(战争?国家安全?)我让结核病患者读到以下内容:

 d->d_name = estrdup(name);

over the phone, I'd say:

在电话里,我会说:

"thing pointed to by d's d_name member gets assigned the result of calling estrdup with name as the parameter"

" d的d_name成员所指向的东西被指定为调用estrdup的结果,并以名称作为参数"

But that is emphactically not what is going through my head when I read the code.

但这并不是我读代码时脑子里想的东西。

#2


10  

ASCII Pronunciation Rules for Programmers has a large list of, well, the ASCII pronunciation rules... not so much the best way to pronounce a pointer, necessarily, but at least how to verbalize symbols.

对于程序员来说,ASCII发音规则有一大堆,嗯,ASCII发音规则……不一定是发音指针的最佳方式,但至少是如何表达符号。

#3


9  

I pronounce this as:

这个是我发音:

d = emalloc(sizeof(*d));

dee equal ee malloc size of star dee

d级的大小,大小相等。

d->d_name = estrdup(name);

dee arrow dee underscore name equal ee stir dupe name"

dee箭头下划线名称相等ee搅拌dupe名称"

But I wouldn't call any of that best practice. I don't pronounce the parenthesis except when it would be ambiguous, such as with operator precedence. I do pronounce almost all other punctiation. I actually read == as "equal equal"

但我不认为这是最好的做法。我不发括号,除非它是模棱两可的,比如运算符优先级。我确实发过几乎所有其他的穿刺音。== == == == == == == == == == == == == == == ==

#4


4  

I am not an English native, but those are the terms I see being used the most:

我不是英语母语人士,但我觉得这些词用得最多:

'=' - assign to
'==' - equals
d->d_name member of a struct
&d - address of d
& - bitwise and
&& - logical and (or just "and" for short)
*d - dereference d, or in case of *d = x "assign x to what is being pointed by d"

As for your example:

至于你的例子:

d = malloc(sizeof(*a));

Allocate (malloc) an amount of memory equal to the size of what is pointed to by a and assign the result to d.

分配(malloc)与a所指向的大小相等的内存量,并将结果分配给d。

d->d_name = estrdup(name);

Assign to the member d_name of d the value returned by the call to estrdup with name as the parameter.

将调用estrdup返回的值(名称为参数)赋给d的成员d_name。

#5


3  

(Speaking slowly and clearly.) I am going to read some text to you, one character at a time. I will read it slowly and clearly, pausing slightly after each character. I'm going to ask you to read it back to me when I'm finished, so listen carefully, but just keep going if you make a mistake. All of the letters are lower case and it contains punctuation throughout. Are you ready? (Wait for confirmation.)

(说得很慢很清晰)。我要给你们读一些文字,一次一个字。我将慢慢地、清晰地读它,在每个字符后稍作停顿。我要让你们在我写完后把它读给我听,所以请仔细听,如果你犯了错误,请继续读下去。所有的字母都是小写字母,并且自始至终都有标点符号。你准备好了吗?(等待确认。)

(Adjust speed between slow and extremely slow based on audio feedback.) d space equals space e m a l l o c left-parenthesis s i z e o f left-parenthesis asterisk d right-parenthesis right-parenthesis semi-colon new-line d dash greater-than d underscore n a m e space equals space e s t r d u p left-parenthesis n a m e right-parenthesis semi-colon

之间(调整速度缓慢,极其缓慢的基于音频反馈。)d =空间e m z l l o c左括号s i e o f左括号星号右括号右括号分号换行d '大于d强调n m e =空间e s t r d u p左括号n m e右括号分号

Ok, please read it back to me slowly and one character at a time. (Confirm that they received the transmission correctly. Retransmit if necessary.)

好的,请慢慢读给我听,每次一个字。(确认他们正确地接收了传输。必要时重新发送。)

#6


2  

I normally use the word/group of words in which are already used for the C syntax elements if i need to pronounce/read aloud the C code-

如果我需要大声读出C代码,我通常使用C语法元素中已经使用的单词/组

e.g.

如。

*d = value at memory address in d

*d = d中内存地址的值

d->d_name = value at memory address pointed at some offset from base address in d (this value could be a integral value or another address value!)

d->d_name =内存地址的值,指向与d中的基地址的某个偏移量(该值可以是一个整数值,也可以是另一个地址值!)

&d = memory address of value d

&d = d值的内存地址

x=y = value of x updated and changed with value in y.

x=y = x值随y值的变化而变化。

x == y =compare value of x with value of y

x = y = x值与y值的比较

When i was doing this i found that 'pronouncing' following C language constructs would be tricky/interesting:

当我这样做的时候,我发现C语言结构后面的“发音”会很棘手/有趣:

union
{
  int a;
float b;
}

function pointers,

函数指针,

typecasts,

类型转换,

-AD.

广告。

#7


2  

IMO, here is the best way to read the following (insofar as PHP is concerned):

在我看来,以下是最好的阅读方式(就PHP而言):

= "is (now) set to"

= "is (now) set to"

== "is (now) equal to"

== "is (now) = "

=== "is (now) identical to"

=== "is (now) same to"

  • Calling = "is set to" helps to avoid using it incorrectly.
  • 调用= "is set to"有助于避免使用不正确。
  • The word "now" is optional but reminds programming novices (like myself) of the dynamic nature of values.
  • “现在”这个词是可选的,但它提醒编程新手(像我一样)价值观的动态性。

#8


1  

Hmm, never thought about this directly before.

嗯,以前从没想过这个。

For words and identifiers, I just pronounce them as they are written.

对于单词和标识符,我只是按它们的书写方式发音。

For symbols, I don't think there is a hard and fast rule. If the operator pronounced as it's written makes sense then I do that. For example, == I would pronounce as equals equals. There's no ambiguity there and for most programmers it flows.

对于符号,我认为没有硬性规定。如果运算符读起来是有意义的,那么我就这么做。例如,== I的发音为等于。这里没有歧义,对于大多数程序员来说,它是流动的。

Operators like -> though I would just say member access. Saying "dash greater than" is correct but I'm guessing I'd get a few funny stares.

操作符像->,我只说成员访问。说“dash greater than”是对的,但我猜我会得到一些有趣的眼神。

#9


1  

I'm not a native english speaker and neither do I read out code, but here's how I'd do it:

我的母语不是英语,也不读代码,但我的做法是:

= -> equals
== -> is equal to
& -> bitwise and
&& -> and
&d -> address of

= -> == -> = -> / >和&d ->地址。

I would read *d, d->d_name, d.d_name as "d"/"d d_name", as it should be clear from the context.

我会读*d, d->d_name, d。d_name为“d”/“d d_name”,从上下文应该很清楚。

#10


1  

 d = emalloc(sizeof(*d));

dee equals ee malloc sizeof star dee

dee等于ee malloc sizeof star dee

 d->d_name = estrdup(name);

dee ref dee name equals ee stir dup name

dee ref dee name = ee stir dup name

 if ( d -> d_name == NULL )

if dee ref dee name is null

如果dee id名称为null

or

if dee ref dee name is equal to null

如果dee dee dee name等于null

#11


1  

I usually pronounce both "=" and "==" as "equals".

我通常把"="和"="都读成"等于"。

There's little ambiguity, since I very rarely use "=" in a context where its result is used, or "==" in a context where its result is not used. If the former came up, perhaps in code I was reading that I didn't write, I might say "if a single-equals b", or "for i equals j semi-colon i single-equals j semi-colon plus-plus j. I hope that's not a bug".

这里几乎没有歧义,因为我很少在使用结果的上下文中使用“=”,或者在不使用结果的上下文中使用“==”。如果出现前者,也许在我读到的代码中我没有写,我可能会说“If a single-equals b”,或者“for I = j分号I single- = j分号+ j,我希望这不是一个错误”。

In any case, it's pretty rare for me to speak code out loud in a context where ambiguity really matters. So how I normally say it is different from what I'd do in a situation where I needed to be really pedantic about conveying text accurately.

在任何情况下,我都很少在模棱两可的环境下大声说出代码。所以我通常会说,这和我在这种情况下所做的事情是不同的,在这种情况下,我需要对如何准确地传达文本进行真正的学究。

If I was dictating a lot of code (pair programming, for example) then I'd normally expect to read it as it was typed, so any errors can be spotted. If my ambiguity was frequently confusing the typist and wasting time, then I'd expect just to agree a convention on the spot. Far easier than forming a consensus among all C/C++/Java/C# programmers in the world, ever.

如果我在口述大量的代码(例如,结对编程),那么我通常希望在输入时读取它,这样就可以发现任何错误。如果我的模棱两可经常让打字员感到困惑和浪费时间,那么我希望我能当场同意一个约定。这比世界上所有的C/ c++ /Java/ c#程序员达成一致要容易得多。