Live data from Hacker News

A Guide to Naming Variables

a-nickels-worth.blogspot.com

41–50 of 175 posts

Re: A Guide to Naming Variables

#41

In dynamic languages, like Python, I think it's often good to call variables something like `fruit_list`, to quickly pick up on a bug where ie: `fruit_list == {'apple'}`.

That feels like an ad-hoc reinvention of half a type system. If you want to keep track of your values' types (and you should), why not use a typed language?

Re: A Guide to Naming Variables

#42
Good advice about variable names (though surely "dentist" isn't all that obscure), but the example function suffers from the boilerplate that the author rightly says is a problem. Looking at the Java Collection interface, the removeIf() description sounds like it actually modifies the Collection rather than creating a new one. Is there a way in Java to do the equivalent of the following Haskell (I'm being lazy and not writing a version for Traversable)

    take n (filter (> 0) xs)

Re: A Guide to Naming Variables

#43
post #2

Good advice. If this sort of thing is appealing to you, then read Code Complete by Steve McConnell. It's basically an entire book full of very solid advice of this sort.

Another book you'll enjoy if you liked this post: "The Art of Readable Code" (http://www.amazon.com/Art-Readable-Code-Theory-Practice/dp/0...)

Re: A Guide to Naming Variables

#44
post #22
post #8

Off-topic, but blogspot links are a constant frustration for me on the phone. It's super easy for my fingers to swipe slightly left/right when scrolling and triggering blogspot's next/previous post navigation. I can even accidentally end up slightly scrolled horizontally and have no way to see the left side again. As a result I mostly ignore blogspot links. But this seems up my alley ( http://akkartik.name/post/reada…

Reader view on iOS makes blogspot, and lots of other articles, quite bearable. (I assume it works?)

Ah, why didn't I think of that?! Thanks!

Edit: Firefox on Android seems to eat much of the article in readable mode, while Chrome or Opera on Android seem to have none. So I'm back at square one..

Re: A Guide to Naming Variables

#45

A good guide, but I didn't like this part: Avoid Over-used Cliches In addition to not being Teutonic, the following variable names have been so horribly abused over the years that they should never be used, ever. val, value result, res, retval tmp, temp count str Cliches? If you are trying to communicate, these names are well known ways to do that. If there is something more specific to put in there, by all means, bu…

Agreed. The distinction between cliche and idiom is arbitrary. We have an idiom of using 'result' for many methods that return a value (e.g. derived getters, builders) making it clear that this thing we just introduced is what will ultimately be returned. As with any set of guidelines, apply common sense to suit your needs.

Re: A Guide to Naming Variables

#46

"rather than the elliptical vagueness of Romance languages like English" - what?

I had the same reaction too. After googling it to make sure, english was a germanic language like I always knew. Then I'm like, English might as well be romance because it's riddled with french words.

Re: A Guide to Naming Variables

#47

Earlier quoted context omitted.

A convention I've always done for this is to just use a plural name. There are fewer errors based on mixing up the names than you'd think, and it's very readable. for fruit in fruits: assert fruit is not 'apple' print(fruit) assert 'banana' not in fruits etc. Especially in Python, it makes code flow very much like English. Edit: It also goes with the "don't put the type in the name" point, which I agree with in most…

Even where the convention isn't to use natural language names, something like this is fairly common (e.g., in lots of fairly terse functional language the convention is to use, e.g., "xs" for a list and "x" for a member -- in some with destructuring matching on lists, the convention is "xs" for the list, "x" for the head, and "xr" for the tail (rest) of the list.)

this one is nice because the difference is so staggering. you just cannot mix up `x` and `xs`.

`movie_collection` vs `movie_collections` is a bit trickier

Re: A Guide to Naming Variables

#48
post #41

In dynamic languages, like Python, I think it's often good to call variables something like `fruit_list`, to quickly pick up on a bug where ie: `fruit_list == {'apple'}`.

That feels like an ad-hoc reinvention of half a type system. If you want to keep track of your values' types (and you should), why not use a typed language?

the job is in Python?

I like type systems when I play around with personal projects in well-typed languages

Re: A Guide to Naming Variables

#49

when it's coding as fast as humanly possible, the last thing I want to do is slow down to think of good var names. I'll often just name something "foo" if I can't think of a good name and come back and rename it later. Sometimes it's a little better than foo but still not perfect but I'll leave that and move on to something else. I really don't like the idea that code must be perfect. Code is never perfect.

Oh gosh. I really hope I will never have to work with you or ever use a product you have worked on. This is just terrible software engineering practice. You are doubling the amount of work you have to do and are creating a breeding ground for bugs. Remember this age-old wisdom "measure twice, cut once"? You are doing the exact opposite.

Re: A Guide to Naming Variables

#50

A good guide, but I didn't like this part: Avoid Over-used Cliches In addition to not being Teutonic, the following variable names have been so horribly abused over the years that they should never be used, ever. val, value result, res, retval tmp, temp count str Cliches? If you are trying to communicate, these names are well known ways to do that. If there is something more specific to put in there, by all means, bu…

I think that those are valid names in a good context. e.g. if you have a function like "substring", then you can name "str" as the parameter and "result" the variable where you accumulate the result. Any other more semantic name for "result" would be "substring", i.e. the name of the function itself.
Post reply on HN