Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding Styles
#6
(08-09-2024, 03:21 PM)SpriggsySpriggs Wrote: I like to code like a C coder. Declaring everything explicitly, avoiding temp variables when possible, and using semi-descriptive names for the variables. I also like to have all my constant declarations before everything else. Then the UDTs. Then the DECLARE LIBRARY sections. Depending on whether or not the program is large, I even like to have a "main" sub/function that is called in the code rather than just naked code in the main body. Constants are always SCREAMING_SNAKE case. Functions and subs are always UpperCamelCase. Variables are lowerCamelCase. BM files always contain only functions. BI files only contain declarations. If the program doesn't need any graphics, it will always be a console program. If a task can be performed using an external library, I will use that first before trying to reinvent the wheel.
Spriggsy's use cases are pretty much identical to mine. Of course I comment the crap out of everything too.

One thing I've started doing more of lately is prefacing variables with 3 lowercase letters do identify their use (there is a term for this but can't remember it now).

sndScreaming& ' a sound file
imgBigHead& ' an image file
sprMario% ' a sprite handle

When it comes to UDTs and variables I always share them at the sub/function level.

TYPE TYPE_SPRITEDB ' sprite definition
    ptrHimg AS INTEGER ' pointer to image database (handle)
    ptrHsnd AS INTEGER ' pointer to sound database (handle)
..
END TYPE
...
...

REDIM SpriteDB(0) AS TYPE_SPRITEDB ' sprite database
REDIM SoundDB(0) AS TYPE_SOUNDDB ' sound database
REDIM ImageDB(0) AS TYPE_IMAGEDB ' image database

SUB MySub(...)
    SHARED SpriteDB() AS TYPE_SPRITEDB ' need access to the sprite database
    SHARED ImageDB() AS TYPE_IMAGEDB ' need access to the image database
END SUB

I always try to stick with the UDT naming convention I used above as well. This makes it easy for me to remember their names.

By using SHARED within subs/functions I've found it easier to identify exactly which variables the sub/function will use and/or affect.

For really big projects I'll make a "dummy" sub or function that I can copy/paste and then rename that contains all of the shared declarations that can exist. I then simply delete the declarations that are not needed after I have finished with the sub/function code.
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply


Messages In This Thread
Coding Styles - by Pete - 08-09-2024, 03:09 PM
RE: Coding Styles - by SpriggsySpriggs - 08-09-2024, 03:21 PM
RE: Coding Styles - by TerryRitchie - 08-09-2024, 05:29 PM
RE: Coding Styles - by Pete - 08-09-2024, 03:54 PM
RE: Coding Styles - by bplus - 08-09-2024, 04:03 PM
RE: Coding Styles - by SMcNeill - 08-09-2024, 09:03 PM
RE: Coding Styles - by Pete - 08-09-2024, 04:16 PM
RE: Coding Styles - by SpriggsySpriggs - 08-09-2024, 06:40 PM
RE: Coding Styles - by TerryRitchie - 08-09-2024, 07:52 PM
RE: Coding Styles - by Jack - 08-09-2024, 07:10 PM
RE: Coding Styles - by Jack - 08-09-2024, 07:19 PM
RE: Coding Styles - by Kernelpanic - 08-09-2024, 10:29 PM
RE: Coding Styles - by CharlieJV - 08-09-2024, 10:56 PM
RE: Coding Styles - by Kernelpanic - 08-09-2024, 11:21 PM
RE: Coding Styles - by CharlieJV - 08-09-2024, 11:27 PM
RE: Coding Styles - by Pete - 08-10-2024, 03:46 AM
RE: Coding Styles - by SMcNeill - 08-10-2024, 04:55 AM
RE: Coding Styles - by JRace - 08-10-2024, 09:41 AM
RE: Coding Styles - by SMcNeill - 08-10-2024, 10:43 AM
RE: Coding Styles - by OldMoses - 08-10-2024, 12:08 PM
RE: Coding Styles - by CharlieJV - 08-10-2024, 02:11 PM
RE: Coding Styles - by Pete - 08-10-2024, 02:55 PM
RE: Coding Styles - by bplus - 08-10-2024, 03:19 PM
RE: Coding Styles - by SMcNeill - 08-10-2024, 03:41 PM
RE: Coding Styles - by Pete - 08-10-2024, 04:28 PM
RE: Coding Styles - by SMcNeill - 08-10-2024, 11:08 PM
RE: Coding Styles - by Kernelpanic - 08-10-2024, 06:00 PM
RE: Coding Styles - by Pete - 08-10-2024, 07:30 PM
RE: Coding Styles - by TerryRitchie - 08-10-2024, 07:53 PM
RE: Coding Styles - by TempodiBasic - 08-11-2024, 12:46 PM
RE: Coding Styles - by Pete - 08-11-2024, 06:45 PM
RE: Coding Styles - by Kernelpanic - 08-19-2024, 06:35 PM
RE: Coding Styles - by quickbasic - 09-04-2024, 06:09 PM
RE: Coding Styles - by dano - 09-09-2024, 12:46 AM



Users browsing this thread: 31 Guest(s)