How do you organize code elements in a file?
-
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?
- No. Never. Bad idea. 2) I follow the VS Default: outside the namespace. 3) I do, but not in that order. Here are my standard class regions (as imported with a new class file)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ParseTags.Parsing;namespace SlideShowManager
{
/// ///
///
internal class MyClass : MyBaseClass
{
#region Constants
#endregion#region Fields #region Internal #endregion #region Property bases #endregion #endregion #region Properties #endregion #region Regular Expressions #endregion #region Enums #endregion #region Constructors #endregion #region Events #region Event Constructors #endregion #region Event Handlers #endregion #endregion #region internal Methods #endregion #region Overrides #endregion #region Private Methods #endregion } }
Generally, my methods are organised by function groups, and may be split into different files if the class is too big (which may also have the same defines as above). Within a group, the methods are generally in alphabetical order (or creation order, earliest first, if I have been too lazy to move them) 4) This works for me: with Outlining I can find things easily, and I insist on XML comments so that Intellisense works. Oh, and "Treat warnings as errors" on as well.
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?
Rama Krishna Vavilala wrote:
Do you put multiple classes in a single file? - If yes how do you order them.
Never... I always use different files for different classes...
Rama Krishna Vavilala wrote:
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).
Usually I put Constructors and destructors first and then the functions are sometimes alphabetically organized but mainly grouped by kind/data getters/setters... (but my job is very special). JPS: you should have started your post with "this is not a programming question..." :laugh:
[www.tamelectromecanica.com] Robots, CNC and PLC machines for grinding and polishing.
-
-
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?
1. Yes, no order (unless there is a class with a designer, like UserControl or Form). 2. Outside, sorted (with VS's refactor thingy). 3. Nope, dont care (F12 takes care of it). 4. Nope, dont care.
-
-
- No. Never. Bad idea. 2) I follow the VS Default: outside the namespace. 3) I do, but not in that order. Here are my standard class regions (as imported with a new class file)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ParseTags.Parsing;namespace SlideShowManager
{
/// ///
///
internal class MyClass : MyBaseClass
{
#region Constants
#endregion#region Fields #region Internal #endregion #region Property bases #endregion #endregion #region Properties #endregion #region Regular Expressions #endregion #region Enums #endregion #region Constructors #endregion #region Events #region Event Constructors #endregion #region Event Handlers #endregion #endregion #region internal Methods #endregion #region Overrides #endregion #region Private Methods #endregion } }
Generally, my methods are organised by function groups, and may be split into different files if the class is too big (which may also have the same defines as above). Within a group, the methods are generally in alphabetical order (or creation order, earliest first, if I have been too lazy to move them) 4) This works for me: with Outlining I can find things easily, and I insist on XML comments so that Intellisense works. Oh, and "Treat warnings as errors" on as well.
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:
- No. Never. Bad idea.
Why is it a bad idea? How do you deal with nested types? Put them in separate files too?
-
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?
1. Inner classes obviously yes, regular classes occasionally, if they are few and small, at the end (except for enums that participate in the file's main class "API"). 2. Using statements outside namespace (pretty irrelevant, I never have several namespaces in one file).
Rama Krishna Vavilala wrote:
Do you organize the methods and properties based on the accessibility?
3. Not by accessibility; data fields first (static, then instance), then constructors, logging methods, properties, methods. And methods pretty much by your "Step Down rule". And similar methods close together (e.g. all event handlers). I don't consider this very important, a typical IDE is very good at navigating around.
Rama Krishna Vavilala wrote:
What are some other neat ways you have found or code organization is something you don't think about?
4. It is always good to have an open question! My main concern is readability, which includes: - comments describing the functionality of public stuff, and the fundamental implementation choices made (e.g. reference to algorithms used) - carefully choose identifier names for items with non-local scope. - and many many more. You did not mention regions. I don't use them. Again, IDE navigation is good enough without them. If I were to have a large class (>1000 LOC) and wanted regions, I'd probably go for multiple files ("partial class" would help). :)
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.
-
-
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?
1. only if they're nested 2. outside, because they're put there by VS and I see no reason to change it 3. I usually group by "kind" first (field, ctor, property, method) and then "things that use each other close" 4. I use many #regions, and partial classes can also be useful to "hide" things
-
-
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?
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)
-
-
Rama Krishna Vavilala wrote:
Do you put multiple classes in a single file?
1. Inner classes obviously yes, regular classes occasionally, if they are few and small, at the end (except for enums that participate in the file's main class "API"). 2. Using statements outside namespace (pretty irrelevant, I never have several namespaces in one file).
Rama Krishna Vavilala wrote:
Do you organize the methods and properties based on the accessibility?
3. Not by accessibility; data fields first (static, then instance), then constructors, logging methods, properties, methods. And methods pretty much by your "Step Down rule". And similar methods close together (e.g. all event handlers). I don't consider this very important, a typical IDE is very good at navigating around.
Rama Krishna Vavilala wrote:
What are some other neat ways you have found or code organization is something you don't think about?
4. It is always good to have an open question! My main concern is readability, which includes: - comments describing the functionality of public stuff, and the fundamental implementation choices made (e.g. reference to algorithms used) - carefully choose identifier names for items with non-local scope. - and many many more. You did not mention regions. I don't use them. Again, IDE navigation is good enough without them. If I were to have a large class (>1000 LOC) and wanted regions, I'd probably go for multiple files ("partial class" would help). :)
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.
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.
-
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.