Exceptional coding style

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Jan 15 10:43:44 PST 2013


On Tue, Jan 15, 2013 at 06:11:46PM +0000, Russel Winder wrote:
[...]
> On Tue, 2013-01-15 at 12:09 -0500, Andrei Alexandrescu wrote:
> […]
> > In book code samples you need frequently to align things vertically 
> > (e.g. comments):
> > 
> > int a = 42;   // the meaning of everything
> > a += 0.1;     // error! cannot assign double to int
> 
> That sort of alignment is nigh on impossible in a proportional font
> using listing or minted. Of course there is no association between the
> comments so no reason why they should align.  Tab alignments
> definitely a bad idea for import by reference material.
[...]

This is exactly the sort of thing where I find proportional fonts
unusable for code.

It's not inherently so, though. I've oft dreamed of a new way of
displaying code, in which same-line comments are displayed in a separate
pane in the editor (rather than intermixed with the code in the same
pane). This comment pane can be adjustable in size and position, and
comments can even be made collapsible (and the code pane would adjust
the amount of space between lines to keep all comments aligned with
their respective lines of code). Why should the textual representation
of the code be conflated with the way it is displayed anyway? Let the
code editor do all the formatting and layout, including alignment of
same-line comments across lines. The actual source file can be free of
all unnecessary whitespace (the compiler doesn't care anyway).

In fact, this allows for very long lines of code (say a complicated
if-condition) to be formatted alongside a long comment block, something
like this:

	if ((cond1 && !cond2) ||	// Check if the complicated
	    (!cond3 && cond4) ||	// conditions described earlier
	    (value1 < value2*16) ||	// are fulfilled.
	    (value1 >= minVal))
	{
		...
	}

You never have to manually wrap the code and comment lines yourself
(which would be very painful if you want to keep everything aligned).
The source file could very well represent this thus:

	if ((cond1 && !cond2) || (!cond3 && cond4) || (value1 < value2*16) || (value1 >= minVal)) // Check if the complicated conditions described earlier are fulfilled.
	{
		...
	}

and the code editor does the pretty layout for you.


T

-- 
Spaghetti code may be tangly, but lasagna code is just cheesy.


More information about the Digitalmars-d mailing list