Xojo, Swift and the Pyramid of Doom

Sounds like a great hollywood adventure movie, no?

I’ve done zero Swift programing but a ton of Xojo programming, and recently was reading the Swift description on Wikipedia: https://en.wikipedia.org/wiki/Swift_(programming_language)

Reading through this, I was struck at how many times I said “Oh, just like REALbasic (Xojo) did it years ago…”

I had this thought for

  • Protocols (aka Interfaces in Xojo)
  • Classes vs. Structs
  • ARC
  • Categories (aka Extends in Xojo)

and others…

Clearly REALbasic / Xojo predate Swift by years. I wonder if any of these Swift features were Xojo inspired?

Or perhaps computer science is just maturing and the “right way” of doing things is becoming more obvious?

I believe that old REALbasic was itself Python-inspired so perhaps the causal chain of design is complicated?

Question for Xojo fanatics: How to avoid the https://en.wikipedia.org/wiki/Pyramid_of_doom_(programming) in Xojo? Is just having exception handling the way to do it? In other words, instead of

  if a <> nil then 
      if a.b <> nil then
        if a.b.c <> nil then
           a.b.c.d(foobar)
       end if
     end if
  end if

just do this:

try
    a.b.c.d(foobar) // hope this works!
catch nilObjectException
   Oops!
end try
if a isa object and a.b isa object and a.b.c isa object then...

[quote=284490:@Kem Tekinay] if a isa object and a.b isa object and a.b.c isa object then... [/quote]
That avoids the “pyramid” in terms of using a lot of screen space, but I think it’s kind of the same problem: too many characters, lots of typing, not easy to read or debut necessarily.

I’ve not used Swift, but seen comments that it doesn’t have exception handling at all. Is this true?

If so, the Xojo way seems very much superior to me.

Of course it does…

all those features existed in other languages that predated even RealBasic… So no, I doubt Apple got much (if any) inspiration from Xojo (not being negative, just two vastly different camps)

Xojo current has the advantage in its multi-platform… While Swift for the “most part” is iOS and macOS… (the open source versions have a LONG way to go)

The wikipedia page states:

Influenced by C#, CLU, D, Haskell, Objective-C, Python, Rust, Ruby.

This is what I know (or think to know well enough to post here – but I might be wrong):

Protocols were introduced with Smalltalk in the early 70s. Interfaces go back to at least C++ (as abstract classes) if not even Simula (don’t know that). They were made popular with Java in the mid 90s.

They have nothing in common. Xojo structures are more or less C structures so you can write declares. Swift structs are similar to classes which you cannot inherit from and they act as a kind of value-type version of classes (faster, no memory-leaks, thread-safe).

Reference counting is quite old (beginning of the 90s). Microsoft’s COM is reference counted. Visual Basic had it (pre-. NET). Delphi had it for built-in data types. PHP I think had it too.

They date back to Smalltalk.

So it was the other way around. Xojo “stole” the good parts from other programming languages and mixed them in the best possible way.

I think in general one could say that REALbasic was influenced by Visual Basic, Java and Objective-C. Itself it didn’t and as Xojo doesn’t influence any other programming language at all.

You can certainly use a try/catch block, but I’d recommend that only if you think an error would be rare. Exceptions are expensive and raising them a lot will affect performance. If, however, you’d expect the exception to be… well, the exception, or this code won’t be called often, it’s a fine solution.

Remember though, it’s catch err as NilObjectException. (I mention this for future readers of this thread.)

You beat me to it Kem. I try to avoid raising exception wherever possible. Good planning and coding should allow you to avoid the need for these in most situation.
I always look at exceptions being the answer to ‘exceptional’ circumstances, and not be used when it can be avoided.

Consider the case of removing a key from a Dictionary. Normally you might code it this way:

if d.HasKey( k ) then
  d.Remove k
end if

But if you are doing that in a loop and most of the time the key will be present, you could do this instead:

try
  d.Remove k
catch err as KeyNotFoundException
end try

If the key will not be present once per eight cases, you will break even in performance. But if it’s less than that, you will get a performance boost. (Yes, I’ve timed this. :slight_smile: ) Imagine if this code is called 1,000 times and only once will the key not be present.

While I agree that you should code defensively to avoid exceptions where possible and practical, there are also cases where they can be used strategically.

[quote=284489:@Michael Diehr]Protocols (aka Interfaces in Xojo)
Classes vs. Structs
ARC
Categories (aka Extends in Xojo)[/quote]
Most of these have been available in Obj-C for a while.

To clarify some points:

Yes, and I wish we had a more generous edit window on this forum so I could go back and correct my mistake.

Most of these have been available in Obj-C for a while.
Yes and no. Obj-C has been through several iterations of memory management / garbage collection schemes over the years, all of them (IMHO) terrible. And the syntax of Obj-C is nothing like Xojo or Swift. I’m talking about form as well as function.

all those features existed in other languages that predated even RealBasic.... So no, I doubt Apple got much (if any) inspiration from Xojo (not being negative, just two vastly different camps)

A side point I would argue is that it’s not just individual features in isolation that make Swift remind me of Xojo, but the combination together.

Apparently Swift reminds people of other languages too: here’s a page in which Swift is like Scala, or Swift is like C#, or Swift is like Groovy… https://www.quora.com/Why-is-Swift-so-similar-to-Scala

[quote=284511:@Mark Oxley]You beat me to it Kem. I try to avoid raising exception wherever possible. Good planning and coding should allow you to avoid the need for these in most situation.
I always look at exceptions being the answer to ‘exceptional’ circumstances, and not be used when it can be avoided.[/quote]

I find exceptions immensely useful when you need a rare error to “bubble up” all the way to the top from the bottom of a deep chain of methods, and there’s no reason or need to write explicit error-handling code for all the intermediate methods. YMMV of course.

Xojo doesn’t have a great solution for this.
I know in C# you can use “?” to safely dot into things, so you could just do this:

a?.b?.c?.d?(foobar)

Wish Xojo would adopt this syntax, but I don’t think there’s a feedback request yet - not sure.

In the meantime I would do:

if a<>nil and a.b<>nil and a.b.c<>nil and a.b.c.d <> nil then a.b.c.d(foobar)

I would put it into a subroutine:

[code]Sub PyramidOfDoom()

If a Is Nil Then Return
If a.b Is Nil Then Return
If a.b.c Is Nil Then Return
If a.b.c.d Is Nil Then Return

a.b.c.d(foobar)

End

PyramidOfDoom()[/code]

One thing I dislike about Swift is it’s immaturity. I routinely hear from my Swift developing friends how Apple’s changed the language and code they wrote yesterday or last year no longer works.

I understand that Apple’s direction at the moment is to keep everything flowing and shifting, but it sure does piss developers off.

Yeah, I’ve tried Swift 3 times and every time, after a couple months, there’s an update and my projects no longer compile. Sometimes the necessary changes can be automatically applied but usually not. So frustrating.

I have many 10 year old RB projects that run in Xojo just fine! Beautiful.

It was known to every developer that Swift was (and still is) a work in progress. It is not mature enough to be used for large applications. Just look at Proposals.

If a serious developer is using Swift for complex projects and then rants about the changes, I would say he is not a serious developer.

Swift has changed, and it has matured… Swift 1, 1.1, 2, and coming soon 3
and yes each time there have been changes, deprecations, additions and removals, but at least for me XCODE has identified and most times altered the code for me. Rarely has it taken me much time to upgrade a project.
Frustrating, a bit… worthwhile in the end, yes…

And Sam… SWIFT 3 is not even an official release yet… it is still 2.2

xojo will drop out of an if statement as soon as it finds a criteria that breaks the logic, I forget what thats called, so you won’t raise an exception by doing it like this either:

if a <> nil and a.b <> nil and a.b.c <> nil then

the first one that is nil will cause the comparison to stop and just be false, so if b is nil it will never try to access c through b and cause an error.

I do things like that all the time, you can do an object comparison while first making sure it’s not nil in a single statement like:

if myDate <> nil and myDate.totalSeconds > something

and be protected from myDate being nil.

Short-Circuit Evaluation