How Far One Has Come...

Pages: 12
Has anyone had any experiences where they'd go back to old programs they wrote and admire how much they improved in some way? Alternatively, has anyone had any experiences where they'd go back to old programs they wrote and think "Oh gosh, what a idiot I was back then. If I was to do this now, I'd..." and so on? If so, I thought it'd be neat if we could share them here.

Backstory.

So as anyone who checks my profile on a regular basis knows, I'm programming a virtual instrument. I managed to get the program to its first milestone a couple of months ago. Since then, most of my time has been spent writing a MIDI (.mid) file parser. After spending nearly two months on that one part of the program alone my coding style had changed quite a bit. I began to spend more time analyzing my own code between compilations looking for errors myself, I didn't feel as guilty as I used to about declaring new types, I began to make a much more liberal use of enumerations, and overall my coding style changed quite a bit. I think it's for the better.

Partway though writing the MIDI file parser, I had an idea to add another type of sound wave to my program: an oval wave. I figured it'd add a rather interesting sound.

The part of the synthesizer that handles simple periodic waves is (if I may say so myself) very well-written. It took me about 3 minutes to add the wave into the system and add a user interface button so that one could easily select it. However, after testing the wave, I noticed a significant bug in the development branch. I searched around, and realized that it was in the class I wrote to composite samples and to manage the sound generators and effects. Part of the "management" duty of that class is to record and relay note on/off events to the sound generators. That apparently was where the problem lay. I opened the implementation file for the class, and literally my first thought was "Who in the nine hells wrote this class? It definitely wasn't me. It must've been the gremlin I saw hanging around my porch the other night."

Although I did keep documentation for the class, it did suffer from some readability issues. As an example, here's one of the member functions from the class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
     void Synth::noteOn(int halfsteps, sbSample amp) {
        if (amp <= 0.0)
            return;
        for (size_t i = 0; i < notes.size(); ++i) {
            if (notes[i].second.first == 0.0) {
                notes[i].first = halfsteps;
                notes[i].second.first = amp;
                if(holdped && !notes[i].second.second)
                    notes[i].second.second = 'h';
                else
                    notes[i].second.second = false;
                for (size_t ation = 0; ation < channelcount; ++ation) {
                    if (gener[ation] != NULL) //Older/cynical programmers: This does NOT unconditionally evaluate to false.
                        gener[ation]->noteOn(halfsteps,amp,i);
                }
                break;
            }
        }
    }


Gruesome, isn't it? Without the documentation, I myself wouldn't remember what holds what.

So I spent some time changing that class to better fit my new coding style. Here's how that function looks now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    void Synth::noteOn(int halfsteps, sbSample amp) {
        if (amp <= sbSampleZero)
            return;
        for (size_t i = 0; i < notes.size(); ++i) {
            if (notes[i].amp == sbSampleZero) {
                notes[i].noteoffset = halfsteps;
                notes[i].amp = amp;
                if(holdped && notes[i].pedal == NoPedal)
                    notes[i].pedal = Hold;
                else
                    notes[i].pedal = NoPedal;
                for (size_t ation = 0; ation < channelcount; ++ation) {
                    if (gener[ation] != nullptr)
                        gener[ation]->noteOn(halfsteps,amp,i);
                }
                break;
            }
        }
    }


Much better, no? :)

-Albatross
Last edited on
I love looking back on old code and going "Who in their right mind...?"
Must say yes, definitely much better. I don't know much about sound theory but I'm pretty sure notes shouldn't have a second.first.
My least proud moment in my sad history of programming was looking back on a program I wrote a year or two before and realizing I didn't understand that pointers could be directly assigned to pointers, so I thought I would have to be "clever" and write it as ptr1 = &(*ptr2);. Not so much ugly as just stupid. Otherwise, I don't have the source code to a lot of my old programs, but I'm sure I would find lots of silly faults.
Albatross wrote:
and think "Oh gosh, what a idiot I was back then. If I was to do this now, I'd..." and so on?

Yep, all the time. Although once or twice I've looked at old code and been surprised that I thought of doing something in a certain way.
Well, I don't, but that's mostly due to the fact that I didn't consider any of my projects so far particularly good, so I haven't kept any of them.
I was much imaginative and passionate a while ago (some 5-6 years). It doesn't bother me that
i.)I used to write a function myself for searching a string within a string.
ii.) Used to delete an element of the array and the shift all other remaining elements one step back
iii.) Used to sort elements in an array my way (selection sort)
iv.) Used to use a hell lot of gotos
v.) Used to loop using if..and goto instead of for/while loop.
vi.) Used to write code that no one but me would understand

Point is, I was imaginative (most people say I'm still, but I think otherwise)

My coding style has certainly improved now.
i.)I now use strstr.
ii.) I mostly use vector nowadays, so I just erase the elements.
iii.) sort()
iv.) I have gotten into the good habit of eliminating gotos.
... and.. you get my point


and most importantly, I now know C/C++ (I used Visual Basic previously). I have gained experience and knowledge, but as I said, I've lost a little passion and imagination over the course of time. Its my personal theory that children are more intelligent and creative the grown ups. So I don't feel particularly bad.

So, when I look back, I don't know. I kinda have a mixed feeling about it. Both happy (that I've learned few good things) and a little sad (coz, I don't have that kind of passion nowadays)..
I think the word you're looking for isn't passion, just being a masochist.
ascii wrote:

I think the word you're looking for isn't passion, just being a masochist.


pardon..
You described things that would inevitably lead to poor performance and obfuscated code, then said that you loved them. Sounds like you are a) nostalgic about your childhood (and you did say in your past you love the creativity of children) or b) love working through confusing code that sounds frustrating and would make most people angry. I assumed b.

I was joking, so let's just stay on topic.
You described things that would inevitably lead to poor performance and obfuscated code

I described things that I'd invented myself. For instance, I knew ifs and gotos. I invented looping myself. Frankly, I never understood for loops until recently(that would be some 2-3 years ago). And yes it does lead to poor performance. I used them only because I didn't know about sophisticated methods.

then said that you loved them


I loved my creativity. I personally believe that what matters most for a programmer is not knowledge, and not even experience (not that they don't matter at all). What matters is his ability to come up with new ideas.

Sounds like you are a) nostalgic about your childhood (and you did say in your past you love the creativity of children)

yup!

b) love working through confusing code that sounds frustrating and would make most people angry

nope. But I do use goto sometimes and that does make my instructor mad..

I said it was a joke, and I said let's stay on topic. You seemed to ignore both of those statements, and felt a need to defend yourself. So let's forget this silly argument, move on, and not ruin Albatross's thread.
sure..
closed account (S6k9GNh0)
I do that on this forum quite a bit, looking back at some old posts and code I put down. I remember just yesterday looking back at the beginner challenges I did awhile back. I don't think I even finished them but I do remember they motivated me to change my coding style and improved my way of thinking a great ordeal just because of the experience it gave me.

I'd say the only downside to becoming more experienced is I become more picky and people get pissed off when I point out little things.

Also, I have a thing where it's vice versa and I can admire code from a long time ago! I think the linked list I posted up on the Articles is still pretty amazing for me for doing it in just under 10 minutes.

Sometimes I have confidence issues on what I can and cannot do. I'll often say I can't do something even though I've done it quite well in the past. Looking back at old jobs and code is a really good reminder for me on what I'm capable of.
Last edited on
When I was quite young (think 10~11), I got a bit into html & php. One of the first things I made was a quiz page. The score display code looked like this (pseudo):
1
2
3
4
if (score == 0) echo 0;
else if (score == 1) echo 1;
....
else echo 10;


A few months later I revisited that code, laughed at my own childish ignorance, and swiftly replaced the monstrosity with the much sleaker version:
1
2
3
4
5
switch(score) {
case 0: echo 0;
case 1: echo 1;
...
}


I was pretty stupid as a kid.

Not much has changed.
Just because we don't have the rest of the code I'd like to ask you about Line 8: if(holdped && notes[i] == NoPedal) Is that what you really meant to do? Am I the only one having a bit of trouble reading this because of the recursion?

As far as going back to old code, I'm almost at the point where I can't even look at stuff I did more then a year ago. It's more then me just using "goto" and the never ending if(...) else if(...) else if(...) else(...) tree's, I literally can't follow it because of the way I named the variables (example: int a, b, c = 0;) and the fact that I thought "\\Pay Attention A-$H--E" was an adequate comment.
Whoops. I thought I posted the correct version. Thanks for noticing; the code should be correct now.

-Albatross
Last edited on
Gaminic wrote:
A few months later I revisited that code, laughed at my own childish ignorance, and swiftly replaced the monstrosity with the much sleaker version:


Why not just echo score; ?
Last edited on
@Disch,
That's the point. I guess he thought echo repeated whatever you put next to it verbatim. Apparently it's a common mistake.
Disch wrote:
Why not just echo score; ?

Congratulations! Your tests came back negative, for sense of humor.

Back on topic: I'm still in my programming infancy, so unfortunately I cannot contribute juicy code examples.
I can say, however, that I cut down on the curly braces since I learned they're not always needed.
I can say, however, that I cut down on the curly braces since I learned they're not always needed.


In C/C++ that's not really a good thing.

Congratulations! Your tests came back negative, for sense of humor.


Why? It's a legitimate question.
Pages: 12