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.
  • 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
                          • 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?

                            M Offline
                            M Offline
                            Marc Clifton
                            wrote on last edited by
                            #22

                            using drugs;

                            namespace Dopey
                            {
                            public ClassOf1980
                            {
                            protected List marijuanaFields;
                            public CriminalRecord InnocentOf {get; protected set;}

                            // Construct More Growing Rooms
                            public ClassOf1980
                            {
                            }
                            
                            public int Deal(int amt)
                            {
                            }
                            
                            protected bool Grow()
                            {
                            }
                            
                            private void Use()
                            {
                            }
                            

                            }

                            Does that help? Marc

                            My Blog

                            1 Reply Last reply
                            0
                            • R Rama Krishna Vavilala

                              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 Offline
                              N Offline
                              Nemanja Trifunovic
                              wrote on last edited by
                              #23

                              Rama Krishna Vavilala wrote:

                              I elaborated it in more detail for the numerous C# programmers and the one VB.NET programmer we have here.

                              For the numerous VB programmers and the one who publicly admits he codes in VB :)

                              utf8-cpp

                              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?

                                C Offline
                                C Offline
                                Chris C B
                                wrote on last edited by
                                #24

                                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:

                                N 1 Reply Last reply
                                0
                                • C Chris C B

                                  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:

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

                                  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)

                                  C 1 Reply Last reply
                                  0
                                  • N Nagy Vilmos

                                    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)

                                    C Offline
                                    C Offline
                                    Chris C B
                                    wrote on last edited by
                                    #26

                                    Thank you Sir, you are a gentleman! :rose:

                                    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?

                                      W Offline
                                      W Offline
                                      wizardzz
                                      wrote on last edited by
                                      #27

                                      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

                                      1 Reply Last reply
                                      0
                                      • E Ennis Ray Lynch Jr

                                        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 Offline
                                        R Offline
                                        RichardGrimmer
                                        wrote on last edited by
                                        #28

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

                                        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?

                                          P Offline
                                          P Offline
                                          PIEBALDconsult
                                          wrote on last edited by
                                          #29

                                          Yes.

                                          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