classMyClass:
def__init__(self, value):
self._whatever = value
@propertydefwhatever(self):
returnself._whatever
Personally, I would type hint all of that but I’m just showing how you can do it without types. Your linter should be smart enough to say “hey dumbass did you mean this other thing”? Also since we didn’t create a setter you can’t arbitrarily overwrite the value of whatever so thats neat.
And I’ll just say before I post that I’m on mobile and I’m sorry if the formatting is fucked. I’m not going to fix it.
This is literally a getter function. How is a getter awful code? It’s the simplest function there is. The only function simpler than that is returning the input itself.
How does “foo” mean “get”? Half the battle of writing correct code is writing code that’s easy to interpret. Do you always look at the guts of every function you’re about to use?
What’s the purpose of foo? Why an ambiguous single character variable? What if the property was there but the value was null? Why not use (assuming JS) optional chaining?
It’s an example to demonstrate that linters cannot reliably detect variable name typos - you need static types. None of the stuff you mentioned is relevant.
The typo in your example is also undetectable by linters. I think you’re missing the point.
Always love seeing the trope:
How would you make it non-awful, without specifying static types?
I guess, a unit test would catch it, but needing 100% test coverage to catch typos isn’t exactly great…
I would do
class MyClass: def __init__(self, value): self._whatever = value @property def whatever(self): return self._whatever
Personally, I would type hint all of that but I’m just showing how you can do it without types. Your linter should be smart enough to say “hey dumbass did you mean this other thing”? Also since we didn’t create a setter you can’t arbitrarily overwrite the value of whatever so thats neat.
And I’ll just say before I post that I’m on mobile and I’m sorry if the formatting is fucked. I’m not going to fix it.
I use a spell checker in my IDE. It would catch this.
This is literally a getter function. How is a getter awful code? It’s the simplest function there is. The only function simpler than that is returning the input itself.
How does “foo” mean “get”? Half the battle of writing correct code is writing code that’s easy to interpret. Do you always look at the guts of every function you’re about to use?
It’s a one line function in an example. It’s a getter.
What’s awful about this example? The only thing I do is access an object member. Does your code not do that??
What’s the purpose of foo? Why an ambiguous single character variable? What if the property was there but the value was null? Why not use (assuming JS) optional chaining?
I’d approach it more like this:
function getWhatevrProp(userData) ( const default = { whatevr: "n/a" }; return { ...default, ...userData }.whatevr; }
Sorry, read too fast the first time. It’s more likely Python. I also don’t know Python well enough to give recommendations on that.
Lmao, and they say dynamic typing is supposed to speed up the developer.
It’s an example to demonstrate that linters cannot reliably detect variable name typos - you need static types. None of the stuff you mentioned is relevant.
The typo in your example is also undetectable by linters. I think you’re missing the point.