Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Suggestion] Prefix or Localize Global Variables in ox_lib to Prevent Variable Shadowing #456

Closed
jnccloud opened this issue Nov 16, 2023 · 2 comments

Comments

@jnccloud
Copy link

Is your feature request related to a problem? Please describe.

I've spent several hours debugging a issue in my script, which boiled down to variable shadowing. My script used a global variable named Zones, and I encountered unexpected behaviors related to the creation and removal of zones using ox_lib's exports.

Discovery: When I renamed my script's global variable from Zones to JobZones, the erratic behavior was resolved. This led me to conclude that variable shadowing is occurring between my script and ox_lib. I used QBCore.Debug(Zones) and noticed there were both integer (1-5) and string (a,b,c,d,e from my script) keys in Zones.

Impact: While this might seem like a minor issue, it was HARD to diagnose. The collision was not immediately apparent until I looked into this file:

Describe the solution you'd like
I would suggest prefixing global variables or localizing when possible to help others avoid issues like this. I am not sure how widespread this practice is within the library.

I appreciate the work you guys put into these projects!

thelindat added a commit that referenced this issue Dec 6, 2023
@thelindat
Copy link
Member

I am not sure how widespread this practice

Aside from some special cases I do try to avoid globals; obviously I can't remove existing ones as that would break some resources.
Regardless I localised a few globals to prevent shadowing, but still need to add them to _ENV.

@jnccloud
Copy link
Author

I am not sure how widespread this practice

Aside from some special cases I do try to avoid globals; obviously I can't remove existing ones as that would break some resources. Regardless I localised a few globals to prevent shadowing, but still need to add them to _ENV.

Regardless, I appreciate the effort!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants