离线和在书中呈现源代码的最佳方式是什么?

发布于 2024-07-25 21:46:00 字数 343 浏览 8 评论 0原文

由于对排版有些痴迷,我很好奇:在线和离线呈现代码的最佳方式是什么。 我特别感兴趣的是:

  • 什么类型的字体和尺寸最适合在线和离线? 在代码中使用比例字体是否更好(例如,像 Stroustrup 在“C++ 编程语言”中所做的那样)? 衬线、无衬线或混合?
  • 代码应该如何注释 - 使用典型的 // comment 标记,或者可能只是使用不同的字体(并且没有注释标签)?
  • 是否值得绘制箭头(或类似的东西)来显示不同的代码块如何相互关联?
  • 在几列中并排显示代码是否有效?
  • 有人需要诸如行编号、更改行等之类的东西吗?

Being somewhat typographically obsessed, I am curious: what is the best way to present code - both online and offline. In particular, I'm interested in:

  • What kinds of typefaces and sizes work best online and offline? Is it better to use proportional fonts for code (e.g., like Stroustrup does in 'The C++ Programming Language')? Serif, sans-serif, or mixed?
  • How should code be annotated - with the typical // comment markup, or perhaps just with a different typeface (and no comment tags)?
  • Is it worth drawing arrows (or something similar) to show how different blocks of code relate to one another?
  • Does showing code in several columns side-by-side work at all?
  • Does anyone need things like line numbering, altering lines, etc.?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

江湖正好 2024-08-01 21:46:00

This doesn't exactly answer most of the questions, but you might be interested in reading up on Literate Programming. The gist of it is that you write your code and documentation in the same document, and it is formatted for web or print or whatever. And when I say documentation here, it's not usually one-line snippets of documentation, but whole paragraphs or pages describing what's going on in the program.

孤独陪着我 2024-08-01 21:46:00

好吧,让我投票反对 BS 在 TC++PL 中使用的比例字体。 这真的让我抓狂! 《Effective STL》中的索克特·迈耶斯 (Soctt Meyers) 也有类似的情况,但还添加了一些令人讨厌的突出显示,从而增加了恐怖感。

对我来说,关于编程的经典书籍永远是《K&R》,因为我认为它从未在演示的清晰度方面得到过改进。 所以我建议盲目地复制它。

Well, let me add my vote against proportional fonts, as used by BS in TC++PL. It really drives me nuts! Similarly Soctt Meyers in Effective STL, with the added horror of some nasty hightlighting.

For me, the classic book on programming will always be K&R, as I don't think it has ever been improved on for clarity of presentation. so I would suggest copying it slavishly.

稀香 2024-08-01 21:46:00

我喜欢在文本中编写虚拟代码时使用扩展字符集。 用于代码和数学标记的 LaTeX 字体效果很好。

一个很好的例子是使用左箭头进行赋值或绑定,而不是 =。 我见过的书籍中最好的代码示例是《Dragon Book》或《TAOCP》。 我认为由于能够呈现这些字符以及与常见语法的关系而将数学词源混合在一起是很棒的。

常见的固定快递类型字体和通过 Computer Modern in LaTeX 提供的高级数学标记的混合,我认为对于具有数学倾向的读者来说效果很好......而且它看起来非常漂亮。

I like the use of an extended character-set when writing dummy code in text. The LaTeX fonts for code and math markup work well.

A good example would be using a left arrow for assignment or binding rather than =. The best examples of code in books I have seen has been things like the Dragon Book or TAOCP. I think a mixture of mathematical etymology due to the ability to render those characters and a relation to common syntax is great.

A mixture of the usual fixed-courier type fonts and advanced math markup available through Computer Modern in LaTeX I think works well for readers of a mathematical disposition ... and it can look very nice.

恍梦境° 2024-08-01 21:46:00

我想到的只有几件事:

  • 将行数限制为最少
  • 仅在需要时才行数
  • 您当然可以使用不同的字体,但是不同的背景怎么样? 比如浅灰色? 把你的代码装箱,我认为将两个盒子放在一起是没有问题的。
  • 取决于成本,但是像大多数 IDE 那样为某些元素着色怎么样?
  • 编写有效的、现实世界的代码! 因此,我建议使用正常的 // 注释样式,而不仅仅是不同的字体。

Just a couple of things that cross my mind:

  • Limit the number of lines to a minimum
  • Line numbers only when needed
  • You can certainly use different fonts, but what about a different background? Like a light gray? Box your code and I think there is no problem in putting two boxes next to each other.
  • Depends on the costs, but what about coloring certain elements - like most IDEs do -?
  • Write working, real world code! Therefore I would suggest to use the normal // comment style and not just a different font.
檐上三寸雪 2024-08-01 21:46:00
  • 我会使用等宽字体
    康索拉斯。
  • 由于注释通常显示为不同的颜色(至少在我见过的 IDE 中),因此我会将它们保留原样。
  • “显示块如何相互关联”是什么意思? 指出函数、if 语句等的开始和结束位置? 如果是这样,可能会有所帮助; 老实说我不知道​​。
  • 如果您比较几块代码来指示一些细微的差异,这些差异在一个代码块中不起作用,但在另一个代码块中起作用,那么它可能会起作用,但除此之外,我认为它没有多大用处。
  • 如果您引用“转到上一个代码示例中的 XYZ 行”之类的内容,行号可能会有所帮助。
  • I would use a monopaced font like
    Consolas.
  • Since comments usually appear as just a different color (at least in the IDEs i've seen), i'd leave them in as-is.
  • What do you mean by "show how blocks relate to each other"? indicate where functions, if statements, etc. start and end? if so, it might be helpful; i honestly don't know.
  • It might work if you were comparing a couple of chunks of code to indicate some subtle difference that would not work in one but work in another, but i don't see much use for it beyond that.
  • Line numbers might help if you make references like "go to line XYZ in the previous code sample".
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文