Letters in front of variables

Moderators: LCNeil, heatherlaine, kevinmiller, elanorb

Post Reply
Bobisback
Posts: 15
Joined: Wed Mar 12, 2014 6:07 pm

Letters in front of variables

Post by Bobisback » Thu Mar 27, 2014 4:48 pm

I have noticed that alot of variables in live code tend to have a letter in front of them. Is this a live code naming convention or is it required, and lastly what do they stand for/mean to me as a developer?

Thanks,
Sean

P.S Examples: uText, uTabText, tDatabasePath, pType, sDatabaseConnectionId etc, etc

Bobisback
Posts: 15
Joined: Wed Mar 12, 2014 6:07 pm

Re: Letters in front of variables

Post by Bobisback » Thu Mar 27, 2014 5:12 pm

From another post: http://forums.runrev.com/viewtopic.php?f=73&t=19704

So with this in mind that answers most of my question. So what does the 'u', 'p', and the 't' mean.
There are various variable types in LiveCode-

-Temporary Variable (tVar)
-Local Variable (sVar)
-Global Variables (gVar)

Temporary variable are variable that are used within the context of a handler and can only be accessed when the handler they are present in is being executed-

Code: Select all
on mouseUp
put "test" into tTest
answer tTest
end mouseUp



After the handler has finished executing, the variable is discarded

Local Variables can be accessed throughout all handlers within a specific object (e.g. card script). You must declare these before they can be used

(code placed on card script)

Code: Select all

Local sTest

on openCard
put "test" into sTest
send "showMessage" to me in 3 seconds"
end openCard

on showMessage
answer sTest
end showMessage



Global Variables can be accessed throughout your whole stack (e.g. across multiple cards) and like Locals they must be declared, but this time everywhere they are being called

(following is global assigned on one card and then answered on another card)

Code: Select all

(card 1)

global gTest

on openCard
put "test" into gTest
end openCard

(card2)

gloabl gTest

on showMessage
answer gTest
end showMessage

--show message called from a mouseUp on a button



You should notice that we have an internal naming structure for our variables, this just makes it easier to see which variable is of what type (t = tempoary, s = local, g = global)

Now for your stack, since you are placing a value into a variable when you select the correct answer (from a field) and you want this value to be checked on your card script, this value will need to be placed into a global variable and not local variable.

So for your field your fields you would declare your global variable with

Code: Select all
global selectedAnswer



outside the mouseUp handler at the top of the field script and you would do the same on the card script-

Code: Select all
local theQuiz, correctAnswer
global selectedAnswer



After I made these changes, the answeresponse appeared as expected.

pink
Posts: 256
Joined: Wed Mar 12, 2014 6:18 pm

Re: Letters in front of variables

Post by pink » Thu Mar 27, 2014 5:23 pm

someone correct me if I'm wrong, I believe it goes as such:

g = global var
s = local var
t = temporary var
p = parameter being sent to function/handler
u = user defined var

using naming conventions makes it easier for people to read your code, so when sharing or asking for help it is useful

other than that, they don't mean much and you can do things however you want :mrgreen:
Greg (pink) Miller

MadPink, LLC
I'm Mad, Pink and Dangerous to Know

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Letters in front of variables

Post by Simon » Thu Mar 27, 2014 6:37 pm

Pink is correct.
Just missing
k = constant

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Location: Plymouth, UK
Contact:

Re: Letters in front of variables

Post by dave.kilroy » Thu Mar 27, 2014 7:58 pm

I also do something similar with controls so if I have a field holding data about prawns (for example) I don't call the field "prawns" but "fldPrawns" - similarly with buttons (btn), groups (grp) etc.
"...this is not the code you are looking for..."

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8146
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Letters in front of variables

Post by FourthWorld » Thu Mar 27, 2014 9:16 pm

Bobisback wrote:I have noticed that alot of variables in live code tend to have a letter in front of them. Is this a live code naming convention or is it required, and lastly what do they stand for/mean to me as a developer?
Definitely not required, but often helpful.

A very detailed variant of this was popularized by Charles Simonyi during his years at Microsoft, which led to the practice being called "Hungarian notation". Over the years many different forms have become common in other languages. Several years ago I noticed many people writing code like this in the xTalk family of languages (which includes LiveCode) with a simpler stye some call "Hungarian-lite".

I've documented that and other common xTalk code style practices here:
http://www.fourthworld.com/embassy/arti ... style.html

These days I even use Hungarian-lite in my JavaScript and shell scripts.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

Bobisback
Posts: 15
Joined: Wed Mar 12, 2014 6:07 pm

Re: Letters in front of variables

Post by Bobisback » Thu Mar 27, 2014 11:12 pm

Thanks for all the feed back on this. Its funny that I have been coding for some time and never ran into this. I seem to attribute it to the fact that i have only used strongly type languages in the past which limits the usefulness of this because of all the reminders of what type it is at that moment in time. And then added to the fact that most of the time for global's I use all caps and some other conventions for those languages that do similar things.

See ya,
Sean

Post Reply

Return to “idea2app and Coding School”