Page 2 of 2

Re: tidying up code - tips?

Posted: Mon Aug 26, 2013 5:02 am
by mwieder
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.

Re: tidying up code - tips?

Posted: Mon Aug 26, 2013 3:02 pm
by PoLyGLoT
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?

Re: tidying up code - tips?

Posted: Mon Aug 26, 2013 4:49 pm
by mwieder
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

Re: tidying up code - tips?

Posted: Tue Aug 27, 2013 5:38 pm
by PoLyGLoT
I successfully used this trick, and it helps out immensely by making the code much more readable. Thank you!!

Re: tidying up code - tips?

Posted: Tue Aug 27, 2013 5:58 pm
by mwieder
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.

Re: tidying up code - tips?

Posted: Tue Aug 27, 2013 7:05 pm
by PoLyGLoT
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.