patterncsharpModerate
Is indentation of #if & #endif directives an important readability convention?
Viewed 0 times
endifconventionreadabilityindentationimportantdirectives
Problem
I'm sharing C# code with a fellow programmer who is more use to writing C++. He uses
It is interesting that the code sample in the
#if and #endif directives (which I do not tend to use) and when they occur they have no indentation, for example private void dumpToDisplay()
{
#if false
ushort[] ScanStatus = new ushort[6];
for (int ch = 0; ch < 6; ch++)
ScanStatus[ch] = mySteppers.GetStatus(ch);
return;
#endif
string dumpString = stepDump();
Debug.Print(dumpString);
}It is interesting that the code sample in the
#if C# documentation adopts the same convention and has no indentation for the #if and #endif directives. Is this important? It certainly makes them stand out, but is that good?Solution
I think it makes sense this way, because the
This is valid code in both states (
Note: I'm not saying that this is a good use of
#if directives are pretty much outside of the normal syntax. For example, you can write something like:#if DEBUG
if (CheckConditionDebug())
{
DoADebug();
#else
if (CheckCondition())
{
DoA();
#endif
DoB();
}This is valid code in both states (
DEBUG defined or not), but there is no good way to indent the directives.Note: I'm not saying that this is a good use of
#if, just that it is a possible use and that it makes sense to take this into account.Code Snippets
#if DEBUG
if (CheckConditionDebug())
{
DoADebug();
#else
if (CheckCondition())
{
DoA();
#endif
DoB();
}Context
StackExchange Code Review Q#45482, answer score: 14
Revisions (0)
No revisions yet.