Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
From single block to structure and object oriented programming

If you're writing a very short, one-off program, you're likely to mix your calcualtion details in with your data input / output and data format handling - "quick and easy" - for example, code (Python 2.7 and 3.x) [here].

But as your application grows, and the code grows, a single block becomes less managable, you lack "test points" to test it in sections, the detailed data algorithms are in-line with the managemnt which makes it hard to look after if each section's got a different lead programmer, and the algorithms can't be shared between programs unless you copy and paste - and if you copy and paste, your duplicating any program bugs you have, and doubling up future work when you upgrade your algorithms too.

The first solution I'm offering you today is to move your algorithms out into named blocks of code (they'll be called "subroutines", "functions", "procedures", "macros", "commands" or "methods" depending on the language you're using - for my short example, the code becomes a little longer but it also becomes much more managable - see [here].

In that first solution, the data remains in the main program and is passed to each of the functions as required to be manipuated. So there remains quite a data management task in your main application as it grows. My second (and improved) solution is to pass in the data to a single named block of code intially, then pass around just a handle to each of the functions that access that data. Not only does this save you holding and managing the data in the main program, but it also provides a robustness against errors in that it will only let you perform sensible, predefined tasks on the data. There's an example of this technique [here].

To keep the examples neat and in a single window, I've kept the detailed logic in my above examples in the same file, but moved it above the main control code ... in a live application, you would use separate files for the "controller" and the "model" - in the Python example using the import or from commands in the main code to load the detailed logic when required.

Example written during today's Python course.

P.S.
The default way - "Single block coding"
The first suggestion - "Struuctured coding"
The second suggestion - "Object Oriented Programming"
(written 2015-12-02, updated 2015-12-03)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q906 - Object Orientation and General technical topics - Object Orientation: Individual Objects
  [4650] Why populate object with values as you construct them? - (2016-02-18)
  [4448] What is the difference between a function and a method? - (2015-03-04)
  [4021] Spike solution, refactored and reusable, Python - Example - (2013-02-28)
  [3721] Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks - (2012-05-06)
  [3436] Moving from scripting to Object Orientation in Python - (2011-09-13)
  [2651] Calculation within objects - early, last minute, or cached? - (2010-02-26)
  [2393] A first demonstration of OO, including polymorphism - (2009-09-04)
  [2173] Basic OO principles - (2009-05-11)
  [2171] Cleaning up redundant objects - (2009-05-11)
  [1925] Introduction to Object Oriented Programming - (2008-12-06)
  [1864] Object Oriented Perl - First Steps - (2008-11-01)
  [1543] Learning Object Oriented Principles (and perhaps Java) - (2008-02-17)
  [507] Introduction to Object Oriented Programming - (2005-11-27)
  [227] Bellringing and Programming and Objects and Perl - (2005-02-25)

Y106 - Object Oriented Python
  [4721] When to check an object type - Python isinstance example - (2016-11-03)
  [4129] Simple OO demonstration in C++, comparison to Python - (2013-07-01)
  [4028] Really Simple Class and Inheritance example in Python - (2013-03-04)
  [3947] this or self - what are they, and what is the difference? (Python) - (2012-12-08)
  [3878] From Structured to Object Oriented Programming. - (2012-10-02)
  [3673] Object oriented or structured - a comparison in Python. Also writing clean regular expressions - (2012-03-26)
  [3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
  [3085] Object Oriented Programming for Structured Programmers - conversion training - (2010-12-14)
  [2604] Tips for writing a test program (Ruby / Python / Java) - (2010-01-29)
  [2169] When should I use OO techniques? - (2009-05-11)
  [2017] Python - a truly dynamic language - (2009-01-30)
  [1348] Screw it or Glue it? Access to Object variables - a warning - (2007-09-12)
  [1306] Python class rattling around - (2007-08-16)
  [900] Python - function v method - (2006-10-20)
  [834] Python makes University Challenge - (2006-08-15)
  [477] Class, static and unbound variables - (2005-10-25)


Back to
Progress on moving from Python 2 to Python 3 - training for both versions
Previous and next
or
Horse's mouth home
Forward to
A comparison of the public transport alternatives to the Royal United Hospital, Bath - from Melksham.
Some other Articles
Python formatting update - including named completions
XML handling in Python - a new teaching example using etree
Command line parameter handling in Python via the argparse module
A comparison of the public transport alternatives to the Royal United Hospital, Bath - from Melksham.
From single block to structure and object oriented programming
Progress on moving from Python 2 to Python 3 - training for both versions
Principles or a GUI and their practical application using wxPthon
What teach you in a week stays with you for a decade
shell - bash. Writing conditional tests and statements - the options available
Extending your bash shell with aliases, functions and extra commands
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4591_Fro ... mming.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat