473,657 Members | 2,559 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Naming conventions in For... each

Hello all,

If I have a class called "Foo," is there a preferred naming convention for
iterating through each Foo instance in a collection of Foos? I've seen
several different variations, even in the MSDN documentation:
#1:
For each Foo as Foo in Foos
...
Next Foo

This variation is direct, but a bit ambiguous since it's using the same name
for the Foo class and the Foo instance. (I know that the "Foo" in "Next
Foo" is optional -- I'm just including it for clarity.)
#2:
For each MyFoo [or aFoo, TheFoo, etc.] as Foo in Foos
...
Next MyFoo

This variation eliminates the ambiguity by adding a prefix to the instance
name. However, it seems a bit too "cutesy" for me.
#3:
For each f as Foo
...
Next f

This appears to be the C# convention, for better or worse. Terse and to the
point.
I tend to use the first variation, but wonder if this makes my code less
readable. Does anyone have a preference for any of these, or any other
variations? I don't want to start a religious war, but am curious about
whether there's a consistent "best practice."
Nov 20 '05 #1
48 1780
Hi Robert,

Shame about number 1. ;-)

I would say the only 'best practice' is don't give your variables the same
name as the class.

Is this a Foo which I see before me,
The handle toward my hand?
Come, let me clutch thee.
I have thee not, and yet I see thee still.

For you are the class not the object,
Or is it the other way round?
And now I'm all confused.
Everything else. I would say, is optional and as varied as you suggest,
and more.

For short pieces of code like a loop, the single letter can be good. I use
'c' for char loops, 'S' for simple string manipulation, 'F' for a Form in a
small Form utility routine, etc. Generally the scope will be small and the
variable used a lot.

For objects in general I use oDis and oDat. (is dat 'cos Oi'm Oirish,
maybe not). It's a habit with history and it's a pretty strong one - but I'm
re-evaluatiing it in .NET. It does have the advantage of using the same name
as the class.

I absolutely <hate> My anything - now <that's> cutesy.

But occasionally I use TheWidget or ThisWidget.

Regards,
Fergus


Nov 20 '05 #2
* "Robert Jacobson" <rj************ **********@nosp am.com> scripsit:
If I have a class called "Foo," is there a preferred naming convention for
iterating through each Foo instance in a collection of Foos? I've seen
several different variations, even in the MSDN documentation:
#1:
For each Foo as Foo in Foos
...
Next Foo

This variation is direct, but a bit ambiguous since it's using the same name
for the Foo class and the Foo instance. (I know that the "Foo" in "Next
Foo" is optional -- I'm just including it for clarity.)
I think that's a really bad convention.
#2:
For each MyFoo [or aFoo, TheFoo, etc.] as Foo in Foos
...
Next MyFoo

This variation eliminates the ambiguity by adding a prefix to the instance
name. However, it seems a bit too "cutesy" for me.
Seems "nice" to me.
#3:
For each f as Foo
...
Next f

This appears to be the C# convention, for better or worse. Terse and to the
point.


The "quick" solution.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #3
LOL! You need to write a book on the Art and Poetry of VB. Now if only you
could come up with bawdy VB limericks. "There was a VB programmer from
Nantucket..."

"Fergus Cooney" <fi******@tesco .net> wrote in message
news:eo******** ******@TK2MSFTN GP10.phx.gbl...

[Snip]
Is this a Foo which I see before me,
The handle toward my hand?
Come, let me clutch thee.
I have thee not, and yet I see thee still.

For you are the class not the object,
Or is it the other way round?
And now I'm all confused.

Nov 20 '05 #4
Well, two votes against my typical practice. I'll see if I can live with
aFoo, or the C# variation. Habits are hard to break. <g>
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
* "Robert Jacobson" <rj************ **********@nosp am.com> scripsit:
If I have a class called "Foo," is there a preferred naming convention for iterating through each Foo instance in a collection of Foos? I've seen
several different variations, even in the MSDN documentation:
#1:
For each Foo as Foo in Foos
...
Next Foo

This variation is direct, but a bit ambiguous since it's using the same name for the Foo class and the Foo instance. (I know that the "Foo" in "Next
Foo" is optional -- I'm just including it for clarity.)


I think that's a really bad convention.
#2:
For each MyFoo [or aFoo, TheFoo, etc.] as Foo in Foos
...
Next MyFoo

This variation eliminates the ambiguity by adding a prefix to the instance name. However, it seems a bit too "cutesy" for me.


Seems "nice" to me.
#3:
For each f as Foo
...
Next f

This appears to be the C# convention, for better or worse. Terse and to the point.


The "quick" solution.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Nov 20 '05 #5

"Robert Jacobson" <rj************ **********@nosp am.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Hello all,

If I have a class called "Foo," is there a preferred naming convention for
iterating through each Foo instance in a collection of Foos? I've seen
several different variations, even in the MSDN documentation:
#1:
For each Foo as Foo in Foos
...
Next Foo

This variation is direct, but a bit ambiguous since it's using the same name
for the Foo class and the Foo instance. (I know that the "Foo" in "Next
Foo" is optional -- I'm just including it for clarity.)
Yeah, not my favorite approach. Aids in confusion later.

#2:
For each MyFoo [or aFoo, TheFoo, etc.] as Foo in Foos
...
Next MyFoo

This variation eliminates the ambiguity by adding a prefix to the instance
name. However, it seems a bit too "cutesy" for me.
This is my choice.

#3:
For each f as Foo
...
Next f

This appears to be the C# convention, for better or worse. Terse and to the
point.
This works too, however, if we keep copying what they do in C#, it will only give them more reason
to think they are better than us...


I tend to use the first variation, but wonder if this makes my code less
readable. Does anyone have a preference for any of these, or any other
variations? I don't want to start a religious war, but am curious about
whether there's a consistent "best practice."

Nov 20 '05 #6
Fergus,
I would say the only 'best practice' is don't give your variables the same name as the class. Curious on where you came up with this idea? ;-)

As it seems to me to be the best practice! As the names should be
descriptive enough that the name & type can be used to determine its
meaning. If the type happens to be the most descriptive name then why not
use it?

In other words if I have a Person object, why not name the field person,
especially when I am dealing with a generic person or I am iterating a
collection of specialized (derived objects) people objects?

I don't have a single web link handy...

Just a thought
Jay

"Fergus Cooney" <fi******@tesco .net> wrote in message
news:eo******** ******@TK2MSFTN GP10.phx.gbl... Hi Robert,

Shame about number 1. ;-)

I would say the only 'best practice' is don't give your variables the same name as the class.

Is this a Foo which I see before me,
The handle toward my hand?
Come, let me clutch thee.
I have thee not, and yet I see thee still.

For you are the class not the object,
Or is it the other way round?
And now I'm all confused.
Everything else. I would say, is optional and as varied as you suggest, and more.

For short pieces of code like a loop, the single letter can be good. I use 'c' for char loops, 'S' for simple string manipulation, 'F' for a Form in a small Form utility routine, etc. Generally the scope will be small and the
variable used a lot.

For objects in general I use oDis and oDat. (is dat 'cos Oi'm Oirish,
maybe not). It's a habit with history and it's a pretty strong one - but I'm re-evaluatiing it in .NET. It does have the advantage of using the same name as the class.

I absolutely <hate> My anything - now <that's> cutesy.

But occasionally I use TheWidget or ThisWidget.

Regards,
Fergus

Nov 20 '05 #7
Robert,
Does anyone have a preference for any of these, or any other
variations? I use a variation of #1:
For Each foo As Foo in Foos
...
Next
Notice the lower case f in foo, I follow the .NET Design Guidelines for
Class Library Developers and all variables, fields & parameters are
camelCased.

Unless I have a more descriptive name I use the type name, as names should
be descriptive enough that the name & its type can be used to determine its
meaning. If the type happens to be most descriptive then that is the name I
use. On rare occasions will I use an abbreviations of the type, usually when
the type name is a reserved word, for example ch for Char, str for String.

Right now I don't have a specific link.
I don't want to start a religious war, but am curious about
whether there's a consistent "best practice." I don't think there is a best practice per se. I tend to feel as long as you
are consistent within your project, solution, team, enterprise. Remember the
variable you use is more then likely private to the method, so it really
doesn't matter, its there more for your & your teams readability.

Hope this helps
Jay

"Robert Jacobson" <rj************ **********@nosp am.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. .. Hello all,

If I have a class called "Foo," is there a preferred naming convention for
iterating through each Foo instance in a collection of Foos? I've seen
several different variations, even in the MSDN documentation:
#1:
For each Foo as Foo in Foos
...
Next Foo

This variation is direct, but a bit ambiguous since it's using the same name for the Foo class and the Foo instance. (I know that the "Foo" in "Next
Foo" is optional -- I'm just including it for clarity.)
#2:
For each MyFoo [or aFoo, TheFoo, etc.] as Foo in Foos
...
Next MyFoo

This variation eliminates the ambiguity by adding a prefix to the instance
name. However, it seems a bit too "cutesy" for me.
#3:
For each f as Foo
...
Next f

This appears to be the C# convention, for better or worse. Terse and to the point.
I tend to use the first variation, but wonder if this makes my code less
readable. Does anyone have a preference for any of these, or any other
variations? I don't want to start a religious war, but am curious about
whether there's a consistent "best practice."

Nov 20 '05 #8
Cor
Hi Robert,

When I see this, it reminds me on the first time I started with a database.

For each mare as horse in the stable
'bring it some water
next

I think that your program has to describe what you are doing that is the
first rule in all programming.

Cor
Nov 20 '05 #9
* "Rick Mogstad" <ri**@NOSPAM.co mputetosuit.com > scripsit:
#2:
For each MyFoo [or aFoo, TheFoo, etc.] as Foo in Foos
...
Next MyFoo

This variation eliminates the ambiguity by adding a prefix to the instance
name. However, it seems a bit too "cutesy" for me.


This is my choice.
#3:
For each f as Foo
...
Next f

This appears to be the C# convention, for better or worse. Terse and to the
point.


This works too, however, if we keep copying what they do in C#, it will only give them more reason
to think they are better than us...


I often used something like 'f' for loop variables very long time before
C# has been invented.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #10

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

Similar topics

1
6537
by: clintonG | last post by:
Does the use of DTD, XML Schema and similar constructs adopt the use of C# naming conventions? If so how do I make the distinction of how to apply C# conventions with XML elements, attributes and so on? Any referrals to resources that discuss or document XML Naming Conventions? -- <%= Clinton Gallagher, "Twice the Results -- Half the Cost" Architectural & e-Business Consulting -- Software Development NET...
4
7144
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 convensions. The thing is that Ive never really get the full reference to check against. Ive seen a couple of articles, but there always seems to be a bit missing. I also always seem to run into conflicting convensions both in code samples themselves...
3
4341
by: clintonG | last post by:
Does the use of DTD, XML Schema and similar constructs adopt the use of C# naming conventions? If so how do I make the distinction of how to apply C# conventions with XML elements, attributes and so on? Any referrals to resources that discuss or document XML Naming Conventions? -- <%= Clinton Gallagher, "Twice the Results -- Half the Cost" Architectural & e-Business Consulting -- Software Development NET...
5
6159
by: rastaman | last post by:
Hi all, I know of the existence of Object Naming Conventions for Visual Basic 6. Now I'm reading some books about VB .NET, but the names used for the objects are button1, picturebox1, etc. I wonder if I can use the same naming conventions as for VB6 ? If so, what about the names for the new objects in .NET. Kind regards rastaman
4
1778
by: Sturdy | last post by:
Hi, I'm new to C# and .NET. I'm a first time user of Visual C# 2005 Express and have a very basic question. I've looked at several links and lots of docs but can't find any tips on naming multiple controls used for the same purpose on different Forms. Specifically, I have several identical TextBox controls each on a different TabPage. The textboxes display the same variable content on each TabPage. An example might be display of name...
8
2083
by: Daz | last post by:
Hi all! This question may hopefully spark a little debate, but my problem is this: I am sure it's not just me who struggles to think up names for variables. Obviously, thinking up a name 'can' be simple, but when you are trying to create variable names that are easy to remember, descriptive, and not more than say 15-20 characters long, I come a cropper! Would anyone be able to give me any pointers as to how I can
35
12175
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 recommended practise? Thanks.
18
3276
by: psbasha | last post by:
Hi, I would like to know what naming conventions we can follow for the following types of variables/sequences : Variables : ------------- Integer Float Boolean
9
7022
by: BillCo | last post by:
I'm coming from a MS Access background and so I'm very used to and comfortable with the hungarian (Leszynski et al) naming conventions. However, I'm getting started into my first SQL Server Database and really want to use the appropriate up to date standard naming convention (ISO compliant). I think I have the general idea from raking though countless conflicting sites and posts, but I'm a bit stuck on what to do regarding pk / fk naming
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7324
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1611
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.