2

I'm writing on a script for Unity 3D and have/want to use some pre-processors.

For example VisualStudio always throughs some warnings such as

Field 'foo' is never assigned to, and will always have its default value 'null' (CS0649) (Assembly-CSharp)

for a code like

puclic class Example : MonoBehvaiour
{
    [SerializedField]
    private GameObject exampleObject;

    ...
}

though the field ofcourse will be set within the Inspector of the Unity Editor.

Additionally I never use the field in this class but have written a CustomEditor where the field is needed. So I also would get some other warnings something like

variable declared but not used.
variable assigned but not used.
private field assigned but not used.

So after making sure that the code is correct and those warnings unneccesary I decided to follow this and this post and used some pre-processors e.g.

#pragma warning disable 0414
#pragma warning disable 0649

    // some fields here

#pragma warning restore 0414
#pragma warning restore 0649

So now to my question:

And how could I add comments for pre-processors?
It's a pity there is not just something like

#comment "some comment here"

This seems not really intended/thought about but I would really like to do it in order to let my collegues know what the pre-processors are good/used for.


Ofcourse I could just add the comments but they are not intended with the pre-processors but with the normal code as e.g.

    // This silences "private field assigned but not used." warnings
#pragma warning disable 0414

    // flag to look for unsaved changes
    private bool _unsavedChanges = false;

    // This re-enables "private field assigned but not used" warnings
#pragma warning restore 0414

this makes it look very cluttered instead of clarify things and the comments don't seem to refer/"belong" to the pre-processors but the normal code instead.

The alternative would be inline comments which for now come closest to it I guess

#pragma warning disable 0414 // This silences "private field assigned but not used." warnings

    // flag to look for unsaved changes
    private bool _unsavedChanges = false;

#pragma warning restore 0414 // This re-enables "private field assigned but not used" warnings

Note:
I do not want to just disable warnings in general for the file or project!
And these examples are not the only pre-processors I'ld like to add comments for.

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Possible duplicate of [Custom Compiler Warnings](https://stackoverflow.com/questions/154109/custom-compiler-warnings) – Hazel へいぜる Jul 31 '18 at 08:46
  • Comments should never be seen as clutter. They assist future developers that come in behind you to maintain your code. – Hazel へいぜる Jul 31 '18 at 08:50
  • 1
    @davisj1691 please see my comment on your Answer .. this is not what I'm looking for. I don't want to add a custom **warning** but instead a **comment** which explained that my pre-processor **disabled** some warnings – derHugo Jul 31 '18 at 08:54
  • Why not just use a normal comment? It may look cluttered, but the better the documentation, the easier it is for someone with no knowledge of your code to maintain. – Hazel へいぜる Jul 31 '18 at 08:55
  • 1
    @davisj1691 Because as I mentioned already it makes it look very strange since normal comments have a "wrong" indentation and don't seem to refer to the pre-processor. Especially in some `#if... #else... #endif` blocks it doesn't really make things better understandable – derHugo Jul 31 '18 at 08:56
  • Well there aren't any [preprocessing directives](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/) that handle comments, you could always remove the indentation manually. – Hazel へいぜる Jul 31 '18 at 09:06
  • 1
    @davisj1691 it will always be re-intended by VisualStudio automatically together with all other lines. – derHugo Jul 31 '18 at 09:13
  • You have the option to turn auto-formatting off. Either way, traditional comments are really going to be the only option here. Trust me, comments go a long way. I currently have to maintain an application that is 15 years old in a 43000 line single code file and it has almost no comments what-so-ever. Debugging has literally been "Changing stuff and seeing what happens.". – Hazel へいぜる Jul 31 '18 at 09:23
  • I would remove the comment for the `restore` part as that's redundant and would rather comment on *why* the directive is there instead of describing what it does. Also, IMO this would not clutter the code any more than any other comment. Yes, the indentation is unlucky maybe making the comment on the same line as the directive could work for you? – Freggar Jul 31 '18 at 09:39
  • @Freggar damn :D This is actually an idea ... now I feel dumb, tanks ^^ – derHugo Aug 06 '18 at 07:07

0 Answers0