foo、bar到底是什么意思

在学习编程语言的过程中,尤其使用的是英文的书籍,我们经常发现一些foo、bar、baz之类的名字,要么是变量的名字,要么是文件的名字。。。深究起来完全不明所以。这到底是什么意思呢?

示例

下面是《C++17 STL Cookbook》这本书的一些用例:

//foo用做类名
class foo {
public:
    static std::string &standard_string() {
        static std::string s{"some standard string"};
        return s;
    }
};

还有:

//用作文件名
path p {"testdir/foobar.txt"};

同样其他语言里也有这种情况,《The Go Programming Language》这本书使用了大量此类名字:

f, err = os.Open("foo.txt") // 用作文件名
//用做字符串
fmt.Println(Equal([]string{"foo"}, []string{"bar"}))

解释

那这些单词传达给我们的意思是什么呢?实际上这些东西跟我们中文里的张三、李四、王五这类词属于一类,表示当前命名“无关紧要”,不影响当前概念的表达。

其他资料

维基百科里有比较专业的解释:

The terms
foobar (/ˈfuːbɑːr/), or
foo and others are used as placeholder names (also referred to as metasyntactic variables) in computer programming or computer-related documentation.[1] They have been used to name entities such as variables, functions, and commands whose exact identity is unimportant and serve only to demonstrate a concept.

直译过来就是:

foobar或者
foo等诸如此类的措辞在计算机编程或计算机相关文档中被用作占位符名字(也称为元语法变量)。它们通常被用来命名一些变量、函数或命令等此类实体,而这些实体通常不重要,而且仅仅用来演示一个概念。

请关注我的公众号哦。
《foo、bar到底是什么意思》

    原文作者:ideami
    原文地址: https://segmentfault.com/a/1190000017500898
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞