2.3.2. Code Style


This chapter is a continuation of Code Formatting

Tabs or Spaces? Or Both? Indent with 2 spaces? 3? 4? Or 8 spaces? Do these questions make any sense to you? If yes, you would be lucky to have Eclipse. If not — pity on your peer developers.

Most open source projects have their own strict coding conventions. [1] The feature of Code Style within Eclipse can make this task very easy for developers. Eclipse also supports profiles for using different coding styles. (Or share code style within in Organization.) Profiles

A profile is a set of rules for Code Style/formatting.

By default, Eclipse has the following profiles.

  • GNU
  • K&R
  • BSD/Allman
  • Whitesmits

You can also create/share your own profile. For e.g. let’s take [BSD/Allman] profile, and extend it.

Go to Window → Preferences → C/C++ → Code Style. You will get a list of Code style profiles


Code style profiles

Click on New and select the base profile.


New Code Style

Extend the profile and save it. Now we have profile Meera that is almost same as BSD/Allman but uses SPACES instead of TABS.


Editing Code Style

You can see the effect of your settings in the right area of the dialogue box. This will give you a hint on changing the style. The Eclipse code style formatter is much more extendible. Share/Export/Import Profiles

The profiles can be shared across projects/developers.

To export code profile, use the Export in Editing Code Style dialogue. To import, use the Import in Code style profiles of a Project [2] or Workspace [3].

[1]Linux kernel mandates 8 spaces of indentation.
[2]Project → Properties → C/C++ General → Code Style.
[3]Window → Preferences → C/C++ → Code Style