-
Tidy code...
Evening:
Besides neatness, are there any other practical reasons for indentation of code? I find indented code actually quite difficult to read (human eyes) Before finishing a project I always write code left-indented; I like the flush edge look, but that is just me. When I post code on this board people must think "What unruly junk!" (and at times it is indeed :o ;) ) Any thoughts are really appreciated and will help me develop along a choice path as a programmer. Thanks.
-
>I find indented code actually quite difficult to read
You're a member of the .000000001% of the population that's crazy like that. ;) Everyone else seems to think otherwise.
>When I post code on this board people must think "What unruly junk!"
No, they probably think "Feh, another one forgot code tags".
-
There actully are practal reasons for indentaion of code. As for the amount of indenation and what to indent I prefer to code like this.
Code:
#include <iostream>
int main(void)
{
int variable = 0;
if(variable == 0)
{
std::cout<<"Variable equals 0"<<std::endl;
}
return 0;
}
same kind of spacing for loops and I always use braces plus the extra two spaces. but by no means am I the standard. The reasons for indentation and such is to see your program flow as with all left justified it might get confusing to see what if statement is doing what and such.
eg.
Code:
#include <iostream>
int main(void)
{
int a = 0;
int b = 0;
if(a == 0)
{
if(b == 0)
{
std::cout<<"a and b equal 0"<<std::endl;
}
}
return 0;
}
-
You have a bright future in obfuscated C code contests. :cool: :D
-
conventional programming, from naming conventions to format conventions, are important if you ever do programming with a team. Everyone relies on each other to do their job perfect and they won't be too happy if you don't follow conventions. Otherwise, if you are just doing one man projects, be sloppy if thats what you want. Especially in C. The only one it can hurt is you lol.
-
Hello,
Indentation is very important to the readability of your code. Proper indentation can show the reader the structure of the code at a glance, but inconsistent indentation can mislead the reader.
Proper, consistent indentation is required to emphasize program structure. It is also important to maintain a consistent format to aid in searching for the beginning and end of a control structure. Indentation is used to highlight the block structure of the code and should be used consistently. The proper use of whitespace and indentation makes the structure of the program evident from the layout of the code.
To help the understanding of your C/C++ code, whitespace (spaces, tabs, new lines) are all ignored by the compiler, which means you can scatter this whitespace all over your code and make it readable by everyone. This is especially helpful when asking for help on forums. Very few want to read code that has no formatting. It makes the code hard to understand. Also, lack of formatting make some errors nearly invisibe, like mismatched braces and the like.
Code blocks are defined by their indentation. By "code block", I mean functions, if statements, for loops, while loops, and so forth. Indenting starts a block and unindenting ends it. There are no explicit braces, brackets, or keywords. This means that whitespace is significant, and must be consistent. The number of spaces used for indentation is a matter of taste (usually about 3 or 4 spaces is normally used), but whatever number you use you must be consistent.
You can adapt to your own Coding Style. Just remember, a consistent mark for indentation is advised.
- Stack Overflow
-
Properly formated code is good for other people to follow your work. good indentation and commenting is crucial to writing for buisnesses that want to be able to give your code to another and have them capable of picking up were you left off with minimal delay.
But unformated code is good if you are trying to hide what is happening. for instance if you were coding for internet (html, xml, etc.) and you wanted to hide how everthing works.
i.e.
Code:
#include <iostream>int main(void){int variable = 0;if(variable == 0){std::cout<<"Variable equals 0"<<std::endl;}return 0;}
But any good programmer would still be able to read it after some time.
-
Well, you do need one new line in that code (for the header file).
Anyway, check out the IOCCC :)
And on the serious side, everyone's points are valid:
Easy to read = Less bugs + Easier to maintain + Happier coders who have to maintain = More efficient coding process + Happier users
-
opps about the mistype.
anyway.
Zach, that website has some pretty intense examples of Obfuscated code.
-
i like indentation because it gives me a cause and effect type visual:
Code:
if(a == 1) // cause
b = 2; // effect
But i find in some other languages indenting can get in the way. But that is usually specific to doing one task. As a whole, I think indenting just gives most people a way to read code fast. When reading if statements sometimes you have to do some retracing your steps when everything is unindented.
-
If you don't like to indent you can always use Alt-F8 in Visual C++ after you have selected your code press alt-F8 and it will automatically indent everything for you. I am sure there are other code formatters out there you can check into. I am not sure what compiler your using.
-
> are there any other practical reasons for indentation of code?
No one will help you
No one will work with you
No one will hire you
On a line-for-line basis, your code will always have more bugs than well-formatted code.
-
Just about the only reason to NOT use indentation is to save a bit of diskspace for your sourcefiles.
A properly indented source of say 1000 lines would be maybe 3k larger than a source without any indentation whatsoever.
If you're really strapped for diskspace and can't afford to add another disk, remove indentation as a last resort. But beware of doing so in Python code as there indentation IS relevant and determines program flow.
-
>Just about the only reason to NOT use indentation is to save a bit of diskspace for your sourcefiles.
Well...yes...you drive a hard argument there. However take HTML for example. The code that I work with is pretty and indented. The code that I post on the web will be stripped of extra spaces, new lines and indentation. Still no reason I see giving up readable code for a marginal gain.
>Python code as there indentation IS relevant and determines program flow.
Very good point on this one. Laguages such as python do not use { } they go strictly by the indentation. But even then I'd hardly use this as driving reason to use indentation.
Long story short. I think we should all send you a very large source files that are indented but just to keep you on your toes delete random closing braces so if statements and loops are not properly closed. A very fun problem to fix without proper indentation.
-
> Just about the only reason to NOT use indentation is to save a bit of diskspace for your sourcefiles.
Or just delete some of the other crap on your machine, like programs no longer used, music never listened to, pictures never viewed.
Saving disk space has never been an argument in favour