#if Targetxxxx

I’ve seen this a lot in posts on the forums and even in code submitted as part of bug reports

If TargetWindows Then
  // Windows only code not excluded from compilation.
End If

The code inside the if will only execute on Windows. However becuase this code is just a normal if it HAS to COMPILE on ALL targets even if the intention is that it only be used on Windows (which it obviously is)

If you intend some code to ONLY be used on Windows you’re better off to use this form

#If TargetWindows Then
  // Declares for example
#Endif

This code will ONLY exist in a Windows builds and so can ONLY execute on Windows. And, because it will only be used on Windows the compiler will ignore the code inside the #if … #endif on all other platforms so the code doesn’t even have to compile on macOS or Linux (which it would in the first example shown)

Usually when you see the first form you really want the second using #if

Next time you find yourself writing

if Target

reconsider if you really should be using #if instead

Classes, Instances and Objects

There’s a fun “theoretical question” on the forums.

What is the relationship between a class, object, and instance?

Lets see if I can help any (or maybe just confuse the heck out of folks even more)

In Xojo in the IDE when you define a CLASS you are creating a “template” for how every item of this type will behave. You’re defining the properties, the methods, events etc etc etc. And EVERY item that IsA <this type I defined> returns true for IsA one of these.

AT runtime when you application runs and it uses the NEW operator to create a new item of this type you are creating new INSTANCES. Instances are the ACTUAL copies you create that you can manipulate at runtime.

And EVERY instance in Xojo IsA Object. Its the base object for EVERY dynamically created type where you use NEW to create one.

So CLASSES are a design time thing.

INSTANCES and OBJECTS are runtime things.

Silent readership

I’m curious about something. Actually I’m curious about a LOT of things; just ask my Canadian Skip Patrol instructors 😛
But I’m REALLY curious about one thing when it comes to this blog.

I can see there are a decent number of readers & views thanks to the stats that WordPress gives me. But, there’s very few comments.

I’m really curious why people don’t comment. Do the same things that make it so you don’t comment here also apply to other venues like the Xojo forums ? The reason I ask is that, as anyone who knows me well can vouch for, when I have something to say I say it. And if I have a strong opinion about something its nearly impossible to get me to shut up. Again you can ask anyone who knows me well – and if it’s Bob’s Keeney reading this I’m 100 sure he’s just shaking his head going “Yup!”

Even just a “Hey thanks I read that post and it was informative” is great feedback to get. Or “Hey I found a typo”. And yeah you can tell me I’m being dumb if I post something dumb 🙂

So what is it that keeps you from commenting ? And yes I get the irony of asking people who don’t comment on other posts to comment on this one 🙂

Performance tweaks

In Xojo there are a number of things you can do to improve the performance of your code.

There are various pragmas you can enable that can improve speed – I’d suggest only doing this once you code is fully debugged and known to be working properly as turning these checks of can mean IF you have a bug your application just crashes hard.

And there are other things you can do like lifting out code that repeatedly does lookups in arrays.

So instead of

dim something(1000) as String

// intervening code to load data into the array

for i as integer = 0 to something.ubound

   if something(i) = "abc" then
   elseif something(i) = "def" then
   elseif something(i) = "ghi" then
   end if
next

you might rewrite this as

dim something(1000) as String

// intervening code to load data into the array

// 1) eliminate repeatedly accessing the Ubound property
dim maximumIndex as integer = something.ubound 

for i as integer = 0 to maximumIndex

   // 2) eliminate repeatedly doing an array access
   dim thisItem as string = something(i)

   if thisItem = "abc" then
   elseif thisItem = "def" then
   elseif thisItem = "ghi" then
   end if

next

It would be nice if the compiler could/would do more optimization of code. There are a host of optimizations that you could apply manually like common subexpression elimination and loop invariant code motion.

But sometimes the biggest performance wins are not from tweaks like these. Often applying a different algorithm has a much bigger bang for the buck than anything.

When I worked at Xojo there were a couple areas where simply by inverting how some data was accessed in memory the entire process was sped up enormously.

Databases will do this and may use the MOST selective index to retrieve data first so the initial set of data is very tiny, instead of starting with the most broad set of data, and then winnowing it down further. By doing this they can decrease the amount of memory required and the amount of data to iterate though to satisfy whatever query you’ve executed.

When you have a performance issue I would START by reconsidering how the data is accessed and used and whether you can alter the algorithm to gain an initial big speed up by doing things in a different order.

And once you get the algorithm working the way you want then apply all the other tweaks to code to squeeze the most out of it.

And JUST to be clear make sure you do timings in a COMPILED and BUILT version NOT aa debug version using RUN as a debug version still has a lot of debug related code in it that can influence the results and MAY mislead you.

Structures vs Classes

While structures & classes may seem to be very similar in most respects there aren’t many cases when I would suggest you use a structure instead of a class.

A class with public member properties isnt that different from a structure in terms of how your code uses it.

The biggest difference is that for a class you must use new to get a new instance. With a structure you just declare it and use it.

Structures are handy for some things. If you have to read or write a specific binary file format you might use a structure. If you’re going to call some OS API via a declare then structures are useful. And if you’re reading or writing binary data to binary streams via sockets or other communication channels they are useful.

But there are downsides. Since structures are JUST data any methods to manipulate them have to go in a module. Unlike a class you cannot add methods to the structure itself. And so you end up using a very non-OOP style – maybe a module with the structure defined in there along with all the code that manipulates the structure.

Classes allow you to just bundle up the data and code into a single item and all the code that manipulates the class is part of the class – along with any hidden methods it might need that can be completely hidden from the outside world.

If your still using VB style syntax and creating a lot of structures I’d suggest you give using classes a try.

Once you get the hang of it you will appreciate how classes let you organize your code and data in a nice single item.

Operator_compare

Ever wanted to define a class, maybe a vector class, and make it possible to write code like

dim v1 as new Vector(1,1)
dim v2 as new Vector(2,2)

if v1 > v2 then
  // do something when v1 > v2
elseif v1 < v2 then
  // do something when v1 < v2
else
  // do something when v1 = v2
end if

Perhaps you have some other kind of class you’d like to define that has some kind of custom mechanism to compare itself to other instances – or even other data types.

If so you need to know how to implement operator_compare

First off operator_compare can ONLY be implemented in a Class. You cannot extend existing classes and add it to them.

It’s a method, or several methods, you add to a class that will be called whenever you use the comparison operators =, <, >, <=, >= and <> are used. Note that this has implications for your code if you are using = to check is one instance is the same instance as another. In that case you should probably use IS rather than =.

Beyond that operator_compare is pretty straight forward.

You define the method with that name, and the parameter is whatever type you wish to compare your instance, the “self” instance, to. So you can have many as each signature would be different. The return value is and integer that is

  • < 0 means SELF is “less than” the passed parameter
  • = 0 means SELF is “equal to” the passed parameter
  • > 0 means SELF is “greater than” the passed parameter

and you get to define what less than, equal to, and greater than mean.

So suppose our Vector class was defined as follows

Class Vector
   protected x as double
   protected y as double

   Public Sub Constructor(x as double, y as double)
      Self.x = x
      Self.y = y
   End Sub

   Function Operator_compare(other as vector) as Integer
      Dim a, b As Integer
      a = Self.x ^ 2 + Self.y ^ 2
      b = other.x ^ 2 + other.y ^ 2
      If a > b Then Return 1
      If a = b Then Return 0
      If a < b Then Return -1
    End Function
End Class

And now our original code from way back would work

dim v1 as new Vector(1,1)
dim v2 as new Vector(2,2)

if v1 > v2 then
  // do something when v1 > v2
elseif v1 < v2 then
  // do something when v1 < v2
else
  // do something when v1 = v2
end if

Now you could make Vectors compare themselves to Strings if you really wanted. I’m not sure what exactly that might mean – but you could. You might try implementing this as

Public Function Operator_compare1(other As string) as Integer
  Dim myRepresentation As String = "(" + Str(x,"-######.00") + "," + Str(y,"-######.00") + ")"
  
  If myRepresentation = other Then Return 0
  
  return -1
End Function

Again I’m not sure what this might truly mean but we’ll indicate that unless they are the exact same string the “self” is < the string value. Suppose you try to test this with

Dim v1 As New Vector(1,1)

If v1 = "(foo)" Then
  Break
Else
  Break
End If

And now you Vectors can be compared to Strings. With extra overloads of Operator_compare you can make your classes compare themselves to all sorts of your other classes & types.

Enjoy !

Method signatures

I know I’ve probably used this term a bunch in other blog posts but I don’t think I’ve ever explained what a method signature is.

In Xojo a “method signature” is the unique way Xojo identifies each method so it can know which method you are calling in your code.

So what does that include ?

First off it does NOT include the SCOPE. PRIVATE, PUBLIC, GLOBAL, and PROTECTED are scope modifiers – but they are not part of the signature of a method.

Attributes are also not part of the signature.

And, curiously enough, the return type is NOT part of the signature. While that determines where you can use the method (ie in an expression where a value is expected vs not) it is NOT part of the signature.

The signature includes only the name and parameter list.

It gets a LOT more confusing if your parameter lists have optional or default parameter values since the signature basically can be thought to be “variable” – but its still just the name and parameter list.

And if you ever get an error saying

There is more than one item with this name and it’s not clear to which this refers.

and the compiler has hilighted a method call then the likelihood is you have two signatures that can be treated the same and the compiler cannot figure out which one you really meant to use.

2019r3

I’ll admit that I really haven’t seriously touched R3 since most of its focus was on iOS and dark mode which isn’t something that matters to me.

The times I did start it I found it to still be slow like R2.1 and otherwise not many significant differences in the things I was trying.

Autocomplete is still glaringly bright in dark mode on macOS. I’m now wishing there was a way to turn autocomplete off its so hard on my eyes being in bold and bright white. It’s definitely NOT the code editors text color or any user selected color.

And the new default color set is harsh as well. Again its incredibly bright and in dark mode its too darned bright for my liking. Makes me really glad I made it so users could configure their own themes way back when 😛

But you be the judge. In the following pictures R3 is first and R1 is next. Both are set to use default colors. (To be honest the images do NOT show how bright those colors are here on all 3 monitors)

R3 in default colors in dark mode are very bright
R1 default colors are subdued in dark mode

Beyond that I haven’t used R3 for anything. I’m still seeing too many odd bugs being reported to make me comfortable moving any client projects to it. So I’m still using 2019r1.1 – basically the last version I contributed to 🙂

In the mean time I have managed to assemble lists of differences for various project types. These are all the differences I can discern between 2019r3 and 2019r1.1

macOS Console 64 bit

macOS Web 64 bit

macOS Desktop 64 bit

iOS 64 bit

I’ll see about compiling lists of differences on Windows & Linux as well

Note that this is changes and may simply indicate that some reorganization has taken place. For instance the HTMLViewer appears to have had many events removed but it appears the code is simply reorganized so that event handlers, for instance, are not handled in a Xojo stub and re-raised but are raised directly by the HTML Viewer plugin.

EDIT – I can see that I’ll need to update how this data is presented to make situations like the event handling on the HTMLViewer more clear. It appears that the old HTMLViewer had event handlers and event definitions so the HTMLViewer would handle the event and re-raise it so you could handle the event. This appears to have been reconfigured in 2019r3 – so the event is no longer handled by the HTMLViewer and re-raised but just not handled at all so the base class event can still be handled. To the end developer this change is invisible but in the overall framework set up it is readily apparent which is why my 2019r3 differences list includes the removal of all these events.