Insights from a PR

Recently, I submitted a PR to an open-source project, and the author quickly responded, modified my code, and merged it into the main branch.

However, I gained much more than that. This PR brought me numerous insights, including the relationship between programmers and AI, the philosophy of programming, and more. In this article, I will share these insights in detail, hoping to inspire readers.

The project is a cross-device transfer tool developed using Flutter, and what I did was add the "press Esc to go back" functionality to several pages. The code to implement this feature is less than 100 lines and was entirely written with the help of ChatGPT.

The version written by GPT:

1import 'package:flutter/material.dart';
2import 'package:flutter/services.dart';
3
4class ShortcutWrapper extends StatelessWidget {
5  final Widget child;
6  final Map<LogicalKeySet, Intent> additionalShortcuts;
7
8  ShortcutWrapper({
9    required this.child,
10    this.additionalShortcuts = const {},
11  });
12
13  @override
14  Widget build(BuildContext context) {
15    final shortcuts = {
16      LogicalKeySet(LogicalKeyboardKey.escape): ActivateIntent(),
17      ...additionalShortcuts,
18    };
19
20    return Shortcuts(
21      shortcuts: shortcuts,
22      child: Actions(
23        actions: {
24          ActivateIntent: CallbackAction<ActivateIntent>(
25            onInvoke: (ActivateIntent intent) => Navigator.pop(context),
26          ),
27        },
28        child: Focus(
29          autofocus: true,
30          child: child,
31        ),
32      ),
33    );
34  }
35}

The author's modified version:

1import 'package:flutter/material.dart';
2import 'package:flutter/services.dart';
3import 'package:localsend_app/util/native/platform_check.dart';
4import 'package:routerino/routerino.dart';
5
6class ShortcutWatcher extends StatelessWidget {
7  final Widget child;
8  const ShortcutWatcher({required this.child});
9  @override
10  Widget build(BuildContext context) {
11    return Shortcuts(
12      shortcuts: {
13        // The select button on AndroidTV needs this to work
14        LogicalKeySet(LogicalKeyboardKey.select): const ActivateIntent(),
15        // Add Control+Q binding for Linux
16        // https://github.com/localsend/localsend/issues/194
17        if (checkPlatform([TargetPlatform.linux])) LogicalKeySet(LogicalKeyboardKey.control, LogicalKeyboardKey.keyQ): _ExitAppIntent(),
18
19        LogicalKeySet(LogicalKeyboardKey.escape): _PopPageIntent(),
20      },
21      child: Actions(
22        actions: {
23          _ExitAppIntent: CallbackAction(onInvoke: (_) => exit(0)),
24          _PopPageIntent: CallbackAction(onInvoke: (_) async => Navigator.of(Routerino.context).maybePop()),
25        },
26        child: child,
27      ),
28    );
29  }
30}
31
32class _ExitAppIntent extends Intent {}
33
34class _PopPageIntent extends Intent {}

Before this, I had never worked with Flutter, and with the help of GPT, I quickly developed my own product. I applied this logic to my Flutter project and submitted the PR to this repository unchanged.

The author replied that we should apply this design globally and significantly modified my code. I was shocked after seeing their modified version.

In an instant, I learned a few things:

GPT does not proactively help us consider edge cases, but rather executes precisely according to our instructions. In this PR, I only considered the exit logic for Windows and did not take into account the logic for Linux and Android TV. If I do not actively think about these situations while programming, I will only modify the code upon receiving user feedback. Considering various extreme situations should be a basic quality of a programmer.

Moreover, when the repository maintainer modified my code, they used some code organization methods I had never seen before. Without trying things out, solving bugs, or continuously adjusting the code structure, it is difficult to improve programming skills; it merely becomes a matter of completing a task.

The above situation arises because we are accustomed to thinking that the code provided by GPT is perfect.

When we get used to having GPT complete our requirements without writing code ourselves, our coding ability will decline sharply. If it is a project developed by a single person, it is even more concerning because no one points out the potential issues and areas for improvement in your code; only user feedback can help identify problems.

Therefore, we should approach the code generated by GPT with a code review mindset, rather than just "as long as it runs." You may be eager to become a prompt engineer, but I believe this is an unavoidable path to becoming a truly high-level programmer.

Feynman's words still hold true today: "Only by creating something can you truly understand it."