Click here to Skip to main content
15,846,038 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:

There are many options in the C# code formatting dialog, but there doesn't appear to be one to stop #pragmas being forced to the left-hand margin? Personally, I have always found this leads to less-readable code, and so I would like to be able to put them where I want. This seems to mean that I have to turn off all code formatting because I can't find a way to stop the formatter putting the #pragmas against the left margin.

Given the modern trend for everything to be controlled by separate XML (or similar) files, I wondered if anyone knew a 'back door' method of getting the formatter to leave my #pragma statements where I put them.

Any help would be very much appreciated.

Best wishes, Patrick
Updated 24-Aug-10 9:35am
Toli Cuturicu 24-Aug-10 10:11am    
What if somebody else would ever want (or have) to read your strangely indented code?!
Patrick Skelton 24-Aug-10 10:23am    
They would have to do what I have had to do hundreds of times in my 20-year career as a professional developer - accept that the way the code was formatted at the time of writing suited the development team who wrote it. In other words, accept that other professionals may have different opinions to me.

I worked in a large team as a senior developer on a video game that ran on multiple platforms and hence required a fair amount of #if code, and the whole team of 15 developers agreed that indenting #if statements within functions as if they were normal if() statements produced much more readable code.

I urge you to try it before you dismiss it. I personally find multiple levels of #if all against the margin to be TOTALLY unreadable.

- Patrick

1 solution

When you're working on an existing file, you should format your code to match the formatting of the existing code. If you are creating a new file (and if corporate code stayle guidelines permit it), then you can use your own style.

I rarely use compiler definitions, and if I do, it's only one level, and usually just to omit code that's been refactored until I'm happy that the refactored code is as good/better than what it replaced.
Share this answer
Sandeep Mewara 24-Aug-10 12:16pm    
Comment by OP: Thanks for the reply.

I agree that #if blocks of code are to be avoided if possible, but sometimes it simply isn't possible (take a look at Microsoft's own XNA examples or the standard C++ header files or any Unix kernel code or similar).

I also agree you should generally match the style of the code that has already been written when working on it.

I'd prefer not to use #ifs at all, but when I do have to on my own new code, I reserve the right to put them where I want them and not where Microsoft think I should put them. Just because they are Microsoft doesn't make them automatically right - as I am sure many readers on here will agree.

- Patrick

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900