Mobile Development 11 min read

Automated OC Code Refactoring Practice (Part 3): Data Item Read/Write Class Encapsulation and Call Site Adaptation

The third part of the series shows how Python scripts can automatically refactor Objective‑C code by encapsulating each data item’s read/write operations in static wrapper classes and rewriting over 600 call sites to use generated XXXSettingReader and XXXSettingWriter methods, achieving zero bugs.

Baidu Geek Talk
Baidu Geek Talk
Baidu Geek Talk
Automated OC Code Refactoring Practice (Part 3): Data Item Read/Write Class Encapsulation and Call Site Adaptation

This article is the third part of a series on using Python scripts to automate Objective-C code refactoring. It explains how to upgrade direct data item read/write operations to indirect access through a data channel.

Data Item Read/Write Class Encapsulation: To reduce refactoring costs, a wrapper class is created in the data item usage module. Each data item's read/write operation is implemented as a static function. The encapsulation rules are: 1) For read operations, the function return type, function name, and data item must match (e.g., NSString *value1 becomes +(NSString *)value1); 2) For update operations, the function follows the pattern set_ + function name (e.g., +(void)set_value1:(NSString *)value). The generated files use XXXSettingReader and XXXSettingWriter as class name prefixes.

Data Item Usage Module Call Site Adaptation: When the data provider module supports data channel access, the data item usage module needs adaptation. This includes two parts: 1) Data item update call site adaptation - converting patterns like [XXXSetting share].value1 = @"str" to [XXXSettingWriter set_value1:@"str"]; 2) Data item read call site adaptation - converting [XXXSetting share].value1 to [XXXSettingReader value1]. The implementation uses Python with regex-based string matching and replacement, supporting whole-word matching to handle cases where data items might be substrings of each other.

The automation approach achieved zero bugs in testing and deployment phases for this refactoring work involving over 600 call sites.

Pythonautomationsoftware engineeringcode refactoringiOS DevelopmentscriptingObjective-CData Channel
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

login 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.