[swift-evolution] [Idea] Distinguishing code comments from text comments.

Andre Ponzo andre_ponzo at differentapps.com
Sat Aug 27 14:03:11 CDT 2016


Good day, swift-evolution followers,

After reading Doug McKenna's email, I also see advantages in using specific syntax to disable code instead of commenting it out.

To illustrate these ideas lets pick the more appropriate syntax (in my opinion) proposed by Doug: /{ ... }

For example:
/{
   print("Disabled code")
}

will produce the same result as:
if false {
   print("Disabled code")
}


1) Advantages of disabled code ( /{ ... } ) instead of commented-out code (/* ... */):

1.1)
The disabled code will evolve in case of refactoring. Variable and function names will be still valid the day the developer decides to re-enable the code.
Similarly, it prevents dead code inside comments:
   let x = 0
   ...
   /{x += 1}  // will cause a compiler error if the first and last lines are deleted.
   print(x)

1.2)
Permits the use of dedicated syntax colouring in the IDE.

1.3)
Permits correct indentation of the commented code (taking surrounding enabled code into account).


2) Advantages of /{ ... } instead of "if false { ... }":

2.1)
The IDE will not generate the warning "Will never be executed".

2.2)
It is more convenient.
For example suppose we have cond = true and a variable x, and that we prefer to add 2 to x instead of 1:
   original code: if cond {x += 1}
   - "if false { ... }" syntax:          if cond if false {x += 1} {x += 2}      // compiler error.
   - "/{ ... }" syntax:          if cond /{x += 1} {x += 2}     // OK.

2.3)
Semantically:
"/*" initiates a text comment. The symbol * refers to text as it is used in typography to mark footnotes.
"/{" initiates a code which is not executed. "/{" looks like "no {".



André Ponzo
DifferentApps.com
Geneva, Switzerland



More information about the swift-evolution mailing list