16
Things I've learned from Upgrading a Django Project Ikhsan Noor Rosyidin essanpupil essanpupil essanpupil

Python id meetup, Maintaining a Dirty Code Django Project

Embed Size (px)

Citation preview

Page 1: Python id meetup, Maintaining a Dirty Code Django Project

Things I've learned fromUpgrading a Django Project

Ikhsan Noor Rosyidin

essanpupil

essanpupil

essanpupil

Page 2: Python id meetup, Maintaining a Dirty Code Django Project

Starting point

I am dealing with:

1.Outdated

2.Unsupported

3.Unmaintained

4.Undocumented

5.untested

6.Complicated

7.Dirty

Legacy Code,

Alone . . . .

Page 3: Python id meetup, Maintaining a Dirty Code Django Project

Outdated

The project was using old libraries, so it's lack of:

1.Security

2.Speed

3.Efficiency

4.Documentation

5.StackOverflows :)

6.and any new features . . . .

Page 4: Python id meetup, Maintaining a Dirty Code Django Project

Unsupported

The project is develop with:

1.No paid support

2.No early core developer member in team

Page 5: Python id meetup, Maintaining a Dirty Code Django Project

Unmaintained

The project stay alive with:

1.No refactoring

2.Duplicate function

3.Out of range data type

4.Feature addition without design consideration

Page 6: Python id meetup, Maintaining a Dirty Code Django Project

Undocumented

Imagine getting started without:

1.No business flow doc

2.No technical design doc

3.Little to meaningless docstring

4.A lot of commented code

Page 7: Python id meetup, Maintaining a Dirty Code Django Project

Complicated

In order to function properly, the project depends on:

1.Rsync

2.Database replication

3.Custom “task queue” process

4.. . . . unintegrated

Page 8: Python id meetup, Maintaining a Dirty Code Django Project

Dirty

Imagine a project with:

1.Too much conditional branch

2.Too much statement

3.Giant module

4.Unused variables

5.Unused imports

6.Commented code

7.Hundreds of pylint/flake8 warnings & errors

Page 9: Python id meetup, Maintaining a Dirty Code Django Project

My First Step (Upgrade)

Upgrade to Django 1.8 with the purpose of:

1.Bring the project into my “realm”

2.Ease newly joined developer

3.Harness new built-in features as soon as possible

4.Compatibility with modern debugging & development library

5.Use available online documentation

Page 10: Python id meetup, Maintaining a Dirty Code Django Project

My Second Step (Split)

1. Write smoke test, request to urlpatterns

2. Split large modules (except models)

3. Update library imports

If you still want to split models, do it extra carefully & prepare for cyclic import. Models connect python code with database, do not threat models like regular modules.

Page 11: Python id meetup, Maintaining a Dirty Code Django Project

My Third Step (Clean)

Follow this advice, http://programmers.stackexchange.com/a/155505

1. Set up CI & Git

2. Analyze● Pylint, flake8 or other static code analyzers, but don't take it too much

3. Identify● Set up priority, do it one feature at a time

4. Test● Write more proper test before modifying feature

5. Fix● Optimize, improve and make it better

6. Clean● Fix weak warning from code analyzers to reduce “noise” alert

7. Repeat 2 – 6 & don't forget the meaningful documentation in each steps

Page 12: Python id meetup, Maintaining a Dirty Code Django Project

My Fourth Step (Database)

To optimize django ORM, do this after upgrade:

1.Check default value between model attribute & database field– Not just data type, but also the implementation of class

Meta

2.Use backup database to compare models & database

3.Update database to models– Use django_extension

Page 13: Python id meetup, Maintaining a Dirty Code Django Project

Benefit of Upgrade

1.Bug fixed

2.Increase performance

3.Increase security

4.Built in feature

5.Ease modification

6.Stand by for scaling project/business

Page 14: Python id meetup, Maintaining a Dirty Code Django Project

CMIIW

Any ideas for me to handle dirty code better?

Page 15: Python id meetup, Maintaining a Dirty Code Django Project

Lesson Learned

1.Document your project business plan, the code is written based-on this.

2.Document your code to be read by human, not machine.

3.Follow style guide, make other developer easier to join the development

4.Don't rush

5.Test as much as you can

Page 16: Python id meetup, Maintaining a Dirty Code Django Project

Splitting Django Models

1.Create empty package named models

2.Move models.py to models package, renamed it to models_legacy.py

3.Create models_common.py

1.This module can not import from other modules

2.Only import from built-in models

4.Create other modules as necessary

5.Add import * from all modules in models/__init__.py

6.Change import in number 5 to be more explicit if possible