開源日報 每天推薦一個 GitHub 優質開源項目和一篇精選英文科技或編程文章原文,堅持閱讀《開源日報》,保持每日學習的好習慣。
今日推薦開源項目:《簡單方便實惠 notable》
今日推薦英文原文:《The Art of a Well Composed Pull Request》

今日推薦開源項目:《簡單方便實惠 notable》傳送門:GitHub鏈接
推薦理由:這世界上沒有一個具有如下特點的 Markdown app: GitHub 風格,沒有 WYSIWYG(儘管這在某些時候很方便),沒有專用格式,支持附件,允許導入等等。所以我就自己造了一個。——作者
如果沒有就自己做一個適合自己的 app,這個思想是相當好的,自己動手不僅能滿足需求還能學到東西何樂而不為呢?
今日推薦英文原文:《The Art of a Well Composed Pull Request》作者:Zoe Larsen
原文鏈接:https://medium.com/@zlarsen.zl/the-art-of-a-well-composed-pull-request-3815fc7e9610
推薦理由:Pull Request 的藝術

The Art of a Well Composed Pull Request


What is a Pull Request?


A pull request (PR) is a request submitted to a GitHub repository to merge code into that project. The PR allows requested reviewers to view and discuss the proposed code. Once the PR passes all standards of reviews and all necessary revisions have been made, it can be merged into the code base.

To get an in depth overview of git and how to make commits, PRs, and repositories; here is a great guide to get you started: git — the simple guide

The purpose of this article is to present good practices for how to piece together a PR that is clear, easy to review, and inherently self-documenting.

Why are Pull Requests Important?


Pull requests are important because they help ensure that quality reviewed code is merged into GitHub repositories. Without PRs, messy and confusing code can easily run rampant in a code base. Each developers』 style of programming will compound into a repository with wildly different standards of code. This creates a confusing repository that is difficult to decipher for new developers or even the same developers six month later. PRs are a gate keeping tool to prevent bad or malicious code from being merged into the code base.

Even with this tool, it does not prevent developers from making bad PRs. Especially if there are time restraints, a complex set of commits, or a myriad of other excuses, it』s easy to lump all code changes into a single commit and push it up the repository. However, that is a terrible PR for other developers to review. A bad PR with sparse description and no forethought for how it』s structured can waste multiple developers time.

Concise pull requests allow for developers to efficiently review and quickly merge code into master. Mastering the art of a well composed PR will make you a valuable asset to any developer team.

Pull Requests are Made of Commits


A pull requests is made of a number of committed code changes. The developer then pushes the commits to a repository on github. It』s important to organize commits in a coherent manner to best convey what changes were made to the code made with each commit.

I personally try to organize my commits with some sort of flow in mind. I think it』s important to have some sort of story that helps make the abstraction of coding into something that easier to read through and understand. Here are several ways to organize commits

+ MCV flow: commit changes made to the model first, then followed by changes made to the controllers, finally changes made to the views. + Importance flow: Commit changes that are the most important first then commit associated minor changes. + Biggest to smallest flow: Commit changes that were the biggest changes to the code, followed by more minor changes.
These are just a few suggestions, there may be better ways to organize your commits that make the most sense to your team. The most important takeaway is to put some thought into it.

Thus when the commits are pushed to the GitHub repository and the pull request is made, it is easy for a PR reviewer to go through each commit and understand the thought process that necessitated the changes made in each commit.

I try to keep commit messages as concise as possible and limit the code changes to a single idea or step.

A useful git function for organizing commits is rewriting commit history.

Commit Tips


I structure my commit messages the following way

+ Title summing up the code changes I made in the present tense. + Message of why I made those changes.
Change variable name foo_bar to foo_bars

Renamed the variable foo_bar to foo_bars across all files that had that variable. Renaming this variable better conveyed that the variable was an array of strings instead of a single value.

I avoid commits that have a ton of changes across many different files unless they are all a similar change.

For example, If I made changes to a user controller file, a product subscription helper file, and product view file I would make three commits for the changes I made in each file.

Another example, if I made changes to ten view files that just changed the same variable name to a new variable then I would add all ten files to one commit.

Finally before you push up all your commits and are ready to open a new pull request, do everyone a favor and personally review your code.

Make sure you try to catch as many dumb small mistakes as you can.

Indention off? Fix it.

Variable/function names not very clear? Rename them.

DRY up your code.

You will save yourself and your reviewers time if you catch these hiccups before submitting them for review. Nothing sucks more when the senior developer comments that you misspelled organism_name as orgasm_name.

Usually I』ll commit these small changes and fixes as a personal PR review.

A Well Composed PR


Now that we have some well structure commits we can now push our changes to the GitHub repository and open a brand new PR. Composing a PR is similar to composing a commit, except it encompasses the whole purpose of the code changes instead of the small changes made to each file or feature.

PR Title


I sum up the general purpose of the PR being made in the title as concisely as I can manage. I typically have the general goal of the PR followed by the area and function

For Example:

+ New Design Bug Fix: User Controller Index Action. + System Test: React Tables Basic Functionality. + Model Bug Fix: Permission Class Method Update.

PR Body


For the PR Body Message I break it up into several sections.

+ Purpose of PR/What Issue it is Resolving + Main Changes made + Code Before Changes/Code After Changes + Additional media resources (Images, links to other PRs/Issues)
I also include other media sources if they are relevant to help explain the changes made. Things like before/after images for view changes, gifs to show functionality changes, or other media assets would be great to help illustrate what happened in the PR that affected the code base.

Additionally, I link any issues or other PR』s that are associated with the current PR being reviewed. This is especially helpful for the future when, for whatever reason, you or another developer have to look back on the changes you made.

You might have to figure out why such vague variable names were assigned, why such hacky code was implemented, or a myriad of other things that make sense now but won』t in a few months when you have completely forgotten about this pull request.

Having as much contextual information associated with the PR is extremely helpful for future developers.

The PR Conversation


Finally once you have crafted a fine PR and submitted it for review, the work is not over yet. The developers reviewing your PR will no doubt make comments on your code to clarify something or ask you to make revisions. This will start the conversation in your PR review.

I find that it is important to respond to every comment made on your PR with either a thumbs up emoji to acknowledge that you made small changes requested and with another comment if you need to explain something. Including images in your comment response when necessary is also a great way to help clarify your response.
下載開源日報APP:https://openingsource.org/2579/
加入我們:https://openingsource.org/about/join/
關注我們:https://openingsource.org/about/love/