News for hackers. Stuff that matters.
Re:How to name things in programming by jacquesm on Monday May 25, 2015 @05:24AM (#9599255)
An important rule is missing here: variables should be named with their scope in mind. So if a variable is longer lived and has larger scope its name should be that much more descriptive because when you're looking at it the only thing that will tie the value of the variable to the context within which it can be used is its name.

So 'i' is fine for a loop control variable with a scope of five lines but totally inadequate for something expressing a larger and longer lived concept.

Ditto for function names and parameters to functions, if the function and the parameters are named properly understanding the function is trivial.

So if you write a chunk of code that exports one or more functions that is where your effort should go, that's the public interface. The reduced scope of the rest of the code should make any naming issues much more limited.

This is also why it is good to assign one person on a team to defining the interfaces between the code. That way you get consistency in naming which is a great thing to have in a codebase.

Anecdote time:

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.

cucumber(cherry, strawberry, orange);

Good luck with that...

Re:How to name things in programming by itsybitsycoder on Monday May 25, 2015 @12:20PM (#9600572)
I didn't think it could get much worse than my former classmate, who would name all his pointer variables after famous ghosts (casper, bloody_mary, etc) because they "weren't real", but I think your greengrocer friend still wins...
Re:How to name things in programming by msellout on Monday May 25, 2015 @09:49AM (#9600013)
Correspondingly, some objects don't need names at all.

    sorted(pairs, key=lambda p: p[1])
Though in that case you might prefer using the operator module.

    sorted(pairs, key=operator.itemgetter(1))
The aspect of Ruby that frustrates me the most is over-use of anonymous blocks when a good name would help me enormously.
Re:How to name things in programming by techiferous on Monday May 25, 2015 @10:48AM (#9600223)
> The aspect of Ruby that frustrates me the most is over-use of anonymous blocks when a good name would help me enormously.

Ruby gives you enough tools to write code clearly with good names if you want to. I think your complaint might be more about the particular style of Ruby a certain programmer wrote than the language itself.

Re:How to name things in programming by msellout on Monday May 25, 2015 @11:34AM (#9600416)
Correct, though I find that a language is more about the community and culture than the syntax itself.
Re:How to name things in programming by epidemian on Monday May 25, 2015 @10:07AM (#9600062)
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.
Re:How to name things in programming by msellout on Monday May 25, 2015 @11:42AM (#9600439)
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 are appropriate for different teams and projects.

Re:How to name things in programming by spoiler on Monday May 25, 2015 @01:20PM (#9600860)
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 by agumonkey on Monday May 25, 2015 @12:21PM (#9600579)
With the functional composition and currification, you can get point free programming (variable-less definitions). Some people don't like it, I personally find it appealing (I like APL and such). To quote prof. Dan Grossman, lambda p: p[1] === lambda p: p.__getitem__(1) which he calls an `unnecessary wrapping` of itemgetter(1). Similar to `return true if <bool> else false` which most people would find redundant.
Re:How to name things in programming by peter_hilton on Thursday May 28, 2015 @04:48AM (#9616821)
> An important rule is missing here: variables should be named with their scope in mind

True, but I have plenty of slides in my presentation without adding more :)

The 'small scope rule' is more of a situation when you can get away with names that are less good, because of the small scope. When I get into discussions about that, everyone agrees with each other, and it distracts from the issue of coming up with good names when the scope isn't tiny.

Also, 'i' for a loop control variable is a trick example: why aren't you using a language that loops over collections without using a loop index?

Re:How to name things in programming by ezy on Saturday May 30, 2015 @02:34PM (#9631223)
If you buy into the prose/code relationship, this is silly. It is like complaining about an author using prepositions.
Re:How to name things in programming by slig on Monday May 25, 2015 @11:50AM (#9600471)
> 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

Re:How to name things in programming by dllthomas on Monday May 25, 2015 @12:00PM (#9600507)
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: /\<a\>

Re:How to name things in programming by reitanqild on Monday May 25, 2015 @02:11PM (#9601027)
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 by dllthomas on Monday May 25, 2015 @02:25PM (#9601081)
"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 "beginning of word", and \> is "end of word", so \<a\> 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.

Re:How to name things in programming by dalore on Tuesday May 26, 2015 @10:50AM (#9605213)
If all the variables are 'a' then how do you find the one that's yours?
Re:How to name things in programming by dllthomas on Tuesday May 26, 2015 @12:48PM (#9605946)
That's why an editor that actually understands the language can do better.

That said, this is also where the scope consideration comes into play. If something is used in 100 different lines, 300 lines apart across 3 files, then of course naming it 'a' is crazy. If something is only in scope over 5 or 10 lines, then you simply restrict your search to those lines (either explicitly with marks, line numbers, or pattern bounds; or implicitly by exiting the search when it gets to a region you don't care about - find-and-replace in vim has a "confirm" option).

Re:How to name things in programming by shoo on Monday May 25, 2015 @08:45AM (#9599820)
continuing with the anecdotes:

i heard a tale of some graph layout code that ended up as part of a Microsoft product - all of the things were named after birds.