Style of coding

Previous topic - Next topic

yaKC

Can I just ask please about peoples style of coding, does anyone like I am doing (as a beginner) just code the whole thing in one file and not use functions, i.e. I make everything Global and have my routines just laid out and skipping them sometimes by GoTo and using labels... I know this is probaly bad programming pratice and I did have experience on SAM Coupe 8-bit with procedures and similar in AMOS on the Amiga but I am the only person coding like this as I learn GLBasic, will I eventually have to code my projects again if they grow too large due to this?

Also, would be very inerested to know how silly it is making everything global, especially if I'm guessing correctly by not using functions and having my projects just in one long laid out way I probably don't need to make my variables Global ?

And when I've Dim'ed arrays I get an error if I then try to make that array Global IIRC from when messing around, does the dimensioning of an array make it automatically global?

thanks for any views :)

edit:- just so I don't need to make a new topic...

Is it possible to load jpg's into GLBasic in anyway, or GIF's? I'm writing a picture viewer (and gonna be starting an art package soon) and whilst it's nice to load BMP's (especially a few) and be able to see the type of memory they will need internally the thought of not being able to view or load jpg's and GIF's is a bit worrysome to say the least.

Wampus

#1
I wouldn't use functions for short simple stuff. No need. However, with anything large & complex functions make coding a lot easier. Also you can fold functions in the editor which also helps with visual organisation. I need help with staying organised because otherwise I will produce very messy noodle-tangled code which becomes hard to debug or understand.  :doubt:

If you don't use functions then you don't need to declare variables as global because they'll automatically be assigned that way. However, you probably should declare them as global because its a good habit.  :good:

The way you declare an array to begin with makes it global. e.g.:-

GLOBAL fighter_combo[]      // Otherwise LOCAL fighter_combo[] if used inside a function and not needed to be accessed outside
DIM fighter_combo[14][8]

If you do not declare it GLOBAL to begin with then it is implicitly made global.

Ian Price

Get used to FUNCTIONs - they make large programs easier to read and to jump to specific areas of your code if you want to update it, or search for bugs. Life gets terribly complicated with GOTOs and GOSUBs and very quickly.

With regard to GLOBAL values - this will depend if you need the values to be GLOBAL or not. If you need variables to be read and/or altered at any time by any piece of code, then make it GLOBAL, if not, make it LOCAL. At the end of the day (before it gets dark) it's upto you. be aware thouhg that the next version of GLB may alter the way that variables are created - there was a poll posted by Gernot about this recently.

As for JPEG - that's a dirty question around these here parts! :P GLB does load JPEG images, but there are serious bugs that accompany their loading for the moment. Hopefully this will be fixed in GLB v8.0.
I came. I saw. I played.

aonyn

Also, consider that by building well thought out functions, code can be easily reused among your projects.
Why write the same code over and over again, when you can just create a function which can be used whenever appropriate.

regards,
Dave
For by grace are ye saved through faith, and that not of yourselves: it is the gift of God: Not of works, lest any man should boast. -Ephesians 2:8-9

doimus

@yaKC: I know where you're coming from... I started on C64 with lots of line numbers and GOTOs.

What got me out of that way of thinking was QBASIC that was installed on every PC with MS-DOS. It didn't require line numbers and had SUBs and functions (I think?).

GLB is VERY similar in syntax and "philosophy" to QBASIC (or QUICKBASIC). So, if GLB code base is not enough or is too confusing to start, try searching around for QBASIC sites - lot's of good beginner info is still available on the net. The language itself is way outdated, but principles are there.

And if you REALLY want to dig into all that functions & Co. stuff - I recommend you get a book on beginner programming in C.
It'll teach you all you need and more about structured/procedural programming. The syntax is not the same, the language is much complicated, but C is the foundation of programming. You don't have to master it, but some understanding will help you a lot.

MrTAToad

When the extended type system becomes available that will be handy in keeping code tidy.

erico

Hi yaKC,

Like you said I use to code the same way and did in fact achieve my things.
Thanks to the glbasic and this forum, I learned quite a lot, could probaly reduce my old codes 90%. Functions are still a ghost for me, but I'm workng on it...

Moru

Functions are nothing magical, it's just a GOTO that returns to the line after the GOTO when the function is done. Very useful for things that needs to be done more than once, for example printing the score on the screen while playing.

LOCAL variables in the function is not accessible from outside the function
GLOBAL variables are accessible inside the function even though they are declared outside.

FutureCow

I think everyone started coding the way you do - it's the easiest to understand and allows you to get programming quickly. Though you can write almost any sized program this way, you'll make life difficult for yourself when you want to write a large program if you're using gotos and not functions.

For small test programs (eg. upto ~30 lines) I'll just write it all in the main routine.

For longer programs, I'll split it into functions.
Sub(routine)'s are handy, but seeing I often find myself changing a subroutine to a function (as what I'm doing with the subroutine has changed slightly and I now want to pass it a variable to do something with or I want it to return a value) I generally just write functions in the first place regardless.

I still end up with lots of global variables (though that's not necessarily "good" programming style). If you have variables that are getting referenced within a lot of functions, I find it makes sense to leave them as global variables. Nowadays having the memory allocated to the variables for the duration of the program is not a big deal as most machines tend to have plenty to spare (writing for a handheld device like an iPhone with limited memory is a different matter). By having the variables global I don't have to pass them to each function, which keeps my function calls more streamlined as I don't have to call them with a list of parameters as long as my arm each time.

The reality is if you're not doing it for a living (and therefore the only person who criticises your code is you), then code in whatever way is most comfortable for you. It really does make your life easier though as your programs increase in size and complexity to compartmentalise it with functions and local varables - unless you like spending hours debugging your programs that is  ;) . Also, as most non-beginners use functions / local variables, if you ask someone to help you debug a piece of "unstructured" code full that only uses globals, only one huge main function and has a million gotos in it, your chances of getting help are slim.

yaKC

hiya :) I just wanted to say that I've been ill last few days so not posted, as I find it hard to multiquote at the best of times etc etc, but will reply hopefully tommorrow and just wanted to say how refreshing it is to see so many points of view and also how I appreciate the level of detail people have gone into ;) didn't realise this was such a friendly forum :D

thanks a lot !