The different Variable Scopes [Math Mondays]
One of the most overlooked innovations in programming. Learn to utilize them for best results
Hey, it’s your favorite cult leader here 🐱👤
Mondays are dedicated to theoretical concepts. We’ll cover ideas in Computer Science💻💻, math, software engineering, and much more. Use these days to strengthen your grip on the fundamentals and 10x your skills 🚀🚀.
To get access to all the articles and support my crippling chocolate milk addiction, consider subscribing if you haven’t already!
p.s. you can learn more about the paid plan here.
I’m about to cover one of the most overlooked ideas in software engineering,
As a programmer, you are probably familiar with the concept of variables - a value or a reference to a value that can be changed during the execution of a program. However, not all variables are created equal, and the scope of a variable determines where and how it can be accessed within a program.
Most of us work with high-level languages, and scopes are taken for granted. But this is a giant mistake. Understanding variable scope is important for writing efficient and secure code. By setting the scopes in the appropriate way, you can avoid potential conflicts between different parts of the program. You should learn to set the code of variables to only where they are needed because messed-up scopes can make it a nightmare to track down errors and bugs.
By now, you should understand why scopes are important. So let’s get into the different kinds of scopes you will be working with.
By using the appropriate scope for your variables, you can ensure that your code is efficient, secure, and easy to manage.
-The importance of scopes
The different kinds of variable scopes
Local/block scope: Variables that are declared inside a block have a local/block scope. These variables can only be accessed within the specific block in which they are declared. Once the block has been completed, the variables are destroyed and their values are lost. This makes local variables useful for temporary storage or for passing values between functions.
def Function(): if(condition): localVar=1 print(localVar) #will work print(localVar) #breaks since we are outside the if block.
Function Scope: In some programming languages, variables can also have function scope. This means that the variables are accessible only within the specific function in which they are declared, and are destroyed when the function has been completed. Function scope can be useful for creating private variables that are not accessible outside of a particular function.
def funkyFunc(): var funk=1 print(funk)# error funk can't be accessed outside
Global scope: Variables that are declared outside of any function or block of code have a global scope. These variables can be accessed from anywhere within the program, including inside functions and other blocks of code. However, global variables can also be modified from anywhere in the program, which can make them difficult to manage and can lead to unintended consequences. You want to be very careful while using these
Static variables: If you do a lot of Object Oriented Programming, you will also deal with these. A static variable is a variable that has been allocated "statically", meaning that its lifetime (or "extent") is the entire run of the program. These are typically allocated at compile time and not run time (like the other types of vars). Thus, they are not present on the call stack or the heap memory. You will see them in OOP-
In object-oriented programming, there is also the concept of a static member variable, which is a "class variable" of a statically defined class, i.e., a member variable of a given class which is shared across all instances (objects), and is accessible as a member variable of these objects.
You should now have a solid understanding of the different scopes and how to use them. For the more advanced readers amongst you, this might seem trivial. And in a way, you’re right. Scopes by themselves are not a hard concept to understand. However, because the concept is so easy, they can often be overlooked. Look through your systems and be on the lookout for the scopes of different variables and how they affect your code. You’ll be surprised to see how many scopes are not defined properly.
That is it for this piece. I appreciate your time. As always, if you’re interested in reaching out to me or checking out my other work, links will be at the end of this email/post. If you like my writing, I would really appreciate an anonymous testimonial. You can drop it here. And if you found value in this write-up, I would appreciate you sharing it with more people. If you’re looking to read more, but have been super busy, check out the sponsor of today’s newsletter
Sponsored Segment
Key ideas from bestselling non-fiction books, distilled by experts into bitesize text and audio. Explore our vast library of over 5,500 titles and stay up-to-date with 40 new titles that are added each month. Join the reading revolution! You can start your free trial here.
For those of you interested in taking your skills to the next level, keep reading. I have something that you will love.
Upgrade your tech career with a premium subscription ‘Tech Made Simple’! Stay ahead of the curve in AI, software engineering, and tech industry with expert insights, tips, and resources. 20% off for new subscribers by clicking this link. Subscribe now and simplify your tech journey!
Using this discount will drop the prices-
800 INR (10 USD) → 533 INR (8 USD) per Month
8000 INR (100 USD) → 6400INR (80 USD) per year
Reach out to me
Use the links below to check out my other content, learn more about tutoring, reach out to me about projects, or just to say hi.
If you like my writing, I would really appreciate an anonymous testimonial. You can drop it here.
To help me understand you fill out this survey (anonymous)
Check out my other articles on Medium. : https://rb.gy/zn1aiu
My YouTube: https://rb.gy/88iwdd
Reach out to me on LinkedIn. Let’s connect: https://rb.gy/m5ok2y
My Instagram: https://rb.gy/gmvuy9
My Twitter: https://twitter.com/Machine01776819