Latest string_token Code

bearophile bearophileHUGS at lycos.com
Tue Jun 22 12:33:37 PDT 2010


Ben Hanson:

Even if you are an expert C++ programmer, I can express few more comments about your code, to show how to program in D (here I comment only the first example of each thing. More cases can follow).

------------------

You can write this:

template regex(CharT)
{
struct BasicStringToken
{

Like this:

template regex(CharT)
{
    struct BasicStringToken
    {

------------------

In this part:

    void negate()
    {
        CharT curr_char = START_CHAR;
        CharT[] temp;
        CharT *ptr = cast(CharT *) 0;


This is closer to normal D code, because in D there is "null", and in D the * of pointer definitions is better written on the left, because it's part of the type:

    void negate()
    {
        CharT curr_char = START_CHAR;
        CharT[] temp;
        CharT* ptr = null;


But the idiomatic D code is this because pointers are automatically initialized to null, and nornally in D variable names are camelCase with a starting lowercase (sometimes I'd like to use underscores, but this is the D style. Constant names can contain underscores in D, I presume):

    void negate()
    {
        CharT currChar = START_CHAR;
        CharT[] temp;
        CharT* ptr;

------------------

This line:
        
            else if (!overlap.charset.length == 0)

I don't like it a lot, maybe this is better:

            else if (overlap.charset.length)

------------------

This code:

        else if (negated)
        {
            intersectNegated(rhs, overlap);
        }
        else // negated == false
        {
            intersectCharset(rhs, overlap);
        }


There is no need of that comment, never add useless noise to the code:

        else if (negated)
        {
            intersectNegated(rhs, overlap);
        }
        else
        {
            intersectCharset(rhs, overlap);
        }

------------------

I see that in your code you lot of pointers. Pointers can be used in D, but I suggest you to use them only when necessary. Maybe some usage of pointers can be replaced by normal array indexes, that are safer too (because in D in nonrelease mode array bounds are tested).

For some situations I have created in D2 a "safer pointer" that in release mode is as efficient as a pointer but in nonrelease mode asserts if you make it step out of a pre-specified memory interval. I don't think lot of people here has appreciated it, but I have used it to catch some of my pointer-releated bugs.

Bye,
bearophile


More information about the Digitalmars-d mailing list