Solved

Preventing Methods stacking in c#

Posted on 2016-10-07
4
29 Views
Last Modified: 2016-10-27
Dear Sir/Madam,

I need to find out how best I can avoid methods stacking in a C# windows application like this?
Its a project I have already done to some extent. I just need to make improvements to it.

What is the best approach I can apply to avoid this situation?
I have a lot of controls added to the forms as well.

A();
B();
C();
D();
E();
F();

Kind Regards,
Indunil Sanjeewa
0
Comment
  • 2
4 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41834709
>>What is the best approach I can apply to avoid this situation?

Well you could put all the code into one method so it will just be
A();
but I would not advise that in terms of writing nicely structured code.  Maybe you aren't being clear but I do not see anything wrong in principle with:
A();
B();
C();
D();
E();
F();
0
 
LVL 80

Expert Comment

by:David Johnson, CD, MVP
ID: 41834717
@AndyAinscow  You're not the only one that is not clear on the askers objective
0
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points (awarded by participants)
ID: 41834943
Yeah, it's not clear to me as well.

However, just some thoughts about methods. Normally it's a good idea that one function, or one method does one thing only. And a function is pretty small in code, say 10 - 20 lines or even shorter. Still, I like the statement like :
...use common sense, stick to small function sizes in most instances but don't be dogmatic about it if you have a genuinely good reason to make an unusually big function...

Just to repeat, don't be dogmatic. For example, a button click handler (just another method)  may call a number of other methods and make a number of jobs, more than one. For example, when you click a Save button you may want:

 - to validate an object you are saving
 - display a message saying what's wrong if an object is not valid
 - save the object into database
 - give a user notification that save was successful
 - log any errors and outcomes
 - close or open any other forms,
 - etc.
Indeed the above jobs are to be implemented in a separate method so that the button click handler invokes these methods and does not hold all this code.

Just think what you want to achieve and - use common sense.
1
 
LVL 80

Expert Comment

by:David Johnson, CD, MVP
ID: 41861861
Most clear answer to an unclear question
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question