Any way to further simplify/shorten this code?

Code for copy paste
local function updateHighlight(highlight)
	if highlight then
		highlight.Parent = nil
		highlight.Parent = workspace
	end
end

local function changeThe1of2to3andSaveTo4(property, input, new, inputTable, highlight) -- changeThe1of2to3andSaveTo4andOptionallyUpdate5
	
	boxes[input] = input
	
	inputTable[input] = input[property]
	input[property] = new
	
	updateHighlight(highlight)
	
end

local function revertThe1of2from3(property, input, inputTable, highlight) -- revertThe1of2from3andOptionallyUpdate4
	
	boxes[input] = nil
	
	input[property] = inputTable[input]
	inputTable[input] = nil
	
	updateHighlight(highlight)

end

Thanks!

i think this is already as simple as you can get it

1 Like

If you wish to shorten it, the main way I can think of is to call one of the functions from the other, as they contain almost identical code.

e.g.

local function updateHighlight(highlight)
	if highlight then
		highlight.Parent = nil
		highlight.Parent = workspace
	end
end
local function changeThe1of2to3andSaveTo4(property, input, new, inputTable, highlight) -- changeThe1of2to3andSaveTo4andOptionallyUpdate5
	
	boxes[input] = input
	
	inputTable[input] = input[property]
	input[property] = new
	
	updateHighlight(highlight)
	
end
local function revertThe1of2from3(property, input, inputTable, highlight) -- revertThe1of2from3andOptionallyUpdate4
	changeThe1of2to3andSaveTo4(property, input, nil, inputTable, highlight)
end

If you are willing to change the order that you supply the parameters in, you can also do this:

local function updateHighlight(highlight)
	if highlight then
		highlight.Parent = nil
		highlight.Parent = workspace
	end
end
local function changeThe1of2to3andSaveTo4(property, input, inputTable, highlight, new) -- changeThe1of2to3andSaveTo4andOptionallyUpdate5
	
	boxes[input] = input
	
	inputTable[input] = input[property]
	input[property] = new
	
	updateHighlight(highlight)
	
end

local function revertThe1of2from3(...) -- revertThe1of2from3andOptionallyUpdate4
	changeThe1of2to3andSaveTo4(...) -- optionally add a `nil` after the ...
end

You can then shorten it further to be

local revertThe1of2from3 = changeThe1of2to3andSaveTo4andOptionallyUpdate5
1 Like

I don’t think this’ll work, since in the first table it’s:

inputTable[input] = input[property]

and in the second table it’s

input[property] = inputTable[input]

aka the inverse, which is exactly what I want since I’m putting it back the way it was.
I don’t see how what you’re suggesting will work unless I’m missing something…

Thanks for the suggestion though!

Ah, I missed that part.
You could still put the common code into a new function, then just have the main functions handle the switching bit.

No worries :p
Yep, I guess I can do that. Thanks! :D