On Propertiness

I’m not sure thats a word – propertiness

I’m using it to describe something that acts or behaves as a “property”

What defines something being a “property” ?

C# uses

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties

There’s no specific requirement that a property be a simple type or a computed property – just that it has the ability to be read, written or possibly can access a private member.

A property could be implemented in Xojo using :

  1. a simple type like an Integer, Color, etc
  2. a computed property
  3. a pair of methods

Each has pro’s an cons

A simple property allows you to quickly and easily add a some setting for whatever you are adding this to. But, you cant easily make such a property read only or write only. And your code wont notice when this is set since there’s no event or anything to tell you it has been set. Subclasses cannot “override” this kind of property but they can “shadow” it.

A computed property is a simple set of get / set methods that are associated with the property. The setter has an implied parameter, value, that is the same type as your computed property. The getter has no such parameter and returns a value that is the same type as the computed property. The upside to this kind of property is now you have a place to put code to react to changes in the value, or to know when something accesses the value by putting code in the getter. Subclasses cannot “override” this kind of property but they can “shadow” it. This often has a protected or private backing variable that actually gets set or read – but not always as it may be derived from some other data held internally like the ubound of an array, number of rows in a listbox, etc.

A pair of methods, one define to return the value of whatever type and one to set the value, using assigns, are almost identical to a computed property. The biggest difference here is that, because methods ARE virtual this kind of property can be overridden by subclasses if they simply implement one or both of the methods that define the property.

Perhaps the most unique and useful aspect of these different types is that, in code that uses the property, you dont have to care what implementation is used. And, in code that creates & defines the property you can safely switch from one type to another as long as you maintain the same API (ie/ if a property is initially readable & writable then you have to maintain that when you switch to a different style otherwise code that expects to be able to write that property will no longer compile)

The upside to the last style is that the means you can “define properties” on an interface. Since a property isnt a specific implementation but a behaviour an interface can define the getter & setter and then any implementors of that interface now can expose that property with whatever custom implementation they need to implement.

Have fun !

Enum extender update

Added a little editor pane so you can quickly add values to an enum then get the whole mess written to the clipboard so you can paste that into the IDE

Hunting

Not with a gun – I’ve never shot a living thing in my life although I have fired guns of various kinds.

Right now I’m on the hunt for a “ridiculously fast string”

Xojo’s are OK but things like split, left, right, mid can really bog down if you use enormous strings. I’ve looked at a couple alternatives but so far haven’t found what I need 🙁

And right now I’m working on a text editor (more or less) and stress testing it with big strings. Think “code editor” with gigantic swaths of code. My test string is 15Mb and while that might be crazy big when compared to the amount of code you might put in any single method in the IDE – if I can make this really responsive with that big string then it will fly with any thing smaller.

I’ll keep you posted on progress.

Optimization

Code optimization is often described as hand waving and magic.

But mostly its science.

Compilers implement transformations on code that results in provably identical semantic operation but that can result in faster performance, smaller code, or in some cases both (depending on what settings the particular compiler in use exposes)

LLVM uses a form known as SSA (single static assignment). This particular form makes reasoning about and proving the results of transformations are equivalent to the original code since once a value is assigned its never mutated.

SSA makes things like constant propagation & dead code elimination easier to do. Not that it could not be done in other forms compilers use juts that SSA happens to make these easier because of how SSA works.

Xojo’s use of LLVM enables some optimizations – but not all optimizations that LLVM can perform (there are literally hundreds)

So sometimes you need to perform some yourself.

  1. invariant code motion – these are statements, or portions of statements, that can be moved outside a loop and computed once because they do not change in the course of a loop. Some care needs to be taken with this optimization when performed manually as noted in the Wikipedia article. In Xojo constantly referencing picture.graphics can often be lifted outside the loop into a local and cutting down the number of calls to picture.graphics and using the cached local variable.
  2. loop unrolling – some times you can achieve a significant speed up by repeating the body of the loop operating on multiple different items in each loop pass. For instance instead of traversing an array in reverse order and deleting one element on each pass it may be faster to use a step size in the loop of 5 and remove 5 elements on each pass. Again there are some hazards with this. Often its useful to know the number of iterations of the loop at compile time in order to most effectively unroll the loop.
  3. common subexpression elimination – sometimes a portion of a calculation is repeated more than once and lifting the repeated portion out and doing it once and the reusing the pre-computed value can speed things up (it may also improve code clarity). In something like the following
    a = b * c + g
    a = b * c * e

    you might lift out the computation of b * c into a local and then just reuse the local
    bc = b * c
    a = bc + g
    a = bc * e
  4. constant folding and propagation – when known at compile time instead of generating code to compute a constant value, typically from literals, the result will be inserted instead.
    So instead of inserting code to do the computation of 24 * 60 * 60 the compiler will simply insert 86400. This optimization may also be applied to strings and other literals.

These are just 4 of many many possible optimizations. You can implement these by hand in your own code as well and some, like common subexpression elimination, may make long lines of code simpler by replace repeated calculations with a single local variable.

I’d encourage everyone to read the Wikipedia pages about these optimizations implement them in your own code.

Have fun !

Code posting

One of Xojo’s forum guidelines I dislike is

Code should be provided in your reply directly, rather than links to code on other websites or blogs, in order to maintain the posts’ viability for a longer period.

I’ve come to dislike it because sometimes code examples require more than a handful of lines of code and posting a complete example application requires it be hosted somewhere else in order to link to it. Xojo’s forums have no way to post a long sample inline.

Longer samples posted with links in the forums post remains no more, or less, viable than it ever has when you do need to post more than a few lines of code.

Images and other content has the exact same issue. It has to be hosted elsewhere anyway.

Xojo edits get disabled quite rapidly after posting. It seems its about an hour and then the post is no longer editable. If there’s a correction to be made after this you cannot and then have to post all new code. For anyone who may not read the entire thread this can be a problem as they may see the first chunk of code and use it then complain it doesnt work or compile or whatever. And the auto, hopefully, notices this and posts new code.

So, unless its just a small amount of code I’m likely to post more substantial items on my blog or IfNotNil or linked to from there (since they also dont host the content for more substantial examples).

Full samples are on my servers – same as always.

And the other upside to if not nil is edits are enabled forever (so far). And should you ever want to remove the post you can. Its yours to do with as long as you want.

Enum extender

Tired of writing methods to convert enums to strings and back ?

This little tool lets you drag enum(s) from the IDE onto it and then select the text and paste it back in to the IDE

I will write two styles of conversions – one pair as extends and one as ToString/FromString

To paste the code back into the IDE see my previous post

You’ll find it here

Pasting plain text code

If, like me, you sometime munge text into code and then want to paste it into the IDE that can sometimes be a real chore

Unless you happen to know this little trick

If you have plain text lke

Public Function ToString(extends enumValue as Untitled) as String
  select case enumValue
    case Untitled.valueName
        return "valueName"
    case Untitled.valueName
        return "valueName"
    case Untitled.valueName
        return "valueName"
Else
 raise new UnsupportedOperationException
  end select
End Function


Public Function ToUntitled(extends stringValue as String) as Untitled
  select case stringValue
    case "valueName"
        return Untitled.valueName
    case "valueName"
        return Untitled.valueName
    case "valueName"
        return Untitled.valueName
  end select
Else
 raise new UnsupportedOperationException
End Function

Public Function Untitled_ToString( enumValue as Untitled) as String
  select case enumValue
    case Untitled.valueName
        return "valueName"
    case Untitled.valueName
        return "valueName"
    case Untitled.valueName
        return "valueName"
Else
 raise new UnsupportedOperationException
  end select
End Function


Public Function Untitled_FromString(stringValue as String) as Untitled
  select case stringValue
    case "valueName"
        return Untitled.valueName
    case "valueName"
        return Untitled.valueName
    case "valueName"
        return Untitled.valueName
Else
 raise new UnsupportedOperationException
  end select
End Function

and select it all and copy then, in the IDE select the MODULE, WINDOW, CLASS (not the methods group in any of those) and Paste

The downside here is that only the FIRST method gets pasted – but it does get pasted

So you have to do them one by one

But its better than nothing

Works well with a little helper app I’ll post later than writes methods to extend enums so its easy to convert them to/from strings

Who set that (and who wants to read it) ?

Sometimes you’d like to use what in other languages is referred to as a watchpoint. Some hardware support exists for this in Intel CPU’s but it is limited (there are only a handful or hardware watchpoints available.)

Or it could be done in software; but as noted on Wikipedia this is a ton slower.

Xojo really doesn’t support either type.

But, you can fake it by using computed properties. And since Xojo makes it easy to switch between a public property and a computed one WITHOUT having to update your code (this is a wonderful feature) you can quickly and easily put whatever break points and conditional watches you want IN your code and have much of the benefit of watchpoints.

If you have a public property that you want to see who is setting it simply

  1. select the property
  2. right click and select “Convert to computed property”

And now you can put a break point in the SET, or GET, code for that computed property.

With some creativity you could even get to being able to modify whether the break point is reached while watching your program execute.