begginer

R

RobcPettit

HI, please forgive this question. Im new to c# and as you probably seen
from prev questions Ive got along way to go. Ive been woking my way
through a couple of books, asking questions here and Id say im
improving. After asking a couple questions yesterday I realised theres
one arae that I just dont seem to grasp, and the books dont make clear.
Basically Ive got code which reads a text file, grabs html from web
site and writes to another text file, in the meen time keeps user
informed as to how many records processed. Upto now ive used a form
with a button and texbox, and put all code in the buttonclick command.
And this is the problem were should the bulk of the code be written. Do
I add a class to my project, put code there, and call it with button.
So far ive used class files in the reference of the project. Im reading
about threading which is what made me think Im putting my code in the
wrong place. And all though the books say classes are one of the most
important areas, there not very clear on what to do when writing alot
of code. Hope this makes sense.
Regards Robert
 
B

Barry Kelly

And this is the problem were should the bulk of the code be written.

For simple algorithms, a single method is fine. If you're going to reuse
it, or if it is going to be maintained separately, it should be put into
a separate method and called rather than left inline.

Where you see the code do something again and again, refactor the
repeated code as a separate method, and replace the original code with
calls to the method. Add parameters if that's necessary to get the same
effects. Similarly, if the code is doing different things that can be
logically packaged, refactor. If the code is doing too much to
understand easily, refactor. This is called functional decomposition.

Where you need to share variables among a group of functions, look for
logical entities to which these variables logically belong, and move the
variables to fields of classes, and move the methods which used these
variables to methods of these classes. This is called object-oriented
decomposition.

Both decomposition techniques require experience and a developed skill
to get right. Developing larger programs, reading existing code and
examining how well-designed frameworks such as the .NET frameworks are
put together helps develop this skill.

There are higher forms of (de)composition, involving more meta-notions
that aren't explicit language structures, such as design patterns. If
you read about these after you've seen a few frameworks, you'll start
recognizing them.

Good luck!

-- Barry
 
R

RobcPettit

Thanks Barry for your reply. Plenty of errors for me to read up on.
Thanks for taking the time to point these out.
Regards Robert
 
D

Daniel

Sometimes an example helps too so for your text reading thing i guess you
have something like

public void ButtonClick(object sender, e eventArgs)
{
for(int i=0; i<numRecs; i++)
{
//get record and read it
//do all your stuff with it
//inform user
//write text file
//do something else
//etc etc etc
}
}

Reulting in a big button method. As Barry said break this down into
something logical and easy to read and keep in mind scalability and
maintenance.

Now say you do a new project and think, that code i wrote for the text
reader was good lets reuse that, you will then go into that button, copy the
code and paste to a new project. Say you use it in 6 projects and along the
way you update the code or someone else wnats to use it this is a problem.
Just one problem of many from not doing this right to be honest.

So you break it down by making a new class and create get and set methods
and so on. A good starting point to understand classes is to write a basic
calculator.

At the moment i bet you'd do something like this:

Public ButtonClick()
{
switch(choice)
{
case divide:
return x/y;
case multiply:
return x*y;
}
}

etc

But a better way is a new class called say Calculator and you'd make methods
such as these in that class

public int Add(int x, int y)
{
return x +y;
}
public int Subtract(int x, int y)
{
return x-y;
}
public int Divide(int x, int y)
{
return x/y;
}

etc

Now your button click code would become

Public ButtonClick()
{
Calculator myCalc = new Calculator();

switch(choice)
{
case divide:
return myCalc.Divide(x, y);
case multiply:
return myCalc.Multiply(x, y);
}
}

I know in this case they look very similar but the second one is much more
scalable and easier to read. So now if you did a new project and wanted to
use your calculator you need only add the Calculator clas to it and voila
you can use it as above. My example above is VERY basic but better to show
that at the stage you are at. Imagine the methods were much bigger and you
can see the benefit, Easier to read, easier to manage and if you decide to
say add a handler in the Multiply method you just go to the calculator class
and change it there and everywhere the method is called is updated.

Dunno if that helps your understanding of the basics how and why's?
 
R

RobcPettit

Thankyou for your reply. Sorry for delay in answering, Ive been away.
This is great and much appreciated. Ive started practessing with my
code and others. When you start t obreak down the code to smaller
pieces its alot easier to understand whats going on. Thanks for taking
the time.
Regards Robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top