Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / How do you organize your code

Author
Message
Vandetta
23
Years of Service
User Offline
Joined: 17th Dec 2002
Location:
Posted: 19th Oct 2003 02:46
I was just wondering how all of you organize your code and projects. The main reason I've never been able to finish a project is mainly because all of them lack a good organizational structure. Once I get passed a few hundred lines I can never keep track of how this relates to that, why this does that, when that does this, but this was put in so that wouldnt need to be there anymore but when I take that out this doesnt work anymore even though they should be unrelated, etc. Any of you suffer from that syndrome?
CattleRustler
Retired Moderator
22
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 19th Oct 2003 03:09
the trick is good VARIABLE NAMING conventions, and breaking out your code into as many possible functions/subroutines - you want to have as little in-line code as possible! Hard-Code as little as possible.

This is short answer - I am sure many others will chime in with specifics....watch.

-RUST-
andrew11
23
Years of Service
User Offline
Joined: 23rd Feb 2003
Location: United States
Posted: 19th Oct 2003 03:16 Edited at: 19th Oct 2003 03:19
In the begining I just code everything down in one place. There's no apparent organization yet. Later, when code gets too hard to handle, I break it apart into files, then subroutines and functions. Just don't wait too long to seperate it or it will get too hard.

Also, I use types alot, because the are organized and take up less variable names.

"All programmers are playwrites and all computers are lousy actors" -Anon
Hamish McHaggis
23
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 19th Oct 2003 03:44
I organise mine in subroutines. I usually have for example with pong...

<setup, loading images etc>

do
gosub handlePaddles
gosub handleBall
gosub handleScore
gosub handleDisplay
loop

<subroutines here>

That way I can find parts of my code easily.

Brains are for idiots.

Athelon XP 1400 Plus - Nvidia Geforce MX400 - 256mb RAM
STE
22
Years of Service
User Offline
Joined: 7th Oct 2003
Location:
Posted: 19th Oct 2003 04:20
Ah, code organisation, an interesting subject.

Lots of companies have specific standards for this sort of thing, the last document I saw ran to 10 pages.

I use prefixes for all my variable names like this:
rem Variable prefixes are:
rem g = Global
rem b = boolean
rem f = float/real
rem i = integer
rem w = Word
rem dw = DWORD
rem y = byte

So "gfCameraPositionX" would store the cameras X axis location in space as a float and can be used from any part of the program because it is global.

After I have finished writing a small piece of code, I will ensure the variable names still relate to what they actually do.
I often spend a few minutes working out good names for variables and functions.
This helps LOADS.

I also comment like a mad-man. even if I understand what something does easily, I might not tomorrow.

I use loads of procedures and functions, and make code as modular as possible.
For example, my main loop currently looks something like this:



The light source code is just temporary, but I still commented it.

I also define all my initial variables in the same place, create my objects in the same place etc. so when I look for something I can find it easily.
You need to get a good mental picture of how the code is structured to work on it sucessfully.
For example, in the code above, only the "_PlayerShipMovement" procedure will ever move the main space ship, nothing else will, ever.

It's all very well knowing how to hack out a piece of code, but if it's written badly it will be worthless in a weeks time (this is especially true in a development environment where other people will use your code).

And while we are on the rough subject, make sure you backup your code.
I use a 3 day rotating offsite backup, which simply means 3 different directories that store the 3 last versions of my code on a website somewhere that only I have write access to.

STE ;¬!
KOL
22
Years of Service
User Offline
Joined: 10th Aug 2003
Location:
Posted: 19th Oct 2003 04:21
The Abstract Thing:
This is a thing that you will have to figure out as you go. I find it's like David Bowie... Re-invent yourself alot. Keep trying until you like your results. Sooner or later you will break some critical threshold and everything will be smooth as a tutle [ You will probably start working on code in your sleep at that point ]

The Specific Thing: [ My latest stage ]
* If you can't recall what a function or [ method ] is used for, name it something that it's related to [ ie if the majority of the code is related to matrix-stuff have "Matrix" in the name ]
* Microsoft had one good idea with naming conventions.... Name your functions by ObjectAction convetion [ ie MatrixSetup, MatrixDestroy, MatrixUpdate ] . Unfortunatly DBPro dosen't sort Function/Labels [ unless I've missed something somewhere ] so this doesn't help completely in DB's situation
* Comment Alot
* If you are looking to break your stuff into multiple files keep related stuff together
* KISS or "Keep It Simple [ Stupid ]"
KOL
22
Years of Service
User Offline
Joined: 10th Aug 2003
Location:
Posted: 19th Oct 2003 04:37
OOO, I almost forgot

Don't be so inflexible that you won't change tactics when needed [ IE, don't be afraid to throw away the book when the sweeping winds of change come, and they do blow often! - which blows! ]
David T
Retired Moderator
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 19th Oct 2003 14:38
Subs, Subs, Includes and more Subs is my method

Fishie says:
"Those who fail to prepare prepare to fail"
"Rules are for the guidance of the wise and the blind obdeniency of the fools"
Mussi
23
Years of Service
User Offline
Joined: 27th Jan 2003
Location: Netherlands
Posted: 19th Oct 2003 15:30
make a lot of reminders while you code, so when you later on look to your code you can read the reminders to see what did what again

Specs: AMD Athlon 1800, 256 DDRRam 266mhz, 80GB HD 7200rmp U133, Geforce 4 Ti4400 128mb ddr sdram
CattleRustler
Retired Moderator
22
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 19th Oct 2003 18:16
yes, Comment in your code heavily....

`\\Baddie movement routine
...code...
...code...
...code...
...code...

`\\Player movement
...code...
...code...
...code...

you get the idea.
And then there is Hungarian-Notation for variable naming...

intPlayerLives as Integer
fltXPosition as Float
strPlayerName as String

get it?

-RUST-
zircher
23
Years of Service
User Offline
Joined: 27th Dec 2002
Location: Oklahoma
Posted: 19th Oct 2003 20:33 Edited at: 19th Oct 2003 20:35
A word on commenting, when you look at a block a code ask yourself, "Will someone else understand this six months or a year from now?" If the answer is maybe or no, add some comments. That person six months or a year from now will most likely be you thanking yourself for adding the comments when it was fresh in your memory.

I'm also a fanatic about indentation, in a deeply nested loop of conditional statements, indentation can save your sanity.

In addition to the various variable prefixes that have been mentioned, I like to place some logic to my variable names. For example, if the status of a game has changed from its initial load condition, I set a variable called 'isDirty'. The 'is' serves the same function as the b for boolean. When I write my code, it looks something like this...

if isDirty
` ask if we want to save before exit
else
` clean up variables and exit without question
endif

It's a style thing, but to me adding 'is' or 'has' to a boolean variable makes the variable self commenting in my mind.
--
TAZ
zircher
23
Years of Service
User Offline
Joined: 27th Dec 2002
Location: Oklahoma
Posted: 19th Oct 2003 20:44
As a DBP specific thing, I prefer functions to gosub statements. Sure, it means more global variable declarations than I like. But, the alternative is that everything is a global variable if you use gosubs only.

I also like the function side bar in the IDE, it allows me to easily navigate through my code. And while it does not automatically sort for you, it is fairly simple to cut and paste your functions into any sort of order that you desire. In some ways, that's better than automatic sorting since you can group by functionality and not the alphabet.
--
TAZ
QuothTheRaven
23
Years of Service
User Offline
Joined: 2nd Oct 2002
Location: United States
Posted: 19th Oct 2003 21:31
I never used to think subroutines and functions were useful. To me, it just seemed like a function or subroutine was adding more code than needed, instead of doing something in the the main block you have to go somewhere else to do it, to me that seemed not needed.

But now I'm taking a Java programming course, and I have to say, subs and functions are just what the doctor ordered. They organize, they help you pick out problems, they seperate your code, it makes it much easier to look at; they're great!

CattleRustler
Retired Moderator
22
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 19th Oct 2003 21:35
nice QuothThe Raven! Cheers and Kudos! What you just said makes my OOP toes tingle!

-RUST-
M00NSHiNE
22
Years of Service
User Offline
Joined: 4th Aug 2003
Location: England, UK
Posted: 19th Oct 2003 21:41
Use subs to structure your main loop, eg

do
gosub PlayerMove
gosub EnemyMove
gosub ControlPlayerBullets
gosub ControlEnemyBullets
gosub UpdatePositions

loop

Then, if your player is moving like a tortoise and turning like a helicopter, you can go straight to your PlayerMove sub and start looking there for your bugs.

"It's amazin' what you can do with a computer and access to t'internet"
Vandetta
23
Years of Service
User Offline
Joined: 17th Dec 2002
Location:
Posted: 19th Oct 2003 23:00
Thanks everyone, there's lots of good information in here.
orv
22
Years of Service
User Offline
Joined: 9th Sep 2003
Location: Chicago, IL
Posted: 20th Oct 2003 02:22
speaking of loops, and statements that have an opening statement and a closing statement, type the closing statement in IMMEDIATELY after typing in the closing statement and then go back an fill in the stuff in between.

Example: FOR X=1 TO 10 is the opening statement. Immediately put in the NEXT X after it.

Why? If you do this, it will keep you from not having matching statments and getting errors and wondering where the heck you screwed up. A C programmer taught me this one many years ago and its saved me many headaches since!

Also, remark at the end of closing statement to give you a clue which opening statment it goes with.

Like was said above, indent code. This makes nested stuff Sooo much easier to read.

If you aim at nothing you'll hit it every time. 806mhz AMD Athlon Processor. 384MB memory.
Windows XP Home Edition. RADEON 9700 Pro graphics card. - editor.exe=1.0.6.1
CattleRustler
Retired Moderator
22
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 20th Oct 2003 02:45
indentation makes things so much more readable.

-RUST-
Rob K
Retired Moderator
23
Years of Service
User Offline
Joined: 10th Sep 2002
Location: Surrey, United Kingdom
Posted: 20th Oct 2003 06:01
I think I might email this thread to Lee. Looking through the examples, it is very easy to get confused over what code is going what media has been loaded in which "slot".

Ian T
23
Years of Service
User Offline
Joined: 12th Sep 2002
Location: Around
Posted: 20th Oct 2003 18:38
Keep a design document!

I have a document that clearly lists object and textures indeces (indexes) so:

Static Ground Objects; 999
Player Object- 1000
Sky Objects- 1001-1050
etc

If you find yourself looking for anything frequently in the code, right it down in a seperate document that you can access quickly.

--Mouse: Famous (Avatarless) Fighting Furball

A very nice %it, indeed.
Beta 1
22
Years of Service
User Offline
Joined: 27th Aug 2003
Location:
Posted: 20th Oct 2003 19:13
Great thread guys.

I'm getting very fussy about my variable names. Especially of the code if part of a "module" that I intend to use in multiple programs.

Currently everything not game specific gets prefixed

EG

m_ = variable from the matrix routines
p_ = physics engine
t_ = dynamic terrain stuff

I also like types to make variables readable.

eg m_terrainobject(N).pos.y

Fairly self explanatory - contains the y world position of secenery object N

Types are also good for handling position/speed/acceleration type maths

My object(N) type will include a pos.x/y/z vel.x/y/z and acc.x/y/z

Each time through the loop I calculate total forces and put in acc.x/y/z then add those to vel.x/y/z and then add those to pos.x/y/z and update object Ns position.

That would require a lot more variables or a multidimensional array which would be a lot less readable.
Surreal Studio IanG
22
Years of Service
User Offline
Joined: 21st Sep 2003
Location: Cyberspace...I think
Posted: 25th Oct 2003 16:07
gosubs functions gosubs and well functions
thats what i use
xtom
23
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Ireland
Posted: 25th Oct 2003 17:10
Aswell as closing loops imediately and filling in the stuff later I do the same with quotes for strings and for brackets in commands aswell. Reduces chances of a mistake I think.
Codger
23
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 25th Oct 2003 21:24 Edited at: 25th Oct 2003 21:36
Outline 1st then fill in
Top Down Programming using Functions rem create empty funtions so that program compiles , fill in when at that point

Gosubs only used as Labels to organize set up procedures and data

For example a card game such as Blackjack

The code is a complete program waiting for the simple task of adding the code. Once you have outlined the tasks for most the code is comparatively simple



System
PIII 650 MZ H.P. Pavillion
394 Mem GeForce 4 400MX
WKS Controls
22
Years of Service
User Offline
Joined: 25th Oct 2003
Location: Mansfield Texas
Posted: 25th Oct 2003 22:20
Vandetta
I understand were your coming from so I develope a template to help me organize my code and projects. I also had the problem of not finishing a project and forgetting were I was in the code and development because of the lack a good organizational structure.

I hope this helps and if you send me your email address to [email protected] I have a zip file ready with the Dlls, listing, souvce and project ready for you. This code is project I just finish for a friend who had a problem call Windows 2000 dlls calls.

Walter of WKS Controls / Mansfield TX
WKS Controls
22
Years of Service
User Offline
Joined: 25th Oct 2003
Location: Mansfield Texas
Posted: 25th Oct 2003 22:23
[email]Vandetta
Sorry this is the first post I done belive code will be in this one

Walter of WKS Controls / Mansfield TX
Jess T
Retired Moderator
22
Years of Service
User Offline
Joined: 20th Sep 2003
Location: Over There... Kablam!
Posted: 26th Oct 2003 09:42
I find that when i have an idea, but am not too sure how to impliment it, or have to ask a question about it, that Rem'ing in the idea, then actualy coding it later helps you remember your idea a few days later so you can keep track of your creativeness....

E.g.


This would then later be:



Team EOD :: Programmer/Logical Engineer/All-Round Nice Guy
Critters
23
Years of Service
User Offline
Joined: 11th Apr 2003
Location: United Kingdom
Posted: 26th Oct 2003 13:52
gosubs usually work for me. Without organisation, my code would be very messy indeed.

- My online portfolio/under construction
- The Media Base/under construction
Genesis Rage
22
Years of Service
User Offline
Joined: 5th Oct 2003
Location:
Posted: 26th Oct 2003 18:25
my trick is put all DIM's GLOBAL's ARRAY's and main stuff at the top... break it down into SUBROUTINES... lots of REM statements to tell me what does what... and LOTS of spaces...

i made a simple text program that was 320 lines... after i took out REM and the extra spaces... it was only 102 lines! but it was organized!

==Main Computer==
Athlon XP 3000+, 1024mb, Radeon 9800 Pro 128mb 8xAGP, XP Pro
Genesis Rage
22
Years of Service
User Offline
Joined: 5th Oct 2003
Location:
Posted: 26th Oct 2003 18:34
another thing i almost forgot about... DBP has that TODO LIST in the side-bar... if you end up stopping and know you have to work on 'PlayerDieScene' for example... put it in the to-do list... mabee add a line number or something to help you out...

==Main Computer==
Athlon XP 3000+, 1024mb, Radeon 9800 Pro 128mb 8xAGP, XP Pro
B1aze
22
Years of Service
User Offline
Joined: 1st Nov 2003
Location:
Posted: 1st Nov 2003 17:21
Okay first post here hehe... I was also wondering about the code structuring in DBP. I'm a student (second year computer sience) and have 'some' experience with Java. With Java you hava some very nice ways to structure your code(ie. classes, methods, interfaces, inheretance...) and to save a lot of hadaches in the long run. Are there more ways to orginize your code in DBP apart from subroutines and functions?

I read in the above posts that ppl in DBP need(?) to use alot of global variables. I hope I'm wrong about that because if something goes wrong in your game/program, (a global variable has the value it shouldn't have) it is somewhere in your code but you will have a hard time finding it! If you program properly in Java there is an easy way to avoid this. Make most (better is all) variables private (not global) and use 'set' public (global) methods (subroutines)(ie. public void setX(int x)). Checks like x must be greater than 0 are done in this set method. So whatever you do x will always be greater than 0.
CattleRustler
Retired Moderator
22
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 1st Nov 2003 19:20 Edited at: 1st Nov 2003 19:21
DBP is a form of Procedural Basic, not OOP. You use mostly global variables I am afraid. And when I say Global I mean regular variables aside from the ones that are Explicitly set Global

if you define: strMyVar as String
then the whole code module can "see" it including subroutines

if you define: Global g_strMyVar as String the whole code module can see it, including subs AND from Within a function

Local L_strMyVar as String is used for a var defined in a function and is local to the function - going out of scope when the function ends

Also the function can return any value, reg global , local or explicit Global - which is wierd but whatever.

DBP scope of variables is a little behind the times or the norm but it is ok when you get used to it - be careful. Oh, yeah and for good measure they left out any form of Option Explicit meaning variable mispellings in your code are not flagged and will compile, but treat the mispelled variable as a new variable with a value of zero - how nice.

-RUST-
B1aze
22
Years of Service
User Offline
Joined: 1st Nov 2003
Location:
Posted: 1st Nov 2003 19:27
Ow too bad. If I tried DB(P) 1 1/2 year ago I'm sure I would really liked it but now I like OOP languages hehe... Oh well maybe I can find something else.
CattleRustler
Retired Moderator
22
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 1st Nov 2003 19:43 Edited at: 1st Nov 2003 19:43
Blaze not so quick!
Yes I am all about the oop but trust me, dbp is sweet - give a good try, like a full devoted month at least, then decide. If you quit now you'd be missing out.

If you do decide to leave dbp then you might want to give TrueVision3D 6 a look. Search for their site and read up, I have it for vb and is pretty sweet.

-RUST-
B1aze
22
Years of Service
User Offline
Joined: 1st Nov 2003
Location:
Posted: 1st Nov 2003 22:07
I dunno about DBP I think basic is great for small apps/games but I feel like I would lose control when I want something large/huge (certainly if there is no way to protect you variables). But is it still possible to make different modules?(spread your code over multiple files). I give the trial version a try heh. I know a little bit about Truevsion3D as soon as I have .net installed I try that out hehe (it's also completely free unless you want to sell something with it).
CattleRustler
Retired Moderator
22
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 1st Nov 2003 22:50
yes you can use modules of code, so that helps.
I am running vb.net with truevision and all is well, TV3D are COM objects so they are getting wrapped in interop. you can use it from vb6 or net if you like.

tv3d is free unless you wanna sell, and also, it puts their logo in the lower right corner of everything you make. I haven't written anything in it yet, but have been browsing/running the tut's

lemmee know what you think of dbp when you get it - there is plenty of help and advice on the boards so if you get hemmed up just post up here and someone will definitely help you out.

-RUST-

Login to post a reply

Server time is: 2026-07-26 08:30:50
Your offset time is: 2026-07-26 08:30:50