3

I am used to using tabs to indent a program. but recently I was asked to use spaces over tabs in my work place. Also I have come across different indentation styles(within the program) used by different programmers.

Is there a universally accepted indentation practice for c++(or any other language for that matter)?

Mc Missile
  • 715
  • 11
  • 25
  • 4
    No. You have stumbled into an on-going war. There will likely never be peace. – user4581301 Jul 03 '18 at 06:04
  • 4
    When in Rome.... Stick to the style that is present in the file you are editing. Don't take up arms over it. – StoryTeller - Unslander Monica Jul 03 '18 at 06:07
  • How far does your tab indent? – user1810087 Jul 03 '18 at 07:32
  • 2
    There is no universally accepted style. At most each project picks a specific style and commits the team to comply with their internal specifications. Additionally, each team can adopt an official code formatter/code beautifier, set an official config file, and commit to run the beautifier prior to each commit/merge. Here's a link to a stackoverflow discussion on source code beautifiers for C++: [Best C++ Code Formatter/Beautifier](https://stackoverflow.com/questions/841075/best-c-code-formatter-beautifier). – RAM Jul 03 '18 at 08:43

1 Answers1

3

Indentation and formatting ain't important enough to fight about. I've already seen a couple of projects with strange styles, however to be successful, they use the same style through the code.

Whatever the style at work: adapt. You don't want to manually reformat all the code.

In this case, using the correct editor settings will already do a lot. In the broader case, convince them to use an automated formatting tool as clang-format. That way, you can write as quick and dirty as you want. With a press of a button, this all follows a single style throughout the code.

Now you can start arguing about the actual code.

JVApen
  • 11,008
  • 5
  • 31
  • 67
  • *"With a press of a button, this all follows a single style throughout the code."* - this clearly does not apply to C++. – user7860670 Jul 03 '18 at 08:06
  • @VTT the grammar is finite, it is *possible* to cover all the elements with indentation rules – Caleth Jul 03 '18 at 08:51
  • @Caleth The problem is that there is no such button... – user7860670 Jul 03 '18 at 09:07
  • @VTT "automated formatting tool [such] as clang-format". I'm pretty sure that can be wired up to a single button. Recent Visual Studios have a (two-key shortcut) action that does C++ formatting – Caleth Jul 03 '18 at 09:09
  • @Caleth Alright, my point is that is no existing tool is capable of formatting C++ code. All the IDEs and tools claiming to do so that I've tried so far (including VS and clang format) are rather worthless and can not even perform code indentation properly (regardless of selected code style). Some people would pay big bucks to get a working formatting tool (and also a tool to organize includes and a refactoring tool). However considering C++ language complexity (that is getting mindlessly increased with every new standard) I doubt that such tools are going to appear any time soon. – user7860670 Jul 03 '18 at 09:39
  • @VTT maybe you just have an overly complex definition of "correctly formatted C++". I'm perfectly happy with the result of applying VS's formatter (having fiddled with the settings provided) – Caleth Jul 03 '18 at 09:42
  • The official clang format extension of visual studio allows formatting on save. – JVApen Jul 03 '18 at 10:36
  • @Caleth Indeed, my definition of "correctly formatted C++" is rather complex, but it is mostly because of the language complexity, not because I have some twisted preferences. Actually I would be happy if some formatting tool could perform just indentation. But all the tools struggle to fulfill even this modest request. For example VS assigns +1 indentation level for class methods declarations but not for class constructor declarations or for no reason applies +1 indentation level for member initializer list items in constructor definitions. – user7860670 Jul 03 '18 at 10:43