How to Perform Code Review Process for Magento 2 Effortlessly


This post was written by Bitbucket user and Plumrocket Developer Ihor Kalaida.


Software development helps company owners of different sizes automate business processes and increase productivity with less effort. In order to write the software efficiently without any issues, the code of your software can be reviewed by the developers, as well as added to and improved if necessary. Let's take a closer look at the code review process and how to use the version-control systems to make the process more efficient. 

Why Consider a Code Review Process? 

Code review process has a number of benefits for a project or a team of any size. 

First, it allows you to minimize the number of mistakes and limit risks. This is the most common reason why code review is necessary, but also is the most important. When another person checks your code, it helps to find bugs and fix errors even before going to QA stage. 

Additionally, the code peer review process helps to dramatically improve your code quality. This includes the same code style across the whole project, making it easy to read and understand each others code and suggest better design patterns to improve code performance.

By reviewing someone else’s code, developers can also improve their programming skills and become mentors at the same time. The code review process magically works both ways, it helps the reviewer and the reviewee. If the reviewer is a senior level developer, he can learn how to work with junior team members by mentoring them. And vice versa, by reviewing your peers code, you can always learn something new, interesting ways of solving problems or special programming techniques.

Lastly, another set of eyes can help to find out if the project requirements are fulfilled. It should come as no surprise, that any person may miss a very important detail in the project requirement and therefore a completely different results can be achieved. 

Fortunately, the developer community realized the extent of the problem and a need for one centralized software that can help run the big development projects in a simple and effective way and perform code reviews with ease. That was the time of the appearance of version-control programs like Git, CVS, Subversion, Bazaar, Monotone, Aegis, etc. 

When working with Git, after you finish your task, you create a pull request and assign the task to your team member for a code review. Once your coworker finds any issues, he will add a comment on the pull request and assign it back to you to fix it.

With that being said, continue reading to learn the main aspects of version-control systems (VCS), as well as finding out how Plumrocket uses Git repository to perform code review of their Magento 2 projects. 

What is a Version-Control System? 

Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. It allows you to revert selected files back to a previous state, revert the entire project back to a previous state, compare changes over time, see who last modified something that might be causing a problem, who introduced an issue and when, and more. To put it simply, VCS generally means that if you screw things up or lose files, you can easily recover. Hence, consider the following top 3 reasons for using a version-control system: 

#1: Saves all stages of development and rolls back to previous versions.

After making changes to one or more project files, the developer writes down all changes to the repository – the archive of all project versions and updates. Note, the repository records only modified files to save space and time for saving changes. If the chosen direction in the project development is wrong or contains errors, then version control systems allow you to solve the problem. It can return the developed software to one of the latest working versions by copying the required software version or individual files from the repository.

#2: Merges the code modifications and resolves the conflicts.

Often, several developers simultaneously modify the same files. If the changes do not intersect, version control systems make it easy and simple to combine these changes. If several employees change the same part of the code, merging such changes automatically is not possible. Usually, version-control systems provide you with the tools that allow you to manually merge the conflicted code pieces. 

#3: Supports several directions of project development.

There are cases when it is not possible to save the changes right away. It often takes a long time to develop and debug individual edits before they can be combined with the production code. In this case, version control systems allow you to organize parallel branches to control multiple methods of software project development, quickly switch between them, and merge them with production after that.

As you can see, a VCS can simplify the process of software development and help you with team collaboration. Also, it is worth mentioning the different types of version-control systems, so consider the following ways of managing the software development process: 

Centralized control systems

In the case where your project runs through the centralized control system, each member of the team can download the latest version of the code, make changes and upload the results to the server. All the modifications are done with the help of the latest version of the code. Once you need to come back to the old version, it is required to download the necessary version from the server.  

Distributed control systems 

With the distributed systems like Git, users don't just check out the latest snapshot of the files; rather, they fully mirror the repository, including its full history. In this case, if any server dies, and these users were collaborating via that server, any of the user's repositories can be copied back up to the server to restore it. Every clone is really a full backup of all the data.

Now, after you got acquainted with version-control systems, let's take Git VCS as an example and discover the vital points on how to perform the code review process. 

How to Install Git on Bitbucket for Magento 2

In order to set up Git on Bitbucket for Magento 2 platform, create a new repository (for instance "Magento 2") and consider the following steps: 

1. Go to the folder with Magento 2 files in the console. 

2. Run the command 'git init' to create the empty Git repository. Then, run the command 'git status' to see a list of untracked files of our project. 

3. Add the files to the repository by running the command 'git add'. After that, you need to commit the added files with the help of the command: 'git commit -m "Initial commit.'

Note, Magento includes the default .gitignore file that has already included the description of all rules for file ignoring. In this case, the repository will receive only the files that can be edited or automatically generated, as well as cache files, etc. 

4. The next step is to connect to our local repository with the remote one that we created on the Bitbucket page. So, if you download Magento from your local repository, you can run the command in the following format: 

git remote add origin git@bitbucket.org:<user-name>/<repo-name>.git

In our case, we will use the next command:

git remote add origin https://ihorkalaida@bitbucket.org/ihorkalaida/magento2.git

After that, push the local changes to the remote repository by setting up the command. 

git push -u origin master 

5. In order to check if the process went the right way, go to the Bitbucket interface and check whether our project is there. 

Magento 2 Code Review Process in Bitbucket: Best Practices

The Plumrocket team reviews customer code on a daily basis. Whether it's a Magento 2 custom development project where you need to add a feature, a Magento 2 extension that conflicts with other plugin or a speed optimization project where the old code has a lot of unnecessary loops and other issues. You need to be able to perform these actions in order to solve issues on a larger scale.

Imagine the situation that your project consists of 2 branches: master and feature/test. We want to merge the new changes from the branch feature/test into master and make changes to the project. Now, let's look closer at the main aspects of the process:

  • Before merging the changes, it is recommended to check whether the code quality fits the requirements and does not violate the existing functions. Then, you should create a 'pull request' for the branch we want to merge with the master branch. 
  • After the 'pull request' is created, you can see the detailed information about it: author, a list of changed files, description, comments, and so on. 
  • If you click on the tab 'Commits', you can check all the comments that have been included in this branch.

  • Also, if you go to the tab 'Activity', you can look through all activities that have been done within this branch. 
  • If you click on the separate commit in the Activity tab, you will receive detailed information about it, and what changes have been added in this particular commit.
  • Also, you can easily approve the commit on this commit page. In the code review process, you get an opportunity to switch to the separate file in "Side-by-side diff."
  • The code review process enables you to leave comments about the code which might have some issues or misunderstandings. The developer who wrote the code will get the notification about the someone's feedback and can leave the explanation to the problem. 
  • Once all issues have been resolved, the code reviewer can merge the changes and close the "pull request".
  • Finally, all changes can be reviewed in the tree of commits.

Code Review: More Benefits to Consider

Now, when you have got acquainted with the main aspects, and implementation steps of the code review process, there are a few more things to be mentioned about its advantages. Take into account the tasks that are solved with code review: 

  1. Get efficient team collaboration. Code review is a great way to help your employees collaborate with each other and build a friendly work environment.  Conducting reviews on development can ensure that your whole team understands the end goals of a project. 
  2. Educate yourself. The procedure enables the users to always learn new information from the colleagues’ reviews. Also, each team member can analyze the different parts of the code that offers them to go beyond their particular responsibilities. In addition, the more experienced employees can become mentors and train others. 
  3. Create a high-quality code. The code review procedure allows you to build the style of writing the code for an individual team and keep multiple developers during different development phases on the same page. 

Wrapping Up 

Code review is an important aspect in the software development process that includes a number of benefits. Software engineers enjoy using Jira for issue tracking and collaboration while utilizing the in-line commenting, discussions and merging code in Bitbucket. With the help of Atlassian products, you can minimize the code errors, acquire skills to boost the performance of your code, share the techniques with other developers, and so on. In other words, the procedure helps a fresh set of eyes to identify bugs and coding errors before your product gets to the next step. 


Author bio:  Ihor Kalaida has been a Magento Developer at Plumrocket since 2016. He is involved in the development of extensions, data migration, and Magento stores' optimization. He loves football and skiing, so he can often be met at local stadiums and on the slopes of the Carpathian mountains!


Love sharing your technical expertise? Learn more about the Bitbucket writing program.