My friends… now it is the time to talk about how do you indent your code.
I am not too comfortable about my identation, so i was wondering if you could share with me your technics
Where do you put brackets?.. and after an If block, do you left a blank line before enter a new one?.
Fell free to post codes
I don’t understand why some people do that. Nothing lines up, and it’s pretty difficult to follow (at first it looks like the end of the while loop is the end of the try block). It shouldn’t require any effort to have properly formatted and indented code. Also I don’t understand why a few people smush everything together with no spaces, which makes a big long string of semi-indecipherable characters. I think some people who have problems with code would have fewer problems if they visually made the code make sense, however it seems not so uncommon that people who say they’re visual and not really coders have the worst-looking code for some reason, which seems wrong.
Whether you do
something
{
// blah
}
or
something {
// blah
}
is a matter of preference. I use the latter since it makes more sense to me visually as to what goes with what, and isn’t spaced out so much. However you should pretty much always indent even simple stuff, since you’ll quite possibly end up going back and adding stuff. JRavey’s example isn’t simple enough for me; I’d say it would have readability benefits to be formatted as usual. Only if it’s really really simple do I make an exception:
I think I started doing that for simple stuff, but it grew from there. A few times I have put a few very basic commands on one line and had to pull my head out of my 4th point of contact and put it in a proper block.
You don’t write code in Unity. Whatever you’re using for a code editor should have the option to do that, but if you’re used to Visual Studio, then you might as well keep using it.
…it immediately doesn’t look right. Of course, anyone anxious about indentation should just use Boo, where the decision is taken completely out of your hands
You will never get a clear answer on this question.
How to indent is a religious question among programmers. Its like asking all the people on earth which religion is best
The two brackets are perfectly mirrored abut their centers. With the alternative, there can be a number of glyphs beginning the line to the left of the opening brace. In addition, syntax coloring makes nearly everything that starts these lines a different color than the braces.
Eric5h5 and andeeee, are either of you colorblind? I wouldn’t doubt this having an influence on the coding community; programming on the whole is a sausage party, and men are 16 times more likely than women to have the affliction. In addition, I’d figure it to be a lot more likely for a games-minded person to go into programming, rather than art, the two biggies in the community, if the person were colorblind.
I very much agree with andeee. While a lot of people seem to prefer
if (condition)
{
// my code
}
It terribly bloats the code. Then, with one liners, they do stuff like:
if (condition)
// my code
… to save some space. And then, someone comes along and does:
if (condition)
// my code
// more of my code
And you got a bug
I think everyone should just get used to writing it the RIGHT way:
if (condition) {
// my code
}
:twisted:
Oh … and if you’re using C#, there’s really no reason in this world not to use Visual Studio. Seriously. In fact, being able to use Visual Studio would be a reason to use C#. I’ve been playing a little with MonoDevelop because Unity 3 will kind of have it bundled and it will be the IDE of choice for debugging … but … well … it doesn’t even come close. Eclipse is pretty cool, too … but that would only be my IDE of choice for Java.
PS: I can distinguish colors pretty well … but I like code to look beautiful (yeah yeah, I know … beauty is in the eye of the beholder … but there’s one exception: Code formatting … there can only be one style per project, so … YOU MUSSSST!!!)
The funny thing is that Visual Studio has this terribly ugly eye-cancer causing style of:
if (condition)
{
// code that looks broken by nature
}
as DEFAULT!!! I mean, isn’t it obvious that using this style is what caused people to dislike Microsoft (I’m pretty sure they invented that ugly style back in the C pretending to be C++ days).
Fortunately, that’s that default setting is really the only major bug, and there’s an easy quick-fix available: Tools / Options … in there, navigate to Text Editor / C# / Formatting / New Lines … and make sure to uncheck everything under “New line options for braces” and “New line options for keywords”.
And thus, Visual Studio was all fixed and we get the beautiful
if (condition) {
// code that soothes the eyes when you read it
}
I kind of like to keep “New line options for expressions” all checked, though.
Screenshots … btw, I’m not colorblind. To me, the empty lines in the second screenshot (the one with the braces in a new line) seriously disturb the rhythm of the textflow. That’s why I consider that kind of formatting “ugly”. It bloats the blocks and shifts the focus away from what matters, IMHO.
In the first example, things are really easy to read because I do have indentation for my blocks. That’s a clear and easy to understand semantics: Indented => one level down. In the second example, it’s a mess: Indented + a whole lot of space wasted the flow torn apart => one level down.