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 / Programming tips..

Author
Message
Matto
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location:
Posted: 5th Dec 2002 18:08
I am wondering if anyone knows if some set ways of programming are better than others, by this I mean the lay out of the code in which order or what commands are best for different things, I find I use alot of lables and for/next loops, do/loops and have a very spaghetti like code thats jumping all over the place, I also note that some peolple have said it is not good to rely on lables and should use other ways to do things instead?? but I always end up on lables as they fit what I do or how I do things better.

I was wondering if anyone well endowed with programming skills or layout of code knowhow would like to add comments on better coding practice below here.. I am sure I could use a few tips as also I am sure others around here may benefit too.

thanks in advance.
1ghz Cel,512 sdram 133,Gf2 MX 400 64,SBL 5.1,Win98
Best Upcoming MMOG - http://WWW.Atriarch.com
Project: Card Game with No Name
ZomBfied
21
Years of Service
User Offline
Joined: 2nd Oct 2002
Location:
Posted: 5th Dec 2002 18:30
I believe a good db program should look like this.

Setup:
(setup stuff)
(variable data declarations)
(reading constants etc)

Main_Loop:
do
function()
function()
function()
...
sync
loop

Functions:
(put all your functions here)

I find for organization and for easier coding and debugging, everything I do is pretty much in a function. If you do anything at all twice or more, it should be a function you can just call from whereever. Functions and Types are your best friends.

Kale
21
Years of Service
User Offline
Joined: 8th Sep 2002
Location: United Kingdom
Posted: 5th Dec 2002 20:02
I agree, labels are BAAAAADDD!, use as many functions as you can. 'A function must do one thing and do it well' from some book i read ages ago! labels should be used sparingly, to aid readability and i think labels are slower than functions.

What the flame does not consume, consumes the flame.
----------------------------------------------------------------------
AMD XP2100+, Geforce4Ti 4400, 512Mb DDR, Abit KX7, WinXP Home
haggisman
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: United Kingdom
Posted: 5th Dec 2002 21:36
tbh it depends upon your style whither you use functions, gosubs or even gotos. In Dbpro gosubs are faster than functions but only by a small margin that doesn't really impact a game. I'd expect it also depends on what you are doing, you wouldn't bother writing functions for a pong game as i doubt you would ever use the code again, unless you were some kind of retro game fanatic

Specs:- 1GHZ athlon, Radeon8500, 192mb ram, winxp
AsylumHunter
21
Years of Service
User Offline
Joined: 3rd Oct 2002
Location: United States
Posted: 5th Dec 2002 22:12
Functions can only ever be of proper use when pointers, functions and Structs are properly integrated with each other. Otherwise, there is nothing wrong with Subroutines.

AsylumHunter

still falling out of my binary tree most evenings (hicup!)
mimesis
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Norway
Posted: 5th Dec 2002 22:25
I use mainly subrutines for the more complex procesess,
and functions for making new commands like those in DBP
rapscaLLion
21
Years of Service
User Offline
Joined: 29th Aug 2002
Location: Canada
Posted: 5th Dec 2002 22:47
I don't see what the big aversion to subs is. I use subs were necessary and convenient, and I use functions were necessary and convenient. I find it's much easier that way.

Alex Wanuch
aka rapscaLLion
Kousen Dev Progress >> Currently Working On Editors
denki
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: United Kingdom
Posted: 6th Dec 2002 00:16
I use whatever the ocassion asks for, but I do like gosubs and do think they are tidier than functions, they aren't, but I'm odd and have and odd way of coding :S

denki
Kousen DPB RPG latest - Working on editors!
TheNVS
21
Years of Service
User Offline
Joined: 25th Oct 2002
Location: United States
Posted: 6th Dec 2002 00:19
i am wondering about how the best way programing is done. not just wether to use functions or labels, but how the functions and labels are created,how to make math equations and expressions, if we should delete a media file or not, and other things that programing is about. i have a feeling that some of us are programing functions wrong and in the future, the functions go bad and drive us crazy. and there is a better way to make one. it happened a lot that if we create something, we think it is good but it is actualy bad. eg. in the old days, we used sets of pictures to create a 3d model for racing. there always might be a better way of programing.

THE TRUTH IS OUT THERE!
Kangaroo2
21
Years of Service
User Offline
Joined: 26th Sep 2002
Location: United Kingdom
Posted: 6th Dec 2002 10:55
The majority of my programs use (go)subroutines more often than fuctions, I find it neater and quicker. I haven't noticed and actual speed difference between the methods, and if anything I have heard that in DB at least, gosubs are faster (true?). But I guess different people have different ways of doing the same thing, it doesn't really matter :p

* If the apocalypse comes, email me *
Lazzarus
21
Years of Service
User Offline
Joined: 19th Sep 2002
Location:
Posted: 6th Dec 2002 12:06
I'd have to aggree with ZomBfied. As well as being neat and readable you can fold the functions in the editor and save having to scroll through screens and screens to find the routine your looking for.

There's also the point that if any half decent professional coder saw you using gosubs they'd proberly laugh in your face!

Yarbles
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Toronto
Posted: 6th Dec 2002 13:24
"There's also the point that if any half decent professional coder saw you using gosubs they'd proberly laugh in your face!"

I laugh in your face for saying that with such ignorance! I have worked with many so called "professional coders" and I found the ones that are most stringent about coding style and OO are only that way to cover-up their own ineptitude! Your classic manager-wanna-be-coder or office politics power player.

The true worth of a programmer is measured by timely, independent, results. Good organization of code is great, but within reason. You can't be so worried about organization of your code that your main goal goes out of focus. If certain organization of your code is gonna make it harder to debug and much longer to code, well maybe it's not the best solution for you. However, if you are working on a large project with many coders and modules, there may need to be certain common coding principles that everybody should adhere to. It should always boil down to common-sense and the parameters of the project.

As for the functions vs. subroutines debate. In most programming languages there isn't much of a difference between the two. It's all up to the coder and how they have the values passed in and returned. In DB, there is more of a difference in that subroutines are treated like in-line code. Again use common sense to determine what is best for you and don't listen to people that say it HAS to be a certain way because they read it in some book or because Microsoft says so!

The Yellow Jester does not play but gently pulls the strings
And smiles as the puppets dance in the court of the Crimson King.
Jeffro
21
Years of Service
User Offline
Joined: 2nd Dec 2002
Location: London, mostly....
Posted: 6th Dec 2002 14:41
My small contribution to good coding practice -

Comment your code as if your life depended on it!

It's never as easy as it looks!
ZomBfied
21
Years of Service
User Offline
Joined: 2nd Oct 2002
Location:
Posted: 6th Dec 2002 18:58
Yeah I mostly like functions in db because they act more like subs in other languages, you can pass variables, etc) Subs in db are fine, I just find myself using them less and less.

A professional coder (Wait... I am a professional coder) doesn't care as long as your code is easy to follow and you're not needlessly repeating yourself. Even gotos have their use. I use them all the time to exit loops.

Labels are actually cool in DB because you can skip to them in the debugger.

I also agree with Jeffro Affro. Comment a lot. I've never said to myself, "Boy I sure commented that code too much. I wish I hadn't included all those comments!" Nope. Quite the opposite.

Kale
21
Years of Service
User Offline
Joined: 8th Sep 2002
Location: United Kingdom
Posted: 6th Dec 2002 19:44
Quote: "Comment your code as if your life depended on it!
"

i wholeheartedly agree! comment you code as if telling another person what obscure bits do or as if it is going to be used as a tutorial piece, you'll thank yourself when you come back to it in 6-12 months a few minutes of commenting can save an hour of re-learning.

What the flame does not consume, consumes the flame.
------------------------------------------------------------------------
AMD XP2100+, Geforce4Ti 4400, 512Mb DDR, Abit KX7, WinXP Home
Drunken Fingers
21
Years of Service
User Offline
Joined: 2nd Oct 2002
Location: United States
Posted: 6th Dec 2002 19:49
What about performance? I've read some discussion about functions and subs being slower than procedural. If you put all your code in your main loop and never call functions or subs would it be faster? How much faster? Would it be worth the mess?

Mike
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 6th Dec 2002 22:12
Use functions:
1) Functions limit variable scope (the amount of the program that knows/can affect the variable value). This makes them far far easier to debug when you have an error - and you will have them.
2) Functions encourage reuse (not guarantee).
3) Fuctions should be well defined (do one thing only) as this makes them easier to understand (and to debug).

Variables:
1) Give them meaningful names. 'LivesLeft' meands much more to the coder than 'l'.
2) Limit their scope (see functions). Global variables can be accidentally modified by ANY part of the code - especially if you are cutting/pasting code.

Comments:
1) Use them!
2) Don't overuse them - if the code says it all, why explain again, one comment per code line is *way* too much.
3) Keep them up to date - maintain your comments as if it were code. A lying comment will cause you to take 3-4 times as long to debug the code it explains.

Optimising (when to).
Performance is the very last thing you should be worrying about - don't optimise unless:
1) You've finished the program.
2) The program operates correctly.

Before tweaking for speed, optimise your algorithms. For example, if you are using a bubblesort, switch to a quicksort.
rapscaLLion
21
Years of Service
User Offline
Joined: 29th Aug 2002
Location: Canada
Posted: 6th Dec 2002 23:33
Without reading all the above posts:
I sometimes have a lot of variables and such, and can't be bothered to pass them all into a function... it would create more work, and then I'd have to use up more memory... it's just inconvenient and just hurt performance. Of course, this doesn't occur often, but I'm just saying you should overlook subs totally. And I really don't care if some "better" programmer laughs, he can suck on an egg for all I care.

Functions are very useful of course, and I use them often as well. I don't find I reuse functions much though, theres often huge changes involved anyway. The only ones I really reuse are basic: Printing text w/shadows, mouse collision, basic camera movement.

On comments: I like comments. I find that putting in lots of comments and keeping the code looking nice just encourages me Theres no performance hit because all the comments and formatting are stripped out anyway, so it doesn't matter.

Alex Wanuch
aka rapscaLLion
Kousen Dev Progress >> Currently Working On Editors
SonicBoom
21
Years of Service
User Offline
Joined: 26th Nov 2002
Location:
Posted: 7th Dec 2002 06:13
Unfortunatley there is no facility to pass a variable by reference to a DB function. C/C++ does this and even VB.

If you could do this then better code structure would be possible. The return value of the function would merely state if the function had succeeded or not.

If you want your functions to be able to change values of variables then they have to be global. Therfore you must create an array (even if you just want a scalar value), a user defined type (even if it just has one integer element).

eg



The new Types in DBPro take care of this unweidly method for you but essentially is the same thing, you still cannot pass your object to the function thus;

OK = SetupObject(MyObject)
If Not OK then ErrorCode("SetupObject failed")

The alternative is to use Gosub, which may not be the best method for you.

Login to post a reply

Server time is: 2024-04-30 17:58:25
Your offset time is: 2024-04-30 17:58:25