How do you organize code elements in a file?
-
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.
-
-
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. I do not intend it to be langauge specific. The same question applies to C++ or C or Ruby.
I wouldn't say its entirely language agnostic... In C/C++ it is generally better - in large projects - to put more than 1 class per file. Otherwise the linking time when building the project becomes unmanageable and ultimately unproductive. In C#, this is less of a big deal and you can get away with putting 1 class per file - although some working on those "1,000,000+ files" projects may disagree... As for other languages I don't really use them to know.
-
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. Most of time i keep classes separated in different files, but sometimes (really small classes :), private classes, utilities...) I prefer to keep them in one file. 2. outside. 3. I organize new and essential classes, but I avoid to do it in legacy code. Usually I use this pattern: fields, constructors, properties, methods, event handling. I create regions for each. 4. I don't think about it, No.3 is my standard, but when situation ask I adapt.
-
-
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 organise my code in the One True Way - the way everyone should. 1. One file = one class, period. 2. Using statements at the top of the file, before anything else (except header comments). 3. Backwards: Private methods first, then public methods, then public properties, then constructor, then static methods. The idea is that readers get to know what each method property does, working from the inside out. When you get to the constructor and it calls a method called "PerformQuirkafleeg", you already know what a Quirkafleeg is and what happens when you perform one. 4. I think about code organisation all the time. Unfortunately, this means I change my mind quite often... but whatever I think at any given moment is "The One True Way", and should be followed by everyone.
-
-
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. Multiple classes in a file: Yes, as long as they are related in some way. For example, I have a file named
ValidationRules.cs
which implements a pile of binding-related validation rules. 2. Myusing
statements are outside namespaces and at the top of the file as God and Visual Studio intended. 3. My preferred order is:public
methods/properties first (andpublic
constructors first within those), followed by implementation details (internal methods, fields, etc.). I use this approach for both C++ and C#, especially the ordering. I've found that my coworkers have short attention spans and a very low tolerance for reading long bits of text. (Their eyes would have glazed over and they would have wandered off during item #2 above).Software Zen:
delete this;
-
-
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.
I like to group by functional relatedness and not visibility or any other arbitrary quality. I've found that by placing important public members towards the beginning of the file, and then related non-public members, it helps to improve readability.
Daniel Vaughan Twitter | Blog | Microsoft MVP | Projects: Calcium SDK, Clog | LinkedIn
-
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
Ravi Bhavnani wrote:
Judicious use of nested
#region
s.I agree totally with this statement. Regions by far to me are the most useful for making code files readable.
-Bryan My latest programming adventure was coding a facial recognition system for the Harry Potter Forbidden Journey Ride at Universal Studios Florida. I love my job.
-
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?
No.
Rama Krishna Vavilala wrote:
Do you put them outside namespaces or inside?
Outside
Rama Krishna Vavilala wrote:
Do you organize the methods and properties based on the accessibility?
Not strictly.
Rama Krishna Vavilala wrote:
What are some other neat ways you have found or code organization is something you don't think about?
I think about things that actually impact delivery. Such as how to test error cases.
-
-
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.
I'm like this too; constructors, then destructors, then operator=()'s, then methods alphabetically without regard to visibility, and at the bottom of the file, a static method called ModuleTest() that does the module test (duh). In the class declaration in the .h file, it's public accessors, then public constructors, destructors, and then public methods alphabetically, then protected accessors and methods, then private accessors and methods, then members. Since my getters and setters all have the form FooGet() and FooSet(), they always appear together. I have one .cpp file containing a class's method definitions with each .h file containing class declarations. Every now and then I put a little helper class in the .h file, and even more rarely I have to put a method definition for the helper class in the corresponding .cpp file. However, way too many times it happens that the little helper class eventually grows to need its own .h and .cpp files, so when I'm not in a rush, I just start out that way. Having so many internal rules may appear somewhat anal, but it makes the files really easy to navigate without even really looking at the method names. A method called ZoobaGet() is way down at the end of the file. Ctors and dtors go at the top because I fiddle them relatively frequently as I add/change members, and ModuleTest() goes at the bottom because I frequently add test cases. In general, alphabetizing the methods puts them in the same order as the class browser and autocompletion stuff puts them so these views of my class are familiar. I don't like nested classes. I prefer to control their visibility by only including their .h files where they are needed. When each class has a clear interface and good data hiding, there is no need to nest them. When the definitions for a single class get much over 1000 lines including my copious comments, it is a signal to me that my class is too big, and I have a separation-of-concerns problem. By 2500 lines, it takes way too long to page through a file.