Solved

Preventing Methods stacking in c#

Posted on 2016-10-07
4
32 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 82

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 30

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 82

Expert Comment

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

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

617 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