[Article Contest, first draft] D Slices

Steven Schveighoffer schveiguy at yahoo.com
Thu May 19 06:32:52 PDT 2011


On Thu, 19 May 2011 00:27:51 -0400, Jonathan M Davis <jmdavisProg at gmx.com>  
wrote:

> On 2011-05-18 21:09, Jesse Phillips wrote:
>> Jonathan M Davis Wrote:
>> > The huge advantage of assert over writeln is that it shows you what  
>> the
>> > result is supposed to be. If you're reading the code or documentation,
>> > that's extremely valuable, whereas writeln is useless.

Both assert and writeln with a comment describing what is output are  
equally as useful for code that is not compiled and executed (i.e. you are  
just reading).  Both describe what you should expect, and you can't really  
prove it is wrong without compiling and executing.

writeln is just a little more friendly to those who want to see something  
happening when you do actually execute it.  Whenever I'm learning a  
language, I like to write little test programs to print out what's going  
on to make sure I understand it.

>> > However, if what
>> > you're concerned about is running the code and see the result, writeln
>> > is far more useful. Since you're writing an article, I would  
>> definitely
>> > go with assert, but it really depends on what you're trying to do.
>> > assert works far better as documentation because you can see the  
>> result
>> > as you read, but writeln when running code because when you do, you  
>> can
>> > see the result.
>> >
>> > - Jonathan M Davis
>>
>> I agree, but he is conflicted on whether the assert should pass or  
>> fail. By
>> your logic though it sound like you want it also passing. There isn't  
>> much
>> difference between these:
>>
>>    assert(arr.length == 2); // error!
>>
>>    wirteln(arr.length); // 5
>
> Yes. Such assertions should always be passing.

Shouldn't asserts be what you *expect* the value to be?  For example, in  
this case, a novice programmer may have written this code expecting that  
the value should be 2.  At least, that's what I was going for when I wrote  
an assert that I knew would throw an error.

I agree that proper documentation for the D spec or library modules should  
specify all passing asserts.  Especially when eventually these examples  
are used as unit tests.

In any case, I'm going to change it to writeln.  Thanks for all the  
opinions on this, it definitely helps to see what other people are  
thinking.

-Steve


More information about the Digitalmars-d mailing list