Solved

Preventing Methods stacking in c#

Posted on 2016-10-07
4
27 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 AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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