December 2017

S M T W T F S
     12
34 5 6789
1011 12 13141516
1718 19 20212223
2425 2627282930
31      

Style Credit

Expand Cut Tags

No cut tags
Wednesday, September 13th, 2006 09:39 am
Ok, so, the original program I wanted to improve (netpong), it turned out that I couldn't (yet) figure out a way to merge the two aspects into a single function, since they hard-code different keymappings. Moving on, the next program I wanted to work on (shareaza) was FAR too complicated for me to get into (yet). I still think it's within my abilities, but I need to get a bit more familiar with the structure of the language again. It's been a while since I've worked with C++. Finally, I think I've found something more my present speed, which will let me get refamiliar with the language and also work on structure. In a group of C++ tutorials, I found a hangman program. A VERY VERY BAD hangman program. It functions, but it isn't the least bit object oriented (it has exactly one function other than main, and all it does is run a set of four if-then statements that would probably be better formulated as a single switch statement). It also doesn't even approach human-readable... most of its variables are single letters with no indication of what they do unless you read through the code and find them in action, and they're all declared in a clump. It looks like the programmer was probably taught this as a way of shortening the program, in that you only need one declaration of variables per variable type and the variable names themselves take up less space, but any speed increase due to that would be negligible and completely countered by the increased time needed to understand the program in order to maintain or update it. I'm going to see what I can do to get it fixed up, then I'll post both versions of the code and see what people think.
Wednesday, September 13th, 2006 02:23 pm (UTC)
Modern compilers don't even save the variable names, internally, and precompilation processing will kick all the variables in a given block to the top anyway, so from a compiled perspective, there's no difference between their difficult code and something more legible. Some people do think it's a good idea to declare all the variables In The Beginning just to see what you're working with as you step in, which I suppose holds some merit but tis really mostly because some plain C *makes* you do that and they got used to it. But there's no excuse for the names unless they're all loop-counter variables (by tradition, they go i, j, k, etc). Someone is lazy.
Wednesday, September 13th, 2006 04:07 pm (UTC)
Some of them are loop-counter variables, and that I can see having as just letters. (Though I'd prefer at least a comment before the declaration stating that's what they are, if they're not going to be declared immediately before the loop-count where that usage would be obvious.)

However, they are declared in a group that includes a variable to hold the length of the word, first loop count variable, a variable to hold the number of guesses remaining, a variable to hold the number of spaces in the "word", a variable used to enter characters and commas into a string that is being used as an array to represent letters already chose, and a variable that is being used to hold a random number for a do-while loop that chooses the word. These variables are, respectively, named nothing more descriptive than: l,i,ng,n,k,x And that is just the integers. There's also a group of chars that are similarly helpful: char c,h,ch,ch1,ch2. c holds lines of the input file as it is being read in. h holds a user's choice of a word category. ch holds guesses, ch1 holds a choice of whether or not they would like to play again, and ch2 holds an initial menu choice. Frankly, the whole thing is written very poorly. I've seen (and written) more elegant code in BASIC.
Wednesday, September 13th, 2006 05:00 pm (UTC)
wow. That code fails, and when I was TA'ing comp sci classes, I would have left them a very, very nasty note.
Wednesday, September 13th, 2006 05:17 pm (UTC)
It's actually worse than that, that's just the variable declarations. Here's the source:
http://www.bloodshed.net/c/hangman.zip

I'm going to work on it in two steps: First, I'm going to produce a cleaned up version that does the exact same stuff without the needless overhead, and in a more orderly fashion. Then, I'm going to see about extending it and making it more functional.

I know I'd probably be better off, in a sense, just starting over from scratch... but this is serving as a good lesson in working with someone else's code, and also helping me rebuild my skill with c++ "grammar" and "vocabulary", which has admittedly atrophied from even the small degree I once possessed.
Wednesday, September 13th, 2006 05:20 pm (UTC)
Sounds like a plan!