View Full Version : Formatting long lines of code..
dandylion13
March 14th, 2010, 05:01 AM
Hi,
I was wondering whether anyone could suggest a style for formatting methods with lots of parameters, or long lines of code in general.
For example, this is quite long and a bit hard to read:
_bitmapData.copyPixels(sourceBitmapData, sourceRectangle, destinationPoint, null, null, true);
Does it make sense to format it like this?
_bitmapData.copyPixels
(
sourceBitmapData,
sourceRectangle,
destinationPoint,
null, null, true
);
IQAndreas
March 14th, 2010, 05:15 AM
Yes, that works, and is definitely readable.
Here is another common method. The advantage of this one is that every parameter is indented far enough that the reader realises that they are parameters and not part of the function "body".
_bitmapData.copyPixels(sourceBitmapData,
sourceRectangle,
destinationPoint,
null, null, true
);
wvxvw
March 14th, 2010, 05:26 AM
When I need the line to wrap, I usually break it on the first "break opportunity", unless it is a period. This is not a rule or anything like that, but I try to keep all my code in 80 characters bound. (Well, I had a small screen in the office and I can understand the pains of someone having to scroll your code horizontally).
Or, sometimes, if I want to make fun of my colleague, I do it like this:
function foo(bar:Object):
void { trace("Hello world");
}
dandylion13
March 14th, 2010, 05:26 AM
Thanks for the feedback, IQ! :)
dandylion13
March 14th, 2010, 05:59 AM
Thanks wvxvw!
I also like to keep my code under 80 characters horizontally. It's very useful to scan code quickly and for side by side comparisons.
Here's another formatting puzzle:
if(objectBitmap.bitmapData.hitTest(new Point(bullet.x, bullet.x),255, collisionBitmap,new Point(collisionBitmap.x, collisionBitmap.y), 255))
I'm tempted to format it like this:
if(objectBitmap.bitmapData.hitTest
(
new Point(bullet.x, bullet.y),
255,
collisionBitmap,
new Point(collisionBitmap.x, collisionBitmap.y),
255
)
)
{
//...
}
I see your point IQ where it could become confusing where the body of the if block starts.
Would this be better?
if(objectBitmap.bitmapData.hitTest(new Point(bullet.x, bullet.y),
255,
collisionBitmap,
new Point(collisionBitmap.x, collisionBitmap.y),
255)
)
{
//...
}
My only problem with this one is that it's over the 80 character line limit that I like to stick to.
So I could do this:
if(objectBitmap.bitmapData.hitTest(new Point(bullet.x, bullet.y),
255,
collisionBitmap,
new Point(collisionBitmap.x,
collisionBitmap.y),
255)
)
{
//...
}
... although that seems harder to read...?
What do you guys think? :)
wvxvw
March 14th, 2010, 08:16 AM
The first one looks almost like Lisp already... but not yet :P
Well, I'd say it depends, if that's a part of the code you're not going to modify a lot, then making some pretty formatting is good. But if you're going to change that place every so often, then you'll start hating yourself for all that prettiness :)
dandylion13
March 14th, 2010, 08:51 AM
Thanks, yes, I hear you :)
It's good for the feedback as I wasn't sure whether I was going slightly off my rocker or breaking some hard formatting conventions.
kadaj
March 14th, 2010, 09:52 AM
if(objectBitmap.bitmapData.hitTest (
new Point(bullet.x, bullet.y), 255, collisionBitmap,
new Point(collisionBitmap.x, collisionBitmap.y), 255 )) {
//...
}
or
// looks more like LISP
if(objectBitmap.bitmapData.hitTest
(new Point(bullet.x, bullet.y), 255, collisionBitmap,
new Point(collisionBitmap.x, collisionBitmap.y), 255 )) {
//...
}
or
if(objectBitmap.bitmapData.hitTest (
new Point(bullet.x, bullet.y), 255, collisionBitmap,
new Point(collisionBitmap.x, collisionBitmap.y), 255 ))
{
//...
}
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.