www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - A small style tip

reply Mail Mantis <mail.mantis.88 gmail.com> writes:
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?
Jan 01 2012
next sibling parent David <d dav1d.de> writes:
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
Jan 01 2012
prev sibling next sibling parent bearophile <bearophileHUGS lycos.com> writes:
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
Jan 01 2012
prev sibling parent "F i L" <witte2008 gmail.com> writes:
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; } }
Jan 01 2012