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}) {
6
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}) {
7