News for hackers. Stuff that matters.
Re:Don't name your variables after types:you wouldn’t name y... by munchbunny on Friday February 08, 2019 @11:53AM (#19116111)
I highly disagree with this advice. I understand the intent of the advice, I just think the proposed rule (in the body of the post) is a bad implementation of the intent because it is overly general.

Name your variable in a way that makes as clear as possible what the variable is used for.

He uses the example of "userMap". I think its mistake is not saying what the mapping is for. Names to users? Phone numbers to users? Emails to users? Its mistake is not calling it a map, which tells the reader that a variable behaves as a mapping. (And if it's not actually a map, then don't call it a map.)

Re:Don't name your variables after types:you wouldn’t name y... by drdeadringer on Friday February 08, 2019 @12:18PM (#19116422)
> Name your variable in a way that makes as clear as possible what the variable is used for.

This is my consideration as well. It's like "documentation for free" or "commenting without comments".

When I get hit by a bus tomorrow, you will be able to look at my variables and have an easy, viable clue about what is going on. Reading my mind will be that much easier when figuring my code.

Re:Don't name your variables after types:you wouldn’t name y... by klyrs on Friday February 08, 2019 @11:24AM (#19115715)
My first girlfriend named our cat named Kitty; my first spouse named our truck Trucky and wouldn't let me name our snake Fluffy. Sometimes my variable names reference types, but typically in dynamically typed languages. Didn't read past the title because it's wrong about me and wrong about my code
Re:Don't name your variables after types:you wouldn’t name y... by tebruno99 on Friday February 08, 2019 @10:12AM (#19114878)
I disagree. I don’t want to have to hunt around for the definition of users to know how to use it. Yes the compiler will throw an error, but that doesn’t help me when I’m typing code.
Re:Don't name your variables after types:you wouldn’t name y... by janpot on Friday February 08, 2019 @11:45AM (#19116006)
"Users": sounds like an array to me

"UsersMap": doesn't add much value, what are you mapping?

"UsersById": now this is my style

Re:Don't name your variables after types:you wouldn’t name y... by nickm12 on Friday February 08, 2019 @11:54AM (#19116122)
Agreed. I think the article is wrong to imply that the type adds no value. Even if you have typechecking, the names of variables help you understand code. Also, why should we compare variables to pets versus the many other things in our life that we do refer to by their type? I don't have proper names for my filing cabinet or office chair.
Re:Don't name your variables after types:you wouldn’t name y... by Noumenon72 on Saturday February 09, 2019 @12:47PM (#19123558)
That's good style. The important thing you will get out of the map is at the front of the variable name, and there's a less important little qualifier "byId" to help you understand how the data is structured.
Re:Don't name your variables after types:you wouldn’t name y... by romwell on Friday February 08, 2019 @02:59PM (#19118071)
Eh, my friend's cat name is exactly that -- cat.

My cat has a name, but I just call him "cat" most of the time.

The point being - if the name is not ambiguous, it's fine.

Re:Don't name your variables after types:you wouldn’t name y... by Jyaif on Friday February 08, 2019 @10:17AM (#19114945)
"If there is more that one *Config in scope at any one time, calling them config1 and config2 is less descriptive than calling them original and updated ."

The correct naming is of course "originalConfig" and "updatedConfig".

Re:Don't name your variables after types:you wouldn’t name y... by eMSF on Friday February 08, 2019 @11:57AM (#19116166)
I think your proposals are still missing some sort of indication or description on what type of configurations those variables might hold. After all, we don't want to have any unnecessary confusion, do we?

Yeah, just 'original' and 'updated' are probably a better choice.

Re:Don't name your variables after types:you wouldn’t name y... by nonamenoslogan on Friday February 08, 2019 @03:02PM (#19118099)
I completely agree--having inherited a few applications worth of .NET code in which the last (younger, right out of college) developer named all the variables for things from Norse Mythologies.

I also inherited a bunch of circa-2005 ColdFusion with aptly named variables in CamelCase which are much easier to understand.

varUsersTableID > pointer*Freya any day of the week.

Re:Don't name your variables after types:you wouldn’t name y... by mnorton on Friday February 08, 2019 @03:42PM (#19118453)
> developer named all the variables for things from Norse Mythologies.

this is hilarious.

Re:Don't name your variables after types:you wouldn’t name y... by ken on Friday February 08, 2019 @11:07AM (#19115510)
I actually had a teacher whose cat was just “cat”.

“He’s the only one. He knows I’m talking to him.”

Re:Don't name your variables after types:you wouldn’t name y... by gamblor956 on Friday February 08, 2019 @10:13AM (#19114900)
Horrible advice unless you use an IDE which keeps track of variable types for you.
Re:Don't name your variables after types:you wouldn’t name y... by coldtea on Friday February 08, 2019 @02:29PM (#19117793)
>Don't name your variables after types: you wouldn’t name your pets “dog” or “cat”

I would if I could not tell just by looking at them what species they are (and thus what I can do with them, what to feed them, etc) among 200 other animals in my house.

Re:Don't name your variables after types:you wouldn’t name y... by defterGoose on Friday February 08, 2019 @11:59AM (#19116198)
I wouldn't name my variables "Fred" or "Noodles" either.
Re:Don't name your variables after types:you wouldn’t name y... by mikestew on Friday February 08, 2019 @11:12AM (#19115555)
Were my domestic pets actually amorphous blobs of text, damned straight I'd prefix or suffix their names with "dog" or "cat" just so I could keep them straight. "Honey, where's the header file for the blue one? Can't remember if it gets dog food or cat food."

To torture the metaphor a bit more, of course I'd eventually remember which one is which because they live with me. A better metaphor is the feral cat that shows up on my doorstep once a week looking for a handout. I've slept since then, what are you again?

Re:Don't name your variables after types:you wouldn’t name y... by Meph504 on Monday February 11, 2019 @01:01AM (#19132389)
Firstly, I have known several people to name their animals some variation of their type, but barring that.

I see it as a courtesy to myself than those that come after me to provide relevant context to a variable name. If the type in the scope of the variable is meaningful to its use then I will include it.

Good or bad code tells me what it does, rarely does it tell me what its supposed to do.

Re:Don't name your variables after types:you wouldn’t name y... by CM30 on Friday February 08, 2019 @12:54PM (#19116843)
Clearly the writer hasn't seen Tom and Jerry.
Re:Don't name your variables after types:you wouldn’t name y... by SomeHacker44 on Saturday February 09, 2019 @12:11AM (#19120882)
Odd. I could swear I just saw a movie with a boy and girl (human children) named, respectively, Boy and Girl.
Re:Don't name your variables after types:you wouldn’t name y... by htfy96 on Saturday February 09, 2019 @03:26PM (#19124384)
Hungarian notation exists/existed for a reason: m_nWheels is still quite useful in C++ program
Re:Don't name your variables after types:you wouldn’t name y... by maceurt on Friday February 08, 2019 @01:18PM (#19117085)
For statically typed languages yes, but for dynamically typed languages I would say not in all scenarios.
Re:Don't name your variables after types:you wouldn’t name y... by ntlk on Friday February 08, 2019 @12:45PM (#19116741)
I had two mice named Mouse, and several hamsters called Hamsted, plus a guinea pig called Guinea Pig.
Re:Don't name your variables after types:you wouldn’t name y... by rbanffy on Friday February 08, 2019 @03:43AM (#19112478)
I once had a cat named "dog".
Re:Don't name your variables after types:you wouldn’t name y... by Cheyana on Friday February 08, 2019 @08:56AM (#19114091)
Did it compile okay?
Re:Don't name your variables after types:you wouldn’t name y... by LorenPechtel on Friday February 08, 2019 @03:37PM (#19118411)
Were I to get a cat (can't, my wife is afraid of them) I very well might name it "dog" or "doggy".
Re:Don't name your variables after types:you wouldn’t name y... by codyb on Friday February 08, 2019 @06:28PM (#19119633)
"Reward for doggy" with a picture of your cat would be a great missing pet flyer
Re:Don't name your variables after types:you wouldn’t name y... by shaftway on Friday February 08, 2019 @01:59PM (#19117522)
My Shiba Inu would beg to differ.

犬 = Inu = Dog

Re:Don't name your variables after types:you wouldn’t name y... by eznoonze on Friday February 08, 2019 @03:05PM (#19118140)
myCat1 myDog2