Replace inline docs with .swiftDoc files

When I go to look at the actual source code of something, it’s almost always because:
(a) the documentation was insufficient for me to really understand what’s going on, or
(b) I already know what’s happening but I just want set a breakpoint for debugging.

In both of these cases, the presence of huge amounts of inline documentation in the source files just makes the code very difficult to read.

In ObjectiveC we could mostly get around this by putting docs in the headers. But Swift doesn’t have headers.

So I find myself wishing I could keep documentation in a separate file, perhaps a file that even takes advantage of XCode 9’s new built-in Markdown formatting.

Is this a pipe dream or could it someday happen?

I could even imagine a future in which the leading Swift IDEs can show you a split screen view in which the documentation for a function automatically appears in an *editable* side bar, and when you edit it there, the .swiftDoc file gets updated as well, but your code itself remains pure.

That way, you can keep comments to a minimum in your source files.

Thoughts?

(If this has been pitched before, slap me please, but I didn’t see it.)

- Jon

1 Like

Colocation of docs with the material they document is valuable to me and I presume anyone updating code. If anything, it would be nice if Xcode provided a show/hide doc headers toggle though.

-- E

···

On Nov 8, 2017, at 11:20 AM, Jon Gilbert via swift-evolution <swift-evolution@swift.org> wrote:

When I go to look at the actual source code of something, it’s almost always because:
(a) the documentation was insufficient for me to really understand what’s going on, or
(b) I already know what’s happening but I just want set a breakpoint for debugging.

In both of these cases, the presence of huge amounts of inline documentation in the source files just makes the code very difficult to read.

In ObjectiveC we could mostly get around this by putting docs in the headers. But Swift doesn’t have headers.

So I find myself wishing I could keep documentation in a separate file, perhaps a file that even takes advantage of XCode 9’s new built-in Markdown formatting.

Is this a pipe dream or could it someday happen?

I could even imagine a future in which the leading Swift IDEs can show you a split screen view in which the documentation for a function automatically appears in an *editable* side bar, and when you edit it there, the .swiftDoc file gets updated as well, but your code itself remains pure.

That way, you can keep comments to a minimum in your source files.

Thoughts?

(If this has been pitched before, slap me please, but I didn’t see it.)

- Jon
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

5 Likes

I agree with Jon that conflating docs with code makes an otherwise clean swift file into a bit of a mess, and it discourages me slightly from properly documenting.

If there was something akin to "extension" but for adding documentation from a separate file ("doc Class {...}"), that could be great. For me it would make maintaining easier, as I could have the doc file open in one window, where I scroll through independently and see several function docs near each other, and my clean code in another.

Code files would benefit from less cognitive overload when wading through them. Doc syntax is its own thing. If desired, Xcode could certainly warn against obvious mismatches that might happen as the code evolves.

My 2¢,
jeff

···

On Nov 8, 2017, at 12:20 PM, Jon Gilbert via swift-evolution <swift-evolution@swift.org> wrote:

When I go to look at the actual source code of something, it’s almost always because:
(a) the documentation was insufficient for me to really understand what’s going on, or
(b) I already know what’s happening but I just want set a breakpoint for debugging.

In both of these cases, the presence of huge amounts of inline documentation in the source files just makes the code very difficult to read.

In ObjectiveC we could mostly get around this by putting docs in the headers. But Swift doesn’t have headers.

So I find myself wishing I could keep documentation in a separate file, perhaps a file that even takes advantage of XCode 9’s new built-in Markdown formatting.

Is this a pipe dream or could it someday happen?

I could even imagine a future in which the leading Swift IDEs can show you a split screen view in which the documentation for a function automatically appears in an *editable* side bar, and when you edit it there, the .swiftDoc file gets updated as well, but your code itself remains pure.

That way, you can keep comments to a minimum in your source files.

Thoughts?

(If this has been pitched before, slap me please, but I didn’t see it.)

- Jon
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Hi Jon,

An interesting proposal. I see value in an external markdown-formatted document to compliment our Swift code, but currently I already do this when discussing ideas or architecture and just call it “.md”.

I agree with Erica on the point though that documentation with code is actually helpful to me and I would probably prefer documentation around methods and types to sit with the content itself. I also find documentation really helpful in breaking up a swift file, away from being code soup, and into a very formalised structure that I find easier to cognitively parse.

I really would like some work on Xcode though to allow hiding documentation, and improvements to the generated interfaces which are somewhat lacking atm.

- Rod

···

On 9 Nov 2017, at 5:57 am, Erica Sadun via swift-evolution <swift-evolution@swift.org> wrote:

Colocation of docs with the material they document is valuable to me and I presume anyone updating code. If anything, it would be nice if Xcode provided a show/hide doc headers toggle though.

-- E

On Nov 8, 2017, at 11:20 AM, Jon Gilbert via swift-evolution <swift-evolution@swift.org> wrote:

When I go to look at the actual source code of something, it’s almost always because:
(a) the documentation was insufficient for me to really understand what’s going on, or
(b) I already know what’s happening but I just want set a breakpoint for debugging.

In both of these cases, the presence of huge amounts of inline documentation in the source files just makes the code very difficult to read.

In ObjectiveC we could mostly get around this by putting docs in the headers. But Swift doesn’t have headers.

So I find myself wishing I could keep documentation in a separate file, perhaps a file that even takes advantage of XCode 9’s new built-in Markdown formatting.

Is this a pipe dream or could it someday happen?

I could even imagine a future in which the leading Swift IDEs can show you a split screen view in which the documentation for a function automatically appears in an *editable* side bar, and when you edit it there, the .swiftDoc file gets updated as well, but your code itself remains pure.

That way, you can keep comments to a minimum in your source files.

Thoughts?

(If this has been pitched before, slap me please, but I didn’t see it.)

- Jon
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

1 Like

[Xcode 9.1]

Editor > Code Folding > Fold Comment Blocks

Editor > Code Folding > Fold Methods & Functions

···

On 8 Nov 2017, at 18:57, Erica Sadun wrote:

Colocation of docs with the material they document is valuable to me and I presume anyone updating code. If anything, it would be nice if Xcode provided a show/hide doc headers toggle though.

1 Like

They removed the little UI sidebar that was so useful for code folding, though. I do hope it makes a return.

Also I am not saying projects should be forced to keep docs in separate files, just that it would be nice to have the option. I suppose it could be achieved through an XCode plugin though.

J

···

On Nov 9, 2017, at 03:19, Ben Rimmington via swift-evolution <swift-evolution@swift.org> wrote:

On 8 Nov 2017, at 18:57, Erica Sadun wrote:

Colocation of docs with the material they document is valuable to me and I presume anyone updating code. If anything, it would be nice if Xcode provided a show/hide doc headers toggle though.

[Xcode 9.1]

Editor > Code Folding > Fold Comment Blocks

Editor > Code Folding > Fold Methods & Functions
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

1 Like

To those who pointed out that XCode can already fold comment blocks, let me point out that only works in XCode, and not in, say, github.

If my pitch were implemented, your IDE could still show you the documentation inline with the code.

In fact, XCode already lets you option-click a function call to see a pop-up that contains the documentation (which currently comes from the doc comments but could easily come from a .swiftDoc file). XCode can also already show you the documentation in the help sidebar.

So why not go a step further and have all documentation in a separate file, then have the IDE be able to make it appear as if it were inline, but without it affecting the line numbers of code?

As well, you could have PR rules that permit .swiftDoc-only commits with fewer required code reviewers. The IDE could require that any new .swift file to be accompanied by a .swiftDoc file.

During code review, empty .swiftDoc commits, or commits that substantially change a .swift file without changing the associated .swiftDoc file, would stand out like a sore thumb during code review, helping to prevent documentation getting out of sync.

As well, since the swiftDoc file could now be analyzed according to its own grammar, the compiler could even auto-generate the basic docs for any new function and throw warnings for any undocumented functions, arguments, vars, etc.

The live Markdown capabilities already in XCode means that users could be prevented from incorrect Markdown by compiler errors/warnings.

As well, when you’re browsing a repo in github, it would be trivial to open a second browser tab (or split view, thanks to the latest OSs) with the accompanying .swiftDoc markdown file (which github would auto-format, making it much more legible!). It’s easy to use command-f to jump to the declaration you are interested in, and XCode could of course jump you to it.

Currently there is nothing to prevent doc comments from getting out of sync with the code they are supposed to document, however with a .swiftDoc file I believe it would be easier for the compiler to make sure every function’s documentation at least has the correct labels for each argument, and include such changes into refactors.

This feature would be fully optional for folks who want to keep documentation in-line, or don’t have time to migrate existing projects. Of course, imagine if XCode could migrate your projects for you? :smiley:

One last thing: using separate files for documentation would allow for comments to be in multiple languages. There are multiple ways this could be approached. One way would be for the .swiftDoc files that are in the same folder with your source to be in default language for the project (assuming English), and then the translated docs are kept in subfolders. Or, the translations could all just be in different parts of the same file, or they could all be kept in folders.

I know project maintainers likely don’t want to think about “oh god the idea of maintaining multiple languages of documentation”, but some projects already have to do this, and for them, this might be a godsend, since the compiler would know if the files got out of sync. Also, it might be nice to refer to the docs in the native language of the project, when it’s not English, and the broken-English documentation makes no sense.

In the future (or even now), having docs in separate files could also make it much simpler to create an auto-translator, since the parts of a swiftDoc that refer to actual code could be in backticks, forcing monospace and preventing translation. m

In conclusion, I believe in the single responsibility principle. Making a .swift file contain two entirely different syntaxes of code (Swift and Markdown) seems like a violation of this. Having small comments and //MARK: - to help someone reading code is great, but huge paragraphs and lists of full documentation often requires more room than the code itself, so what am I looking at? Code or documentation? Both? It’s just bad design.

I’m just saying. :smiley:

- Jon

···

On Nov 8, 2017, at 12:59, Rod Brown <rodney.brown6@icloud.com> wrote:

Hi Jon,

An interesting proposal. I see value in an external markdown-formatted document to compliment our Swift code, but currently I already do this when discussing ideas or architecture and just call it “.md”.

I agree with Erica on the point though that documentation with code is actually helpful to me and I would probably prefer documentation around methods and types to sit with the content itself. I also find documentation really helpful in breaking up a swift file, away from being code soup, and into a very formalised structure that I find easier to cognitively parse.

I really would like some work on Xcode though to allow hiding documentation, and improvements to the generated interfaces which are somewhat lacking atm.

- Rod

On 9 Nov 2017, at 5:57 am, Erica Sadun via swift-evolution <swift-evolution@swift.org> wrote:

Colocation of docs with the material they document is valuable to me and I presume anyone updating code. If anything, it would be nice if Xcode provided a show/hide doc headers toggle though.

-- E

On Nov 8, 2017, at 11:20 AM, Jon Gilbert via swift-evolution <swift-evolution@swift.org> wrote:

When I go to look at the actual source code of something, it’s almost always because:
(a) the documentation was insufficient for me to really understand what’s going on, or
(b) I already know what’s happening but I just want set a breakpoint for debugging.

In both of these cases, the presence of huge amounts of inline documentation in the source files just makes the code very difficult to read.

In ObjectiveC we could mostly get around this by putting docs in the headers. But Swift doesn’t have headers.

So I find myself wishing I could keep documentation in a separate file, perhaps a file that even takes advantage of XCode 9’s new built-in Markdown formatting.

Is this a pipe dream or could it someday happen?

I could even imagine a future in which the leading Swift IDEs can show you a split screen view in which the documentation for a function automatically appears in an *editable* side bar, and when you edit it there, the .swiftDoc file gets updated as well, but your code itself remains pure.

That way, you can keep comments to a minimum in your source files.

Thoughts?

(If this has been pitched before, slap me please, but I didn’t see it.)

- Jon
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

1 Like

I'm strongly against this. We already have way to few docs for Swift and this splitting would with a high probability reduce it even more (out of sight, out of mind...).

This thread was dead for more than half a year. I have only skimmed a little bit, and tend to agree with you, but bumping a discussion that seems to have died on it's own just to say you prefer it'd stay that way doesn't seem very sensible :confused:

2 Likes