The Journey of a New Computer Programmer

A longtime computer-literate who wants to learn "real" programming language(s).

Saturday, May 12, 2007

The progress of Naval Duels

Lee said he isn't much of a fan of copy/pasting code blocks with similar functions, but it appears that that's what I'm going to be doing...

Once I get the fighter- versus - fighter calculation working, I'm going copy/paste it, manually replace the appropriate appearances of 'fi' with 'ca', copy/paste that into the fi versus ca section of my program, then find/replace to turn 'ca' into 'su', copy/paste that in, etc.

Right now, I have a lot of 'blanks' in my program that indicate the structure I plan to use; I'll fill them in one section at a time. In a few situations, I can test it as is, to see if I even have the basic structure set up properly.

It's full of 'pass' statements right now; that's a fancy Python term for a placeholder. Pass statements don't do anything, but if the section of code needs to be filled up with *something*, you use them.

Well, when putting in Fleet A as 100 Fighters, and Fleet B, the program does get to the bottom of the function (at the bottom are 'if' statements that check to see if a particular side is out of ships, and thus, determine the winner). Since my program is able to get to the bottom of the function, where those statements are, that means it isn't getting stuck on an earlier part of the program, the basic structure of the calculation loop, the area that currently has 'pass'.

4 Comments:

At 3:19 PM, Blogger Lee said...

> Lee said he isn't much of a
> fan of copy/pasting code blocks
> with similar functions

Understatement of the year.

I guess you will just have to experience the pain of copy/paste for yourself before you will believe ;o)

 
At 5:54 PM, Blogger Alan said...

I'll see your 'Lee doesn't like copy/paste when writing code' and raise you a 'They're both Republicans, but Bush is no Lincoln'.

:D

 
At 5:57 PM, Blogger Alan said...

one method I've used when doing copy/pastes is:

copy/paste into new window.

I do the find/replaces I need to do only within that window, so the original code block doesn't need to be changed.

Then I copy/paste back into the main program.

Yeah, sooner or later, I'm gonna mess something up...

 
At 8:51 AM, Blogger Lee said...

The big problem is not that you will make a mistake while altering your pasted code -- although that is bound to happen. The big problem is that the code is impossible to maintain.

Imagine that every time you wanted to add 2 numbers, you had to paste in a big block of code ...

result, carry = nmbr1[0] || numbr2[0]
result, carry = nmbr1[1] || nmbr2[1] || carry
result, carry = nmbr1[2] || nmbr2[2] || carry
etc...

then you realize that your algorithm is flawed. You would have to grub through all of the code changing the code everywhere.

Why, oh why, couldn't there be a way to centralize that code in one place!

There is!

You factor that code out in to a function, and call the function from any place that needs to use the code. Now if there is a problem, you change the code in the function and instantly fix the problem in any and every place that calls the function.

 

Post a Comment

<< Home