preface

“Do you know what it’s like in Los Angeles at four o ‘clock in the morning?” Bryant asked. I haven’t seen it but I often see it at four o ‘clock in Baiyun District of Guangzhou.

Is it in the morning sun watching the shadow following their own way: “get up earlier than the chicken, sleep later than the dog, maybe this is my last effort to struggle.”

Sleep wake up

I have a noon free half an hour to read the habit, that noon to see 1 o ‘clock, really a little sleepy, quickly closed the book lying on the chair to sleep, is dreaming! Suddenly a colleague (sister) pushed awake: “quick quick, get up to help me see, a problem!”

I opened my eyes to see colleagues have anxious sweating, tightly zhang looked at me, as if grasping a life-saving straw; Kind of teary-eyed.

“In a hurry, why is this? Was there an earthquake?”

“Wocao (estimate very anxious), not is not, the online activity has a problem, quick help me look, I looked several times, can not find the problem.”

“What activity, I don’t know?”

“It is a 520 Valentine’s Day activity that the operation gave two days ago.”

“Oh, what’s the problem on the line?”

“Online users send gifts of props every day, and some people’s rankings are distributed according to the first prize.”

“Really? How many users has it affected?”

“10,000 users.”

He looked confused. I was a little broken at that time, this affected the number of people plus props are valuable (RMB), won’t it be reported to the notice, deduction points. It suddenly occurred to me that many years ago, when I just entered the programmer industry, I lost 3W + and was complained by customers because I did code review. At that time, I was reported by the boss, operation manager and test manager. That night, I stood alone in a corner of the Bund, secretly wiping tears and being photographed. The bund cold wind, also cold my heart.

“What branch is the code in, send it to me so I can pull the code? Did you not give me the review code?”

“Feature /#87656-Valentine-Day, is this branch, enterprise wechat soon sent me.”

I looked at her helpless eyes, now become a general rely on.

“Don’t worry, I’ll look at the code first!”

I’ve probably looked at it for a few minutes (1000 + lines of code) and have basically identified the problem. I guess she saw me stop scrolling through the code, and it lightened the mood.

“Did you not see the Lua specification wiki documentation? Don’t you know that global variables are forbidden?”

“What? Am I using global variables? I don’t know, I’m useless, isn’t it the same?”

Looking at the hurried explanation, I began to speak but stopped. Is how to calmly talk to her about their own details, made a very low-level problem, could have been avoided but made a mistake.

“You put your head over here, here and here, right? You set the item global variable in the global, but you changed the value of the variable in the following query, so that all users who are not the first place get the first prize; Global data is tainted.”

Lua global variables and local variables in lua? Global variables: as the name implies, the life cycle is global, the entire Lua file can be accessed, set the full office variables can be accessed in the _G table through the table; There are variables that pollute files and are cumbersome to maintain. Local variables: variables that are valid only within certain limits, called local variables and modified with local; So you use local variables for easy maintenance.”

“Oh, oh, I see, I saw other people’s code also write this, so I copied, who knew this would happen.”

You can’t just CTRL + C + CTRL + V as a two year end developer. Documents also need to be read, I am like a big brother in educating little sister (96).

“I don’t want to. I just switched from Java to Lua. How do you know so much?”

Angry 😤 I, this is not into my heart! It’s here with the notes, and it’s marked in red: “Say important things three times.”

“Modify these places, submit the test environment, find the test again, this must appear the problem, did not test out! We need to review this problem later.”

“Oh!”

Once again a face meng force: this attitude change is too big, now the little girl are so arrogant? Taking on my new colleague is really killing me. Fortunately, it is a beautiful girl, or I would be angry to teach, it is our line of beautiful back-end girls too rare. The whole group is only a few girls, she is the most beautiful, must be a giant panda level protection.

“Zha, zha, don’t understand return can’t ask, who call me so vegetable? I’ll treat you to tea this afternoon.”

That’s how bold I am. I can’t stand it! However, I heard that there was afternoon tea and said, “Nothing, nothing, I will talk to you in detail about the use of global variables and local variables after I finish the online modification.”

In the evening twilight

Approaching to go off work, is concentrating on thinking about the problem, review what things have done today, what things have not finished, arrange the next tomorrow’s work! Suddenly I heard:

“Finished? I want to ask you about the bug problem at noon?”

“You don’t want to surprise me and frighten me, thinking about something?”

Most programmers have a lot of confidence in the code they write, and they often hear things like, “Oh my God, that’s impossible, I’ve tested it and it’s all right, can you reboot it or can you try again? In fact, I have secretly run the test environment to see, hurry to fix it. And then say to the test: “I really have no problem, you look again!” . Take the test: “Am I wrong? And said, “No problem.”

“I usually tell you to read more wiki documents, above the organizational structure chart, Redis cluster, mysql machine, middleware, remote live and so on, and even the pit you encounter, you should take the initiative to look at it and learn!

“Walk, walk, let’s take the computer to a small conference room chat!”

Neurotic: Must do so? Just sit in a chair and talk. What if someone sees it? Could it be misunderstood that I have feelings for her? There were many eyes staring at her in that large, covetous crowd, and she gave a shiver.

“Don’t make it so tense that I’m not used to it!”

“Simply talk, ask questions in a place where there are fewer people, don’t wait for everyone to hear, and say that MY food cut feet!”

“Did you know that lua basically disrecommends global variables in favor of local variables?”

“I don’t know. Tell me about it!”

# # that you want to remember, I tell you carefully again, next time you can't make such a low-level error # # not recommend the use of global variables for two reasons: (1) in order to avoid setting for global variable naming conflicts, make the pollution of the whole project, so try to use local variables (2) access to the local variable speed faster than global variablesCopy the code

“Can you explain why it affects the other files?”

print("* * * * * * * * * * * * * * * * * * * * * I am a global variable * * * * * * * * * * * * * * * * * * * * * * * * * *")
global_variable = function (...).
    name = "I'm Mu."
end
global_variable() Call function
print(name)

print("* * * * * * * * * * * * * * * * * * * * * I'm a local variable * * * * * * * * * * * * * * * * * * * * * * * * * *")

local_variable = function (...).
    local age = 26
end
local_variable() Call function
print(age)

print("* * * * * * * * * * * * * * * * * * * * * remove global variable value * * * * * * * * * * * * * * * * * * * * * * * * * *")
_G.name = nil
print(name)
Copy the code

“Let’s see what happens up here? “

“Must be I am Mu, 26, I am Mu! “

You almost gave me a heart attack. Check out the result set below.

* * * * * * * * * * * * * * * * * * * * * I am a global variable * * * * * * * * * * * * * * * * * * * * * * * * * * I am mu * * * * * o * * * * * * * * * * * * * * * * I'm a local variable * * * * * * * * * * * * * * * * * * * * * * * * * *nil

nil
Copy the code

“So amazing? How did the global variable end up with no value? “

“Do you want to know why? “

“Think about it. Tell me. It’s interesting.”

_G is a table that holds all the global functions and variables used by Lua. We can access the values in _G just like we can access table.

print("* * * * * * * * * * * * * * * * * * * * * _G table * * * * * * * * * * * * * * * * * * * * * * * * * *")
To verify that _G stores only global variables and functions, set a local variable first
local sex = 1
-- Large G table is a table with key-value pairs
for i,v in pairs(_G) do
    print(i,v)
end
Copy the code

Take a look at the execution result:

* * * * * * * * * * * * * * * * * * * * *_G************************** name I am A muThis is the global variable we just set
table           table: 0x7faba8c036b0
dofile          function: 0x1007b697d
assert          function: 0x1007b684e
_G              table: 0x7faba8c02770
string          table: 0x7faba8c04fc0
io              table: 0x7faba8c04260
ipairs          function: 0x1007b6aa8
tostring        function: 0x1007b72a8. .debug           table: 0x7faba8c04b80
require         function: 0x7faba8c03d30
Copy the code

“Oh oh, I see, I know why I had this problem in the morning activity, because I declared the global variable in the header of the file, and changed it in another method, which caused me to get the value changed again! “

“You are really smart, ‘a bit broken’ 🙄, I I I I, really too difficult. “

“Why, can’t you teach me like I’m a lua nerd? Besides, I’m still a girl. Don’t you have a little compassion? Hum ~ ~ ~”

“Fine, fine, can teach you! I see. Can we go home now? “

“Wait, I have another question. You said that accessing local variables is faster than accessing global variables. How do you understand this? 🤔”

“You this is don’t want me to go off work, I still want to go back to do a MAO blood flourishing eat! You know what? You’re the prettiest kid in the group.”

“I am the most beautiful girl in the whole group, not son! “

Lua uses a register-like virtual mode after version 5.0. â‘¡ Lua uses stacks to store its registers. Lua assigns a stack to each function to store the active data in the function. (3) Lua compilers store local variables in registers. Local variables operate on registers. Global variables need to be retrieved before they can be manipulated. Remember that memory access is not as fast as registers, and lua global variables are accessed through the table, equivalent to the middle of another layer of indirect access, must be much slower than local variables

“I have a little dizzy, can cite an example to prove that a pile of concepts as a case to the truth, hee hee 😬. “

print("* * * * * * * * * * * * * * * * * * * * * global variable and a local variable access speed * * * * * * * * * * * * * * * * * * * * * * * * * *"### global variable sum =0 Declare a global variable value
for i=1.100000 do
  sum = sum + i
end

print(sum)
print(os.clock()) Returns an approximation of the CPU usage of a program### Local variableslocal sum = 0  Declare a local variable value
for i=1.100000 do
  sum = sum + i
end
print(sum)
print(os.clock# # # ()) to perform the result set ➜ ~ lua hello.html lua * * * * * * * * * * * * * * * * * * * * * global variable and a local variable access speed * * * * * * * * * * * * * * * * * * * * * * * * * *5000050000
0.006693

5000050000
0.00768
Copy the code

“Take a close look at the above two execution time, what is the difference? “

“Wow! The difference between the two results is so large that global variable access is much slower than local access! Got it, got it.”

“Let me highlight the downside of using global variables in Lua! “

  • Because it is global, the declaration variable name is prone to conflict, the project has many people to maintain, can not be avoided every time
  • Global variables are slower to access than local variables

Usually write code to be careful, not careless! Think about why it’s okay for other people to write this way, but you copy and paste it, right? Learn to think inertia, and more importantly, always consult me for code review before submitting the code and going through the test process.

“Yes, boss! I know, in the future study also need your more guidance! Buy you starbucks tomorrow afternoon.”

“Oh, can I knock off now? “

“Go on, go on, I’m not holding you back with your legs! Let’s go! It’s time to knock off! It’s already 8:30! “

Walk out of the park together, watching each other leave the back; Seems to see that I was a novice, but also can not withstand blows, can not withstand the pressure from superiors; Look up at the stars and tell yourself: “Just because we’ve been through the hardest part of the journey doesn’t mean we can relax. Behind will be beaten, immutable truth! “

When we are confused: “effort, may be your last straw! “.