Thread overview
A small style tip
Jan 01, 2012
Mail Mantis
Jan 01, 2012
David
Jan 01, 2012
bearophile
Jan 02, 2012
F i L
January 01, 2012
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?


January 01, 2012
Am 01.01.2012 23:16, schrieb Mail Mantis:
> 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?
Nice, I like it
January 01, 2012
Mail Mantis:

> 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 );
> }

It's an interesting idea, I will try it.

Bye,
bearophile
January 02, 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;
   }
}