This was something originally discussed during a presentation given by Charles Brian Quinn of the Big Nerd Ranch at acts_as_conference. He was discussing what he had learned from instructing a Ruby on Rails Bootcamp to many people both new to programming and new to Rails.
这是最初在大型书呆子牧场的查尔斯布莱恩奎因在acts_as_conference上发表的演讲中讨论的内容。他正在讨论他从指导Ruby on Rails Bootcamp中学到的东西给许多刚接触编程和Rails新手的人。
One particular slide that stood out was along the lines of never using foo and bar as examples when trying to teach someone to program. His reasoning was very simple.
突出的一个特别的幻灯片就是在尝试教别人编程时,从不使用foo和bar作为例子。他的推理非常简单。
Which is easier to understand?
哪个更容易理解?
baz = foo + bar
or
answer = first_number + second_number
It's happened many times myself when explaining something and I immediately jump to the go to foo bar placeholders but then realize my mistake and make the example make a lot more sense by using a real world scenario.
在解释某些内容时,我自己发生了很多次,然后我立刻跳到foo bar占位符,然后意识到我的错误并通过使用真实世界场景让这个例子变得更有意义。
This is especially applicable when trying to teach someone who has had no programming exposure and you end up needing explain foo and bar before explaining what you're actually trying to teach.
这一点尤其适用于尝试教导没有编程曝光的人,并且在解释您实际上要教授的内容之前最终需要解释foo和bar。
However, using foo and bar for experienced programmers seems OK, though I personally think, along with Charles, that it's something that needs to change.
然而,对有经验的程序员使用foo和bar似乎没问题,尽管我个人认为,与Charles一起,这是需要改变的东西。
A quick SO search for "foo" returns over 20 pages of results with foo being used in more ways that I can comprehend. And in some cases where I'm reading a question on a particular language and I'm doing so to help understand that language better. If applicable variable names are used instead of foo and bar, it makes it much easier to understand and interpret the problem. So for seasoned developers, the construct seems a bit flawed as well.
快速SO搜索“foo”会返回超过20页的结果,foo以我能理解的更多方式使用。在某些情况下,我正在阅读关于特定语言的问题,而我正在这样做以帮助更好地理解该语言。如果使用适用的变量名而不是foo和bar,则可以更容易理解和解释问题。因此对于经验丰富的开发人员来说,这个结构似乎也有点缺陷。
Is this a habit that will ever be able to be kicked? Why do you choose to foo bar or to not foo bar?
这是一种能够被踢的习惯吗?你为什么选择foo bar或者不选择foo bar?
10 个解决方案
#1
37
It strictly depends on what are you trying to teach. Sometimes, when showing a programming example, you have to declare a few things just for the snippet to be "complete", and those few things are not the core of what you are showing.
它严格取决于你想要教什么。有时候,在展示一个编程示例时,你必须声明一些只是为了使代码片段“完整”,而这几件事情并不是你所展示的核心内容。
For example, if you want to show how to throw an exception, I believe it is ok to present a snippet like
例如,如果你想展示如何抛出异常,我相信可以提供一个类似的代码片段
public void foo() {
// Do some things
if (errorCondition) {
throw new Exception("Error message");
}
}
Since the point in it is showing exceptions, there is no point in caring about the method name, so foo is "legal" in this context, or at least for me.
由于它中的点是显示异常,所以关心方法名称是没有意义的,所以foo在这种情况下是“合法的”,或者至少对我而言。
What I would not accept (in this same example) would be
我不会接受(在同一个例子中)
public void foo() {
// Do some things
if (bar) {
throw new Exception(baz);
}
}
as it is obscuring what you are trying to teach.
因为它模糊了你想要教的东西。
#2
15
I can see the point when talking to non programmers, but when you're at a whiteboard discussing a problem with some team members .. I would miss my foos and my bars. I think the prevalence of foo/bar is an example of the ability of most programmers to think abstractly.
我可以在与非程序员交谈时看到这一点,但是当你在白板上与一些团队成员讨论问题时......我会想念我的抱负和我的酒吧。我认为foo / bar的盛行是大多数程序员抽象思考的能力的一个例子。
Probably more of an issue if you're in the training arena.
如果你在训练场上,可能会出现更多问题。
#3
7
I use them sometimes. But only if a "real" name is not relevant.
我有时会用它们。但只有“真实”的名称不相关。
#4
3
I use them when demonstrating that any values of 'foo' and 'bar' will suffice, like "you can get the size of an object with sizeof(foo)." It's handy for getting people to understand the general concept and not just the particulars. For instance, if I'd said "you can get the size of an object with something like sizeof(int)", then it's almost guaranteed that someone would ask if that also works for floats.
我在演示'foo'和'bar'的任何值都足够时使用它们,比如“你可以获得sizeof(foo)对象的大小。”让人们理解一般概念而不仅仅是细节是很方便的。例如,如果我说“你可以获得像sizeof(int)这样的对象的大小”,那么几乎可以保证有人会问这是否也适用于浮点数。
#5
2
For totaly new programmer I have to say that terms foo and bar might not be known. I thought that they were something language specific (namely C), but after cheking Wikipedia I now know they are just abstract place holders. So if your audience consist of people who don't know meanings of them, something else is much clearer. Also first_number and so tells that those are numbers how ever they are presented and not something else.
对于全新的程序员,我不得不说术语foo和bar可能不为人所知。我认为它们是特定于语言的东西(即C),但在浏览*后,我现在知道它们只是抽象的占位符。因此,如果您的观众由不了解其含义的人组成,那么其他内容就会更加清晰。 first_number也是如此告诉我们这些数字是如何呈现的而不是其他内容。
#6
1
I choose not to foo and bar whenever my audience is familiar enough with the concept at hand that it would prove a detriment to their understanding.
每当我的观众对手头的概念足够熟悉时,我就会选择不去追求它,这会对他们的理解产生不利影响。
The only time Foo and Bar should be used is when you are talking about something so abstract that adding a context would require additional discussion. Then Foo and Bar are much more readable and created code that is more followable than the alternatives, like x, y and z.
应该使用Foo和Bar的唯一时间是当你谈论一些如此抽象的事情时,添加一个上下文需要额外的讨论。然后Foo和Bar更具可读性,并且创建的代码比x,y和z等替代品更具有可跟随性。
#7
1
I think it is due to mildly, or maybe not so mildly, sarcastic nature of many programmers. While many people have tried to place different meanings on foo/bar most , or at least many, of us think of "FUBAR", F**K Up Beyond All Recognition. Its a way for "experienced" people to make a snide comment about everyone else.
我认为这是由于许多程序员的讽刺性或温和性,或许不那么温和。虽然许多人试图在foo / bar上设置不同的含义,但我们中的大多数人,或者至少很多人都会想到“FUBAR”,F ** K Up Beyond All Recognition。它是“经验丰富”的人对其他人做出讽刺评论的一种方式。
Because of this I never use it for non programmer and seldom use it even with experienced programmers. If I do use you can bet I am making a veiled reference to the subject at hand.
因此,我从不将它用于非程序员,即使是经验丰富的程序员也很少使用它。如果我确实使用你可以打赌,我正在掩饰手头的主题。
#8
1
On top of avoiding nonsensical words like foo & bar, I've found it's much more important to provide code examples for real-world scenarios which have the same relationships. This really helps a learner to understand a topic properly and prevents misunderstandings. E.g., if I'm teaching about Dependency Injection and show example code where an instance of the Car class is injected into the Driver class, no one's going to get confused and think "So that means the Car controls the Driver then?".
除了避免像foo和bar这样荒谬的单词之外,我发现为具有相同关系的真实场景提供代码示例更为重要。这确实有助于学习者正确理解主题并防止误解。例如,如果我正在教授依赖注入并显示示例代码,其中将Car类的实例注入到Driver类中,那么没有人会感到困惑并且认为“那么这意味着Car控制驱动程序呢?”。
#9
1
I think there is another important reason for using foo
and bar
in examples. These names make it clear that you are not invoking any magic keywords. Whenever I am reading some documentation or code examples, I like the arbitrary parts of the example to be clearly distinguished from the necessary parts.
我认为在示例中使用foo和bar还有另一个重要原因。这些名称清楚地表明您没有调用任何魔术关键字。每当我阅读一些文档或代码示例时,我都希望将示例的任意部分与必要部分明确区分开来。
If you substituted the nonsense word for what it generically represents in the example code, you might end up with some names that look a lot like the keywords, classes, or methods you're trying to explain. The "my" prefix, as in myNumber
, myFunction
, is a good compromise that makes names stand out as being arbitrary.
如果您在示例代码中用无意义的单词替换它通常表示的内容,您最终可能会得到一些与您尝试解释的关键字,类或方法非常相似的名称。像myNumber,myFunction这样的“my”前缀是一个很好的折衷方案,它使名字脱颖而出是任意的。
#10
0
I am new to programming, and more or less self taught. I read a lot of example code online and at the beginning found myself replacing foo and bar &c. with more relevant names, such as the firstnumber and secondnumber examples above.
我是编程新手,或多或少自学。我在网上阅读了很多示例代码,最初发现自己替换了foo和bar&c。具有更多相关名称,例如上面的firstnumber和secondnumber示例。
I now prefer x,y,z,i... because foo and bar seem to spark linguistic impulses in my mind and can distract me from the routine, and I've developed, somewhat, the ability to hold a whole bunch of different variables in my head and remember what they are. But I would still definitely recommend using relevant naming when teaching someone else, especially when explaining code to someone that doesn't program but needs to understand how the program works.
我现在更喜欢x,y,z,i ...因为foo和bar似乎在我脑海中引发语言冲动并且可以分散我的注意力,而且我已经开发了一些能够控制一大堆不同的东西我头脑中的变量,记住它们是什么。但我仍然建议在教别人时使用相关的命名,特别是在向没有编程但需要了解程序如何工作的人解释代码时。
#1
37
It strictly depends on what are you trying to teach. Sometimes, when showing a programming example, you have to declare a few things just for the snippet to be "complete", and those few things are not the core of what you are showing.
它严格取决于你想要教什么。有时候,在展示一个编程示例时,你必须声明一些只是为了使代码片段“完整”,而这几件事情并不是你所展示的核心内容。
For example, if you want to show how to throw an exception, I believe it is ok to present a snippet like
例如,如果你想展示如何抛出异常,我相信可以提供一个类似的代码片段
public void foo() {
// Do some things
if (errorCondition) {
throw new Exception("Error message");
}
}
Since the point in it is showing exceptions, there is no point in caring about the method name, so foo is "legal" in this context, or at least for me.
由于它中的点是显示异常,所以关心方法名称是没有意义的,所以foo在这种情况下是“合法的”,或者至少对我而言。
What I would not accept (in this same example) would be
我不会接受(在同一个例子中)
public void foo() {
// Do some things
if (bar) {
throw new Exception(baz);
}
}
as it is obscuring what you are trying to teach.
因为它模糊了你想要教的东西。
#2
15
I can see the point when talking to non programmers, but when you're at a whiteboard discussing a problem with some team members .. I would miss my foos and my bars. I think the prevalence of foo/bar is an example of the ability of most programmers to think abstractly.
我可以在与非程序员交谈时看到这一点,但是当你在白板上与一些团队成员讨论问题时......我会想念我的抱负和我的酒吧。我认为foo / bar的盛行是大多数程序员抽象思考的能力的一个例子。
Probably more of an issue if you're in the training arena.
如果你在训练场上,可能会出现更多问题。
#3
7
I use them sometimes. But only if a "real" name is not relevant.
我有时会用它们。但只有“真实”的名称不相关。
#4
3
I use them when demonstrating that any values of 'foo' and 'bar' will suffice, like "you can get the size of an object with sizeof(foo)." It's handy for getting people to understand the general concept and not just the particulars. For instance, if I'd said "you can get the size of an object with something like sizeof(int)", then it's almost guaranteed that someone would ask if that also works for floats.
我在演示'foo'和'bar'的任何值都足够时使用它们,比如“你可以获得sizeof(foo)对象的大小。”让人们理解一般概念而不仅仅是细节是很方便的。例如,如果我说“你可以获得像sizeof(int)这样的对象的大小”,那么几乎可以保证有人会问这是否也适用于浮点数。
#5
2
For totaly new programmer I have to say that terms foo and bar might not be known. I thought that they were something language specific (namely C), but after cheking Wikipedia I now know they are just abstract place holders. So if your audience consist of people who don't know meanings of them, something else is much clearer. Also first_number and so tells that those are numbers how ever they are presented and not something else.
对于全新的程序员,我不得不说术语foo和bar可能不为人所知。我认为它们是特定于语言的东西(即C),但在浏览*后,我现在知道它们只是抽象的占位符。因此,如果您的观众由不了解其含义的人组成,那么其他内容就会更加清晰。 first_number也是如此告诉我们这些数字是如何呈现的而不是其他内容。
#6
1
I choose not to foo and bar whenever my audience is familiar enough with the concept at hand that it would prove a detriment to their understanding.
每当我的观众对手头的概念足够熟悉时,我就会选择不去追求它,这会对他们的理解产生不利影响。
The only time Foo and Bar should be used is when you are talking about something so abstract that adding a context would require additional discussion. Then Foo and Bar are much more readable and created code that is more followable than the alternatives, like x, y and z.
应该使用Foo和Bar的唯一时间是当你谈论一些如此抽象的事情时,添加一个上下文需要额外的讨论。然后Foo和Bar更具可读性,并且创建的代码比x,y和z等替代品更具有可跟随性。
#7
1
I think it is due to mildly, or maybe not so mildly, sarcastic nature of many programmers. While many people have tried to place different meanings on foo/bar most , or at least many, of us think of "FUBAR", F**K Up Beyond All Recognition. Its a way for "experienced" people to make a snide comment about everyone else.
我认为这是由于许多程序员的讽刺性或温和性,或许不那么温和。虽然许多人试图在foo / bar上设置不同的含义,但我们中的大多数人,或者至少很多人都会想到“FUBAR”,F ** K Up Beyond All Recognition。它是“经验丰富”的人对其他人做出讽刺评论的一种方式。
Because of this I never use it for non programmer and seldom use it even with experienced programmers. If I do use you can bet I am making a veiled reference to the subject at hand.
因此,我从不将它用于非程序员,即使是经验丰富的程序员也很少使用它。如果我确实使用你可以打赌,我正在掩饰手头的主题。
#8
1
On top of avoiding nonsensical words like foo & bar, I've found it's much more important to provide code examples for real-world scenarios which have the same relationships. This really helps a learner to understand a topic properly and prevents misunderstandings. E.g., if I'm teaching about Dependency Injection and show example code where an instance of the Car class is injected into the Driver class, no one's going to get confused and think "So that means the Car controls the Driver then?".
除了避免像foo和bar这样荒谬的单词之外,我发现为具有相同关系的真实场景提供代码示例更为重要。这确实有助于学习者正确理解主题并防止误解。例如,如果我正在教授依赖注入并显示示例代码,其中将Car类的实例注入到Driver类中,那么没有人会感到困惑并且认为“那么这意味着Car控制驱动程序呢?”。
#9
1
I think there is another important reason for using foo
and bar
in examples. These names make it clear that you are not invoking any magic keywords. Whenever I am reading some documentation or code examples, I like the arbitrary parts of the example to be clearly distinguished from the necessary parts.
我认为在示例中使用foo和bar还有另一个重要原因。这些名称清楚地表明您没有调用任何魔术关键字。每当我阅读一些文档或代码示例时,我都希望将示例的任意部分与必要部分明确区分开来。
If you substituted the nonsense word for what it generically represents in the example code, you might end up with some names that look a lot like the keywords, classes, or methods you're trying to explain. The "my" prefix, as in myNumber
, myFunction
, is a good compromise that makes names stand out as being arbitrary.
如果您在示例代码中用无意义的单词替换它通常表示的内容,您最终可能会得到一些与您尝试解释的关键字,类或方法非常相似的名称。像myNumber,myFunction这样的“my”前缀是一个很好的折衷方案,它使名字脱颖而出是任意的。
#10
0
I am new to programming, and more or less self taught. I read a lot of example code online and at the beginning found myself replacing foo and bar &c. with more relevant names, such as the firstnumber and secondnumber examples above.
我是编程新手,或多或少自学。我在网上阅读了很多示例代码,最初发现自己替换了foo和bar&c。具有更多相关名称,例如上面的firstnumber和secondnumber示例。
I now prefer x,y,z,i... because foo and bar seem to spark linguistic impulses in my mind and can distract me from the routine, and I've developed, somewhat, the ability to hold a whole bunch of different variables in my head and remember what they are. But I would still definitely recommend using relevant naming when teaching someone else, especially when explaining code to someone that doesn't program but needs to understand how the program works.
我现在更喜欢x,y,z,i ...因为foo和bar似乎在我脑海中引发语言冲动并且可以分散我的注意力,而且我已经开发了一些能够控制一大堆不同的东西我头脑中的变量,记住它们是什么。但我仍然建议在教别人时使用相关的命名,特别是在向没有编程但需要了解程序如何工作的人解释代码时。