DEV Community

Discussion on: Variables names don't need their type

Collapse
 
ashuk profile image
Ash

"Encoding the type of a function into the name (so-called Hungarian notation) is brain damaged—the compiler knows the types anyway and can check those, and it only confuses the programmer" - Linus Torvalds

With modern tools and IDE's I really don't see this having to be a thing anymore. If you can just hover over or cursor to a variable and know its type. Then there is no need to add a type affix. Sadly I still do see this in the wild. Either due to an old code base or an old mindset and almost always when I do come across it there are at least a few dozen variable names that have since changed and have not been updated.

Collapse
 
codemouse92 profile image
Jason C. McDonald • Edited

That beside, if you can't tell the semantic meaning of a variable by its name, and thereby deduce the like type(s) in use, you need better names anyway.

For example, what is the type of is_ready? How about like_count? average_temp? pending_requests? (If you guessed "boolean", "integer", "float", and "something akin to a queue", you're right!)

Collapse
 
waylonwalker profile image
Waylon Walker • Edited

Duck typing 🦆

Thread Thread
 
codemouse92 profile image
Jason C. McDonald

Which reminds me of something I put in my upcoming Python book...

In other words, Python doesn’t care if it’s actually a robotic duck, or a moose in a duck costume; if it has the traits needed, the rest of the details are usually a moot point.

Collapse
 
waylonwalker profile image
Waylon Walker

I had no idea what Hungarian notation was. That is an amazing quote.

Just today I had vscode (pyright in particular) catch me with an edge case. I had a function that would return an iterable if there were values, but False if there were none. It caught me that I wasn't catching the False case.