Results 1 to 4 of 4

Thread: Programming Practices

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    74

    Programming Practices

    I originally learned to program with VB4 using a Sams Publishing "12 Easy Lessons" book. For the most part, I have no real issues except that I'm certain I'm not coding in anything even remotely approaching a "standard" manner.

    What I'm starting to wonder about is when to use Subs, Functions and Classes. Just because of the way I learned, I pretty much use Subs exclusively.

    For example, in the landscape generator I'm working on, I used mostly Subs and no Classes (except for the Form Class). I have a few Functions. I'm currently writing code to give the user an option other than fBm for creating a random image to use as a height map that involves several iterations of random noise, blurring, scaling, rotating and blending of images.

    The app has come way too far to rewrite it for release, which I plan on doing within a week, but as I'm writing this new code, I'm finding that I can do it with Functions as opposed to Subs. And the only reason I'm using Functions is because I can do so without having to create additional global variables of which the app already has in excess of 120. And for some reason, it seems that Functions make the code easier to manage, but that may just be my imagination.

    Should I create a Class for these Functions? Is there any advantage to doing so given that I wouldn't be using the code in any other app? It seems to me that for the most part, a Class is something you would create if you wanted to use that code in another app. Or should I be creating a Class for each integral section of the app?

    If you're curious, you can see screen shots of the app here.

  2. #2
    Junior Member
    Join Date
    Jan 2004
    Posts
    25

    Re: Programming Practices

    Code Complete by Steve McConnell is a book that might interest you...

    Regards,
    Tim

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    74

    Re: Programming Practices

    Quote Originally Posted by XYZ-R
    Code Complete by Steve McConnell is a book that might interest you...
    Thanks, I'll check it out.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Programming Practices

    You don't HAVE to use classes...EVER! However, the idea behind a class is that you are encapsulating some variables, and any functions which work on those variables. Data, and the methods that work on that data. The purpose behind that is that if you write to a bunch of files, you could encapsulate the readers, streams, or whatever, in a class, and just have some subs or functions which control all the rest. Far easier to call MyFile.Write(string1) to write string1 to a file than it is to set up the stream every time, and you might write another program that needs the same file functionality, so you could just drop that class in there, knowing how it works already.

    I can see how you don't need classes, but I'm wondering how you got around using functions? The difference between a sub and a function is that a function returns something. Did you never write a procedure that returned anything?
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width