June 03, 2008

Sourcecode-Formatting in the OTN-Forum

Nearly each day I see unformatted code in the OTN-Forum. Why?

The problem is, that many poster don't know, how to format sourcecodes. Example:


/*
|| Selecting the sum from all sub-order's
*/
if :control.sub_order_id is not null then
select sum (value)
into :control.sum
from sum_orders
where order_id in (select order_id
from sub_orders
where sub_order_id = :control.sub_order_id
and sub_order_type = 'ONLINE');
else
:control.sum := 0;
end if;
/*
|| now we use the sum in the ...
*/

Who can read that? I love puzzling, but not in sourcecodes. How can we solve this problem?

Use [pre] before your sourcecode and [/pre] after the sourcecode. Then it is readable like your original code:

[pre]
...
if :control.sub_order_id is not null then
select sum (value)
into :control.sum
...
[/pre]

and here is the solution of the above example after using the preformat-tags:

/*
|| Selecting the sum from all sub-order's
*/
if :control.sub_order_id is not null then
select sum (value)
into :control.sum
from sum_orders
where order_id in (select order_id
from sub_orders
where sub_order_id = :control.sub_order_id
and sub_order_type = 'ONLINE');
else
:control.sum := 0;
end if;
/*
|| now we use the sum in the ...
*/

This is readable code at it's best!

Try it
Gerd

7 comments:

Anonymous said...

I usually prefer [code] for pl/sql-blocks because this tag preserves blank lines.

SydOracle said...

A button, like the ones for bold, underline and italic, would solve the problem.

Gerd Volberg said...

[pre] vs. [code] seems to be identical in the otn-forum. [pre] is the only documented way: http://www.oracle.com/technology/forums/faq.html#q14

Anonymous said...

I have always formated code in the manner suggest and do my best to influence junior programmers to do the same. This is the FIRST time in 25 years that I have EVER seen ANYONE other than me recommend this formatting style.

Gerd Volberg said...

This kind of formatting is one of the best readable formats I found in the last 16 years. So I use it :-)

Anonymous said...

Also,like Jens,I prefer [code]. This tag preserves color for reserved words too.

Gerd Volberg said...

Hi Rosario,
can you post an example with color?