JuneYuan / Week2327

Created Sat, 08 Jul 2023 17:49:49 +0800
694 Words

2023 年第 27 周。

Code Complete Ch8. Defensive Programming

草记。一遍读过,很多东西是生疏的,甚至没听过。

像 Assertion, Exception, Debugging Aid (and preprocessing), 感觉是把一些程序语言的设计或经典实践,放在 Defensive Programming 语境下探讨,得到了新的视角。可以去理解为什么语言会提供这些设计,知其所以然。关于用 preprocessing 来区分开发环境和生产环境执行的代码,其实没有看太懂。

Robustness v.s Correctness, 是鱼和熊掌不可兼得的关系,要在 high level 的设计上考虑清楚,并且在实现中始终保持 consistency. 否则就会常常陷入纠结,最后的程序也很混乱。

Barricade 建议把输入检测单独做成一个模块,放在 input 和 main processing 之间,和防火墙的位置有点像。作用类似于一层防御工事。而明显的好处是,简化了 main processing 的错误处理负担。

Git submodule

From ChatGPT.

git submodule is a Git command that manages the integration of external sub-projects into your main project. A submodule is essentially a Git repository embedded within another Git repository. It allows you to keep an external project and its history separately, while still incorporating its files and changes at a specific Git commit into your main project.

Using submodules is useful when you need to include external libraries, plugins, or other projects in your codebase while maintaining their separate version control and work history. It also provides a way to keep separate teams working on the different repositories without interfering with one another.

Here’s an overview of frequently used git submodule commands:

  1. Adding a submodule: Add an external Git repository as a submodule in your project, by running:
git submodule add <repository_url> <path_to_submodule_directory>
  1. Initializing and updating submodules: After adding a submodule, initialize and fetch its contents using:
git submodule update --init --recursive
  1. Updating submodules: To update the submodule to the latest commit from its remote repository, use:
git submodule update --remote --recursive
  1. Cloning with submodules: When cloning a project that contains submodules, to pull submodule contents as well, run:
git clone --recursive <repository_url>

Or, if you have already cloned the repository, fetch individual submodules using:

git submodule update --init --recursive
  1. Removing a submodule: To remove a submodule, you should perform the following steps:

    • Delete the relevant section from the .gitmodules file.
    • Delete the relevant section from the .git/config file.
    • Run git add .gitmodules.
    • Delete the submodule’s directory from your local project folder manually.
    • Commit your changes with git commit -m "Removed submodule".

Submodules can be helpful for keeping external dependencies separate and organized, but they can add some complexity to the workflow. It’s essential to understand their proper usage and potential caveats when integrating them into your Git projects.

补充:

  1. 如果当前 repo 有多个 submodule, 而某一次只想更新其中一个,可以在上述 3. 的命令后加上 summodule path. 通过 git submodule help 也可看到用法提示。
  2. submodule 有新的 commit 后,父模块也会提示有改动,需要 commit.
  3. GitHub UI 可看父模块关联的是 submodule 的特定 commit.

把已经 track 的内容加入 gitignore

git rm --cached <file>

全局把特定的文件设为 ignore:

echo .DS_Store >> ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global

参考: https://stackoverflow.com/a/19299889