Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

What is the definition of optimization in code?

Say I wanted to optimize this how do I start optimizing?


Tool.Equipped:connect(function()
        Book.Transparency = 1
        wait(1)
        Book.Transparency = 0
        wait(1)
       Book.Transparency = 1
        wait(1)
        Book.Transparency = 0
        wait(1)
       Book.Transparency = 1
        wait(1)
        Book.Transparency = 0
        wait(1)
end)

Please understand that I don't even know what optimizing is!

2 answers

Optimizing your code is basically just making it as efficient and effective as possible! This means eradicating any deprecated functions, and using the best methods to go about doing tasks in your code.

Your code is a terrific example of something that can be optimized. The same code repeats. In cases like this, you can almost always use a loop. Loops loop over a section of code multiple times.

In this case, a numerical for loop would be perfect. You're "flickering" the Book's Transparency on and off 3 times. The syntax for a numerical for looks like this: for [variable] = [start],[end],[increment] do

[variable] is the variable you will set to track the iteration of the for loop. [start] is the starting point, [end] is the finishing point, and [increment] is how the for loop will count from [start] to [end]. The increment is defaulted to 1 if it's not provided.

So, since you're trying to count to 3, for 3 times of repeating the same code it would look like this:

Tool.Equipped:connect(function()
	for i = 1,3 do
		Book.Transparency = 1
		wait(1)
		Book.Transparency = 0
		wait(1)
	end
end)

1 comment

0

thx

"Optimizing" comes from the word optimal, meaning the best possible solution.

In programming, an "optimal" program is the one that uses the least amount of resources; the least time, power, and memory.

The first rule of optimization: Don't optimize

The second rule of optimization: Warning: For experts only: Don't optimize yet

The third rule of optimization: Profile before optimizing

-Michael Jackson [^1]

[^1]: The British computer scientist, not the popstar

Making your code blazingly fast is rarely worth your time. It's not worth the headaches that it will cause when you have to go back to code that was optimized solely for speed over more reasonable considerations, like the fact that you will have to go back and modify that code one day, and you will have to make sense of the mess you left behind all in the name of "speed" that you didn't need.


Instead, write good, clean code.

Clean code reads like well written prose -Grady Booch

There are lots of principles that you can apply to write clean code. Here's a few general guidelines. These all have exceptions, but are often good advice (especially for beginners)

  1. Less code is better. The best code is the code you don't have to write.
  2. Use many small functions to break up code
  3. Use function parameters and return instead of assigning to and reading from global variables
  4. Use local variables and don't use _G. Define variables as late as possible, in as small as possible of a scope.
  5. DRY -- Don't repeat yourself. If you find yourself repeating yourself (like you are in what you posted) stop and fix it. Use loops or functions.
  6. Make use of objects, dictionaries, and lists. Instead of having many variables, have a few variables that have properties. Be consistent with how you use objects.
  7. Use good, descriptive names. Spend time thinking about names. Don't use one-letter names like v or abbreviations like plr .

1 comment

0

thx

This discussion is preserved as it was. New answers, comments, and votes are closed.