Author Topic: Passing Shape to sub by reference or by value  (Read 2718 times)

0 Members and 1 Guest are viewing this topic.

dmbalzer

  • Jr. Member
  • **
  • Posts: 16
Passing Shape to sub by reference or by value
« on: April 27, 2021, 02:09:33 PM »
I have an event monitor that tracks if a shape is dropped and if the shape meets a certain criteria (has certain cells with values) I do some manipulation to its shape data on its drop.

My subroutine(s) that does the manipulation has the dropped shape as its argument.

My question is if there any benefits to passing a shape as an argument to a subroutine (or a class) by value or by reference?  Is there a performance gain doing it by reference vs value?

Nikolay

  • Hero Member
  • *****
  • Posts: 1056
    • UnmanagedVisio
Re: Passing Shape to sub by reference or by value
« Reply #1 on: April 27, 2021, 06:28:00 PM »
Here is an example to clarify "ByRef" vs "ByVal" semantics, it basically has nothing to do with performance, when we are talking about shapes:

ByVal:
Code
Sub Foo(ByVal shape)
  Set shape = F
End Sub

Set s = S
Call Foo(s)  // s = S after call (still)
ByRef:
Code
Sub Foo(ByRef shape)
  Set shape = F
End Sub

Set s = S
Call Foo(s) // s = F after call (modified)

In your scenario, there is no difference because all you have is the "Foo" handler and you basically don't care what happens after your handler (Foo) is executed.
Please also notice that modifying input parameters is not a good practice anyway (behavior of the second example with "ByRef" may be "unexpected" and "unexpected" things are bad)
« Last Edit: April 27, 2021, 06:39:53 PM by Nikolay »

dmbalzer

  • Jr. Member
  • **
  • Posts: 16
Re: Passing Shape to sub by reference or by value
« Reply #2 on: April 27, 2021, 07:46:32 PM »
Thank you for the response Nikolay, that makes sense.  I definitely would prefer to go by best practices, and at least with my limited C programming knowledge working with local variables is strongly encouraged with functions to prevent some hard to trace bugs.  Thanks!

Visisthebest

  • Sr. Member
  • ****
  • Posts: 376
Re: Passing Shape to sub by reference or by value
« Reply #3 on: April 29, 2021, 06:38:31 AM »
Is it possible to truly pass a shape (that is on a page) by value in VBA? What happens in this instance, hopefully one of the Visio development experts can explain. Shape objects I always pass by reference.

I agree it is best to program with the concept of immutability in the back of your mind, but it is not a good fit overall for programming VBA and Microsoft Office object models.

If a shape is truly passed by value, then a complete copy would have to be created. However, if this is a shape on a page that means there are suddenly 2 shape objects, one currently in the context of the sub to which it is passed.

Because a shape refers to so many other objects (that are in effect still passed by reference in the by-value-copied shape) all these objects are still mutable you only protect the shape object from changes but not the objects in its context that the sub can change.

From the goal of your code:

“I have an event monitor that tracks if a shape is dropped and if the shape meets a certain criteria (has certain cells with values) I do some manipulation to its shape data on its drop.

My subroutine(s) that does the manipulation has the dropped shape as its argument.

My question is if there any benefits to passing a shape as an argument to a subroutine (or a class) by value or by reference?  Is there a performance gain doing it by reference vs value?”

By reference is faster, but seldom noticeably so. Passing a 1 Gigabyte size array by value probably is slower though. You want to mutate (change) the original shape, pass the shape by reference and like Nikolay (he is a top Visio programmer, I am a true amateur) make sure that you understand the effects the changes may have (are you triggering other event handlers, what kind of code flows are possible).

Immutability/functional programming can make complex code much simpler to debug, but OOP has other advantages.

I am personally not a big OOP code writing fan (meaning developing new classes and objects beyond what is already there), but I like using the objects Microsoft/the Visio dev team has created for us, I like using UDTs when passing multiple values to subs/functions:
https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/type-statement

It allows me to just pass one argument with a set of values to a sub/function, and add values if needed at some point.

I pass the UDT by value but even if I forget to set ByVal, the code calling the sub/function loads the values in to the UDT and NEVER uses the UDT again after calling the sub/function. It is used only once, to provide information to the sub/function to do its processing.

When you add values to a UDT to pass additional information to a sub/function, you don’t have to rewrite calls to a sub/function because more arguments have been added.

There are no doubt trade-offs and disadvantages to this approach, but it has worked well for me with subs/functions that allow a lot of input values, and it supports immutability thinking. The sub/function can never change something that will impact my code elsewhere, because the UDT is never used again.

« Last Edit: April 29, 2021, 06:45:53 AM by Visisthebest »
Visio 2021 Professional

dmbalzer

  • Jr. Member
  • **
  • Posts: 16
Re: Passing Shape to sub by reference or by value
« Reply #4 on: April 29, 2021, 07:32:30 AM »
Very good points Visisthebest, I had not considered making custom types for grouping related parameters such as a shape itself, and maybe some strings for the shape data you want to work with.

I am guessing that is similar to using structs in C, where you don't have class creating but can get close with structs.

Thank you both for the great insight. Has definitely given me a better understanding.

Visisthebest

  • Sr. Member
  • ****
  • Posts: 376
Re: Passing Shape to sub by reference or by value
« Reply #5 on: April 29, 2021, 07:46:14 AM »
Yes it is like structs/structures, in VB.NET they also call it structs.

Of course objects have a lot more functionality, but with UDTs I can simplify my code with very little effort.

Objects have the disadvantage that the wrong choices up front will make for a lot of wasted effort (and refactoring), and I often do not know where I’ll end up exactly as I build a solution. For a mature project where I understand the ins and outs, refactoring parts by creating new classes and objects makes sense of course.

There are some real OOP haters out there:
https://thenewstack.io/why-are-so-many-developers-hating-on-object-oriented-programming/

I for one am very grateful for the Visio, Excel and Word object model. Microsoft put in a HUGE effort to develop these, and as an ‘object consumer’ this works very well for me!
Visio 2021 Professional

Paul Herber

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3050
    • Paul Herber's website
Re: Passing Shape to sub by reference or by value
« Reply #6 on: April 29, 2021, 11:20:38 AM »
Microsoft put in a HUGE effort to develop these

Ooo, no. It was developed by Shapeware, then MS bought them in the year 2000. MS have done some additions to the model, so far without messing anything up (well, I think there have been a few messups but nothing too egregious).


Electronic and Electrical engineering, business and software stencils and applications for Visio -

https://www.paulherber.co.uk/

dmbalzer

  • Jr. Member
  • **
  • Posts: 16
Re: Passing Shape to sub by reference or by value
« Reply #7 on: April 29, 2021, 11:30:34 AM »
I believe he was referring to Visual Basic for Applications, which may have been included with Visio 5.0 before it was bought by Microsoft?

Paul Herber

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3050
    • Paul Herber's website
Re: Passing Shape to sub by reference or by value
« Reply #8 on: April 29, 2021, 11:33:08 AM »
I think VBA has been in Visio almost right from the start. It was something companies could license from MS.

« Last Edit: April 29, 2021, 11:37:51 AM by Paul Herber »
Electronic and Electrical engineering, business and software stencils and applications for Visio -

https://www.paulherber.co.uk/

Visisthebest

  • Sr. Member
  • ****
  • Posts: 376
Re: Passing Shape to sub by reference or by value
« Reply #9 on: April 29, 2021, 11:58:27 AM »
Yes of course Paul but those Visio employees have all become Microsofties I just assumed :).

I remember, hopefully correctly, that some Visio versions became a lot less expensive after Microsoft acquired Visio.

And yes a big part of it is the appeal of Visio within a suite of VBA applications, I use Excel with Visio often and Access when the data needs more structure, if it was just Visio that would leave a big gap.

Now if only Microsoft started investing more in developing the Visio desktop application than it has in the past years... ;)
Visio 2021 Professional

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3974
  • Ideas Visio-lized into solutions
Re: Passing Shape to sub by reference or by value
« Reply #10 on: April 29, 2021, 02:19:43 PM »
M$ = less expensive.  😵  Not in this universe.  Maybe older, u unsupported version s.
Visio 2019 Pro

Surrogate

  • Hero Member
  • *****
  • Posts: 1227
    • ShapeSheet™ Knowledge Base
Re: Passing Shape to sub by reference or by value
« Reply #11 on: April 29, 2021, 04:26:01 PM »
Yes of course Paul but those Visio employees have all become Microsofties I just assumed :).
Not all Visio Corp employees become Microsoft employees  :(
Now if only Microsoft started investing more in developing the Visio desktop application than it has in the past years... ;)
Really ? In last years we can see giant new opportunities like:

6) Crime Scene Investigation template: Back by popular request, our template for crime scene investigations has been re-introduced to Visio desktop.

Enjoy an iconic moment in your next diagram Pick from 26 new stencils with icons for analytics, arts, celebration, faces, sports, and more.

« Last Edit: April 29, 2021, 04:43:31 PM by Surrogate »

Visisthebest

  • Sr. Member
  • ****
  • Posts: 376
Re: Passing Shape to sub by reference or by value
« Reply #12 on: May 04, 2021, 04:55:44 AM »
dmbalzer I want to add a caveat for vb.net add-ins, there I think simple classes are preferable to structures, in particular because the whole boxing/unboxing thing can be a nightmare with structures:

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/boxing-and-unboxing

It gets worse if you create a list of structures:

https://stackoverflow.com/questions/7126318/how-do-you-assign-values-to-structure-elements-in-a-list-in-vb-net

So here classes and objects are easier even if OOP isn’t quite the strongest defense against mutation, as even an object passed byval can be mutated by the receiving sub/function.

In VBA big fan of UDTs though!
Visio 2021 Professional

dmbalzer

  • Jr. Member
  • **
  • Posts: 16
Re: Passing Shape to sub by reference or by value
« Reply #13 on: May 04, 2021, 10:26:09 AM »
Thanks for the additional info!