Cleaning up code?

So I have a lot of repeat type of code, example below, and I’m just wondering if there is any way to simplify it

This for some reason really bothers me, and if there is anyway to make it into one function please let me know

Code:



	if plyHealth != 0 then
		draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, Color(255,255,255,50), TEXT_ALIGN_CENTER)
	end
	if plyHealth <= 75 then
		draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, Color(255,112,112,255), TEXT_ALIGN_CENTER)
	end
	if plyHealth <= 50 then
		draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, Color(255,91,91,255), TEXT_ALIGN_CENTER)
	end
	if plyHealth <= 25 then
		draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, Color(255,58,58,255), TEXT_ALIGN_CENTER)
	end
	if plyHealth <= 10 then
		draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, Color(221,17,17,255), TEXT_ALIGN_CENTER)
	end


[lua]
local colors = {Color(221,17,17,255), Color(255,58,58,255), Color(255,91,91,255), Color(255,112,112,255), Color(255,255,255,50)}
local colorToUse = colors[(plyHealth > 10 and math.floor((plyHealth)/25)+2 or 1)]

draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, colorToUse, TEXT_ALIGN_CENTER)
[/lua]

why disagree? the code will do exactly what he’s doing in 3 lines

Why not do something like this:


local boxColor = Color(255,255,255,50)

if plyHealth <= 10
  boxColor = Color(221,17,17,255)
elseif plyHealth <= 25 then
  boxColor = Color(255,58,58,255)
elseif plyHealth <= 50 then
  boxColor = Color(255,91,91,255)
elseif plyHealth <= 75 then
  boxColor = Color(255,112,112,255)
end

draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, boxColor, TEXT_ALIGN_CENTER)

FYI, your code is drawing multiple boxes but you can’t see it because the dimensions are the same. If the **plyHealth **variable is equal to **5 **for example, every single one of those conditions is being met (5 != 0, 5 <= 75, etc). Are you purposefully wanting to draw the first transparent box with the others?

Works, thanks m8

That’s much messier imo. You can do much cleaner logic with a table of colours. It also prevents having to create Color objects every time.



if health <= maxHealth then
	local i = maxHealth - health
	draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, Color( 73 + i, 255 - i, 73, 200 ), TEXT_ALIGN_CENTER )
end


I think this way is much smoother and looks nicer imo. If you want it to change much more red much faster, you can put the i’s in a bracket and times it by a number.
Example:



if health <= maxHealth then
	local i = maxHealth - health
	draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, Color( 73 + ( i*1.5 ), 255 - ( i*2 ), 73, 200 ), TEXT_ALIGN_CENTER )
end


Generally speaking, the best way to get rid of repeating code is to put that code inside of a function and call that function whenever you need your addon to do that thing.

In some cases (this one, for example), however, it’s not needed. Here you just store the Color you use into a variable and then use it when calling draw.RoundedBox(…). There are some good examples posted above.

I personally prefer ‘…and…or…’ over ‘if…else…’ when working with variables, but in some cases it makes code harder to understand.



draw.RoundedBox(0, ScrW() / 2 - 175, ScrH() - 45, (350) * plyHealth / 100, 25, Color(plyHealth*2.55,plyHealth,plyHealth,255), TEXT_ALIGN_CENTER)


I have two suggestions for you.

  1. https://www.lua.org/pil/2.5.html
  2. https://www.lua.org/pil/3.1.html