Shoud I check all of params?

For example this is the part of my code

HitboxSystem

type Hitbox = {
	player: Player,
	hitboxId: string,
	originPart: BasePart,
	offset: CFrame,
	size: Vector3,
}

local RegisteredHitboxes: { [string]: Hitbox } = {}

function HitboxSystem.register(params: hitboxParams)
	local hitbox: Hitbox = {
		player = params.player,
		hitboxId = params.hitboxId,
		originPart = params.originPart,
		offset = params.offset,
		size = params.size,
	}

	RegisteredHitboxes[params.hitboxId] = hitbox
end

Should I check all like this?

if typeof(params.hitboxId) ~= "string" then
	warn("The type of hitbox id isn't match.")
	return
end

--And

if not params.hitboxId then
	warn("Hitbox id is nil.")
	return
end

No. If you’re validating something like params.hitboxId, just checking the type is enough. typeof(nil) won’t match “string”, so it’ll catch both wrong types and missing values.

Instead of writing both:

if not params.hitboxId then
...
if typeof(params.hitboxId) ~= "string" then

Just do

if typeof(params.hitboxId) ~= "string" then
	warn("Invalid hitboxId (expected string)")
	return
end
1 Like