STM32例子中变量名开头的"uw"是什么意思?

What does the "uw" mean at the beginning of variable names in STM32 examples?

当我阅读 STM32 示例代码时,我看到相当多的变量以 uw 开头。例如:

static __IO uint32_t uwLsiFreq = 0;
__IO uint32_t uwCaptureNumber = 0;
__IO uint32_t uwPeriodValue = 0;
__IO uint32_t uwMeasurementDone = 0;

每件事都有其意义或背后的故事。这里的uw是什么意思?

示例来源:STM32Cube_FW_F2_V1.7.0/Projects/STM32F207ZG-Nucleo/Examples/IWDG/IWDG_Example/Src/main.c.
Download link --> 单击 "STM32CubeF2" 旁边的 "Get Software" 按钮。

在此命名约定中表示无符号字。现在几乎不用了。但有些人喜欢它,因为它向他们展示了变量的类型。另一些人讨厌争论这是最糟糕的编程习惯之一(包括 Stack Overflow 的创建者)。 IMO没关系

好的,我想 post 我自己的更完整的答案。感谢@njuffa 在我的问题下方的评论中指出“可能:uw 在某种 Hungarian notation," thanks to @PeterJ_01 for that the CEO of Stack Overflow has some opinions on the matter, and thanks to @Sigve Kolbeinson for pointing out in a comment under PeterJ_01's answer that the CEO of Stack Overflow's name is Joel Spolsky (as found in the link), he actually doesn't hate Hungarian notation, but rather is just upset a limited and corrupted form of it [Systems Hungarian] got traction for a while, and for giving us the actual link to the article 中代表 'unsigned word',因此我们可以了解更多信息并自己阅读文章。

1。这是我回答问题的结论:

许多STM32示例变量开头的

uw在逻辑上肯定意味着"unsigned word,",其中"word"在这种情况下是32位。当我阅读代码时,知道这一点会增加很多清晰度,并消除一些对名称的混淆,所以我很高兴知道这一点。

在这种情况下,这是 Systems Hungarian 用法的一种类型,对于具有显式类型的语言(例如 C 和 C++),通常不鼓励使用这种类型,因为它是多余的并且添加价值不大。将此与我在下面描述的 Apps Hungarian 进行对比,Joel Spolsky(Stack Overflow 的首席执行官)strongly promotes 作为一种帮助 "wrong code look wrong."

2。这是我从@Sigve 和@njuffa 那里学到的一些额外的见解(主要是关于匈牙利表示法),通过他们的评论和他们提供的 links:

您可以将此部分称为 "what exactly is Hungarian notation in computer programming?"

  1. Hungarian notation存在(之前不知道),指的是在每个变量and/or函数名的开头多加几个字符来提供附加信息的概念关于变量或函数,例如它的目的、它的类型,或者它的return类型.
  2. 无意中这也回答了我关于 FreeRTOS 命名约定的问题。现在我知道!他们也使用[主要是系统]匈牙利符号。这里有一些 link。请注意,在第一个 link 中,您将看到 FreeRTOS 中所有匈牙利符号用法的列表。这种表示法几乎完全是 Systems Hungarian 表示法,但是当它们指定 文件的名称 时,可以说也使用了一点 Apps Hungarian 表示法,其中函数和宏是在函数内部定义的,或者宏名称。
  3. 匈牙利表示法有两种主要形式:Systems HungarianApps Hungarian。查看它们的差异 here
    • Systems Hungarian 本质上是对 "Hungarian" 符号的原始意图的破坏,它的创建者 Charles Simonyi 错误地使用了这个词 "type" 而不是他对它的描述中的 "kind" (source). Charle's original intent was to encode additional information into variable names that isn't inherent in the programming language itself. However, Systems Hungarian notation says basically to store the variable's type into the variable name. Ex: unsigned long myVar now becomes unsigned long ulMyVar. Most people seem to argue this is either of limited use or completely worthless. Joel Spolksy is not a proponent of this form of Hungarian notation, as indicated in his article, but he is strongly for Apps Hungarian style. Other opinions can be found in the "Notable opinions" section of the Wikipedia article here.
    • Apps Hungarian 描述了将附加信息存储到变量名前面的概念,否则无法轻易推断出这些信息,例如使用 us 表示“ unsafe string" 和 s 表示 "safe string"。许多人对这种方法要么感觉比较中立,要么喜欢它并推广它。 Joel Spolsky(Stack Overflow 的 CEO)和 Steve McConnel 都认为这是一个好主意并推广它的使用。

请注意,以下是 Joel's article 的有用摘录:

Somebody, somewhere, read Simonyi’s paper, where he used the word “type,” and thought he meant type, like class, like in a type system, like the type checking that the compiler does. He did not. He explained very carefully exactly what he meant by the word “type,” but it didn’t help. The damage was done.

Apps Hungarian had very useful, meaningful prefixes like “ix” to mean an index into an array, “c” to mean a count, “d” to mean the difference between two numbers (for example “dx” meant “width”), and so forth.

Systems Hungarian had far less useful prefixes like “l” for long and “ul” for “unsigned long” and “dw” for double word, which is, actually, uh, an unsigned long. In Systems Hungarian, the only thing that the prefix told you was the actual data type of the variable.

This was a subtle but complete misunderstanding of Simonyi’s intention and practice...

(强调)