You should try to prevent writing lines longer than 80 characters rather than breaking them:
- Try to minimize indentation by converting conditions and encapsulating code.
Linus Torvalds: If you need more than 3 levels of
indentation, you're screwed anyway,
and should fix your program.
This also has the side effect of making you code much more readable, besides that the conditions are the other things you encapsulate are ready to be used elsewhere.
bool encapsulatedLongCondition() // Add some parameters
{
if (!condition1)
return false;
if (!condition2)
return false;
// ... (Other conditions)
return true;
}
if (encapsulatedLongCondition())
{
// ... (Call some methods, try not to introduce deeper if/loop levels!)
}
Simplifying your condition through boolean algebra and trying to invert the condition and return value can help a lot. :-)
See also: Can you simplify this algorithm?
See also 2: Refactor for C# has the ability to assist you with this. ;-)
- Use type definitions and try to avoid long names
A simple example, imagine how long it would be using Days without typedef with longer names in another container.
struct Day
{
// Some data
};
struct Event
{
// Some data
};
typedef list<Event> Events;
typedef map<Day, Event> Days;
// Some other container that would else be long...
- ... (You should try to analyze why your line is long and find a solution for it)
Hope you get the general idea, this way you won't need to come up with a dirty line break. ;-)
The only place where I would see long lines occur is in the prototypes of your functions or when calling them, there you should just try to break after the last possible comma and continue on the next line. Rather than doing it after each and wasting multiple lines making scrolling bloated and your function stand out too much... You could place the return type on the line before the function name if you happen to frequently see these long lines.
void longFunctionName(ParameterType1 parameter1, ParameterType2 parameter2,
ParameterType3 parameter3, ParameterType4 parameter4)