tidying up code - tips?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
tidying up code - tips?
Hi All,
Does anyone have any tips on how to keep code "tidy"? I mean keeping track of which parts of code does what precisely, taking notes on your code, writing shorter code, etc. If you have lots of code, how can you know the function of each part of the code (do you just take notes commented out?).
Is there any solid method for organizing code or keeping code tidy? My goal is to learn to write very succint code and the problem will probably solve itself, but until then I'd be interested in what the veterans (or non-vetarans) have to say about this!
Thanks!
Does anyone have any tips on how to keep code "tidy"? I mean keeping track of which parts of code does what precisely, taking notes on your code, writing shorter code, etc. If you have lots of code, how can you know the function of each part of the code (do you just take notes commented out?).
Is there any solid method for organizing code or keeping code tidy? My goal is to learn to write very succint code and the problem will probably solve itself, but until then I'd be interested in what the veterans (or non-vetarans) have to say about this!
Thanks!
Re: tidying up code - tips?
Hi.
LiveCode is verbose. This is a good thing.
There are books about this, but I will start (you will see much more soon) by saying to do three things:
1- Comment carefully.
2- Move self-contained routines into separate functions or handlers. Once these are reliable, they can be placed apart from the general flow of your script. This also has the advantage of their ability to be called from more than one location, perhaps even from different handlers in different scripts in different stacks.
3- Practice. This trumps everything else.
Craig Newman
LiveCode is verbose. This is a good thing.
There are books about this, but I will start (you will see much more soon) by saying to do three things:
1- Comment carefully.
2- Move self-contained routines into separate functions or handlers. Once these are reliable, they can be placed apart from the general flow of your script. This also has the advantage of their ability to be called from more than one location, perhaps even from different handlers in different scripts in different stacks.
3- Practice. This trumps everything else.
Craig Newman
Last edited by dunbarx on Sat Aug 24, 2013 6:08 pm, edited 1 time in total.
Re: tidying up code - tips?
One thing specific to LC is that you end up with functions and handlers that interact with each other, but are distributed over several objects, groups, stacks etc. For some code it makes sense to be put into the higher up "container".
As a very basic example, instead of having the same date conversion function in several buttons, group the buttons and put the function into the group.
Generally I do put code into the "largest" script like that, so I often end up with almost all functional code in the stack, while only gui specific stuff remains in buttons and other controls. But that of course is more of a personal preference then a hard rule.
In the end all code is a bit messy
As a very basic example, instead of having the same date conversion function in several buttons, group the buttons and put the function into the group.
Generally I do put code into the "largest" script like that, so I often end up with almost all functional code in the stack, while only gui specific stuff remains in buttons and other controls. But that of course is more of a personal preference then a hard rule.
In the end all code is a bit messy

Various teststacks and stuff:
http://bjoernke.com
Chat with other RunRev developers:
chat.freenode.net:6666 #livecode
http://bjoernke.com
Chat with other RunRev developers:
chat.freenode.net:6666 #livecode
-
- VIP Livecode Opensource Backer
- Posts: 184
- Joined: Wed Apr 10, 2013 5:09 pm
Re: tidying up code - tips?
Hi,
I would say that commenting code is one of the most important things to do to tidy up your code.
Although it may seem like your code is very easy to understand when you write it, when you come back in a few weeks/months/years you will probably find it much harder to understand.
Andrew
I would say that commenting code is one of the most important things to do to tidy up your code.
Although it may seem like your code is very easy to understand when you write it, when you come back in a few weeks/months/years you will probably find it much harder to understand.
Andrew
Re: tidying up code - tips?
To keep my scripts readable I avoid nested IF THEN... clauses whereever possible!
Example:
VERSUS:
You get the picture 
Best
Klaus
Example:
Code: Select all
if conditionA then
if conditionB then
if conditionC then
if conditionD then
## finally do somehting!
end if
end if
endif
end if
...
Code: Select all
if NOT conditionA then
exit handlername
end if
if NOT conditionB then
exit handlername
end if
if NOT conditionC then
exit handlername
end if
if NOT conditionD then
exit handlername
end if
## finally do something :-)
...

Best
Klaus
Re: tidying up code - tips?
I keep a dustpan and brush next to my computer.
Or I stay in a hotel, they have people that come in and clean up everyday. I don't even have to ask!!!
Simon

Or I stay in a hotel, they have people that come in and clean up everyday. I don't even have to ask!!!
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
Re: tidying up code - tips?
Lazybone! 

Re: tidying up code - tips?
Some good tips in here. Keep them coming!! 

Re: tidying up code - tips?
Klaus' point about deeply nested "if" constructions is on point.
But to his point, in terms of simple tidiness as you originally posted, always see if you can use a "switch" construction in place of "if". These are built in a very linear way, and with long constructions are invariably nicer. In addition, you can combine cases, which would require logical operators in an "if" line of code:
if something or somethingElse or someOther then...
is better served as:
case
something
somethingElse
someOther
Craig
But to his point, in terms of simple tidiness as you originally posted, always see if you can use a "switch" construction in place of "if". These are built in a very linear way, and with long constructions are invariably nicer. In addition, you can combine cases, which would require logical operators in an "if" line of code:
if something or somethingElse or someOther then...
is better served as:
case
something
somethingElse
someOther
Craig
Re: tidying up code - tips?
Craig,
Could you explain the "switch" and "case" functions? I've never ever used those but they seem helpful.
I usually do things like:
Could you explain the "switch" and "case" functions? I've never ever used those but they seem helpful.
I usually do things like:
Code: Select all
if condition = 1 then
do some stuff
else if condition = 2 then
do other stuff
else if condition = 3 then
do some other stuff
end if
Re: tidying up code - tips?
Hi Poly,
Much more readable, you don't have to count the ELSEs, ELSEIFs and END IFs 
Best
Klaus
if condition = 1 then
do some stuff
else if condition = 2 then
do other stuff
else if condition = 3 then
do some other stuff
end if
Code: Select all
...
switch tCondition
case 1
## do stuff
break
case 2
## do other stuff
break
case 3
## even other stuff...
break
end switch
...

Best
Klaus
Re: tidying up code - tips?
What Klaus said. Hypercard had no such feature, and it is one of my most beloved in LC, like custom properties.
With short conditionals, most authors use "if-then". But for even medium or longer ones, "switch" is a pleasure.
Make sure you understand the need for the inclusion of the "break" line. Normally, this is de rigueur, though there are times when you want one case to "pass" to one below. Practice just a bit without the "break" line, and you will see.
Craig
With short conditionals, most authors use "if-then". But for even medium or longer ones, "switch" is a pleasure.
Make sure you understand the need for the inclusion of the "break" line. Normally, this is de rigueur, though there are times when you want one case to "pass" to one below. Practice just a bit without the "break" line, and you will see.
Craig
Re: tidying up code - tips?
Thank you both to Klaus and Craig. The tip about the "switch" and "case" and "break" is amazing. I use "if / end ifs" all the time, and it's SUCH A PAIN to have to locate which "if" corresponds to which "end if". And if you have a rogue "end if" that's out of position, your entire line of code is off.
I will try this out and I'm excited to give this a whirl. Probably makes for much neater code. The ability to not have to count the if's and end if's is a big enough win to make me switch.
EDIT: How do you know which "case" corresponds to which "condition" in the example I gave? Is it as simple as: case 1 = condition 1?
I will try this out and I'm excited to give this a whirl. Probably makes for much neater code. The ability to not have to count the if's and end if's is a big enough win to make me switch.
EDIT: How do you know which "case" corresponds to which "condition" in the example I gave? Is it as simple as: case 1 = condition 1?
Last edited by PoLyGLoT on Mon Aug 26, 2013 4:33 am, edited 1 time in total.
-
- VIP Livecode Opensource Backer
- Posts: 10058
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: tidying up code - tips?
In addition to the excellent advice given here, some of the items in this article may be helpful:
Fourth World Scripting Style Guide
Helpful tips for xTalk, ActionScript, JavaScript and other scripting languages
http://www.fourthworld.com/embassy/arti ... style.html
Fourth World Scripting Style Guide
Helpful tips for xTalk, ActionScript, JavaScript and other scripting languages
http://www.fourthworld.com/embassy/arti ... style.html
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: tidying up code - tips?
Terrific website, Fourth World! Thanks for sharing.