Subj : Re: "File Scope" objects To : comp.programming,comp.software-eng From : Richard Heathfield Date : Fri Aug 19 2005 08:22 am Chris Sonnack wrote: > Richard Heathfield writes: > >>> ...no sane programmer would name a non-local, 'buff'..... >> >> I have my own feelings about otherwise sane programmers wantonly >> using file scope objects... but I'll keep those feelings local >> for now. > > Having not unrecently read someone speak very much in favor of > file scope objects, I'd be interested in a little round-about > on the matter, if you're willin'. (I find I have no real opinion > one way or the other on this one, although I do tend to not use > many of the critters.) I use almost none. My strategy is to assume there's no syntax for them, and write my code according to that assumption. Then, if I find myself asking my user-programmers to pass one lousy pointer - always the same one - into every single call of every single function in a large library, then I might consider "discovering" the syntax for a file scope object. > The thought was they were similar to (private) class variables, > which I found an interesting, undisagreeable point of view. It is certainly interesting, but I think I can nonundisagree with it easily enough. Now, the context of this discussion is C (you introduced C a few articles upthread), and it was in that context that I made my remark, so I'll confine any syntactical comments here to C, but I think it's fair to say that the principles apply generally. There are two kinds of file scope object in C - those with external linkage, and those with no linkage. Let's firstly point out that file scope objects with external linkage are about as private as a World Wide Web page! Clearly they are not what "someone" meant. So that leaves us with file scope objects with no linkage. These are visible within the translation unit but not visible (by name) outside it. Therefore, they could indeed be used as private "class variables", so to speak. Nevertheless, you would struggle to use them as private "class instance variables" - you'd have to write all the code that let you decide which file scope object corresponded to which class instance. A real nuisance. Much simpler to encapsulate the hidden information within a struct. Keep the struct def private, only /declaring/ it in the header. And then pass a "this" pointer around the place. That is almost the way C++ does it, the principal differences being (a) that the 'this' pointer is done for you via a little syntactic sweetener, and (b) C++ doesn't bother to hide the fieldnames from the user-programmer, relying on a syntax error rather than information hiding! Of these, I think (a) is a plus, and (b) is a minus. Of course, you /can/ hide the fieldnames of a C++ class if you want, but it takes a bit of extra work, that's all. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk mail: rjh at above domain .