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.
Community
  • 1
  • 1
Camilo Martin
  • 37,236
  • 20
  • 111
  • 154
  • 13
    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. – Cᴏʀʏ Mar 29 '12 at 01:30
  • 1
    How can it be *too* hungarian? – Mathias R. Jessen Mar 29 '12 at 01:31
  • @Cory So you say I should instead decorate stuff like this so that FxCop and StyleCop does not complain? – Camilo Martin Mar 29 '12 at 01:33
  • @MathiasR.Jessen `using TooHungarian = HorribleStuff.Hungarian;` – Camilo Martin Mar 29 '12 at 01:35
  • 1
    @TLama Ok so I'll edit it and remark that I like when FxCop and StyleCop don't complain. – Camilo Martin Mar 29 '12 at 01:36
  • 4
    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. – BoltClock Mar 29 '12 at 01:37
  • @BoltClock Because FxCop and StyleCop complains. I could however decorate the class so as for it not to be triggered I guess. – Camilo Martin Mar 29 '12 at 01:41

2 Answers2

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.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
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.