Skip to main content

How to Check Format of Your C++ Code

As a concern of a group work in development, understandability and standards have a significant place. For the purposes of suitability, each work done by different team members should have a coherence. This behavior avoids different code writings which may confuse developers. It is also a control mechanism against unexpected weird code blocks. Maybe someone in your team has an interest to write his code squeezed in a square block :)

The team is coding in c++. They agree on a coding standard. Each member will check their code before committing. For this purpose, I have discovered clang-format tool which provides easy and quick formatting. Its integration to CLion is also simple. However, if you would like to do a double check before merging a code, there is more to do. 

Continuous integration is an option to check whether the committed code is in desired format or not. If it does not suit, then CI job fails. At the very beginning, related job is started. If it fails, it will force the committer to correct the code. Otherwise, the work will be ignored. Currently, I could not find a complete and mature solution which provides this functionality. clang-format only gives the formatted version of the given input. But it does not have a verification functionality. However, it is elegant, light-weight and effective in formatting. Therefore, I have written a bash script (see the code block below) to achieve this goal by using clang-format. It recursively traverses all files in a directory whose path is passed as an argument. Names of files which violate the standards are printed out. If such files exist, the script returns 1 which is a fail state. In this case, the CI job fails. If all files satisfy the standards, the script successfully concludes. Then, upcoming CI jobs, if exist, start. Alternatively, the script can be run locally to see which files need an update.

Each member can check their code by using clang-format locally. For the coherence in a group work, an extra check may be necessary. It depends on the team's concern. Free to use this code block if you think it is useful for you. If you think there is a better way or if you have an idea to improve this way further, please drop a comment. Happy coding.

Comments

Popular posts from this blog

Integration of MuPDF Project as a Library into an Android Studio Project

I have needed to use MuPDF library in my android project. After some research, I have seen that there are many integration tutorials but, but integrated projects are developed on Eclipse. For projects on AndroidStudio+Gradle, there is no example. I mean there is no specific example which exactly refers to this issue. So, after achieving my goal, I want to share the steps publicly so that it can be reused by others.

Migration from Proxmox to Openstack

I needed to migrate virtual machines in proxmox to openstack. VMs are in raw format. I needed to take some actions for a succesfull migration. I have perform all actions on Ubuntu 12.04 with virt-manager. qemu-kvm is installed. Here is the list of actions that I took: First, close the machine and copy the image file into your Ubuntu. Convert raw image to qcow2 format: qemu-img convert -O qcow2 image1.raw image1.qcow2 You need the image in qcow2 format for compatibility with openstack platform.  Open the converted image in virt-manager. Before opening, edit disk options. Under ' advanced options ' section, select ' qcow2 ' as ' storage forma t '. Start the virtual machine. You should see the login screen soon. (If you don't set storage format, vm will not find a bootable device. )   If everything is ok so far, close the vm. Take qcow2 image and upload it into glance. It may take time depending on size of it. After this process is completed, open a...

How to avoid API-level warning of Android Studio

Before giving the solution, let's start with a scenario. setSelectionFromTop() is a new method in Android Lollipop API. This method is basically beneficial to precisely keep scroll state of a ListView. By keeping that info, a developer can go back to old scroll state after doing some operation like data set change. You are aware of API level and you do your control before you call this function: if (currentapiVersion >= Build.VERSION_CODES.LOLLIPOP) { srlistview.setSelectionFromTop(index, top); } But if project minSdk is set to a lower level (in this case it is 15), this warning will still be displayed in Android Studio: