Notes

 

Walking backwards in Arrays

 

Walking arrays backwards are useful for all-sorts of things such as removing an item from an array.

 

Three main ways to do this, take your pick.

 

1.

            for ( int n = max; n>0; )

                        A[--n];

 

2.

            for ( int n = max-1; n>=0; n-- )

                        A[n];

 

3.

            for ( int n = max; --n>0; )

                        A[n];

 

Which is preferable? Well that’s really up to your style of coding and what you are doing.

 

1 has extra possibilities. If you add brackets you can write code before and after n-1.

            for ( int n = max; n>0; )

            {

                        //Code before n-1

                        A[--n];

                        //Code after n-1

            }

 

If you’re a classic stick-to-the-format type of person then 2 is probably your cup-of-tea. It keeps the loop in its general format and there’s no dirty pre-logic stuff.

 

If you’re a keyboard-speed-freak then 3 is probably your motor car. It has slightly less code then the other two.

 

 

Joel Anderson

 

D arrays can be sorted

 

Forget loops, D has sorting as one of an array’s attributes.           

 

This will sort the array A in-place.

 

            int[] A;

            ...

            A.sort;

 

 

Joel Anderson

 

D arrays can be resized on the fly

 

Forget realloc, D can resize array’s dynamically.

 

This will resize the array A by one.

 

            int[] A;

            ...

       A.length++;

 

Joel Anderson

 

Comparing arrays

D allows you to quickly compare arrays with “==”.

 

            int[] A;

            int[] B;

            ...

            if (A == B) //Then both are equal in length and items.

 

But don’t worry referential testing is still supported with “===”

 

            if (A === B) //Then both references are equal.

 

Joel Anderson

 

 

HTML Imbedded code

D can automatically extract code from html pages and compile it like normal code.  What are the advantages of this? Apart from beginning able to keep code with documentation future D IDE’s may be able to use this feature for automatic syntax highlighting and object link embedding.

 

Joel Anderson

 

Notes

If you have any notes you would like to share, email Mathew Wilson dmd@synesis.com.au or Joel Anderson anderson@firestar.com.au. They must be between one-sentence => two-medium-paragraphs and/or small snippets of code. Otherwise they’ll be considered for the hints and tips section for the Journal.

 

Joel Anderson

 

High Cohesion and Low Coupling

 

Programs should be lowly coupled and highly cohesive. Why? What does this mean?

 

Coupling

Programs are highly coupled when they are heavily reliant on one another. This is considered NOT A GOOD THING. For example a car that only uses one type of tyre and that tyre only being useable on that car. In this case the tyre is not reusable. You can’t very well take the tyre and use it on another car. High coupling make programs less reusable.

 

Cohesion

Cohesion is the way things work together. Take the car example again. Suppose the tyre was so generalised that it even works on your-next-door-neighbours billycart. The car may be reduced a maximum of 10km/h. Low cohesion makes components less flexible/efficient together.

 

It’s often difficult to get the balance just right between these two concepts but just it in the back of your mind and your programs will be all the better. Remember, high cohesion and low coupling = good thing but low cohesion and high coupling = bad thing.

 

Joel Anderson

 

 

Speed Optimisation with For Loops

 

When using for loops (or any loop for that matter) make sure that you’re not reloading the same value every time around the loop.

 

Don’t

for (int n=0; n < getSizeof(X); n++)

{

}

//Where getSizeof returns a constant value

 

Instead,

int Size = getSizeof(X);

for (int n=0; n < Size; n++)

{

}

 

That way your not calling the fucntion getSizeof(), getSizeof(X) times.

 

Joel Anderson

 

Speed Optimisation for linear searches

 

The basic search looks like.

 

int n=0;

while (n<Max)

{

            if (A[n]==Search)

            {

                        //Value found at n

                        break;

            }

            n++;

}

 

 

//Where A is the value we are searching for

 

This can be improved to…

 

int n=0;

while (n<Max && A[n++]!=Search);

 

if (n != Max) {} //Then the value is found at n-1

 

 

A comparison can be saved by making the last character in the array the value being searched for.

           

A[A.length++] = Search;

 

int n=0;

while (A[n++]!=Search);

 

if (n != Max) {} //Then the value is found at n-1

 

A.length--;

 

 

Joel Anderson

 

D newsgroup

Did you know that there is a D newsgroup on news.digitalmars.com server? Here you can ask/suggest anything about D and even talk to the creator Walter.

 

Joel Anderson