Solved

Preventing Methods stacking in c#

Posted on 2016-10-07
4
22 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
Comment Utility
>>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 78

Expert Comment

by:David Johnson, CD, MVP
Comment Utility
@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)
Comment Utility
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 78

Expert Comment

by:David Johnson, CD, MVP
Comment Utility
Most clear answer to an unclear question
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

11 Experts available now in Live!

Get 1:1 Help Now