How should i organize my code

hey, i am a new programmer learning lua through corona and everything is going great. I just found out that i can access functions in different scenes, so should i create a lua file, where all my functions are, or should i copy and paste those functions in every scene that needs that function. Thanks and have a great day.

I suggest learning to write lua modules and then use that style of code organization.   There are many examples available, but you might start here:

http://coronalabs.com/blog/2015/12/15/tutorial-implementing-cross-module-functions-and-events/

Also, I have tons of dev samples here:

http://github.com/roaminggamer/RG_FreeStuff

and here:

https://github.com/roaminggamer/CoronaGeek/tree/master/Hangouts

PS - Welcome to the community.

To add on to what @roaminggamer said…

You should modular your functions in a way that makes sense. We recommend people follow the DRY principle where possible. Don’t Repeat Yourself. In other words if you have 10 scenes that need a function “doSomething”. If you make a change to doSomething you have to make it 10 times (once for each scene). That function is a candidate for being put in a functions module. You have another function doThisOnlyForThisScene(). It makes sense to keep it in the scene because that’s where it logically belongs.

Now for those functions that you do pull out, depending on what they are, you may want multiple modules. For instance, I’m working on a weather app. I put all of my weather related functions (shared between Current Conditions and Forecast) is a module weather.lua. Functions for dealing with changing themes is in theme.lua. But I have a hodge-podge of functions I’ve collected over the years, like string.trim(), string.split(), urlecode() urldecode() etc. They don’t have home, so they go into my “utilities.lua” (or extras.lua)

Hope that helps.

Rob

ahh thx you guys are the real mvp

I suggest learning to write lua modules and then use that style of code organization.   There are many examples available, but you might start here:

http://coronalabs.com/blog/2015/12/15/tutorial-implementing-cross-module-functions-and-events/

Also, I have tons of dev samples here:

http://github.com/roaminggamer/RG_FreeStuff

and here:

https://github.com/roaminggamer/CoronaGeek/tree/master/Hangouts

PS - Welcome to the community.

To add on to what @roaminggamer said…

You should modular your functions in a way that makes sense. We recommend people follow the DRY principle where possible. Don’t Repeat Yourself. In other words if you have 10 scenes that need a function “doSomething”. If you make a change to doSomething you have to make it 10 times (once for each scene). That function is a candidate for being put in a functions module. You have another function doThisOnlyForThisScene(). It makes sense to keep it in the scene because that’s where it logically belongs.

Now for those functions that you do pull out, depending on what they are, you may want multiple modules. For instance, I’m working on a weather app. I put all of my weather related functions (shared between Current Conditions and Forecast) is a module weather.lua. Functions for dealing with changing themes is in theme.lua. But I have a hodge-podge of functions I’ve collected over the years, like string.trim(), string.split(), urlecode() urldecode() etc. They don’t have home, so they go into my “utilities.lua” (or extras.lua)

Hope that helps.

Rob

ahh thx you guys are the real mvp