Huh. I tried it with g++ after casting &c to (int*) from (const int*) and yes, it printed out.
BUT, if I “properly” change the declaration of c to “int c = 3;” instead of const int, it doesn’t.
I’m guessing that when you declare a const value of a native type, the compiler (or rather, the linker) must be at liberty to optimize it to the equivalent magic value.
For example, this code does NOT print out:
#include <stdio.h>
class Int
{
public:
Int(int i) : _i(i) {}
int get_int() const { return _i; }
void set_int(int i) { _i = i; }
private:
int _i;
};
int main()
{
const Int c(3);
Int *x = (Int*) &c;
x->set_int(5);
if ((&c == x) && (x->get_int() != c.get_int()))
printf("Everything you know is wrong!
");
}
Oh, and as an artifact of my training (first C out of K&R, then C++ out of Stroustrup), I use the older notational convention for pointers and the newer one for references. I guess I get my own Pit thread as far as the OP’er goes.
int i = 0;
int *ptr = &i;
const int& ref = i;
Far more annoying than notational vagaries are stupid comments. It’s just not true that having uncommented code is the worst sin as far as maintainability goes. It’s worse to have wrong and outdated comments that do not apply to the code it annotates – either the code changed and the comments didn’t, or whoever wrote the code and comments were different people, or confused or plain evil.
Though completely out of reference comments can be a source of humor. Like the following more-or-less real life examples I have seen:
[something innocent looking] // no.
or
#include <somelib/include_file.H> // do not move this line
(which, I will add, lived up to its warning)
or
[blah blah blah] // added today
Your BASIC interpreter automatically showed you the program in order of the line numbers? Whoa, you had it sweet. The one I learned on spat back the lines in the order they were received (even though, of course, the program was executed in line number order), which meant the really nerdy hackers at my school took great pleasure in floating code around like this:
100 END
999 PRINT "THINK I EVER GET HERE? HAHAHAHA"
001 LET X = 1
075 GOTO END
023 IF X < 100 GOTO 40
...
You code this way and you’ll find you voted for Pat Buchanan for president, mark my words.
I agree with you about the braces, though.
I hate too short variable names, and the worst culprit I know of in that regard was the original Zurich Pascal compiler. Wirth’s data structure book has code examples with single letter names, and they coded like that. Maybe it was a reaction to too long German names? It took me months to figure out what everything meant when I was in grad school.
I have to deal with code that iterates over a doubly linked list structure. The nodes in this llist are called “instruments”.
One of the core routines iterates this doubly linked list with three pointers: c_inst, p_inst and n_inst. The iteration begins with “c_inst” being set to the front of the list.
Any sane person, I would venture to guess, would thereby assume that “c_inst” was the current node, “p_inst” the previous node, and “n_inst” the next node in the iteration.
Think again. As it happens, this particular routine doesn’t ever look “backwards” in the iteration, but does need two forward-looking pointers, so “p_inst” refers to the next node, and “n_inst” to the one AFTER that.
What I think happened is that the writer of this code cut-and-paste the outer loop from some other, very similar and very core routine that sorts this doubly linked list using three pointers, where the declarations for c_inst, p_inst and n_inst DO mean “current”, “prev” and “next”, and then just… reused them to mean different things.
Hear hear! Javascript is a great language, but why it thinks it should look like Java I have no idea. new Constructor(); in a prototype based language? What the fuck where they drinking?
And Microsoft just can’t write a Gui APIs and I’d really like them to stop trying and use someone else’s please.
Nonsense. I think Windows messages were an inspired design, especially for its time. You could subclass, superclass and generally take advantage of built-in window widgets and change their behavior to suit what you needed. Want an edit box that only takes numeric input? Superclass the edit control, intercept WM_CHAR and friends, and you got what you want in no time, in a perfectly encapsulated way. Then you just do a CreateWindow(myNumericEditClass, blah,blah) and use it anywhere. So it’s not C++ or objecty enough? Well, use WTL or similar. Jeez, the design is from 1989 fer chrissakes. Oh, And Apple was apparently impressed enough to rip it all off for Carbon Events much later.
If you want to talk about GDI on the other hand, I don’t really think there’s any defense.
And if you want to talk about how the API is organized and documented, well, it sucks.
A consulting company, which is trying to get the maintenance contract for that system.
Somewhere back at their office, they have a version of the code with real, meaningful variable names used. I once worked on such a system, developed by a very small consulting company, where the source actually left enough space for the real variable names, but only showed the short ones. For example:
IF A1 = "MN"
when study of the code showed that the real version was
IF STATE-CODE = "MN"
Real obvious what they had done. And a rather sneaky way to try to guarantee that they would get the maintenance contract. But not uncommon.
This was in COBOL. So the variable names were only used by the compiler, not by the actual generated code. A name like WS-PAYROLL-EMPLOYEE-LAST-NAME or one like WS-A produce the same object code. The only place it might make a difference is in the compiler stack, and even back in the days of COBOL E and F there was plenty of room for long names.
I once had to try and understand a Fortran-77 program written by a mathematician who clearly had no training as a programmer. Goto’s everywhere, no indenting at all, never a three-letter variable name when a two-letter one would do, no comments, and oh sweet Og the input format. Do you know how you input a line of text from standard input in Fortran-77? Why, like this:
50 READ 60
What’s ‘60’, you ask? Why that’s the line where the input format is specified. What’s to stop someone from writing " 50 READ 1008", putting the input format miles away from where it’s actually used, you ask? Not a damned thing.
Oh, and somehow before the program got to me an extra space had been added to the beginning of each line of the program. This, of course, causes Fortran-77 to not compile. I had to go to a dusty corner of the engineering library on the other side of campus to find a book that explained this to me.
And you’re complaining about whether there’s an line return before the brace? Wimps.
FORTRAN had a rule that a variable name couldn’t be more than 6 (I think) characters long, and the first character indicated the data type (IIRC, I through N were for integers, the rest were reals. There were no other types). It pretty much forced you to use obscure abbreviated variable names.
What I remember from that time, the program text wasn’t stored anywhere – except on a deck of punched cards. Online DASD was far too precious to use it for things like program source. It was a few years later, when DASD space was bigger & cheaper, that we got source management packages like Panvalet and Librarian.
Though there were a few programmers around who used very abbreviated data names, because they were poor typers (keypunchers, really). But generally, you wrote your program on coding pads, and turned it in for an expert keypunch girl to punch for you, so data name length didn’t really matter.
If you can rewrite this block to not include goto and not make it a horrible monstrosity, be my guest:
for(int i = 0; i < 100; i++)
{
for(int j = 0; j < 100; j++)
{
for(int k = 0; k < 100; k++)
{
if (found(i,j,k))
{
doSomethingComplicated(a, b, c, d, e, f, g);
goto FOUND;
}
}
}
FOUND:
}
Gotos should be avoided whenever possible but it is possible to use them judiciously.
finally, fuck all language designers for not letting me hide error checking code
I would say somewhere between 30% and 80% of the code generally gets taken up making sure nothing goes wrong in a good system. When I’m trying to figure something all, all that cruft is just obscuring the the main logic of the code. I want a simple language spec and a simple IDE where I can toggle between the “full” view of the code and one showing only the main path. All the brace conventions in the world isn’t going to amount to a hill of beans if all you can see is a bunch of error code you don’t care about.
Ah. You must predate me slightly. We had punched cards, but for initial input only. It was with great pride and wonderment that we retrieved our small ribbon of magnetic tape. In the future, we dared believe, reels would be four feet wide.