views:

34

answers:

1

I'm working on a tasklist program to brush up on my C before I take a class. I have two structs, task and tasklist.

Here is the task struct:

typedef struct task
{
  char name[100];
  bool completed; /* bool is described as an enum in an included header file */
  date due_date; /* date is a described as a struct in an included header file */
} task;

I have the following working functions:

  • List item
  • init_tasklist(): initialize tasklist variables
  • add_task(): add task to the tasklist
  • delete_task(): delete the given task from the tasklist
  • print_tasklist(): print out the tasklist similar to a normal todo list with a checkbox, name and due date for each item
  • delete_tasklist(): delete the given tasklist
  • print_task(): print out the details of a given task

My question has to do with which variables to have as parameters to functions and what to return.

I would like to use this tasklist for a cli interface and then change it to a GUI, so I would like to make that an easy switch.

At first, I had add_task() malloc()-ing a new task and returning a pointer to that task, and then delete_tasklist() recursively went through and free()-ed the memory as it deleted the tasklist. I got this approach from my experience with OOP and Java.

What's the best way to design this in C?

Should the add_task() function take a task struct to add to the list or perhaps just the variables that should be in the task and have the add_task() function initialize it?

Should I write a get_task() function that returns the pointer to a task with the given name for functions like print_task() and delete_task(), which would take a task pointer, or should those take char pointers and find the task themselves?

Any guidance on how to design the I/O of these functions would be very helpful.

+2  A: 

Since your data structure is a list of tasks, it's cleaner to call add_task() with a task struct (The tasklist doesn't have to know about the internal representation of the task struct).

It might be handy to have a create_task() method which receives the task fields and returns a task struct filled with these fields.

Logically, create_task() belongs to the task struct, while add_task() belongs to task list. (e.g. If you were writing in OO language, you would have TaskList.AddTask(task), and Task.Create(name, dd, isCompleted))

The get_task() method, is something that you will need to use more than once (as you stated, print_stack(), delete_stack()). Encapsulating this code in a function is obviously better than code duplication. (e.g. if you find a bug in this code, you'll have to fix it once instead of searching everywhere in your program). Also, since this code has a "stand-alone meaning", it's convenient to have a function which performs it.

AmirW
Thanks. Good advice.
mouche