Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / Code beautification and formatting

Author
Message
nonZero
12
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 6th Mar 2014 10:02 Edited at: 6th Mar 2014 10:07
So I've been giving it some thought and I, along with prolly half the *nix community, may be wrong. I always open my curly braces on the same line. But I recently got to thinking that maybe I shouldn't. Curly braces denote code blocks and can enclose and isolate a piece of code, making everything local within it. Therefore maybe they should line up (ie open on a new line). But it just doesn't look right. Maybe because I've coded this way for years. That got me to thinking about style in general. I have strict disciplines about things like all comments being on the RHS and starting at column 85 (at least for C, my style varies per language). I also always double-indent the first statement following a function, struct, etc and then single-indent all nested statements afterwards. I even align declarations so that the variables all start on the same column and, much to the C++ community's horror, I try to declare everything at the top of a function. The return statement for functions with returns is always on its own, separated by a blank line and functions are separated by three newlines. I align parameters that are part of a call that's too long for a single line and align enums, variable declarations, etc too. All in all, my code looks neat, imho. Yet... I feel it's lacking. Like it's still missing something. So I thought maybe you guys could share your opinions and techniques. Maybe we can all learn something as pretty code is what I strive for.

ver 7.5 /// int 145 /// str 45 /// dex 85 /// end 200 /// mat 3
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 6th Mar 2014 11:28
No idea what you think is lacking. Colour? Sound? Taste?

Your suggestions are very similar to my own conventions. A minor quibble is with the braces, brackets issue. If the code block fits neatly on one line (without it being inconveniently long) I often put the opening and closing brackets on the same line. Otherwise I make sure the opening and closing brackets line up clearly in the vertical direction. In my opinion that makes it easy to see where a code block starts and finishes. But that works for me - others might see it differently. For example, text .X files often have blocks structured like this:



That's not too bad because the starting character of the block, e.g. the "M" of "MeshTextureCoords {" lines up vertically with the closing "}". I prefer something like



or perhaps



Anyway, such distinctions are nit-picking and the important thing is to have a consistent readable style which you seem to have.



Powered by Free Banners
nonZero
12
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 6th Mar 2014 14:09
Yeah, you're right. My problem is I'm never satisfied. So, I like to see as many alternatives as possible so I can get a broader view. I'm like that. I spent weeks writing a simple FIFO buffer because of that and even now I think back to things I could change. In all likelihood, I'll go crazy and cut an ear off before 50 and then the other one after 50 in the name of symmetry.

ver 7.5 /// int 145 /// str 45 /// dex 85 /// end 200 /// mat 3
The Zoq2
14
Years of Service
User Offline
Joined: 4th Nov 2009
Location: Linköping, Sweden
Posted: 6th Mar 2014 15:05
I usualy open the brackets on a new line to make sure that they allign with the closing bracket. Usually I indent the code one step per bracket (or code segment) for example, I indent declarations after public: one more step since they are "part of public".



Also I prefer to use 4 spaces as indentation and usualy use the tab character instead of 4 spaces.

Say ONE stupid thing and it ends up as a forum signature forever. - Neuro Fuzzy
budokaiman
FPSC Tool Maker
14
Years of Service
User Offline
Joined: 24th Jun 2009
Playing: Hard to get
Posted: 6th Mar 2014 18:21
Quote: "I usualy open the brackets on a new line to make sure that they allign with the closing bracket. Usually I indent the code one step per bracket (or code segment) for example, I indent declarations after public: one more step since they are "part of public"."


I prefer it this way as well. I do use 3 spaces instead of 4, I find 4 too much.


"Giraffe is soft, Gorilla is hard." - Phaelax
The Zoq2
14
Years of Service
User Offline
Joined: 4th Nov 2009
Location: Linköping, Sweden
Posted: 6th Mar 2014 20:48
For those of you using C++, how do you structure classes in files. I used to keep one class per file but now I have started using one file per "feature". So the file world.h contains a World class, a Segment class and a Building class.

Say ONE stupid thing and it ends up as a forum signature forever. - Neuro Fuzzy
Kevin Picone
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 7th Mar 2014 01:00
I'm pretty simple, scopes intended (tabbed) and markers aligned to the left most border. Do like a good headline around key markers & sections, really just depends on the IDE..

nonZero
12
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 7th Mar 2014 10:48 Edited at: 7th Mar 2014 11:28
I used to use 3 spaces. then I started using 4 because I started using a smaller screen with a higher resolution (laptop) so 3 spaces didn't stand out. It's also why I started double-indenting the first nest-level in functions/etc.
I also sometimes compress if-blocks on a single line and I always use curly braces, eg:
if (something) {doThis();}
For which I've come under fire for. Personally, it's a safety thing for me. It makes me feel secure even though it isn't necessary. I can't actually not do it. It also ensures I always keep things in context. It's one of my many quirks like how I declare enums vertically to make them readable. I've never considered good or bad practice, just readability.

ver 7.5 /// int 145 /// str 45 /// dex 85 /// end 200 /// mat 3
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 8th Mar 2014 13:07
Quote: "I also sometimes compress if-blocks on a single line and I always use curly braces, eg:
if (something) {doThis();}
For which I've come under fire for. Personally, it's a safety thing for me. It makes me feel secure even though it isn't necessary. I can't actually not do it. It also ensures I always keep things in context. It's one of my many quirks like how I declare enums vertically to make them readable. I've never considered good or bad practice, just readability."




I sometimes insert the braces in that situation as well for the same reason - but I confess to not being very consistent about it.



Powered by Free Banners
Yodaman Jer
User Banned
Posted: 8th Mar 2014 17:46
Usually, if an if statement only has one line to execute, I write it like so:



I suppose you could blame Python, but I don't really use Python ever so that's not really it.

I also like to keep my curly braces on their own line:




Come check out my new website!
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 8th Mar 2014 19:46
I sometimes do that but it seems a bit excessively pedantic for just one line.



Powered by Free Banners
Rudolpho
18
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 8th Mar 2014 23:51
This just doesn't sit right with me...

Under such circumstances I always add the additional curly brackets for the last clause even though they aren't of any necessity. Otherwise I have no problem with the braceless one-liners; in a sense I find them quite beautiful. The brackets really are just a way to be able to put multiple instructions into a block that can be interpreted as a recursive construct, thus allowing the syntax to be defined like "IF <expression> <statement> ELSE <statement>". Or that's how I look at it now at least, after being messed up in the head about such things by a course in programming language construction and Lisp.


"Why do programmers get Halloween and Christmas mixed up?"
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 9th Mar 2014 20:27
Quote: "thus allowing the syntax to be defined like "IF <expression> <statement> ELSE <statement>". "


That may be true - but readability and symmetry are more aesthetically pleasing in my opinion. After all, compilers don't generally care how you format code - it's "merely" us mortals that like code to look pretty.



Powered by Free Banners

Login to post a reply

Server time is: 2024-05-04 15:06:30
Your offset time is: 2024-05-04 15:06:30