0

I am trying to find out how to correctly use ncurses. I want to create couple of menus, and each menu will lead to another menu for example.

Should I make a template of a menu then add parameter to the function of the generic menu that will choose which of the menu choices/size of the choice array/where it would lead to (using switch-case/if else)

gen_menu(1){...if (choice==1){gen_menu(2);}...}

or

Create a different function for every menu then call the function from each menu to another?

menu1(){...if (choice==1){menu2();}...}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Mike L
  • 1,955
  • 2
  • 16
  • 18
  • You can use extension [library for Menus](http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/menus.html) – Majlik Nov 01 '13 at 10:59

1 Answers1

0

The answer is the ubiquitous "it depends"; both designs can be made to work more or less cleanly. However, in general, I think the second organization is better than the first.

If you use data-driven menus, you will need a moderately elaborate data structure for each menu. If the data comes from files (think I18N/L10N — internationalization/localization), then each menu and each item in each menu will need to be appropriately defined. This can lead to quite a lot of code behind the scenes. For simple menus (no I18N), you probably end up with a moderately elaborate data structure for each menu. You can define those at the file level (static variables), or at the function level. Your gen_menu() design looks like it will run with all the menu structures available to the single function. Your menu1() design allows the menu structures to be defined per menu function.

If the menus are not data driven (for example, if you have a separate switch statement to handle the actions for each menu), then you should have a separate function for each menu. Function size constraints should indicate that, if nothing else. Functions that are many hundreds of lines long are not good.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278