Variables 101 - When to make them?

I still see new scripters not being able to variables at the correct time. Here’s an example code of what I mean.

game.Workspace.Model.Value = game.Workspace.Model.Value + game.Workspace.Model.Value / 5

I usually see this kind of code in a scripting support server.

When to use variables
You should be using variables when you have to index/make this thing more than once.

local Stamina = 100
local Vector0 = Vector3.new() -- made a reference to it since creating a new vector every frame isn't the smartest thing to do

game:GetService("RunService").Heartbeat:Connect(function()
    Stamina += HumanoidMoveDir ~= Vector0 and 5 or -2.5 -- assuming we had a reference to the humanoid and stuff
end)

I was scrolling through the scripting support part of the devforum and found a post with code that cached(made a variable for) workspace. That is weird since referencing workspace will give you the workspace itself.

Somewhat unnecessary caching
So like mentioned above, there would be times where you don’t need to cache that reference at all for good reasons. According to Luau optimizations, some libraries are imported which means that you don’t need to cache the functions. I said some since it wasn’t specified, but it had an example with the math library so we can safely assume that the math library is imported.

local max = math.max -- it wouldn't change anything now


source

Hopefully you learned when to use variables from this post.

11 Likes

Hm, Seems alright. Just make the title brief, yet informative.

Have any ideas on what the title should be?

Hm, If i did this, i would name it: “Variables 101” or something like that.

Seems great except most ppl would think they already know about variables which usually isn’t the case.

Perhaps something like Times when to use or not use variables?

EDIT
Just a thought, but what about combining @cool900s 's title? Something like Variables 101 - When to utilize them?

I sort of specified to use variables when you’re using something more than once

When to use variables
You should be using variables when you have to index/make this thing more than once.

Is there anything wrong with making workspace a variable?

Well yes, you’re caching unnecessary memory. workspace is already a global variable that is a reference to game.Workspace

Ooh I like that idea, very neat.

1 Like

Is it even a significant amount?

I personally do something like

local Workspace = game:GetService("Workspace")

so it is more consistent with other code where I do the same for other services

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

etc

1 Like

that works just fine, but did you know you could just do

print(workspace.Part) -- you dont even have to use GetService
print(game:GetService("Workspace").Part)

I am well aware of that. I prefer consistency though, makes code more readable and maintainable in my opinion and experience.

2 Likes

respectable, just thought i would tell you just in case you didnt know about it, you would save a lot of unnecessary typing :cowboy_hat_face:

I don’t think the term ‘unnecessary’ should be used. The difference is not documented anywhere, and they’re all valid.

You don’t need documentations, workspace is already a variable, what’s the use of making another one + doing a function call.

It’s primarily a readability factor.

If all services that are being defined are using :GetService(), using workspace hastily becomes an anti-stylistic pattern in your codebase. Parallelism for assigning services would be lacking.

You could argue that you can shred off a few microseconds or femtoseconds, but to a normal human, the performance gain is practically unnoticeable.

2 Likes

I disagree on you with that so much but, you do you I guess