Teaching Programming Style with Ugly Code
Total Page:16
File Type:pdf, Size:1020Kb
2013 Proceedings of the Information Systems Educators Conference ISSN: 2167-1435 San Antonio, Texas, USA v30 n2537 _________________________________________________ Teaching Programming Style with Ugly Code Kirby McMaster [email protected] Computer Science, Lake Forest College Lake Forest, IL 60045, USA Samuel Sambasivam [email protected] Computer Science, Azusa Pacific University Azusa, CA 91702, USA Stuart Wolthuis [email protected] Computer & Information Sciences, BYU-Hawaii Laie, HI 96762, USA Abstract In this paper, we describe how good programming style can contribute to better software. Good style makes source code easier to read and understand, which can reduce errors and simplify maintenance. We discuss several popular style practices. We then introduce a software product we have written called UglyCode, which allows instructors to demonstrate various programming style options. Specific examples that illustrate the use of UglyCode follow. With UglyCode, programming style can be visualized interactively, showing the immediate effect of style choices on the readability of code. Keywords: programming style, layout, ugly code, algorithm, Java. 1. INTRODUCTION As students gain programming experience, they become more concerned with the design and Teaching computing students how to become implementation of algorithms (Dijkstra, 1971). professional programmers involves substantially Three desirable characteristics of algorithms more than helping them learn the syntax of a receive early emphasis: (1) correctness, (2) programming language. In a beginning performance, and (3) efficiency. We describe programming class, the focus is on teaching a these characteristics in the context of the computer how to solve a problem (Shustek, courses that spend substantial class time on 2009). This involves a description of a higher- them: Data Structures, Algorithms, and level programming language (e.g. C++, Java, Software Engineering. We also discuss the Visual Basic, or Python), and practice organizing concepts of modularity and maintainability, the language statements into a working which have become increasingly important in program. At this initial stage, students write software development as systems have grown in code that the computer can understand. size and complexity. _________________________________________________ ©2013 EDSIG (Education Special Interest Group of the AITP) Page 1 www.aitp-edsig.org 2013 Proceedings of the Information Systems Educators Conference ISSN: 2167-1435 San Antonio, Texas, USA v30 n2537 _________________________________________________ Eventually, to become a professional memory (Silberschatz, et. al., 2012). There is a programmer, a student must be able to develop fundamental trade-off between performance and systems that satisfy additional objectives, such efficiency. A process can be given dedicated as dependability, reliability, safety, security, CPU time and "unlimited" memory to improve usability, and portability. performance, but at the expense of efficiency. Correctness In a system where multiple programs run Programmers continually strive to write concurrently, primary responsibility for programs that contain no errors. There are managing these tradeoffs is handled by the several aspects to program correctness. The operating system. CPU scheduling algorithms aspect that receives the most attention from interleave CPU time with I/O activities. Virtual mathematically-trained computing professionals memory management schemes allow for is logical correctness. The primary method used dynamic reassignment of memory for processes to determine logical correctness is proofs. A during execution. The goal is to balance software development group in Australia (Klein, throughput and response time measures for 2009; Klein, et al, 2009) recently announced performance. that they have proven their microkernel operating system code to be correct. (It took A programmer can influence performance and several years to complete the proof.) Textbooks efficiency through the choice of algorithms. For on algorithms demonstrate proofs for many example, the speed of a sorting algorithm can common algorithms (Cormen, et al, 2009; depend on the type of data being sorted, along Sedgwick & Wayne, 2011). with the amount of memory available. Merge sorts are faster when additional memory can be When you prove that an algorithm is correct, the allocated to hold intermediate results (Lafore, proof does not guarantee that the source code 2003). will be without errors. Proofs also do not ensure that the program meets customer requirements, Often, performance is affected most by a since requirements are often unstated, bottleneck in the system. If the slowest part inaccurate, or changeable. involves disk storage, then memory caching for disk reads can greatly improve performance. A proof is based on a mathematical model, Sometimes a small section of code can slow which is an abstraction of a real world situation. down the system, if performed repeatedly. If the model does not accurately represent the Rewriting the code in a faster language (e.g. C real world, then the proof is irrelevant. In or assembly language), using multiple threads, addition, the mathematical model never or finding a better algorithm can improve completely matches the version of the model performance. presented to the computer. For example, the math model may assume that variables are Modularity and Maintainability continuous, whereas all numerical values in a Programmers are introduced to modular code in computer are discrete. their first programming class (Lewis & Loftus, 2011; Liang, 2012). The modules, in this case, An alternative approach to verifying program are functions and procedures. Not all of the correctness is based on empirical results. This benefits of modularity are grasped initially. In approach depends on thorough testing of the object-oriented programming, the design and software as it executes. A well-designed test use of classes, objects, and encapsulation plan consists of a broad range of tests, both for becomes a valued way to manage complexity in individual parts of the system and for the larger programs. system as a whole (Somerville, 2011). In the Software Engineering course sequence, students Maintainability is a less understood characteristic should be required to construct test plans for of software. It depends on a variety of methods their software development projects before the that make code easier to correct and modify. source code is written. Most software is not maintained by the original developer. Developers move on, but code will Performance vs. Efficiency often last for years. Readability is essential for Programmers are encouraged to write programs continual maintenance. The Department of that perform well and make efficient use of Defense estimates that 60-80% of software life computer resources, especially the CPU and cycle costs are for maintenance. _________________________________________________ ©2013 EDSIG (Education Special Interest Group of the AITP) Page 2 www.aitp-edsig.org 2013 Proceedings of the Information Systems Educators Conference ISSN: 2167-1435 San Antonio, Texas, USA v30 n2537 _________________________________________________ Modular code is easier to maintain, but other But which programming style is best? Expert practices can also improve maintenance efforts. programmers almost always have their own Programming courses spend little time directly preferred style for writing code. A conversation on maintainability. More detailed presentations about which style is best often takes a religious appear in Software Engineering books tone. One point of consensus is that "the details (McConnell, 2004; Somerville, 2011). Topics of a specific method of structuring a program relevant to maintenance include agile are much less important than the fact that the development, configuration management, program is structured consistently" (McConnell, version control, and refactoring. 2004). What About Programming Style? In The Elements of Programming Style, Programming style involves ways that a Kernighan and Plauger (1978) describe many programmer can organize and present code to style choices for programmers. We present a make it more understandable to other partial list of their style topics. Our discussion of programmers. programming style is organized into two groups, layout and content. "The smaller part of the job of programming is writing a program so that the computer can read Program Layout it; the larger part is writing it so that other Program layout involves techniques to rearrange humans can read it." (McConnell, 2004). source code to make it more readable. No content is added to the code, other than This includes a variety of code layout, changes in spacing. Several layout methods are formatting, and content enhancing techniques, described in the following paragraphs. such as the use of white space and variable naming conventions. Blank lines: In a written report, blank lines are added between paragraphs and sections to make By making code more understandable, style the report easier to read. Similarly, blank lines improvements contribute to other desirable can be added to source code between functions program features. For example, readable code and to bind together lines of code that perform is more likely to be correct when initially written, some computing activity (e.g. input). The effect and it is easier to modify when changes are for the code is the same as for the report. It required.