Code Tidiness
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Code Tidiness
I'm trying to set up an ordering system. So far I have a button which adds field lines to the card, with each new one appearing just below the last one, and a button which removes the last one added from the list.
When new lines appear, it groups all fields and gives them the name LineGroupX, where X is the latest line number added.
The next step is to make it so that the numbers in the first column of the field cause the line groups to move up and down.
But for some reason I can't work out how to interact with a group. While this works fine:
on mouseUp
set the top of field NameField2 to 55
end mouseUp
This has no effect, but also no errors:
on mouseUp
set the top of group LineGroup2 to 55
end mouseUp
I can't find any documentation that explains why this shouldn't work, but it doesn't. Neither does changing text size or any other property of the group.
When new lines appear, it groups all fields and gives them the name LineGroupX, where X is the latest line number added.
The next step is to make it so that the numbers in the first column of the field cause the line groups to move up and down.
But for some reason I can't work out how to interact with a group. While this works fine:
on mouseUp
set the top of field NameField2 to 55
end mouseUp
This has no effect, but also no errors:
on mouseUp
set the top of group LineGroup2 to 55
end mouseUp
I can't find any documentation that explains why this shouldn't work, but it doesn't. Neither does changing text size or any other property of the group.
Last edited by A1Qicks on Mon Oct 22, 2018 10:00 am, edited 1 time in total.
-
- VIP Livecode Opensource Backer
- Posts: 3990
- Joined: Sun Jan 07, 2007 9:12 pm
- Location: Bochum, Germany
Re: Interacting with Groups
I don't quite understand what exactly you are trying to do but an unlocked group will always fit the content of the group (+ margins of the group)
If you want to cut off some of the group's content then set the lockLoc of the group to true.
If you just change the top the group will move down/up so you might need to change the rect of the locked group
I hope I got you right.
Kind regards
Bernd
PS do yourself a favor and quote literals
instead of
say
this way you won't have a namespace conflict with a variable NameField2
If you want to cut off some of the group's content then set the lockLoc of the group to true.
If you just change the top the group will move down/up so you might need to change the rect of the locked group
Code: Select all
put the rect of group x into tRect
put 55 into item 2 of tRect
set the rect of group x to tRect
Kind regards
Bernd
PS do yourself a favor and quote literals
instead of
Code: Select all
of field NameField2
Code: Select all
of field "NameField2"
Re: Interacting with Groups
That's what I'm trying to do! But with my code above, it's not currently doing anything. The group isn't moving. Checking in browser shows me the group exists, but nothing happens when I run anything to do with the group, e.g.
Code: Select all
set the textsize of group "LineGroup2" to 25
(While I'm here - is there a way to make dynamic naming work with other commands? When naming an object, this works:
Code: Select all
set the name of it to "Name"&variable
Code: Select all
set the top of "Name"&variable to 52
Re: Interacting with Groups
Instead of you would need a) to state the "control" as well as the name and b) wrap the concatenation in parentheses to force the engine to resolve the name correctly.
Useor other object type if not field, obviously.
Code: Select all
set the top of "Name"&variable to 52
Use
Code: Select all
set the top of field ("Name"&variable) to 52
Re: Interacting with Groups
Thanks, that's really helpful!
Have you got any suggestions on the group problem? I can't see why it wouldn't work. I did wonder if it's because it's moving the "group" without moving the objects in the group, but I can't see why it would do that and it doesn't look like it's doing it anyway.
Have you got any suggestions on the group problem? I can't see why it wouldn't work. I did wonder if it's because it's moving the "group" without moving the objects in the group, but I can't see why it would do that and it doesn't look like it's doing it anyway.
-
- VIP Livecode Opensource Backer
- Posts: 3990
- Joined: Sun Jan 07, 2007 9:12 pm
- Location: Bochum, Germany
Re: Interacting with Groups
Hi A1,
I made a little stack that lets you explore a grouped field when moving the group, setting the rect of the group, lockLoc - ing the group etc.
Also it lets you set the textSize of a group which will only affect the field if it has not set a textSize set of its own. Only then it inherits the textSize of its owner.
Maybe that helps in your design.
Kind regards
Bernd
I made a little stack that lets you explore a grouped field when moving the group, setting the rect of the group, lockLoc - ing the group etc.
Also it lets you set the textSize of a group which will only affect the field if it has not set a textSize set of its own. Only then it inherits the textSize of its owner.
Maybe that helps in your design.
Kind regards
Bernd
Re: Interacting with Groups
I also made a little stack, but only to demonstrate that there is no problem here with setting the top of a group to a value to shift the whole group along with its content. It's not as exploratory as Bernd's.
Check the lockLoc property of your objects, and double-check the addressing, and play with Bernd's stack to see what might be the answer for you.
Check the lockLoc property of your objects, and double-check the addressing, and play with Bernd's stack to see what might be the answer for you.
- Attachments
-
- GroupMoveByScript.zip
- (885 Bytes) Downloaded 172 times
Re: Interacting with Groups
Thanks guys! I'll have a play around with these when I'm at home later.
Re: Interacting with Groups
OK, so here's my trouble.
The actual group code works fine. So if I target LineGroup1, there is no issue in moving it. Moves fine.
The difficulty comes in targeting other LineGroups.
So, what the code does currently, is on the press of a button, it spawns a set of new fields (it currently does each one at a time, there's probably an easier way of doing this), names each one, then assigns all of them to LineGroup X, where X is the LineGroup following the last one.
So the steps would be:
1. Start state
2. Press "Add" button, spawn LineGroup2
3. Press "Add" button again, spawn LineGroup3
But the problem I'm having is that code targeting LineGroups 2 and 3 isn't doing anything. The groups aren't moving.
I've attached my version so far. Apologies for the mess, I haven't done any of this stuff in a while.
The actual group code works fine. So if I target LineGroup1, there is no issue in moving it. Moves fine.
The difficulty comes in targeting other LineGroups.
So, what the code does currently, is on the press of a button, it spawns a set of new fields (it currently does each one at a time, there's probably an easier way of doing this), names each one, then assigns all of them to LineGroup X, where X is the LineGroup following the last one.
So the steps would be:
1. Start state
2. Press "Add" button, spawn LineGroup2
3. Press "Add" button again, spawn LineGroup3
But the problem I'm having is that code targeting LineGroups 2 and 3 isn't doing anything. The groups aren't moving.
I've attached my version so far. Apologies for the mess, I haven't done any of this stuff in a while.
- Attachments
-
- Initiative Tracker.zip
- (19.37 KiB) Downloaded 178 times
-
- VIP Livecode Opensource Backer
- Posts: 3990
- Joined: Sun Jan 07, 2007 9:12 pm
- Location: Bochum, Germany
Re: Interacting with Groups
Hi A1,
the reason why the groups you addressed did not work is the that when you delete an entry you delete all the fields of the group but do not delete the group itself. This leads to a lot of groups with the same name. When 2 controls have the same name and you address that control by name it will work on the control with the lowest layer. Since in your case that group is empty and thusly invisible you don't see what the (empty) group does.
I changed some of your code to address that.
Kind regards
Bernd
the reason why the groups you addressed did not work is the that when you delete an entry you delete all the fields of the group but do not delete the group itself. This leads to a lot of groups with the same name. When 2 controls have the same name and you address that control by name it will work on the control with the lowest layer. Since in your case that group is empty and thusly invisible you don't see what the (empty) group does.
I changed some of your code to address that.
Kind regards
Bernd
Re: Interacting with Groups
Thanks a lot Bernd! There are a few cleanups you've made that give me new info to work with - the templateField section is also going to be really helpful!
Re: Code Tidiness
I figure no sense cluttering up the board with multiple threads when I can just rename.
This one's just a general question really - apart from my own sanity, how important is it to keep code tidy?
By tidy I specifically mean using elegant solutions and keeping handlers in the card where possible rather than objects. Because of my limited understanding of the language at this point, my solutions tend to be pretty brute force or workarounds rather than potentially more obvious stuff. For example, I might use 2-3 variables to do something a single line of code could manage if I understood it.
Is there any performance advantage to avoiding this situation, or can I rest easy that my code is split across multiple objects and groups as well as the card and the stack, provided that I know where everything is?
Subsidiary to that, is it better to keep code in the card or the stack, with the understanding that the whole thing is only ever going to be one card?
This one's just a general question really - apart from my own sanity, how important is it to keep code tidy?
By tidy I specifically mean using elegant solutions and keeping handlers in the card where possible rather than objects. Because of my limited understanding of the language at this point, my solutions tend to be pretty brute force or workarounds rather than potentially more obvious stuff. For example, I might use 2-3 variables to do something a single line of code could manage if I understood it.
Is there any performance advantage to avoiding this situation, or can I rest easy that my code is split across multiple objects and groups as well as the card and the stack, provided that I know where everything is?
Subsidiary to that, is it better to keep code in the card or the stack, with the understanding that the whole thing is only ever going to be one card?
Re: Code Tidiness
Well, the reason might be more apparent if you consider future users. Lets say someone comes looking for, oh, how to "Interact with Groups". Well, there used to be a topic with that title, making it easy to find, but now the topic has morphed into "Code Tidiness", which, even though the answers given may satisfy the original search, won't be as easy to connect in someones mind.
As to your question, there is no 'one way' to do anything. A lot of it depends on personal preference, and since you are likely to be the maintainer of your code, your 'sanity' is really what counts here.
The message path determines a lot about how your placement of certain things go. While the path is usually simplified as
Code: Select all
# top to bottom
object -> card -> stack
I've made simple programs where all the code was in the stack or card script, although this isn't considered ideal, especially if you have a more complicated program your working on. It works for me in those situations because there just really isn't much to those programs.
No matter how complicated the program I write is, though, I generally put the handlers at the stack level, unless there are handlers specific to a group. The exceptions to this for me are if I have a variable(s) specific to a task concerning one object. I then put the variable at the top of that objects script and any handlers that use the variable are also in that objects script.
I haven't been using this language very long, though, and more experienced users may have different insights.
-
- VIP Livecode Opensource Backer
- Posts: 3990
- Joined: Sun Jan 07, 2007 9:12 pm
- Location: Bochum, Germany
Re: Code Tidiness
Hi A1,
have a look at this very instructive introduction to scripting style conventions by Richard Gaskin
http://www.fourthworld.com/embassy/arti ... style.html
It will make your life easier if you follow those suggestions.
E.g. prepend your variables depending on scope. You use global variables in your sample stack. It is much easier to see you are using a global variable when reading your script when you prepend globals with the letter "g".
Also if you prepend your local variables with "t" you will never have to worry about inadvertently using a reserved name for a variable. No reserved name starts with a "t". Also as I have mentioned before do quote string literals.
Strict compilation mode is a matter of preference. It is a pain to declare each variable but on the other hand it saves you from typos in variable names giving you strange results.
Splitting up code into sub handlers has little processing overhead but can make your code easier to follow because instead of your main handler consisting of 500 or more lines of code you call sub handlers to execute your logic.
My use of variables is rather liberal, it helps me in debugging when I can see what is actually in that variable. Putting all into one line might be shorter but harder to debug.
Also use descriptive naming. Variable names like t1, b1, c1 are shorter to but harder to understand what they are referring to.
If you have a stack that only has one card then I would put the main logic into the card script. I tend to move all of the logic into the card script because spreading out the logic over many buttons makes it harder to understand the flow of logic.
Those are some suggestions and you will pick what suits you best. But do read Richard's scriptstyle text a couple of times.
Kind regards
Bernd
have a look at this very instructive introduction to scripting style conventions by Richard Gaskin
http://www.fourthworld.com/embassy/arti ... style.html
It will make your life easier if you follow those suggestions.
E.g. prepend your variables depending on scope. You use global variables in your sample stack. It is much easier to see you are using a global variable when reading your script when you prepend globals with the letter "g".
Also if you prepend your local variables with "t" you will never have to worry about inadvertently using a reserved name for a variable. No reserved name starts with a "t". Also as I have mentioned before do quote string literals.
Strict compilation mode is a matter of preference. It is a pain to declare each variable but on the other hand it saves you from typos in variable names giving you strange results.
Splitting up code into sub handlers has little processing overhead but can make your code easier to follow because instead of your main handler consisting of 500 or more lines of code you call sub handlers to execute your logic.
My use of variables is rather liberal, it helps me in debugging when I can see what is actually in that variable. Putting all into one line might be shorter but harder to debug.
Also use descriptive naming. Variable names like t1, b1, c1 are shorter to but harder to understand what they are referring to.
If you have a stack that only has one card then I would put the main logic into the card script. I tend to move all of the logic into the card script because spreading out the logic over many buttons makes it harder to understand the flow of logic.
Those are some suggestions and you will pick what suits you best. But do read Richard's scriptstyle text a couple of times.
Kind regards
Bernd