Solved

Preventing Methods stacking in c#

Posted on 2016-10-07
4
26 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 79

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 79

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now