Ways to Simplify or Improve code

It’s a good guide!

I’d change the bit about using error(); I almost never use error() because it terminates the entire thread for that script and halts execution. It’s OK to use in a pcall() but I don’t recommend using it outside of that.

Its mainly about the usage of assert() by yeah, you can just use pcall()

Because it isnt

assert() errors if the statement inside is false, also it is about assert() so idk what youre complaining about

Just use warn if you want, just dont complain about it.

Isnt that the point?

Late reply but the for loop would actually be more preferable, mainly because you are able to decide what can and what cant be deleted while ClearAllChildren() would delete everything.

Again, Late Reply but:

It isn’t wrong, nobody said it was wrong, I’m saying it isnt recommended

Array, function.

Yes, but still it doesnt seem nessacary to have that many conditions for one if statement, also, I’m referring these kinds of things:

local random = math.random(1, 10)

if random == 1 then

elseif random == 2 then

elseif random == 3 then

elseif random == 4 then

-- and so on

Which isnt really nessacary, it can be Changed to just an Array or function used for all of them

1 Like

Can I have a code sample on what you mean?

What makes assert bad practice?

This method does not properly deal with strings which have multiple unicode characters in one grapheme, like the string “Em biết nấu ăn.” Trying to use your typewriter effect with this results in a bunch of weird characters popping up, as the diacritics have their own separate characters from the letters. To properly deal with this, please use MaxVisibleGraphemes

I have made a Tutorial on that, And I said you can looknat it if you want, I even gave the link to it within the that example if you actually read it.

Its slower (which i confirmed myself), and (This is from him so I cant really say if its true or not) is appearently not useful for handling things.

Then why not just show the correct method in the tutorial?

Because both are the “correct method”, So im not sure where you getting that its wrong, but The Tutorial I posted shows both of them.

I literally just commented why the string.sub solution is incorrect. If you think this is just some issue with other languages which you obviously will never use:

  1. Why not just future proof your work so you can translate your game into other languages?
  2. It’s not just other languages; multiple emojis like the flag emojis and family emojis use multiple characters per grapheme.

I can read, and I know that it isnt an issue with other languages, it doesnt mean its “incorrect”. use MaxVisibleGraphemes, I dont really understand why you have to be upset about it when its as simple as that

Plus, Argue somewhere else. not here.

xGOA7x is probably talking about something like this:

Bunch of elseifs:

--math.random() isn't pre-seeded and has problems with very large numbers
local value = Random.new():NextInteger(1,20)
If value == 10 then
	RunFunction1(data) --data is whatever needs to be passed, doesn't matter in this example
elseif value == 14 then
	RunFunction2(data)
elseif value == 17 then
	RunFunction3(data)
elseif value == 19 then
	RunFunction4(data)
else
	RunNormalFunction(data)
end

Reduced elseifs:

local functionReferences = {
	[10] = RunFunction1; --don't add parentheses here since we don't want to run it, we want the reference
	[14] = RunFunction2;
	[17] = RunFunction3;
	[19] = RunFunction4;
}
local randomNumber = Random.new():NextInteger(1,20)
if functionReferences[randomNumber] then
	functionReferences[randomNumber](data)
else
	--Need to account for everything else. Probably don't want a bunch of the same reference for every possibility
	--Obviously this is really dependant per use-case. May need it, may not
	RunNormalFunction(data)
end

Working with references can be quite handy, but keep in mind they behave differently than values.

Sure, this method takes up memory, but it’s quite small since it doesn’t store the whole function, rather it’s just a reference to that function. I use this method from time-to-time, but IMHO if you prefer a bunch of elseifs then go for it; What is easy to read by one programmer may not be for the next and is not a method to gauge a programmer’s skill level. I believe most programmers code it to work firstly, then enter a clean-up phase of the code to increase readability and intuition.

But don’t design your own nailgun just to hammer in a single nail (don’t over-complicate). Can you do it? Absolutely…but should you? It’s vital to weigh the cost versus the benefit. Remember the most important cost that comes with increased complexity: your time … and there won’t be a parade marching down main street to honor how “perfect” the code is.

Many(most?) programmers have gaps in their knowledge because we tend to be self-taught, so even a truly gifted programmer can be “noob-like” in certain ways or have misinformation. I actually didn’t know modulus returns the numerator if the denominator is larger, but this makes sense since 10%9 and 10%100 would return the same number of .1…one less gap.

2 Likes

Why not just use this?

local Active = false -- Active is false

if not Active then -- if false of nil
    warn("Not active")
    return
end

This was already said by other people, I know.

It’s not necessarily bad, but it always evaluates the message even if the value passes, so if you were concatenating or formatting the message i.e.

assert(type(Name) == "string", "Expected string, got " .. type(Name))

You’ll still have to pay for the evaluation in production. Plus, assert doesn’t have a level parameter like error does which can help with abstracted modules.

One good thing about assert though is it does support type refinement unlike guard clauses.

local A: number? = 1
assert(A)
local B: number = A + 1 -- OK
1 Like

EDIT: nevermind, figured it out! That’s what the task.wait(seconds) is for!

Is there any way to speed up the typewriting effect? Am I just missing something? It just seems a bit slow, especially when typing out longer strings.

You can actually make the code even shorter using TextLabel.MaxVisibleGraphemes:

for MaxGraphemes = 0, #TextLabel.Text do
    task.wait(0.1)
    TextLabel.MaxVisibleGraphemes = MaxGraphemes
end

Basically, MaxVisibleGraphemes tells Roblox how many characters in the TextLabel it should actually try to render, but it doesn’t affect the positioning of the text. By default, it is set to -1, which disables it.

2 Likes