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

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: tidying up code - tips?

Post by mwieder » Mon Aug 26, 2013 5:02 am

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?
Yep.

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

Re: tidying up code - tips?

Post by PoLyGLoT » Mon Aug 26, 2013 3:02 pm

Cool, but let's assume I have multiple "end / ifs" as Klaus was alluding to earlier.

For instance:

Code: Select all

if condition = 1then
if item 1 of line x of data = "test" then
if item 7 of line x of data = 1 then
#STUFF

else if item 7 of line x of data = 3 then 
# STUFF
end if
else if item 1 of line x of data = "XYZ" then

#Stuff

end if
else if condition = 2 then
if item 1 of line x of data = "test" then
if item 7 of line x of data = 1 then
....
In this example, it's much more nested. Anyway to use the case / switch stuff on a scenario like this?

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: tidying up code - tips?

Post by mwieder » Mon Aug 26, 2013 4:49 pm

Here's my take on that. Note that switch statements can be nested and that you can mix switch statements with embedded if/then/else statements as well. And also that you haven't defined any default catch-all cases (I put one in for you as an example).

Code: Select all

switch condition
  case 1
    switch item 1 of line x of data
      case "test"
        switch item 7 of line x of data 
          case 1
            #STUFF
            break
          case 3
            # STUFF
            break
        end switch --  item 7 of line x of data
      case "XYZ"
          #Stuff
        break
      default
          -- do stuff here to catch item 1 not being "test" or "XYZ"
        break
    end switch --  item 1 of line x of data
  case 2
    switch item 1 of line x of data
      case "test"
        switch item 7 of line x of data 
      ....
    end switch -- item 1 of line x of data
end switch -- condition

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

Re: tidying up code - tips?

Post by PoLyGLoT » Tue Aug 27, 2013 5:38 pm

I successfully used this trick, and it helps out immensely by making the code much more readable. Thank you!!

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: tidying up code - tips?

Post by mwieder » Tue Aug 27, 2013 5:58 pm

Glad it helped. I tend to use switch statements a lot, but there are also some situations where if/then/else statements make things more clear. An example might be long switch statements where you have to switch back and forth a lot to try to figure out the program flow to see what's connected to what and what level of indentation you're looking at.

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

Re: tidying up code - tips?

Post by PoLyGLoT » Tue Aug 27, 2013 7:05 pm

I noticed you have some software in your personal signature related to debugging. Could you talk about that a bit? I'm interested in more efficient and comprehensive debugging methods, and perhaps your software can help me with that?

Thank you.

Post Reply