Coding Standard
Introduction
This standard defines how your code should be styled for assignments and projects. By
following these conventions, you will produce code which is more readable and easier for
you or someone else to follow. These guidelines apply to all programming languages. The
examples are shown in Python. Being able to write clean legible code will also improve your
employability.
The coding standard guidelines should also help you stay within the word count limits by
including relevant information in your code instead of the written report.
Identifiers
Variables/Attributes
Identifiers for all attributes and variables should be meaningful and if appropriate contain
its own unit of measure. The unit of measure helps in understanding how to use it without
reading the comments. Variables/attributes should always start with a lower case letter.
Always use camel case to help aid reading of identifies, so an upper-case letter is introduced
for every new word.
Examples:
weightInKilos heightInMetres delayInMilliseconds
Class Identifiers
Class identifiers always start with an uppercase letter and should always be a noun. So,
Encryption is not a good name but Encryptor or EncryptionHelper are fine.
Examples:
Person Doctor Appointment
Constants
Identifiers of constants should always be expressed in uppercase, unless and only unless
there is a necessary convention to use lowercase (for example to distinguish between g (ac-
celeration due to earth’s gravity and G the universal gravitational constant). For constants
underscores are used to separate words.
Examples:
PI=3.149265445 COLUMNS=10
1
Method / Function Names
Method names should start with a lower case letter and then follow camel case. All method
names should be verbs.
Examples:
add(a, b)
checkTwoEqual(array)
Comments
Your code should always contain comments that for a reasonably self-contained part of the
code explain what the intention of that code is, what it is aiming to achieve. The comments
should also provide an outline of the algorithm that is used to achieve that aim.
Besides that, the code should ideally be self-documenting, that is, it should be simple
enough to be understood without having to refer to the comments. If the code is hard to
follow then it should be simplified and in some cases broken up by the use of methods /
functions to better structure the code.
Comments must always precede the part of the code that they refer to. Comments should
not go over the right hand side of the edit window. If a comment is too long, then it should
be broken down over several lines.
Pydoc
All comments in Python programs should follow the Pydoc format, this allows code docu-
mentation to be generated automatically.
Class Comments
All classes should start with a preamble describing the purpose of the class, what data it
stores and what services it provides, so that a user of the class can quickly determine how to
use the said class.
Method / Function Comments
At a minimum each public method in a class should be commented. This is vital since the
public methods are the public interface to the code. Each method should have comments for
both the input variables and the returned values (if there are any).
Example:
"""
Calculates the factorial of a number
@param Input integer to take the factorial of
@return Factorial of input
"""
def factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
2
Use of On-line Resources and Textbooks
If in the preparation of your code you have used specific ideas or code fragments that you
have found on-line or in a textbook, then you must include references to those sources in
your comments and indicate clearly which part of the code is based on those. The reference
must be precise:
• If you have used a textbook, then you must point to the specific page, pages, or figure that
you have used.
• If you have used or adapted an answer to a question posted on some on-line forum, say,
stackoverflow, then you must point to that specific answer, including a URL to that answer.
• If you have used or adapted a solution provided by some AI tool, say, OpenAI’s ChatGPT
or GitHub Copilot, then you must include the prompt or prompts that you have used.
If you have used a source just to gain an understanding of how a particular language construct
works, then no reference to that source is required.
Examples:
"""
The code for the following function has been taken from
terminus (https://stackoverflow.com/users/9232/terminus):
Extracting minimum and maximum x-values Python
Stack Exchange Network, 11 Dec 2010.
https://stackoverflow.com/a/4416448 [accessed 14 Jan 2025].
User contributions licensed under cc by-sa 3.0
https://creativecommons.org/licenses/by-sa/3.0/
"""
def readfile(pathname):
f = open(sti + ’/testdata.txt’)
for line in f.readlines():
line = line.strip()
x, y = line.split(’,’)
x, y = float(x),float(y)
yield x, y
"""
The code for the following function has been obtained from
ChatGPT: Write a Python function that takes an array of strings as
argument and returns a sorted array as result but do not use the
built-in sort function.
OpenAI, ChatGPT based on GPT-4.
https://chat.openai.com/chat [accessed 14 Jan 2025].
"""
def sort_strings(arr):
# Get the length of the array
n = len(arr)
# Perform bubble sort
for i in range(n):
# Flag to optimize: if no swaps, the array is already sorted
swapped = False
3
for j in range(0, n-i-1):
# Compare adjacent strings
if arr[j] > arr[j+1]:
# Swap if out of order
arr[j], arr[j+1] = arr[j+1], arr[j]
swapped = True
# If no two elements were swapped in the inner loop, then the array
# is sorted
if not swapped:
break
return arr
# Example usage:
arr = ["banana", "apple", "cherry", "kiwi"]
sorted_arr = sort_strings(arr)
print(sorted_arr) # Output: [’apple’, ’banana’, ’cherry’, ’kiwi’]
This attribution is required under the licenses that apply to the sources used in the examples
and to the Academic Integrity Policy of the university.
Indentation and Braces
All codes must be indented in a consistent way to correctly convey the structure of the pro-
gram. Unlike other programming languages, Python enforces the correct indentation and
uses it to define blocks of code and indicate groups of statements.
Blocks of code that belong to the same structure (like if, for, while, def, etc.) must be
indented the same amount. The level of indentation determines which statements are part of
the block.
You must use spaces or tabs, but do not mix both in a single project or file. Python’s official
style guide (PEP 8) recommends using 4 spaces per indentation level. If you mix spaces and
tabs, this can result in an IndentationError. If the indentation is inconsistent, Python will
raise an IndentationError.
Blocks such as the body of statements if, for, while, def, and class must be indented.
Examples:
# If-else example
x = 10
if x > 5:
print("x is greater than 5") # This line is part of the ’if’ block
else:
print("x is 5 or less") # This line is part of the ’else’ block
def greet(name):
print("Hello, " + name) # Indented inside the function body
if name == "Alice":
print("Welcome back, Alice!")
else:
4
print("Welcome, guest!")
greet("Alice")
Conclusion
Following this guide will make your code easier to understand and follow and will help you
debug and maintain your code.
The coding standard guidelines should also help you stay within the word count limits by
including relevant information in your code instead of the written report.