Tutorial - thinking functions

Started by CreeperGoBoom, Dec 22, 2019, 05:17 AM

Previous topic - Next topic

CreeperGoBoom

Hi guys.

This is a short tutorial / theory session regarding functions.
Writing functions is almost like writing a game engine. Functions can easily simplify and shorten your code. And make it faster to complete while also making future programs faster to write.
Function sets like that are also known as an APIs.

Specifically by removing repeating code. Remember that if after you have functioned it all up. If code is repeating with some sort of pattern. Chances are you can function it out further.
Remember this rule however:
For the sake of troubleshooting. Always use good naming schemes. Avoid single letter variables. Eg
for i,v in pairs() doShould be:
for key,val in pairs() do
Most function names that are readily made look like this:
myFunction
fileWrite
getFileData
Keep it simple. Keep it memorable. Keep it readable. Always.
I have been there and done that. I have been caught up in the "too simple" trap of using too many single letter variables and then my code was very difficult to troubleshoot. Stay away from them like the plague. Eg

for i=1,numVar do
 chest.pushItemInSlot(pushDir,i,c)
end

Should probably be:
for slot= 1,NumVar do
  chest.pushItemInSlot(pushDir,slot, count)
end
Remember: readability is Everything. Always try write it so it reads almost like plain English.

Another good skill to have is. Take a good look at what your code does. Break it down into steps. Does it perform how you wanted? Is your print in the right spot. Is your counting code going to get multiplied due to it's positioning? There are lots of things to consider when proofing your code prior to testing.
Do you have enough prints to tell you when something goes awry?
Are your error() calls going to catch your code when it goes critical.
Also. Does your table need mentioning in your for in pairs loop after your table has been called in pairs() since your key,val assignments already contains everything regarding said table?
Eg: good code

for key,val in pairs(myTable) do
  print(key," : ",val)
end

Bad code:

for key,val in pairs(myTable) do
  print(myTable[key]," : ",myTable[val])
end

Not only is the above hard to read but the computers also have trouble running it. Its literally like giving someone a list of things and then getting them to recheck from some previous lists you gave them earlier that day (that are possibly no longer relevant) while practically discarding the (updated) list that you just gave them. You can see how inefficient that is. Not to mention the original lists probably contain useless information for that function: Eg nil values. This is practically asking for errors.

I hope this helps any newbies or returning programmers.
Remember. Coding is all about how that program will work. Step for step.

Credit to SquidDev and Lupus590 for putting up with my nooby questions long enough to point all that out. Plus everyone else who has been helping on discord. FatBoyChummy and Gallart for piping in. Thanks guys :D.
Let me know if I missed anything and I'll add it in.

Other contributors
daelvn