How can I downsize on this segment of code?

Hello everyone! Happy new year! I have been writing my code like this for a while when needed and I have hated it. I am quite the clean freak and I feel like there is a great way to downsize but I cant find it anywhere. Here is the code:

if KitRarity == "Basic" then
		RequiredDiamonds = 10000
		WaitTime = 900
	elseif KitRarity == "Rare" then
		RequiredDiamonds = 20000
		WaitTime = 3600
	elseif KitRarity == "Epic" then
		RequiredDiamonds = 50000
		WaitTime = 14400
	elseif KitRarity == "Legendary" then
		RequiredDiamonds = 1000000
		WaitTime = 43200
	elseif KitRarity == "Ancient" then
		RequiredDiamonds = 500000
		WaitTime = 86400
	elseif KitRarity == "Exotic" then
		RequiredDiamonds = 1000000
		WaitTime = 259200
	end

A lot of these problems can be compactly solved with tables (and loops in some cases).

How would a “clean freak” go about this? :slight_smile:

KitRarities = {
	Basic = {
		RequiredDiamonds = 10000,
		WaitTime = 900
	};
	Rare = {
		RequiredDiamonds = 20000,
		WaitTime = 3600
	};
	Epic = {
		RequiredDiamonds = 50000,
		WaitTime = 14400
	};
	Legendary = {
		RequiredDiamonds = 1000000,
		WaitTime = 43200
	};
	Ancient = {
		RequiredDiamonds = 500000,
		WaitTime = 86400
	};
	Exotic = {
		RequiredDiamonds = 1000000,
		WaitTime = 259200
	};
}

local info = KitRarities[KitRarity]
--> info.RequiredDiamonds
--> info.WaitTime
-- . . .

And they would possibly reuse this table as the one and only source of this information.

Happy new year to you too!

1 Like

Pretty much as clean as you can get, tables and stuff wont really save you from much size. If you don’t like this code you can make value instances and read them.

1 Like

I know I was talking about “downsizing” but this works a lot better! I don’t know what about it helps, but it just does! Thank you!!!

1 Like

Glad you like it. As far as actual downsizing goes, it isn’t really visible in an individual use case. All the information has to be available one way or another, but we strive for approaches a principled programmer would use, such as DRY (don’t repeat yourself).

It’s completely fine if you use if-else statements for short options, like if, ifelse, and else, but they can get too repetitive and even unreadable. In scenarios like this where constant data is used, it’s usually a good idea to form a table and reuse this table everywhere. It can be it’s own module. Then any changes to this module apply to any code reading from it. It can be placed into a replicated server and used both by the server and clients.

Lastly, in terms of consumed memory, if-else statements should occupy a bit less space, but the numbers and benefits turn if the module is used in a lot of different places. Not that the used memory is a significant factor here in any way.

We just try to write code that is balanced between great performance and readability, and maintainability.

Update. Rephrased individual sentences.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.