Naming Windows API constants in C#
Asked Answered
W

2

13

The naming convention for constants in C# is Pascal casing:

private const int TheAnswer = 42;

But sometimes we need to represent already-existing constants from the Windows API.

For example, I don't know how to name this:

/// <summary>
/// With this style turned on for your form, 
/// Windows double-buffers the form and all its child controls.
/// </summary>
public const int WS_EX_COMPOSITED = 0x02000000;

What should I name it?

Keeping it as WS_EX_COMPOSITED allows me to quickly associate it with the WinAPI, but it's wrong.

Some options:

  • WsExComposited -- Too hungarian
  • Composited -- Too short
  • WsEx enum with Composited in it -- Still hungarian
  • ExtendedWindowsStyles.Composited -- Constant in a class? Enum?

It should be noted that the objectives for a good naming are:

  • It must be readable.
  • It must not trigger FxCop and StyleCop, even if that means hiding it from them.
Wilds answered 29/3, 2012 at 1:21 Comment(7)
IMO, I think disobeying the guideline is perfectly ok in this situation. The summary comment and its blatant WinAPI-ishness are good enough to leave it as is -- most people would understand. If you must pick a format, I think I like the ExtendedWindowStyles.Composited as a constant in a class the most.Gasman
How can it be too hungarian?Inconsiderable
@Cory So you say I should instead decorate stuff like this so that FxCop and StyleCop does not complain?Wilds
@MathiasR.Jessen using TooHungarian = HorribleStuff.Hungarian;Wilds
@TLama Ok so I'll edit it and remark that I like when FxCop and StyleCop don't complain.Wilds
Why is WS_EX_COMPOSITED wrong? I really don't get it. As far as interfacing with the Windows API is concerned, it's as good as it gets.Geopolitics
@Geopolitics Because FxCop and StyleCop complains. I could however decorate the class so as for it not to be triggered I guess.Wilds
E
15

WS_EX_COMPOSITED is perfectly fine for part that directly interfaces with Win API (or any other documented API for that matter). The portion that you want to expose should follow standard conventions - have separate public facade to call native functions with good method names (you'll eventually forget what you've researched about particular combinations of flags, but good wrapper method name will at least let you use it).

The side benefit of keeping the name as close as possible to native API is that you can search for constant directly in the MSDN/other documentation and find answer immediately. It will be much harder if you rename it.

Eustashe answered 29/3, 2012 at 1:42 Comment(1)
That is the exact reason I feel I should not rename it. I believe you are right.Wilds
C
0

Maybe this will help new visitors with this question.

If the field or variable name is intended to match the name of an item associated with Win32 or COM, and thus needs to begin with an underscore, place the field or variable within a special NativeMethods class. A NativeMethods class is any class which contains a name ending in NativeMethods, and is intended as a placeholder for Win32 or COM wrappers. StyleCop will ignore this violation if the item is placed within a NativeMethods class.

Chrisom answered 1/3, 2018 at 18:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.