Thursday, December 06, 2012

Declare the Variables First

Back in the early days of programming one pretty much had to declare ones variables before writing any useful code. COBOL had (probably still has) a special section where all the variables were declared. Early versions of FORTRAN were pretty picky about where variables were declared. One didn’t see languages that used late binding or made assumptions about undeclared variables based on usage. No, one had to be very explicit about such things. While it could be a pain in the neck it at least forced the programmer to think things out before laying other code.

Recently I was writing a simple little program. Honestly I don’t even remember what it was as it was a throw away for a specific task. But half way though I realized I needed a couple more variables. While this is/was no big deal in the scheme of things it made me realize that I had started writing my code without completely thinking things though. This bothered me because it made me wonder what else I had forgotten to take into consideration.

Beginners are even worse at this and will willy nilly add new variables at all sorts of “interesting” places. The compilers don’t care so students tend to think it doesn’t matter. Often it doesn’t. That is to say where the declarations are made doesn’t matter. Not much anyway. It tends to make One runs into the occasional scope issue as well. This doesn’t mean that all variables have to be declared at the beginning of the program as they once did. But it does mean that some thought should go into what variables are declared and where they are declared.

In the sort of small programs that students write I have wondered if we should require students to hand in a variable list before they start writing code.  I think most students would try to write the code first and hand in the list later and I am not sure how to prevent that. Still it would be an interesting experiment.

The sneaky part of this is that students would have to have an algorithm in mind to create the list. They would have to think about what I used to call the goesinta* (information that goes in to the program) and the goesouta (information that comes out of the program) and and intermediate variables needed for the calculations.  They'd have to give some thought to what data types the variables would be, what naming convention they would use, and verify that they had all they needed to solve the problem.

Would students come up with perfect lists? Probably not but that is not really the point. Rather the point is to make sure they think about what they are doing before they start doing it. It might be worth a try. What do you think? Silly suggestion? Something you have tried or might try? What do you do to make sure students think before coding?

 

* I’m from Brooklyn where they talk like that. Fahgetaboutit!  Smile

1 comment:

Free Computer Education said...


Superb blog..nice, neat, very informative...
Thank you so much. I really enjoyed and hope you find it useful. Thank you for linking to it!