Clang-format is a tool to automatically format C++ code (and other programming languages too). We favor its use (or the use of any other tool, for that matter), basically because of the "broken windows." Pragmatic Andrew Hunt Dave and Thomas used the broken windows metaphor about bad code, which leads to software rot: When disorder increases in software, programmers call it "software rot." [Hunt-1999, Ch. 1, Tip 4] Here we tackle "just" formatting--see [Martin-2009, Ch. 5] for reasons about the importance of this subject.
I simply do this loop:
- Edit
.clang-format
to revise or extend it - Use it:
$ clang-format.exe before.cpp >after.cpp; clang-format.exe before.hpp >after.hpp
- Verify the expected differences between
before.cpp
andafter.cpp
(for example, withp4merge
)
Some interesting URLs:
- ClangFormat documentation, and its Style Options
- clangformat.com, an interactive guide and builder
- Interactive clang-format configurator (GitHub repo)
Material cited within .clang-format
(or within this README file), sorted alphabetically by reference label:
- [Alexandrescu-2001] Andrei Alexandrescu, Modern C++ Design: Generic Programming and Design Patterns Applied, Addison-Wesley, 2001
- [Bulka-1999] Dov Bulka, David Mayhew, Efficient C++ Performance Programming Techniques, Addison-Wesley, 1999
- [Cline-1998] Marshall Cline, Greg Lomow, Mike Girou, C++ FAQs, Second Edition, Addison-Wesley, 1998
- [Dewhurst-2002] Stephen C. Dewhurst, C++ Gotchas: Avoiding Common Problems in Coding and Design, Addison-Wesley, 2002
- [Hunt-1999] Andrew Hunt, David Thomas, The Pragmatic Programmer: From Journeyman to Master, Addison-Wesley, 1999
- [Koenig-1999] Andrew Koenig, Barbara E. Moo, Ruminations on C++: a decade of programming insight and experience, AT&T, 1999
- [Martin-2009] Robert C. Martin, Clean Code - A Handbook of Agile Software Craftsmanship, Prentice Hall, 2009
- [Meyers-2014] Scott Meyers, Effective Modern C++: 42 Specific Ways to Improve Your Use of C++11 and C++14, First Edition, O’Reilly, 2014
- [Milewski-2001] Bartosz Milewski, C++ In Action: Industrial Strength Programming Techniques, Addison-Wesley, 2001
- [Pohl-2002] Ira Pohl, C++ by Dissection, Addison-Wesley, 2002
- [Schildt-2003] Herb Schildt, C++: The Complete Reference, Fourth Edition, McGraw-Hill, 2003
- [Stroustrup-2013] Bjarne Stroustrup, The C++ Programming Language, Fourth Edition, Addison-Wesley, 2013
- [Stroustrup-2014] Bjarne Stroustrup, Programming - Principles and Practice Using C++, Second Edition, Addison-Wesley, 2014
- [Sutter-2001] Herb Sutter, More Exceptional C++, Addison-Wesley, 2001
- [Telles-2005] Matthew Telles, C++ Timesaving Techniques For Dummies, Wiley Publishing, Inc., 2005
- [Williams-2012] Anthony Williams, C++ Concurrency in Action: Practical Multithreading, First Edition, Manning Publications, 2012