为什么全帽常数被认为是坏的编码风格? [英] Why are all-caps constant considered bad coding style?

查看:114
本文介绍了为什么全帽常数被认为是坏的编码风格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jeff完成了他的帖子,谈论这个,但我不'

Jeff finished his post talking about this, but I don't catch the idea.

那么,你为什么认为这是一个坏的编码风格?

So, why do you think this is a bad coding style?

EDIT:

我,很多你不认为这是一个坏的编码风格。但是杰夫比我更好的程序员,他的观点打开了我的头上的灯,如果我错了,回答。我是 Delphi 开发人员几年,现在成为一名C#开发人员,在德尔福这是一种常见的做法。

I, as a lot of you, don't think that it is a bad coding style. But Jeff is far better programmer than me, and his point of view turned on lights on my head to get answering if I was wrong. I was a Delphi developer for some few years and am becoming a C# developer now, and in Delphi it was a common practice.

推荐答案

所有大写都是预处理器宏常量的传统C名称。在不同的命名空间中使用它们是非常有用的,因为预处理器将在找到该名称的任何地方替换,而不管范围。

All caps is the traditional C designation of a preprocessor macro constant. It's very useful to have these in a different namespace from anything else, since the preprocessor will substitute wherever it finds the name, regardless of things like scope.

感觉到Jeff使用的是,在语义上,一个不能改变的变量。

A constant in the sense that Jeff was using is, semantically, a variable that can't be changed. It obeys all scoping principles and everything, and is semantically identical to a non-const variable with the same value.

换句话说,

#define max_length 5

是一个问题,因为有人可能在不同的上下文中使用 max_length 作为变量,它通常是安全的,而

is a problem because somebody might use max_length as a variable in a different context, where it would normally be safe, while

const int max_length = 5;

只是一个变量声明。因此,使用

is simply a variable declaration. Therefore, there's an advantage in using

#define MAX_LENGTH 5

因为惯例是只有预处理程序常量是全限制,所以它不会干扰任何其他用途。

because the convention is that only preprocessor constants are all-caps, so it will not interfere with any other use.

这篇关于为什么全帽常数被认为是坏的编码风格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆