Mar 27
Jolle Carlestam The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 27, 2015; 20:27
Jolle Carlestam
The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
It took me a while to figure out. Experts that I admire and want to be like when I grow up kept saying =94Never ever use thread variables=94. But why? They=92re handy and needed to pass information along or to store initiated type objects. Right?
Nope.
Here=92s an approach that I think is very common in the Lasso community.
We want to store some information that is used on almost every call.
In a site config file that we include on every call we do this
var(dbname = =92myDB=92)
var(session_timeout = 1800)
var(sitename = =92My site=92)
etc, etc
Convenient since it allows us to then call any of these settings anywhere knowing that they are setup and there.
if($session_timeout > #myvalue) => {
do this
}
Same with the need to store a type object. The type has to be used further on in the processing so we need to store it somehow. Again it is common to look at thread variables.
On first page
var(myuser = myusertype)
On an included page
$myuser -> name = =92Billy Bong=92
$myuser -> age = 32
Another include
$myuser -> name
So, what=92s wrong with this approach? It has worked for many years in the Lasso community and there is no other way. Or?
Well, there are several drawbacks. For example
Thread vars are not safe from tampering.
If they don=92t supply the answer you=92re looking for it can be very hard to bug track where they got the wrong value.
In a fairly complex project it is way to easy to inadvertently overwrite them or use the same name again for something completely different.
But what to do instead? The need to store and pass along data is still there.
Well, I suggest the Ke approach. Named after Ke Carlton, one of the experts I look up to. Ke was the one who introduced me to the concept so I=92m assuming he is the origin.
Take a look at this
define session_timeout => var(__session_timeout) or $__session_timeout := 1800
To be used like this
session_timeout
So what are the perks here?
Well, for one, it is not initiated until called for. Unlike the var(session_timeout = 1800) technique that will always create the var regardless if it is actually used on the specific call.
Let=92s do a walk thru on what=92s happening.
define session_timeout
Is what we do to define a method.
When we call
session_timeout
it will attempt to return the thread variable
var(__session_timeout)
If that variable does not exist it will be created but with a null value.
In comes the conditional check, the =94or=94 in the definition. Since the result of the newly created var(__session_timeout) is null it will fail the conditional and the code to the right of the =94or=94 will be triggered.
$__session_timeout := 1800
Here we=92re using the short hand call for the var, the dollar sign, since it was just created and there=92s no need to create it again.
The := might look daunting (yes, I=92m looking at you Kim=85) but what it does is a simple assign and return. That is, assign the value 1800 to the var session_timeout and return the var.
So with this approach we can call session_timeout anywhere in our code, not having to worry about if it has been initialized or not and trusting that it will always return the same value.
Same Ke technique can be used to handle type objects.
define myuser => var(__myuser) or $__myuser := myusertype
When we need a myusertype object all we do is call it like this
myuser -> name = =92Billy Bong=92
(this will create the object and store the name param Billy Bong in it)
myuser -> age = 32
(same object as before now adding the age param)
myuser -> name
Simple, efficient, safe from tampering and easy to bug track.
Now, there can be picky readers that complain that we are still using thread vars. Yes, you=92re right. But, we never expose those vars in our code. Instead they are kept hidden away and thus remains safe and untampered.
Time for supper, I will enjoy it as I hope you will with this Tidbit.
HDB
Jolle
#############################################################
This message is sent to you because you are subscribed to
the mailing list Lasso Lasso@lists.lassosoft.com
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <Lasso-unsubscribe@lists.lassosoft.com>
Send administrative queries to <Lasso-request@lists.lassosoft.com>
Mar 27
Marc Pinnell Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 27, 2015; 12:35
Marc Pinnell
Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 27
deco rior Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 27, 2015; 13:36
deco rior
Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 27
Jolle Carlestam Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 27, 2015; 23:18
Jolle Carlestam
Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 27
Jolle Carlestam Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 27, 2015; 23:21
Jolle Carlestam
Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 27
deco rior Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 27, 2015; 16:36
deco rior
Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 27
Jolle Carlestam Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 27, 2015; 23:38
Jolle Carlestam
Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 27
deco rior Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 27, 2015; 16:40
deco rior
Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 27
French, Shelane Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 27, 2015; 22:41
French, Shelane
Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 28
Jolle Carlestam Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 28, 2015; 00:03
Jolle Carlestam
Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 27
deco rior Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 27, 2015; 17:07
deco rior
Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 28
Jolle Carlestam Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 28, 2015; 00:09
Jolle Carlestam
Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 28
deco rior Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit
Mar 28, 2015; 08:31
deco rior
Re: The better alternative to thread variables - Yet another issue of the highly irregular Non Weekly Lasso Tidbit