A Look into Variable Scoping in Programming.

A Look into Variable Scoping in Programming.

Hello there, welcome to my blog. Building enterprise applications is no small feat for any software engineer, the code can span up to millions of lines and can't be written by a single engineer, many software engineers come together to write this application. While at this, it is important that each engineer on the team should have the ability not just to write code but to write clean, readable, and reusable code. But to be able to write and read clean code, a software engineer should have a strong foundation of programming concepts. One of such concepts are what I am going to be teaching you today called "Scoping". If you are a software engineer willing to grow your coding skills to another level by following the best practices, writing cleaner and reusable code, then this article is for you. I have been following best practices since I became a developer, and I thought I share some of what I have learned so far. Relax and enjoy the read, and do grab a pop-corn

What you learn from this article

  • What is Scoping?
  • Scoping in JavaScript
  • Scoping in Golang

What is Scoping?

A lot of newbie developers and even junior developers still find this concept hard to grasp and they, therefore, skip it and move to the next phase, in turn making them write bad code.

Scoping in programming refers to the position of a variable, which determines the accessibility of variables in the codebase. That is how simple it is. Where you declare your variables and how you access them plays an important role in how you write good code that everyone can read.

There are 2 types of scoping: Global scoping and Block scoping. In this section, I would be explaining scoping in 2 programming languages, Golang and JavaScript.

Global Scoping vs Block Scoping

Global scoping are those variables declared outside a block which can be a Function, If statement, Object methods, Loops block e.t.c and to this effect can be accessed anywhere within the codebase.

Block scoping are those variables declared inside a block, and such blocks include; Function, If statement, Object methods, Loops blocks e.t.c and therefore can only be accessed within that block which it was declared. Do you get it? Hang on, I have some real-life examples to show you.

Scoping in JavaScript

Let use the dog-pet example, while some people buy a pet dog to keep in their home, others choose to buy the pet and let it wander around the neighborhood and comes back when it feels like. Mr. James buys his dog and lets it wander while Mr. Franklin buys his dog and keep it in his house only. From the illustration above, we can bring it into programming and say Mr. James has declared a global variable and Mr. Franklin has declared a local variable. Mr. James's dog can be seen and touched by everyone in the hood while Mr. Franklin's dog is meant to be touched and seen by only his family. Now let see that in code.

let dog1 = "flames";

function dogName(){
     let dog2 = "fire";
      console.log(dog1)
     console.log(dog2)
}
console.log(dog1)
     console.log(dog2)

Output:

// Code in the function
"flames"
"fire"

// Code outside function
"flames"
Reference error: dog2 is not defined

From the above code, you can see dog1 was accessible everywhere in the code including inside the function but dog2 which was declared in a function block was not accessible outside the function block which is its birthplace and death place too. When I tried to log out dog2, JavaScript simply told me "Hello, There is nothing like dog2 within my view". Does this mean JavaScript is blind or that dog2 doesn't exist? Nope. It simply means we set the rules that dog2 should only be available in the dogName function.

Nevertheless, we can get dog2 to log by calling the dogName function since it was declared globally. See the example below:

let dog1 = "flames";

function dogName(){
     let dog2 = "fire";
      console.log(dog1)
     console.log(dog2)
}
console.log(dog1)
  dogName()

Output:

// Code in the function
"flames"
"fire"

// Code outside function
"flames"
"flames"
"fire"

Scoping in Golang

One of the most elite languages developed by Google and a very useful tool. I picked it up sometime last year and it has been great learning it. Lol...

Just like every other programming language has the same concept just a change of name, Golang has the package level scoping(Global scoping) and Block scoping. The very same way we did it in Javascript, just a change of name and syntax. Still using Mr. James and Mr. Franklin's illustration, let write some code.

package main

import (
    "fmt"
)

var dog1 string = "mike"

func main() {
var dog2 string = "miko"
    fmt.Println(dog1)
   fmt.Println(dog2)
}

func printDog(){
 fmt.Println(dog1)
   fmt.Println(dog2)

}

Output : (spoiler alert: The code won't run)

undefined: dog2

Go is a statically typed language, unlike JavaScript and other loosely typed languages, Go would never run a code if one line is wrong, but JavaScript let us run it and tell us the error when the job is done, not so with Go, you can try this example on Go playground. But away from that, we know that dog2 was declared in the main function, therefore not accessible outside it, and that line broke the code.

If you are really interested in Go, or the spirit of Go has whispered to you "Learn me", you should check Introduction to Go and Variables and Data types in Go.

With this piece of knowledge, you can now know how best to create variables and use them properly giving your codebase readability and a clean look, and also avoiding you pulling off your hair when JavaScript tells you "undefined". All you just have to do is check the scope of the variable you are using at the moment, you just solved a bug!!.

In the end...

So we have come to the end of this amazing article. Hope you learned something useful. If you did, like, share, comment, just do anything to make it go viral.

I would be posting more articles, so be sure to follow me to get notified when I post them.

If you have any questions concerning Go, Javascript, TailwindCSS, Open-source, or this article? You can reach me on Twitter. Till next time, see ya. Thank you.