Refactoring the Bad Boids


In an appendix, taken from here, is a very poor implementation of the Boids flocking example from the course.

Your task is to take this code, and build from it a clean implementation of the flocking code, finishing with an appropriate object oriented design, using the refactoring approach to gradually and safely build your better solution from the supplied poor solution. Simply submitting a good, clean solution will not complete this assignment; you are being assessed on the step-by-step refactoring process, on the basis of individual git commits. You should develop unit tests for your code as units (functions, classes, methods and modules) emerge, and wrap the code in an appropriate command line entry point providing an appropriate configuration system for simulation setup.

Assignment Presentation

For this coursework assignment, you are expected to submit a short report and your code. The purpose of the report is to answer the non-coding questions below, to present your results and provide a brief description of your design choices and implementation. The report need not be very long or overly detailed, but should provide a succinct record of your coursework.


You should submit your report and all of your source code so that an independent person can run the code. The code and report must be submitted as a single zip or tgz archive of a folder which contains git version control information for your project. Your report should be included as a PDF file, report.pdf, in the root folder of your archive. There is no need to include your source code in your report, but you can refer to it and if necessary reproduce lines if it helps to explain your solution.

Marking Scheme

  • Final state of code is well broken down into functions, classes, methods and modules [3 marks]

  • Final state of code is readable with good variable, function, class and method names and necessary comments [2 marks]

  • Git version control used, with a series of sensible commit messages [2 marks]

  • Command line entry point and configuration file, using appropriate libraries [3 marks]

  • Packaging for pip installation, with file with appropriate content [2 marks]

  • Automated tests for each method and class. [5 marks]

  • Supplementary files to define license, usage, and citation. [3 marks]

  • A text report which:

    • Lists by name the code smells identified refactorings used, making reference to the git commit log [2 marks]

    • Includes a UML diagram of the final class structure [1 mark]

    • Discusses in your own words the advantages of a refactoring approach to improving code [1 mark]

    • Discusses problems encountered during the project [1 mark]

[25 marks total]


from matplotlib import pyplot as plt
from matplotlib import animation
import random

# Deliberately terrible code for teaching purposes

boids_x=[random.uniform(-450,50.0) for x in range(50)]
boids_y=[random.uniform(300.0,600.0) for x in range(50)]
boid_x_velocities=[random.uniform(0,10.0) for x in range(50)]
boid_y_velocities=[random.uniform(-20.0,20.0) for x in range(50)]

def update_boids(boids):
	# Fly towards the middle
	for i in range(50):
		for j in range(50):
	for i in range(50):
		for j in range(50):
	# Fly away from nearby boids
	for i in range(50):
		for j in range(50):
			if (xs[j]-xs[i])**2 + (ys[j]-ys[i])**2 < 100:
	# Try to match speed with nearby boids
	for i in range(50):
		for j in range(50):
			if (xs[j]-xs[i])**2 + (ys[j]-ys[i])**2 < 10000:
	# Move according to velocities
	for i in range(50):

axes=plt.axes(xlim=(-500,1500), ylim=(-500,1500))

def animate(frame):

anim = animation.FuncAnimation(figure, animate,
                               frames=50, interval=50)

if __name__ == "__main__":