7

I'm on my first bigger C++ project and find that I have some really long lines. My goal is to break them to 79 columns, but I do not really know how to do this in a neat way. Are there some guidelines to break lines like these:

VectorHistogram::VectorHistogram(size_t bins, size_t cache) : bins(std::vector<double>(bins)), cache(std::min(cache, MAX_CACHE_ENTRIES)) {

or

VectorHistogram position_histogram {settings.position_hist_bins, settings.time_sites * settings.iterations};

or

MetropolisDriver::MetropolisDriver(Settings settings) : settings(settings), system(HarmonicOscillator {settings.time_step, settings.mass, settings.mu_squared}), trajectory(ListQuantity {settings.time_sites}), ma(MetropolisAlgorithm {trajectory, system, settings.position_seed, settings.accept_seed}) {

closed as primarily opinion-based by ratchet freak, user40980, amon, Robert Harvey, user53019 Apr 8 '14 at 15:47

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    What does the IDE suggest? – user40980 Apr 8 '14 at 15:31
  • I can only second MichaelT: Let an automatic formatting tool decide. – Nobody Apr 8 '14 at 15:34
  • 2
    #1: This 80-column limit convention thing is ancient and is designed for ancient monitors; given modern monitors and IDEs, 80 columns just doesn't quite fit enough on one line. #2: Whatever number of columns you decide on, double-indent consecutive sublines. That is, if you have a really long if statement and have to break it up, double-indent every subline of that if statement except for the first, don't indent the curly brace on the next line, and single-indent what's inside the curly braces. – Panzercrisis Apr 8 '14 at 15:59
  • @MichaelT I do not have an IDE, I use Vim. With gqq, it wraps it only on spaces that are already there, which is not really a big help. – Martin Ueding Apr 8 '14 at 16:35
  • 2
    @Panzercrisis: Given modern monitors and IDEs 80 columns still makes a lot of sense because 1) the IDEs usually have a lot of junk to the sides and 2) because you often need to compare two bits of code side-by-side. In those cases people with good sight and thus small font will fit perhaps 100 characters, but people with worse sight who need bigger font will struggle to fit even 80. – Jan Hudec Nov 21 '14 at 13:38
8

I would break them so each line is conveying a different concern such as superclass constructor invocation or an expression. This might not be 79 characters, but with modern widescreen, high resolution monitors is that truly necessary anymore?

Whenever possible, let the IDE format it for you. Some are better at this than others: for example, I have found that Eclipse likes to break expressions in weird places. Sometimes (rarely) I need to resort to manual formatting, and I try to break code the way I described above.

Anyway, here is how I would format the code in your question:

VectorHistogram::VectorHistogram(size_t bins, size_t cache)
    : bins(std::vector<double>(bins)),
      cache(std::min(cache, MAX_CACHE_ENTRIES)) {

VectorHistogram position_histogram {
  settings.position_hist_bins, settings.time_sites * settings.iterations
};

MetropolisDriver::MetropolisDriver(Settings settings)
    : settings(settings),
      system(HarmonicOscillator {settings.time_step, settings.mass, settings.mu_squared}),
      trajectory(ListQuantity {settings.time_sites}),
      ma(MetropolisAlgorithm {trajectory, system, settings.position_seed, settings.accept_seed}) {
  • 3
    +1 for conveying a different concern. A small point of contention I have is that not everyone views code on hi-res, wide-screen monitors, so line widths are still a concern. I often run into annoying wrap-around issues when: posting code to/reading it on web forums/pages, pasting code snippets into word documents, printing code, looking at code on smaller devices (netbooks, smartphones/tables, etc.) – paul Apr 8 '14 at 15:56
  • 1
    @paul In addition to this, people naturally scan top-to-bottom so beyond a certain width you're more likely to miss something at first glance. – Doval Apr 8 '14 at 16:11
  • 1
    I agree that line breaking is important and that it should be done in a way that makes logical sense based on the code being formatted. My contention is that 79 or 80 characters is too short with modern displays capable of much more and modern languages being more verbose than COBOL (which was designed for specific width lines) or old-school C (which was ridiculously terse). I am honestly not sure how long is too long: I agree that the human eye has difficulty with wide lines, this is the same reason why newspapers and magazines use multiple columns. – user22815 Apr 8 '14 at 16:33
  • 2
    I have a 12.5" laptop which is 1366×768. With 80 cols, I can have a terminal and a Vim next to each other, each fitting 80 or 81 cols. So 80 is just right. – Martin Ueding Apr 8 '14 at 16:36
  • @JohnGaughan I've heard that 480px is a good width for content on screen (on regular monitors -- though w/ different screens having different ppi it's kinda moot). Your re-formatted version of the code on this page is maxed at 660px and the longest line has 99 chars; this seems like a visually pleasing max to me on 21"@1920x1080 (though I might be biased: the company where I work has a line limit standard of 100 chars for source code) – paul Apr 8 '14 at 17:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.