Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the cleanest code when reporting progress to a user?

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.

like image 362
Gavin Miller Avatar asked Nov 17 '08 22:11

Gavin Miller


People also ask

What makes a code clean?

The following characteristics of clean code are what make it easy to read: The entire application's order of execution is logical and clearly structured. The connection between different parts of the code is quite obvious. The task or role of each class, function, method, and variable is immediately understandable.

What do you consider to be clean code?

Clean code is a reader-focused development style that produces software that's easy to write, read and maintain. Often, you may be tempted to consider your work complete when the application operates as expected. But we're not merely writing code for computer consumption.

What is clean code code that is easy to read and understand?

When we talk about clean code, we talk about a reader-focused development style that produces software that's easy to write, read, and maintain. Clean code is code that is easy to understand and easy to change. The word “clean” has become very trendy nowadays if you look at design, photography, etc.


1 Answers

set up your tasks as an event stream, and have the event-processing 'engine' report progress. Each event instance can have its own name, progress-reporting blurb/template, etc. if you want to go that far

if this is a pattern that occurs often, it is worth the effort for the infrastructure. When you're done, the usable code might look something like:

EventQueue tasks = new EventQueue();
tasks.Add(new TaskEvent(this.doTask1,"Foo-ing the bar"));
tasks.Add(new TaskEvent(this.doTask2,"Bar-ing the foo"));
tasks.Add(new TaskEvent(this.doTask3,"Glitching and whinging"));
...
tasks.Execute(this.ProgressEventHandler);
like image 108
Steven A. Lowe Avatar answered Sep 24 '22 09:09

Steven A. Lowe