Code Organization

Just starting out? Need help? Post your questions and find answers here.
JaxMusic
User
User
Posts: 20
Joined: Sun Feb 14, 2021 2:55 am
Location: Max Meadows, Virginia, USA
Contact:

Code Organization

Post by JaxMusic »

Good evening. As I explore Pure Basic I am trying to get a handle on code organization. I see we can have multiple code files in a program and can access them in the IDE, or even organize them in a project. What I don’t see is how you can reference functions in other code (.pb ) files. For example, I would like to write a series of related functions and keep them in a code file separate from the main code file. How are the references to those functions in the other file declared? Is there something equivalent to #include x.pb to use in your main flow?

Thanks
BarryG
Addict
Addict
Posts: 4130
Joined: Thu Apr 18, 2019 8:17 am

Re: Code Organization

Post by BarryG »

JaxMusic wrote: there something equivalent to #include x.pb to use in your main flow?
Sure is:

https://www.purebasic.com/documentation ... ludes.html

So you can create all the common reusable code in such a PB file, and then include them in any source at the point of the IncludeFile() command.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Code Organization

Post by skywalk »

2 schools.
Use IncludeFiles of your xxx_something.pbi code or use Modules.
I prefer the former method.
This does demand a prefixed naming convention for large projects, but there is no additional syntax burdens.
You can also use both methods when the community provides libraries in module or includefile format.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
TI-994A
Addict
Addict
Posts: 2702
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Code Organization

Post by TI-994A »

JaxMusic wrote:...how you can reference functions in other code (.pb ) files. For example, I would like to write a series of related functions and keep them in a code file separate from the main code file. How are the references to those functions in the other file declared?
Quite simple, actually. Consider this example:

example 1:

Code: Select all

Procedure add(a, b)
  ProcedureReturn a + b
EndProcedure

Procedure subtract(a, b)
  ProcedureReturn a - b
EndProcedure

Debug add(1, 2)
Debug subtract(6, 3)
To save the two procedures (add & subtract) separately, simply paste them into another code file, save them as myFunctions.pb, for example, and then call them from any other code file.

myFunctions.pb:

Code: Select all

Procedure add(a, b)
  ProcedureReturn a + b
EndProcedure

Procedure subtract(a, b)
  ProcedureReturn a - b
EndProcedure
Now, to use these functions, simply include them into any code file, and call them as usual, like so:

myCode.pb:

Code: Select all

IncludeFile "myFunctions.pb"

Debug add(1, 2)
Debug subtract(6, 3)
Multiple files can be included in this manner. Let's add another:

myFunctions2.pb:

Code: Select all

Procedure multiply(a, b)
  ProcedureReturn a * b
EndProcedure

Procedure divide(a, b)
  ProcedureReturn a / b
EndProcedure
Then simply add it to the code file to use its functions:

Code: Select all

IncludeFile "myFunctions.pb"
IncludeFile "myFunctions2.pb"

Debug add(1, 2)
Debug subtract(6, 3)

Debug multiply(2, 3)
Debug divide(12, 2)
The IncludeFile function simply inserts the referenced code file directly where it is called. Technically, when expanded by the compiler, the code in myCode.pb would look exactly as the code in example 1.

It should be noted, however, that if a file is included further down the code list, any preceding calls to any functions within that file will not work. This is because all definitions for arrays, variables, structures, procedures, etc., must precede their reference (see Question 2 in this post for a more detailed explanation).

This sequence will fail with exception:

Code: Select all

; the compiler will throw an error > add() is not a function...

Debug add(1, 2)
Debug subtract(6, 3)

IncludeFile "myFunctions.pb"
This can be remedied by simply declaring the required functions prior to their calls, though it may not be practical for lengthy includes.

Code: Select all

Declare add(a, b)
Declare subtract(a, b)

Debug add(1, 2)
Debug subtract(6, 3)

IncludeFile "myFunctions.pb"
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Code Organization

Post by Tenaja »

I prefer XIncludeFile, because of you have a large project with multiple shared files, you don't accidentally send up with duplicates.

For instance, if both library A and library B require library C to work, in a project that requires both A&B, using normal IncludeFile in each could cause errors due to the duplicate includes. But XIncludeFile avoid/prevents that issue.
JaxMusic
User
User
Posts: 20
Joined: Sun Feb 14, 2021 2:55 am
Location: Max Meadows, Virginia, USA
Contact:

Re: Code Organization

Post by JaxMusic »

Excellent, thanks everyone.
JaxMusic
User
User
Posts: 20
Joined: Sun Feb 14, 2021 2:55 am
Location: Max Meadows, Virginia, USA
Contact:

Re: Code Organization

Post by JaxMusic »

[quote="TI-994A"][/quote] thanks for the excellent example TI-994A
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: Code Organization

Post by eck49 »

Include / XInclude

If you want or need to make your eventual executable small, keep your individual Included files small - any code in them which is not actually used in the current context is still compiled and included in the executable.

Naming convention

Think about this early on. I name anything in a file-to-be-included which has external exposure (procedures, constants, variables) using a prefix unique to that file which is related to the file name (I use a trigraph of capital letters). If it is within the scope of the IDE editor, auto-complete then prompts with a list of possible completions, and the IDE knows procedure parameters just as with PB-native names.

Trying out snippets

Do you want to try out a self-contained bit of code, a demo of something you may want to use, before incorporating it into your project? Perhaps there is more than one possibility and you don't want to touch your already working good stuff before deciding between them. Click on File > New in the IDE and paste in what you want to try out. As long as it is truly self-contained - meaning it has a main loop, as most example code does - you can run it without affecting the main project you are coding so you can see what it does. Very useful when venturing into unknown territory.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
Post Reply