Strictly Private: Abstraction and the importance of ‘privacy’

by Paul Curzon, Queen Mary University of London

Celebrities want fame but they also want privacy. They want some things they do known to the public, but not everything. It turns out that privacy is important when writing good code too, and in object-oriented programming the personal body guards that provide it are built-in.

StrictlyLeaderboard

In the last blog post we explored the basics of object-oriented programming by looking at how to make game show judges for the Strictly Come Dancing of the future. Object-oriented programming is centred on the idea of abstraction: hiding information to make a problem (here coding) easier. In particular, it is built around the concept of an abstract data type. You create new types – collections of related values – for your program to use,  but hide their complicated inner details, their representation. We will illustrate the ideas by continuing our Strictly theme and look at how we might keep track of contestants’ scores using objects, and in doing so find out what abstract data types are, and what they give us.

We are going to think of Strictly Come Dancing contestants as objects just as we did with judges. We will create a new type of Contestant for our programs to use. We will then be able to create new Contestants, and record and change their details. However, we will do it in a special way, making those inner details private and only accessible by the privileged parts of our code (a bit like the way real celebrities’ lives are protected from the masses).

Properties of a Strictly contestant

AlexandraGorkaWe need to decide how to represent a contestant. First we ask what properties – what attributes – we need to record about contestants. How will we represent them? In Strictly contestants always come in pairs: a celebrity and a professional, so we will group them together and treat the contestant as the pair. Each person in the pair has a name. They are also given jointly, after they dance, a score from the judges. To keep things simple we will ignore the audience vote. We will also assume they just dance once for each show. Their score is just made up of four individual judge’s scores, but it is the total that matters and appears in the leader board. So that is what we will record. The properties of a contestant can be defined (in a simple pseudocode rather than a real language) as:

DESCRIPTION OF A Contestant:
    CelebrityName: String
    ProfessionalName: String
    TotalScore: integer

CelebrityName, ProfessionalName and TotalScore are the instance variables of the class we are defining. They hold the property values. This says a contestant is defined by the name of the celebrity, the name of their paired professional dancer and their score, which is an integer.

Keep it private

Now we do something that seems a bit perverse at first, but is the key to the power of objects. We mark all those instance variables as being private! They cannot be seen outside the Contestant class we are defining. The representation we just carefully created will be hidden.

DESCRIPTION OF A Contestant:
    PRIVATE CelebrityName: String
    PRIVATE ProfessionalName: String
    PRIVATE TotalScore: integer

Privacy is important to the talent of shows like this! Of course, this form of privacy is nothing to do with the stars’ private lives. Privacy in OOP is all about making the program easier to change in the future, as we will see. What marking them as private specifically means is that the rest of the program (outside the description of a Contestant we are defining) can not see any of these attributes of a contestant directly. We have set up a curtain around the representation of the contestants. We will only allow it to be accessed in carefully controlled ways via methods. In real life you can ask a star for an autograph or a selfie but they won’t go down the pub with you. Their behaviour is controlled. It is as though they come with personal bodyguards that keep all unwelcome advances away – very important in show business it seems for stars, but also for code. Access is strictly controlled to variables marked as PRIVATE. In practice, in compiled languages as least, it is the compiler that does all the checking – it ensures nothing marked PRIVATE is mentioned anywhere it shouldn’t be in the code.

Accessing marks

How then do we access the names and score? We are going to code a set of behaviours – a set of methods – whose job will be to allow those properties to be read and modified, but only in the controlled ways they define. These methods take the place of bodyguards for the code.

For example, we do not want the total score to be set arbitrarily. It MUST always be just the result of adding the four separate judges scores together. Therefore we will write a single method to do this and then enforce that it is used. It will be given the four different judges scores and add them up. No other way will be given to change the total.

So the behaviour we must define for a contestant pair gaining a score is that given four marks, which we will refer to as judge1Mark, judge2Mark, and so on, their sum is recorded. We define a method to do this as part of the description of a Contestant.

TO SetContestantScore:
    GIVEN judge1Mark AND judge2Mark AND judge3Mark AND judge4Mark:
        TotalScore = judge1Mark + judge2Mark + judge3Mark + judge4Mark

Whereas the actual property, TotalScore is private and so cannot be used directly in the rest of the program (just here), the method SetContestantScore is public. It is visible and can be used elsewhere. By setting this method as the only way TotalScore is accessed, we ensure it can ONLY by set by combining four judge’s marks together correctly. This protects the program from bugs caused by programmers getting confused or misunderstanding things elsewhere in the code.

We don’t want to just be able to set the score, we also need a way to access that score too, so it can be displayed on the leader board, for example. We again give a method to access it. By providing this method, we show we are happy for it to be accessed.

TO GetContestantScore:
         RETURN TotalScore

It just accesses the score from the Contestant object and returns it. This seems a little odd at first. Why use a method to do that? As we will see, accessing the values of a type through methods like this makes the program easier to change with less risk of mistakes being introduced. It is a good thing to do even in simple situations like this, just in case we later want to change them.

What’s your name?

What about the contestant’s names? We do the same sort of thing. We want them to be set only when the pairs are put together at the start (in real life with cries of “You were the partner I always wanted”). After being set they should not be changed, so we won’t give a way to change them. We do not want a bug elsewhere to be able to mess with their names so we make it impossible to do. We can build the setting of names in to a method called CreateContestant, say.

TO CreateContestant GIVEN celeb AND pro:
    CelebrityName = celeb
    ProfessionalName = pro
    TotalScore = 0;

We store a given celebrity name (celeb) and a given Pro’s name (pro) in to the corresponding instance variables of the object. We also set the TotalScore Instance variable to 0 as at the point they are created they have no score.

Methods like this that, when executed, create and initialise an actual object of a given class are called constructors. In many languages constructors are by convention called the same name as the type and automatically called as part of creating a new object, so we will do the same and change the name. Actually what they do instead of the above is something more like:

TO CREATE A NEW Contestant GIVEN celeb AND pro: 
    CREATE A NEW OBJECT WITH SPACE FOR THE ATTRIBUTES NAMED ABOVE
    THEN DO THE FOLLOWING
        CelebrityName = celeb
        ProfessionalName = pro
        TotalScore = 0;

We will use this version, called Contestant, that spells out that it creates an object of type Contestant first before setting the values, just to emphasise that constructors are a more complicated kind of method.

Let’s suppose once formed we ALWAYS want to refer to the celebrity-professional couple by combining the names, as in “Alexandra and Gorka”, with the celebrity first. We can enforce this by making the method that returns the name always return it in that form. Again if that is the only method we write, any code using our class can’t get it wrong. We glue the two names together with “ and “ between to make the form as above.

TO GetContestantName:
    RETURN (CelebrityName + “ and “ + ProfessionalName)

Abstract Data Types

At the start we talked about an abstract data type. Where does that come in? An abstract data type is a type that is defined by the things that can be done on values of that type. We have just defined what it means to be a contestant in exactly that way. We have specified that a value – an object – of type Contestant (as seen by the rest of the program) has four behaviours, and only four behaviours. You can

  • Create a contestant by providing the name of the celebrity and of the professional
  • Get the name of the contestant as a couple
  • Set a Contestant’s score as the total of four judge’s scores,
  • Get a Contestant’s score

You can do nothing else with a Contestant as all its inner details are hidden and inaccessible to any code using the class.

Our description above is a description of an abstract data type because it doesn’t say anything about how the names and scores are stored, and because there is nothing else you can do to manipulate the values other than those four actions.

If we put the parts together we get our whole class description for a contestant – the blueprint for a Contestant that can be used to create and manipulate them elsewhere in the program,

DESCRIPTION OF A Contestant:
  PRIVATE CelebrityName: String
  PRIVATE ProfessionalName: String
  PRIVATE TotalScore: integer

  TO CreateContestant GIVEN celeb AND pro:
      CelebrityName = celeb
      ProfessionalName = pro
      TotalScore = 0;

  TO SetContestantScore
    GIVEN judge1Mark AND judge2Mark AND judge3Mark AND judge4Mark:
      TotalScore = judge1Mark + judge2Mark + judge3Mark + judge4Mark

  TO GetContestantScore:
      RETURN TotalScore

  TO GetContestantName:
      RETURN (CelebrityName + “ and “ + ProfessionalName)

Using Contestants

So we have now created a new type of Contestant. How do we use it? We can now write code, for example, to create pairs of contestants as needed:

Pair1 IS A NEW Contestant USING “Alexandra” , “Gorka”

This is using our class blueprint to create our first object of type Contestant. We give it the names of “Alexandra”  and “Gorka”. It calls our constructor, our special method used to make Contestants, creates an empty blank of the object and then sets the CelebrityName to be “Alexandra” and the  ProfessionalName to be “Gorka”. It also sets their TotalScore to 0. Notice here we do not actually refer to any of those variable names. Writing the rest of the code we can forget about all that detail. We do not want or need to know the actual representation. We create more pairs as needed

Pair2 IS A NEW Contestant USING “Aston” , “Janette”
Pair3 IS A NEW Contestant USING “Jonnie“, “Oti”
Pair4 IS A NEW Contestant USING “Susan“, “Kevin”

and so on.

For each pair the program will also need to give them their marks as allocated by the judges eg by calling Pair1’s SetContestantScore method to set Alexandra and Gorka’s scores:

Pair1.SetContestantScore WITH 9, 10, 10, 10

Of course the actual marks will be provided by our judge objects. We do not need to know what happens to these values at this point as long as we trust it was the right thing. For all we know, writing this part of the program, each mark might just have been stored separately.

Similarly we can retrieve the total score when we need it. As long as that one method was written correctly, the right thing will always happen for all pairs. We will get the total (and in the following display it).

DISPLAY Pair1.GetContestantScore ()

We could do a similar thing to get the pair’s names. In the following we get both names and score and put space between the name and the score.

DISPLAY Pair1.GetContestantName() + “  “ + Pair1.GetContestantScore()

If we do this for each pair we will have displayed the leaderboard.

You can do no more …

As those method are the only ways our class provides for the code to access the details of the pairs, when writing the rest of the code, we can’t accidentally introduce bugs, printing the names in mixed up pairs, for example, or missing one of the judge’s scores when we total their marks. Programming in this style helps ensure we rule out the possibility of making these easy-to-make kinds of mistake. This is especially important as our whole program gets larger and larger, perhaps millions of lines of code, and as more people become involved in writing it. Those new people may not understand how we have coded a Contestant, but the point is they do not need to. All they need to know are what the methods we have provided to access it do not how they do it. They are the parts we have made public.

Next episode we will see more about the benefits of the abstract data type idea and so of object-oriented programming. Abstraction, i.e., hiding the implementation, allows us to modify the class implementation and yet the rest of the code still works without any further change.

Catch up with Paul’s other blog posts

 

Two CAS London Teach A-Level Computer Science courses in Autumn 2017

Update 18 September 2017: The A2 Projects with Databases & Python course (pink in the diary image below) is now full but we have spaces on the Object Oriented Programming course at QMUL (blue in the image below). If you’re attending the A2DB course you’ll be able to attend the OOP one as well.

Screenshot 2017-10-13 10.57.40


We have two ‘Teach A Level Computer Science’ courses coming up later this year. Please bookmark this page as it will shortly be updated with details of the earlier course (to be held at Queen Mary University of London in October / Nov). The details of the later course (to be held at King’s College London Waterloo in Nov / Dec) are below. Both cost £100 and free places are available for CAS Master Teachers.

  1. Teach A-Level Computer Science: Object Oriented Programming
  2. Teach A-Level Computer Science: A2 Projects with Databases and Python

Please note that these courses are for in-service teachers who are already confident in teaching computing at GCSE level, Python will be the programming language used.

The course dates are arranged so that it will be possible for teachers to attend both courses.

 

1. Teach A-Level Computer Science: Object Oriented Programming

Find out more and book a place

Course dates

  • Wed 4 October
  • Wed 11 October
    [Break – Wed 18 Oct]
  • Wed 25 October
    [Break – 1 Nov]
  • Wed 8 Nov
  • Wed 15 Nov

Location: Queen Mary University of London
Tutor: Dr William Marsh, CAS London

2. Teach A-Level Computer Science: A2 Projects with Databases and Python

Find out more | Apply for a place

Course dates

  • Week 1 (1 Nov 2017) – Single table databases and sqlite3
    [Break – 8 Nov]
    [Break – 15 Nov]
  • Week 2 (22 Nov 2017) – Foreign keys and relationships
  • Week 3 (29 Nov 2017) – Many-many relationships
  • Week 4 (6 Dec 2017) – Developing a project for A-Level Part 1
  • Week 5 (13 Dec 2017) – Developing a project for A-Level Part 2

Location: King’s College London, Waterloo
Tutor: Dr Sue Sentance, CAS London

For more information please contact Jo (j.brodie@qmul.ac.uk)

Short courses at a glance

Short courses from CAS London
2017 course dates and prices are in brackets, similar prices and dates are envisaged for 2018.

  • KS3 Teach KS3 Computing – January to March
    (8 sessions, £160, 23 Jan to 20 Mar)
  • MA module in Computing Education – January to March
    (10 sessions, £1,150, 17 Jan to 28 Mar)
  • KS4 Teach GCSE Computer Science – April to July
    (10 sessions, £200, 24 Apr to 3 Jul)
  • KS5 Teach A level Computer Science: Algorithms and Data Structures – March
    (5 sessions, £100, 1 Mar to 29 Mar)
  • KS5 Teach A level Computer Science: Object Oriented Programming in Python – June to July
    (5 sessions, £100, 7 Jun to 5 Jul)
  • Diving Deep into Primary Programming – Summer / Winter
    (All-day session, £60, 9 Jun)
  • KS5 Teach A level Computer Science: A2 Projects with Databases and Python – September to November
    (5 sessions, £100, 1 Nov to 29 Nov – register of interest full, please contact Jo Brodie (j.brodie@qmul.ac.uk) for waiting list information)
  • Diving Deep into Primary Programming – Winter / Spring
    (All-day session, £60, tbc)

New 2017 short courses from CAS London at King’s (Waterloo)

We have a series of twilight courses which will take place at King’s College London (Waterloo) campus, under the CAS London (King’s College London / Queen Mary University of London) banner on Monday and Wednesday evenings in 2017. Each lasts between five and eight weeks with courses costs from £100 to £200.

PDF flyer for these events with clickable links.


KS3 Computing

January – March 2017

Teach KS3 Computing
Monday Evenings 5pm to 7pm
23rd January to 20th March (8 sessions)
£160
generic-eventbrite-register-button


MA Module in Computing Education

January – March 2017

Computer Science Education: Theory and Practice (30-credit Masters’ module).
Tuesday evenings 5.30pm to 7.30pm
17th January to 28th March.
Focus on research in computer science education and start working towards a Masters’ degree. Includes 6,000 word assignment.
£1,150

generic-eventbrite-register-button


GCSE Computing

April – July 2017

Teach GCSE Computing
Monday Evenings 5pm to 7:30pm
24th April to 3rd July (10 sessions)
£200
generic-eventbrite-register-button


A level computing

March 2017

Teach A level computing: Algorithms and Data Structures
Five twilights on Wednesdays at King’s College London
Cost: £100
Starts Wednesday 1st March ends Wednesday 29nd March 2017 (5.00-7.30pm)
generic-eventbrite-register-button

(note that this course was originally advertised in November with a start date in the last week of February and this has been moved back to the first week of March – 4 Jan 2016)


June – July 2017

Teach A level computing: Object Oriented Programming in Python
Five wednesday twilights at King’s College London.
Cost £100
Starts Wednesday 7th June 2017 ends Wednesday 5th July 2017 (5.00-7.30pm).
generic-eventbrite-register-button


November 2017

Teach A level computing: A2 Projects with Databases and Python
Five Wednesday twilights at King’s College London.
Cost £100
Starts Wednesday 1st November 2017 ends Wednesday 29th November 2017 (5.00-7.30pm)
generic-eventbrite-register-button

Have you attended one of our courses or workshops? What did you think?

If you’ve attended one of our events we’d love to hear what you thought about it as our project comes to a close. We would like to know if our project has benefitted you as a Computing teacher and, if so, how. Please take a look at https://www.surveymonkey.com/r/TLCFinalSurvey (the survey should take around 10 minutes to complete).

Also – our next A-level Computing CPD course starts next Wednesday with FREE places for London Master Teachers.


The current incarnation of Teaching London Computing aka ‘TLC’ (as funded by the Mayor of London’s London School’s Excellence Fund grant) is coming to an end however we are very keen to continue with the project and make it sustainable.

We know from teachers telling us (thank you!) that they really value our in-depth CPD courses, our ‘miniCPD‘ sessions, and our free workshops are perennially popular too. We also know that lots of teachers in London, the rest of the UK and even from around the world are downloading and using our free classroom resources – hooray! We know this partly because people tell us, but also from the website stats (at time of writing we’ve just passed 87,000 blog ‘hits’) and the figures telling us that our PDF have been downloaded around 28,000 times.

The team behind TLC (staff from Queen Mary University of London and King’s College London) have taken on the role of one of the new CAS regional centres (we’re ‘CAS London’) and we’ve working with CAS to enable Master Teachers to attend free* at our next course (A-level Computing CPD, starting on Wednesday 7 October 2015) *while places last.

Thank you for making this project so interesting and rewarding, from all of us at Teaching London Computing.

New course date: A-level Computing CPD for London teachers – Wed 7 Oct, 10-week course

We will be running our A-level Computing CPD course again this Autumn for London teachers.

The course will run for 10 weeks from 5pm to 7.30pm on Wednesday evenings from 7 October to 16 December 2015 with a half-term break on Wednesday 28 October (ie a 10 week course running within an 11-week timeframe) in the ITL Building, Queen Mary University of London.

As always the course costs £300 but those teaching in London can take advantage of a 50% reduction thanks to funding from the Mayor of London, ie £150. In addition, with support from the new CAS London hub, we are also making FREE places available for London Master teachers only – please note that we will be requesting evidence of Master Teacher status before confirming your free place. The course will be taught by William Marsh and Trevor Bragg.

Full information and guide syllabus on our A-level Computing page, and there are some quick links to register below. Any questions? Contact Jo (j.brodie@qmul.ac.uk) for more.


Next course(s): 10-week Wednesday evening course: 7 October to 16 December 2015 (half-term 28 October 2015), 5.00pm to 7.30pm. For this course we are making free places available for London Master Teachers (you will be sent an application for to confirm your eligibility).

Eventbrite - A-level Computing CPD - Teaching London Computing & CAS London for a place on A-level Computing CPD – Teaching London Computing & CAS London

Please sign-up to be kept informed of future courses.


Our next events – one free workshop, one (not free) CPD course for A-level Computing teachers

Here’s what we have coming up so far in August… do also sign up to our mailing list to be kept informed of future activities.

1. Course for A-level Computing teachers in London
A-level Computing CPD August one-week intensive 2015 – from Teaching London Computing
Monday, 17 August 2015 at 10:00 – Friday, 21 August 2015 at 16:00 – at QMUL
This is a one-week intensive A-level Computing CPD course from Teaching London Computing. The course equips Computing teachers with the programming subject knowledge and skills to teach the new A-level Computing curricula.
[More information] [Eventbrite tickets]

2. Free workshop for Computing teachers in London
Using Turtle Graphics to Transition from Visual to Textual Programming
Wednesday, 26 August 2015 from 13:00 to 15:30 – at QMUL

How to transition from visual languages (e.g. Scratch) to text based languages (e.g. Python)?
A hands on workshop exploring the use of turtle graphics for making the transition from visual programming (using Scratch) to textual programming (using Python).

  • Comparing programs in Scratch and Python
  • Setting problems that can be soved in both visual and textual languages

Computers are provided but delegates may bring a laptop if they wish
(with MIT Scratch 2 and Python 3 installed). Some knowledge of basic programming assumed.
[More information] [Eventbrite tickets]

Do I have to be a London computing teacher to attend Teaching London Computing courses and workshops?
While we prioritise London computing teachers we do make space available for those from outside London. For our courses we charge non-London teachers the full price (£300) but thanks to funding from the Mayor of London we’re able to offer a 50% discount to London teachers only (£150). Our courses are aimed at those who are currently (or who are about to begin) teaching the Computing curricula (GCSE and A-level). Contact Jo Brodie (j.brodie@qmul.ac.uk) for further information.

What short courses & workshops do we have available for London Computing teachers at the moment? These!

Newly announced: we have a new free event, Understanding New Careers: Creativity and Technology on 14 July and a one-week intensive A-level CPD course coming up in August (17-21). More details about these below.

We also have space on our evening workshop session, on the 13th July, on GUI Programming in Python. (There’s a waiting list for the earlier one (identical workshop) on Monday 29 June though).

In upcoming-date order…

1. GUI Programming in Python – Monday 29 June 2015 – FREE (limited availability)

Python is a popular language for elementary programming but it not so easy to write programs with a graphical user interface (GUI). This workshop will introduce GUI programming in Python, covering:

  • the concepts common to all GUI frameworks: events, widgets and attributes
  • the role of object-oriented programming in GUIs
  • the choice of GUI frameworks (but looking mainly at tkinter, the default framework)

[Our page on this workshop]
[Link to Eventbrite tickets for 29 June workshop]

 

2. GUI Programming in Python – Monday 13 July – FREE (spaces available)

This workshop is identical to the one above.

[Our page on this workshop]
[Link to Eventbrite tickets for 13 July workshop]

 

3. Understanding New Careers: Creativity and Technology – 14 July – FREE

The event is for any London teacher who advises young people on careers, or who teaches art, or technology. Part-presentation, part-panel discussion, we will look at what happens in Tech City and discuss routes into new career opportunities in technology and creativity.

[Eventbrite link to more information and tickets for ‘Understanding new careers: creativity and technology’]

 

4. A-level Computing CPD: one-week intensive – 17-21 August 2015 – £300/150

This course equips Computing teachers with the programming subject knowledge and skills to teach the new A-level Computing curricula.

The course is taught using Python and includes much practical work. It is essential to have experience in programming, to GCSE level at least, including assignment, if statements, loops and arrays and ideally also function definitions. Delegates should be confident to solve simple programming problems requiring approx 20-50 lines, either with Python or a similar language.

The course costs £300 but thanks to funding from the Mayor of London we are able to reduce this for teachers in London.

[Our page on the A-level computing course]
[Link to Eventbrite tickets for this A-level Computing CPD]

*New course* A-level CPD Computing for London teachers, from @TeachingLDNComp

Our new A-level CPD course will run over Spring and Summer at King’s College London (Waterloo Campus). The course will run on Tuesday evenings from Tuesday 28 April and run for 10 weeks (with a break for half term) until 7 July 2015. William Marsh from QMUL will be the tutor and all materials will be made available to course delegates at the start of the course.

The course dates are below, full course content information is available from our A-level CPD Computing page and tickets are available from Eventbrite.

You might also be interested in the new free material we’ve added to our section on Interdisciplinary Computational Thinking – lots of classroom activity sheets to download and other free resources.


 

Next A-level CPD course(s): Our next 10 week A-level CPD course will begin on Tuesday 28 April 2015.
Eventbrite - KCL: Summer 2015 - A-level Computing CPD - from Teaching London Computing

Fees
Our courses are reduced by 50% to £150 for London teachers, thanks to our funding, the full price for non-London teachers is £300 – please see our Fees and Funding page for more information. London teachers have priority on our courses.

Eligibility requirements
This course is a follow-on from our GCSE Computing CPD courses. The course assumes that you will be familiar with programming in Python or a similar language.

More information?
Please contact Jo Brodie (j.brodie@qmul.ac.uk)

Course dates: all on Tuesdays

Week 1: 28 April 2015
Week 2: 5 May 2015
Week 3: 12 May 2015
Week 4: 19 May 2015
Half-term: 26 May 2015 (no class)
Week 5: 2 June 2015
Week 6: 9 June 2015
Week 7: 16 June 2015
Week 8: 23 June 2015
Week 9: 30 June 2015
Week 10: 7 July 2015

Please sign-up to be kept informed of future courses.