DEV Community

Cover image for OverSimplified: Variables
Sina Nikmaram
Sina Nikmaram

Posted on • Updated on

OverSimplified: Variables

Welcome! In this series, I try to oversimplify CS concepts in an attempt to help us all better understand them. Thanks for your time, enjoy!


What are variables?

  • Variables are names we assign to storage slots in our computers memory. Alt Text
  • In the picture above, imagine each container is a slot of memory in our computer.
    • The food itself is data sitting in a slot of memory.
    • The labels are variable names we give to that slot of memory.

How do we use variables?

  • We use variables to retrieve data sitting in named storage slots
num1 = 1900   // Save 1900 in storage and name the slot num1
num2 = 121    // Save 121 in storage and name the slot num2
sum(num1 + num2)   // Add data in storage slots num1 & num2
Enter fullscreen mode Exit fullscreen mode

What are the benefits of using variables?

  • Use simple names to evaluate complex expressions
  • Rename any storage slot that's holding data
  • Replace the data in any storage slot
  • Use a single name for similar values in a list of data

Descriptive Variable Naming (... and why it's important!)

Alt Text

  • In my second attempt to oversimplify using a food analogy:
    • Sugar and Salt look the same but they taste completely different
    • It is important that we put them in properly labeled containers or we might get weird tasting food.
    • It is also important that we put data in properly named storage slots or we will get weird expression results / errors.
  • Inaccurate variable names will confuse other developers and your future self, which could lead to unexpected results & bugs

As mundane as variables seem, using them properly is crucial to being a good developer.


BONUS: Variable Typing

  • Some programming languages require us to explicitly define what data type the variables storage slot will hold.
int num1 = 1900   // Variable that only stores integers
str str1 = "A string"   // Variable that only stores strings
Enter fullscreen mode Exit fullscreen mode
  • Typing helps productivity by showing us errors when we use a variable incorrectly (Ex: Adding a boolean variable and an integer variable).

Top comments (0)