tidying up code - tips?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

tidying up code - tips?

Post by PoLyGLoT » Fri Aug 23, 2013 4:10 pm

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!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10356
Joined: Wed May 06, 2009 2:28 pm

Re: tidying up code - tips?

Post by dunbarx » Fri Aug 23, 2013 6:43 pm

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
Last edited by dunbarx on Sat Aug 24, 2013 6:08 pm, edited 1 time in total.

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Re: tidying up code - tips?

Post by BvG » Fri Aug 23, 2013 7:14 pm

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 ;)
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

andrewferguson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 184
Joined: Wed Apr 10, 2013 5:09 pm

Re: tidying up code - tips?

Post by andrewferguson » Sat Aug 24, 2013 4:46 pm

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

Klaus
Posts: 14213
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: tidying up code - tips?

Post by Klaus » Sun Aug 25, 2013 12:33 pm

To keep my scripts readable I avoid nested IF THEN... clauses whereever possible!

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
...
VERSUS:

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 :-)
...
You get the picture :-)


Best

Klaus

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

Re: tidying up code - tips?

Post by Simon » Sun Aug 25, 2013 1:08 pm

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Klaus
Posts: 14213
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: tidying up code - tips?

Post by Klaus » Sun Aug 25, 2013 2:19 pm

Lazybone! :D

PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

Re: tidying up code - tips?

Post by PoLyGLoT » Sun Aug 25, 2013 3:51 pm

Some good tips in here. Keep them coming!! :)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10356
Joined: Wed May 06, 2009 2:28 pm

Re: tidying up code - tips?

Post by dunbarx » Sun Aug 25, 2013 4:13 pm

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

PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

Re: tidying up code - tips?

Post by PoLyGLoT » Sun Aug 25, 2013 5:48 pm

Craig,

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

Klaus
Posts: 14213
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: tidying up code - tips?

Post by Klaus » Sun Aug 25, 2013 6:28 pm

Hi Poly,
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
...
Much more readable, you don't have to count the ELSEs, ELSEIFs and END IFs ;-)

Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10356
Joined: Wed May 06, 2009 2:28 pm

Re: tidying up code - tips?

Post by dunbarx » Sun Aug 25, 2013 7:27 pm

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

PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

Re: tidying up code - tips?

Post by PoLyGLoT » Mon Aug 26, 2013 4:28 am

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?
Last edited by PoLyGLoT on Mon Aug 26, 2013 4:33 am, edited 1 time in total.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10058
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: tidying up code - tips?

Post by FourthWorld » Mon Aug 26, 2013 4:32 am

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
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

Re: tidying up code - tips?

Post by PoLyGLoT » Mon Aug 26, 2013 4:43 am

Terrific website, Fourth World! Thanks for sharing.

Post Reply