Programming Practices – Linking in Code Comments to Unique Anchors

coding-stylecommentsmaintenanceprogramming practices

How would you recommend creating "anchors" in code comments, so that during maintenance, developers can easily cross-reference other code comments.

I realise that software should be intelligently structured and this should mitigate this requirement to some degree, but I have often found myself writing a comment where I would like future developers to take something else into account (not necessarily a direct dependency).

For example, "If you change this mechanism, be sure to review XYZ". I'm thinking of hyper links, wiki-style, where each link has a unique identifier and can have a one-to-many relationship with other links.

Ideally, if someone re-writes some of my code, I would like them to be automatically warned that "something else links here, and you should check it out in case you're changing this" – and I don't mean breaking something as in "something won't compile", but really just an easy-to-follow pointer (or pointers) to somewhere else in a code base that someone (or indeed your past self) felt there was a human-understandable conceptual link, concept or implementation.

I would be surprised if there isn't already a standard in place for this, but searching for "comment", "link", "anchor" understandably yields a proverbial haystack.

Best Answer

There's no language-agnostic way of dealing with this problem: the solution is very much tied to your method of consumption of this information and what your language and tools can natively support.

If your language and IDE has a way to create and links to code elements (like C# XML Doc Comments or Java Doc), you can associate your documentation with a relevant code element and then link to that element from the other locations which are related to this. This might be good enough as the basis for a knowledge-base to consume through your IDE. You can probably extract this meta-information and build reference documentation that could be easier to consume.

If your IDE doesn't support this sort of linking, or you don't want to consume this information through your IDE, you'll need to go to something external, at which point a Wiki is the easy solution. A hyperlink to an article can be represented in any source file and anything starting http:// is easily followed by any developer.