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
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.
Top comments (0)