Software Engineering Preliminary
Software Engineering Notes 2024/25
By Joshua Chow
Paradigms
Object Oriented Programming -
Abstraction
Simplifying complex systems by focusing on essential characteristics and hiding unnecessary details
E.g. not focusing on how it looks, rather the gameplay of the character e.g. name, type, level, stats, and moves
Encapsulation
Bundling of data (attributes) and actions (methods) into objects
Allows for data hiding and provides a clean interface for interacting with the object
A function that does something which is attached to the character
Class (species)
Object (specific type)
Data Types
Data is organised in different ways. The data type you use will depend on what you intend to do with it. When you create a variable, it will be cast as a certain data type.
For example, if you want to store the user’s name, you probably want to use a string.
If you want to store the user’s age, you may want to use an integer.
The main data types we will be working with are
Strings
Integers
Floats
Boolean
And these are some data structure that we can use to organise pieces of data
Lists / arrays
Dictionary
Stacks
Variable naming conventions
Well written/formatted code is easier to read and understand
Badly written code can be confusing to read, especially for other people
Use appropriate, informative variable names
Be consistent with variable formatting
There are three naming conventions
Snake_case
All lower case, separated by underscore
PascalCase
Every word capitalised
camelCase
First word lower case, every other word capitslied
Any style is technically ok, but Python programmers tend to use snake_case
Strings
Strings are used to store text data
This can include letters, numbers, and special characters
You can manipulate strings (addling/deleting characters, changing the order of characters, etc) using code.. But you can’t treat them like numbers
Integers
Integers are a whole number (no decimals or fractions)
They can be positive or negative.
Floating Point Numbers
Floating point numbers (AKA ‘floats’) are numbers with decimal points.
There’s a limit to how many decimal places you can use in a float.
Working with floats is more processor-intensive than Integers.
Boolean
The Boolean data type is used to store logical data - either True or False
All Booleans have only two possible values.
Can be used whenever you have only two things to choose from.
They only use one bit of data, very efficient.
Python example : likes_pizza = true
Input
The Input() function allows the user to type data into the program.
This data can then be used for a variety of purposes.
The input() function defaults to a string data type, so you might need to cast it into a different data type
Casing
Casting is the process of changing the data type of a piece of data
int() converts to an integer
str() converts to a string
float() converts to a float
bool() converts to a boolean
Operators
Python uses a number of different types of operators
Operators are used to do a mathematical calculations, compare values, etc.
The operator types we will be using are:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Arithmetic Operators
These are used to do mathematical calculations
Addition operator
Subtraction operator
Multiplication operator
/ Division operator
Assignment Operators
Assignment operators are used to assign values to variables.
Used to ass
Comparison Operators
Comparison operators are used to compare two values
Examples
== compares two values to see if they are equal to each other
!= compares two values to see if they are NOT equal to each other
> compares two values to see if the first is greater than the second
<= compares two equal values to see if the first is less than or equal to the second
Logical Operators
Logical operators are used to combine conditional statements
Examples:
And returns ‘true’ if both statements are true
Or returns ‘true’ if one of the statements is true
Formatting
Python uses indentation to structure its code
This is not common
It makes it easier to read
Other languages use different wants of structuring code
Control structures
Control structures
Computer code can be very complex, but if you break it down into its smallest parts, the
Sequence
In a computer program or an algorithm, sequence involves simple steps which are to be executed one after the other. The steps are executed
Selection
Selection is used in a computer program or algorithm to determine which particular steps or set of steps is to be executed. A sleection statement can be used to choose a specific path dependent on a condition. There are two types of selection: binary (two-way branching) selection and multi-way
Repetition
Repetition allows for a portion
Types of Loops
FOR loops
Run a set number of times
Can be based on a number (eg: “run the loop 10 times”)
Can be based on analysing a piece of data (eg:’run the loop once for every letter in a string”)
Data you can run a loop on is called an iterable (iteration)
There are different ways you can use a for loop
WHILE loops
While loops run as long as a condition is true. As soon as it stops being true, the loop exits
My_variable = 1
While my_variable ==1:
print(“Good news, your variable is still 1!”)
Control Structures
Sequences
In a computer program or an algorithm, sequence involves simple steps which are to be executed one after the other. The steps are executed in the same order in which they are written.
Selection
Selection is used in a computer program or algorithm to determine which particular step or set of steps is to be executed. A selection statement can be used to choose a specific path dependent on a condition. There are two types of selection: binary (two-way branching) selection and multi-way (many way branching) selection. The following slides will give examples of each.
Binary selection
As the name implies, binary selection allows the choice between two possible paths. If the condition is met then one path is taken, otherwise the second possible path is followed. In each of the examples below, the first case described requires a process to be completed only if the condition is true. The process is ignored if the condition is false. In other words there is only one path that requires processing to be done, so the processing free path is left out rather than included saying ‘do nothing’
Multi-way selection
Multi-way selection allows for any number of possible choices, or cases. The path taken is determined by the selection of the choice which is true. Multi-way selection is often referred to as a case structure
Pre-test repetition
A pre-tested loop is so named because the condition has to be met at the very beginning of the loop or the body of the loop is not executed. This construct is often called a guarded loop. The body of the loop is executed repeatedly while the termination condition is true
Post-test repetition
A post-tested loop executes the body of the loop before testing the termination condition. This construct is often referred to as an unguarded loop. The body of the loop is repeatedly executed until the termination condition is true. An important difference between a pre-test and post-test loop is that the statements of a post-test loop are executed at least once even if the condition is originally true, whereas the body of the pre-test loop may never be executed if the termination condition is originally true.
Sub-programs
Subprograms, as the name implies, are complete part programs that are used from within the main program section. They allow the process of refinement to be used to develop solutions to problems that are easy to follow. Sections of the solution are developed and presented in understandable chunks, and because of this, subprograms are particularly useful when using the top-down method of solution development.
Error Types and Debugging
Errors can occur at all stages of software development. The earlier you can find these errors and fix them, the better. During the coding stage of your software development, there are basically three types of errors:
Syntax errors
Runtime errors
Logic errors
Syntax Error
A syntax error results when the code you’ve written doesn’t follow the rules of the coding language. These errors are usually identified by your IDE (in our case, PyCharm). You, the coder, will have to fix them yourself before your program can run properly. Syntax errors are usually indicated by coloured/underlined text. For example:
Runtime Error
Runtime errors occur when the program can no longer run the code due to hardware or software problems. For example:
Impossible mathematical equations (e.g. div by 0)
Data out of range (e.g. 16-bit integer calculations result in out of range result)
Runtime errors are not detected by your IDE, so you’ll need to test your program to make sure it works properly.
Logic Error
Logic errors occur when the program provides an incorrect output due to poor algorithm design. The program may continue to run, but it won’t output the ‘correct’ results. Basically, the computer is doing exactly what you told it to do.. but you told it to do the wrong thing.
Your IDE won’t help you with this type of error, so you’ll have to find and fix it yourself. Use systematic error detection techniques such as desk checking to correct logic errors.
Stubs
Stubs are a small subroutine used in place of a yet-to-be-coded subroutine
They simulate the processing that will occur in the final subroutine
Flags
A flag is usually a boolean variable. Flags are often used to check if the program has reached a certain point in your code -- sort of like a stub.
Flags can be used for other stuff too, they’re often used like this to help debug code. This example code contains two flags. Where are they and what do they do?
Output statements
Output statements are a good way of checking the 'flow' of your code – finding which parts are being executed and when that execution happens.
These are usually output to the console. They tend to be things like,
print("just started the calculate_rain() function"
or
print(f"temperature variable is {temperature}"
Single line stepping
The process of halting execution after each statement is executed.
It shows you the values of variables at each step of execution.
It is used to detect errors in code.
Breakpoints are used to temporarily halt the execution of code. Once stopped, it is possible to examine the current value of each variable. By adding breakpoints at strategic points within your code, it's possible to locate the source of an error
Try / Except
Using try/except can prevent a crash in the event of an error. For example, if you try to divide by zero, your Python program will crash. But using try/secept will generate an error message and allow the program to continue
try:
numerator = int(input("Enter a number to divide: "))
denominator = int(input("Enter a number to divide by: "))
result = numerator / denominator
print(result)
except ZeroDivisionError:
#if you try to divide by zero it sums the code below
print("Can't divide by zero, sorry")
except:
#and if there's some other type of error (maybe trying to divide by a letter).
# It runs this other code..
print("Some other error, sorry.")
print("Ok bye. ")
System Modelling (SDD Resources)
Most models represent another physical object
We’ll be using models to represent systems and processes
Context Diagrams
Gives an overview of an entire system
Only shows how the system relates to external entities
Focused on inputs and outputs
One of the simplest models we’ll be using only has two symbols
There is only one system, there can be multiple entities though -
This is the world’s simplest calculator. It allows the user to input two numbers and run a mathematical calculation (add, subtract, divide or multiply). The calculator then displays the result to the user.
System: Calculator
Entities: User
Data: Num 1 Num 2
Result
Operators
Example: Context Diagram - Email server
Users are able to log into an email server using a username and password. Once logged in, the user can send an email message to a recipient somewhere outside the server. To do this, the user must provide the recipient’s email address and a message to send. When that message is successfully sent, the email server gives a confirmation message to the user.
System: email server
Entities: user, recipient
Data: email message, username, password, email address, confirmation message
Example 2: Context Diagram - Snupchat
Snupchat is a social media app that allows people to take pictures using their phone and send those pictures to other people.
Snupchat users must log into the system with a username and password. Once logged in, they can take a picture and send it to another Snupchat user if they know the recipient's username. When the picture is sent, the user receives a confirmation from Snupchat indicating that the picture was sent successfully. Once the recipient has seen the picture, the sender will receive a confirmation that the picture has been viewed.
A copy of each picture is also automatically uploaded to a server run by ExtremeMarketing, a company that analyses the photos for marketing purposes.
System: Snupchat
Entities: Sender, Receiver, Marketing
Data: Username/Password, Confirmation, Recipient Name
Data Flow Diagram
DFDs have three types of symbols instead of two. But they look very similar. In a DFD, you can have more than one of every symbol.
YouTube Viewers and YouTube Partners access YouTube by logging in using their accounts with a username and password (which are stored in an accounts database) Once they have logged in, both types of users are able to do the same things, but the Partners will also be able to receive payment from YouTube. The things both Viewers and Partners can do are:
Upload videos
Edit videos they have already uploaded
Publish videos to be viewed by others
View videos
Social media functions like subscribing, liking and commenting
Advertisers are not able to any of the things mentioned above, but they are able to:
Insert their adverts into YouTube videos at the time they are viewed
Make payments to bank (which will then be used to pay Partners)
Lastly, the Bank is able to:
Receive payments from advertisers (which will then be used to pay Partners)
Pay Partners
Entities: YouTube viewers, Partners, Advertisers, Bank
Processes: (bullet points)
Data Stores: accounts database