How to make the cleanest code when reporting progr

2019-03-18 19:07发布

I've struggled for the last couple of months to come up with some clean code to report progress to a user. Everything always seems to boil down to:

ReportProgress("Starting Task 1");
doTask1();
ReportProgress("Task 1 is done");

ReportProgress("Starting Task 2");
doTask2();
ReportProgress("Task 2 is done");

//etc... where report progress does some form of output to the user.

The good coder in me screams "There's got to be a cleaner way!" But I'm stumped. Any thoughts?

EDIT :: I'm looking more for information on architectural information as opposed to implementation specific. The code given is very oversimplified.

13条回答
啃猪蹄的小仙女
2楼-- · 2019-03-18 19:41

You could call ReportProgress from inside the doTask methods, that might make it look a little cleaner, instead you would just have:

doTask1();
doTask2();

The reporting would be handled inside those methods.

You could use AOP, but my brain screams KISS!!(Keep It Simple Stupid) in this case. If this is just a simple representation of something more complicated that you are dealing with, AOP could be an option.

查看更多
登录 后发表回答