www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - A small style tip

reply Mail Mantis <mail.mantis.88 gmail.com> writes:
--002354477e36afff2004b57ed064
Content-Type: text/plain; charset=ISO-8859-1

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?

--002354477e36afff2004b57ed064
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Just a small tip for those people, who use following code style:<div><br></=
div><div>if( cond ) {</div><div>=A0 body</div><div>} else {</div><div>=A0 b=
ody</div><div>}</div><div><br></div><div>I&#39;ve found it very convenient =
to &quot;attach&quot; unittest block to my function declatarions in a same =
way:</div>
<div><br></div><div><div>private int find_pos_divisor( int first, int secon=
d ) {</div><div>=A0 =A0 int temp;</div><div>=A0 =A0 while( second ) {</div>=
<div>=A0 =A0 =A0 =A0 temp =3D second;</div><div>=A0 =A0 =A0 =A0 second =3D =
first % second;</div><div>
=A0 =A0 =A0 =A0 first =3D temp;</div><div>=A0 =A0 }</div><div>=A0 =A0 retur=
n abs( first );</div><div>} unittest {</div><div>=A0 =A0 assert(=A0find_pos=
_divisor=A0( 10, 20 ) =3D=3D 10 );</div><div>=A0 =A0 assert(=A0find_pos_div=
isor=A0( 10, 0 ) =3D=3D 10 );</div><div>
=A0 =A0 assert(=A0find_pos_divisor=A0( 9, 6 ) =3D=3D 3 );</div><div>=A0 =A0=
 assert(=A0find_pos_divisor=A0( 10, 3 ) =3D=3D 1 );</div><div>}</div></div>=
<div><br></div><div>Looks as a natural extension to the declaration, and he=
lps to keep all related code in one place. Did anyone found out any more co=
nvenient D-specific conventions?</div>

--002354477e36afff2004b57ed064--
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?

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