[Tutor] pylint(too-many-nested-blocks)

dn PyTutor at DancesWithMice.info
Tue Nov 30 18:19:11 EST 2021


On 01/12/2021 10.57, Phil wrote:
> Thank you dn for taking the time to write a lengthy informative reply
> and thank you once again Cameron and Mats. I will endeavor to make good
> use of the advice given.

Glad to hear it's helpful.

It is evident that there are plenty of other folk 'here', who read posts
which are not explicitly addressed to them, and who gain in some small,
or maybe, large way. That's one of the functions (hah!) and benefits of
the list.

(also, you can believe that I collect that these are re-worked and
become multi-purpose - ooh, somewhat similar to a Python function. Oh
boy, he's on-a-roll today, as you will see...)


>> Does Python itself see this as an error and stop working, or is it only
>> the code-checker s/w?
> 
> It's not so much an error message, rather a recommendation and one that
> I feel that I should act on.

Right answer!


>> Were we conducting a Code Review, this function would definitely attract
>> criticism. You have already recognised the complexity, but seem somewhat
>> satisfied - on the  grounds that the whole works.
> 
> I think I've only ever produced one application that was truly useful.
> My current project doesn't do anything that others cannot, it's just a
> my-way project and so I could leave it as it is or try to make it look
> more a work of art.

The "Ikea effect", like home-cooking*, is predicated largely on one's
feelings of satisfaction at accomplishment, over-riding other factors,
eg constructing an Ikea cot for baby, but only one side of the cot will
slide up-and-down its rails due to a construction error - but hey, I
built it with my own (?)fair hands!

* we had a rule in my student flat/'frat house' (last century) that "if
the cook eats the food, no-one else is allowed to complain".


OTOH, the non-practical pursuit of perfection leads to one never being
'finished'!


Where is your 'happy medium'?
(rhetorical question)


There are (apparently) employers who will look at your portfolio of work
(should you ever be applying for such a job), and thus it doesn't matter
whether you are repeating others' projects, or making one that is only
useful to yourself...

Then there is: "practice makes perfect"!


>> Here's where things become rather subjective. You (no criticism) seem to
>> have a mathematical view of programming,
> 
> Isn't computer programming a pursuit based on maths? Even so, I suppose
> the result doesn't need to look like a dog's breakfast.

Apologies, I suspected you'd fall into this pattern - and many others
'here' (and amongst your correspondents) will ask/challenge "and what's
wrong with that?".

Programming as a hobby may well be a solitary activity, but few
professionals work in-isolation (says he, who works in a country on the
opposite side of the world from all of his colleagues, whilst dealing
with folk from all corners of the globe*).

Accordingly, the line about code being read by humans! So, if you are
writing for humans, how mathematical do you expect us to be? (free
advice: don't expect it to be 'much')

Things only roll (rapidly) 'down-hill' from there: what about programs
being used by "users"? The 'mental model' that users have of any
computer system is almost certainly, and quite possibly,
radically-different to the way we 'see' it. If the authors want a system
to be used, this needs to be taken into account.

Now you've left Math way-behind, and are slogging through the slough of
"User Experience", ie Psychology and Sociology.


It may not matter in this case, indeed in any 'hobby' scenario. However,
good habits are by-definition "good" - and bad habits are hard to lose
("become vices")! Also, please recall that 'six month you' concept - a
human ex machina!


* gratuitous use of English-expression, with the possible outcome of
irritating mathematicians everywhere.
(Heh, heh, heh - cackle, cackle...)


>> I have been criticised (but exhibit little remorse) for breaking things
>> down into "smaller" units of code than others feel necessary.
> 
> I can see where I could split the function that I posted into two and
> one of those parts could be reused in a yet-to-be-written function. I do
> try to reuse code where I can. I'll need to give a lot more thought to
> how I might reduce the complexity.

As mentioned, "re-use" is not the only justification for
building/abstracting code away and into a function.

I'm currently reviewing "Learn Python Programming, 2nd Edn" by Fabrizio
Romano, Packt, 2018. Chapter 4 is entitled "Functions, the Building
Blocks of Code". After the chapter-intro, he invites us to consider:
"Why use functions?". Here are his answers:-

- they reduce code duplication in a program... [exactly what I was first
taught back in the days of "spaghetti code" and "monolithic programs"]

- they help in splitting a complex task or procedure into smaller
blocks, each of which becomes a function [you may have read something
similar, recently]

- they hide the implementation details from their users [I didn't
introduce the term "Information/Implementation Hiding" before - you
had/still have enough to catch-up on. That said, it is an important
concept, following-on from the previous point]

- they improve traceability [another word related to debugging and testing]

- they improve readability ['nuff said]

(the chapter thereafter details each of these points)

NB I'm not recommending the book (haven't read it (all) yet!) per-se,
simply taking-advantage of the serendipitous opportunity.

It may be worth your while to purchase a (reasonably-recent) text-book,
to give you some guide - what we call a "learning path"!

Incidentally, there's an impression that you've adopted a 'grey gypsy'
life-style. However, if you can gain decent Internet access, perhaps an
online course might confer similar additional/alternate benefit?


>> Now is a good time pick-up the testing tool's feedback: "understandable
>> and maintainable". It's not merely a matter of
>> "readability"/"understandability", but also maintainability - and that
>> of the objectives of the tool: "testability".
> 
> I know that testing tools exist, however, I've always considered their
> use to be beyond the understanding of the neophyte especially this
> amateur programmer.

Well then, you can color me impressed, that you are using any such
'testing tool' at all. Few trainees see the need (a direct consequence
of trainers' employing 'toy examples' which are short and easily(?)
digested - but quite unlike 'real life' - and as such, the "need" for
much beyond the 'mathematical side' of programming is not adequately
established!)

I cheerfully blather-on about pytest and TDD. Some of the irritation is
(that I'm irritating) because folk don't like to have un-learn/re-learn
'stuff'! It's hard work - and harder than learning something for 'the
first time'!


Are you using an IDE such as PyCharm or Codium (the F/LOSS-y alternative
to VS-Code)? If so, there are plenty of "linters" (as others mentioned
earlier) and other learning-tools. I'm keen on Sourcery (British, hence
the spelling) which spots opportunities to show how things could be
better-expressed and/or accomplished more efficiently or more
'pythonically'.

That said, add too many of these tools, and you will quickly feel
overwhelmed by the volume of 'feedback'. Hence YMMV!


>> Earlier the idea of naming a chunk of code was mentioned.
> 
> I find it difficult to come up with meaningful names for functions and
> even variables. I know that 'x' or 'y' is often a poor choice and I
> often leave them in place until I can think of something better. Often I
> don't and so they stay, and of course, I forget what they mean the next
> time I look at the code again.

Welcome to the club!

On another serendipitous note, a few weeks ago I was tracking-down the
source of a quotation (about our work being the invention of 'names'),
and fellow list-members here (or maybe the Python list) were able to
remind me of "The Mental Game of Python" a one-hour talk given by
Raymond Hettinger - one of the "giants" of Python, ie one whose
"shoulders" it is worth "standing upon"
(https://www.youtube.com/watch?v=UANN2Eu6ZnM) Well worth the time - and
maybe you have a YouTube downloader if Internet-reliability is an issue...


Back to the earlier question about IDEs. I'm using PyCharm at the moment
(I use whatever other project-members use/insist I use). PyCharm makes a
point of trying to 'understand' the Python code (which requires a
larger/faster computer than a Notepad-level editor!), and that empowers
functionality such as being able to "refactor" by renaming. Thus, every
use of that name is refactored auto-magically and in one-step (don't
forget to re-run all your pytests afterwards just be sure!). To which
short-cuts, this lazy-boy says, "Oh yeah"!


>> (I'll leave you to research the topics-mentioned that may interest you,
>> as you see fit...)
> 
> I haven't spent anytime on the laptop or the Internet for the fast few
> days and so I haven't had a chance to study the topic of cylomatic
> complexity, however, I do have a Wikipedia page open and I will try to
> gain some understanding of it after I post this e-mail.


Now you have even more...
Isn't life great?
-- 
Regards,
=dn


More information about the Tutor mailing list