Friday Finking: Source code organisation

DL Neil PythonList at DancesWithMice.info
Sat Dec 28 17:35:57 EST 2019


Is it helpful to, and thus, do you have a style/convention for ordering 
the methods within each class in your code?


Python's "honking great idea" of namespaces enables us to gather 
functions/methods under a single name-umbrella, thereby avoiding 
name-clashes/name-space 'pollution'. [Oh yeah!]

Thus, if we collect a bunch of module-functions, we refer to them 
collectively by module-name/import-name, eg

	import collected_functions_module as cfm
	...
	cfm.make_it_happen()
	cfm.make_it_better()

Similarly, methods within a class may be accessed either as 
class-methods, or through instances (as required).
[am assuming no code example necessary]

A major difference however, is that if our mythical collection of 
module-functions has an internal-reference, eg b() requires a(), then 
function a() MUST exist, ie be defined, 'before' function b(). Whereas a 
class's methods may be defined in any (complete) sequence.

In the ?good? old days, eg COBOL - or more logically: when source code 
was most-usually viewed as printouts and the prevailing coding-style was 
for largely monolithic program(me)s; we would often number subroutines 
(initially in tens) to better navigate long listings of source-code, and 
more easily locate the subroutine's code from some reference-point. In 
data-terms, we might even create an abbreviation of a record's name and 
use that as an hyphenated-prefix for all of its data-fields' names.

Today, such artifice is unnecessary, given that code-editors (normally) 
offer one-click access from reference to source - even spanning multiple 
Python modules. [another, "oh yeah"!]


So, do you have an orderly method [hah!] for presenting/locating 
class-methods (and module-functions) within your code?

- why bother, the editor does 'the heavy lifting'
- dunders to the fore
- alphanumeric sequence by name
- order of appearance/use in 'mainline code'
- as they sprang to mind during TDD-creation
- most-used first, least-used last
- my code 'at the top', their stuff later...
- names of Monty Python characters by TV appearance date
or,
- some combination of ideas
and,
- how do you vary the above when dependencies intrude?

-- 
Regards,
=dn


More information about the Python-list mailing list