BenH
BenH

Reputation: 2120

Advice on Naming Things

I have a list, and I'm trying to decide whether to call it item or items. If I call it items, it's clear that foo(items) is being passed a list of items and not a single item. On the other hand, I would prefer to write item[i] to refer to a particular element instead of items[i] (because I would say "bring me item 7", not "bring me items 7").

I also have a function, and I'm trying to decide whether to call it value or get_value. If I call it value, I can write nice statements like value_squared = value() ^ 2 (which I think is nicer than value_squared = get_value() ^ 2), but I might run into problems writing stuff like value = value(), whereas value = get_value() would not be problematic.

(I know that this question does not have a right answer, and that it is two questions. I am open to suggestions that I edit/remove/move/split it, etc., as I'm not quite sure what the right thing to do is. I would, however, like to see some responses, since I'd like to write code in a way that is easy for others to understand.)

Upvotes: 0

Views: 153

Answers (3)

N M
N M

Reputation: 26

I like plural names for lists and 'get' prefix for functions. I think immediately knowing that they are lists and functions outweighs the negatives.

Upvotes: 1

JonH
JonH

Reputation: 33153

This is always about preference, do what you or if you are on a team-what the team does.

It's about the shop you work at and not about being right or wrong. Besides it's just a name.

In my case I don't like to add "s"'s to anything. For instance, if I am designing a database system of issues with related comments I call my Issues table Issue, because one single tuple (or row) represents a single issue. Same for my Comments table I end up naming it Comment. I don't care that the total of all rows represents "Issues", to me the table represents one entity. But again this is personal preference.

You also mentioned this:

On the other hand, I would prefer to write item[i] to refer to a particular element instead of items[i] (because I would say "bring me item 7", not "bring me items 7").

But if your list is called items then if you did items[i] it should be a no brainer to see that you are referring to one item in the list. To me this is very readable.

As for your second point it really depends, do you really want to use value? In some languages that may be a reserved word. If the function returns a value why not getValue() ? I don't see any harm in that. Just be consistent with your entire project, it will benefit you and others looking at your code.

Upvotes: 1

paulsm4
paulsm4

Reputation: 121669

Call the list (the "container") "items". Call each element in the list an "item" :)

"items[i]" is perfectly appropriate.

So is:

  foreach (ListItem item in items) {
    ...

Upvotes: 3

Related Questions