Page 1 of 2

tidying up code - tips?

Posted: Fri Aug 23, 2013 4:10 pm
by PoLyGLoT
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!

Re: tidying up code - tips?

Posted: Fri Aug 23, 2013 6:43 pm
by dunbarx
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

Re: tidying up code - tips?

Posted: Fri Aug 23, 2013 7:14 pm
by BvG
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 ;)

Re: tidying up code - tips?

Posted: Sat Aug 24, 2013 4:46 pm
by andrewferguson
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

Re: tidying up code - tips?

Posted: Sun Aug 25, 2013 12:33 pm
by Klaus
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

Re: tidying up code - tips?

Posted: Sun Aug 25, 2013 1:08 pm
by Simon
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

Re: tidying up code - tips?

Posted: Sun Aug 25, 2013 2:19 pm
by Klaus
Lazybone! :D

Re: tidying up code - tips?

Posted: Sun Aug 25, 2013 3:51 pm
by PoLyGLoT
Some good tips in here. Keep them coming!! :)

Re: tidying up code - tips?

Posted: Sun Aug 25, 2013 4:13 pm
by dunbarx
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

Re: tidying up code - tips?

Posted: Sun Aug 25, 2013 5:48 pm
by PoLyGLoT
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

Re: tidying up code - tips?

Posted: Sun Aug 25, 2013 6:28 pm
by Klaus
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

Re: tidying up code - tips?

Posted: Sun Aug 25, 2013 7:27 pm
by dunbarx
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

Re: tidying up code - tips?

Posted: Mon Aug 26, 2013 4:28 am
by PoLyGLoT
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?

Re: tidying up code - tips?

Posted: Mon Aug 26, 2013 4:32 am
by FourthWorld
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

Re: tidying up code - tips?

Posted: Mon Aug 26, 2013 4:43 am
by PoLyGLoT
Terrific website, Fourth World! Thanks for sharing.