---
description: Complexity comshmexity.
---

# 31.1 Complexity II

In the previous Software Engineering lecture, we introduced the concept of complexity.&#x20;

We define complexity as: anything related to the structure of a software system that makes it hard to understand and modify the system.

Project 3: Build Your own World provides the first opportunity to create complex code. We do not explicitly define what constitutes a room and what constitutes a hallway. There is a great deal of freedom for not just the implementation but the entire architecture of the project. As a result, it is of utmost importance to understand how to manage the complexity.&#x20;

In this chapter, we will use examples from Project 3: BYoW to illustrate cases of poorly managed complexity. Consider the following code snippet:

<figure><img src="https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2FiiGu5PN9feYP1ucw45fU%2Fcomplex%20code.png?alt=media&#x26;token=3f2dc863-81bf-498b-8153-0ecf70c1d8b3" alt=""><figcaption></figcaption></figure>

There are multiple complexity issues with this code:

1. It lacks readability. The code itself is not narrative. It is not immediately clear to someone looking at the code for the first time to figure out what it does.
2. There are many complex manual computations of moving in each direction. The details of these should be hidden away.
3. The code is very repetitive. It will be difficult to make changes because the programmer will need to make certain that all of the updates have been fixed.

Ultimately, there is no _right_ answer for what the best practice is for coding in this movement functionality. One could argue that this code could benefit from having helper methods to check if a move in a certain direction is valid and helper methods to do the updates. However, it could also be argued that this code is simple enough to not have any helper methods. It is up to the programmer to decide what would be easiest to write and maintain.
