How do you organize code elements in a file?
-
0. All public clases shoudl be in their own file. Most of my coding is Java now so it's explicitly required. 1. Structure by type/visibilty:
class MyClass
extends Something
impliments SomethingElse{
private static final String CONST = "CONSTANT";
private static String classVariable;
// all the static variablesprivate String memberVariable;
static {
// static initialise
}MyClass() {
// constructors
}public void doSomething() {
// methods, starting public and ending private
}public static String getStatic() {
// static methods, starting public and ending private
}
}There's my tuppence.
Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre Have a bit more patience with newbies. Of course some of them act dumb -- they're often *students*, for heaven's sake. -- (Terry Pratchett, alt.fan.pratchett)
Nagy Vilmos wrote:
Most of my coding is Java now so it's explicitly required.
Not my problem ;p
-
How do you organize classes, methods, properties and functions within a file?
-
Do you put multiple classes in a single file? - If yes how do you order them.
-
What about using statements (or import statements if you are John Simmons)? Do you put them outside namespaces or inside?
-
Do you organize the methods and properties based on the accessibility? Stylecop default order is fields, followed by Constructors, followed by properties which are followed by methods (public come first and private last). Another way is to follow the Step Down rule. Where order is based on what method calls what. For instance, here is an example of step down rules.
void Main()
{
OpenOrCreateDatabase();
LoadRecords();
ProcessRecords();
}void OpenOrCreateDatabase()
{
if (DatabaseExists())
{
OpenDatabase();
}
else
{
CreateDatabase();
}
}bool DatabaseExists()
{
...
}void OpenDatabase()
{
}void CreateDatabase()
{
}As Main is the entry point it comes first followed by the very first method which it calls (OpenOrCreateDatabase). OpenOrCreateDatabase is followed by the first method which it calls, followed by the second method and so on. The idea behind the step down rule is that code is easier to read and comprehend for other.
-
What are some other neat ways you have found or code organization is something you don't think about?
Rama Krishna Vavilala wrote:
Do you put multiple classes in a single file? - If yes how do you order them.
No. I might sometimes split a large class into (appropriately named) multiple files.
Rama Krishna Vavilala wrote:
What about using statements
Outside the namespace.
Rama Krishna Vavilala wrote:
Do you organize the methods and properties based on the accessibility?
A la StyleCop, except that I put fields last since (imho) they're the least visible entities in a class. I tend to position methods in alpha order (within a method group) as it makes them easier to find when scrolling through a file.
Rama Krishna Vavilala wrote:
What are some other neat ways you have found or code organization is something you don't think about?
Judicious use of nested
#region
s. /raviMy new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
-
Luc Pattyn wrote:
You did not mention regions. I don't use them.
:thumbsup:
Luc Pattyn wrote:
. If I were to have a large class (>1000 LOC) and wanted regions, I'd probably go for multiple files
:thumbsup::thumbsup: Regions were good when they first came out but I do not like them as they are always hidden by default.
Rama Krishna Vavilala wrote:
as they are always hidden by default
Yes, that was my immediate objection too. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
OriginalGriff wrote:
- No. Never. Bad idea.
Why is it a bad idea? How do you deal with nested types? Put them in separate files too?
Yes. Every class in a separate file. Since a class can be spread over several files, it is no big chore to put nested classes in a separate one. Besides, I try to avoid nested classes!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
How do you organize classes, methods, properties and functions within a file?
-
Do you put multiple classes in a single file? - If yes how do you order them.
-
What about using statements (or import statements if you are John Simmons)? Do you put them outside namespaces or inside?
-
Do you organize the methods and properties based on the accessibility? Stylecop default order is fields, followed by Constructors, followed by properties which are followed by methods (public come first and private last). Another way is to follow the Step Down rule. Where order is based on what method calls what. For instance, here is an example of step down rules.
void Main()
{
OpenOrCreateDatabase();
LoadRecords();
ProcessRecords();
}void OpenOrCreateDatabase()
{
if (DatabaseExists())
{
OpenDatabase();
}
else
{
CreateDatabase();
}
}bool DatabaseExists()
{
...
}void OpenDatabase()
{
}void CreateDatabase()
{
}As Main is the entry point it comes first followed by the very first method which it calls (OpenOrCreateDatabase). OpenOrCreateDatabase is followed by the first method which it calls, followed by the second method and so on. The idea behind the step down rule is that code is easier to read and comprehend for other.
-
What are some other neat ways you have found or code organization is something you don't think about?
I have a rule ... every time I think I need a region, I refactor. Regions are for bad programmers. For those of you that argue, remember exactly what category I am going to place you the second you defend a region. In general I like constructors at the bottom, members at the top, static members at the toppermost, properties after members, and then code in the middle. I have a strong preference for helper methods and private methods to be next to their associated methods. I always specify my access modifiers (public, private, etc) One thing I have experimented with is partial classes and assigning groupings in the VS project file so they show up in the tree together. It does make it easy to create a large class with a lot of properties but I still feel like it is a poor solution. I put my imports at the top. I don't care what style cop says ... (Ironically, when I pay people to code I do care what style cop says, I guess that makes me a hypocrite)
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
-
Nagy Vilmos wrote:
Most of my coding is Java now so it's explicitly required.
Not my problem ;p
[Vilmos prepares to loose half his rep] I like Java.
Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre Have a bit more patience with newbies. Of course some of them act dumb -- they're often *students*, for heaven's sake. -- (Terry Pratchett, alt.fan.pratchett)
-
Yes. Every class in a separate file. Since a class can be spread over several files, it is no big chore to put nested classes in a separate one. Besides, I try to avoid nested classes!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
OriginalGriff wrote:
Besides, I try to avoid nested classes!
You should not be avoiding it. It is extremely good for hiding abstractions.
-
How do you organize classes, methods, properties and functions within a file?
-
Do you put multiple classes in a single file? - If yes how do you order them.
-
What about using statements (or import statements if you are John Simmons)? Do you put them outside namespaces or inside?
-
Do you organize the methods and properties based on the accessibility? Stylecop default order is fields, followed by Constructors, followed by properties which are followed by methods (public come first and private last). Another way is to follow the Step Down rule. Where order is based on what method calls what. For instance, here is an example of step down rules.
void Main()
{
OpenOrCreateDatabase();
LoadRecords();
ProcessRecords();
}void OpenOrCreateDatabase()
{
if (DatabaseExists())
{
OpenDatabase();
}
else
{
CreateDatabase();
}
}bool DatabaseExists()
{
...
}void OpenDatabase()
{
}void CreateDatabase()
{
}As Main is the entry point it comes first followed by the very first method which it calls (OpenOrCreateDatabase). OpenOrCreateDatabase is followed by the first method which it calls, followed by the second method and so on. The idea behind the step down rule is that code is easier to read and comprehend for other.
-
What are some other neat ways you have found or code organization is something you don't think about?
Why are you assuming everybody writes C#/VB code?
-
-
[Vilmos prepares to loose half his rep] I like Java.
Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre Have a bit more patience with newbies. Of course some of them act dumb -- they're often *students*, for heaven's sake. -- (Terry Pratchett, alt.fan.pratchett)
Nagy Vilmos wrote:
I like Java.
My answer stays the same :) Personally, I prefer Java over C# too, but only on Android.
-
Why are you assuming everybody writes C#/VB code?
It is a language agnostic question. I do not intend it to be langauge specific. The same question applies to C++ or C or Ruby.
-
It is a language agnostic question. I do not intend it to be langauge specific. The same question applies to C++ or C or Ruby.
Rama Krishna Vavilala wrote:
It is a language agnostic question
How does this apply to anything but .NET?: "What about using statements (or import statements if you are John Simmons)? Do you put them outside namespaces or inside? 3. Do you organize the methods and properties based on the accessibility? Stylecop default order is fields, followed by Constructors, followed by properties which are followed by methods (public come first and private last)."
-
Rama Krishna Vavilala wrote:
It is a language agnostic question
How does this apply to anything but .NET?: "What about using statements (or import statements if you are John Simmons)? Do you put them outside namespaces or inside? 3. Do you organize the methods and properties based on the accessibility? Stylecop default order is fields, followed by Constructors, followed by properties which are followed by methods (public come first and private last)."
Well, the original question was "ow do you organize code elements in a file?". I elaborated it in more detail for the numerous C# programmers and the one VB.NET programmer we have here.
-
How do you organize classes, methods, properties and functions within a file?
-
Do you put multiple classes in a single file? - If yes how do you order them.
-
What about using statements (or import statements if you are John Simmons)? Do you put them outside namespaces or inside?
-
Do you organize the methods and properties based on the accessibility? Stylecop default order is fields, followed by Constructors, followed by properties which are followed by methods (public come first and private last). Another way is to follow the Step Down rule. Where order is based on what method calls what. For instance, here is an example of step down rules.
void Main()
{
OpenOrCreateDatabase();
LoadRecords();
ProcessRecords();
}void OpenOrCreateDatabase()
{
if (DatabaseExists())
{
OpenDatabase();
}
else
{
CreateDatabase();
}
}bool DatabaseExists()
{
...
}void OpenDatabase()
{
}void CreateDatabase()
{
}As Main is the entry point it comes first followed by the very first method which it calls (OpenOrCreateDatabase). OpenOrCreateDatabase is followed by the first method which it calls, followed by the second method and so on. The idea behind the step down rule is that code is easier to read and comprehend for other.
-
What are some other neat ways you have found or code organization is something you don't think about?
using drugs;
namespace Dopey
{
public ClassOf1980
{
protected List marijuanaFields;
public CriminalRecord InnocentOf {get; protected set;}// Construct More Growing Rooms public ClassOf1980 { } public int Deal(int amt) { } protected bool Grow() { } private void Use() { }
}
Does that help? Marc
-
-
Well, the original question was "ow do you organize code elements in a file?". I elaborated it in more detail for the numerous C# programmers and the one VB.NET programmer we have here.
-
How do you organize classes, methods, properties and functions within a file?
-
Do you put multiple classes in a single file? - If yes how do you order them.
-
What about using statements (or import statements if you are John Simmons)? Do you put them outside namespaces or inside?
-
Do you organize the methods and properties based on the accessibility? Stylecop default order is fields, followed by Constructors, followed by properties which are followed by methods (public come first and private last). Another way is to follow the Step Down rule. Where order is based on what method calls what. For instance, here is an example of step down rules.
void Main()
{
OpenOrCreateDatabase();
LoadRecords();
ProcessRecords();
}void OpenOrCreateDatabase()
{
if (DatabaseExists())
{
OpenDatabase();
}
else
{
CreateDatabase();
}
}bool DatabaseExists()
{
...
}void OpenDatabase()
{
}void CreateDatabase()
{
}As Main is the entry point it comes first followed by the very first method which it calls (OpenOrCreateDatabase). OpenOrCreateDatabase is followed by the first method which it calls, followed by the second method and so on. The idea behind the step down rule is that code is easier to read and comprehend for other.
-
What are some other neat ways you have found or code organization is something you don't think about?
What's this? You can have more than one file in a project? Well, who knew! I just have one great big file, and lots and lots of GOTOs. :laugh: Sorry guys, but for over a month now I have been trying to buy a new camera by remote control, and finally, after much blood, sweat and tears, I have just heard that it is winging it's way to me, courtesy of DHL, and as a result I am feeling rather frivolous. :cool:
-
-
What's this? You can have more than one file in a project? Well, who knew! I just have one great big file, and lots and lots of GOTOs. :laugh: Sorry guys, but for over a month now I have been trying to buy a new camera by remote control, and finally, after much blood, sweat and tears, I have just heard that it is winging it's way to me, courtesy of DHL, and as a result I am feeling rather frivolous. :cool:
Some scrote 1'ed ya, have a batch of GOSUB's to compensate.
Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre Have a bit more patience with newbies. Of course some of them act dumb -- they're often *students*, for heaven's sake. -- (Terry Pratchett, alt.fan.pratchett)
-
Some scrote 1'ed ya, have a batch of GOSUB's to compensate.
Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre Have a bit more patience with newbies. Of course some of them act dumb -- they're often *students*, for heaven's sake. -- (Terry Pratchett, alt.fan.pratchett)
-
How do you organize classes, methods, properties and functions within a file?
-
Do you put multiple classes in a single file? - If yes how do you order them.
-
What about using statements (or import statements if you are John Simmons)? Do you put them outside namespaces or inside?
-
Do you organize the methods and properties based on the accessibility? Stylecop default order is fields, followed by Constructors, followed by properties which are followed by methods (public come first and private last). Another way is to follow the Step Down rule. Where order is based on what method calls what. For instance, here is an example of step down rules.
void Main()
{
OpenOrCreateDatabase();
LoadRecords();
ProcessRecords();
}void OpenOrCreateDatabase()
{
if (DatabaseExists())
{
OpenDatabase();
}
else
{
CreateDatabase();
}
}bool DatabaseExists()
{
...
}void OpenDatabase()
{
}void CreateDatabase()
{
}As Main is the entry point it comes first followed by the very first method which it calls (OpenOrCreateDatabase). OpenOrCreateDatabase is followed by the first method which it calls, followed by the second method and so on. The idea behind the step down rule is that code is easier to read and comprehend for other.
-
What are some other neat ways you have found or code organization is something you don't think about?
I leave it in the order i copy and paste my codez from the interwebs ;)
Craigslist Troll: litaly@comcast.net "I have a theory that the truth is never told during the nine-to-five hours. " — Hunter S. Thompson
-
-
I have a rule ... every time I think I need a region, I refactor. Regions are for bad programmers. For those of you that argue, remember exactly what category I am going to place you the second you defend a region. In general I like constructors at the bottom, members at the top, static members at the toppermost, properties after members, and then code in the middle. I have a strong preference for helper methods and private methods to be next to their associated methods. I always specify my access modifiers (public, private, etc) One thing I have experimented with is partial classes and assigning groupings in the VS project file so they show up in the tree together. It does make it easy to create a large class with a lot of properties but I still feel like it is a poor solution. I put my imports at the top. I don't care what style cop says ... (Ironically, when I pay people to code I do care what style cop says, I guess that makes me a hypocrite)
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
Ennis Ray Lynch, Jr. wrote:
Regions are for bad programmers.
Why's that then?
C# has already designed away most of the tedium of C++.
-
How do you organize classes, methods, properties and functions within a file?
-
Do you put multiple classes in a single file? - If yes how do you order them.
-
What about using statements (or import statements if you are John Simmons)? Do you put them outside namespaces or inside?
-
Do you organize the methods and properties based on the accessibility? Stylecop default order is fields, followed by Constructors, followed by properties which are followed by methods (public come first and private last). Another way is to follow the Step Down rule. Where order is based on what method calls what. For instance, here is an example of step down rules.
void Main()
{
OpenOrCreateDatabase();
LoadRecords();
ProcessRecords();
}void OpenOrCreateDatabase()
{
if (DatabaseExists())
{
OpenDatabase();
}
else
{
CreateDatabase();
}
}bool DatabaseExists()
{
...
}void OpenDatabase()
{
}void CreateDatabase()
{
}As Main is the entry point it comes first followed by the very first method which it calls (OpenOrCreateDatabase). OpenOrCreateDatabase is followed by the first method which it calls, followed by the second method and so on. The idea behind the step down rule is that code is easier to read and comprehend for other.
-
What are some other neat ways you have found or code organization is something you don't think about?
Yes.
-