# Code Smell 226 - Mixed Priorities

> TL;DR: Design and test software. It is cheaper than hardware

# Problems

- [Bijection](https://maximilianocontieri.com/the-one-and-only-software-design-principle) Fault

- Unexpected Defects

# Solutions

1. Create accurate simulations

2. Make [fault-tolerant](https://en.wikipedia.org/wiki/Fault_tolerance) software

# Context

[Luna-25](https://en.wikipedia.org/wiki/Luna_25) crashed on the moon's surface on August 19, 2023. 

4 days before India’s [Chandrayaan-3](https://en.wikipedia.org/wiki/Chandrayaan-3) soft landed on Moon's south pole.

A forensic analysis revealed that the instructions shared a bus and were not prioritized correctly.

Spacecrafts have a [long history](https://maximilianocontieri.com/coupling-the-one-and-only-software-design-problem) of software faults.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/e828fb96c53f62f62b40dc70bc1b02ee)
```python
class TaskManager:
    def __init__(self):
        self.tasks = []

    def add_task(self, task, priority):
        self.tasks.append((task, priority))

    def execute_tasks(self):
        # No sorting

        for task, _ in self.tasks:
            task.execute()

class Task:
    def __init__(self, name):
        self.name = name

    def execute(self):
        print(f"Executing task: {self.name}")

task_manager = TaskManager()
highPriorityTask = Task("Slow down")
mediumPriorityTask = Task("Take Photos")
reviveKlaatu = Task("Klaatu barada nikto")

# unsorted
task_manager.add_task(mediumPriorityTask, 2)
task_manager.add_task(highPriorityTask, 1)
task_manager.add_task(reviveKlaatu, 3)

task_manager.execute_tasks()
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/eb49b855ffcefc372150228b9b9f0a70)
```python
class TaskManager:
    def __init__(self):
        self.tasks = []

    def add_task(self, task, priority):
        self.tasks.append((task, priority))

    def execute_tasks(self):
        # Sort tasks by priority (high to low)
        self.tasks.sort(key=lambda x: x[1], reverse=True)

        for task, _ in self.tasks:
            task.execute()

class Task:
    def __init__(self, name):
        self.name = name

    def execute(self):
        print(f"Executing task: {self.name}")

task_manager = TaskManager()
highPriorityTask = Task("Slow down")
mediumPriorityTask = Task("Take Photos")
reviveKlaatu = Task("Klaatu barada nikto")

# unsorted
task_manager.add_task(mediumPriorityTask, 2)
task_manager.add_task(highPriorityTask, 1)
task_manager.add_task(reviveKlaatu, 3)

task_manager.execute_tasks()
```

# Detection

[X] Manual

This is a design smell

# Tags

- Reliability

# Conclusion

Create software components and simulate real and not real conditions

# Relations

%[https://maximilianocontieri.com/code-smell-198-hidden-assumptions]

# More Info

%[https://t.me/roscosmos_gk/11053]

%[https://asiatimes.com/2023/08/luna-25-crash-lands-russia-china-space-ambitions/]

# Disclaimer

Code Smells are my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).
  
* * *

> The Analytical Engine has no pretensions whatever to originate anything. It can do whatever we know how to order it to perform… But it is likely to exert an indirect and reciprocal influence on science itself.

_Ada Lovelace_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
