3 Replies - 4063 Views - Last Post: 18 December 2012 - 11:55 PM Rate Topic: -----

#1 flexin1981  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-September 12

Best practices.

Posted 18 December 2012 - 02:56 PM

Hi Guys,

just have quick question, i am just starting to develop my first proper application. I have a question is there any best practices i should adhere to to make my life easier in the future versions of the app. For instance i have been writing code directly to the forms events like below;


        private void button3_Click(object sender, EventArgs e)
        {
            //Lots of code here
        }




Should i be writing a method in a separate class and just call it from the form like below;

        private void button3_Click(object sender, EventArgs e)
        {
            class C = new class();
            class.run();
        }



is there any performance increases or benefits to doing this way or am i just creating extra work for myself by doing this.

I would appreciate any advice from experienced dev's out there.

Many thanks

Flexin

Is This A Good Question/Topic? 0
  • +

Replies To: Best practices.

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: Best practices.

Posted 18 December 2012 - 03:02 PM

It's hard to sum it up in simple terms, because it's a complex subject. The short answer is "it depends". One thing is for sure, you don't need to make a new class for every function. The goal is logical separation of code into sections that make sense. Remember that Forms are classes too, so code that relates to the form itself should be in the form itself.

The general philosophy is separation of concerns. To use a contrived example, let's say we're dealing with "cars". You don't want to put all your logic directly in the form you're using to display the car. You'd create a car class that holds the information about the car, and has functions and properties to manipulate that information however it needs to be. Then you'd make a form that holds a Car object, that displays it, and calls the necessary methods on Car.

On the other hand, if you're writing simple one-off applications, there's nothing wrong with doing your work directly in the form's code-behind. Because if it's a single-form, single-use application, there's no reason to waste your time making an Entity class.

The upshot of all this is, put code where it logically makes sense. If you have a definable "thing", it's probably better to make a separate class for it, then put code related to displaying it in a separate form.
Was This Post Helpful? 4
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,838
  • Joined: 02-June 10

Re: Best practices.

Posted 18 December 2012 - 05:12 PM

Separating data from GUI - PLUS - serializing the data to XML

Some of my common tips (some may apply more than others to your specific style):
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readable. This is also how you can send parameters rather than use excessive global variables. Get in this habit even if you are using WinForms because WPF works a lot under the idea of "commands" and this will get you working towards that. Think of each gester, control click, menu option etc. as a command to do something such as a command to SAVE. It doesn't matter where the command comes from, all sources should point at the same target to do the actual saving.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attempts below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.

Was This Post Helpful? 1
  • +
  • -

#4 flexin1981  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-September 12

Re: Best practices.

Posted 18 December 2012 - 11:55 PM

Wow,

I am humbled by those responses. Many thanks for taking the time to write such detailed responses.

I really appreciate your advice.

Flexin
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1