Skip to content

217. Quick tip for building/testing apps

July 15, 2015

Codea allows you to have multiple versions of the same function (in the same tab, or in different tabs). It compiles from left to right, and overwrites as it goes, so if you do have multiple versions, the right hand one will be run.

This allows us to use a cool trick when building apps. You know how, when you add a new function, you want to test it without having to run all the other code?

This is how.

Add a tab on the right (I usually name mine Test). Give it a setup and draw function, and insert and then run your test code. For example, suppose I just added a function GetAngle(v1,v2), that calculates the angle between two points v1 and v2, and I want to test it. My test tab might look like this:

function setup()
    v1=vec2(3,4)
    v2=vec2(-3,1)
    print(GetAngle(v1,v2))
end

function draw()
end

So all it does is run the function I want to test. (Why do I include a blank draw function? Because otherwise Codea will run the draw function in my app, which will probably crash because it refers to variables I haven’t created in the test setup function).

When I want to run my app (and not the test code), I can either

  • move the Test tab to the left of the other tabs (touch the tab label at the top, and slide it with your finger), so my app is now on the right of it and will get used by Codea, or
  • I can comment it out. The best way to comment it out is like this – the double dash and square brackets at start and end will comment out everything in between.
--[[  
function setup()
    v1=vec2(3,4)
    v2=vec2(-3,1)
    print(GetAngle(v1,v2))
end

function draw()
end
--]]

The nice part about this method is you only have to delete one square bracket from the top (ie change it from –[[ to –[), to uncomment the code quickly. Then you can put it back to comment the code out again.

This trick is very useful for testing small pieces of code, or figuring out how some Codea functions work.

Advertisement

From → Programming

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: