caustik's blog

programming and music

Archive for April, 2010

Prettier C/C++ code using explicit scope

with 3 comments

Probably the single most impactful epiphony I’ve had in my years of programming in C/C++ is discovering the ability to provide explicit scope inside a function. For snippets of code that are conceptually self-contained, but only used once in the program, they improve readability, provide optimization hints to the compiler, and are just kind of cool =)

Here’s some “before” code, like you’d expect to see throughout C source code:

int func1(int param1, int param2)
{
    int ret = GREAT_SUCCESS;
    int tmp1, tmp2;
    structType struct1;
    otherStructType struct2;

    struct1.val1 = param1;
    struct1.val2 = 20;

    /*! do something */
    ret = func2(&struct1);

    if(FAILED(ret)) { return ret; }

    struct2.whatever = param2;
    struct2.somethingElse = 4;

    /*! do something else */
    ret = func2(&struct2);

    if(FAILED(ret)) { return ret; }

    return ret;
}

The variables are declared at the top of the function, the parameters for each function call
are determined, the struct parameter is filled out, return code checked.

Compare to using explicit scope:

int func1(int param1, int param2)
{
    int ret = GREAT_SUCCESS;

    /*! do something */
    {
        structType struct1;

        struct1.val1 = param1;
        struct1.val2 = 20;

        ret = func2(&struct1);

        if(FAILED(ret)) { return ret; }
    }

    /*! do something else */
    {
        otherStructType struct2;

        struct2.whatever = param2;
        struct2.somethingElse = 4;

        ret = func2(&struct2);

        if(FAILED(ret)) { return ret; }
    }

    return ret;
}

What this has accomplished is your eyes immediately see that “do something” and “do something” else
are functional blocks, which you can quickly read as independent units. In addition, you can very
easily tell that the function simply “does something”, then “does something else”, then returns the result.

Each functional block has it’s variables declared only inside it’s explicit scope. So you don’t have this huge block of declarations at the top of your function, making things much easier to read. Also, the compiler now knows without ambiguity which variables are used where, which can help optimize it’s memory usage (e.g. it can re-use stack space instead of reserving it all at the top).

To me, the difference is night and day – and this is only a really simple example. Try it out on some
more complex code, and watch your code quality/readability improve. This works in both C and C++, it’s
particularly great for C since you normally are forced to declare your variables at the top of the function.
When you explicitly declare your scope, this is no longer the case! Really, C just makes you declare at the
start of {} blocks.

Written by caustik

April 12th, 2010 at 12:37 am

Posted in Programming

Tagged with

April 2010 Progressive Trance Mix

with 3 comments

New trance mix, pulled from the current top 100 from trackitdown.net – mastered using Har-Bal, DensitymkII, iZotope, Elephant

Written by caustik

April 11th, 2010 at 3:43 am

Parallel Intervals 001

with 4 comments

Relaxed style progressive breaks mix, using a new track selection technique

(here comes a music theory rant …)

Each track in this mix is carefully selected so that each track key+mode is exactly a perfect fourth interval from the previous track. The result is highly consonant transitions, causing the tracks to blend melodically, and causing a consistent feeling of progression.

More detail for anybody interested:

Let’s say track 1 is in the key “A minor phrygian” … meaning the root note is A and the scale intervals are H-W-W-W-H-W-W

The following track would then be selected as 5 semitones above (in other words, 7 semitones below) the note ‘A’, and would be selected to share the same mode “phrygian”. The result, since A+5 = D (A-7 = D), would be “D minor phrygian”, meaning the root is D and the scale intervals are still H-W-W-W-H-W-W.

This means the root notes are a perfect fourth interval apart from one another, as are each of the other 6 notes in the scale. So, not only do you have melodic interplay between root notes, but all notes on the scale.

=)

Written by caustik

April 7th, 2010 at 2:33 am