We can draw parallels between code features and glacial features.
All software has a shape. I call this a codescape.
Some code is easy to follow, some is rugged and complex.
New hardware 🎮, new frameworks 🏗️, new languages 🐍, new tools 🗜️.
The promised land of bug-free, performant, easy to modify, unit-tested code doesn't exist.
There are magics sprinkled in the code and deployment toolchain.
Even cosmic rays cause problems.
Software projects have an evolving codescape.
Bugs 🐛 Backwards compatibility. Database cruft. Relics. Business priorities and community pressure. Team churn.
My head hurts.
During this phase, we'll act like glacial detectives, looking for clues in the structure of our code.
Our goal? To gradually flatten and streamline the code, smoothing out those rugged 'code mountains' we encounter.
The first step in glacial refactoring is understanding our terrain. We'll look for and identify features in our codescape that have emerged over time.
from db.manager import db_connection, dsdb
class GlacierAnalysisTool:
def __init__(self, db=db_connection): ...
def analyze_ice_thickness(self): ...
def map_glacial_retreat(self): ...
class MoraineDataProcessor(GlacierAnalysisTool):
def aggregate_debris_data(self): ...
def calculate_moraine_age(self): ...
def import_initial_core_analysis(self, path): ...
def legacy_data_cleanup(self): ...
def add_core_analysis(self, core_id, url=CORE_API, provider="USGS"): ...
from db.manager import db_connection, dsdb
class GlacierAnalysisTool:
def __init__(self, db=dsdb): ...
def analyze_ice_thickness(self): ...
def map_glacial_retreat(self): ...
class MoraineDataProcessor(GlacierAnalysisTool):
def aggregate_debris_data(self): ...
def calculate_moraine_age(self): ...
def import_initial_core_analysis(self, path): ...
def legacy_data_cleanup(self): ...
def add_core_analysis(self, core_id, url=CORE_API, provider="USGS"): ...
from db.manager import db_connection, dsdb
# GlacialNote: dsdb appears to be a data science database that we don't have access to.
class GlacierAnalysisTool:
def __init__(self, db=dsdb): ...
def analyze_ice_thickness(self): ...
def map_glacial_retreat(self): ...
class MoraineDataProcessor(GlacierAnalysisTool):
__glacial_note__ = "Flatten: Pull base up, this is the only use of GlacierAnalysisTool."
def aggregate_debris_data(self): ...
def calculate_moraine_age(self): ...
def import_initial_core_analysis(self, path):
# GlacialNote: Appears to be unused, no refs in current code
# Investigate: Git history for reference.
...
def legacy_data_cleanup(self): ...
def add_core_analysis(self, core_id, url=CORE_API, provider="USGS"):
# GlacialNote: Investigate: if CORE_API has other providers
# or if its is only ever USGS.
...
A 'Code Mountain' is an unusually deeply nested code formation.
One of the most intimidating naturally occuring code formations to work with. Usually full of Code Moraines and Code Erratics.
for slide in presentation:
if slide.is_have_content():
for elemento in slide.elements:
if elemento.is_textual():
for parrafo in elemento.parrafos:
if parrafo.is_have_special_format():
if parrafo.is_bold():
elemento.apply_bold(parrafo)
elif parrafo.is_italic():
elemento.apply_italic(parrafo)
else:
if sys.getenv("FF_20090101_OPS-2319"):
sys.exit("Unsupported format - 不适合")
continue
else:
parrafo.normalize_format()
else:
for img in elemento.media:
if img.is_fit(slide):
if media_utils.needs_light_adjustment(img):
slide.add_img(img, bg=(255,255,255))
else:
raise NotImplementedError('फिट नहीं होता')
elif img.need_resize():
img.resize_for(slide)
else:
raise ValueError("Img not match - 图片不匹配")
else:
elemento.handle_no_media()
else:
slide.alert_no_elementos()
else:
logging.error("Slide is empty")
for slide in presentation:
if slide.is_have_content():
for elemento in slide.elements:
if elemento.is_textual():
for parrafo in elemento.parrafos:
if parrafo.is_have_special_format():
if parrafo.is_bold():
elemento.apply_bold(parrafo)
elif parrafo.is_italic():
elemento.apply_italic(parrafo)
else:
# GlacialNote: Flag no longer set, JIRA issue closed in 2022
# Clue: merge commit for REL-4832 fixed in presentation UI.
# Action: Remove
if sys.getenv("FF_20090101_OPS-2319"):
sys.exit("Unsupported format - 不适合")
continue
else:
parrafo.normalize_format()
else:
# GlacialNote: Good spot to flatten or extract to functions
for img in elemento.media:
if img.is_fit(slide):
if media_utils.needs_light_adjustment(img):
slide.add_img(img, bg=(255,255,255))
else:
raise NotImplementedError('फिट नहीं होता')
elif img.need_resize():
img.resize_for(slide)
else:
raise ValueError("Img not match - 图片不匹配")
else:
elemento.handle_no_media()
else: # GlacialNote: validation, flip if-else
slide.alert_no_elementos()
else: # GlacialNote: validation, flip if-else
logging.error("Slide is empty")
With our intent committed, we can now make fixes, one at a time. One example is Code Erratics..
for slide in presentation:
if slide.is_have_content():
for elemento in slide.elements:
if elemento.is_textual():
for parrafo in elemento.parrafos:
if parrafo.is_have_special_format():
if parrafo.is_bold():
elemento.apply_bold(parrafo)
elif parrafo.is_italic():
elemento.apply_italic(parrafo)
else:
# GlacialNote: Flag no longer set, JIRA issue closed in 2022
# Clue: merge commit for REL-4832 fixed in presentation UI.
# Action: Remove
if sys.getenv("FF_20090101_OPS-2319"):
sys.exit("Unsupported format - 不适合")
continue
else:
parrafo.normalize_format()
else:
# GlacialNote: Good spot to flatten or extract to functions
for img in elemento.media:
if img.is_fit(slide):
if media_utils.needs_light_adjustment(img):
slide.add_img(img, bg=(255,255,255))
else:
raise NotImplementedError('फिट नहीं होता')
elif img.need_resize():
img.resize_for(slide)
else:
raise ValueError("Img not match - 图片不匹配")
else:
elemento.handle_no_media()
else: # GlacialNote: validation, flip if-else
slide.alert_no_elementos()
else: # GlacialNote: validation, flip if-else
logging.error("Slide is empty")
Add Glacial Notes as needed.
for slide in presentation:
if slide.is_have_content():
for elemento in slide.elements:
if elemento.is_textual():
for parrafo in elemento.parrafos:
if parrafo.is_have_special_format():
if parrafo.is_bold():
elemento.apply_bold(parrafo)
elif parrafo.is_italic():
elemento.apply_italic(parrafo)
continue # GlacialNote: Appears to be unnecessary.
else:
parrafo.normalize_format()
else:
# GlacialNote: Good spot to flatten or extract to functions
for img in elemento.media:
if img.is_fit(slide):
if media_utils.needs_light_adjustment(img):
slide.add_img(img, bg=(255,255,255))
else:
raise NotImplementedError('फिट नहीं होता')
elif img.need_resize():
img.resize_for(slide)
else:
raise ValueError("Img not match - 图片不匹配")
else:
elemento.handle_no_media()
else: # GlacialNote: validation, flip if-else
slide.alert_no_elementos()
else: # GlacialNote: validation, flip if-else
logging.error("Slide is empty")
Flipping if-else blocks is a powerful way to evaluate and flatten code.
for slide in presentation:
if slide.is_have_content():
for elemento in slide.elements:
if elemento.is_textual():
for parrafo in elemento.parrafos:
# GlacialNote: if-else main code path second
if parrafo.is_have_special_format():
if parrafo.is_bold():
elemento.apply_bold(parrafo)
elif parrafo.is_italic():
elemento.apply_italic(parrafo)
continue # GlacialNote: Appears to be unnecessary.
else:
parrafo.normalize_format()
Here we negate the if
and swap the code before and afer else
. The normal main code path is now first.
for slide in presentation:
if slide.is_have_content():
for elemento in slide.elements:
if elemento.is_textual():
for parrafo in elemento.parrafos:
if not parrafo.is_have_special_format():
parrafo.normalize_format()
else:
if parrafo.is_bold():
elemento.apply_bold(parrafo)
elif parrafo.is_italic():
elemento.apply_italic(parrafo)
continue # GlacialNote: Appears to be unnecessary.
Now we can use continue
to tell the else to take a hike and peel off a layer.
for slide in presentation:
if slide.is_have_content():
for elemento in slide.elements:
if elemento.is_textual():
for parrafo in elemento.parrafos:
if not parrafo.is_have_special_format():
parrafo.normalize_format()
continue
if parrafo.is_bold():
elemento.apply_bold(parrafo)
elif parrafo.is_italic():
elemento.apply_italic(parrafo)
continue # GlacialNote: Appears to be unnecessary.
Evolutionary Codescaping is envisioned as an integral part of software development life-cycles.
Join me in shaping this methodology.
Thank you for joining me on this exploration of Glacial Refactoring.
Here's ways to reach out to me.
rose@rosehooper.com | |
Web | rosehooper.com |
Mastodon | @krayola@mastodon.social |
Github | github.com/rhooper |
linkedin.com/in/rosehooper | |
Discord | PyOhio https://www.pyohio.org/2023/discord/ |