May take a programming course - deciding which one to pick

Started by fellowroot, November 20, 2012, 05:37:32 AM

Previous topic - Next topic

BRPXQZME

I pasted the exact snippet I was referring to. I don't know PHP, but if you did this in C, the loop would never end no matter how much you messed with the string, on virtue of never changing the thing you check.

Then again, if you did this in C you would just check the first character being equal to 0.
we are in a horrible and deadly danger

LostTemplar

I think what he meant was that you do something in the loop that shrinks the string's length by a known amount, and instead of recalculating the length of the string each iteration (which certainly would be a waste), you operate on the precalculate length. If it's only tested once I think it's definitely better to put it directly as condition because it's more concise. Most modern compilers will probably optimize it either way anyway, though.

Regarding the loop variables, I concur that giving them one-letter names is absolutely appropriate if they're just an index or even just a counter.

BRPXQZME

But the pseudocode doesn't say that, which I think is a good demonstration of why it is not necessarily the best way to write a similar loop ;)
we are in a horrible and deadly danger

LostTemplar

I thought that's what the comment's saying, but I'm not a 100% sure either. Maybe Garoth should enlighten us :p

RedComet

Little late, but in my experience classes are only good for learning about algorithms and conceptual stuff. I've had one or two professors who wrote clean code that was readable while the rest prefer old-school cryptic variable names and jumbled messes. I've had to spend the majority of my time on my projects for my Web Programming class rewriting and massaging my professors shit heap of code to get it into a state where I can work with it easily without wanting to pull my hair out. My assembly language class is just as bad.

The other people in my classes haven't written code for 10 years to know what bad code is and my professors essentially encourage them. I dread having to work on a project after one of these people have worked on it.

In general, it seems that most professors are out of touch with industry and what students would really benefit from. Or they only care about their research. Or I go to a really shitty school. Probably all the above. All that said, I have enjoyed my logic and math classes a lot more than my programming classes just because I don't have to deal with fixing other people's mess.
Twilight Translations - More than just Dragonball Z. :P

Garoth Moulinoski

#25
Quote from: BRPXQZME on November 21, 2012, 08:24:37 PM
This would be an infinite loop, wouldn't it? Operations in a condition check are A-OK as long as they make paradigmatic sense and aren't terribly expensive.

Ahh... I did screw up. I wrote that pretty quickly. In that example, you would have to modify the length, so it still defeats the point I was trying to make. I should have put more thought into it. I meant, basically, if you're going to reuse a certain value, it's better to just store it in a variable than to call the function that gives you said value every time you need it (when you know the value isn't going to change).

Quote from: BRPXQZME on November 21, 2012, 08:24:37 PM
I'm also okay with one-letter variables being used for obvious and transient purposes. Saves typing time, confuses nobody.

Not picking on you, but I already quoted you and other people said this too so... In a small loop, for a counter, I can live with that. It's when you have "w" and "u" throughout all of your code without so much as a single explanation as to what it's for.

As for adding the datatype before the variable name, for some names, it's probably obvious and in some ways can be the convention itself. Some people are also better at naming things than others. I feel that I can come back a year later, read my code, and understand that, yeah, $strTitle is supposed to be a string that holds a title to something.

Oh. And to the person who said that PHP is badly designed: Yeah, I know. :/ But it's what I have to use at work, so I end up using it. Edit: I'm reading through the PHP: a fractal of bad design and it's pretty interesting stuff. It really does bring up valid flaws in PHP. I learned to use it and work around its type conversion (I remember first running into that! Hahaha...) but like the guy in the article said: That doesn't make it any less bad.

Quote from: RedComet on November 22, 2012, 09:24:06 AM
Little late, but in my experience classes are only good for learning about algorithms and conceptual stuff. I've had one or two professors who wrote clean code that was readable while the rest prefer old-school cryptic variable names and jumbled messes. I've had to spend the majority of my time on my projects for my Web Programming class rewriting and massaging my professors shit heap of code to get it into a state where I can work with it easily without wanting to pull my hair out. My assembly language class is just as bad.

It was the same at my school, although I got lucky that I got more "clean code" professors. I even had a few that would deduct points if you didn't put comments on your code. I did have a few, though, that just... I don't know, didn't care or something. They just wanted pump out line after line of code. I mean, that's fine when you're just saying "you do this in order to format a string" or something quick, but notes? You want comments on notes!

How bad was your assembly class? I mean, my professor used put comments on his ASM code and so would I (because that stuff gets cryptic after a while) but it's not like ASM is too pretty to begin with.

Quote from: RedComet on November 22, 2012, 09:24:06 AM
The other people in my classes haven't written code for 10 years to know what bad code is and my professors essentially encourage them. I dread having to work on a project after one of these people have worked on it.

Enforce good practices! It'll help them (which you don't care about) and it can help you improve your leadership skills.
Who will quote me next?
Disclaimer: If it sounds wrong, I may have been posting while asleep.

BRPXQZME

Quote from: Garoth Moulinoski on November 26, 2012, 11:53:29 AM
As for adding the datatype before the variable name, for some names, it's probably obvious and in some ways can be the convention itself. Some people are also better at naming things than others. I feel that I can come back a year later, read my code, and understand that, yeah, $strTitle is supposed to be a string that holds a title to something.
This sort of "Hungarian notation" for types is more or less pointless in a strongly+statically typed language like C, though I do not deny the efficacy of Apps Hungarian in certain situations. I suspect many coders would have a visceral reaction to a variable name like $strTitle in that that shouldn't have to be something so important to the variable that it needs to be in the name, but then again those coders don't PHP.
we are in a horrible and deadly danger

LostTemplar

I used to use Hungarian notation but it really clutters your code. With good enough naming sense it shouldn't be a problem to discern what type a variable is. "title" is obviously a string or something similar unless you're doing something wrong. If it's an integer maybe you should call it "titleIndex" or something like that instead. Does it really matter then if it's a short or an unsigned long? Maybe, but then you should probably know the code pretty well before you're going to change anything anyway. Also, what are you going to call instances of your own classes?

Well, all in all, it probably is a matter of taste though. I grew out of it, but there are probably more important topics if one really were to discuss programming styles.

BRPXQZME

Well, you have to keep in mind that one of the things it was born out of was non-OOP languages (in fact, it came from a language with only one data type) and big multi-person projects. Nowadays OOP makes it a lot easier to figure out where to look if you don't understand what the heck that variable is supposed to be and who was smoking what.

The reasoning Mr. Spolsky gives in that article I linked makes a lot more sense than just prefixing with types. Type checking really should be left to the compiler.
we are in a horrible and deadly danger

LostTemplar

I hadn't even noticed the link; it just might be that I need a new monitor, or theme. Or both.

Anyway, that article was very insightful, especially the part about Apps vs. Systems Hungarian. Consciously I only knew about the latter one, thus my criticising post. The former one makes a lot more sense because it conveys crucial information that might not be visible at first. However, it's still relying on the programmer's judgment who tend to be human and thus make human mistakes. Hence, when concerning critical security matters, it makes sense to depend on the compiler instead of the person writing the code. For example, using C++'s type system lends itself to such tasks.

Garoth Moulinoski

Quote from: BRPXQZME on November 26, 2012, 04:44:12 PM
but then again those coders don't PHP.

Yeah. I can't even ask the IDE to backtrack to a variable's declaration, because even if it does find it (and it can, depending on the circumstances), without a comment telling me what the variable is, I still won't know what does. Even then, there's a good chance that somewhere along the road, PHP could figure some random type for the variable and I have to sit there for hours trying to figure out how to make it stop doing that.

Like LostTemplar said, it is a matter of taste. I like having a standard convention that tells me "this thing should be a string" although I might not be too anal about it in something like Java, where variables are (not counting casting) statically typed.

I like the rules that Spolsky mentions:
QuoteKeep functions short.
Declare your variables as close as possible to the place where you will use them.
Don't use macros to create your own personal programming language.
Don't use goto.
Don't put closing braces more than one screen away from the matching opening brace.
(I would add "Give variables descriptive names" to that list)

Quote from: BRPXQZME on November 26, 2012, 05:13:12 PM
The reasoning Mr. Spolsky gives in that article I linked makes a lot more sense than just prefixing with types.

I actually hadn't thought about using it like that.

I understand that a lot of people hate having to type a lot of things out, but I don't mind. *shrug* As long as there's proper indentation for scoping and the code is well commented, I'll be happy.
Who will quote me next?
Disclaimer: If it sounds wrong, I may have been posting while asleep.

BRPXQZME

I know some people name their variables stuff like zxcv at first, then they search and replace the verbose version.
we are in a horrible and deadly danger

Garoth Moulinoski

Quote from: BRPXQZME on November 26, 2012, 05:46:21 PM
I know some people name their variables stuff like zxcv at first, then they search and replace the verbose version.

Interesting. I can't imagine doing that myself, though. I know I won't go back and replace (although on occasion, I do if I come up with a much better name).
Who will quote me next?
Disclaimer: If it sounds wrong, I may have been posting while asleep.

RedComet

Quote from: Garoth Moulinoski on November 26, 2012, 11:53:29 AM
How bad was your assembly class? I mean, my professor used put comments on his ASM code and so would I (because that stuff gets cryptic after a while) but it's not like ASM is too pretty to begin with.

My professor's few comments were the kind that pointed out the obvious, e.g "LDA #$12  ; Loads 12H into A" and stuff like that instead of comments that actually explain what the code is doing. It was made even worse by the fact that my professor couldn't speak comprehensible English if his life depended on it. Yay Engrish. I think I'm the only person who understood what was going on in there and that's only because of all the years I've spent writing assembly for my projects. See, kids, romhacking is a good thing!
Twilight Translations - More than just Dragonball Z. :P

Garoth Moulinoski

Quote from: RedComet on November 28, 2012, 08:35:23 PM
My professor's few comments were the kind that pointed out the obvious, e.g "LDA #$12  ; Loads 12H into A" and stuff like that instead of comments that actually explain what the code is doing.

Ah... Yeah, that can be problematic.

Quote from: RedComet on November 28, 2012, 08:35:23 PM
It was made even worse by the fact that my professor couldn't speak comprehensible English if his life depended on it. Yay Engrish. I think I'm the only person who understood what was going on in there and that's only because of all the years I've spent writing assembly for my projects. See, kids, romhacking is a good thing!

Ah. Fortunately, my ASM professor was fluent in English and was a good teacher. My microcode professor was fluent in English (British English, which can sound kinda strange to American ears, but English nonetheless) but he spent most of his time talking about the history of computers ("It's amazing, I mean really, look at these tubes. You wouldn't think of having something that big in a computer anymore, would you?").

But I did have a few professors like that... My Data Structures professor wasn't perfectly fluent in English (he was Chinese) and on top of that, liked to skimp on his explanations or contradict himself often. My C Programming professor was Middle Eastern (I don't know from where), spoke strange English and would sometimes just write code silently, run it, watch it not work, and then be all like "Well class... Uh... I don't know what happened here... Anyone want to give suggestions?" *15 minutes later* "Oh, oh! Look, it's doing something! It's actual- Oh Segfault..." :/ Wow, I'm totally learning.

And my Unix professor was fluent in English, but had a monotone voice and sucked the life out of class every day. At least I'd already been using Linux for some time before that class, so I knew most of the stuff already.
Who will quote me next?
Disclaimer: If it sounds wrong, I may have been posting while asleep.

Bregalad

QuoteLDA #$12  ; Loads 12H into A
My god I can't belive professors can still use comments THAT bad. Why put a comment if it just repeats what is right before it ?

As a very experienced guy with many assembly languages I can assure you placing comments is more important in assembly than in any other language, but that you should NEVER use this style of comment which just repeats the code.

The same would apply with any programming languages too.

BRPXQZME

we are in a horrible and deadly danger

furrykef

Quote from: Bregalad on November 30, 2012, 04:21:14 PMMy god I can't belive professors can still use comments THAT bad. Why put a comment if it just repeats what is right before it ?
If it's a new construct the students are just learning or one they might have forgotten, it makes sense. But it should be made abundantly clear that this isn't how comments work in the real world.

Incidentally, I got in minor trouble for not using such inane comments in the "real world" when I worked on a commercial DS game. One of the programmers was complaining to me that he couldn't understand my code because I didn't use enough comments. (I do try to write self-documenting code in favor of using comments, as I view comments as an admission of failure at making the code self-documenting. That said, I do comment much more than some people.) I said, OK, maybe my code isn't as clear as I thought. Show me an example of code you don't understand. So he showed me a line. I asked what the problem with the line was. He said he didn't even know what it means. It was a bitwise operation or something elementary like that -- I was setting or clearing bits. It was something that would have been instantly obvious to anybody with real experience with C or C++. Why I was manipulating those bits was obvious too; it wasn't something like "foo |= 0x12" with no explanation. So adding a comment there would have added nothing of value -- to somebody who already knows basic C++. But apparently expecting a programmer to know basic C++ is too much to ask for.

LostTemplar

Quote from: furrykef on December 01, 2012, 09:25:31 AM
If it's a new construct the students are just learning or one they might have forgotten, it makes sense. But it should be made abundantly clear that this isn't how comments work in the real world.

Yeah, I sometimes do that if I'm new to an assembly language and the mnemonic of an instruction isn't that obvious, or if the instruction is used in a not-so-obvious way. For example, some SNES games use "TDC" for setting the 16-bit accumulator to 0 (their DP register is always set to 0). There I usually wrote "TDC ; A = 0" just to remind me.

Quote from: furrykef on December 01, 2012, 09:25:31 AM
Incidentally, I got in minor trouble for not using such inane comments in the "real world" when I worked on a commercial DS game. One of the programmers was complaining to me that he couldn't understand my code because I didn't use enough comments. (I do try to write self-documenting code in favor of using comments, as I view comments as an admission of failure at making the code self-documenting. That said, I do comment much more than some people.) I said, OK, maybe my code isn't as clear as I thought. Show me an example of code you don't understand. So he showed me a line. I asked what the problem with the line was. He said he didn't even know what it means. It was a bitwise operation or something elementary like that -- I was setting or clearing bits. It was something that would have been instantly obvious to anybody with real experience with C or C++. Why I was manipulating those bits was obvious too; it wasn't something like "foo |= 0x12" with no explanation. So adding a comment there would have added nothing of value -- to somebody who already knows basic C++. But apparently expecting a programmer to know basic C++ is too much to ask for.

I have the feeling a lot of (hobby, but probably also professional) programmers actually struggle with bit-wise operations; they're not THAT often used in "normal" application programming I'd guess. In most programming courses and books they're usually only hinted at ("there are these operators ...") as well. But it's not that they're difficult; for ROM hackers they belong to the basics ;)

Bregalad

QuoteI do try to write self-documenting code in favor of using comments, as I view comments as an admission of failure at making the code self-documenting. That said, I do comment much more than some people.
Well, the more you write in a higher level language, the more this is true. But if you write something in assembly, it's hard to write "self-documenting" code as soon as you have something complex enough so that you don't have enough register to do everything at a single time. Which in the case of the 6502 or 65816 is very soon as they have only 3 registers.

You should also remember that your code is always much clearer to you than it is for everyone else. I know it sound incredible but I always have a very hard time understanding other people's code, when I can always clearly see what happens in mine, even when I dig back something old I didn't touch for years.
This can be because my comments are good, but mostly just because the code was written with my logic, which I understand better than everyone else's logic.