Shutdown code comment: small life hacking

    Despite the simplicity (and, in general, the triviality, if you think about) of the solution described, I came across it purely by chance, while decorating the finished program, ready for delivery.

    In programming practice, a situation regularly happens when during development and debugging it is required to turn on some code and turn off another. This is easy to do with special type constructions #if true ... #else ... #endif, changing trueto false, or resorting to more sophisticated conditions.However, this design does not allow to create more than two alternative sections of code.

    In languages ‚Äč‚Äčthat support type comments /* ... */using a slightly non-standard construction, /**/ ... /*/ ... /*/ ... /**/you can create as many alternating sections of code that will alternately turn on and off with just one space in the first (starting) comment.

    For example:

    /**/
            Console.Write("1");
    /*/
            Console.Write("2");
    /*/
            Console.Write("3");
    /*/
            Console.Write("4");
    /**/
            Console.Write("5");
    

    When executed, this code will output a string to the console "135". That is, all odd inference operators will be executed - and the last one, which is already outside the whole construction. But if you insert a space (or, strictly speaking, any character except an asterisk) in the starting comment between the second asterisk and the slash, the same code will output the line "245": only even operators will be executed, and, again, the last one that is already outside . (UPD: Thanks to FluffyMan for pointing out the error.)

    The syntax of commenting delimiters is extremely strict: you /*/can neither subtract nor add anything to the structure , it will destroy its functionality. The syntax of the start and stop comments opposite is completely arbitrary. It can be minimalistic/**/or it may contain any comments that are legal in the sense of the language. From where it is clear that start and stop comments are strictly obligatory, and that it is impossible to simply use a legal type comment in the construction itself /* ... */, since he immediately becomes a stop for the entire previous sequence of comments, separators /*/, and starting for the entire subsequent sequence. But sensible use of such inserts may be useful.

    Single-line comments //do not affect the functionality.

    Dixi :)

    Also popular now: