Mobile Development 16 min read

How Python Automates iOS Code Refactoring: A Step‑by‑Step Guide

This article explains how to use Python scripts to automate the analysis and refactoring of iOS configuration data items in Baidu App, detailing the extraction of public properties, usage detection across modules, CSV reporting, and the overall refactoring workflow to improve maintainability and reduce risk.

Baidu Geek Talk
Baidu Geek Talk
Baidu Geek Talk
How Python Automates iOS Code Refactoring: A Step‑by‑Step Guide

Background

In software development, complex logic, tangled dependencies, redundant code, and unclear naming reduce maintainability and increase cost. Refactoring improves readability and stability without changing behavior.

Case Study: Baidu App iOS Search Configuration

Configuration items are centralized in the XXXSetting class and are used by over 30 components. Direct dependencies cause interface changes to ripple across the system. The goal is to migrate these items to a “data channel” to decouple components, improve stability, and lower impact of changes.

Refactoring Process

Familiarize with business and technical status : understand logic, identify problematic code.

Define refactoring plan : decide how to rewrite or adapt dependent code.

Phase implementation : modify code in stages, run tests, avoid affecting unrelated modules.

Effect evaluation and monitoring : compare pre‑ and post‑refactor behavior, collect metrics.

Python Automation Tool

The tool automates the most labor‑intensive steps of the refactoring workflow.

1. Extract public properties from XXXSetting

Parse Objective‑C header files, remove comments, and use regular expressions to capture @property declarations, clean asterisks, and obtain type‑name pairs.

@property (nonatomic, assign) BOOL value;</code><code>@property (nonatomic, copy) NSString *value1

2. Find usage of each property

Build a global dictionary valueCallInfoDic that maps each property name to a map of file → call count. For each source file, construct a regex‑escaped lookup string like [XXXSetting share].value1 and count matches.

# Example snippet
valueCallInfoDic = {}
realValueName = '[XXXSetting share].' + valueName
for fileName in fileNameList:
    callNum = 0
    for line in f:
        if re.match(r'.*' + regAbKey, line):
            callNum += 1
    if callNum > 0:
        fileCallInfoDic[fileName] = str(callNum)
    if len(fileCallInfoDic):
        valueCallInfoDic[valueName] = fileCallInfoDic

3. Export results to CSV

Two CSV files are generated:

Detailed usage: value , uselib , usefile , usenum Aggregated usage type: value , uselib , usenum , usetype The script determines four usage categories based on where the property is accessed:

selfCall : used only inside XXXSetting.

otherCall : not used in XXXSetting but used in a single external module.

selfAndOtherCall : used both inside and outside XXXSetting.

othersCall : used in multiple external modules without internal usage.

Conclusion

The automated analysis provides precise statistics of each configuration item’s exposure, enabling risk‑aware refactoring and reducing manual effort and errors. Future articles will cover the implementation of the data channel and its integration.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

iOSPythonautomationCode Refactoringstatic analysisCSV exportconfiguration data
Baidu Geek Talk
Written by

Baidu Geek Talk

Follow us to discover more Baidu tech insights.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.