The minefield of sample code for Newbies

I have been coding with Corona for 3 months now. I had no prior experience(didn’t know how to create a folder!) Here are some questions…

Whats the deal with the all these different variations of code i am trying to learn from? I just read a great code for a pong game in 83 lines from Peach Pellen which i was able to understand great. (thanks Peach!) Then I decided to look at Ansca code for their Air Hockey game because I thought pong and air hockey would be similar, but I’m not sure because what I got was about 500 lines of big fat steaming crap! I found most of it to be incomprehensible and wasteful.

I plan to re-write the air hockey game in the Peach pong format and post it for you to share so that you don’t scare off other newbies.

Also another great code video i was watching from Ansca was showing some gears rotating nicely until the guy decided to confuse me with Sin and Cos. Im 38 and haven’t used Sin and Cos since i was 16. That’s trigonometry right? Should I relearn it if I want to move forward with Corona?

Github seems to be the place to find a lot of samples of work. Are there any other websites to find samples?

Why do programmers list all their variables and stuff 1st without having them = to something?
ex.
local this
local that
local theOtherThing

I guess once you move from “newbie” to “otherThanNewbie” the bulk of your code doesn’t go into the main.lua anymore, this is to save memory correct?

p.s.

It was driving me crazy why this guy kept putting “;” at the end of every line. Lol I think i figured it out; It means that you are so great and so pompous at programming and you have been doing it for so long (since birth?), that the computer has in fact “programmed” the programmer into doing it (binary code?). So you see than the programmer cannot possibly “undo” what has been done. You dig? I may do that on my Peach hockey remake so that people will think I’m smarter than i really am :wink:

cheers,
Russell305 [import]uid: 75779 topic_id: 17173 reply_id: 317173[/import]

Hahaha Russel,
Great Post and I am looking forward with you getting the answers from the Pros. =]

Regards,
[import]uid: 89165 topic_id: 17173 reply_id: 64655[/import]

i’m no pro, but can answer some of your questions

  1. About different code and unable to read it properly
    I’d like to see your code when you actually done something, believe me, it tend to be messy pile) If you can read it yourself and not get confused- good for you, eventually you will learn to read other’s code and understand it all
    2.Sinus and Cosinus,yes, thats trigonometry and no, you dont need to learn trigonometry to make awesome apps, except when you dealing with something, that requires mathematic equations
    3.local this, local that without “=” mark: thats called “forward declaration”, it used for simplifying things mostly, you can write “local this” and then address it like “function this()” and that function already will be local, it saves memory
  2. When you place your code outside main.lua its done simply for use of Object Oriented Programming, you can call classes from another files and chunks of code, that you dont need in your primary file, also done for simplifying developing and expanding it
    5.When someone puts “;” after their lines, it just means that this person have a long history with other programming languages before Lua, something like C++ or C#

hope i answered all your questions [import]uid: 16142 topic_id: 17173 reply_id: 64673[/import]

Where to start…

  1. Coding methods.

Every programmer has different ways for doing things. In my first Corona Game, I used the enterFrame event and checked all of my objects to see if they were manually running into other objects (laser blasts vs. enemy ships). Others would have made physics objects out of them, even though physics wasn’t really needed for the game to take advantage of Event based collision detection.

Some people prefer to keep a player’s data separate from the player’s graphic on the screen, others combine them into one object. As they say, different strokes for different folks.

I’m glad you found code that was easier to understand, but that doesn’t mean the code you had trouble with isn’t as valuable. Perhaps its using different but valid techniques that you may need in the future once you learn more.

  1. Math and Trig.

Programming depends a lot on algebra, after all its a lot about writing formulas:

ship.x = display.contentWidth / 2

is simple algebra.

But when you get to a point that you want to rotate that ship (say for an asteroids game) well now your using degrees and you start getting into circle formulas. If you have an enemy on the screen and you need to know how far to rotate your player object so that its facing/pointing to the enemy, then you’re going to need things like right-angle triangle math, which can involve Sines’, Cosines and Arctangents to calculate your rotation.

Luckily many of these formulas are so common place in what we do, that a search in the forums can frequently find the exact answer your looking for, or a quick hit on google can refresh you. If you want to build a TableView app of all your store’s inventory, no you won’t need Trig. Wanna build games, expect some simple to some complex depending on what you’re trying to do.

  1. Why do programmers list all their variables and stuff 1st without having them = to something?

Well there are a couple of different reasons. One, if you are coming to Lua from a language like C, you have to declare all your variables at the top of the program and at the top of functions. So for some of us, its habit. Secondly, it makes the code more readable. If you have your variable declarations in the middle of your code, they can become hard to find or you might try to use a variable name that you’ve already used which you would have spotted had they all been declared in close proximity to each other.

However, others would argue, in particular coming from languages like C++ and JavaScript, that declaring variable when you actually use them makes more sense and easier to read.

In my case, I’m the old school C guy so I like mine together at the top, but I almost always initialize them, like:

local x = 0

In Lua however there is a very practical reason to put them at the top, and its called “Forward Declaration”. Basically if you have something like this:

  
local function a()  
 b()  
end  
  
local function b()  
 do other stuff  
end  
  

It will error on you because “b” has not been declared beforehand, so a has no clue what to do. So you end up using a different syntax for functions:

  
local b  
  
local function a()  
 b()  
end  
  
b = function()  
 do other stuff  
end  
  

And now a knows that b exists. This doesn’t only impact functions but variables too.

  
local function a()  
 print(c)  
end  
  
local c = 12  
  

Function a doesn’t have a clue what “c” is and it blows up on you. Hmmm, had “c” been declared at the top???

  1. I guess once you move from “newbie” to “otherThanNewbie” the bulk of your code doesn’t go into the main.lua anymore, this is to save memory correct?

It’s not to save memory. In fact, it probably uses a little more memory. The purpose is to save your sanity. Its a real pain in the rear to try to edit a single main.lua that has 5,000 lines of code in it. You want to break out what you can to keep your code in manageable chunks. And if you write your “modules” with common functionality grouped together, you start building a library of code that you can use on your next project.

  1. p.s. Semicolons
    One of the rationals behind Lua is to cater to non-programmers. The more punctuation a language uses the harder it is for a novice to understand. Take for example:
BASIC:  
  
IF a = 10 THEN   
 b = 20  
ELSE  
 b = 30  
END  
  
Lua:  
  
if a == 10 then  
 b = 20  
else  
 b = 30  
end -- notice BASIC and Lua are very similar...  
  
Compare to C  
  
if (a == 10) {  
 b = 20;  
} else {  
 b = 30;  
}  

All three of those are identical code, but for the non-programmer, trying to read that block of C is scary.

What does this have to do with semi-colons? Well almost every civilized language uses semi-colons to separate statements so you can do more than one thing on a line.

C:  
if (a == 10) { b = 20; c = 30; d = 40; }  
  
Lua  
  
if a == 10 then b = 20 c = 30 d = 40 end  

From a lexical parser’s perspective, the second example is harder. Where does one statement end and the next begin? By putting in semi-colons, it’s easier to parse the data:

if a == 10 then b = 20; c = 30; d = 40; end  

Having them to terminate lines just simply makes sense when you’re trying to parse the code.

Lua needs them in the example above, but does not required them if a line of code is on a line by itself (or before the closing “end”). It lets them be optional on any line of code.

That’s the practical side of it, but why do so many programmers do it?

Well we program in Objective C, JavaScript or PHP or a host of other languages besides Lua and in those languages the semi-colons are required. It’s a comfort spot for us to be able to maintain some commonalty with our other languages we work with.

Another example you might run into and ask yourself why is this:

C:  
  
if ((somecondition \< 0) || (somecondition \> 100)) {  
 do stuff  
}  
  
Lua:  
  
if somecondition \< 0 or somecondition \> 100 then  
 do stuff  
end  
  

First || in C is the same as the word “or” in Lua. But why all the parens? Well C requires the outer set. You can think of a the minimum if statement as:

if (condition) { code; }  

The inner parens are to make sure things happen in the right order. In the above example I could have left the inner parens off because the “Or ||” has a higher presidence than the binary operators (less than, greater than, equals, not equals). There are many times where your conditions are complex enough, grouping using parens helps make sure the “algebra” is right in those conditions.

Hope this helps.
[import]uid: 19626 topic_id: 17173 reply_id: 64680[/import]

I am not the thread`s owner but just would like to thank you guys for being so helpful!

PS: Very good “tutorial” by @robmiracle as well! So much helpful.

I really appreciate the effort.
Regards,
[import]uid: 89165 topic_id: 17173 reply_id: 64723[/import]

I feel the same way as RSCdev. Thank you guys!

Naomi [import]uid: 67217 topic_id: 17173 reply_id: 64725[/import]

robmiracle - great post! [import]uid: 40033 topic_id: 17173 reply_id: 64752[/import]

sinus ==> Google

Does Sine and Cosine change with languages other than English? I thought that the mathematical terms kind of remained the same.

@rmckee282002,
putting semi colons are not reminiscent of binary code, infact those that wrote binary had to learn binary just like any language, every op code. There are OP Codes for every number 0-255 (single instructions OP Codes) then there are more variations on them. ‘;’ are from the C heritage, not binary/assembly

From what you have just outlined, there are just three categories, Beginners, Beginners and real Beginners before they develop into Developers and start making apps. Corona SDK reduces the lines of code not because it is a beginners language/framework but because it is encapsulating a whole lot of other stuff that the developer does not have to bother with.

From a novelty point of view to talk about in workshops, etc yes it sounds good to say that this code is xx number of lines of code. Like someone created Director in 24 lines of code.

The major portion of any good developers’ code goes towards error handling and ensuring that things work.

here’s an example

 --theArray holds some objects  
  
 local i  
 for i=1, #theArray, do  
 local temp = theArray[i]  
 print("Got " .. temp)  
 end  

You can count that as 5 lines of code, but you *might* not know that it will be the first point of failure in your code. So my code might have had 10-15 lines ensuring that it does not fail.

So it is totally your call on what do you prefer? Software that makes planes fall out of the skies or rock solid code that “Make mankind Boldly go where no man has gone before”??

On the topic of Math, I am sure that unless you are a 12/13 year old (Grade 7-8) you might not have learned this, but other than that, anywhere in the world, Kids have learned the basics of Math (Algebra / Trigonometry) which is used, it is just that we cannot relate how that is used in game making.

The beauty of CoronaSDK is that it lets anyone create apps, which is a good thing. Add some physics and you’re set.

However before Physics *engines* like Box2D existed, it was all math that helped provide the fx. Now all of that math is encapsulated into these libraries.

So, yes there need to be baby steps, kids need to learn the A, B, C and how to trace them over to learn writing the A, B, C then they need to learn constructing words, and sentences. Obviously if these were to read Shakespeare it would only confuse them. However then they need to learn Grammar and write small school essays or show and tell, which are simple sentences that need not necessarily take the grammatical bit into consideration (few lines of code minus the error checking).

Long story short, I have seen some developers grow from Basic developers to amazing developers, I have seen some source code that has been wow, I have also seen a lot of code that is “Holy!! WOW!!” for the sheer length and complexity of doing nothing.

The Newbie space is what most would linger in unless they apply the basics of Logic and Math and Principles of programming (Alphabets, Grammar and Language). The code shows…

So I can say that there could be 500 lines of crap, however there can also be 500 lines of amazingly solid code that *noobs* might find as steaming crap!!

it’s just about can you recognise that? or just call everything that is long crap?

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 17173 reply_id: 64761[/import]

I guess am the “pompous programmer”… can’t get rid of the “;” habit. LOL ! /JK
:slight_smile:

Well said @robmiracle. Thanks for the great explanation.
and @yajant @rmckee282002 : crap code?

how can yo increase the running time of a Quicksort algorithm by 10%. Given that the running time of Quicksoft is nLogn - or better yet, how can you guarantee that a Quicksort will always give you n2 performance?

now that’s crappy code.

c. [import]uid: 24 topic_id: 17173 reply_id: 64892[/import]

Funny story. I developed my own sorting algorithm which by one standard of measure… is the most efficient sort ever written… I dubbed it the Robbie Sort.

It was your basic Bubble Sort, except that I left out the check to see if I was done sorting or not. It was more efficient because I could write it in fewer lines of code, so it was efficient on “MY TIME”.

Of course the bubble sort is one of the slowest, and dropping that “I’m done” check" makes it even slower. But if your sorting an array of 10 elements it just doesn’t matter.

The point is that anyone can make good code crappy! Spot on Carlos!
[import]uid: 19626 topic_id: 17173 reply_id: 64893[/import]

robmiracle

thanks for the knowledge dude! I guess it doesn’t matter if your game takes 2000 or 10000 lines of code as long as it works and its good and it makes you mucho money! Your right I will go back to the code I called crappy and see if I can learn something I may be able to use elsewhere. Because I have big plans. I am still moving forward with plans to design my air hockey game in 100 lines or less. I should have it posted in a few days. Let me know if it is steaming hot or not lol.

let me know if you have any games on android I will buy them!

russell [import]uid: 75779 topic_id: 17173 reply_id: 64938[/import]

Just to add my 2 cents.

It doesn’t matter if your game is 100 lines of code or 100,000 lines of code. What does matter is that you program efficiently and are memory and speed conscious.

take the big retail games like gears of war or final fantasy, you can’t even imagine how many lines of code they contain. You can be sure however the code is well written and efficient. Thats what it all boils down to. [import]uid: 84637 topic_id: 17173 reply_id: 65051[/import]