473,395 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

begginer

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

May 24 '06 #1
6 1415
"Ro********@yahoo.co.uk" <Ro********@yahoo.co.uk> wrote:
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

--
http://barrkel.blogspot.com/
May 24 '06 #2
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

May 24 '06 #3
"Ro********@yahoo.co.uk" <Ro********@yahoo.co.uk> wrote:
Thanks Barry for your reply. Plenty of errors for me to read up on.
Thanks for taking the time to point these out.


Uh, they weren't errors as such, just things to keep in mind as you
learn. That's all!

-- Barry

--
http://barrkel.blogspot.com/
May 24 '06 #4
Sorry I meant Areas.
Regards Robert

May 24 '06 #5
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?
<Ro********@yahoo.co.uk> wrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
Sorry I meant Areas.
Regards Robert

May 24 '06 #6
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

May 26 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mansoor | last post by:
Hi, I'm going to start scripting with PHP. I have a good understading of programming languages and have done it for years. Right now I'm seeking an advice on a book that can get me started...
0
by: Alex Glaros | last post by:
I need help on my basic table connection syntaxt using PHP. I call the php from a web browser url_name_goes_here/test.php. The database name is tennis and the user name is tennis. If I type...
4
by: Player | last post by:
During my setup install of python, I came across an option stating... Compile python files to, .pyc I left it unchecked for now, should I of done otherwise?? As I am a begginer to this, I...
0
by: dhillon2004 | last post by:
Hello All, I am beginer in Perl. I am trying a serial port data logging application. Requirement of my application is that it should be capable of receiving and transmitting on same or diffrent...
4
by: Louise Woodward | last post by:
I am looking for some beginner guide for someone to learn asp - tutorials, examples etc Thanks -- Louise Woodward Opal Telecom Application Team Irlam
1
by: ::::x:::: | last post by:
Anyone want to help me with this?? Even when all condition are met, it drops to the else and doesn't look at the if and else if. Ie. when bet_in = 1 and rullnum is between 0 and 5, it doesn't...
2
by: Emmie | last post by:
hi, i wanted to display the value of my textbox into my msgbox. does anyone know the correct code for this? i have tried..... MessageBox.Show("You are about to insert the product:...
2
by: thecamisland | last post by:
Hello, I'm a begginer. Is this going to work? Dim fm As FirstMenu Class TransHeadInfo Public Function Validate() As Boolean ... fm.Show() ...
13
by: deshix | last post by:
Hi everyone, can you recommend me the best way to learn the PHP? I'm really begginer...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.