|
-
Oct 30th, 2010, 06:49 AM
#1
Thread Starter
Fanatic Member
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?
-
Oct 30th, 2010, 07:10 AM
#2
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.
-
Oct 30th, 2010, 07:48 AM
#3
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
-
Oct 30th, 2010, 07:50 AM
#4
Thread Starter
Fanatic Member
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.
-
Oct 30th, 2010, 09:36 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|