21st November, 2006
learn2code
Tuesday, 10:37 pm in CodeGirl
Fun statistic; I’ve been in three dumb internet fights in the last week or so (one of which wasn’t even with a random stranger)! It must be that time of year again. You know, the one where I decide too many people like me and I need to start doing some alienating. Or something. Hur hur.
Anyway, as some of you know, I occasionally lurk around the CodeGrrl Forums partly because it tickles my helpful streak, and partly because after some initial revulsion I got gradually more and more curious as to the kinds of issues that pop up there. Today’s adventure comes from this thread. It starts off simply enough; kid having trouble with the Build A Blog ‘tutorial’ script. For those of you who read this and have some coding experience, I’m going to sit here and let you read a few segments to kinda get the drift of why this script makes me cringe so much. Firstly from a teaching perspective, since that’s exactly the one thing that it utterly fails to do; instead mostly presents hunks of lines of code to be copypasta’d. And secondly from a coding perspective, since it teaches some absolutely horrible habits that I then see repeated all over the place. The scary thing is that it’s also allegedly been ‘cleaned up’ since it was written. Shudder.
Look, don’t get me wrong; I dig the whole concept of making PHP ‘accessible’ to hip young internet chix0rz, really I do. But this isn’t the way to go about it. CodeGrrl is the official (and unofficial) distribution point for several scripts commonly used on personal domains, including phpFanBase; a script so truly terrible that not only has my domain host issued an official warning to all its subscribers not to use certain versions of the code, but that I completely re-wrote instead of actually using.*
Like most people, I learnt to code in the proverbial School of Hard Hacking Knocks, and I truly, honestly believe this is the only way to learn to code; by getting down and dirty with your own project and grinding out code until it all just works. This is why PHP is the only language I feel I’ve truly mastered (even though technically I’ve had experience in C/++, Java, Perl, Python and even COBOL); because it’s the only language I’ve ever used to program something for me, that I thought of, because I wanted it. Every other person I know who is even vaguely competent at coding has learnt exactly the same way.
I did not learn, and I’d bet money that neither did any single person in the world, by crawling into a forum and begging “lolz it’s broken again fix pls” and waiting with my mouth open for someone to shove some answers down my throat. Proving you’re a Super 1337 Haxx0r who can debug code with nary but a glance is cool for pointless ego stroking, but it’s not helping anyone. Somewhere halfway down the thread I got sick of ego-stroking, and decided it was time to stop playing Rent-a-Coder. I fess up straight away; it’s my fault the code got mangled. I stuck an if-not in by accident where it shouldn’t go. Any first year CompSci student should be able to spot it except, oops, the BAB ‘tutorial’ says exactly nothing on the subject of debugging or, for that matter, the subject of the not operator (or any operator). And because the vast majority of people who use the tutorial seem to solely use that one tutorial (a friend of mine used to suspect that it was primarily so they could claim to have “written their own blog”, as if copy-pasting some code out of a tutorial and whining in a forum when it broke somehow constituted “writing your own blog”), well then the thread was screwed from the start.
Dragging out for a week and three pages, eventually some admins poked their heads in suggesting that it might just be easier to give the solution. Believe me, I’d thought about the pages ago when I realised the error was my fault. But I decided to gt preachy instead:
I’m all for people learning to code, and believe me I’ve been there myself and I know how frustrating it is (the first night I sat down to write my own blog I actually cried in desperation thinking I’d never understand any of it), but the fact that the vast majority of posts in this forum are people handing out answers to already solved problems over and over again isn’t helping anyone. If someone can’t even be bothered searching the forum for the answer to the bloody register_globals problem, should they really be “writing their own blog script”? You want to learn to code? Great, I’m all for that and I’m all for helping people with their code so long as they can give me at least half an answer about what they think is going wrong. But – maybe this is a newsflash – you’re not going to learn to code by reading the BAB ‘tutorial’; you’re going to need to go to some outside sources and get some basics on control structures, expressions, variables and operators.
Harsh? Elitist? Both; but I worked hard to learn what I know (which admittedly isn’t much), and so did every other person I know who codes. I also know, from firsthand experience, that spoonfeeding doesn’t work. I remember one uni assignment we were asked to (and this was the whole assignment spec) “Solve the producer-consumer problem in Nachos”. This is a pretty common CompSci 201 kind of assignment. I had no idea where to even begin; I didn’t even understand what the producer-consumer problem was (the entire lecture notes on it included a diagram of a segmented doughnut with an arrow going in one side and another arrow coming out the other side). So I Googled the question, copied some code, handed it in and passed my assignment. Do I know what the producer-consumer problem is? Vaguely, now. Do I know how to ‘solve’ it? Hell no; I didn’t understand the solution then and I still don’t. But my problems with the producer-consumer problem wasn’t just about the question itself; it was about a fundamental problem suffered by pretty much every student at my uni, and that was lack of a decent grounding in programming basics. Not just the syntax stuff, but fundamental architectural concepts.
And that’s what I’m seeing here. Copying code without understanding the hows and whys of what you’re doing isn’t ‘learning to code’, and won’t help you solve your own producer-consumer problems when you encounter them. And yeah, this kind of attitude is going to turn a lot of people off; good, I’d much rather people turn away and go back to WordPress (which is good logware), than hear another sob story about how someone’s database got hacked or (worse) they got kicked off their host for using dodgy ‘homebrew’ scripts they didn’t really understand.
Quoted From: Dee @ CodeGrrl
I admit it now, it’s not going to be a popular opinion, and yeah, maybe it makes me an egotistical jerk. Or maybe it just makes me arrogant (since the underlying assumption is that I’m ‘saving’ people by turning them off before they get a real chance to hurt themselves).
But you know what? The internet already has more shitty PHP code that it knows what to do with; it doesn’t need another noob churning the stuff out.**
Maybe it’s time to put some code where my mouth is…
________________________________________
* Back In The Day it was the only available fanlisting script, and I’d told myself that I just wanted to experiment with something for once without getting the project lost in a mountain of custom webapp. After taking one look at the guts of the code, however, I knew I was doomed; I spent so long one afternoon ‘cleaning up’ the script that I might as well just have started from scratch.
** You can all totally shut up; I’ve been doing this for years and I totally got here first, k. Oldskool noobs FTW.
- « Previous
- Next »
Related Posts
Comments
-
I Agree...
I have to admit, I agree. Both in that spoonfeeding doesn’t work and that the Build a Blog tutorial is useless for learning to code. In fact, it’s pretty useless for writing a blog - even now as someone who HAS wrote their own blog I find it incredibly difficult to follow… not because the code is complex, but because it’s wank. We all had/have to learn somewhere (because I’m certainly no expert ‘yet’), but that somewhere should not the the BAB tutorial.
“because it’s the only language I’ve ever used to program something for me, that I thought of, because I wanted it.” …exactly how I learned PHP. I couldn’t “do” it until I needed to do it and because I wanted to use it to create something that couldn’t be fulfilled by scripts that were already available. It’s exactly that reasoning that is “preventing” me from learning Ruby/Rails/Java/ASP/ASP.NET etc - I just don’t need to.
-
I agree with a lot of what you say. I was the one who cleaned up the Build A Blog tutorial, though I would have liked to do a whole lot more to it (i.e. rewrite it) but wasn’t allowed (didn’t have time to, either). I can’t do any more to it, as I was only allowed to do what I did for the site revamp when Sasha and Vixx were busy. I didn’t write the original tutorial, and I think they wanted Michelle (who wrote it) to retain her ‘style’ or something. Admittedly, it is probably my fault that some of the later stuff isn’t explained properly… I was going rather quickly.
I too agree that the BAB tutorial isn’t the best way of learning PHP/writing a blog, but compared to many more of the tutorials out there, it actually attempts to explain the code rather than saying “do this, do that. Yay! The end”. As for PHPFanBase… Don’t get me started. I dislike it, and I feel it’s high time it was replaced. Version 3.0 (a completely rewritten script) was promised a while ago, but I have no idea what happened with that. Anyway, like I said in the CG thread, although I give out most of the answers, I try to explain what it means and why it works compared to whatever didn’t. I like to think people will learn from that and think about what I said next time something goes wrong.
I’ve written my own blog (not completely finished yet though…) as well as fanlisting script and, like you, I can’t seem to be able to use anybody else’s scripts. I’m using Wordpress to power my site at the moment and I can’t wait to get rid of it. Not only is it bloated, but there are really bad coding habits that I completely hate in there.
I’d be interested in knowing what you think of my FAQ script (the one available from CodeGrrl - PHPAskIt)… I must confess that the code is rather a mess at the moment (trying to finish my blog script before I work on it again) but I’d still love some feedback. I don’t consider myself a PHP expert by any means; I’ve only been doing this a couple of years, yet I don’t consider myself a n00b either. I know about security, I know about the dangers of using scripts like fanbase and such, but I don’t know everything and I don’t claim to. So yes, if you’re interested in pulling it apart and throwing it back at me, do contact me.
-
I have to barrack for WordPress since I knew Michel V. back when he first started writing b2.
I admit I haven’t really looked at the back-end for it in a while, but I’m interested to know what ‘bad coding habits’ are in there. I remember the last version I looked at was total spaghetti, but I don’t remember thinking the overall code itself was poor.I’ve been thinking of re-writing a ‘more different’ version of BAB pretty much ever since I first saw it. I guess one of my main problems is that it doesn’t describe anything to do with development theory or project planning, which are exactly the things that used to make me groan and hide when my tutors talked about them in college… until I realised that, somewhere along the line, I’d managed to intuativley pick them up and they were actually making my code better and more structured. It was a hard road getting there, though.

Anyway, like I said in the CG thread, although I give out most of the answers, I try to explain what it means and why it works compared to whatever didn’t. I like to think people will learn from that and think about what I said next time something goes wrong.
I guess it’s not that I’m adverse to giving out answers, but I am adverse to giving out answers to people who I think understand neither a) the problem, or b) the solution. Some people just really seem to want to fly before they’ve learnt to crawl, and IMHO that’s just really bad news. By all means, be ambitious, but at least take some time to understand the basics first.
I admit I’ve never looked at PHPAskIt; if I get some time I’ll pick it apart for you, since you’ve asked. I know I act like a big code Nazi, but realistically I’m fairly easy-going; I just have a stupid vendetta against PFB since it took away six hours of my life and didn’t contain any functions.

-
Well, I’ve asked Vixx if she’ll allow me to make some more changes to the tutorial (there are some nasty SQL injection risks in there at the moment) so if you have anything to suggest, by all means let me know and I will see if I can add them in. I agree that some of the functions and the development really do need explaining, but when I originally went through and tweaked the code a bit, I really couldn’t be bothered to explain everything in detail… I’m not that good at explaining things so I just left it.
Anyway, hopefully adding some SQL injection protection in there will make it a whole lot safer than it is presently (and it’s MUCH safer than it was to start with, believe me! I went through with my tamper data extension trying to hack my own copy-pasted version of it and I caused myself all sorts of problems, heh).Thanks for offering to look through PHPAskIt - I like to hear others’ opinions of my code, not just because I like to keep everyone happy, but because I genuinely care about its security and the quality of its code (which, at present, is a bit of a tangled mess… Really should sort that out).
As for WordPress, they’re not necessarily bad coding habits in terms of actual code (er… yes, whatever that means), it’s just things I don’t like. I don’t like variables in strings (i.e. echo “something $variable something else”) - concatenation ftw! - and little annoying things like that. Most of WP is full of annoying JavaScript now, they’ve tried to make it all AJAXy fancy-schmancy and it just doesn’t do it for me any more. I’ve used it since the b2 days and I honestly preferred it then.
-
Don’t like variables in strings?
Have to say I disagree with you there; constant string escaping makes code much harder to read IMHO, and easy-to-read code (as well as ‘safe’ and ‘working’ code) is one of the programming holy grails.Then again, I use the terniary operator (?
, and there are few things more obtuse than the bloody ternary operator. 
I noticed the WP AJAX thing the other day when I was commenting in someone else’s blog. I dunno, maybe I’d get more excited about AJAX if it wasn’t just a fancy name for JavaScript. >_> Goddamn useless JavaScript.
-
PHPAskIt
Okay, I’ve scanned PHPAskIt; looks pretty good, IMHO. Some of the code perhaps isn’t as modular as it could be (the pages with large if/else clasues) but pretty much every PHP script is affected by this and few scritps seem to have found a ‘neat’ solution.
I notice in
functions.inc.phpon line 98 you’re using a regexp, you might want to try this instead:preg_match( '/\[(url|link)(=.*)*\].*\[\/\1\]/i', &str );
It’s case-insensitive and backreferences (so it will match [link]foo[/link] but not [link]foo[/url] et al).
I also note you’re using
$_SERVER['PHP_SELF']; I’m not sure if you’re aware, but the usage of PHP_SELF changed with one of the more recent releases of PHP. I’m not sure it effects your script (from memory, the change dropped query strings off, which screwed up sk.log because of the wanky way it uses them).In
config.inc.php, have you considered unsetting the database login/password values after the call tomysql_connect()?I’m also curious as to the purpose of the
r()function.You also use
if( file_exists( 'filename' ) ) include( 'filename' );a lot. Unless you’re specifically testing for a failover condition, would it be better to use a squelchedrequire()?Finally, I’m not sure why you’ve chosen to use
mysql_free_result()so often. Garbage collection in PHP is one of those things that just kind of happens at the termination of script execution. I assume it’s some kind of security feature? -
I know I’m wicked late to this thread, but it only just popped up in my Technorati RSS feed - sorry about the lateness.
Anyway, I just wanted to add my 2 cents and say that as the admin and co-founder of CodeGrrl, I am highly aware there’s a lot of bad coding on there. I am not a coder - and I guess that shows when looking at scripts like phpFanBase. First thing I ever wrote, and it was never meant for anything more than something for me and my friends to use.
I can code some PHP and MySQL for myself, and I can hack existing scripts, but I simply don’t have the skill or the will to write good coding. In fact, I don’t really like coding (I’m a designer, not a coder).
That’s why with the new version of CodeGrrl, we opened a lot of the site up for submissions. I was hoping that instead of constant criticisms from people who are good coders, we’d actually get some submissions of good scripts and tutorials that WOULD teach people.Alas, I think we’ve had about 3 submissions since the new site went live well over 6 months ago. It seems people are only too quick to criticize the site, but don’t actually submit stuff to help us make it better. I know I personally can’t - I just don’t have enough knowledge of PHP. So if you can write better tutorials or better scripts, please know that we’d love to have them. That’s the main reason we also pulled a lot of our scripts for download - they just aren’t good enough as it is, and won’t be returning till they’re rewritten. Which again - I personally don’t know how to do.
Anyway, I guess what I wanted to say is that we welcome any and all submissions and additions to the site to make it better - we’re open to everything. Feel free to contact me via email if you’d like to help CodeGrrl!

-
Aw but mummy mindless non-constructive criticism is the best kind!

Seriously, I know what you’re saying and I totally agree, which is why I’ve always applauded CodeGrrl as a kind of platform even if I think it has some problems (everyone has some problems).
A couple of days after writing this I started to do a new Build-a-Blog (the first two parts are hidden somewhere around the site); I guess I’ll let you know when it’s done…