www.digitalmars.com         C & C++   DMDScript  

c++ - Good programming practices Q.

reply E. Trelmar <E._member pathlink.com> writes:
Thanks for the rather expansive amount of posts to my question, they were quite
helpful.

While not new to code design, programming, or format, I don't follow any
particular standard for programming (In C++ or ASM), and having searched around
a lot for acceptable programming styles, I've found that my own seem to be
thought poorly of. I tend to follow a style of 'whatever is easiest to code and
produces an acceptable result'. Any suggestions for good sites on not only
maintainable, acceptable code, but the reasons the standards are the way they
are would be appreciated. (I am concerned not so much with readability as size
and speed, beings that I am given that leisure/curse of working only with a few
others)

Finally, two particular questions:
Why is it that global variables are thought so poorly of, I haven't ever
received a definate answer. Is it access speed, or the possibility for the
corruption of the contents, or something else?

I use, currently, an array of global variables of different byte sizes for the
majority of my variables, and use defines for offsets to index them, and I am
wondering if there is a way to do this more efficiently but with the same style?

(An example of my variable style)

#define RPRS            0      // Key buffer position
#define GLOBAT          1      // Address of global color
#define PROGRAMSTATE    2      // State id
#define TOPX            3      // Top Coordinates, X
#define TOPY            4      // Top Coordinates, Y
..
..
#define NOGFOLDER      280      // True if there was no global folder
#define NOGFOLDERD     281      // True if there was no drive for global folder
#define CDEF           282      // Number of character defines

char c[5000],                        // Character buffer, access characters
through this array
*searchstring = &c[CDEF + 200], // Scratch buffer for searching (80)
*filestring = &c[CDEF + 280],   // Name of local data file (80)
*cwdir = &c[CDEF + 360],        // Current Working Directory (80)
short i[2500],                       // Short buffer, access 2 byte integers
etc.

Any advice for a somewhat capable, yet not exposed, programmer, would help.
Jun 14 2002
next sibling parent Jan Knepper <jan smartsoft.cc> writes:
"E. Trelmar" wrote:

 char c[5000],                        // Character buffer, access characters
 through this array
 *searchstring = &c[CDEF + 200], // Scratch buffer for searching (80)
 *filestring = &c[CDEF + 280],   // Name of local data file (80)
 *cwdir = &c[CDEF + 360],        // Current Working Directory (80)
I only would do this if I had to change things in a buffer. I would rather use: const size_t SEARCHSTRING = 80; const size_t FILESTRING = FILENAME_MAX; const size_t CWDIR = FILENAME_MAX; char searchstring [ SEARCHSTRING ]; char filestring [ FILESTRING ]; char cwdir [ CWDIR ]; Jan
Jun 14 2002
prev sibling parent "Walter" <walter digitalmars.com> writes:
"E. Trelmar" <E._member pathlink.com> wrote in message
news:aed45k$5rm$1 digitaldaemon.com...
 Why is it that global variables are thought so poorly of, I haven't ever
 received a definate answer. Is it access speed, or the possibility for the
 corruption of the contents, or something else?
Two reasons: 1) Global variables don't work well with multithreaded program, and are almost always a subtle bug. The trouble comes if two threads try to access a single global at the same time. 2) Any function can read/write globals, which means that in a large program, it becomes hard to follow the logic of who reads the globals and who writes them.
Jun 14 2002