473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Naming Convention for Temporaries

Hello:

Have you ever wanted a list of customer IDs, but had to get a list of
Customers first?

I am pretty conscientious about style and good variable names, so I am
wonder what other people do in certain situations when a temporary or
middle step is necessary.

IEnumerable<Customercustomers = getCustomers();
List<CustomerlistOfCustomers = new List<Customer>(customers);
List<intcustomerIds = listOfCustomers.ConvertAll<int>(delegate
(Customer current)
{
return current.ID;
});

listOfCustomers seems dirty to me. I mean, I could make that one line
of code, but then readibility suffers.

Just curious if other people have a better naming convention.

Thanks,
Travis
Jul 11 '08 #1
10 1292
je**********@gmail.com wrote:
Have you ever wanted a list of customer IDs, but had to get a list of
Customers first?
Can't say that I have. :-)
I am pretty conscientious about style and good variable names, so I am
wonder what other people do in certain situations when a temporary or
middle step is necessary.

IEnumerable<Customercustomers = getCustomers();
List<CustomerlistOfCustomers = new List<Customer>(customers);
List<intcustomerIds = listOfCustomers.ConvertAll<int>(delegate
(Customer current)
{
return current.ID;
});
Let's see. No C# 3.0, I take it? Otherwise there'd be no need for the clumsy
delegate syntax. Or a temporary list.

I would not declare a temporary for "customers". There's no point to having
this as an intermediary result, since all you can do with it is enumerate it
(once, usually). So I'd just do a little renaming:

List<Customercustomers = new List<Customer>(getCustomers());
listOfCustomers seems dirty to me. I mean, I could make that one line
of code, but then readibility suffers.
Eye of the beholder, etc. I will say that there's no point worrying about
the names you should give to your temporary variables if you don't have the
temporary variables in the first place. And I wouldn't consider having an
explicit IEnumerable<in my code to make it more readable, on the contrary.
Having multiple variables that express a different "view" of the same data
usually gets quite bothersome (think having "customerIdString" and
"customerId" for the raw value and the parsed value, for example -- you can
think of better names, but it doesn't change the fact that you've got two
variables referring to the "same" data).

And as for less readable, it's nothing compared to stuffing *everything* in
one line:

List<intcustomerIds = new
List<Customer>(getCustomers()).ConvertAll<int>(del egate (Customer current) {
return current.ID; })));

Now that's what I'd call less readable. Thankfully we have LINQ now for
these one-liners...

--
J.
Jul 11 '08 #2
On Fri, 11 Jul 2008 07:36:35 -0700, je**********@gmail.com
<je**********@gmail.comwrote:
Have you ever wanted a list of customer IDs, but had to get a list of
Customers first?

I am pretty conscientious about style and good variable names, so I am
wonder what other people do in certain situations when a temporary or
middle step is necessary.
Well, since you asked... :)

One of the things I like about the Hungarian naming convention is that it
removes these sorts of "how do I name this variable?" questions. If you
need a variable that's simply a temporary holder for something, you simply
use the "T" suffix. For example, if the normal Hungarian tag would be
"customer", then a variable that's a temporary holder for the variable
would be "customerT".

It's not clear that you actually need a temporary variable in this case.
As Jeroen suggests, the code could be consolidated to avoid that need.
And even if you do find the code more readable using an intermediate
variable (which is a fine justification for doing so), you may not find
that particular situation compelling enough to start learning Hungarian.

But just in case, check out:
http://www.byteshift.de/msg/hungaria...n-doug-klunder

Keeping in mind, of course, that the article was written decades ago and
there's room for extending the concepts described there to take into
account longer type and variable names and new fundamental types that
exist in languages like Java and C#. It's a starting point, not the last
word.

And maybe even the Wikipedia article on the topic:
http://en.wikipedia.org/wiki/Hungarian_notation
(paying particular note to the distinction made between "systems" and
"apps" Hungarian...stay away, as far as possible, from the "systems"
dialect, as it's particularly pointless and awkward).

Pete
Jul 11 '08 #3
On Jul 11, 10:58*am, Jeroen Mostert <jmost...@xs4all.nlwrote:
I would not declare a temporary for "customers". There's no point to having
this as an intermediary result, since all you can do with it is enumerateit
Unless there are significant performance issues (i.e. ones that a user
could possibly notice) then I opt to make as many intermediate
variables as possible just for debugging purposes. Nothing annoys me
more than debugging code like:

return SomeMethod( AnotherMethod( SomeObject.Property) );

And I want to see some intermediate variables. With a sufficiently
complex code base and various virtual method calls this style can get
you lost real quick and make you forgot what you were actually
debugging.

Jul 11 '08 #4
Israel wrote:
On Jul 11, 10:58 am, Jeroen Mostert <jmost...@xs4all.nlwrote:
>I would not declare a temporary for "customers". There's no point to having
this as an intermediary result, since all you can do with it is enumerate it

Unless there are significant performance issues (i.e. ones that a user
could possibly notice) then I opt to make as many intermediate
variables as possible just for debugging purposes. Nothing annoys me
more than debugging code like:

return SomeMethod( AnotherMethod( SomeObject.Property) );

And I want to see some intermediate variables. With a sufficiently
complex code base and various virtual method calls this style can get
you lost real quick and make you forgot what you were actually
debugging.
Two points: Visual Studio has excellent support for debugging compound
expressions (press F9 in the right spot and you're good to go) and second,
while I agree with your sentiment in general, having an IEnumerable as an
intermediary can be worse than useless, since enumerating it can change its
state and make further debugging pointless.

--
J.
Jul 11 '08 #5
Jeroen Mostert wrote:
Israel wrote:
>On Jul 11, 10:58 am, Jeroen Mostert <jmost...@xs4all.nlwrote:
>>I would not declare a temporary for "customers". There's no point to
having
this as an intermediary result, since all you can do with it is
enumerate it

Unless there are significant performance issues (i.e. ones that a user
could possibly notice) then I opt to make as many intermediate
variables as possible just for debugging purposes. Nothing annoys me
more than debugging code like:

return SomeMethod( AnotherMethod( SomeObject.Property) );

And I want to see some intermediate variables. With a sufficiently
complex code base and various virtual method calls this style can get
you lost real quick and make you forgot what you were actually
debugging.
Two points: Visual Studio has excellent support for debugging compound
expressions (press F9 in the right spot and you're good to go)
Eh, it's not as good as I remembered. It can't break on actual nested
expressions. You can break on the line and evaluate the subexpressions, of
course, but that's not the same thing.

The Immediate Window also relieves some of the hurt, but in general,
intermediaries are good. My point about IEnumerable intermediaries not being
so good stands, though.

--
J.
Jul 11 '08 #6
On Jul 11, 1:43*pm, Jeroen Mostert <jmost...@xs4all.nlwrote:
Two points: Visual Studio has excellent support for debugging compound
expressions (press F9 in the right spot and you're good to go) and second,
while I agree with your sentiment in general, having an IEnumerable as an
intermediary can be worse than useless, since enumerating it can change its
state and make further debugging pointless.
I guess I'm not sure I follow the part about the IEnumerable. In a
situation like the one noted at the begining of this discussion thread
code like this:
IEnumerable<Customercustomers = getCustomers();
List<CustomerlistOfCustomers = new List<Customer>(customers);
Should be functionally equivilent to this code:
List<CustomerlistOfCustomers = new List<Customer>(getCustomers());

And I guess my point was that the first method (which separates out
the code into 2 lines) is the pattern I usually prefer to make
debugging easier.

Jul 15 '08 #7
Israel wrote:
On Jul 11, 1:43 pm, Jeroen Mostert <jmost...@xs4all.nlwrote:
>Two points: Visual Studio has excellent support for debugging compound
expressions (press F9 in the right spot and you're good to go) and second,
while I agree with your sentiment in general, having an IEnumerable as an
intermediary can be worse than useless, since enumerating it can change its
state and make further debugging pointless.

I guess I'm not sure I follow the part about the IEnumerable. In a
situation like the one noted at the begining of this discussion thread
code like this:
IEnumerable<Customercustomers = getCustomers();
List<CustomerlistOfCustomers = new List<Customer>(customers);
Should be functionally equivilent to this code:
List<CustomerlistOfCustomers = new List<Customer>(getCustomers());

And I guess my point was that the first method (which separates out
the code into 2 lines) is the pattern I usually prefer to make
debugging easier.
My point was that evaluating an IEnumerable changes its state. If the
IEnumerable doesn't provide a usable .Reset() method (and many don't), then
you can't continue debugging the code. That's not particularly convenient.
If getCustomers() is safe to call more than once, you can reinitialize the
variable, but if you can do that then you might as well call getCustomers()
directly from the Immediate window or Quickwatch.

Using a temporary here makes sense if the IEnumerable is a custom
implementation (in other words, you're debugging the enumeration mechanism
itself) or if you need to quickly see if the IEnumerable is null, but for me
those cases are too rare to pre-emptively "temporarize" all my code. There's
good reason why the foreach-statement abstracts away from the IEnumerable
variable; they're almost never interesting on their own.

--
J.
Jul 15 '08 #8
Jeroen Mostert <jm******@xs4all.nlwrote:
My point was that evaluating an IEnumerable changes its state. If the
IEnumerable doesn't provide a usable .Reset() method (and many don't), then
you can't continue debugging the code. That's not particularly convenient.
If getCustomers() is safe to call more than once, you can reinitialize the
variable, but if you can do that then you might as well call getCustomers()
directly from the Immediate window or Quickwatch.
One quick point: there's a big difference between IEnumerable and
IEnumerator. Reset is part of IEnumerator, and you're right that it's
rarely implemented. However, in many (not all, but many) cases you can
call GetEnumerator() multiple times on the same IEnumerable.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 15 '08 #9
Jon Skeet [C# MVP] wrote:
Jeroen Mostert <jm******@xs4all.nlwrote:
>My point was that evaluating an IEnumerable changes its state. If the
IEnumerable doesn't provide a usable .Reset() method (and many don't), then
you can't continue debugging the code. That's not particularly convenient.
If getCustomers() is safe to call more than once, you can reinitialize the
variable, but if you can do that then you might as well call getCustomers()
directly from the Immediate window or Quickwatch.

One quick point: there's a big difference between IEnumerable and
IEnumerator.
My Big Book of Excuses tells me that the appropriate response here is "I
knew that!" I'm not convinced it's a good one, though.
Reset is part of IEnumerator, and you're right that it's
rarely implemented. However, in many (not all, but many) cases you can
call GetEnumerator() multiple times on the same IEnumerable.
Yes, I see. Consulting my handy Book again, I'm now supposed to say "I'm
sorry, that pretty much invalidates everything I said and I'm sorry for
wasting everyone's time", but honestly, why would anybody say *that*? I'm
going to get a refund.

*mumbles something about sleep deprivation*

--
J.
Jul 15 '08 #10
Jeroen Mostert <jm******@xs4all.nlwrote:
Reset is part of IEnumerator, and you're right that it's
rarely implemented. However, in many (not all, but many) cases you can
call GetEnumerator() multiple times on the same IEnumerable.
Yes, I see. Consulting my handy Book again, I'm now supposed to say "I'm
sorry, that pretty much invalidates everything I said and I'm sorry for
wasting everyone's time", but honestly, why would anybody say *that*? I'm
going to get a refund.

*mumbles something about sleep deprivation*
No need, because there *are* times when you really don't want to call
GetEnumerator() again. LINQ to SQL is a pretty obvious example :) Other
times there may be no way of enumerating again - if you're enumerating
a network stream, for example.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 16 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Derek | last post by:
The company where I work uses a naming convention that I have never used before. They use mixed-case letters for public member functions, but lower-case with underscores for the rest, like this:...
7
by: cmiddlebrook | last post by:
Hi there, I keep finding myself getting inconsistent with naming conventions for things like member variables, class names etc and I just want to find something that suits me and stick to it. I...
4
by: Mark Broadbent | last post by:
stupid question time again to most of you experts but this is something that continually bothers me. I am trying to get into the habit of naming variables and controls in an assembly as per...
14
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them......
0
by: Carl Colijn | last post by:
Hi all, Disclaimer: before I might trigger your "let's start a holy war!" button, I'd like to say I'm not intended to; I just post this message to get some input and not to promote "Yet Another...
10
by: jjkboswell | last post by:
I'm trying to pin down a good naming convention for the 3 things required to implement an event. You need: 1) a delegate 2) an event 3) an event handler Below is my understanding of a...
6
by: dm1608 | last post by:
I'm relatively new to ASP.NET 2.0 and am struggling with trying to find the best naming convention for the BAL and DAL objects within my database. Does anyone have any recommendations or best...
114
by: Jonathan Wood | last post by:
I was just wondering what naming convention most of you use for class variables. Underscore, "m_" prefix, camel case, capitalized, etc? Has one style emerged as the most popular? Thanks for...
35
by: Smithers | last post by:
Is it common practise to begin the name of form classes with "frm" (e.g., frmOneForm, frmAnotherForm). Or is that generally considered an outdated convention? If not "frm" what is a common or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.