MadSci Network: Computer Science |
What is the structure(d) way to write a big program? Well, this question has spawned "religious" wars ( spirited arguments of belief or prejudice rather than objectively provable fact ). While there are some things you can learn from a book, programming requires more hands-on practice than formal teaching. Many rules-of-thumb have been found to be useful aids in programming. One important rule-of-thumb is to minimize interelationships between different sections of code. This allows one to pretend that sections of code are 'black boxes' - each one doesn't care how the others work. If one has to be rewritten for some reason ( happens fairly often ), it doesn't really bother anything else. Real life involves compromises of this principle - and the price is paid - to change one thing, you have to check, and possibly change, several others. Let's say a function is written so that it's output is strictly dependant on its explicit parameters. No global variables are referenced in the function. You can rest easy that changes in unrelated sections of the program should not bother the function. You may also cut and paste that function into another program with very little trouble. If that function references any global variables, then it will be more effort to re-use the function in another program. There is also the considerably increased possibility that another 'unrelated' section of the program will interact negatively with this function. Accordingly, global variables should be used sparingly. My favorite way to use them is for a few pieces of information that the whole program needs to be able to read, but only ONE part of the program writes (preferably only on program initialization). One of my personal rules-of-thumb is that if you will deal with three or more of some data structure, then it is time to write a 'class' for it. A class is a combination of data and functions to operate on that data. Everything outside that class needs to use a 'public' set of interface functions that you write. Yes, it takes a bit more time to do things this way. It helps keep a control on the way parts of a program are interrelated though. Languages that don't directly support classes can still benefit from this technique - it just requires more programmer discipline. Another rule-of-thumb is to prefer a lot of relatively simple functions to a very few, really complex functions. Smaller functions are more flexible and can be reused effectively (even within the same program ). If a function is much longer than about 120 lines of substantive code, than you really need to at least examine the possibility of breaking it into smaller pieces. Ideally, you should be able to write one ordinary sentence to describe what each function does. At the head of each function, put a comment containing a brief overall description of the function. This helps later when you have to maintain something you haven't thought about in months. There are all sort's of books about how to structure a program. While you might take a look and experiment with some of them, take it all with a grain of salt. Practice is the real teacher here.
Try the links in the MadSci Library for more information on Computer Science.