c# conventions

Jun 2, 2008
163
0
0
How do you guys program your code? Do you do all the functions at the bottom and call them in buttons etc???

I'm just curious what would be the optimal way for programming something so future programmers can read and understand at the clearest level.

 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Ask your new boss or coworkers what their conventions are. Chances are they have a set they use. With Visual Studio you can set Code Regions via #region Constructors //insert lines of code here and then an #endregion. That will create a region called Constructors that you can use to manipulate the whole block of code.

Doesn't affect the execution at all, but it allows you to minimize sections of code in files that you aren't working on at the time. I will group my declarations and functions based on role and use code regions to keep things organized. Everywhere is different though, so it's best to ask or look at some of their code if you can ;)
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
I basically group each class's members into public, protected, and private sections in that order. Within those sections I group methods, properties, delegates, and event handlers. All the support code is located in private methods, so the event handlers are usually very clean.
 

KB

Diamond Member
Nov 8, 1999
5,406
389
126
I try to add the code to the business object and then have the button event handler just call the business objects method. For example the business object has a Save method and then the OK button event handler will just call bobject.Save().

If the code interacts with the UI, then the code will be within a method of the form object. Again the Event handler is left fairly empty and readable.

private void Button1_Click(object o, EventHandler e){
this.DoSomething();
this.DotheNextThing();
bobject.Save();
}

 

imported_Dhaval00

Senior member
Jul 23, 2004
573
0
0
If you work for a company/team of developers, and don't have a coding standard, I guess this would be a good time to establish one :). We had a similar issue at our company... we kept ranting about each other's coding styles, before we all sat together to establish a uniform coding convention (things like members should be at the top, followed by constructors, then properties, syntax for XML comments, how to use regions smartly, etc.). We also penalize people (verbally) during code reviews if they don't follow our coding standards - be it contractors or full-timers.

If you haven't already, propose it to your boss... you can then sell yourself at the end of the year when he conducts that performance review ;).

I guess in the long term it doesn't matter. As long as there is consistency within the given environment, people will be happy.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Following the conventions makes people happy. At my current job I inherited a LOT of VB6 code(to be ported to C#) written by the same developer... every single one follows different conventions. It's basically a clusterfuck of VB and highly annoying. It's also filled with rather odd 'shortcuts' which only up end making the code harder to read.

Please don't be that guy!