Results 1 to 5 of 5

Thread: Code layout: When you personally are coding.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    Code layout: When you personally are coding.

    I always get worried and my layout when i code. I always get myself in a flap when coding and worried i should be doing it different.

    What i am going on about is not the code it self. But where i actually put the code.

    What i am thinking about is functionality (does it matter if formload code is at top/bottom/ or even middle)

    And read ability. Do i put all button events into regions? etc


    Make sense?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Code layout: When you personally are coding.

    It really depends what you find easiest to work with and also whether others will likely be reading or editing the code and what they would expect.

    Yes, you should certainly use regions in a code file of any real length. I tend to use the following top-level regions in the order they appear: Types, Constants, Fields, Properties, Constructors, Events, Event Handlers, Methods. If there are no members of a particular type then I omit the corresponding region. If it's warranted by the number of members it contains, I may break up a region with one or more sub-regions, e.g. if I have a MenuStrip or ToolStrip then all the event handlers relating to that will go in their own region.

    When it comes to event handlers, I tend to arrange them in a logical order, e.g. Load before FormClosing before FormClosed, KeyDown before KeyPress before KeyUp, etc. If there is no specific order that jumps out then whatever, although I will always group event handlers for the same object together.

    For other members, I tend to order them in descending order by access level, i.e. Public before Protected before Private, and then alphabetically within the same access level.

    That's just one way to do things. You might choose to do it quite differently but you certainly should have some specific arrangement in mind. Just putting anything anywhere is definitely not a good idea.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Code layout: When you personally are coding.

    for any larger than a throw away project, this is how I do it. I create regions for Constants, Private members, Private Properties, Public Properties, Constructors (and the destructor if needed), Private functions/Methods (within that group, functions first, then methods), Public Functions/Methods (same sorting in this group as the last), and lastly Event handlers... I leave the event handlers last because that's there the IDE seems to want to put them... so I leave the actual inserting of those to the IDE. If I re-wire a handler, giving it a new name (usually because the handler is going to handle the event of multiple controls) then I'll move it to the top of the region. Inside that region, things are sorted first by control name, then event name (again, mostly because that's what the IDE does by default.)

    For the most part, that seems to work for me.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    Re: Code layout: When you personally are coding.

    Thanks for that detailed response John. I thought it would make sense to have some sort of arrangement.

    I actually like your thinking there and going to try follow it in the future.

    Thanks again

    Edit: Thank you also techgnome for your reply.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Code layout: When you personally are coding.

    It sounds like I use the same regions as both JMC and TG. Not all that surprised by that.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width