Converter Optimizing

Hi there, I have made a converter for real world units into roblox. I was wondering if someone could help me optimize my code as I cant think of whats unnecessary in it and whats not.

Currently the module is just way too many lines long and i was wondering if anyone could help me with shortening it.

local Converter = {}
local SpeedTypes = {
	"SPS",
	"KMH",
	"MPH",
	"KNOTS",
	"MACH",
}
local HeightTypes = {
	"Imperial",
	"Metric",
	"Studs",
}

local WeightTypes = {
	"Imperial",
	"Metric",
	"RoMass",
}

local TempNormal = 14.96
local TempPerThousand = 1.98
local MachNormal = 661.71
local MachPerThousand = 2.27

function Converter.GetMetricTemperature(alt: number, heighttype: string)
	if not alt then
		alt = 0
	end
	assert(type(alt) == "number", "Number expected got " .. type(alt))
	assert(type(alt) == "string", "String expected got " .. type(heighttype))
	local Temperature = TempNormal - ((Converter.GetImperialHeight(alt, heighttype) / 1000) * TempPerThousand)
	return Temperature
end

function Converter.GetImperialTemperature(alt: number, heighttype: string)
	if not alt then
		alt = 0
	end
	assert(type(alt) == "number", "Number expected got " .. type(alt))
	assert(type(alt) == "string", "String expected got " .. type(heighttype))
	return (Converter.GetMetricTemperature(alt, heighttype) * (9 / 5)) + 32
end

function Converter.GetSPS(spd: number, alt: number, speedtype: string, heighttype: string)
	if not alt then
		alt = 0
	end
	assert(type(spd) == "number", "Number expected got " .. type(spd))
	assert(type(alt) == "number", "Number expected got " .. type(alt))
	assert(type(speedtype) == "string", "String expected got " .. type(speedtype))
	assert(type(heighttype) == "string", "String expected got " .. type(heighttype))
	local Temperature = Converter.GetTemperature(alt)
	if speedtype == "SPS" then
		return spd
	end
	if speedtype == "KPH" then
		return spd / 1.0084
	end
	if speedtype == "MPH" then
		return (spd * 1.609) / 1.0084
	end
	if speedtype == "KNOTS" then
		return ((spd * 1.151) * 1.609) / 1.0084
	end
	if speedtype == "MACH" then
		return (
			(
				(
					spd
					- (Temperature - TempNormal)
						/ (MachNormal - (Converter.GetImperialHeight(alt, heighttype) / 1000) * MachPerThousand)
				) * 1.151
			) * 1.609
		) / 1.0084
	end
end

function Converter.GetKPH(spd: number, alt: number, speedtype: string, heighttype: string)
	if not alt then
		alt = 0
	end
	assert(type(spd) == "number", "Number expected got " .. type(spd))
	assert(type(alt) == "number", "Number expected got " .. type(alt))
	assert(type(speedtype) == "string", "String expected got " .. type(speedtype))
	assert(type(heighttype) == "string", "String expected got " .. type(heighttype))
	local Temperature = Converter.GetTemperature(alt)
	if speedtype == "SPS" then
		return spd * 1.0084
	end
	if speedtype == "KPH" then
		return spd
	end
	if speedtype == "MPH" then
		return spd * 1.609
	end
	if speedtype == "KNOTS" then
		return (spd * 1.151) * 1.609
	end
	if speedtype == "MACH" then
		return spd
			- (
					(Temperature - TempNormal)
					/ (MachNormal - (Converter.GetImperialHeight(alt, heighttype) / 1000) * MachPerThousand)
					* 1.151
				)
				* 1.609
	end
end

function Converter.GetMPH(spd: number, alt: number, speedtype: string, heighttype: string)
	if not alt then
		alt = 0
	end
	assert(type(spd) == "number", "Number expected got " .. type(spd))
	assert(type(alt) == "number", "Number expected got " .. type(alt))
	assert(type(speedtype) == "string", "String expected got " .. type(speedtype))
	assert(type(heighttype) == "string", "String expected got " .. type(heighttype))
	local Temperature = Converter.GetTemperature(alt)
	if speedtype == "SPS" then
		return (spd * 1.0084) / 1.609
	end
	if speedtype == "KPH" then
		return spd / 1.609
	end
	if speedtype == "MPH" then
		return spd
	end
	if speedtype == "KNOTS" then
		return spd * 1.151
	end
	if speedtype == "MACH" then
		return (
			spd
			- (Temperature - TempNormal)
				/ (MachNormal - (Converter.GetImperialHeight(alt, heighttype) / 1000) * MachPerThousand)
				* 1.151
		)
	end
end

function Converter.GetKnots(spd: number, alt: number, speedtype: string, heighttype: string)
	if not alt then
		alt = 0
	end
	assert(type(spd) == "number", "Number expected got " .. type(spd))
	assert(type(alt) == "number", "Number expected got " .. type(alt))
	assert(type(speedtype) == "string", "String expected got " .. type(speedtype))
	assert(type(heighttype) == "string", "String expected got " .. type(heighttype))
	local Temperature = Converter.GetTemperature(alt, heighttype)
	if speedtype == "SPS" then
		return ((spd * 10084) / 1.609) / 1.151
	end
	if speedtype == "KPH" then
		return (spd / 1.609) / 1.151
	end
	if speedtype == "MPH" then
		return spd / 1.151
	end
	if speedtype == "KNOTS" then
		return spd
	end
	if speedtype == "MACH" then
		return spd
			- (Temperature - TempNormal)
				/ (MachNormal - (Converter.GetImperialHeight(alt, heighttype) / 1000) * MachPerThousand)
	end
end

function Converter.GetMach(spd: number, alt: number, speedtype: string, heighttype: string)
	if not alt then
		alt = 0
	end
	assert(type(spd) == "number", "Number expected got " .. type(spd))
	assert(type(alt) == "number", "Number expected got " .. type(alt))
	assert(type(speedtype) == "string", "String expected got " .. type(speedtype))
	assert(type(heighttype) == "string", "String expected got " .. type(heighttype))
	if speedtype == "SPS" then
		spd = Converter.GetKnots(spd, "SPS")
	end
	if speedtype == "KPH" then
		spd = Converter.GetKnots(spd, "KPH")
	end
	if speedtype == "MPH" then
		spd = Converter.GetKnots(spd, "MPH")
	end
	if not table.find(SpeedTypes, speedtype) then
		error("Invalid Speed Type " .. speedtype .. " Given.")
	end
	local Temperature = Converter.GetTemperature(alt)
	local ActualMachGeneration = spd
		+ (Temperature - TempNormal)
			/ (MachNormal - (Converter.GetImperialHeight(alt, heighttype) / 1000) * MachPerThousand)
	return ActualMachGeneration
end

function Converter.GetImperialHeight(height: number, heighttype: string)
	if not heighttype then
		error("No Height Type Given.")
	end
	assert(type(height) == "number", "Number expected got " .. type(height))
	assert(type(heighttype) == "string", "String expected got " .. type(heighttype))
	if not table.find(HeightTypes, heighttype) then
		error("Invalid Height Type Given.")
	end

	if string.lower(heighttype) == "studs" then
		return (height * (3 + (1 / 3))) * 3.281
	end
	if string.lower(heighttype) == "metric" then
		return height / 3.281
	end
	if string.lower(heighttype) == "imperial" then
		return height
	end
end

function Converter.GetMetricHeight(height: number, heighttype: string)
	if not heighttype then
		error("No Height Type Given.")
	end
	assert(type(height) == "number", "Number expected got " .. type(height))
	assert(type(heighttype) == "string", "String expected got " .. type(heighttype))
	if not table.find(HeightTypes, heighttype) then
		error("Invalid Height Type Given.")
	end

	if string.lower(heighttype) == "studs" then
		return height * (3 + (1 / 3))
	end
	if string.lower(heighttype) == "metric" then
		return height
	end
	if string.lower(heighttype) == "imperial" then
		return height / 3.281
	end
end

function Converter.GetRobloxHeight(height: number, heighttype: string)
	if not heighttype then
		error("No Height Type Given.")
	end
	assert(type(height) == "number", "Number expected got " .. type(height))
	assert(type(heighttype) == "string", "String expected got " .. type(heighttype))
	if not table.find(HeightTypes, heighttype) then
		error("Invalid Height Type Given.")
	end

	if string.lower(heighttype) == "studs" then
		return height
	end
	if string.lower(heighttype) == "metric" then
		return height / (3 + (1 / 3))
	end
	if string.lower(heighttype) == "imperial" then
		return (height / (3 + (1 / 3))) * 3.281
	end
end

function Converter.GetRoMass(weight: number, weighttype: string)
	if not table.find(WeightTypes, weighttype) then
		return
	end
	assert(type(weight) == "number", "Number expected got " .. type(weight))
	assert(type(weighttype) == "string", "String expected got " .. type(weighttype))
	if weighttype == "RoMass" then
		return weight
	end
	if weighttype == "Imperial" then
		return weight / 77
	end
	if weighttype == "Metric" then
		return weight / 35
	end
end

function Converter.GetImperialMass(weight: number, weighttype: string)
	if not table.find(WeightTypes, weighttype) then
		return
	end
	assert(type(weight) == "number", "Number expected got " .. type(weight))
	assert(type(weighttype) == "string", "String expected got " .. type(weighttype))
	if weighttype == "RoMass" then
		return weight * 77
	end
	if weighttype == "Imperial" then
		return weight
	end
	if weighttype == "Metric" then
		return weight * 2.2
	end
end

function Converter.GetMetricMass(weight: number, weighttype: string)
	if not table.find(WeightTypes, weighttype) then
		return
	end
	assert(type(weight) == "number", "Number expected got " .. type(weight))
	assert(type(weighttype) == "string", "String expected got " .. type(weighttype))
	if weighttype == "RoMass" then
		return weight * 35
	end
	if weighttype == "Imperial" then
		return weight / 2.2
	end
	if weighttype == "Metric" then
		return weight
	end
end

return Converter
1 Like

exotic memes co has arived and has made your code exactly one line long,

local Converter={}local SpeedTypes={'SPS','KMH','MPH','KNOTS','MACH',}local HeightTypes={'Imperial','Metric','Studs',}local WeightTypes={'Imperial','Metric','RoMass',}local TempNormal=1496 local TempPerThousand=198 local MachNormal=66171 local MachPerThousand=227 function Converter.GetMetricTemperature(alt:number,heighttype:string)if not alt then alt=0 end assert(type(alt)=='number','Number expected got '..type(alt))assert(type(alt)=='string','String expected got '..type(heighttype))local Temperature=TempNormal-((Converter.GetImperialHeight(alt,heighttype)/1000)*TempPerThousand)return Temperature end function Converter.GetImperialTemperature(alt:number,heighttype:string)if not alt then alt=0 end assert(type(alt)=='number','Number expected got '..type(alt))assert(type(alt)=='string','String expected got '..type(heighttype))return(Converter.GetMetricTemperature(alt,heighttype)*(9/5))+32 end function Converter.GetSPS(spd:number,alt:number,speedtype:string,heighttype:string)if not alt then alt=0 end assert(type(spd)=='number','Number expected got '..type(spd))assert(type(alt)=='number','Number expected got '..type(alt))assert(type(speedtype)=='string','String expected got '..type(speedtype))assert(type(heighttype)=='string','String expected got '..type(heighttype))local Temperature=Converter.GetTemperature(alt)if speedtype=='SPS'then return spd end if speedtype=='KPH'then return spd/10084 end if speedtype=='MPH'then return(spd*1609)/10084 end if speedtype=='KNOTS'then return((spd*1151)*1609)/10084 end if speedtype=='MACH'then return(((spd-(Temperature-TempNormal)/(MachNormal-(Converter.GetImperialHeight(alt,heighttype)/1000)*MachPerThousand))*1151)*1609)/10084 end end function Converter.GetKPH(spd:number,alt:number,speedtype:string,heighttype:string)if not alt then alt=0 end assert(type(spd)=='number','Number expected got '..type(spd))assert(type(alt)=='number','Number expected got '..type(alt))assert(type(speedtype)=='string','String expected got '..type(speedtype))assert(type(heighttype)=='string','String expected got '..type(heighttype))local Temperature=Converter.GetTemperature(alt)if speedtype=='SPS'then return spd*10084 end if speedtype=='KPH'then return spd end if speedtype=='MPH'then return spd*1609 end if speedtype=='KNOTS'then return(spd*1151)*1609 end if speedtype=='MACH'then return spd-((Temperature-TempNormal)/(MachNormal-(Converter.GetImperialHeight(alt,heighttype)/1000)*MachPerThousand)*1151)*1609 end end function Converter.GetMPH(spd:number,alt:number,speedtype:string,heighttype:string)if not alt then alt=0 end assert(type(spd)=='number','Number expected got '..type(spd))assert(type(alt)=='number','Number expected got '..type(alt))assert(type(speedtype)=='string','String expected got '..type(speedtype))assert(type(heighttype)=='string','String expected got '..type(heighttype))local Temperature=Converter.GetTemperature(alt)if speedtype=='SPS'then return(spd*10084)/1609 end if speedtype=='KPH'then return spd/1609 end if speedtype=='MPH'then return spd end if speedtype=='KNOTS'then return spd*1151 end if speedtype=='MACH'then return(spd-(Temperature-TempNormal)/(MachNormal-(Converter.GetImperialHeight(alt,heighttype)/1000)*MachPerThousand)*1151)end end function Converter.GetKnots(spd:number,alt:number,speedtype:string,heighttype:string)if not alt then alt=0 end assert(type(spd)=='number','Number expected got '..type(spd))assert(type(alt)=='number','Number expected got '..type(alt))assert(type(speedtype)=='string','String expected got '..type(speedtype))assert(type(heighttype)=='string','String expected got '..type(heighttype))local Temperature=Converter.GetTemperature(alt,heighttype)if speedtype=='SPS'then return((spd*10084)/1609)/1151 end if speedtype=='KPH'then return(spd/1609)/1151 end if speedtype=='MPH'then return spd/1151 end if speedtype=='KNOTS'then return spd end if speedtype=='MACH'then return spd-(Temperature-TempNormal)/(MachNormal-(Converter.GetImperialHeight(alt,heighttype)/1000)*MachPerThousand)end end function Converter.GetMach(spd:number,alt:number,speedtype:string,heighttype:string)if not alt then alt=0 end assert(type(spd)=='number','Number expected got '..type(spd))assert(type(alt)=='number','Number expected got '..type(alt))assert(type(speedtype)=='string','String expected got '..type(speedtype))assert(type(heighttype)=='string','String expected got '..type(heighttype))if speedtype=='SPS'then spd=Converter.GetKnots(spd,'SPS')end if speedtype=='KPH'then spd=Converter.GetKnots(spd,'KPH')end if speedtype=='MPH'then spd=Converter.GetKnots(spd,'MPH')end if not table.find(SpeedTypes,speedtype)then error('Invalid Speed Type '..speedtype..' Given.')end local Temperature=Converter.GetTemperature(alt)local ActualMachGeneration=spd+(Temperature-TempNormal)/(MachNormal-(Converter.GetImperialHeight(alt,heighttype)/1000)*MachPerThousand)return ActualMachGeneration end function Converter.GetImperialHeight(height:number,heighttype:string)if not heighttype then error('No Height Type Given.')end assert(type(height)=='number','Number expected got '..type(height))assert(type(heighttype)=='string','String expected got '..type(heighttype))if not table.find(HeightTypes,heighttype)then error('Invalid Height Type Given.')end if string.lower(heighttype)=='studs'then return(height*(3+(1/3)))*3281 end if string.lower(heighttype)=='metric'then return height/3281 end if string.lower(heighttype)=='imperial'then return height end end function Converter.GetMetricHeight(height:number,heighttype:string)if not heighttype then error('No Height Type Given.')end assert(type(height)=='number','Number expected got '..type(height))assert(type(heighttype)=='string','String expected got '..type(heighttype))if not table.find(HeightTypes,heighttype)then error('Invalid Height Type Given.')end if string.lower(heighttype)=='studs'then return height*(3+(1/3))end if string.lower(heighttype)=='metric'then return height end if string.lower(heighttype)=='imperial'then return height/3281 end end function Converter.GetRobloxHeight(height:number,heighttype:string)if not heighttype then error('No Height Type Given.')end assert(type(height)=='number','Number expected got '..type(height))assert(type(heighttype)=='string','String expected got '..type(heighttype))if not table.find(HeightTypes,heighttype)then error('Invalid Height Type Given.')end if string.lower(heighttype)=='studs'then return height end if string.lower(heighttype)=='metric'then return height/(3+(1/3))end if string.lower(heighttype)=='imperial'then return(height/(3+(1/3)))*3281 end end function Converter.GetRoMass(weight:number,weighttype:string)if not table.find(WeightTypes,weighttype)then return end assert(type(weight)=='number','Number expected got '..type(weight))assert(type(weighttype)=='string','String expected got '..type(weighttype))if weighttype=='RoMass'then return weight end if weighttype=='Imperial'then return weight/77 end if weighttype=='Metric'then return weight/35 end end function Converter.GetImperialMass(weight:number,weighttype:string)if not table.find(WeightTypes,weighttype)then return end assert(type(weight)=='number','Number expected got '..type(weight))assert(type(weighttype)=='string','String expected got '..type(weighttype))if weighttype=='RoMass'then return weight*77 end if weighttype=='Imperial'then return weight end if weighttype=='Metric'then return weight*22 end end function Converter.GetMetricMass(weight:number,weighttype:string)if not table.find(WeightTypes,weighttype)then return end assert(type(weight)=='number','Number expected got '..type(weight))assert(type(weighttype)=='string','String expected got '..type(weighttype))if weighttype=='RoMass'then return weight*35 end if weighttype=='Imperial'then return weight/22 end if weighttype=='Metric'then return weight end end return Converter

please note that our technologies have won 0 nobel prizes.

im getting paid by the line here!!!