Project lifecycle

Code maintenance

Code maintenance

Code maintenance

🤔 When to touch existing code?

  • A new feature requires code changes (violation of the Open-Closed principle)
  • A library interface has changed
  • Advanced knowledge of the developer
  • Clean up legacy code developed by others
  • Performance optimization
  • Bugfixes
Code maintenance

Refactoring

💡 Modify code without changing its functionality

def add(x, y):
    result = x + y
    return result

is equivalent to

def add(x, y):
    return x + y

Advice for refactoring:

  • Ensure that the code is covered by tests
  • Never implement a new feature during refactoring
  • Refactor in small steps
  • Run the tests after each iteration
Code maintenance

Technical debts

💡 Future code work that needs to be invested, because a certain code implementation has been chosen over another

Possible causes:

  • Deliberately decided to implement a feature in a simple and quick way knowing that refactoring will be required (cf. YAGNI principle)
  • Implement a feature without being aware that a different implementation is needed (cf. Open-Closed principle)

✅ Avoid piling up technical debts.

Code maintenance

Other technical debts

  • Incomplete documentation
  • Missing tests
  • Outdated dependencies
  • Insufficient error handling
Code maintenance

Broken window theorem

From criminology: If a broken window in an otherwise neat house is not repaired soon, it will deteriorate quickly.

💡 Broken windows can appear everywhere and should not be ignored.

def add(x, y):
    return x + y

def subtract(x, y):
    #return x - y
    return add(x, -y)

✅ If you see a broken window, fix it.

Code maintenance

Broken windows

  • TODO comments
  • Commented-out code
  • Code duplication
  • Formatting issues
  • Failing tests
  • ...
Collaborative development

Collaborative development

Developing software in a team is challenging.

⚠️ Common problems:

  • Different coding styles
  • Various knowledge levels
  • Undefined interfaces between code sections
  • Violations of the DRY principle
Collaborative development

Different coding styles

✅ Define a coding style for your project and enforce it with a tool.

  • Ruff, pylint, pyflakes, ... as pre-commit hook
  • Extensions of VS Code: Ruff, autopep8, ...
  • Verification job in the CI pipeline
Collaborative development

Various knowledge levels

✅ Encourage exchange between developers.

  • Regular meetings
  • Code reviews
  • Hackathons and workshops
  • Pair programming
Collaborative development

Undefined interfaces between code sections

✅ Developers must agree upon interfaces between their features.

  • Function signatures
  • Object attributes and methods
  • Module structure
Collaborative development

Violations of the DRY principle

✅ Developers should coordinate their features to avoid code duplication.

  • Same function is required and implemented multiple times
  • Different external libraries are imported for the same use case
Collaborative development

Every software project should have one responsible person.

Responsibilities of the team leader

  • Administrate the Git repository
  • Set up communication channels (mailing list, ...)
  • Coordinate developer meetings
  • Moderate discussions
  • Track milestones
Collaborative development

Responsibilities of the entire team

  • Share knowledge
  • Conduct peer reviews
  • Define the coding style
  • Know the technical debts
  • Publish developer guidelines
  • Maintain test pipelines
  • Work on issues
Further reading

Further reading

https://refactoring.guru/refactoring