ebook img

Good Habits for Great Coding: Improving Programming Skills with Examples in Python PDF

323 Pages·2018·3.71 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Good Habits for Great Coding: Improving Programming Skills with Examples in Python

Good Habits for Great Coding Improving Programming Skills with Examples in Python — Michael Stueben Good Habits for Great Coding Improving Programming Skills with Examples in Python Michael Stueben Good Habits for Great Coding Michael Stueben Falls Church, Virginia, USA ISBN-13 (pbk): 978-1-4842-3458-7 ISBN-13 (electronic): 978-1-4842-3459-4 https://doi.org/10.1007/978-1-4842-3459-4 Library of Congress Control Number: 2018934317 Copyright © 2018 by Michael Stueben This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Todd Green Development Editor: James Markham Coordinating Editor: Jill Balzano Cover designed by eStudioCalamar Cover image designed by Freepik (www.freepik.com) Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected], or visit http://www.apress. com/rights-permissions. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/9781484234587. For more detailed information, please visit http://www.apress.com/source-code. Printed on acid-free paper This book is dedicated to the isolated C.S. teacher and student. Table of Contents About the Author ��������������������������������������������������������������������������������vii About the Technical Reviewer �������������������������������������������������������������ix Acknowledgments �������������������������������������������������������������������������������xi Introduction ���������������������������������������������������������������������������������������xiii Part I: Not Learned in School �������������������������������������������������������1 Chapter 1: A Coding Fantasy ����������������������������������������������������������������3 Advice for Developing Programmers (pain management) ������������������������������������6 Chapter 2: Coding Tricks ��������������������������������������������������������������������15 Chapter 3: Style ����������������������������������������������������������������������������������27 Chapter 4: More Coding Tricks �����������������������������������������������������������35 Part II: Coding Advice ����������������������������������������������������������������53 Chapter 5: Function Design ����������������������������������������������������������������55 Chapter 6: Self-Documenting Code �����������������������������������������������������67 Chapter 7: Step-Wise Refinement �������������������������������������������������������91 Chapter 8: Comments �������������������������������������������������������������������������95 Chapter 9: Stop Coding ���������������������������������������������������������������������105 Chapter 10: Testing ���������������������������������������������������������������������������111 v TTaabbllee ooff CCoonnTTeennTTss Chapter 11: Defensive Programming ������������������������������������������������123 Chapter 12: Refactoring ��������������������������������������������������������������������127 Chapter 13: Write the Tests First (Sometimes) ���������������������������������145 Chapter 14: Expert Advice ����������������������������������������������������������������153 Part III: Perspective �����������������������������������������������������������������179 Chapter 15: A Lesson in Design ��������������������������������������������������������181 How to Approach a Major Computer Science Project ���������������������������������������193 Chapter 16: Beware of OOP ��������������������������������������������������������������197 Chapter 17: The Evolution of a Function �������������������������������������������203 Chapter 18: Do Not Snub Inefficient Algorithms �������������������������������209 Part IV: Walk the Walk �������������������������������������������������������������219 Chapter 19: Problems Worth Solving ������������������������������������������������221 Chapter 20: Problem Solving ������������������������������������������������������������241 The Evolution of a Programmer ������������������������������������������������������������������������252 Chapter 21: Dynamic Programming �������������������������������������������������253 The Most Profound Academic Joke Ever Told ���������������������������������������������������253 A Memoir by Richard Hamming ������������������������������������������������������������������������254 The Wayfarer �����������������������������������������������������������������������������������������������������255 Index �������������������������������������������������������������������������������������������������307 vi About the Author Michael Stueben started teaching Fortran at Fairfax High School in Virginia in 1977. Eventually the high school computer science curriculum changed from Fortran to BASIC, Pascal, C, C++, Java, and finally to Python. In the last five years, Stueben taught artificial intelligence at Thomas Jefferson High School for Science and Technology in Alexandria, VA.  Along the way, he wrote a regular puzzle column for Discover Magazine, published articles in Mathematics Teacher and Mathematics Magazine, published a book on teaching high school mathematics: Twenty Years Before the Blackboard (Mathematical Association of America, 1998). In 2006 he received a Distinguished High School Mathematics Teaching / Edyth May Sliffe Award from the Mathematical Association of America. vii About the Technical Reviewer Michael Thomas has worked in software development for over 20 years as an individual contributor, team lead, program manager, and Vice President of Engineering. Michael has over 10 years experience working with mobile devices. His current focus is in the medical sector using mobile devices to accelerate information transfer between patients and health care providers. ix Acknowledgments Sincere thanks go to the following people: programmer Stephen Drodge for reviewing an earlier draft of this book and for making many useful suggestions; programmer Michael Ames for reviewing an earlier draft of this book; Dr. Stuart Dreyfus (University of California, Berkeley) for his personal thoughts on dynamic programming; Dr. Dana Richards (George Mason University) for mathematical advice on algorithms and puzzles; Dr. James Stanlaw of Illinois State University for discussions of signs, symbols, and semiotics; neurobiologist Paul Cammer (the best teacher I ever met) for years of discussions of effective teaching; hundreds of bright students who accepted my sarcasm and returned it right back to me; my wonderful wife of 40 years, Diane Sandford, for editing several versions of this book; Apress technical reviewer Michael Thomas; Apress editors James Markham, Jill Balzano, and Todd Green for their help in bringing this book into print; and the web site Stack Overflow. Finally, I must thank my two colleagues and master teachers: Dr. Peter Gabor (who reviewed this manuscript and made many suggestions) and Dr. Shane Torbert (who created the A.I. course I taught), both for five years of intense discussions about algorithms. Any mistakes in this book are due to the author, not those who gave me good advice. xi Introduction For the player who wants to get ahead, he has only one piece of advice: get to work. Not with generalities taken from books, but in the struggle with concrete [chess] positions.—Willy Hendriks, Move First, Think Later (New in Chess, 2012), page 20. This book is about improving coding skills and learning how to write readable code. It is written both for teachers and developing programmers. But I must immediately tell you that we learn how to write computer code only by trying to code many challenging problems, reflecting on the experience, and remembering the lessons we learned. Hence, you will find here more than twenty quizzes and problems. Chess coach Willy Hendriks is right: There is no other way. xiii

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.