Program Structure (or layout)

New to FreeBASIC? Post your questions here.
dchapman
Posts: 19
Joined: Apr 01, 2010 9:44
Location: Dallas Texas

Program Structure (or layout)

Postby dchapman » Apr 02, 2010 14:10

Just doing a little reading in a tutorial I downloaded, and came across this.

FOUNDATIONAL PROGRAM LAYOUT
===========================

Fundamentally, this is the order you want your program's source code to be
layed out:

1 - Commented info like program author, contact e-mail, date of completion,
description, credits (where applicable) etc
2 - Declare all SUB and function modules
3 - Define all variables and arrays
4 - Set initial values for variables/arrays
5 - Initialization procedures to prepare program
6 - Main program loop and algorithm(s)
7 - Subs and function modules


It's only seven steps, but I was wondering if anyone has come up with a trick or a mnemonic for remembering the sequence of this basic structure?

<EDIT>

Also, I'm finding that 9 times out of 10, I'll use PRINT when I should be using INPUT. I don't know where I picked up the habit, but I rarely notice the error until it comes time to run it. Any suggestions on how to break this incredibly LAME habit?


Thanks,

~David
vdecampo
Posts: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Postby vdecampo » Apr 02, 2010 14:59

Hi dchapman!

Getting to know FreeBASICs OOP (Object Oriented Programming) features will pretty much satisfy the majority of your list there. Where you create an object you will...

2 - Declare all SUB and function modules
3 - Define all variables and arrays
4 - Set initial values for variables/arrays (Constructor)
5 - Initialization procedures to prepare program (Constructor)
7 - Subs and function modules (Part of your object)

The rest come into play when you make a program that implements your object. You will find that this method of programming makes a lot of sense for forcing structure, modularity, and coherence. The benefits are ease of debugging, modifying, and scalability.

There are other benefits but these are the most apparent.

-Vince
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Re: Program Structure (or layout)

Postby agamemnus » Apr 02, 2010 18:07

dchapman wrote:It's only seven steps, but I was wondering if anyone has come up with a trick or a mnemonic for remembering the sequence of this basic structure?

I dunno. I program incrementally. Start a core and expand at will. I will never know what variables and subs and UDTs are needed until the program is finished...


Also, I'm finding that 9 times out of 10, I'll use PRINT when I should be using INPUT. I don't know where I picked up the habit, but I rarely notice the error until it comes time to run it. Any suggestions on how to break this incredibly LAME habit?


Just practice and you'll snap out of it.
Ophelius
Posts: 428
Joined: Feb 26, 2006 1:57

Postby Ophelius » Apr 02, 2010 20:16

That's a good guide and my programs usually always end up looking that way, but don't follow it religiously. There's cases where I had small variations of that for specific reasons. Let your reason tell you how to structure it in the most optimal way.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Postby anonymous1337 » Apr 02, 2010 21:12

#1:

Code should be viewed as a form of problem solving. You map code in whatever manner best suits your solution. Of course, code tends to be algorithm-based, and algorithm-based solutions are often expressed as you described:

1. Summarize the problem and solution
2. If necessary, describe sub-methods
3, 4, 5. State and initialize variables
6 & 7. Algorithm

Any algorithm is describable in terms of a turning machine: http://en.wikipedia.org/wiki/Turing_machine And this is exactly how your code will run, and often how it is written.

Know, however, that your program structure must also cater to the compiler. Declarations come first because without them, single-pass compilers don't know anything about your functions/variables.

#2:

You can always #undefine print or #undefine whatever to make it uncallable :P
dchapman
Posts: 19
Joined: Apr 01, 2010 9:44
Location: Dallas Texas

Postby dchapman » Apr 03, 2010 19:52

Hey Vince, good to meet ya.

Getting to know FreeBASICs OOP (Object Oriented Programming) features will pretty much satisfy the majority of your list there. Where you create an object you will...


Ya, I'm going to hold off on learning about object oriented stuff for now--I'm having enough of a challenge just learning basic commands like input etc... but once I get a good handle on the basics (and have gone through ALL the beginner's tuts I can get my hands on) I'll take a look at the OOP side of things.

Hey agamemnus...

I dunno. I program incrementally. Start a core and expand at will. I will never know what variables and subs and UDTs are needed until the program is finished...


I think I pretty much understand what a variable is at this point, and I'm working on memorizing the different types of variables (!%#&$). Just now learning how to create my own type of variable--NEAT STUFF.

What's a UDT? My first guess is User-Defined-Something..., or am I completely off?

Hey Ophelius

Let your reason tell you how to structure it in the most optimal way.


Well, that's just it. I am COMPLETELY new to programming. I'm learning a lot of things at once. Commands, Loops, Variables... I don't really have any programming "reason" to fall back on. That's why I'm looking for something procedural that I can use as a guide until I develop those kind of instincts... which I'm sure only come with practice.

Pritchard--nice way to model the process, and great link on the turing machine. Wikipedia is so ADDICTING! :)

You can always #undefine print or #undefine whatever to make it uncallable :P


Thanks for that--I needed a good laugh today!

~David
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Postby KristopherWindsor » Apr 04, 2010 0:00

dchapman wrote:I think I pretty much understand what a variable is at this point, and I'm working on memorizing the different types of variables (!%#&$).


It looks like you're making a reference to QBasic there. ;)
Because in FB, and in most languages, those symbols aren't there.
If you use FB (without the -lang qbasic option), and declare your variables like "Dim x As String," then you can remove #3 from your list, and stop using those suffixes like "$" on your variable names.
You can remove #3 from the list because the compiler will tell you when you haven't declared a variable.
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Postby agamemnus » Apr 05, 2010 5:28

User-defined type, ie: your "own" variable. In non-basic languages, most people refer to them as referred to as "classes" or "structs". (I think those people are silly)

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 6 guests