Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. How do you organize code elements in a file?

How do you organize code elements in a file?

Scheduled Pinned Locked Moved The Lounge
tutorialquestion
37 Posts 23 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Rama Krishna Vavilala

    How do you organize classes, methods, properties and functions within a file?

    1. Do you put multiple classes in a single file? - If yes how do you order them.

    2. 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). 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.

    4. What are some other neat ways you have found or code organization is something you don't think about?

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #2
    1. 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."

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    L 1 Reply Last reply
    0
    • R Rama Krishna Vavilala

      How do you organize classes, methods, properties and functions within a file?

      1. Do you put multiple classes in a single file? - If yes how do you order them.

      2. 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). 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.

      4. What are some other neat ways you have found or code organization is something you don't think about?

      J Offline
      J Offline
      Joan M
      wrote on last edited by
      #3

      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.

      D S 2 Replies Last reply
      0
      • R Rama Krishna Vavilala

        How do you organize classes, methods, properties and functions within a file?

        1. Do you put multiple classes in a single file? - If yes how do you order them.

        2. 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). 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.

        4. What are some other neat ways you have found or code organization is something you don't think about?

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #4

        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.

        ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

        1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff
          1. 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."

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #5

          OriginalGriff wrote:

          1. No. Never. Bad idea.

          Why is it a bad idea? How do you deal with nested types? Put them in separate files too?

          ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

          OriginalGriffO 1 Reply Last reply
          0
          • R Rama Krishna Vavilala

            How do you organize classes, methods, properties and functions within a file?

            1. Do you put multiple classes in a single file? - If yes how do you order them.

            2. 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). 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.

            4. What are some other neat ways you have found or code organization is something you don't think about?

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #6

            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.

            R 1 Reply Last reply
            0
            • R Rama Krishna Vavilala

              How do you organize classes, methods, properties and functions within a file?

              1. Do you put multiple classes in a single file? - If yes how do you order them.

              2. 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). 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.

              4. What are some other neat ways you have found or code organization is something you don't think about?

              D Offline
              D Offline
              Daniel Scott
              wrote on last edited by
              #7

              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

              1 Reply Last reply
              0
              • R Rama Krishna Vavilala

                How do you organize classes, methods, properties and functions within a file?

                1. Do you put multiple classes in a single file? - If yes how do you order them.

                2. 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). 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.

                4. What are some other neat ways you have found or code organization is something you don't think about?

                N Offline
                N Offline
                Nagy Vilmos
                wrote on last edited by
                #8

                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 variables

                private 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)

                L 1 Reply Last reply
                0
                • L Luc Pattyn

                  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.

                  R Offline
                  R Offline
                  Rama Krishna Vavilala
                  wrote on last edited by
                  #9

                  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.

                  L 1 Reply Last reply
                  0
                  • N Nagy Vilmos

                    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 variables

                    private 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)

                    L Offline
                    L Offline
                    leppie
                    wrote on last edited by
                    #10

                    Nagy Vilmos wrote:

                    Most of my coding is Java now so it's explicitly required.

                    Not my problem ;p

                    ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                    N 1 Reply Last reply
                    0
                    • R Rama Krishna Vavilala

                      How do you organize classes, methods, properties and functions within a file?

                      1. Do you put multiple classes in a single file? - If yes how do you order them.

                      2. 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). 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.

                      4. What are some other neat ways you have found or code organization is something you don't think about?

                      R Offline
                      R Offline
                      Ravi Bhavnani
                      wrote on last edited by
                      #11

                      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 #regions. /ravi

                      My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                      B 1 Reply Last reply
                      0
                      • R Rama Krishna Vavilala

                        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.

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #12

                        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.

                        1 Reply Last reply
                        0
                        • L leppie

                          OriginalGriff wrote:

                          1. No. Never. Bad idea.

                          Why is it a bad idea? How do you deal with nested types? Put them in separate files too?

                          ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                          OriginalGriffO Offline
                          OriginalGriffO Offline
                          OriginalGriff
                          wrote on last edited by
                          #13

                          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."

                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                          L 1 Reply Last reply
                          0
                          • R Rama Krishna Vavilala

                            How do you organize classes, methods, properties and functions within a file?

                            1. Do you put multiple classes in a single file? - If yes how do you order them.

                            2. 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). 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.

                            4. What are some other neat ways you have found or code organization is something you don't think about?

                            E Offline
                            E Offline
                            Ennis Ray Lynch Jr
                            wrote on last edited by
                            #14

                            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

                            R 1 Reply Last reply
                            0
                            • L leppie

                              Nagy Vilmos wrote:

                              Most of my coding is Java now so it's explicitly required.

                              Not my problem ;p

                              ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                              N Offline
                              N Offline
                              Nagy Vilmos
                              wrote on last edited by
                              #15

                              [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)

                              L 1 Reply Last reply
                              0
                              • OriginalGriffO OriginalGriff

                                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."

                                L Offline
                                L Offline
                                leppie
                                wrote on last edited by
                                #16

                                OriginalGriff wrote:

                                Besides, I try to avoid nested classes!

                                You should not be avoiding it. It is extremely good for hiding abstractions.

                                ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                                1 Reply Last reply
                                0
                                • R Rama Krishna Vavilala

                                  How do you organize classes, methods, properties and functions within a file?

                                  1. Do you put multiple classes in a single file? - If yes how do you order them.

                                  2. 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). 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.

                                  4. What are some other neat ways you have found or code organization is something you don't think about?

                                  N Offline
                                  N Offline
                                  Nemanja Trifunovic
                                  wrote on last edited by
                                  #17

                                  Why are you assuming everybody writes C#/VB code?

                                  utf8-cpp

                                  R 1 Reply Last reply
                                  0
                                  • N Nagy Vilmos

                                    [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)

                                    L Offline
                                    L Offline
                                    leppie
                                    wrote on last edited by
                                    #18

                                    Nagy Vilmos wrote:

                                    I like Java.

                                    My answer stays the same :) Personally, I prefer Java over C# too, but only on Android.

                                    ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                                    1 Reply Last reply
                                    0
                                    • N Nemanja Trifunovic

                                      Why are you assuming everybody writes C#/VB code?

                                      utf8-cpp

                                      R Offline
                                      R Offline
                                      Rama Krishna Vavilala
                                      wrote on last edited by
                                      #19

                                      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.

                                      N Q 2 Replies Last reply
                                      0
                                      • R Rama Krishna Vavilala

                                        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.

                                        N Offline
                                        N Offline
                                        Nemanja Trifunovic
                                        wrote on last edited by
                                        #20

                                        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)."

                                        utf8-cpp

                                        R 1 Reply Last reply
                                        0
                                        • N Nemanja Trifunovic

                                          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)."

                                          utf8-cpp

                                          R Offline
                                          R Offline
                                          Rama Krishna Vavilala
                                          wrote on last edited by
                                          #21

                                          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.

                                          N 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups