-I didn't read-
-I didn't read-
Formatting gave me hard time so I grabbed a class I was working on:
package miles.entity { import flash.geom.Point; import flash.geom.Rectangle; import net.flashpunk.Entity; import net.flashpunk.masks.Pixelmask; /** * An entity that is affected by gravity and friction. * All movement variables are points to indicate x and y. For instance, * acceleration(5, 3) means that x acceleration is 5 and y acceleration is 3. * @author Miles Hearn */ public class PhysicsEntity extends Entity { /* Accleration is applied every keypress. */ protected var acceleration:Point; /* Slows the entity down if no keypresses are made. */ protected var friction:Point; /* Also referred to as speed. */ protected var velocity:Point; /* The maxium movement speed that this entity can reach. */ protected var maxSpeed:Number; /* Which direction the entity is facing. 0 is left, 1 is right.*/ protected var facing:uint; /* The position that the entity will go to after the frame change. */ protected var delta:Point; /* The position the entity was at last frame that wasn't colliding with the level. */ protected var last:Point; /* Instead of having to remember if 0 was left or right, use these consts */ protected static const LEFT:uint = 0; /* Instead of having to remember if 0 was left or right, use these consts */ protected static const RIGHT:uint = 1; protected var pixel:Pixelmask; /** * Sets acc, vel, frc, delta and last to (0, 0) and makes the entity. * @param x the coordinate of the entity to the x-axis * @param y the coordinate of the enetity to the y-axis */ public function PhysicsEntity(x:Number, y:Number) { super(x, y); acceleration = new Point; velocity = new Point; friction = new Point; delta = new Point; last = new Point(x, y); //set to current position pixel = new Pixelmask; facing = RIGHT; } } }
public class Thing { public int Foo1 = 2; public long Foo2 = 5; private bool add = false; public Thing(int foo1, long foo2) { this.Foo1 = foo1; this.Foo2 = foo2; } public int DoStuff() { int result; if (add || Foo1 < 5) { result = Foo1 + (int)Foo2; } else { result = Foo1 * (int)Foo2; } return result; } } //This is how I like to format stuff
Open bracket on first line master race
why do you put braces on the first line? if you're so strapped for linespace why don't you delete the braces on the if/else?
That formatting style is grandpa status.
nobody else really reads my code so i just make it simple to read, so i don't have trouble navigating through my own monstrous code
Other people reading your code is a great reason to make it simple to read.
Formatting only matters when more than one person is working on a project.
I put my open brackets on a new line so they match with the close bracket horizontally.
This is so unbelievably wrong. A project can quickly become hell to work on when you get buried under unreadable code.
If you work on your projects for more than a month, you'll realize that there are parts of your code that go untouched and unviewed for long periods of time. When you finally go back to look at it you might as well be another person looking at your code for the first time. Which is why you should always format things nicely and leave comments where you do something non-obvious.
Mine's nearly like NPerez, but with a few changes where the vars are declared and such.
Edited:Code:public class Thing { public int Foo1 = 2; public long Foo2 = 5; private bool add = false; public Thing(int foo1, long foo2) { this.Foo1 = foo1; this.Foo2 = foo2; } public int DoStuff() { return (add||Foo1<5) ? Foo1 + (int)Foo2 : Foo1 * (int)Foo2; } } /*This is how I like to format stuff*/
Then when you're working on that huge project and you need to fix a bug, you won't ever find that pesky minus that should be a plus.
Opening curly brace is on the same line for data and on the next line for subroutines (functions, if, for, while).Code:typedef struct Thing_Tag { int foo1; long long foo2; int add; } Thing; /* What an useless function. I'd write Thing something; something.foo1 = number1; something.foo2 = number2; instead of Thing something; Thing_New(&something, number1, number2); */ void Thing_New(Thing *this, int foo1, long long foo2) { this->foo1 = foo1; this->foo2 = foo2; } int Thing_DoStuff(Thing *this) { if (this->add || (this->foo1 <= 4)) return this->foo1 + (int)(this->foo2); return this->foo1 * (int)(this->foo2); } /* This is how I like to format stuff */
class Thing { public: Thing(int foo1, long foo2); int DoStuff() const; int Foo1 = 2; long Foo2 = 5; private: bool add = false; } Thing::Thing(int foo1, long foo2) : Foo1(foo1), Foo2(foo2) { } int Thing::DoStuff() const { int result; if (add || Foo1 < 5) { result = Foo1 + (int)Foo2; } else { result = Foo1 * (int)Foo2; } return result; } // This is how I like to format stuff
K&R + 4 space soft tabs.
Incidentally, the if/else statement was presumably to see how you format your if brackets.
Therefore if you compressed the if when you pasted your snippit, you've kind of missed the point.
That doesn't make sense. So because I like to format my code nicely and keep it organized, it becomes harder to find something that needs fixing? It seems like it should be the other way around.
Am I the only one who prefers his public and privates indented?
class Thing { public: int Foo1; int Foo2; private: int Foo3; int Foo4; }
I used to do it like that when I used notepad++.
But when I started using editors with smart-indenters they always put them put them at the same level as the class declaration so I figured it'd be less fuss to just get used to that.
I usually indented them, as you do, until I started C#, which I don't think has categories of public and private.
TIL that you can group publics and privates like that!
Edit: Nvm, didn't know that was only in C++
That's C++. You can't in C# and Java.
Seems to be somewhat uniqueCode:public class Thing { public int Foo1 = 2; public long Foo2 = 5; private bool add = false; public Thing(int foo1, long foo2){ this.Foo1 = foo1; this.Foo2 = foo2; } public int DoStuff(){ int result; if(add || Foo1 < 5){ result = Foo1 + (int)Foo2; } else { result = Foo1 * (int)Foo2; } return result; } }
where are your accessors boy
I have tried on multiple occasions to understand how this actually makes stuff easier to read, and I always find it makes code look more convoluted than it need to be.
I find :
Looks much easier to read (for me, that is) than:Code:public int AddExample(int x, int y) { return (x + y); }
When you get into long methods, it gets to be a royal pain to see where they actually end. Since you have to look diagonally to find where the open-close brace pair.Code:public int AddExample(int x, int y) { return (x + y); }
But in the first example, its just a simple up - down movement to view them.
Managed to pack in a few more lines.Code:public class Thing { public int Foo1 = 2; public long Foo2 = 5; private bool add = false; public Thing(int foo1, long foo2) { this.Foo1 = foo1; this.Foo2 = foo2; } public int DoStuff() { int result; if (add || Foo1 < 5) { result = Foo1 + (int)Foo2; } else { result = Foo1 * (int)Foo2; } return result; }} // This is how I like to format stuff
Code:# I format my code the only way it will fucking work - <3 Python class Lol(): def __init__(self, a, b, c): self.a = a self.b = b self.c = c def sum(self): return self.a+self.b+self.c c = Lol(1,2,3) print c.sum()
Code:public class Thing { public int Foo1 = 2; public long Foo2 = 5; private bool add = false; public Thing(int foo1, long foo2) { this.Foo1 = foo1; this.Foo2 = foo2; } public int DoStuff() { int result; if (add || Foo1 < 5) { result = Foo1 + (int)Foo2; } else { result = Foo1 * (int)Foo2; } return result; } } //This is how I like to format stuff
Python mode?
public class Thing{ public int Foo1 = 2; public long Foo2 = 5; private bool add = false; public Thing(int foo1, long foo2){ this.Foo1 = foo1; this.Foo2 = foo2;} public int DoStuff(){ int result; if (add || Foo1 < 5){ result = Foo1 + (int)Foo2;} else{ result = Foo1 * (int)Foo2;} return result;}} // this is how I like to format stuff
im so sorry
I don't think it's necessarily better, but more of a preference thing - that's how I learned when I started and have been formatting that way ever since. The goal of this style of indentation is so the closing bracket is the only thing matching the indentation of the opening statement, e.g. the while and if below. That makes it easier to see in my opinion
Code:- (int) foo (int:number) { int x = 5 int y = 6 int number = 1 while (x > y) { x += 1 } if (x > y) { number += 5 } return number }
Is there a vs2012 c++ plugin that autoformats your code?
Ctrl+K Ctrl+F
I put a space between anything and an if statement or class or function. I also put an extra tabbed line before ends or }s.Code:public class Thing { public int Foo1 = 2; public long Foo2 = 5; private bool add = false; public Thing(int foo1, long foo2) { this.Foo1 = foo1; this.Foo2 = foo2; } public int DoStuff() { int result; if (add || Foo1 < 5) { result = Foo1 + (int)Foo2; } else { result = Foo1 * (int)Foo2; } return result; } } //This is how I like to format stuff
Code:<?php get_header(); ?> <div class="headshare"> </div> <div id="content"> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div class="entry"> <div id="post-<?php the_ID(); ?>"> <div class="date"><span><?php the_time('M') ?></span> <?php the_time('d') ?></div> <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2> <abbr title="<?php the_date('D dS M, Y'); ?><?php //the_time('Y-m-d\TH:i:sO'); ?>"><?php unset($previousday);?> Scribbled on <?php the_time('l, F jS, Y'); ?> </abbr> <!-- by <?php the_author() ?> --> <br /><br /> <?php the_content('Read on...'); ?> <?php the_meta(); ?> <p class="postmetadata">Posted in <span class="cty"><?php the_category(', ') ?></span> | <?php edit_post_link('Edit', '', ' | '); ?> <span class="cmt"><?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></span></p> </div></div> <?php endwhile; ?> <!--<div class="navigation"> <div class="alignleft"><?php next_posts_link('<span class="prev"> Previous Entries</span>') ?></div> <div class="alignright"><?php previous_posts_link('Next Entries <span class="next"> </span>') ?></div> </div>--> <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?> <div class="post-audits"> <div class="post-recents"> <ul><h2>Recent Posts</h2> <?php $query='showposts=5&offset=0'; $recent = new WP_Query($query); while($recent->have_posts()) : $recent->the_post(); ?> <li><a target="_blank" href="<?php the_permalink(); ?>" title="<?php the_date('D dS M, Y'); ?>"><?php the_title(); ?></a></li> <?php endwhile; ?> <br/> <br/> <?php endif; ?> </div> </div> <?php get_sidebar(); ?> <?php get_footer(); ?>
Code:public class Thing{ public int Fo o1 = 2;public long Foo2 = 5;private bool add = false; public Thing(int foo1,long foo2){ this.Foo1 = foo1; this.Foo2 = foo 2;} public in t DoStuff(){int r e s ult;if (add || Foo1 < 5) result = Foo1 + (int)Foo2; else result = Foo1 * (int)Foo2;return result; } } //This is format how like I stuff.
C#:
public class Thing { public int Foo1 = 2; public long Foo2 = 5; private bool add = false; public Thing(int foo1, long foo2) { this.Foo1 = foo1; this.Foo2 = foo2; } public int DoStuff() { int result; if (add || Foo1 < 5) { result = Foo1 + (int)Foo2; } else { result = Foo1 * (int)Foo2; } return result; } } // This is how I like to format stuff
Java:
public class Thing { public int foo1 = 2; public long foo2 = 5; private bool add = false; public Thing(int foo1, long foo2) { this.foo1 = foo1; this.foo2 = foo2; } public int doStuff() { int result; if (add || foo1 < 5) { result = foo1 + (int)foo2; } else { result = foo1 * (int)foo2; } return result; } } // This is how I like to format stuff
#@@
0123
1E
Code:public class Thing { public int Foo1 = 2; public long Foo2 = 5; private bool add = false; public Thing(int Foo1, long Foo2) { this.Foo1 = Foo1; this.Foo2 = Foo2; } public int DoStuff() { if (add || Foo1 < 5) return Foo1 + (int)Foo2; return Foo1 * (int)Foo2; } } //This is how I like to format stuff
How would you even format an assembly document? If I'm not mistaken, scope isn't a factor, which would mean there's a lot of opportunity to get lost.
line breaks and comment banners.