[Tutor] Review: Good Habits for Great Coding

DL Neil PyTutor at DancesWithMice.info
Thu Jun 25 21:50:26 EDT 2020


Good Habits for Great Coding : Improving Programming Skills with 
Examples in Python,
Michael Stueben,
Apress, 2018


My disappointment with this book is largely a consequence of my own 
expectations. The title raised hopes that it would improve my own coding 
efforts, or that it would offer teaching techniques which might improve 
my approach to training in IT.

The first indication of a poor 'fit' was its dedication "to the isolated 
CS teacher and student". Sadly, many of the errors or examples of poor 
teaching would not have occurred, had the author been less 'isolated' 
and spent time interacting with the world-wide Python community!

The Introduction reveals the author to be a victim of his environment. 
School math and 'toy examples' lead to the conclusion that 
short/algebra-like variable-identification is "efficient" and that 
'pretty boxes' around module docstrings add value and improve 'looks'. 
Yet, the later chapter on "Self-Documenting Code" reverses such advice, 
instead preferring descriptively-meaningful names.

Whilst it is not necessary for all code to adhere to PEP-8 standards, it 
is difficult to imagine why one would deliberately teach differently, eg 
re-defining indentation. Generally, C- and Java-esque techniques do not 
transfer to Python. For example, camel-case for variable or function 
names is frowned-upon, but such, and the likes of for...in range(...) 
constructs, appear with alarming frequency. In eschewing the generic 
'in-joke' function names such as "foo", "bar", and "baz" (which few 
would ever use in practice anyway), the author instead promotes the 
no-less meaningful "doIt()" for general-purposes, purportedly following 
a verb-object name-form recommendation.

Similarly, but contrary to the usual EAFP/'we're all adults' ethos of 
the Python world; in the book's programmer-as-user example exercises 
there is emphasis on "testing for any pre-condition and boundary 
condition". In that environment, using assert to 'prove' an input or a 
result might be acceptable, but in 'the real world' risking such a 
hard-stop (decorated by a user-alarming error message and trace-back) 
would not. Conversely, in the "Expert Advice" chapter we read that 
"Robust code...heals itself...crashes gracefully".

A single, solitary use of the pythonic try...except structure can be 
recalled, but those keywords do not appear in the book's index. The 
'batteries included' advantages of the Python Standard Library don't 
receive coverage. Indeed the author details 'utility code' to 
debug-print a matrix structure, whereas most of us would reach for the 
pprint library. Ironically, recalling the development of such code, 
gives occasion to introduce the concept of "YAGNI".

Stueben concludes (rightly) that the problem with students not 
appreciating the need for style or readability is implicit within the 
traditional 'work by yourself, and for yourself' methods of schooling 
(this is changing); and how, to demand apparently-abstract adherence to 
standards or convention would create an apposite relationship between 
teacher and student. No doubt! Confirming this admission of defeat, the 
chapter on "Style" is sandwiched between two entitled, "Coding Tricks" 
and "More Coding Tricks".

However, professionals would say that coding-style cannot be ignored. 
Accordingly most trainers attempt to model 'best-practice' in our 
training materials. Sadly, the reality is that a good docstring for a 
toy-example may be longer than the actual code. Thus, such ideals may 
actually interfere with the learning process. Exigencies apply! 
Therefore, one of the book's (and the class's) missed-opportunities is 
that professionals (and students) may only ever convince themselves of 
such a need, after 'suffering' through reading and maintaining/improving 
others' code - at which point we 'discover' the struggles of 
wading-through a 'tangled mess'. (Perhaps a message to all of us?)

There is a useful chapter on Testing, followed by four weakly-justified 
pages on Defensive Programming. However, the TDD (and others) approach 
of separating code from test modules is not mentioned. Debug-prints are 
favored over debuggers. Scaffolding "which will be removed eventually" 
is recommended, despite the virtue of test-runners remaining available 
for future use.

There is a nod towards Numerical Analysis in coverage of the 'dangers' 
inherent in floating-point computation, which many would be wise to 
absorb! Unfortunately, this content will not encourage or extend your 
experience into Python's Decimal library. The math-bias shows in the 
strength of algorithmic coverage, and the dearth of data considerations. 
Even Data Scientists preface their analyses with data-collection, and 
data-cleaning processes! Within an environment concentrating on 
implementing algorithms with simple (and tailored) data formats, such a 
view may be justifiable.

Object-Oriented Programming receives short-shrift, which is likely 
appropriate, given the examples' subject-matter, and the lack of (need 
for, teaching-approach using) re-use. Sadly, in a discussion of the 
'Traveling Salesman Problem' he recommends a complex structure of 
mutable lists for x,y co-ordinates to which id-s (pointers) are added. 
Whereas, a custom class would simplify understanding and better relieve 
the loss of "control" (of the code-solution) he describes. Some will 
support the chapter "Beware of OOP", whilst others recoil in horror. 
Again, a divergence between ComSc and industry, and a predilection for 
edge-cases seem to justify the author's views. The many advantages 
built-in to popular Python IDEs are also a missed opportunity. Likewise, 
GUI and web interfaces are not discussed. That said, time is devoted to 
building speed-tests as means of refinement and an aid to refactoring.

To be fair, it is not easy to convince high school students, or for that 
matter many neophyte programmers, of the virtues of 'industry 
standards', eg avoiding 'clever code' to enhance readability. Which 
implies that the title of the book is too general and requires a more 
descriptive (and accurate) sub-title, eg "for High School Computing". 
Further, to be true to the comparisons drawn in "Expert Advice", the 
book's sub-title should replace its current "Programming Skills" with 
the term "Computer Science", thus justifying the author's view that 
"professional software developers work in a significantly different 
world from the C.S. student. ...team...legacy code...end 
users...convenient interfaces...consistency" contrasting radically with 
"code up algorithms that will be run one time in front of the teacher".

This book's title is a publisher's marketing-masterpiece of community 
appeal. However, the content fails to deliver on the promise. If you 
seek advice about professional practice, or to improve Python skills, 
look elsewhere. If you are a High School student considering IT as a 
career, look elsewhere. However, if you are a math-based, ComSc student 
(including, perhaps, first-year University Under-Graduates) and you're 
studying algorithms, this book includes much food-for-thought.


The reviewer's first degree included a ComSc major. He has worked in IT 
and business for decades, and is an international consultant and 
vocational trainer.

-- 
Regards,
=dn


More information about the Tutor mailing list