2019-06-30

17: An Attempt to Structurize LibreOffice/OpenOffice Basic Code

<The previous article in this series | The table of contents of this series | The next article in this series>

LibreOffice/OpenOffice Basic does not allow creating class. Here is an attempt to structurize one's Basic code nevertheless.

Topics


About: UNO (Universal Network Objects)
About: LibreOffice
About: Apache OpenOffice
About: LibreOffice Basic
About: Apache OpenOffice Basic

The table of contents of this article


Starting Context


  • The reader has basic knowledge on LibreOffice or Apache OpenOffice Basic programming.

Target Context


  • The reader will know a way to structurize his or her LibreOffice or Apache OpenOffice Basic code.
Stage Direction
Here are Hypothesizer 7, Objector 17A, and Objector 17B in front of a computer.


Orientation


Hypothesizer 7
In this article, I will attempt to structurize my LibreOffice Basic code. If you are searching for a way to structurize your LibreOffice or Apache OpenOffice Basic code, this may furnish a clue, even if you do not accept all it proposes.

Objector 17A
What do you mean by "structurize"?

Hypothesizer 7
Sir, LibreOffice or Apache OpenOffice Basic does not allow us to create any 'class' (as a term in the object-oriented programming terminology). That fact fogs me, who is adjusted to organize my code in classes.

Objector 17A
So, I'm not allowed to create any class in LibreOffice Basic . . .

Hypothesizer 7
So, it seems as far as I know. In fact, honestly, I am not familiar with LibreOffice Basic much; more honestly, I do not like LibreOffice Basic much; more more honestly, I hate LibreOffice Basic, much.

Objector 17B
. . . Why do you hate it?

Hypothesizer 7
Madam, because it is not object-oriented, it is poor in functionality (no list, no map, no thread, poor GUI, etc.), its variable types system is loose (a too-comprehensive type like 'Variant' or 'Object' has to used in many cases), and last but not least, it is poorly documented (any document I have found does not clarify some essential features of the programming language).

Objector 17B
You are disgusting! Don't use it if you hate it!

Hypothesizer 7
In fact, I use it only when I have to: certainly, I can and happily do implement my main logic in some Java or C++ UNO components, but as LibreOffice requires a macro for each of the event handlers of any button, etc., I create some macros in LibreOffice Basic only as invokers of those UNO components.

Objector 17B
Don't complain if you use it anyway!

Hypothesizer 7
I complain because I have to use it: I am not as meddlesome as I complain (or say something) about whatever I can stay away from.

Objector 17B
. . .

Hypothesizer 7
I have to use LibreOffice Basic at least to a certain degree, and I want to structurize my such code.


Main Body


1: Mapping the Java (, C++, or C#) Structure to LibreOffice or Apache OpenOffice Basic


Hypothesizer 7
Let me map the Java structure to LibreOffice or Apache OpenOffice Basic.

Objector 17A
"the Java structure"?

Hypothesizer 7
In the Java world, there are some projects (let us suppose that each of the projects corresponds to a Jar file), there are some packages in each of the projects, and there are some classes in each of the packages.

Objector 17A
Ah, . . . but why do I have to translate a Java program into LibreOffice Basic?

Hypothesizer 7
I am not talking about translating any existing Java program, but about creating a LibreOffice Basic program in a Java-like structure.

Objector 17B
Nonsense! Create it in the Basic structure!

Hypothesizer 7
Actually, I do not understand what "the Basic structure" is. I know that there is 'module', but what pieces of code should go into what module? An explanation like "module is used to group similar functionality" is too ambiguous to be any practical guideline. . . . What two things are similar and what two things are dissimilar? In fact, any two things can be deemed to be both similar and dissimilar.

In reality, "the Basic structure" tends to be "creating a single module and jumbling all the things into the module", but I do not do any programming like that.

Objector 17B
You are a . . . poop!

Hypothesizer 7
I am not particularly showing any undue partiality to Java; any decent programming language I touch has the same structure of projects-packages-classes, and I demand that structure.

On the other hand, in the LibreOffice or Apache OpenOffice world, there are some libraries and there are some modules in each of the libraries.

In fact, there is also a concept of 'container', but that concept does not interest me here, because I am not interested in stashing some code into a document. So, there can be only a single container for me.

Objector 17A
Ah-ha.

Hypothesizer 7
So, how should I map the Java structure to LibreOffice or Apache OpenOffice Basic?

Objector 17B
Just don't do so!

Hypothesizer 7
Well, I map each Java project to a Basic library.

Objector 17A
That sounds natural to me.

Hypothesizer 7
And I map each Java package to a Basic module.

Objector 17A
Well, I don't know. The options are only 'each Java package to a Basic module' and 'each Java class to a Basic module', I guess.

Hypothesizer 7
Yes. In fact, I had adopted the latter first because 'class' is an indispensable layer while 'package' is just a namespace that groups some classes; in other words, any package is just a part of a full class name.

However, in that way, I have to have too many modules without any directories structure, which, I have found out, is rather cumbersome.

Objector 17A
Hmm, I don't know.


2: Creating Any Pseudo-Class in LibreOffice or Apache OpenOffice Basic


Hypothesizer 7
The object-oriented programming paradigm is one that maps some objects in the domain to some corresponding objects in the program. As I recognize the domain as a world in which some objects are born, interact with each other, and die, it is the most natural and the easiest to construct the program as a simulacrum of the recognition.

So, I want to create some classes, but LibreOffice or Apache OpenOffice Basic does not allow that.

Objector 17B
Don't complain, I say!

Hypothesizer 7
My purpose is not to complain, but to devise a second best, based on the recognition of the reality.

Objector 17B
You should just write code as everybody does!

Hypothesizer 7
It is not "everybody" because I do not do as "everybody" does. What you mean is 'majority', not "everybody", and I do not do anything because a majority do it, but do something because it is reasonable.

Objector 17B
You are . . . a noboby! You don't count!

Hypothesizer 7
Oh my . . .

Anyway, there is a second best for me, and there is no reason why I should not adopt it.

Objector 17B
You are a poop!

Hypothesizer 7
Lady, we can define some user-defined datum types although any of them cannot have any method.

Instead of any class, I define a 'Global' user-defined datum type with its member variables as the instance member variables (if the class has no instance member variable, the user-defined datum type is not necessary).

Instead of any static member constant, I define a 'Global' constant.

Instead of any static member variable, I define a 'Global' variable.

Instead of any instance method, I define a 'Global' function that takes a 'this' instance of the user-defined datum type as the first argument.

Instead of any static method, I define a 'Global' function that does not takes any 'this' instance of the user-defined datum type (it is fine for a static method to take a non-'this' instance of the user-defined datum type).

Instead of any constructor, I define a 'Global' function that does not takes any 'this' instance of the user-defined datum type, but returns an instance of the user-defined datum type.

A problem that I have not found any satisfactory solution to is that I cannot initialize any 'Global' variable as a static member variable at any ideal timing.

Objector 17A
Huh? I don't understand.

Hypothesizer 7
What do you not understand?

Objector 17A
The whole of your babble.

Hypothesizer 7
Well, probably, we should see an example.

@LibreOffice or Apache OpenOffice Basic Source Code
Option Explicit

' A class definition
Global Type ClassA
    ' An instance member variable
    i_memberVariable0 As String
End Type

'A static member constant
Global Const ClassA_c_memberConstant0 = "A string"
' The static initialization flag
Global ClassA_s_isInitialized As Boolean
'A static member variable
Global ClassA_s_memberVariable0 As String

' The static initializer
Global Sub ClassA_initializeStatically ()
    If NOT ClassA_s_isInitialized Then
        ClassA_s_memberVariable0 = "Another string"
        ClassA_s_isInitialized = True
    End If
End Sub

' A static method
Global Function ClassA_staticMethod0 (a_argument0 As String) As String
    ClassA_initializeStatically ()
    ClassA_staticMethod0 = a_argument0 + " altered"
End Function

' A constructor
Global Function ClassA_ClassA (a_argument0 As String) As ClassA
    Dim l_this As New ClassA
    l_this.i_memberVariable0 = a_argument0
    ClassA_ClassA = l_this
End Function

' An instance method
Global Function ClassA_instanceMethod0 (a_this As ClassA, a_argument0 As String) As String
    a_this.i_memberVariable0 = a_argument0
    ClassA_instanceMethod0 = a_this.i_memberVariable0
End Function

Objector 17A
. . . Does 'ClassA_s_memberVariable0' have to be 'Global' when I want the static member to be 'private'?

Hypothesizer 7
It has to be so if you want the static member to retain its value between macro executions: being 'Global' is not only about the scope but also the lifetime of the variable. Because of that specification of Basic, I have given up on making any member 'private'.

Objector 17A
. . . What about the initialization of the static member variable?

Hypothesizer 7
I cannot find out how to initialize any variable except being done in a subroutine or a function. I mean, LibreOffice or Apache OpenOffice Basic does not seem to allow any statement like "Global ClassA_s_memberVariable As String = "Another string"", while that is a feature that is supported by most programming languages.

Objector 17A
It doesn't?

Hypothesizer 7
At least, I cannot find any document that describes how to.

So, I have created the static initializer, 'ClassA_initializeStatically', but I cannot find any way to make it to be called only once before any static method is called . . .

So, I have to make each of all the static methods call the static initializer at the first line of the method.

Objector 17A
Ah. . . . Anyway, how can I use the pseudo-class?

Hypothesizer 7
This is an example.

@LibreOffice or Apache OpenOffice Basic Source Code
' A pseudo-class test
Sub testPseudoClass0 ()
    ' Calling the static method
    ClassA_staticMethod0 ("Test 0")
    Dim l_classA As Object
    ' Instantiating the class
    l_classA = ClassA_ClassA ("Test 1")
    ' Calling the instance method
    ClassA_instanceMethod0 (l_classA, "Test 2")
End Sub

Objector 17A
. . . Can't I define 'l_classA' as of the 'ClassA' type?

Hypothesizer 7
It seems that you cannot: the user-defined type name, "ClassA", seems not to be able to be used in the other modules except the module of the pseudo-class. In fact, the constructor is defined so based on the supposition that the pseudo-class is used via an 'Object' type variable.

Objector 17A
What do you mean?

Hypothesizer 7
As any user-defined type variable definition automatically creates an instance of the type, if any user-defined type variable was used, we would need an instance initializer that initializes the existing instance, instead of a constructor that creates a new instance.

Objector 17A
Ah.


3: The Conclusion and Beyond


Hypothesizer 7
Now, we can structurize our LibreOffice or Apache OpenOffice Basic code in a more object-oriented manner.

Stage Direction
Objector 17B screams in the background, "Nonsense!".

Hypothesizer 7
. . . Although I am not so familiar with LibreOffice or Apache OpenOffice Basic, . . .

Stage Direction
Objector 17B screeches in the background, "Don't talk big, as you aren't familiar with it!".

Hypothesizer 7
. . . and I use LibreOffice or Apache OpenOffice Basic only sparkingly, . . .

Stage Direction
Objector 17B shrieks in the background, "Shut up, as you aren't qualified!".

Hypothesizer 7
. . . I have to clarify some basic things that any manual does not seem to address.

I will take on such things in a future article.


References


<The previous article in this series | The table of contents of this series | The next article in this series>