Live data from Hacker News

How to name things in programming

slideshare.net

131–140 of 177 posts

Re: How to name things in programming

#131
post #22

Nice write-up. But there's not a single slide about usability and how it affects naming. If your library exposes 'getCurrentRuntimeContext' which is going to be used in every other line of user's code, name it 'ctx'. It's ambiguous, but it saves the user 3 seconds per 2 lines of code. As for the readability, when the reader encounteres 'ctx' for the third time (at line 6 of the source file) they will already know wha…

I agree with your point that long names can be a problem by itself. Names should be kept as short as possible.

But not any shorter. "ctx" is just a bad name for a library to expose. Do rename it at those local contexts it'll be on every other line, but do not export it from a global library.

Re: How to name things in programming

#132
post #128

Earlier quoted context omitted.

You are assuming that every one can recall 25+ letter names - dyslectics have problems with this sort of short term memory.

That is a very weak argument. Is there anything that suggests letter count is a barrier to dyslexics, and not, say, word count or number of obscure abbreviations?

Yes short term memory tests are one of the primary diagnostic tests for dyslexia. Ie how many consecutive numbers/letters you can recall.

Long Names like SomeRealyLongNameFunctionThing and SomeRealySimialrLongFunctionNameThing are both hard to work with and confusing to me.

Re: How to name things in programming

#133

Earlier quoted context omitted.

And how would an ide know that n0cars should be nocars

An editor probably not know this but an IDE would. The IDE actually partially understands your code. If your function definition was (in python): def nocars but you called the function with: n0cars() The ide would highlight n0cars because it could not find the definition. The IDE literally destroys the possibility of making trivial mistakes like this in a non-compiled language. Of course this depends on the ide. I us…

I was referring to a variable not a function and as you say IDE's can have heavy over head not every one can afford a full copy of visual studio or a system powerful enough to run eclipse

Re: How to name things in programming

#134

Earlier quoted context omitted.

I didn't understand the comment on Ruby. Aren't those examples equivalent to?: pairs.sort_by(&:last) No need for defining a block and naming its parameter either.

Yes, the lambda was equivalent. However, the second example offers that it might be better to be more verbose and avoid the use of anonymous functions. In general, I prefer to define a block and name parameters. As the slideshow illustrated, it's all about having good taste as an author. Knowing your audience and all that. In fact, that might have been a good point to add to his slides--that different coding styles a…

I'm not sure if I am misunderstanding your argument, but it seems like you're complaining about the fact you can't pass "functions" to methods that accept a block? You can! :D

    times_two = -> (x) {x*2}       # Proc (lambda)
    times_two = Proc.new {|x| x*2} # Proc
    [7, 13, 19, 23, 31].map(&times_two)
Also, if you want to call a method on the objects, you could do:

    [7, 13, 19, 23, 31].map(&:to_s)
Which is a bizarre syntax, if you ask me (actually, not really if you know how I works in the background, but it still does look bizarre IHMO).

Re: How to name things in programming

#135

Earlier quoted context omitted.

why not?

Convention. FYI for non-Pythonistas: the prevailing convention is lowercase_with_underscores for everything, except CamelCase for class names. Of course, the core types are mostly lowercase. I think it's somewhat like being a famous scientist; you know you're really important if your name is lowercase.

This is similar to most dynamic/scripting languages of that era, for that matter; both Perl and Ruby (and, IIRC, PHP) have similar conventions. Things got bungled when Java programmers started insisting on camelCased function names even outside of Java.

Re: How to name things in programming

#136
post #98
post #14

Earlier quoted context omitted.

int n = 42; int acc = 0; Fixed Edit: Install http://wbond.net/sublime_packages/alignment and add this key binding `{ "keys": ["ctrl+shift+a"], "command": "alignment" }`.

I think both practices are pathological. The GP is restricting all variable names to an arbitrary length that will likely obscure meaning. The parent is arbitrarily determining line length based on whichever variable is longer; he may also be mixing in the GP's arbitrary var length rule in order to get a visually pleasing line in the local context. Prose is obviously different, but it's probably worth considering tha…

The point is less about aesthetics for their own sake and more about readability; the LHS and RHS end up being readable as if they were columns, which leaves less work for the brains of coworkers or future selves to perform when trying to mentally parse it.

Re: How to name things in programming

#137
This is a timely article because I was just talking to a friend of mine that works at Google and is about to start an internal project that will be written in Go.

I told him I'm not sure I'd ever fit into the Go community because the Go community seems to like variable names that are extremely short. For example:

  func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
I've even been "gently corrected" in #go-nuts on Freenode when I've shared code and I used "res" instead of "w" and "req" instead of "r" -- they told me my longer variable names wasted space and made it harder to read.

Perhaps my brain works different, but I find variable names that are actual words much easier to read.

Re: How to name things in programming

#138
post #137

This is a timely article because I was just talking to a friend of mine that works at Google and is about to start an internal project that will be written in Go. I told him I'm not sure I'd ever fit into the Go community because the Go community seems to like variable names that are extremely short. For example: func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) I've even been "gently corrected" in #…

These are the same individuals that will argue with you until the end of time about how generics are never necessary and carry too much cognitive burden.

Re: How to name things in programming

#139
post #106

Earlier quoted context omitted.

> I once worked for a game programming company. One of the programmers there would name all his functions and variables for fruit and vegetables. It was his way of ensuring job security. Guess who got saddled with untangling the salad when he left the company. Looks like that guy followed this guide http://mindprod.com/jgloss/unmainnaming.html

From your link: "If you call your variables a, b, c, then it will be impossible to search for instances of them using a simple text editor." If you can't search for single-letter words in your "simple text editor", why are you using that simple text editor to code? In vi, at least, it's trivial: /\

I think the thing is in addition to all instances of the variable called a your search will also stop on any word that contains a. Now of course you can search for " a " in whatever way your editor prefer but programmers who write such variable names might easily write a=10 one time and a = 10 the next and before you know it you have an interesting regex there in your search.

I think he was contrasting it to an ide that will automatically find out which as "a"s are references to the same. ("Find usages" in most Java ides I think."

Re: How to name things in programming

#140

Earlier quoted context omitted.

From your link: "If you call your variables a, b, c, then it will be impossible to search for instances of them using a simple text editor." If you can't search for single-letter words in your "simple text editor", why are you using that simple text editor to code? In vi, at least, it's trivial: /\

I think the thing is in addition to all instances of the variable called a your search will also stop on any word that contains a. Now of course you can search for " a " in whatever way your editor prefer but programmers who write such variable names might easily write a=10 one time and a = 10 the next and before you know it you have an interesting regex there in your search. I think he was contrasting it to an ide t…

"I think the thing is in addition to all instances of the variable called a your search will also stop on any word that contains a."

Yes, that's what was being said.

But it won't. In vi (and friends), \ is "end of word", so \ will not match the a in "absolute" or "tundra" or "fabulous", but will match the a in "(3+a)" and "a=7". Certainly there are text editors that don't let you express this, but they're so broken as to be basically unusable.

An IDE that is able to keep the various references separate is still more powerful, but the difference is tremendously smaller - particularly if you follow the scoping guidance at the root of this comment thread.

Edited to add: Additionally, if your cursor is already on the word in question, that search is one keystroke. * says "search for the word under the cursor" and incorporates the word boundary markers.

Post reply on HN