Ways to Simplify or Improve code

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

Already has been said.

And I already know what it is, I dont need to know.

1 Like

Yes, but it is still in the main post? You could edit the post with the code example and explanation I gave, because most people will not scroll through 40 replies to gather information that should be on the OP.

I gave a Topic I made back in November that has both Graphemes ans sub, I only showed one of them as an example, not both, but sure. I’ll have to do it later.

(Cannot spell)

I should let ya know that this is actually pretty bad advice. Errors should be reserved only for irreversible states. Instead, you can return a boolean success state before or after the rest of your return tuple.

For example, let’s say that you have an inventory system that requires some sort of table. If it doesn’t exist, should you error? No, you replace the parameter with a placeholder value. Or, you can return an unsuccessful boolean state

You’re also wrong on this, since you’re actually making it too complex. Just do math.clamp(value, min, max)

Or alternatively:

local Hours = Seconds % ((60 ^ 2) * 24) -- If needed
local Minutes = Seconds % (60 ^ 2) -- 60 seconds per minute, 60 minutes per hour
local Seconds = Seconds % 60 -- 60 seconds per minutes

Or, you can change the text label’s property, MaxVisibleGraphemes, which simplifies it, and can be tweened.

1 Like

Basically.
Already.
Said.
By.
People.

I keep getting Replies over the same exact things that were said prior.

Sort of, Depends on the Context tho you said one of them so.

Well, shouldn’t you fix the post then?

No.

1 Like

I rather have people see the Original to them than just replace it, so People can actually see what’s wrong with it, and fix it, according to replies, also, it would make the replies seem out of context.

Ok.

You’ll keep getting these replies then, so don’t complain to me about it.

You should read what I posted in a programming guidelines post I made a while back:

2 Likes

Ok.

Ok!