cover-img

Code Smell 226 - Mixed Priorities

Another crashed spacecraft. Another software problem

3 October, 2023

1

1

0

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

Problems

Solutions

  1. Create accurate simulations
  2. Make fault-tolerant software

Context

Luna-25 crashed on the moon's surface on August 19, 2023.

4 days before India’s Chandrayaan-3 soft landed on Moon's south pole.

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

Spacecrafts have a long history of software faults.

Sample Code

Wrong

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

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

Code Smell 198 - Hidden Assumptions

More Info

Roscosmos Telegram

Asia Times

Disclaimer

Code Smells are my opinion.


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

Software Engineering Great Quotes


This article is part of the CodeSmell Series.

How to Find the Stinky Parts of your Code

1

1

0

Maxi Contieri

Buenos Aires, Argentina

🎓Learn something new every day.📆 💻CS software engineer 👷coding👨🏽‍🏫teaching ✍🏾writing 🎨Software Design 🏢SOLID 🌉TDD 👴Legacy 💩Code Smells

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.