A small style tip

Jonathan M Davis jmdavisProg at gmx.com
Sun Jan 1 15:30:16 PST 2012


On Monday, January 02, 2012 00:16:18 Mail Mantis wrote:
> Just a small tip for those people, who use following code style:
> 
> if( cond ) {
>   body
> } else {
>   body
> }
> 
> I've found it very convenient to "attach" unittest block to my function
> declatarions in a same way:
> 
> private int find_pos_divisor( int first, int second ) {
>     int temp;
>     while( second ) {
>         temp = second;
>         second = first % second;
>         first = temp;
>     }
>     return abs( first );
> } unittest {
>     assert( find_pos_divisor ( 10, 20 ) == 10 );
>     assert( find_pos_divisor ( 10, 0 ) == 10 );
>     assert( find_pos_divisor ( 9, 6 ) == 3 );
>     assert( find_pos_divisor ( 10, 3 ) == 1 );
> }
> 
> Looks as a natural extension to the declaration, and helps to keep all
> related code in one place. Did anyone found out any more convenient
> D-specific conventions?

And I find that style to be seriously harming readability (braces should always 
be on their own line IMHO), but I guess that if you like it, it makes sense.

- Jonathan M Davis


More information about the Digitalmars-d mailing list