This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Another formatting question: Short variable names

9 replies
Bill Venners
Joined: 2008-12-18,
User offline. Last seen 31 weeks 5 days ago.

Hi All,

How do people feel about hd for variables that will hold the head of a
list, ls for list, etc. What about pred for predicate? Seems if there
are some very commonly used variable names like that it could make
things more readable, but head and list are just two characters longer
each, so I question whether that wouldn't make code more readable in
general.

Dave Astels once said, whatever you would answer when asked what a
particular variable is, that is what you should name that variable.
Sometimes that approach can result in long variables that can obscure
the logic, and I think sometimes an abbreviation makes things more
readable. But I seem to lean more towards longer more descriptive
variable names than others in my sphere of peers. When would you
shorten or abbreviate a variable name, and how short would you go?

Bill
----
Bill Venners
Artima, Inc.
http://www.artima.com

Kris Nuttycombe
Joined: 2009-01-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Another formatting question: Short variable names

head/list

Any time variable names are abbreviated I have to mentally do the
expansion anyway. Why force the extra effort when you're talking about
two characters?

On Wed, Jun 10, 2009 at 4:46 PM, Bill Venners wrote:
> Hi All,
>
> How do people feel about hd for variables that will hold the head of a
> list, ls for list, etc. What about pred for predicate? Seems if there
> are some very commonly used variable names like that it could make
> things more readable, but head and list are just two characters longer
> each, so I question whether that wouldn't make code more readable in
> general.
>
> Dave Astels once said, whatever you would answer when asked what a
> particular variable is, that is what you should name that variable.
> Sometimes that approach can result in long variables that can obscure
> the logic, and I think sometimes an abbreviation makes things more
> readable. But I seem to lean more towards longer more descriptive
> variable names than others in my sphere of peers. When would you
> shorten or abbreviate a variable name, and how short would you go?
>
> Bill
> ----
> Bill Venners
> Artima, Inc.
> http://www.artima.com
>

Kris Nuttycombe
Joined: 2009-01-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Another formatting question: Short variable names

I'll amend that - I could see abbreviating "list" to "l" - just not to
"ls" because of the collision with ls.

On Wed, Jun 10, 2009 at 6:47 PM, Kris
Nuttycombe wrote:
> head/list
>
> Any time variable names are abbreviated I have to mentally do the
> expansion anyway. Why force the extra effort when you're talking about
> two characters?
>
> On Wed, Jun 10, 2009 at 4:46 PM, Bill Venners wrote:
>> Hi All,
>>
>> How do people feel about hd for variables that will hold the head of a
>> list, ls for list, etc. What about pred for predicate? Seems if there
>> are some very commonly used variable names like that it could make
>> things more readable, but head and list are just two characters longer
>> each, so I question whether that wouldn't make code more readable in
>> general.
>>
>> Dave Astels once said, whatever you would answer when asked what a
>> particular variable is, that is what you should name that variable.
>> Sometimes that approach can result in long variables that can obscure
>> the logic, and I think sometimes an abbreviation makes things more
>> readable. But I seem to lean more towards longer more descriptive
>> variable names than others in my sphere of peers. When would you
>> shorten or abbreviate a variable name, and how short would you go?
>>
>> Bill
>> ----
>> Bill Venners
>> Artima, Inc.
>> http://www.artima.com
>>
>

Blair Zajac
Joined: 2009-01-12,
User offline. Last seen 42 years 45 weeks ago.
Re: Another formatting question: Short variable names

Bill Venners wrote:
> Hi All,
>
> How do people feel about hd for variables that will hold the head of a
> list, ls for list, etc. What about pred for predicate? Seems if there
> are some very commonly used variable names like that it could make
> things more readable, but head and list are just two characters longer
> each, so I question whether that wouldn't make code more readable in
> general.

If you're going to go short and this is for a since method, then h = head, t =
tail and p = predicate.

Blair

Erik Engbrecht
Joined: 2008-12-19,
User offline. Last seen 3 years 18 weeks ago.
Re: Another formatting question: Short variable names
Real programmers only need x, y, z, i, j, k, and temp.

On Wed, Jun 10, 2009 at 6:46 PM, Bill Venners <bill [at] artima [dot] com> wrote:
Hi All,

How do people feel about hd for variables that will hold the head of a
list, ls for list, etc. What about pred for predicate? Seems if there
are some very commonly used variable names like that it could make
things more readable, but head and list are just two characters longer
each, so I question whether that wouldn't make code more readable in
general.

Dave Astels once said, whatever you would answer when asked what a
particular variable is, that is what you should name that variable.
Sometimes that approach can result in long variables that can obscure
the logic, and I think sometimes an abbreviation makes things more
readable. But I seem to lean more towards longer more descriptive
variable names than others in my sphere of peers. When would you
shorten or abbreviate a variable name, and how short would you go?

Bill
----
Bill Venners
Artima, Inc.
http://www.artima.com



--
http://erikengbrecht.blogspot.com/
Jorge Ortiz
Joined: 2008-12-16,
User offline. Last seen 29 weeks 3 days ago.
Re: Another formatting question: Short variable names
Hogwash!

Real programmers use tmp

--j

On Wed, Jun 10, 2009 at 8:37 PM, Erik Engbrecht <erik [dot] engbrecht [at] gmail [dot] com> wrote:
Real programmers only need x, y, z, i, j, k, and temp.

On Wed, Jun 10, 2009 at 6:46 PM, Bill Venners <bill [at] artima [dot] com> wrote:
Hi All,

How do people feel about hd for variables that will hold the head of a
list, ls for list, etc. What about pred for predicate? Seems if there
are some very commonly used variable names like that it could make
things more readable, but head and list are just two characters longer
each, so I question whether that wouldn't make code more readable in
general.

Dave Astels once said, whatever you would answer when asked what a
particular variable is, that is what you should name that variable.
Sometimes that approach can result in long variables that can obscure
the logic, and I think sometimes an abbreviation makes things more
readable. But I seem to lean more towards longer more descriptive
variable names than others in my sphere of peers. When would you
shorten or abbreviate a variable name, and how short would you go?

Bill
----
Bill Venners
Artima, Inc.
http://www.artima.com



--
http://erikengbrecht.blogspot.com/

Ben Hutchison 2
Joined: 2009-02-14,
User offline. Last seen 42 years 45 weeks ago.
Re: Another formatting question: Short variable names

Bill Venners wrote:
> Hi All,
>
> How do people feel about hd for variables that will hold the head of a
> list, ls for list, etc. What about pred for predicate? Seems if there
>
I prefer head, list and predicate, but 'pred' is more acceptable than
'hd' or 'ls'.
> Dave Astels once said, whatever you would answer when asked what a
> particular variable is, that is what you should name that variable.
>
That's a nice heuristic. I prefer longer names and feel anything up to
about 7-10 chars in length should be written out in full. As the length
increases, abbreviation becomes more appropriate and almost mandatory
beyond 20 chars .

However convention establishes some shorter forms, eg x and xs for
pieces of lists, f and g for opaque function values.

-Ben

> Sometimes that approach can result in long variables that can obscure
> the logic, and I think sometimes an abbreviation makes things more
> readable. But I seem to lean more towards longer more descriptive
> variable names than others in my sphere of peers. When would you
> shorten or abbreviate a variable name, and how short would you go?
>
> Bill
> ----
> Bill Venners
> Artima, Inc.
> http://www.artima.com
>

tolsen77
Joined: 2008-10-08,
User offline. Last seen 1 year 38 weeks ago.
Re: Another formatting question: Short variable names
mkString?

On Thu, Jun 11, 2009 at 2:47 AM, Kris Nuttycombe <kris [dot] nuttycombe [at] gmail [dot] com> wrote:
head/list

Any time variable names are abbreviated I have to mentally do the
expansion anyway. Why force the extra effort when you're talking about
two characters?

On Wed, Jun 10, 2009 at 4:46 PM, Bill Venners<bill [at] artima [dot] com> wrote:
> Hi All,
>
> How do people feel about hd for variables that will hold the head of a
> list, ls for list, etc. What about pred for predicate? Seems if there
> are some very commonly used variable names like that it could make
> things more readable, but head and list are just two characters longer
> each, so I question whether that wouldn't make code more readable in
> general.
>
> Dave Astels once said, whatever you would answer when asked what a
> particular variable is, that is what you should name that variable.
> Sometimes that approach can result in long variables that can obscure
> the logic, and I think sometimes an abbreviation makes things more
> readable. But I seem to lean more towards longer more descriptive
> variable names than others in my sphere of peers. When would you
> shorten or abbreviate a variable name, and how short would you go?
>
> Bill
> ----
> Bill Venners
> Artima, Inc.
> http://www.artima.com
>

Michael Campbell
Joined: 2009-03-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Another formatting question: Short variable names

Ben Hutchison wrote:
> Bill Venners wrote:
>> Hi All,
>>
>> How do people feel about hd for variables that will hold the head of a
>> list, ls for list, etc. What about pred for predicate?

> However convention establishes some shorter forms, eg x and xs for
> pieces of lists, f and g for opaque function values.

+1 to this. My domain work is mainly in financial software so I often use
"acct", "pmt", etc. for Account and Payment related entities.

That said, I'm a big "NAMES ARE IMPORTANT" guy and too often we, as programmers,
tend to rely (too much) on our ability to do quick mappings of "what this is" to
"what this means". As I get older, I prefer to do less of that and have been
really trying to name things what they are. I see the biggest offenders of
relying on their ability are the younger and less experienced programmers; or
maybe that should be less wizened/lazy? =)

However, as Ben mentioned, some conventions bypass the mapping altogether -
"acct" means "account" to me every bit as much as "account" does.

Walter Smith
Joined: 2009-05-13,
User offline. Last seen 1 year 39 weeks ago.
Re: Another formatting question: Short variable names

Bill Venners-3 wrote:
>
> When would you shorten or abbreviate a variable name, and how short would
> you go?
>

For variable names as for class, method, or any other names, I'm using full
names by default... the camel case expansion in Eclipse is such a great help
that I can't see any reason not to when typing. And for reading I find
longer names way more helpful, too. The only exceptions are abbreviations
that are more commonly used than the full form, like html or xml. For some
very mathematical algorithms I also prefer the abbreviations I'm used to
from that background, like p and q, and f and g for functions.

But these are exceptions! I find hd or ls just ugly and too much to think
about. But I'm using Java most of the time, and a larger community with a
common language develops it's own language culture, natural or programming
languages alike. It's more important that this does happen than to what end
it does. E.g. in Java most comparisons are done "in reverse" like
"martin".equals(name) in order to check the nullness of the variable as
well; I found it funny to see this idiom to pass along into JUnit
assertEquals methods, where the expected value comes first! The Java culture
develops, though: The current JUnit versions use Hamcrest that tries to
mimic English sentences... so it the more natural ordering of
assertThat(actualValue, is(expectedValue)).

I don't like xs either, but this seems to become part of the Scala culture,
and I will adopt it.

Walter

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland