Pimp my code. Please?

Apprentice
Posts: 19
Joined: 2005.06
Post: #1
Ok, first some background. I'm taking an independent study Cocoa Programming class at school. What that entails is my "sponsor" teacher telling me "hey write some program, and if it works by the end of the semester I'll give you a good grade". This is suppose to be a learning experience for me; and seeing as how my teacher doesn't know cocoa, the majority of my time has been spent reading documentation and looking at sample code.

Recently I started working on my first "project", and one of the classes I'm using feels kinda weird. I've been writing java for 3~4 years so I've become used to my code just looking ugly, but this seems different. Maybe because I'm not accustomed to the way cocoa is suppose to look yet. Since my teacher can't help me improve my code I'm turning to these forums.

If you guys could critique/judge/ridicule/deprecate/humiliate/etc my code and/or me I would be more than gratefull. Any and all feedback is welcomed.

file: http://frozendevil.org/code/FDWebPageHandler.m

Also, I apologize if posting something like is considered taboo. Have pity on me if so Blink

Thanks

Edit: background on the code might help (or not). You give it the url of a parent page and the file extension you want. it finds all the links on that page that link to files with that extension, parses all relative and absolute path URLs into full ones, and returns all the URLs as an array
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #2
You could try, instead of looping over the array and appending the string followed by a /, just ask the array to join itself using the character / and append the resulting string (don't remember the method offhand, but it's analogous to the PHP join() method if that helps)

Might be known as implode or something like that...

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Apprentice
Posts: 19
Joined: 2005.06
Post: #3
Steven Wrote:You could try, instead of looping over the array and appending the string followed by a /, just ask the array to join itself using the character / and append the resulting string (don't remember the method offhand, but it's analogous to the PHP join() method if that helps)

Might be known as implode or something like that...

Thanks, I actually didn't know about that, but it ends up having the same problem as stringByAppendingPathComponent since you can't tell it to sart imploding from a specific element.

For anyone else that's curious it's an NSArray method called componentsJoinedByString: and the docs are here
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #4
You could call that on a subset of the array:
Code:
[[myArray subarrayWithRange:NSMakeRange(2,[myArray count]-2)] componentsJoinedByString:@"/"]


I'm probably off by one on the range, it might be zero-based or something. Didn't check. It's a lot prettier than a loop.

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Apprentice
Posts: 19
Joined: 2005.06
Post: #5
Thanks a ton!

That's one of the things that's tripping me up about cocoa, you're encouraged to do things "just in time" instead of declaring everything at the top of your method.

I remember how cool I thought it was when I first did [myBox setLocation:NSMakePoint(x, y)]; (or something similar)
Quote this message in a reply
Member
Posts: 196
Joined: 2003.10
Post: #6
Until very recently I also used a temporary int variable to iterate through arrays, but there's a better way that is very simple. You've probably seen it before in the docs - NSEnumerator, and gone: "Gah. I can't be bothered with the memory management." But there *isn't* any, see, and it's a much faster, better way to deal with iterating through arrays. You should probably use them.

Sample code ripped from my current project:
Code:
id objectToBeDrawn;

NSEnumerator *drawableObjectsEnumerator = [drawableObjects objectEnumerator];
while (objectToBeDrawn = [drawableObjectsEnumerator nextObject])
{
     [objectToBeDrawn drawToView];
}

Otherwise looks good. Your variable naming is standard, and your braces/spacing make sense. Just a question: why the first brace on the line for loops, but off the line for functions?
Quote this message in a reply
⌘-R in Chief
Posts: 1,254
Joined: 2002.05
Post: #7
Ask Wil Shipley. He'll pimp your code until it's pimp de pimp pimp pimp like his ride.
Quote this message in a reply
Apprentice
Posts: 19
Joined: 2005.06
Post: #8
That's actually where I got the idea from. I figured that he's far too busy to actually pimp it, though. I suppose that it doesn't hurt to send it to him, although he probably wants something that actually resembles finished.

I've been reading his blog constantly since the WWDC guest speaking post. I've changed a lot of the cocoa style I use because of it.
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #9
There is a command-line app somewhere in Jaguar and up (I think) that pimps your code stylistically - fixing indentation, whitespace, comments, and stuff like that. I'll be damned if I can remember the name, though.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #10
man indent
Quote this message in a reply
Post Reply