Writing better program?

J

JoeC

werasm said:
Cut-and-pasting is bad. When things need to change fast, they tend to
fall apart if cut-and-paste stile programming were used. Get into the
habit of avoiding cut and pasting at all costs - serious (even forget
the key sequence, if need be).

Yeh, I know that cut and paste is bad but I was trying to make the
point of how somthing simple could be done better. One of the reasons
why I try to avoid longer names is a basic typing errors. Worse if I
use names that are too close and put the wrong name in. It happens in
other things I do.

As far as const go, this is a little
rule I follow myself. If the value needs to be part of my public
interface, or if it needs to be used in the class definition, then I
use an anonymous enumerated type:

example:

class X
{
//...
enum {Sz = 20 };
char array_[Sz];
};

I don't realy use enum lists much, I think I used them one or two
times. I have seen them in books and how to do it but have never realy
been shown the value for writing better programs. In my current work I
can't see where they would improve my code.
If on the other hand, the value is not required as part of the
definition, I use enumerators in my source file within anonymous
namespace (or static consts). The value of static consts can be
specified at the definition (during initialisation).

You lost me here. I have used static const where I need to but mostly
I am trying to take small steps to improve my code. Programming is
just a bunch of small steps.
[snip]

Most of the above is more about graphics, and less about C++
specifically, I therefore cannot comment, other than what has already
been said.

Thanks, I my graphics are not the focus of what I am doing but they
serve their purpose.
 
W

werasm

JoeC said:
Yeh, I know that cut and paste is bad but I was trying to make the
point of how somthing simple could be done better. One of the reasons
why I try to avoid longer names is a basic typing errors. Worse if I
use names that are too close and put the wrong name in. It happens in
other things I do.

With regards to naming conventions, a rule I used to follow was as
short as possible to be descriptive. I later realized that what is
considered as being descriptive is very subjective. If you know the
domain, that could be very short, but not clear for someone maintaining
your code. I now use the philosophy - as clear and concise as possible.
Spelling errors are the least of my concern. If you can spell
phonetically and the concept is brought over, it is better than using a
short non-descriptive name.
As far as const go, this is a little
rule I follow myself. If the value needs to be part of my public
interface, or if it needs to be used in the class definition, then I
use an anonymous enumerated type:

example:

class X
{
//...
enum {Sz = 20 };
char array_[Sz];
};

I don't realy use enum lists much, I think I used them one or two
times. I have seen them in books and how to do it but have never realy
been shown the value for writing better programs. In my current work I
can't see where they would improve my code.

The benefits of enum are for me:

1) You don't need to provide a declaration and definition (like with
statics). If you want to save typing, use enums :).

2) The minimal type is automatically selected for you. If your range is
less than 128, it is stored within a byte if less than 32768, it may
be stored within a short, etc...


Here I was referring to the class definition:

class XClass //Definition starts here!
{

/*
If a value is not required here (as part of the
interface of the class, so to speak, then don't
define the constant here, as when it changes,
all dependents require unecessary recompilation.
Recompilation times to become an issue in large
projects...
*/

};//Definition ends here!

Thanks, I my graphics are not the focus of what I am doing but they
serve their purpose.

Ok, but I can't comment on that - just the C++.

Kind regards,

Werner
 
J

JoeC

The benefits of enum are for me:

1) You don't need to provide a declaration and definition (like with
statics). If you want to save typing, use enums :).

2) The minimal type is automatically selected for you. If your range is
less than 128, it is stored within a byte if less than 32768, it may
be stored within a short, etc...

I will look into practicing enum lists so I can learn how to use them
and see if they are helpful for for my project.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top