A compiled program may contain the majority of its code in a single program file. More common is to have multiple files, each with compiled program codes that are referenced by one or more primary programs. Do not be swayed by the terminology. A program may have some of its behavior in the main program and some of it kept in other places so that external code can be shared. Well, you have the ability to split of your code in the same way so you don’t end up doing a lot of copy and paste.
Error of Copy and Paste
Copying and Pasting codes from program to program leads to errors and slows you down. Mentally, you might commit yourself to fixing all the duplicated codes in the same way but you miss a spot. Or, you forget you made a change to one of those duplicated codes, it was so slight a change that when you paste the new code in, things get really screwed up. Keeping shared codes in centralized files referring to them by name is a better way to go.
Such files go by several names. Modules is one term used but the most common term is library. A code library usually represents a body of code that contains commonly used functions that solve a certain class of problems. A program file has a single spot that the operating system uses to kick-start the program. That is the only kind of code you cannot (normally) put in a code library. Other than that kick-start part, all other code can and usually should be subdivided into code libraries and/or modules.
Code libraries allows multiple programs written by you to all use the same code. Maybe there is a certain way you read files off a hard drive and you find it useful to have all your programs read files that particular way. A code library will contain that specific sequence accessible by a name for that sequence. Your program then accesses that sequence by that name. One term for these names is API. An API is what you create anytime you give a sequence a name. You can reuse this sequence multiple times by having your programs access it by a specific name you have given it.
The best part of defining a code library is you gain the ability to evolve better sequences to solve problems. You can update a code library and depending on how you update it, other programs can receive higher quality functionality automatically. What you have done is organize your code and provided a means for combining solutions to problems to solve other problems.
When you are first learning to program, you may decide to create your own libraries for many things. That is a good way to learn and understand the concept of code libraries. Later on, you want to transition to libraries provided by others. This is an approach that will allow you to integrate capabilities into your program that may be more substantial of higher quality than you have time to pursue yourself. As a result, you improve the way your program works.
Avoid copy and paste. Subdivide code into libraries and/or modules. Build on third-party libraries when possible.
By this point, you have been introduced to guidance that assists you with learning to make programs. Whenever you have ideas that can be acceptably expressed through a basic program representation, then the guidance so far will prove useful for an indefinite period. Other forms of representation exist and they are discussed separately in the next topic on interactive programs.