OO vs Script Libraries

Now this may start some kind of debate, so apologies if it does!

I’m trying to get into object oriented design within Notes using Lotus Script, and I think I’m getting there, but I have a question.

I use a lot of script libraries so I can reuse code, doing things like database lookups, string manipulation etc. Generally this is constructed in one script library with a bunch of subs and functions.

So I decided to convert one of these libraries into a class. It works very well, however I fail to see the advantages (maybe I used a bad example) but essentially, I still have to declare the class when I want to use it (or am I doing that wrong), and I still have to “Call Object.Somthing(some variable)”, no different than I do with a script library.

So, what is the advantage of using OO over script libraries?

Subject: OO vs Script Libraries

OO is a different way of looking at things. It’s about more than reusability, although it certainly does provide that. Admittedly, it can be difficult to see the advantage of OOP if all you are doing is the same thing you would have done with procedural code, just using a different vocabulary. It’s when you start to see things as objects that the world takes on a slightly different colour. Particularly when you have an entity with a defined set of behaviours that may need to be used in eleventy-three places (events all over a form, for instance) – even if it’s something as simple as a string with a bunch of defined mutations – it’s easier to define the object once and tell it what to do to itself than it is to import and use various bits of code everywhere.

There is no question, though, that you actually have to have an “Aha!” experience before it starts to make any real sense. You know, the kind of application that would be impossible (or at least highly impractical) to accomplish without creating objects.

Or maybe a decent explanation of how one might start thinking in objects might help. Bruce Eckel’s “Thinking in Java” is a good place to start. Yeah, I know – wrong language. Nonetheless and besides, the book is pretty good at explaining the world in OO terms (and you can always stop reading before it gets into actual Java code). The book is available as a free download (or you can buy it at most good bookstores):

http://www.mindview.net/Books/TIJ/

Subject: RE: OO vs Script Libraries

Thanks for that.

I’m assuming I just haven’t had that moment yet. But as an example, I have ben working on an application which creates automated reports in Word. I did this by creating a script library and using subs and functions within that, which worked perfectly well. I used subs to make text bold, to add tables, to create headings, bullets, that kind of thing.

I decided to use this as my OO “cherry popper”. So I created a class called WordDocument, and gave it lots of functions. So now I just do

Dim WordDoc as new WordDocument

Call WordDoc.CreateTable(2)

Instead of:

Call CreateWordDoc

Call CreateTable(2)

So it doesn’t really cut out any lines of code, although I must admit it keeps it more in tune with the rest of the code I use.

So is this the right idea?

Subject: RE: OO vs Script Libraries

It is. Or rather it could be. But if all you’re doing is echoing Word’s own methods in your class’s public subs and properties, there isn’t a lot of advantage. It’s when you have methods like AppendHeading(level,stringHere) that inserts a page break if necessary, creates a new paragraph, sets the heading style, adds the text, finishes the para and sets up for body text below that the advantage begins to show.

Sure, ultimately it’s the same code that will be creating the Word document, but if you abstract away all of the details in your class definition and just leave the methods you need to use hanging out there, it becomes easier to concentrate on what you want to do rather than on how you want to do it. And you can go even further than that, by creating a very generic WordDocument class, say, then extending that class to provide a more specific type of document with automations that may only be useful in a single application. There’s a lot of set-up initially, but eventually you get to a point where you are creating a very specific implementation with the simplest possible hooks for the rest of your code.

As for your choice of a first project, well, that’s debatable. Not that an OO approach is wrong, but it is going to be harder to see the advantage with something like that right away. There’s only the one document in play (at any given time, at least), and it’s not being batted about by every event on a form or anything of that sort. So your class is essentially about setting up a bunch of subroutines that are going to be called in one module against one barely abstracted OLE object. So, teah, after spending all of that time adapting the code, it’s really hard to see what the big deal is. Now, if you had a dozen Word docs whose filenames you wanted to sort based on the fourth word in the seventh paragraph…

Subject: RE: OO vs Script Libraries

I personally try to stay away from classes in LS and use only function libraries, because you only get some of the benefits of OO like abstraction and encapsulation but since you can’t create classes dynamically you have to hardcode your class creation at compile time and therefore lose most of the OO advantage. But worst of all you lose the ability to replicate your script libraries with new changes since you have to recompile all libraries that use that class script library.

Subject: RE: OO vs Script Libraries

Huh? Code that uses script libraries always needs to be recompiled when the library changes, regardless of whether it’s procedural code or classes. I’m afraid I don’t get your first argument at all – unless you’re a big fan of the Execute statement.

Subject: RE: OO vs Script Libraries

No not when it is just a change in the implementation of the functions and adding of new functions then you don’t need to recompile libraries that use that library.

Subject: RE: OO vs Script Libraries

i’m finding this whole discussion fascinating. being the self taught LS guy that i am, i’m ashamed that i didn’t even know the benefits of script libraries and functions until i was working on a project last year. however, i’ve been familiarizing myself with OO concepts and Java recently (and Eclipse for that matter) and i think i am a few steps away from that “aha” moment.

i think i’ve avoided using classes in the five years i’ve been developing because of that old chestnut that LS doesn’t REALLY provide OOP. but let me see if i am following the discussion correctly here.

currently i have a function that i pass many variables to (i believe i pass five variables with an additional array that probably contains five more.) since there are so many different variables that are being passed, it might make more sense to define this function as a class, and pass the variables as necessary based on any methods and properties i might define. that way, when i call it i don’t have to go and try to figure out the correct order of what variables i am passing because as a class, i can pass the appropriate values to the class methods.

or am i totally off base here?

brandt

Subject: OO vs Script Libraries

I would not waste my time writing OO in LS. For one, the LS editor sucks when it comes to writing classes, but the main reason is that LS is not really OO (I am sure Bob Balaban would disagree).

Download Eclipse, and learn OO using Java. Java embraces OO programming (Data abstraction and encapsulation, Inheritance and Polymorphism) - and your “aha” moment will come a lot sooner than if you use LS.

Subject: RE: OO vs Script Libraries

I’m not sure I buy the argument that you shouldn’t even try to write classes in LotusScript because “LS is not really OO”. That’s almost like saying “That car doesn’t have power windows, so don’t even bother driving it.”

True, there are some object-oriented things that LotusScript doesn’t do, but there is still some benefit from using classes in general, especially if you have the need to start subclassing.

Subject: RE: OO vs Script Libraries

For the record, LotusScript supports inheritance.

Subject: RE: OO vs Script Libraries

And it will be a hell of a lot more difficult to implement from the UI (which is where LS OO is at its best).

Subject: RE: OO vs Script Libraries

And you can add learning syntax along with learning a new programming model.

sigh

Look, if you can learn it, it’s worth doing.

Subject: OO vs Script Libraries

Boy, that almost ends up being a chicken-and-egg problem. A lot of the usefulness of classes versus functions (object-oriented versus procedural) isn’t apparent until you’ve written a lot of classes. After a while, as Stan says, you start to have some “Aha” moments.

Unfortunately, most of your first classes will probably be poorly written, because you’ll just write them exactly as you would have written then as functions – in which case you won’t see any advantage at all (and will quickly see the disadvantage of how poorly the Designer IDE handles classes). The “Aha” moments come in when you truly start thinking of things as objects, not a bunch of functions.

There was an array class buried in one of the Sandbox databases that I saw one time, that I remember being a good starter example of a LotusScript class. I’ll try to dig that up for you and post the link.

  • Julian

Subject: RE: OO vs Script Libraries

As one who is just starting to use classes in LS, I can certainly see where some improvement in the way the Designer handles them would be nice.

What I think would be great is if they were treated more like the script libraries that so often contain them - separate viewing of each method or property (if the property is more than a simple variable).

It would also be nice if the Designer could provide auto-complete for custom classes, but I imagine we’ll get auto-complete for global variables/parameters sooner.

Subject: RE: OO vs Script Libraries

I take it you haven’t downloaded TeamStudio’s script browser yet:

http://www.teamstudio.com/tsv3/teamstudio.nsf/0/1F0685F04AA0D6D285256FCB005ABBF2

Subject: RE: OO vs Script Libraries

I use classes a fair bit in LotusScript. I’m currently working on a tool to take files and convert them into file resources. This is implemented as a class, but there are different file resources you might want to create, and they have different code required to create them. Not a lot different, but some different. There’s a lot of code they have in common – for instance, the code to take a NotesStream and base-64 encode it is the same for all types of file resources. But an image resource needs to be able to specify properties like Across and Down which don’t apply to other types of resources. A Stylesheet resource has to let you specify a character set, which is not relevant for an image. An image resource can use a descriptive type of DXL, while other file resources only support the “note style” of DXL. So I created a hierarchy of classes.

GenericFileResourceImporter

NoteStyleFileResourceImporter

StyleSheetResourceImporter

FileResourceImporter

ImageResourceImporter

So to import an image, I can write:

Dim iri As New ImageResourceImporter(db)

iri.Across = 3

iri.Down = 1

iri.ColorizeGrays = True

iri.ImportFile filepath

Each of these classes has an ImportFile method, but only the ImageResourceImporter has the Across, Down and ColorizeGrays properties. Whichever one of these classes I Dim a variable of, I can call ImportFile, and they all use the same code.

Each class has the opportunity to override any part of its inherited functionality, and this overriding can occur in the middle of something which is not overridden. For instance, as part of the ImportFile method, we need a template string of DXL with flag sequences in it that we’re going to substitute in the specific values for whatever we’re importing. So the ImportFile function uses the TemplateDXL property, which returns the DXL to be used for plugging variable values into. Naturally, this template is different for different types of file resources.

If we implemented this procedurally, we could have the ImportFile function accept an argument that contains either the template string (requiring the caller to know it) or an argument that would be an input to a case statement that selects the right template string based on the type of import being done. In either case, the caller is providing extra information every time they call a function, and the code becomes more complex and more apt to be screwed up by the caller providing the wrong value. ImportFile would also have to accept arguments detailing the properties specific to the import (Across and Down, PublicAccess, Language), and most of these would only apply to certain types of imports. So you get something like this:

Call ImportFileResource(RESTYPE_STYLESHEET, strFilepath, true, false, true, 3, “en”, true, true)

Compare this with the previous example for readability.

Now think about what happens if you want to add new functionality. Say you want to write an AppletResourceImporter and make use of the same code. If you do it procedurally, you need to go into the various procedures and add cases to handle the new import type. The experienced developer recoils in fear. “You want me to change working code that’s used all over the place?” she says, her voice quivering. “Are you planning to retest all the applications that use this code?”

If you’ve used OO for the original design, you can reply, “Never fear! We’re not going to touch your working code.” You can just write a new script library:

Option Declare

Option Public

Use “Class NoteStyleFileResourceImporter”

Class AppletResourceImporter As NoteStyleFileResourceImporter

Public Property TemplateDXL As String

    TemplateDXL = ...

End Property

… and so on

End Class

We can take advantage of the pre-existing code without changing it, just extending it by replacing the hooks in the base class with the little bits of code that are different.

Of course this requires careful design of the base classes to make sure the right bits are encapsulated in separate routines to be available for overriding. But that’s why good OO developers make the bug bucks.

Subject: RE: OO vs Script Libraries

It’s nice to see such a healthy response to this question. The last post in particular was very helpful in explaining the idea of encapsulation and expanding on existing code without breaking it, which is something I’m very good at (breaking working code that is!).

I will certainly try and use OO more, but my main gripe is that we still have to declare the class somewhere when we want to use it (unlike the built in Lotus classes, such as NotesUIDocument) and we can’t use the type ahead, which would be so useful.

Subject: RE: OO vs Script Libraries

Sorry, but you need to declare product objects when you want to use them as well. Or are you a frequent user of undeclared variables? If that’s the case, then you need go no further down the class path right now – you have a lot of procedural code to fix first. Start by putting Option Declare in the Options section of your code and watching the code turn red. The only “free rides” are the Source objects in events.

You don’t need to define the class everywhere – you use a script library that defines the class (or you can use the Globals of a design note if you can convince yourself that there is no possible way that you will ever need or want to use the code anywhere else, ever).

Type-ahead? Yeah, that would be nice, but we didn’t have type-ahead for product objects until Designer 6 either. Not an excuse, I’m afraid.

Subject: RE: OO vs Script Libraries

Go a step further - On the format (2nd.) tab of the programmers pane properties, set the “Automatically add option declare” and the designer will add it any time you create a new design element.

Doesn’t help fix old stuff, but it certainly helps.