Tag Archives: scripting

Dear 16 year old me

Dear 16 year old me,

As part of my quest of porting over your old Microbee computer game  ‘Adventureworld‘ that you wrote back in 1990, I’ve been studying the code and trying to work out what the 16 year old me  was thinking at the time.

I realise that that you believed that this was your first real big ‘awesome’ application, but seriously you really need to learn some standards NOW before you get yourself into real trouble.

Here are some comments of your coding standards and what you need to do to improve yourself if the 16 year old me has any hope of coding in the future. These guidelines will also make it easier for your future self (and others) to read your code 20 years later. Trust me, you really need to do this!

  • Please don’t use the variable name ‘O’ for anything. Ever! I mean seriously don’t do it! Imaging having to read this line in the future and trying to work out what it means:
    IFO=8ORO=127THEN364ELSEIFO=10ORO=24ORO=27THEN365ELSEIFO=13ORO=11THEN367

    This is especially important when you are mixing up the letter O with the number 0 . Combine that challenge with a dot matrix printout of your source code where the zeros aren’t slashed,  it will create extra headache for your future self. O, 0 look the same. Dear 16 year old me don’t do it! Don’t even get me started with the variable you used called O0. It’s just madness!

  • Please put a little more thought into your variable names. Granted that 16 year old me is just learning to code, and the basic language was rather limited, but seriously if you are going to be using x,y coordinates then maybe you should use variable names such as X and Y. Don’t use B and C (where C = X coord and B = Y coord).
  • Please put some more thought into the structure of your code. The GOTO command especially is evil, Pure evil. It does have its place though – just not here. As a example, don’t do this
    22 GOSUB23:GOTO25:GOTO99
    23 ...do stuff...
    24 RETURN
    25 ...program continues...
    ...
    99 ..do some stuff..:GOTO22
    

    You are looping all over the place.  Don’t embed your subroutines within your code, and try and bring all your code together in the one place.

I’m sure if the 16 year old me followed these guidelines it will make you a better programmer, and your future self would be able to port and understand your game a lot easier. Get started on it now! OK?

Cheers,

– Future Phil.

Note: You will be pleased to know that I have matured as a programmer since the 16 year old me started coding.

Powershell garbage collection

Today one of my scripts at work was consuming lots of memory. Turns out that Powershell doesn’t handle garbage collection very well, it only runs on the completion of a script, however if you have a script that is in a continuous loop, garbage collection will not happen. Here is a handy hint to anyone writing scripts that don’t quit on how to force Powershell garbage collection.

powershell garbage collection

Simply add the following line of code in your ‘leaking’ foreach or while loop

while(…) {

[System.GC]::Collect()

}

I have to give credit to Dmitry Sotnikov as he stumbled on this issue a few months ago and solved it.

Once I had done this, my script was no longer consuming lots of memory. Win!