newbie question -- repetition in declarations

  • Thread starter Thread starter bubbakittee
  • Start date Start date
B

bubbakittee

I'm new to C#, so this may well be a dumb question.

All my declarations seem horribly repetitive. Things like

ReasonToLive dotnet = new ReasonToLive;

double[,] table = new double[5,5];

Is there some better (less repetitive) way to declare things ??

After all, I don't have to type:

double number = new double;

Does it make a difference (to the declaration style) whether
I use a struct or a class for ReasonToLive ??

Can I achieve more succinct declarations by allocating
storage on the stack (as opposed to the heap ??

thanks

bk
 
Hello BubbaKitee (I'd call you BK, but that name is associated with a
restaurant :-)

I am sorry that you find declarations distressing, but it is perfectly
normal to create a variable that is able to hold a REFERENCE to an object
without actually creating the object. The statements that you refer to:
ReasonToLive dotnet = new ReasonToLive();
are simple ones in which you both delcare the variable and create a new
object. However, this is not always the case. I could, just as easily, do
this:

ReasonToLive dotnet;

This creates an object that will be defined later. This is not an
interesting example, though. Much more interesting is this example:

ReasonToLive dotnet = MyLoveForCats.MakeReason();

Where the object 'MyLoveForCats' is a factory object that returns a new
'ReasonToLive' object when you call the 'MakeReason' method.
In fact, the design of object oriented systems often favors the use of the
'Factory Method' pattern, as this is called, because it hides the details
that may surround the proper creating of the object from the class that
needs simply to use one. This reduces coupling and increases cohesion
within system designs.

In other words, you are right to question the repetitiveness of this
construct. It does indicate that something does not 'smell' right.
However, that odor does not come from repetitveness. It comes from code
that may prove, in the long run, to be less maintainable and less flexible
that you will ultimately prefer. Using a Factory Method pattern will not
only get rid of the repetitiveness, but make your application more robust.

Strike now, while the iron is hot, and jump with both feet into Object
Oriented design.

Read this blog:
http://biztalkbum.blogspot.com/2004/07/how-to-learn-object-oriented.html
Then see this list:
http://www.amazon.com/exec/obidos/t...YU3/ref=cm_aya_av.lm_more/103-9848276-2065456

Good Luck,
--- Nick
 
Thanks, Nick,

I understand the principles of OO design (I think), it was just that
the language syntax looked goofy to me. I have become more
comfortable with the patterns over the last few hours of hacking,
but maybe that's from familiarity, rather than logic.

What about the other questions ...
double[,] table = new double[5,5];

Looks like that mess is unavoidable. I suppose I should
define a table class. That would probably help.
Does it make a difference (to the declaration style) whether
I use a struct or a class for ReasonToLive ??

Looks like the answer is no. Though there is some issue
with default parameterless constructors of structs ??
Can I achieve more succinct declarations by allocating
storage on the stack (as opposed to the heap ??

Again, "no" from what I've read so far.
Maybe I should go back to Python.

thanks again

bk
 
Looks like the answer is no. Though there is some issue
with default parameterless constructors of structs ??

Parameterelss constructors are the default "initalizer" constructor in C#.
They basically generate a struct with all of its fields set as the
default(0, null, or false).
Again, "no" from what I've read so far.
Maybe I should go back to Python.

No, there are no more succient declarations. Also you are confusing
declaration with initalization. As Nick pointed out

double[,] table = new double[5,5];

does two things, it declares a variable table with type double[,], and then
initalizes it with a new array object(new double[5,5]).

Since C# doesn't isn't dynamic in nature, as is python, the type of a
variable needs to be known. The langauge could fake it, but at this point C#
doesn't(and I would personally prefere that it never does).

However, if you want to do python and .NET, you may want to look into
IronPython, which is a python implementation that runs on the .NET runtime.

I do warn you, however, that if you want to write C# you should leave your
python methods and opinions at the door for a while. The langauge wasn't
designed to compete or even co-exist with python or its ideals and it is
built quite a bit differently. Trying to write C# like it is python will get
you no where.
 
Since C# doesn't isn't dynamic in nature, as is python,
the type of a variable needs to be known.

Yes. I'm willing to tell the compiler the types of my
variables. But it feels funny to have to type the same
word twice in the same line so often. I thought there
might be a better way. I thought something like

double[5,5] table;

would tell the compiler everything it needs to know.

I do warn you, however, that if you want to write C# you
should leave your python methods and opinions at the door
for a while.

Thanks for the tip.
 
bubbakittee said:
Since C# doesn't isn't dynamic in nature, as is python,
the type of a variable needs to be known.

Yes. I'm willing to tell the compiler the types of my
variables. But it feels funny to have to type the same
word twice in the same line so often. I thought there
might be a better way. I thought something like

double[5,5] table;

would tell the compiler everything it needs to know.

It does have enough information, but C# was designed to be pretty explicit
in alot of things, first and foremost that nothing is assigned implicitly,
so that sort of syntax isn't available.

Its a trade off between repeativness and explicity, C# chose the latter, ;).
 
Yes. I'm willing to tell the compiler the types of my
variables. But it feels funny to have to type the same
word twice in the same line so often. I thought there
might be a better way. I thought something like

double[5,5] table;

would tell the compiler everything it needs to know.

perhaps. However, it would also mean that this construct:

double[,] table;

and this construct

double [5,5] table;

would look very much alike, yet be fundamentally different in nature.
THe first would declare a variable, but the second would both declare the
variable and create an instance.

In the OO world, these are radically different things.

I, for one, need to know when I have created an object (and thus allocated
memory) and when I have not.
The language made the right choice, IMHO, by allowing me to explicitly
differentiate between the two at the expense of a few keystrokes.

--- Nick
 
Back
Top