# Tip Code Faster

In the field of application development, speeding up the coding process not only saves time but also enhances work efficiency and product quality. Coding faster does not mean reducing code quality. Here, I will present the methods I use to make coding large Flutter applications easier while also facilitating maintenance and debugging.

To build large projects, writing a huge amount of code is inevitable. Instead of trying to reduce the workload unrealistically, you should focus on reducing coding time.

## Using Mason

Mason is a powerful tool that automates source code generation. It allows you to create reusable code templates, helping to reduce the time spent writing repetitive code segments. By setting up code bricks (templates), you can quickly generate components like widgets, screens, or even an entire project structure with just a few simple commands.

Mason does not only generate code for Dart; you can also use it with other languages. Refer to my article about Mason to understand how it works and its applications in my work.

{% content-ref url="mason" %}
[mason](https://wong-coupon.gitbook.io/flutter/easy-code/mason)
{% endcontent-ref %}

## Using Make

When a Flutter project grows, we run many terminal commands again and again.\
My idea is to use `Makefile` to group them into short commands like `make init`, `make get`, and `make push`.

This helps reduce confusion, saves time, and is easier to teach for new teammates.

If you want to see my full guide, check this article:

{% content-ref url="make" %}
[make](https://wong-coupon.gitbook.io/flutter/easy-code/make)
{% endcontent-ref %}

## Code Generation Libraries

Libraries like json\_serializable and build\_runner allow you to automatically generate code from objects, which is especially useful when working with JSON. Instead of manually writing `toJson` and `fromJson` methods, you only need to define your model, and the library will handle the rest. This not only saves time but also reduces human errors.

If you know how to combine it with Mason, you can create objects with full functionality you need, such as `copyWith` or equality comparison. You can refer to my two code samples at <https://brickhub.dev/bricks/dr\\_entity> and <https://brickhub.dev/bricks/dr\\_freezed>.

## Code Generation for Assets - `flutter_gen_runner`

flutter\_gen\_runner helps automatically generate code for resources like images, fonts, and strings. Instead of remembering and typing the exact paths to each resource, you can safely and quickly access them through pre-generated variables. This helps minimize errors and speeds up the coding process. You can learn more in my article about creating a UI package here:

{% content-ref url="../ui/core-ui" %}
[core-ui](https://wong-coupon.gitbook.io/flutter/ui/core-ui)
{% endcontent-ref %}

## Using Shortcuts and Live Templates

### Using Shortcuts

Below are the shortcuts I often use in Android Studio. Using these shortcuts makes programming feel a bit easier:

**Normal**

| Window            | Macos                 | Describe                                                                                                                                                                                                        |
| ----------------- | --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Shift+F10         | Control+R             | Run                                                                                                                                                                                                             |
| Shift+F9          | Control+D             | Debug                                                                                                                                                                                                           |
| Control+Space     | **Control+Space**     | Displays basic suggestions for variables, types, methods, expressions, and so on. If you call basic completion twice in a row, you see more results, including private members and non-imported static members. |
| Control+Tab       | Control+Tab           | Switch between tabs and tool window                                                                                                                                                                             |
| Press Shift twice | **Press Shift twice** | Search everything (including code and menus)                                                                                                                                                                    |
| Control+F         | Command+F             | Find                                                                                                                                                                                                            |
| F3                | Command+G             | Find next                                                                                                                                                                                                       |
| Shift+F3          | Command+Shift+G       | Find previous                                                                                                                                                                                                   |
| Control+R         | Command+R             | Replace                                                                                                                                                                                                         |
| Control+Shift+F   | Command+Shift+F       | Find in path                                                                                                                                                                                                    |
| Control+F12       | Command+F12           | Open file structure dialog                                                                                                                                                                                      |
| Shift+F4          | Shift+F4              | Open current editor tab in new window                                                                                                                                                                           |
| Control+E         | Command+E             | Recently opened files dialog                                                                                                                                                                                    |
| Control+Shift+E   | **Command+Shift+E**   | Recently edited files dialog                                                                                                                                                                                    |
| Control+G         | Command+L             | Go to line                                                                                                                                                                                                      |

**Write code**

| Window              | Macos                     | Describe                                                                                     |
| ------------------- | ------------------------- | -------------------------------------------------------------------------------------------- |
| Alt+Insert          | Command+N                 | Generate code (getters, setters, constructors,hashCode/equals,toString, new file, new class) |
| Control+O           | Control+O                 | Override methods                                                                             |
| Control+I           | Control+I                 | Implement methods                                                                            |
| Control+Alt+T       | **Command+Option+T**      | Surround with (if...else,try...catch, etc.)                                                  |
| Control+Y           | Command+Delete            | Delete line at caret                                                                         |
| Control+D           | Command+D                 | Duplicate current line or selection                                                          |
| Control+Space       | Control+Space             | Basic code completion                                                                        |
| Control+Shift+Space | Control+Shift+Space       | Smart code completion (filters the list of methods and variables by expected type)           |
| Control+Q           | Control+J                 | Quick documentation lookup                                                                   |
| F11                 | F3                        | Toggle bookmark                                                                              |
| Control+F11         | Option+F3                 | Toggle bookmark with mnemonic                                                                |
| Control+/           | Command+/                 | Comment/uncomment with line comment                                                          |
| Control+Shift+/     | Command+Shift+/           | Comment/uncomment with block comment                                                         |
| Control+W           | Option+Up -> **Option+W** | Select successively increasing code blocks                                                   |
| Control+Shift+W     | Option+Down               | Decrease current selection to previous state                                                 |
| Control+Alt+L       | Command+Option+L          | Reformat code                                                                                |
| Control+Alt+I       | Control+Option+I          | Auto-indent lines                                                                            |
| Control+Shift+J     | Control+Shift+J           | Smart line join                                                                              |
| Control+Enter       | Command+Enter             | Smart line split                                                                             |
| Shift+Enter         | Shift+Enter               | Start new line                                                                               |
| F2 or Shift+F2      | **F2 or Shift+F2**        | Next/previous highlighted error                                                              |

**Debug**

| Window   | Macos     | Describe  |
| -------- | --------- | --------- |
| Shift+F9 | Control+D | Debug     |
| F8       | F8        | Step over |

**Refecter**

| Window        | Macos                | Describe         |
| ------------- | -------------------- | ---------------- |
| Shift+F6      | Shift+F6             | Rename           |
| Control+Alt+V | **Command+Option+V** | Extract variable |

**Git**

| Window            | Macos     | Describe        |
| ----------------- | --------- | --------------- |
| Alt+\` (backtick) | Control+V | Open VCS dialog |

Using these shortcuts will help you minimize typing time and focus more on the application's code logic. You can learn more at Learn more

### Using Live Templates

You may be familiar with `stfull` or `stless` in Flutter to create Stateful and Stateless widgets. You can also create your own code templates by creating live templates in your Android Studio IDE. Go to File > Settings > Editor > Live Templates in Android Studio to try creating one for yourself.

## Applying AI

AI has become extremely useful for developers, significantly reducing the workload. Here are some applications of AI in my daily work:

* **Language Translation**: Refer to my article on using multiple languages below.
* **Creating Basic Functions Using Algorithms**
* **Optimizing Code Functions with Better Algorithms**
* **Generating Automatic Code Functions**

{% content-ref url="../ui/multi-language" %}
[multi-language](https://wong-coupon.gitbook.io/flutter/ui/multi-language)
{% endcontent-ref %}

I built the website [https://mymoneycoupon.web.app/en/doc/help/flutter ](https://mymoneycoupon.web.app/en/doc/help/flutter)using AI. It helped me quickly and efficiently write functions to retrieve data from Firebase, saving a substantial amount of research time. AI has tremendously reduced the time spent on learning and setup.

## Creating Buttons for Faster Coding

During development, some features take a lot of time to complete or test. By creating buttons or shortcuts to skip or simulate the results of these features, you can easily test the rest of the application without interruption. Previously, I developed a Bluetooth communication application where waiting for the returned results was time-consuming. I created buttons in the development environment to simulate receiving the results from the device via Bluetooth. This helped me significantly reduce the time spent on development. If you want to know how to create development, staging, and production environments, please refer to my article here:

{% content-ref url="../architecture-code/base-project" %}
[base-project](https://wong-coupon.gitbook.io/flutter/architecture-code/base-project)
{% endcontent-ref %}

<figure><img src="https://2074953091-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F63bX4jdEctx3wiLiur0x%2Fuploads%2Fgit-blob-d09c8210df3b5fdf2d12c605c242d3449c41d773%2Fimage1%20(3).png?alt=media" alt=""><figcaption><p>log and button test</p></figcaption></figure>

One idea I find useful is displaying logs on the screen using these test buttons. This is effective when your device is not in debug mode, or backend developers can use the app and check logs without needing you to debug to see the logs. This helps reduce your workload. However, this will be more effective on larger screens like tablets. If you want to create color-coded logs like this, check out my article here:

{% content-ref url="log-color" %}
[log-color](https://wong-coupon.gitbook.io/flutter/easy-code/log-color)
{% endcontent-ref %}

These are the methods I use to code faster and make coding easier. If you have your own great methods, please share and contribute to the community.

[Buy Me a Coffee](https://buymeacoffee.com/ducmng12g) | [Support Me on Ko-fi](https://ko-fi.com/I2I81AEJG8)
