A small style tip

F i L witte2008 at gmail.com
Sun Jan 1 22:47:36 PST 2012


On Sunday, 1 January 2012 at 22:18:05 UTC, 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?

I've been playing with different style options, especially with 
function contracts. Traditionally, I've used a new line per 
bracket (at least for types and methods) but I've been playing 
around with putting them after the definition. I've found 
readability makes a huge difference that way if your variable 
definition (auto, int, string, etc) are colored different than 
keywords (if, static, public, etc).

class Person
{
    string name;

    void coolName(string name) in {
        assert(name != "F i L");
    } body {
        this.name = name;
    }
}


More information about the Digitalmars-d mailing list