Jump to content

Is it safe to use procedures rather than class methods in UniGUI ?


arilotta

Recommended Posts

Hi all,

I was wondering if it is safe to use procedures not belonging to a class in UniGUI, for example:

function IfThenElse(Condizione: Boolean; CondTrue: string; CondFalse: string): string;
begin
  if Condizione then
    Result := CondTrue
  else
    Result := CondFalse;
end;

In the documentation, it is specified to not use global variables, but nothing is told about global procedures.

Thanks for all the replies.

Andrea

Link to comment
Share on other sites

Of course you can use plain procedures or functions, but they key thing you must understand is that UniGUI is of necessity a multi-threaded environment. 

That means that you need to keep in the back of your mind the question whether there could be concurrency problems when you access any "shared thing" out there. 

Again, while one can mostly program with a single threaded "typical" mindset (ignoring threading issues) providing you're using things belonging to the "current" session, anything else that is shared must be carefully considered regarding possible concurrency problems. 

What would happen for example in your procedure above if 2 sessions execute it at exactly the same time? 

Since all the parameters would be distinct and there's no shared variables anywhere, there's no problem in this case.  But if there was (for example) a shared global variable referenced in that procedure, then you'd have to think how multiple threads simultaneously trying to update such a shared resource would behave.  You'd likely need a lock or monitor or mutex or something to ensure the threads interoperate correctly on the shared resource.

Link to comment
Share on other sites

19 hours ago, Abaksoft said:

IMHO

Simple answer : Not safe

Declare your function inside the class. Never declare it alone as we do it in delphi (global function).

This is part of my rules of good unigui practises,  :)

Then it turns out that all Delphi functions are unsafe: IntToStr, Format, Trim, Len, Copy and 20,000 more functions. :)

Link to comment
Share on other sites

Hi, thanks to all of you for the replies. 

In fact I think that Volk65 is right, while Abaksoft is a little too strict.

"global" procedures that do not access (in update mode) global variables should be fine.

The issues arise when a global variable is set, while the following should be safe in a "global" procedure not belonging to a class:

- create/read/set a local object

- read/set procedure params

- read a global variable

What do you think ?

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...