Better program design?

J

JoeC

I've taken a while to answer this, largely because it's a hard question
to answer well and I've had to think a while about how to say what I
mean and have any hope of getting my point across.

Good code is often a bit like an allegory -- many of the "characters"
are a lot more pure and idealized forms of some particular idea than
you'd usually deal with in real life. In a book or movie, that can lead
to one-dimensionality -- but in code, we're usually more concerned with
clarity and efficency than depth. Much as a politician tries to portray
a world in whcih "we" are pure good and "they" are pure evil, it's often
convenient and useful for us to create characters as the pure essence of
a single idea.

As character development goes, it tends to stink. It's too quick and
easy to understand a character in a novel who's too one-dimensional, so
they lose our interest fairly quickly -- they're too predictable so it's
hardly worth reading the novel to find out how they'll act in a
particular situation.

For code, however, that's much more what we want -- something that
quickly becomes almost boring, because we hardly have to think to know
how it'll act or react in any given situation. Something we in which we
can quickly lose interest because it's obvious and predictable.

Like an overly-obvious allegory, we also want it to be obvious exactly
what concept each of our characters represents. In fact, we usually go
much further than even a really obvious allegory, and do our best to
make a single character represent only a single concept, with no other
characteristics at all.

Then, when we need a "character" with more than one characteristic, we
literally compose it out of the individual characteristics we've already
defined. IOW, we create a class that directly includes instances of
those other classes the represent the characteristics.

In practice, that means well-written code tends to be built up in
relatively small steps. Each step adds only a _few_ simpler concepts
together into one slightly more complex concept, but (with skill) does
so in a way that's still simple and straightforward to understand.

To use a rather different simile: your code is a bit like a spandex
mini-skirt on woman in her mid-50's. We're only separated from some
things that are less than beautiful by one thin layer.

What we'd like is more like a belly-dancer's costume -- lots of layers
that don't cling too closely to each other. They look a lot more
transparent than the mini-skirt, but still do more to cover up any warts
underneath.

--
Later,
Jerry.

The universe is a figment of its own imagination.

I think I get it. My code is not abstract enough. I am inferring that
I need to have more data hidden in objects and functions. For
starters, I wanted to create seperate functions for paint and create
but my compiler complained. First I am trying to get the program to
work the way I want then try to make it better. The problem I keep
adding and expanding my program and never get back to making it better
part. I should do somthing to make the color object better. That
shouldn't be too hard to put the color statment inside the object.
Still I have made some improvment where I see the opertunity.
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

Phlip said:
Why aren't these public?

(Shameless plug warning)

JoeC,

You may find this article on my web site useful as something to play
with. It shows how to use templates to make the members public, but
retains the syntax of the get/set members so you can more easily change
your mind later. It's no where near as complex as it might sound.

http://www.kirit.com/C++ killed the get & set accessors


K
 
J

Jerry Coffin

[ ... ]
I think I get it. My code is not abstract enough.

It's not just lack of abstraction -- it's that you're trying to build
too big of an abstraction in one step. You're starting with primitive
things like ints, and attempting to proceed directly to something with
color, movement, ability to draw itself, keep track of where it is on a
map, etc., etc.
I am inferring that
I need to have more data hidden in objects and functions. For
starters, I wanted to create seperate functions for paint and create
but my compiler complained. First I am trying to get the program to
work the way I want then try to make it better. The problem I keep
adding and expanding my program and never get back to making it better
part.

Yes, it's always hard to balance the two -- but if you have to make a
choice, I think you've made the right now. A working program that's a
bit ugly inside is worth a lot more than beautifully constructed code
that doesn't accomplish anything useful.
 
J

JoeC

Yes, it's always hard to balance the two -- but if you have to make a
choice, I think you've made the right now. A working program that's a
bit ugly inside is worth a lot more than beautifully constructed code
that doesn't accomplish anything useful.

At times I have gone back and inproved my program. I have removed
redundancy in my move commands. I took several similliar statments and
put them into functions. I also went back and improved the workings of
my graphics object. I like the idea of putting the color functions as
a member of the objects and have the color statemnt built in. Right
now I am slowly trying the create a fighting function. That is pretty
tough, first I have to sort the units that are elegible to fight then I
have to find some way to implement an odds table. 1:1 2:1 an on. The
affects are dispersed and eleminated for the attacker or defender.

In the long view I have a full game in my head, some large scale
project I would someday like to create. Right now I call it sim
civilization. I came up with a system that resembles different history
books that I have read and my experience in war and the military. So
far for the look and feel of the game I will have to get very
proficient with dialog boxes because the basis of the game is human and
resopurce managment. Will you have more people allocated to trade or
military or as priests. Based on your technology, how far can your
project a military and of what size. Still all of what I have in my
mind is way byond what I can actually write so I do what I can.
 

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

Similar Threads


Staff online

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top