Own (and Refactor) the Build

本文强调了构建脚本在软件开发过程中的重要性,并指出其维护不当会导致的问题与代码质量不佳相同严重。文章建议开发者应深入了解并负责构建过程,通过正确编写构建脚本可以简化整个开发周期,减少成本,提高代码质量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Own (and Refactor) the Build

Steve Berczuk

iT iS NOT UNCOMMON for teams that are otherwise highly disciplined about coding practices to neglect build scripts, either out of a belief that they are merely an unimportant detail or from a fear that they are complex and need to be tended to by the cult of release engineering. Unmaintainable build scripts with duplication and errors cause problems of the same magnitude as those in poorly factored code.
One rationale for why disciplined, skilled developers treat the build as some- thing secondary to their work is that build scripts are often written in a differ- ent language than source code. Another is that the build is not really “code.” These justifications fly in the face of the reality that most software developers enjoy learning new languages, and that the build is what creates executable artifacts for developers and end users to test and run. The code is useless with- out being built, and the build is what defines the component architecture of the application. The build is an essential part of the development process, and decisions about the build process can make the code and the coding simpler.
Build scripts written using the wrong idioms are difficult to maintain and, more significantly, improve. It is worth spending some time to understand the right way to make a change. Bugs can appear when an application is built with the wrong version of a dependency or when a build-time configuration is wrong.
126 97 Things Every Programmer Should Know

Traditionally, testing has been something that was always left to the “Quality Assurance” team. We now realize that testing as we code is necessary to being able to deliver value predictably. In much the same way, the build process needs to be owned by the development team.
Understanding the build can simplify the entire development lifecycle and reduce costs. A simple-to-execute build allows a new developer to get started quickly and easily. Automating configuration in the build can enable you to get consistent results when multiple people are working on a project, avoiding an “it works for me” conversation. Many build tools allow you to run reports on code quality, allowing you to sense potential problems early. By spending time understanding how to make the build yours, you can help yourself and everyone else on your team. You can focus on coding features, benefiting your stakeholders and making work more enjoyable.
Learn enough of your build process to know when and how to make changes. Build scripts are code. They are too important to be left to someone else, if for no other reason than because the application is not complete until it is built. The job of programming is not complete until we have delivered working software.

[DEBUG]: Remove directory and subdirectory src/main/assets [DEBUG]: Create directory src/main/assets Compiling '/root/android/.buildozer/android/app/sitecustomize.py'... Compiling '/root/android/.buildozer/android/app/main.py'... [DEBUG]: Remove directory and subdirectory /tmp/p4a-extra-env-w25uqkn9 [DEBUG]: Remove directory and subdirectory src/main/res [INFO]: Detected highest available build tools version to be 36.0.0 [DEBUG]: -> running gradlew clean assembleDebug [DEBUG]: [DEBUG]: > Task :processDebugMainManifest [DEBUG]: /root/android/.buildozer/android/platform/build-arm64-v8a/dists/myapp/src/main/AndroidManifest.xml:59:18-50 Warning: [DEBUG]: android:extractNativeLibs should not be specified in this source AndroidManifest.xml file. See https://d.android.com/guide/topics/manifest/application-element#extractNativeLibs for more information. [DEBUG]: The AGP Upgrade Assistant can remove the attribute from the AndroidManifest.xml file and update the build file accordingly. See https://d.android.com/studio/build/agp-upgrade-assistant for more information. [DEBUG]: [DEBUG]: > Task :compileDebugJavaWithJavac [DEBUG]: Note: Some input files use or override a deprecated API. [DEBUG]: Note: Recompile with -Xlint:deprecation for details. [DEBUG]: [DEBUG]: Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0. [DEBUG]: [DEBUG]: You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins. [DEBUG]: [DEBUG]: See https://docs.gradle.org/8.0.2/userguide/command_line_interface.html#sec:command_line_warnings [DEBUG]: [DEBUG]: BUILD SUCCESSFUL in 14s [DEBUG]: 33 actionable tasks: 33 executed [INFO]: <- directory context /root/android/.buildozer/android/platform/python-for-android [DEBUG]: All possible dists: [<Distribution: name myapp with
03-23
要避免在代码中直接使用布尔字面量,你可以采用以下几种方法: 1. **使用变量表示布尔值**: 将 `true` 或 `false` 替换为一个明确意义的变量名,这样可以提供更多的上下文信息。例如: ```javascript const isUserAdmin = true; const isValidInput = false; // 在代码中使用 isUserAdmin 和 isValidInput ``` 2. **使用条件运算符(ternary operator)**: 利用三元运算符 `condition ? valueIfTrue : valueIfFalse` 来替代简单的布尔表达式: ```javascript const isAdmin = getUserRole() === 'admin'; const displayMessage = isAdmin ? 'Welcome admin!' : 'Please log in as an admin.'; ``` 3. **使用 `if...else` 或 `switch` 语句**: 如果条件较多,可以用 `if` 语句来组织结构: ```javascript if (getUserRole() === 'admin') { console.log('Welcome admin!'); } else { console.log('Please log in as an admin.'); } ``` 4. **函数返回值**: 如果某个函数的结果是布尔值,可以改写成返回相应的值: ```javascript function isAuthorized(user) { return user.role === 'admin'; } const message = isAuthorized(user) ? 'Welcome admin!' : 'Please log in as an admin.'; ``` 5. **使用条件断言(Optional Chaining)**: 如果不确定属性是否存在,可以结合 `?.` 运算符来避免错误: ```javascript const isAdmin = (user?.role ?? '').toLowerCase() === 'admin'; ``` 这些方法都可以帮助你清晰地表达代码逻辑,而不是直接依赖于布尔字面量。记住,良好的编程习惯有助于提高代码的可读性和可维护性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值