Is using functions inside a function a good idea?

Okay, so i want to know if using functions inside a function is a good idea.
(Please read everything before responding thx)

So my goal was to make memory efficient code by using less for loops.
And it worked i guess.

This is my previous code:

previous code
local Table = {1, 2, 3}

local function Example1()
	for i, v in pairs(Table) do
		--do stuff
	end
end

Example1()

local function Example2()
	for i, v in pairs(Table) do
		--do stuff
	end
end

Example2()

As you can see i’m using 2 for loops for the same thing, so my idea was to use only one loop.

This is my current code:

Current code
local Table = {1,2,3}

--Only uses one "for loop"
local function Example()
	for i, v in pairs(Table) do
		
		local function Example1()
			--do stuff
			print(v)
		end
		
		local function Example2()
			--also do stuff
		end
		
	end
end

Example()

Here i’m putting functions inside a function.
So is this a good idea?

1 Like

Not a great idea. Rewrite it to use parameters:

local function Example1(i, v)
    -- do stuff
end
local function Example2(i, v)
    -- do stuff
end

local function Example()
    for i, v in pairs(Table) do
        Example1(i, v)
        Example2(i, v)
    end
end
Example()
1 Like

Okay thank you, but why is this not a great idea?

It’s messy and disorganized, primarily. It won’t affect your performance much, Lua is pretty forgiving. It’s unnecessary and makes things a lot more difficult to read.

Also, if you’re going this route, just delete the functions altogether. Functions are for code structuring and for reusable code. If the functions are going to be in the exact place that they’re going to be called and they’re not going to be used elsewhere, there’s no point.

So with your current plan, you can revise this

local Table = {1,2,3}

--Only uses one "for loop"
local function Example()
	for i, v in pairs(Table) do
		
		local function Example1()
			--do stuff
			print(v)
		end
		
		local function Example2()
			--also do stuff
		end
		Example1()
        Example2()
	end
end

Example()

into this

local Table = {1,2,3}

--Only uses one "for loop"
local function Example()
	for i, v in pairs(Table) do
	    --do stuff
		print(v)
		--also do stuff
	end
end

Example()

It does the exact same thing but it’s a lot cleaner and it’s not recreating the function variables and calling them every loop.

4 Likes

Generally in a programming sense using “nested” functions is frowned upon for majority of use cases. The only reason I’d really see the potential need to use nested functions is Aliases which I personally think is still unnecessary.

Some languages allow it and others don’t. I recommend reading the Lua writing style guildlines, or for Roblox specifically Roblox’s Lua Style Guide

Code Readability is just as important as having functional code.

Cheers!

“The only reason I’d really see the potential need to use nested functions is Aliases which I personally think is still unnecessary.”
To add on, a use case may be to hide functions, which I am unsure if they show up regardless of being nested or not. But I’d assume local function foo () already isolates the function.