4.10.2008

Star shapes

So I skipped Philosophy today and went to my CSCI 101 midterm pretty early. Before it started, I was talking to Ashley and Rollerblades Kid (I call him that because I don't know his name, but he uses rollerblades as his primary mode of transportation) about the impending midterm, and computer programming in general. Ashley has no programming experience other than this class, and she was telling me how she had been having trouble on a recent assignment with opening an input file. The relevant code would go something like:

ifstream fin;
string filename;
/* ... */
cout<<"Please enter filename: "; cin>>filename;
fin.open(filename.c_str());

I don't think I ever figured out, based on her description, what she had done wrong, but she said she contacted a friend who works at Google, and her friend had told suggested some sort of modification using character arrays in place of strings. It makes you wonder why, when C++ was designed, they didn't rewrite ifstream::open() to accept strings. I mean, you could just do:

void ifstream::open(string filename) {
open(filename.c_str());
}

C++ just seems so user-unfriendly after playing around with things like Javascript and Ruby. It would be interesting to learn the history of various languages, because programming languages do grow and evolve like spoken languages, but there are also some clear differences between the way they evolve. In any case, I suppose they use C++ for the 101 class because it's well established, and the people running the department probably aren't familiar with a lot of the newer languages anyway. Plus, C++ forces you to think a little bit about the way the computer deals with data, internally, when you pass things by reference, or try to access an array value that's out of range. I suppose that would be helpful when you get to classes on data structures. (One of many classes, by the way, that I should be taking this fall, but can't because it conflicts with a required physics class.)

However, Ashley is a chemical engineer. Many of the people who take this class are business majors. They're not taking a data structures class, and have no desire to become programmers of any kind. There really ought to be another class for such people. In fact, sometimes I think a few basic programming skills ought to be required of all students. However, I would never wish this particular class upon the student body at large. This other class would be like Physics 100, which I gather is a physics class for people who really don't like physics. It would probably be taught in some version of BASIC, maybe VBA since it's infinitely more useful for many people than any other language. Ideally, it would be something where students can type things in at a command line, rather than having to write, compile, fix compiler errors, recompile, test, fix runtime errors, recompile, etc. There would probably be no need to deal with OOP, but maybe they'd touch on it at the very end.

As it is, no one is going to learn anything from a single class like CSCI 101, especially when we spend more time worrying about things like header files than we do actually writing code. I guess that's the whole point of Ruby. The programmer's job is to write code, and the interpreter takes care of everything else, like memory allocation. Anyway, I have a few problems with the way our class is taught, as well, but I'm sure I'll post about that another day.

What I wanted to post about was actually an exchange with Rollerblades Kid. I was complaining about what I called "bullshit" on the test. I tried to reproduce one from today's test from memory, but Blogger tried to interpret the << operator as the beginning of an HTML tag and it got all messed up. In any case, we have a couple of functions that have a more or less random assortment of +'s, -'s, switch statements, if statements, arguments being passed by value/reference, etc. They don't correspond to anything real at all, it's just arbitrary data manipulation. We have to trace through the program, then write down the output it would generate. Admittedly, these problems are pretty simple, and I guess they're okay for test questions, but it completely goes against the principles of good code writing. You can't use descriptive variable names when your variables do not in fact describe anything.

In any case, Rollerblades Kid didn't seem that concerned with this type of problem, because he was used to them from the AP Computer Science test (which I didn't take), but he and Ashley both objected strongly to "Star shapes" problems. The kind which are supposed to print output like:

*
**
***
****
*****
******

or

*
***
*****
*******
*****
***
*

I must admit that, in modern times, when we have the ability to use "real" graphics, there is no need to learn this particular skill. Yet for some reason, I find it much less like a pointless busywork assignment than the "bullshit" trace problems. I think that's because it solves a real problem. Not real as in real-world. Just real as in, there's an actual problem with a non-trivial solution, and the code is that solution. Yes, it's a problem you may never face, but the same could be said of so many problems you're asked to solve as classwork. Still, it's hard to explain just what it is that, to me, makes Star Shapes problems seem not to be completely worthless. You can actually learn something about good programming style from such problems. But of course, there is absolutely no guarantee that will actually happen.

3 comments:

Paige said...

Wow. Like 80% of that post went over my head.

Elliot said...

Rollerblades Kid. Cool nickname.

Excellent post. I really agree with you.

The history of programming languages is fairly complicated. It's good to look into. This semester I learned a lot about how computers work at the hardware level. High level languages like C++ really help to extract tiny operations into code that people can easily write.

At the hardware level, computers don't know anything about objects or even functions. Processors sequentially execute operations based on their instruction set, and only the absolute basics exist. Lots of issues have to be accounted for, like memory access, interrupts, exceptions, branching, etc.

Still, I don't think C++ goes far enough, which is why you still have to deal with quirky stuff which seems strange until you see what it's really doing. Fortunately that's why languages like Ruby, Python, or PHP are so useful. These allow programming on an even higher level. But it's cool to learn the lower level stuff first. I really appreciated learning Assembly this semester, and I finally have an understanding for what machine code is. Each assembler instruction (unless it's a pseudoinstruction which is handled by the compiler) directly translates into machine code, which literally is just 1s and 0s. It's truly amazing.

I would like to hear about your problems with the way the class was taught. I feel the same way. No programming class I've taken has been taught well. EE352 (with assembly and embedded C) is the best I've had so far, and it's not even a CSCI class. That's sad. For this reason, I'm planning to develop a new programming curriculum and independently teach it this summer. I've made a website for the endeavor: Anyone Can Code.net.

Looks like you fixed the << problem. To expand on that, for using special characters, the creators of HTML made up a bunch of character entities. For the < symbol you can use &lt;, and other symbols have similar ones.

There is some value in tracing through poorly-written code. It tests that you understand programming syntax. Every programmer needs to know at least the simple fundamentals of how a compiler works, how it takes your code and runs it. If you don't know what existing code does (even if it's ugly and useless), how can you write code?

However, it could be taken too far. There are obscure code contests where they make up confusing code just for the sake of it, and that gets dumb.

Those star problems are interesting. Why aren't they shaped like actual stars? Or do they just look that way because the leading tabs/spaces were removed since they are considered meaningless in HTML?

Tyler said...

Yay comments!

I know about the codes for < and whatnot, I was just feeling lazy. If I remember correctly there was more code I was going to type out, and I left it out. You got my point anyway, though, it seems.

I've been meaning to write a long, carefully thought out post about why the class was so badly taught. I'll get to it eventually.

They're only called star shapes because they're made with stars. I suppose "asterisk shapes" would be more accurate. Here's a fun one I came across recently which actually has nothing to do with stars or asterisks at all:

http://acm.pku.edu.cn/JudgeOnline/problem?id=1941

Your site sounds great! I'll check it out.