DEV Community

Pontakorn Paesaeng
Pontakorn Paesaeng

Posted on

2 1

Variable are named after its purpose, not the other way around

Original Thai Version here: https://pontakornth.vercel.app/blog/we-defined-variable-name-not-program

It's common problems I found after teaching my friends and girlfriend basic programming. They tend to associate variable name with what is named. For example, i is used as index althought i is the value in for i in a_list.

So I will show the snippet of running code but variables and functions name are not related to its purpose.

import math
def slow_sort(a_list: list, query: int):
    star_platinum = len(a_list) - 1
    the_world = 0
    while the_world <= star_platinum:
        hermit_purple = math.floor((the_world + star_platinum) / 2)
        jonathan_joestar = a_list[hermit_purple]
        if jonathan_joestar == query:
            return hermit_purple
        elif jonathan_joestar < query:
            the_world = hermit_purple + 1
        else:
            star_platinum = hermit_purple - 1
    return -1
Enter fullscreen mode Exit fullscreen mode

If it's not familiar to you, it's fine. This is a snippet of binary search. As you see the variable is not related, to its purpose at all. However, it runs perfectly fine.

To conclude, the variable name is not necessary related to its purpose but it should. When you are programming, you need to remember the purpose of the variable. Happy coding.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more