Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Wednesday, September 05, 2007

I was going to write to my representative (NC district 1) to say 'Support H.R. 811!' but he's a co-sponsor, so instead I wrote saying 'Yay! Thanks!'

But I mistyped my zipcode (resulting in one from NC district 3), and got this reply:

Your zip code indicates that you are outside of the 4th District of Pennsylvania.

Regrettably, I am unable to reply to any email from constituents outside of the 4th District of Pennsylvania.


Write your own commentary about code reuse, hardcoded error messages, or testing of edge cases here. I've not the time just now.

Wednesday, August 15, 2007

I really don't think it's fair for me to be just using a simple script as I always do and it to suddenly not be able to call methods it's been using the whole time. I've changed nothing, but suddenly the each method is being called from an undefined iterator in mysqlPP.pm's fetch method.

It's also lame that I can't use CGI in mod_perl. I don't know why, but in CGI, it's fine, but under mod_perl, it isn't, except for when it is, which isn't often. And sometimes it doesn't work in CGI--I thing it was when I also had it trying to work in mod_perl; that is, loading Apache when mod_perl is trying to use CGI means I can't use CGI anywhere at all, and it sucks, because it's pretty darn tricky to handle stuff properly. My Apache::Request wrapper doesn't handle multiple values properly, and I'm too lazy to fix it.

Friday, July 27, 2007

This rule from Perl Best Practices made me laugh and finally realise what twisted, sick-minded individuals Perl programmers really are.

The correct rule is obviously 'Don't cascade ternary operators. Just don't. Use if-else.' But Perl programmers do it our way, and it's as beautiful as it is evil.

Wednesday, July 25, 2007

In the array and hash naming convention guideline in Perl Best Practices, they have this lovely example of when an array name should be singular (answer: when used as a random access table like a hash):

sub factorial {
my ($n) = @_;

croak "Can't compute factorial($n)"
if $n < 0 || $n > $MAX_FACT;

return $factorial[$n];
}
Factorials not pre-calculated for the cache are simply declared incomputable. Talk about lazy programming!

(Code snippet from:
Perl Best Practices, by Damian Conway, Copyright 2005 O'Reilly Media, Inc., ISBN 0-596-00173-8
Good book.)

Monday, June 11, 2007

I was looking around for suggestions on first programming languages, and I noticed that many people tend to look at this as 'what language is most useful'. If you want to program applications, learn C, C++, or Java (no!) whereas if you want to do web development, learn Perl, Python, Ruby, or PHP (no!). And, yes, I do this too, but I do this for people who want to learn programming because they think it'd be cool to write a Neopets clone or something. Not people who want to be programmers.

Here's the thing: if you're a programmer, you know more than one language.

So why does it matter which language you learn first? Because learning some languages will encourage correct thinking and good programming habits right from the get-go, and that's what you need to be learning. One of my professors, Dr. James Wirth, told us 'It's not that it's a foreign language; it's a foreign way of thinking.' And he's exactly right. I've written small quantities of code in something like twenty different languages. I'm only proficient in a few, but picking up a new programming language is not the obstacle in becoming a good programmer. In one of my courses, we had assignments in something like six different languages, and code snippets littered the blackboard in many more. I haven't programmed in Ada or COBOL or Fortran or Pascal, but I had learn to read them on the fly, and I didn't find it a challenge. This might sound hard if you've never done any programming, but once you've learned C, Assembly, Prolog, and Scheme, you can understand the basics of nearly any language. Trust me: this is something you can do. After you learn to program.

Learning to program is the key. This starts with understanding how the computer runs programs. One guy I knew in school tried the non-command 'hurry' to tell the computer to run his program quickly. When you've never done a lick of programming before, this is not an unusual way of thinking about it. But any language will teach you that. What else is there?

When I first started, I was writing spaghetti code in QBasic. What was I missing? Functions and scope. Once I learned about using functions (and more importantly, once I began putting more and more stuff in functions), I started to think like a programmer. Suddenly programming wasn't rigid. Callbacks a long, long while later brought me even further along. We want a language that encourages the new programmer to build tools to do his work for him. What languages do this? I think LISP and dialects may be the champs her.

What else do beginning programmers desperately need? Style/Formatting. There were even upperclassmen in the CS department who didn't indent. (For the record, they began with C/C++, Java, or Dr. Wirth's Box English (broken link)) Everyone knows which language teaches indentation: Python. Worst might be Perl. Few will allow the programmer to ignore more style guidelines.

It's basic and of debatable importance, and probably should be covered separately if you're in a CS program, but How Things Work is also relevant. By this I mean, for example, understanding variable, something Dr. Wirth's Box English seems to focus on at a high level. Understanding overflows. Understanding data types. Should a beginning programmer be forced to learn the difference between and int and a string? Personally, I love not having to treat strings as char*s, but they'll need to learn sometime. Additionally, it will come up from time to time no matter what language you use. Try adding '3' and '8' in ECMASript. (Perl thinks it's 11. ECMAScript, Python, and Ruby all say it's '38' because '+' means concatenate. C thinks it's k or 107, depending on whether you ask for an int or a char output. A beginner will think Perl makes the most sense, but the most intuitive answer for a wizened programmer put before a language he doesn't know should be the C answer.) What language will teach you the nittiest of the nitty-gritty? Well, I guess machine language or Assembly. But shout-out is due to C for being a usable language that manages to force low-level understanding as well. Whether that's a good thing or a bad thing for beginners, of course, is something we could argue over all day and night.

And, let's face it: if you're trying to learn to program, learning a language at the same time just complicates things. Simplicity, please. I was using Perl for a while before I learned when it's $, %, @, $#, ${%var}, $#@var, \$, \%, \@, $var->{..., and even worse was (is) [] or {} or (). Let's see...a hash is () and a hash reference is {} and a list is () and an array is () but an array reference is [], and a list of references is \() or (\a, \b...). I love Perl, but this would frustrate beginners more than anything. When it comes to a simple, consistent syntax, Scheme obviously ranks way up there. I think Python and Ruby also score quite well. Since I mentioned QBasic earlier, I may as well do so again: QBasic has a very simple syntax. The case-insensitivity would probably go over well, though perhaps not something we should be teaching young programmers.

I started out talking about this, but I think it deserves a broader look: Learning to think. Leaving spaghetti code behind was a major boon, but that's just part of it.

The winner: There is no definite winner, of course. Python, I think, fairs pretty well. Scheme for teaching good thought is a classic answer (though usually just under the guise of LISP in general), and with the added benefit of a simple, clean syntax, is a very important consideration. If you want deep understanding of the computer rather than style and the more abstract 'think like a programmer', C or C++ is an excellent choice.

Personally, I think I'd go for Python to begin with, partly because it's popular. There's a lot to say for an active community when you're learning a new language.
The nitty-gritty can be gotten later (you should learn Assembly at some point anyway, and that's nothing but nitty-gritty), and Scheme also later on in order to broaden the way of thinking. A good programmer needs to touch on functional programming, anyway, and perhaps logical programming with Prolog as well. Meh. I had a bad experience with Prolog. The darn thing insisted I had some syntax error and I couldn't finish the simple assignment. :-(

Thursday, October 26, 2006

I know I shouldn't have, but I named my JavaScript spelling checker function1 spellcheck(event). This, of course is wrong because it's not for checking spells, it's for checking spelling2. But I named it 'spellcheck'. Lame.

It turns out, however, that Firefox 2 uses the same function name for their spelling-checking-as-you-type. I had to rename my function.

Okay, so it took me not long to figure out the cause and fix it. But this isn't a JS1.7 change, so it shouldn't be messing with my scripts.

Fx2 is nice, though. It's much faster and less leaky, probably because Tab Mix Plus didn't support it. This means worse tab behavior, but I'll survive. Or I could try the RC.

1 Not so much a spelling checker, as a tag parser for some forums I use. Convenient to type in, e.g., '[Staff]' and have it translate to a link to the staff list.

2 Seafood node 1.

Saturday, September 16, 2006

For ages, my newer version of my lame Javascript text adventure game has been getting an error in IE6. (Link goes to old version). Sadly, because I had done a lot of changes testing just on Firefox, and the error message was completely useless ('Error: object expected' with the file stated being the HTML file and the line number being the line at which JS is called) it took me a while to figure it out which change had caused the error.

So I just continued developing for Firefox.

This, of course, means that many more IE nonworkinesses have appeared, and since I don't keep detailed version history, it will be painful to identify them all. But I've finally found the original idiocy.

In Javascript--much like Java, C, C++, and so on--you define a constant by writing const variable_name[ = value];. This works just fine in Firefox and Opera, and probably in most others. But In IE, it doesn't. And when it doesn't work, it doesn't complain 'You wrote "const" on line 42 and I don't know what that means'. Instead, it pretends everything is all right. Suppose you have this in your header:

<script type="text/javascript">
const c = 3;
alert('Testing');
</script>
No error message will occur. Yet should you comment out const c = 3;, the alert will run, so there's no question that IE is doing stuff in the right order here. Same thing if you do
<script type="text/javascript">
function foo()
{
const c = 3;
alert('Testing');
}
</script>
<body onload="foo();">
</body>
No error message. Just silence. Commenting out the const (or doing s/const/var/) will make it work fine.

To get it to report the error, the user has to activate it, as best I can tell. Onclick will get the job done:
<script type="text/javascript">
function foo()
{
const c = 3;
alert('Testing');
}
</script>
<body>
<p onclick="foo();">Get an error message</p>
</body>
And you don't need to call the 'erroneous' function:
<script type="text/javascript">
function foo()
{
const c = 47;
alert(c);
}
function bar()
{
var c = 42;
alert(c);
}
</script>
<body>
<p onclick="bar();">Get an error message</p>
</body>

Basically, if the keyword 'const' appears anywhere in that Javascript, it's all dead, but you won't be informed until you explicitly try to execute it. (Multiple <script> tags will work independently, as will code in an onclick attribute.)

Ramble, ramble, ramble. My point is, someone please tell me how to get constants to work in IE. I don't really need them, but it's generally considered a good practice. Probably slightly faster run-time too, though so minor that it doesn't really matter in this situation.

Anyhow, there will likely be a new version coming out in a few days. Unless I decide otherwise. Or I end up having too much trouble with all the other errors IE6 is--oh, wait, hey! No other errors at first glance! Marvy!

Monday, July 24, 2006

If you're hoping for a long, thorough essay covering all sorts of inadequacy and annoyances of Javascript, you're out of luck. I just have one thing to whine about today: lack of interpolation.

I do a lot of stringish stuff in Javascript. Loads. Just recently I was editing this argument: sp + ',' + mp + ',' + dp + ',' + type + ' ' + x + ',' + y + ' (' + done + '/' + total + ')'

That's ugly. Even more than it's ugly, it's a major pain to type out. $sp, $mp, $dp, $type $x $y ($done/$total) is slightly annoying, but almost incomparably better.

sprintf could help, but is still majorly annoying. I want interpolation.

(The biggest Javascript annoyance is, of course, incompatibility, especially when combined with IE's inability to give useful error messages.)

Thursday, March 23, 2006

Some time ago I came across this formula for checking if an integer is a power of two: ((n & -n) == n)

Upon seeing this, of course, I noticed that it was wrongwrongwrong. So I popped into IRC to ask my brother to agree with me, which he did:

(17:00:58) lkbm: One thing iss telling me that '((n & -n) == n)' will check if n (a signed integer, I assume) is a power of two, but wouldn't 0 & -0 == 0?
(17:02:30) Miciah: Absolutely.
(17:02:40) Miciah: Why don't you write some C to try it?
(17:02:49) lkbm: Doing so right now.
(17:02:55) lkbm: Haven't done C in a year.
(17:03:50) Miciah: It is easy stuff.
(17:04:07) lkbm: 'var n = 0;' is wrong.
(17:04:16) lkbm: Let's try 'int n = 0;'
(17:04:17) Miciah: C wants types.
(17:04:39) lkbm: It also wants my "s to match.
(17:04:50) Miciah: If they aren't escaped or quoted,
(17:04:56) Miciah: ...or commented.
(17:05:00) lkbm: Right. Looksl ike we're right though.

Of course, it works for all values but one, and it's trivial to fix. What cleverness.

Oh, and for you ignorant people wondering why it works, look up 'two's compliment' and keep in mind that '&' is a bitwise and.

Wednesday, July 13, 2005

Tripod (which, as I'm sure you know, sucks) gives us free (beer) people a CGI-BIN (with the ability to run our own scripts, of course). Now, CGI sucks, and lack of a relational database sucks, but I'm sure there's delicious stuff to be done with this.

I'm considering doing a toy virtual pet site mock-up, just because, well, I can. I've done a bit of it thus far, but it's untested ('cause I can't test it on my laptop), and doing everything with files is lame.

I also started working on a multi-player mod of the JS text adventure game I started some time ago, but again, untested.

Surely there's something trivial and amusing to do?

Tuesday, June 28, 2005

(Potentially relevant link)

Intended more seriously than my previous games, this is a Javascript-based Text Adventure Game. It's still alpha (my stuff never leaves alpha; I'm too lazy), but fairly functional. It can't yet compete with Hamlet: The Text Adventure (Or Robin Johnson's 'The NONDESCRIPT Text Adventure Engine for Javascript', which Hamlet uses), but it's a nice start for no more than a couple hours work. Most of the time was probably spent writing the example game.

I was writing a Perl-based engine last year, but it got lost when my hard disk died.

Sunday, June 26, 2005

(Potentially relevant link)

I friend mentioned how he'd like to make a text-based GTO game:

I have this odd urge to make a text-based RPG version of Grand Theft Auto. Something like this:

Your Ubercar 3500X is on Fubar Road facing east. The corner of Crime Street is twenty-five ahead on the right. There are three pedestriants directly to your left.
> turn into crime street
Your Ubercar 3500X is on Crime Street facing south. There is an EvenBetterCar 9000N in the parking lot to your left.
> steal evenbettercar 9000N
You stole the EvenBetterCar 9000N. It is facing Crime Street facing South.
> drive south
You drive 50 feet south. There are ten pedestriants in the area, six on your left and four on your right. A police car is chasing you.
> bump police car
You bump into the police car. It takes 2% damage; your EvenBettarCar 9000N's armor keeps your damage to 0%.
> drive south
You drive 50 feet south. Two police men get out of their police car and approach your car.
> run police men ove
You run one of the police men over, but fail to hit the other. He advances toward your car and forces you out. You've been arrseted.

It would be mostly for parody, but with more-or-less complete game play.

Then again, the name Grand Text Auto is apparently taken. Maybe I won't do it.
(Posted by Leif K-Brooks).

This inspired me to make a little Javascript racing game. Future versions may include:
* Other people driving.
* More gruesome deaths (your engine can overheat, but currently that's no problem)
* Better controls (this really should be command-line based. :-)
* Better gameplay (turning all the time is boring. It needs more.)
* More!

But for now, here's the alpha.

Initially, it was going to be much simpler, which is what was so amusing about it. This was my original idea:
* You could accellerate.
* You could decellerate.
* If you went fast enough, you'd win.
No skill involved. No reason not to accellerate as much as possible. Just keep typing 'go faster'. Truly, that would have been fantastic. But then I had to get fancy.

Wednesday, December 01, 2004

This girl--a senior in CS--just came over to ask one of the professors how to compile C++ on Solaris. 'It's something like g++ -o and then something' (She says 'minus oh').
The professor, I kid you not, didn't remember but tried searching online.

I tell her 'g++ -o output_file source_file' and she's like, 'but I only have one input file'.
Dr. Collins then found the command on another professor's website. And, oh look!, I was right!

I told Dr. Collins, 'If I was the professor, I'd fail you both', but I don't think she heard me. (I suppose the professor can be forgiven; at least she was able to look it up. But the student must die.)



I got to do the student evaluation of Dr. Wirth this morning. Wrote a short lecture on letting incompetents learn stuff on their own time.

This is a Java course. Dr. Wirth constantly tries to say, 'you know how it is in C++, well here's how it is in Java...what? You don't know how it is in C++? God! You people really need to learn this stuff. *endless lecture on C++*'
All The Time. Intro C++ and Data Structures (which also uses C++) are both prerequisites for the Java course. These students have taken the those courses. They just Don't Know Anything. For the first month or two, we wrote more C++ code in this course than we wrote Java, because he'd have us write a bubble sort in C++, or a simple command line interpreter in C++, because these fools would be clueless. He sees major holes in our education and takes it upon himself to try to fill them in. He's explained the basics of DOS commands and environmental variables in several of my courses, when what he really should have said was, 'In order to program a computer, you need to know how to use one. Go learn that on your own time'.

He's an interesting professor. Entertaining. Teaches interesting stuff that's very useful and not taught anywhere else. But that leaves very little time for the actual material. Several of us would like to see 'Computer Science 0001: Dr. Wirth's Rantings'. I'd actually like to see two of them, an intro level and a senior level. (Basic DOS commands are intro level [or below], whereas details of C++'s switch implementation should come after intro C++.)

Sunday, January 25, 2004

I've been teaching myself php. Simple, though sessions had me stymied for a bit. I think they're probably working fairly well now, but I haven't actually tested the code.
Yes indeedy, I have learnt nothing from ten years of coding. Surely it will work right off the bat?