tags:

views:

104

answers:

4

Hello all,

I am a beginner programmer and I have a question regarding coding guidelines. Would you recommend hard-wrapping my code (especially comments) at a margin (e.g. print margin)? Is this considered a good practice?

A: 

It's certainly a good idea to limit the maximum length of your lines of code (and comments). If you don't, you would be either scrolling back and forth a lot or relying on your editor word wrap function (which isn't something that programming editors typically do).

The actual right margin position is subject to personal preference and is a controversial topic. Some people stick to 72 or 80 columns, while others prefer longer lines.

Greg Hewgill
A: 

See also

http://stackoverflow.com/questions/268284/when-writing-code-do-you-wrap-text-or-not

But by all means, adhere to whatever conventions your group uses.

Brian
+1  A: 

It used to be convention to limit to to a width of 80 characters because that was the width of most terminals.

Since then a lot has changed.

Always indent your code to the right level, this makes it much easier to read logically.

Besides that, most code formatting rules are subjective. Some prefer end of line comments, some prefer comments on their own lines. Some prefer spaces around if conditional, some don't.

So long as the code is easy-to-read and hard-to-understand sections are commented appropriately, you can generally style your code as you wish without anyone getting upset. Groups sometimes agree upon a certain convention for code, but this again is subjective to the team and isn't really standardized industry-wide an any concrete way.

Ben S
A: 

The most common standard is to limit lines to 80 characters, but it is not universal (unfortunately IMHO).

For comments I don't see a reason to overrun it, just break and continue in the next line. Avoid end-line comments, they encourage meaningless "micro-commenting" and you will have a hard time keeping the lines short enough.

starblue