[Tutor] Anti-Patterns in Python Programming

Alan Gauld alan.gauld at btinternet.com
Sat Jul 12 20:21:59 CEST 2014


On 12/07/14 17:43, Deb Wyatt wrote:
>
>>> So much has been invented since my dos programming days and it is
>>> overwhelming,
>>
>> Actually very little has been *invented* since your DOS days.
>> Almost everything we do today was already around back then.
>>
> I knew someone was going to say that.

:-)


> Maybe it's the jargon that has been invented?

In some cases yes, but mostly it was there all
along just hideen in obscurity. For example all the GUI stuff we take 
for granted really came together at Xerox Park in the 1970's but didn't 
become well known till Steve Jobs paid a visit and promptly built the 
Apple Mac (after initially flopping with the Lisa!) around 1984.

> Some questions I have at the moment:
>
> 1.  What is functional programming?
> 2.  What is procedural programming?
> 3.  What are data patterns?
> 4.  What are regular expression?

As Mark has said I always go to Wikipedia for these kinds of answers.
It is a great resource for all things technical. I'll try to give
the newbies answers too though...

 > 1.  What is functional programming?
 > 2.  What is procedural programming?

These are both programming paradigms.

Most programmers learn procedural programming first - also known as 
imperative programming. This is where you tell the computer how to solve 
the problem using the familiar sequences, conditionals and loops. You 
then package these constructs into procedures (or functions).
You have data floating around loose and your procedures operate
on that data changing its state(values) as it goes along.
This means that in a procedural program two calls to the same
function can return different results even if they have the
same inputs:

 >>> state = True
 >>> def f(n):
...   global state
...   state = not state
...   if state and (n % 2):  # n is odd
...      return n ** 2
...   else: return n
...
 >>> f(3)
3
 >>> f(3)
9

Functional programming is more like pure math. It is built on functions 
that return values and the values should not depend on external state.
They always return the same result for the same input. Functional 
programming also attempts (with mixed results) to produce programs that 
look more like problem specifications than solution descriptions. So the 
functions often operate on data sets rather than individuial items 
(avoiding loops) and tend to use recursion a lot.
( I have a topic on FP in my tutor that explains more and uses
Python to illustrate...)

There are also Object Oriented Programming and Logic Programming paradigms.

 > 3.  What are data patterns?

This is a bit more open ended since there are several possible answers 
depending on context. At one level it can refer to data modelling 
patterns that crop up again and again. For example instead of modelling 
assigned to each, model it as a person and a role. All the people go in 
Person, all the jobs in role. Then you assign one or more roles to each 
person. When a person gets promoted you don't have to move the data 
between tables you just change the relationships slightly. That is one 
common data pattern in the world of modelling. But it depends on the 
context where you came across the term

 > 4.  What are regular expression?

A form of text pattern. They are like wildcards for filenames but more 
powerful. They are used for finding substrings in text. I have another 
topic in my tutorial on regular expressions...
Nowadays most languages support regular expression searches, including 
most SQL dialects on databases.

> You all take this understanding for granted, but it's not common
> knowledge for the rest of the world.

I sympathise.
One of the primary aims of my tutorial is to take a complete beginner to 
the point where they can understand these more technical tutorials. A 
lot of time has gone into explaining general computing terminology as 
well as showing how to use it. As far as possible every jargon term is 
explained when first introduced.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list