Section 1g

Choosing the Right Code


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Implementing Choices

One of the things you may have noticed is that in order to print just one asterisk, you could use printAsterisk() or you could use printAsterisks( 1 );. What is the difference? At this point, mostly style. However, there will come a point in the future where the specifications of the program may require you to use one or the other. The key point here is that as a programmer, you will almost always be able to implement programs or program segments in different ways. This is one of the many opportunities you have to show your creativity when developing software.

Your selection of the proper method or code segment should be based on the following, and in this order: 1) Readability - before anything else, it must be easy to read and understand the code; 2) Correctness - it must implement the appropriate solution to the given problem, or to the provided specifications; and 3) Efficiency - efficiency should always come last because as a programmer, you can never make the code as efficient as your compiler can.

As your compiler creates the program for you from your program code (i.e., the compiling process), it already knows how to make the program as efficient as possible, and it can do things with the code that you cannot do without messing up your program readability or correctness. Bottom line: make your code correct and readable; let the compiler make your program efficient.

You have taken another step into the concept(s) of developing programs using the tools and the strategies that have been developed for you by others. You will find as you continue programming that the programming gets easier thanks to these tools, and you will continue to have opportunities to solve significant problems as you experience this transition.