Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

Never Ever...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
xlar54 - 18 Nov 2006 10:30 GMT
I've been going through the newsgroup, picking up best practices and
things not to do, as I think it helps to make a good programmer a very
good one.  But rather than fishing, I figure that there are many out
there who are tired of fixing someone else's poorly written code as it
relates to best practices, architecture, security and such.  So to the
group, if you had to post a list of "never ever do this..." or "always
do this..." what would that include?  We all know there's many ways to
do something, but some ways are clearly not good solutions.  My
expectations here is that I will learn quite a bit as will everyone
else.  Thanks for your replies.
Michael Nemtsev - 18 Nov 2006 11:58 GMT
Hello xlar54,

In general MS's practice and patterns + FXCorp is a good cases to check your
code (with reasonable approach)

BWT, some of us is writing each own articles, for example Jon Skeet's articles
(http://www.yoda.arachsys.com/csharp/) or Peter's http://www.eggheadcafe.com/articlelist.aspx.
There are some guys who run their faqs, for example Bob www.bobpowell.net/gdiplus_faq.htm

Besides this, I run my .NET tips and trics there http://spaces.msn.com/laflour/blog/cns!7575E2FFC19135B4!279.entry

It general
x> I've been going through the newsgroup, picking up best practices and
x> things not to do, as I think it helps to make a good programmer a
x> very good one.  But rather than fishing, I figure that there are many
x> out there who are tired of fixing someone else's poorly written code
x> as it relates to best practices, architecture, security and such.  So
x> to the group, if you had to post a list of "never ever do this..." or
x> "always do this..." what would that include?  We all know there's
x> many ways to do something, but some ways are clearly not good
x> solutions.  My expectations here is that I will learn quite a bit as
x> will everyone else.  Thanks for your replies.
x>
---
WBR,
Michael  Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
> Adrian < - 18 Nov 2006 16:24 GMT
> I figure that there are many out
> there who are tired of fixing someone else's poorly written code as it
> relates to best practices, architecture, security and such.

No need for further marmalade, here it is:

http://www.tiobe.com/standards/gemrcsharpcs.pdf

Adrian.
Registered User - 18 Nov 2006 19:59 GMT
>> I figure that there are many out
>> there who are tired of fixing someone else's poorly written code as it
[quoted text clipped - 3 lines]
>
>http://www.tiobe.com/standards/gemrcsharpcs.pdf

What does the document naming standard look like <g>?

regards
A.G.
Michael Nemtsev - 18 Nov 2006 16:52 GMT
Hello xlar54,

BTW, see the http://www.idesign.net site (MS Architects and other notable
guys run it) where in the Resource section is some guidelines

x> I've been going through the newsgroup, picking up best practices and
x> things not to do, as I think it helps to make a good programmer a
x> very good one.  But rather than fishing, I figure that there are many
x> out there who are tired of fixing someone else's poorly written code
x> as it relates to best practices, architecture, security and such.  So
x> to the group, if you had to post a list of "never ever do this..." or
x> "always do this..." what would that include?  We all know there's
x> many ways to do something, but some ways are clearly not good
x> solutions.  My expectations here is that I will learn quite a bit as
x> will everyone else.  Thanks for your replies.
x>
---
WBR,
Michael  Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
RobinS - 18 Nov 2006 19:07 GMT
Francesco Balena wrote a book called "Practical Guidelines
and Best Practices"; it's for both VB and C#. He has some
of it available for download on his website:

http://www.dotnet2themax.com/

I read this book from cover to cover; it has a lot of handy
tips, and information about "this way runs faster than that".

Robin S.

> I've been going through the newsgroup, picking up best practices and
> things not to do, as I think it helps to make a good programmer a very
[quoted text clipped - 6 lines]
> expectations here is that I will learn quite a bit as will everyone
> else.  Thanks for your replies.
Michael Nemtsev - 18 Nov 2006 21:33 GMT
Hello RobinS,

And just adding to your post, about books - the ".NET Gotachas" By Venkat
Subramaniam is really nice book about .NET tips

R> Francesco Balena wrote a book called "Practical Guidelines and Best
R> Practices"; it's for both VB and C#. He has some of it available for
R> download on his website:
R>
R> http://www.dotnet2themax.com/
R>
R> I read this book from cover to cover; it has a lot of handy tips, and
R> information about "this way runs faster than that".
R>
R> Robin S.
R>
R> "xlar54" <scott.hutter@gmail.com> wrote in message
R> news:1163845828.185845.243550@b28g2000cwb.googlegroups.com...
R>
>> I've been going through the newsgroup, picking up best practices and
>> things not to do, as I think it helps to make a good programmer a
[quoted text clipped - 6 lines]
>> solutions.  My expectations here is that I will learn quite a bit as
>> will everyone else.  Thanks for your replies.

---
WBR,
Michael  Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Dale - 18 Nov 2006 21:15 GMT
There are obviously thousands of things that can be done wrong but some of
the most common and avoidable things I see are the simplest things done
wrong, such as:

    if (someBooleanCondition)
    {
         myBool = true;
    }
    else
    {
         myBool = false
    }

instead of:

    myBool = someBooleanCondition;

related to the above, is #2:

    bool myBool = SomeBoolMethod();
    // apply #1 so no if/else
    return myBool;

instead of:
    return SomeBoolMethod();

or, #3,

    myInt = myInt + 1;

instead of:

    myInt++;

or even better:

    doSomethingWith(myInt++);

These are very fundamental coding style issues but applying them makes code
much more readable.

Dale
Signature

Dale Preston
MCAD C#
MCSE, MCDBA

> I've been going through the newsgroup, picking up best practices and
> things not to do, as I think it helps to make a good programmer a very
[quoted text clipped - 6 lines]
> expectations here is that I will learn quite a bit as will everyone
> else.  Thanks for your replies.
Bruce Wood - 19 Nov 2006 07:26 GMT
> related to the above, is #2:
>
[quoted text clipped - 4 lines]
> instead of:
>      return SomeBoolMethod();

...just be aware that some programmers (including yours truly) do the
two-step method because in VS2003 it's the only way you can see what
the result will be in the debugger. I believe that VS2005 allows you to
see function results at the return statement, but VS2003 definitely
doesn't.
Dave Sexton - 19 Nov 2006 14:41 GMT
Hi Bruce,

> I believe that VS2005 allows you to
> see function results at the return statement, but VS2003 definitely
> doesn't.

How?

That would be great, but it doesn't seem to work for me.

Signature

Dave Sexton

>> related to the above, is #2:
>>
[quoted text clipped - 10 lines]
> see function results at the return statement, but VS2003 definitely
> doesn't.
Sericinus hunter - 20 Nov 2006 14:41 GMT
> Hi Bruce,
>
[quoted text clipped - 5 lines]
>
> That would be great, but it doesn't seem to work for me.

   If I remember correctly, it is in Autos window, it has a special icon
which looks like an arrow on the Enter key.
Dave Sexton - 20 Nov 2006 15:12 GMT
Hi,

Thanks for the reply, but I don't see it.

No arrow here: VS 2005

Signature

Dave Sexton

>> Hi Bruce,
>>
[quoted text clipped - 8 lines]
>    If I remember correctly, it is in Autos window, it has a special icon
> which looks like an arrow on the Enter key.
Sericinus hunter - 20 Nov 2006 16:49 GMT
Hm... I remember a co-worker showing me this, but cannot find it
myself. Probably confused with something else.

> Hi,
>
> Thanks for the reply, but I don't see it.
>
> No arrow here: VS 2005
Sericinus hunter - 20 Nov 2006 14:36 GMT
...
> ...just be aware that some programmers (including yours truly) do the
> two-step method because in VS2003 it's the only way you can see what
> the result will be in the debugger. I believe that VS2005 allows you to
> see function results at the return statement, but VS2003 definitely
> doesn't.

   If you open Register window (it is under Debug/Windows), you will
see the return value in EAX register.
Dave Sexton - 20 Nov 2006 15:11 GMT
Hi,

Ok, but that's not useful to me at all :)

Signature

Dave Sexton

> ...
>> ...just be aware that some programmers (including yours truly) do the
[quoted text clipped - 5 lines]
>    If you open Register window (it is under Debug/Windows), you will
> see the return value in EAX register.
Jon Skeet [C# MVP] - 19 Nov 2006 08:21 GMT
> or, #3,
>
[quoted text clipped - 7 lines]
>
>      doSomethingWith(myInt++);

I avoid using increment/decrement operators within other statements
where possible. I believe the above is more clearly written (in terms
of chronology)

doSomethingWith(myInt);
myInt++;

I don't know about anyone else, but for me it requires just that little
bit less thought.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Cor Ligthert [MVP] - 19 Nov 2006 08:29 GMT
Really,

I always have the idea that beside property setting this is the statement
that I use most.

for (int i;i<list.length;i++){}

Cor

>> or, #3,
>>
[quoted text clipped - 17 lines]
> I don't know about anyone else, but for me it requires just that little
> bit less thought.
Jon Skeet [C# MVP] - 19 Nov 2006 15:18 GMT
> I always have the idea that beside property setting this is the statement
> that I use most.
>
> for (int i;i<list.length;i++){}

Yes, I pretty much assumed that exception was a "given". In particular,
there's no interaction between the three statement-expression-list bits
of the statement.

I agree I wasn't being strictly accurate though.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dale - 19 Nov 2006 20:03 GMT
What I like about using the increment operator in the method call like I
showed is that it connects the incrementing directly to that method call - in
other words, "because I called that method, now is the time to increment the
variable."

If that statement is not true, then I may use a separate line for the
statement but in general, I don't like lines that say:

    myVariable++;

Dale

Signature

Dale Preston
MCAD C#
MCSE, MCDBA

> > I always have the idea that beside property setting this is the statement
> > that I use most.
[quoted text clipped - 6 lines]
>
> I agree I wasn't being strictly accurate though.
Stephany Young - 18 Nov 2006 22:05 GMT
As to a pair of 'never ever do this...'/'always do this...' lists, I would
take the advice of James Bond (paraphrased) and 'never say never'.

As to a pair of 'these are good techniques...'/'these are not good
techniques...' lists, they would be endless.

But, who is to say what is or is not a good technique in any given
circumstances?

Dale, in this thread, has said that that

 if (someBooleanCondition)
 {
   myBool = true;
 }
 else
 {
   myBool = false;
 }

is bad as compared to:

 myBool = someBooleanCondition;

In general I agree, however, both have their uses.

In a Logic 101 class, the former would be preferred to illustrate a point of
logic, wheras in a C# 101 class, the latter would be preferred to illustrate
a nuance of the language.

Likewise, while:

   doSomethingWith(myInt++);

is perfectly valid from the development coders point of view, from the
maintenance coders point of view it becomes less valid because he/she would
need to go off to the implementation of doSomethingWith to make sure that it
is not defined as doSomethingWith(ref int var) and that the content of var
is not being modified within doSomethingWith.

> I've been going through the newsgroup, picking up best practices and
> things not to do, as I think it helps to make a good programmer a very
[quoted text clipped - 6 lines]
> expectations here is that I will learn quite a bit as will everyone
> else.  Thanks for your replies.
xlar54 - 19 Nov 2006 00:45 GMT
>   if (someBooleanCondition)
>   {
[quoted text clipped - 10 lines]
>
> In general I agree, however, both have their uses.

I would see this and immediately think - why doesn't the compiler see
this as a perfect opportunity to optimize?  Assuming there is no other
activity within the braces, it _appears_ very reasonable for the
compiler to internally optimize this to:

myBool = someBooleanCondition;

Which does raise the point, when we talk about performance and
optimizations, what do we know about the compiler itself and its
ability to optimize code like the above.
Stephany Young - 19 Nov 2006 03:08 GMT
It may well do.

You could compile both and have a look at the IL, if you are interested.

The next point is a very good one.

The amount of machine resource available tends to make a bit lazy, because
we don't really have to worry about such things these days. It's a far cry
from the days when we had to code, (in COBOL), an ALTERED GOTO instaed of a
PERFORM to save 8 bytes of core memory.

>>   if (someBooleanCondition)
>>   {
[quoted text clipped - 21 lines]
> optimizations, what do we know about the compiler itself and its
> ability to optimize code like the above.
Dave Sexton - 19 Nov 2006 03:49 GMT
Hi Stephany,

> You could compile both and have a look at the IL, if you are interested.

It's not optimized out.

Signature

Dave Sexton

> It may well do.
>
[quoted text clipped - 31 lines]
>> optimizations, what do we know about the compiler itself and its
>> ability to optimize code like the above.
Stephany Young - 19 Nov 2006 05:01 GMT
It wasn't me that was interested Dave.

I was hinting to the OP that, seeing as he was so interested, that he start
digging around in the IL to find out waht was optimized and what wasn't.

> Hi Stephany,
>
[quoted text clipped - 38 lines]
>>> optimizations, what do we know about the compiler itself and its
>>> ability to optimize code like the above.
Dave Sexton - 19 Nov 2006 05:33 GMT
Hi Stephany,

I know.  I was just validating for the group :)

Signature

Dave Sexton

> It wasn't me that was interested Dave.
>
[quoted text clipped - 42 lines]
>>>> optimizations, what do we know about the compiler itself and its
>>>> ability to optimize code like the above.
Cor Ligthert [MVP] - 19 Nov 2006 05:13 GMT
Dave,

As far as I know, there are two optimizing phases. However, I hate those who
set a boolean in a property with only that, you become crazy of that while
debugging.

Just my idea,

Cor

> Hi Stephany,
>
[quoted text clipped - 38 lines]
>>> optimizations, what do we know about the compiler itself and its
>>> ability to optimize code like the above.
Dave Sexton - 19 Nov 2006 05:36 GMT
Hi Cor,

What two optimizing phases do you mean?

I tested this in the Debug configuration without optimization checked in the properties window for
my VS 2005 project, and in Release mode with optimization checked.

Neither test optimized out the code.

And I agree that code is too verbose for such simple logic.

Signature

Dave Sexton

> Dave,
>
[quoted text clipped - 46 lines]
>>>> optimizations, what do we know about the compiler itself and its
>>>> ability to optimize code like the above.
Cor Ligthert [MVP] - 19 Nov 2006 06:21 GMT
Dave,

I am one of those who are not really interested in optimizing for
nanoseconds. I am a guy focused on mainenance, therefore I never look for a
nanosecond.

My reply was based on some messages I thought that I had seen from Jay
Harlow, who had tested the casting against the converting: "as" against "()"

Although the code in ILS was different was it (I thought to have understood)
in the real processing the same. That is all I know.

Cor

> Hi Cor,
>
[quoted text clipped - 62 lines]
>>>>> optimizations, what do we know about the compiler itself and its
>>>>> ability to optimize code like the above.
Dale - 19 Nov 2006 20:24 GMT
I'm with you on the optimizing thing.  While we can develop coding habits
that create reasonably optimal code, beyond that source-code level
optimization should happen only when driven by performance issues.

I am a big fan of Martin Fowler's Refactoring.  In it, he says something to
the effect that readable and maintainable code is almost always not the most
optimized code.

In my experience and opinion, when you compare the man-hours cost by those
optimization deficiencies compared to the man-hours it would take to optimize
and maintain the optimized code, it is always better to write readable code
than it is to write optimized code.

Signature

Dale Preston
MCAD C#
MCSE, MCDBA

> Dave,
>
[quoted text clipped - 76 lines]
> >>>>> optimizations, what do we know about the compiler itself and its
> >>>>> ability to optimize code like the above.
Bruce Wood - 19 Nov 2006 07:38 GMT
> Hi Cor,
>
[quoted text clipped - 4 lines]
>
> Neither test optimized out the code.

Remember that after the compiler produces the IL, the JITter gets a
hold of it at runtime and transforms it into actual machine code, and
optimization takes place during the JIT compile, as well. It may be
eventually optimized out. We just now know that if it is in fact
optimized out the optimization isn't at compile time.

Given this, though, it's probably safe to say that it won't be
optimized. It looks like the kind of thing that the compiler should
deal with, not the JITter.
Dave Sexton - 19 Nov 2006 14:38 GMT
Hi Bruce,

Good point - thanks.

Signature

Dave Sexton

>> Hi Cor,
>>
[quoted text clipped - 15 lines]
> optimized. It looks like the kind of thing that the compiler should
> deal with, not the JITter.
Andy - 20 Nov 2006 13:02 GMT
Dave,

The JIT compiler also does some optimization at runtime.

Andy

> Hi Cor,
>
[quoted text clipped - 63 lines]
> >>>> optimizations, what do we know about the compiler itself and its
> >>>> ability to optimize code like the above.
Dave Sexton - 20 Nov 2006 14:39 GMT
Hi Andy,

Yes, thank you.  I forgot about that - Bruce Wood already pointed that out to me.

Signature

Dave Sexton

> Dave,
>
[quoted text clipped - 71 lines]
>> >>>> optimizations, what do we know about the compiler itself and its
>> >>>> ability to optimize code like the above.
Dale - 19 Nov 2006 20:14 GMT
Hate's a strong word.  Couldn't you just dislike them a lot? :)
Signature

Dale Preston
MCAD C#
MCSE, MCDBA

> Dave,
>
[quoted text clipped - 48 lines]
> >>> optimizations, what do we know about the compiler itself and its
> >>> ability to optimize code like the above.
Chris Dunaway - 20 Nov 2006 18:14 GMT
> Hi Stephany,
>
> > You could compile both and have a look at the IL, if you are interested.
>
> It's not optimized out.

Is that true of the JIT compiler?  It may not be optimized out in the
IL, but it might be optimized out when JIT compiled.
Dave Sexton - 20 Nov 2006 18:57 GMT
Hi Chris,

Yes, others have mentioned that as well.

But Bruce Wood has made a good point that something as simple as this will most likely not be
checked at runtime, but instead checked at compile-time, which it's not.  It's probably safe to
assume that the JIT process will not optimize this out if the compiler doesn't already.  JIT
performance would be decreased for no good reason to check for innocuous things such as this.

To be perfectly honest though, who really cares? :)

I don't know of any application that it would make any difference in performance using one construct
over the other.  My personal preference is for simplicity and readability, however.

Signature

Dave Sexton

>> Hi Stephany,
>>
[quoted text clipped - 4 lines]
> Is that true of the JIT compiler?  It may not be optimized out in the
> IL, but it might be optimized out when JIT compiled.
Dale - 19 Nov 2006 20:12 GMT
Even if the optimizer did (which according to Dave Sexton it does not)
optimize out the difference between the two forms, you should still write the
shorter version.  Reading clear, concise code takes much less effort than
reading verbose code.  

Dale

Signature

Dale Preston
MCAD C#
MCSE, MCDBA

> >   if (someBooleanCondition)
> >   {
[quoted text clipped - 21 lines]
> optimizations, what do we know about the compiler itself and its
> ability to optimize code like the above.
Jon Skeet [C# MVP] - 19 Nov 2006 20:28 GMT
> Even if the optimizer did (which according to Dave Sexton it does not)
> optimize out the difference between the two forms, you should still write the
> shorter version.  Reading clear, concise code takes much less effort than
> reading verbose code.  

On the other hand, one could easily have written that reading clear,
verbose code takes much less effort than reading short code.

It's easy to make code shorter - removing whitespace and making
variables and method names tiny does that. Making code concise in a way
which increases clarity is an art - and a subjective one at that, as
this thread has already proven.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Bruce Wood - 19 Nov 2006 07:34 GMT
> Likewise, while:
>
[quoted text clipped - 5 lines]
> is not defined as doSomethingWith(ref int var) and that the content of var
> is not being modified within doSomethingWith.

Not true in C#. If doSomethingWith is defined as taking a (ref int var)
parameter, then the call would have to look like this:

doSomethingWith(ref myInt)

and could not contain an expression. In C# ref and out parameters must
be specified as ref and out arguments by the caller, so there can be no
surprises. Furthermore, if an argument is ref or out then you must pass
a variable, not an expression.

The case does come up, however, for fields:

doSomethingWith(this.myInt++)

is potentially confusing (but not terribly) if doSomethingWith modifies
/ uses this.myInt internally. It's not terribly confusing because C#
clearly defined the order in which these operations proceed (arguments
must be evaluated before the function is called), so in the end it's
unambiguous.

All of that said, I'm not a fan of cramming as many operations into a
line of code as possible. One of the items on my list of "never, evers"
would have to be constructs like this:

int result;
while ((result = CallSomeFunction(a, b)) > 0)
{
   ...
}

Blech. I'd rather repeat the function call than have to look at this,
but I recognize that that's a matter of taste.
Jon Skeet [C# MVP] - 19 Nov 2006 08:24 GMT
> All of that said, I'm not a fan of cramming as many operations into a
> line of code as possible. One of the items on my list of "never, evers"
[quoted text clipped - 8 lines]
> Blech. I'd rather repeat the function call than have to look at this,
> but I recognize that that's a matter of taste.

I agree, *except* in the case of reading streams and StreamReaders. I'm
so familiar with:

int bytesRead;
while ( (bytesReader = someStream.Read(...)) > 0)
{
}

and

string line;
while ( (line = streamReader.ReadLine()) != null)
{
}

that those become simpler for me than the repeated method call.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dale - 19 Nov 2006 20:31 GMT
Again, I agree with you, Jon.  This is a case where the more concise form may
require learning more details of the language but, just as it is with
vocabulary in spoken language, more efficient communication happens with
knowledge, practice, and experience.  The more you use those concise syntax
forms, the more they become part of your every day usage.

Dale
Signature

Dale Preston
MCAD C#
MCSE, MCDBA

> > All of that said, I'm not a fan of cramming as many operations into a
> > line of code as possible. One of the items on my list of "never, evers"
[quoted text clipped - 25 lines]
>
> that those become simpler for me than the repeated method call.
Dale - 19 Nov 2006 20:43 GMT
I thought we were talking about coding practices and not logic class
practices :).
Signature

Dale Preston
MCAD C#
MCSE, MCDBA

> As to a pair of 'never ever do this...'/'always do this...' lists, I would
> take the advice of James Bond (paraphrased) and 'never say never'.
[quoted text clipped - 46 lines]
> > expectations here is that I will learn quite a bit as will everyone
> > else.  Thanks for your replies.
Bruce Wood - 19 Nov 2006 07:49 GMT
> I've been going through the newsgroup, picking up best practices and
> things not to do, as I think it helps to make a good programmer a very
[quoted text clipped - 6 lines]
> expectations here is that I will learn quite a bit as will everyone
> else.  Thanks for your replies.

Number one on my list of foolish things to do in any language is "a
priori" micro-optimization. By that I mean spending hours (or even
minutes) tweaking your code to "improve its speed" before you've
determined that it's slow (and what, exactly, is slow) and needs some
help. Even then, the solution is usually at a higher level: in the
design, in the choice of data structures or things like that.

Of course, it's perfectly legitimate to prefer some constructs over
others if experience has taught you that they're more efficient.

As well, there are areas of computing, and types of problems, that
require screaming tight code. Even in these areas, however, it's a
fool's game to optimize every line of code. The approach is the usual
one: create the best design you can, use the most appropriate data
structures you can, then run your program and find the trouble spots,
and optimize those.

So many people post in this newsgroup, asking whether classes or
structs are "more efficient," or stressing over the cost of boxing,
when in fact they haven't started writing their app yet. (The recent
discussion on boxing overhead excepted: the OP already had a working
app in another language and had a specific question.) So many newbies
think that the way to write good code is to torture and twist the
language to "make it fast" while at the same time they create painfully
bad designs that introduce massive overhead that code tweaking can't
begin to address.

The dumbest "optimization" I've ever seen was this (in C)... I quote it
complete with comment:

// Unwind loop to save clock cycles incrementing loop counter
int a[10];
a[0] = 1000;
a[1] = 1000;
...
a[7] = 1000;
a[8] = 1000;
a[9] = 1000;

Not only was this in the initialization phase of the program (so it
would occur exactly once per program run) but the rest of the program
then built a SQL query and headed off to a database to pull in a few
thousand rows of data. This guy shaved a few microseconds off a program
that ran for five minutes.
Dave Sexton - 19 Nov 2006 14:51 GMT
Hi Bruce,

> The approach is the usual
> one: create the best design you can, use the most appropriate data
> structures you can, then run your program and find the trouble spots,
> and optimize those.

And I'd just like to add that it's not unlikely you'll have to ditch some, or even most, of the
design and start from scratch.  When designing high-performance systems it's difficult to account
for everything without prototyping first and running some tests.

Signature

Dave Sexton

>> I've been going through the newsgroup, picking up best practices and
>> things not to do, as I think it helps to make a good programmer a very
[quoted text clipped - 51 lines]
> thousand rows of data. This guy shaved a few microseconds off a program
> that ran for five minutes.
William Stacey [C# MVP] - 19 Nov 2006 17:05 GMT
:-)  Good one.  I have felt this pain recently.  Another reason to just do a
very thin skeleton first and get basic things working to test your design
and shake out many things you did not think about - resist random stream of
thought and the temptation to add more function then minimal (surely we
never do that ;).  The hard problems (e.g. concurrency, atomic, etc) are
much harder to see and debug with all the junk in the way.  When it feels
good, then add all those nice overloads and properties and keep unit testing
along the way.  Don't push a bad design and think you can "fix" it with more
code.
MS had to do this with Vista.  I think over million lines of code went in
the basket.  Hindsight says that was a great desision as they ended up with
a better product.
Signature

William Stacey [C# MVP]

| Hi Bruce,
|
[quoted text clipped - 62 lines]
| > thousand rows of data. This guy shaved a few microseconds off a program
| > that ran for five minutes.
Dave Sexton - 19 Nov 2006 17:40 GMT
Hi William,

<snip>

> Don't push a bad design and think you can "fix" it with more
> code.

Great advice :)

> MS had to do this with Vista.  I think over million lines of code went in
> the basket.  Hindsight says that was a great desision as they ended up with
> a better product.

I read in a blog 50M lines in Vista and 24 hour builds.  That's insane :|

Signature

Dave Sexton

William Stacey [C# MVP] - 19 Nov 2006 19:21 GMT
Your probably closer.  They still build on an IBM mainframe?  I think I
remember they used to do that with NT.

Signature

William Stacey [C# MVP]

| Hi William,
|
[quoted text clipped - 10 lines]
|
| I read in a blog 50M lines in Vista and 24 hour builds.  That's insane :|
Dave Sexton - 19 Nov 2006 19:32 GMT
Hi William,

But then again, I'm not sure if they actually started from scratch at 50M.  They may have dumped 1M
and ended up with 50M :)

For nightly builds do they start the day before?  ;)

Signature

Dave Sexton

> Your probably closer.  They still build on an IBM mainframe?  I think I
> remember they used to do that with NT.
[quoted text clipped - 15 lines]
> |
> | I read in a blog 50M lines in Vista and 24 hour builds.  That's insane :|
Mark R. Dawson - 20 Nov 2006 01:33 GMT
Hi Dave,
 check-in cut off time is 7PM, that is the time when the build guys kick
off a build,  the build (as long as there are no build breaks) are available
around lunch time the next day :-)  

Mark.
Signature

http://www.markdawson.org

> Hi William,
>
[quoted text clipped - 22 lines]
> > |
> > | I read in a blog 50M lines in Vista and 24 hour builds.  That's insane :|
Dave Sexton - 20 Nov 2006 14:41 GMT
Hi Mark,

Do they build the components serially, concurrently or mixed?

Signature

Dave Sexton

> Hi Dave,
>  check-in cut off time is 7PM, that is the time when the build guys kick
[quoted text clipped - 30 lines]
>> > |
>> > | I read in a blog 50M lines in Vista and 24 hour builds.  That's insane :|
Mark R. Dawson - 20 Nov 2006 18:30 GMT
what do you mean by components?

Mark
Signature

http://www.markdawson.org

> Hi Mark,
>
[quoted text clipped - 34 lines]
> >> > |
> >> > | I read in a blog 50M lines in Vista and 24 hour builds.  That's insane :|
Dave Sexton - 20 Nov 2006 19:00 GMT
Hi Mark,

Well Vista doesn't consist of a single C++ project, right?

And I assume there is an elaborate framework of relationships between link libraries that need to be
built in a specific order.  So I was wondering if any of these smaller components are built
concurrently, to save time, or if the build process just orders by dependency and builds serially.

Signature

Dave Sexton

> what do you mean by components?
>
[quoted text clipped - 39 lines]
>> >> > |
>> >> > | I read in a blog 50M lines in Vista and 24 hour builds.  That's insane :|
Mark R. Dawson - 20 Nov 2006 20:21 GMT
Dependencies are specified in the build scripts,  whether or not the build is
smart enough to build mutually exclusive modules concurrently, that I do not
know :-)

Mark.
Signature

http://www.markdawson.org

> Hi Mark,
>
[quoted text clipped - 47 lines]
> >> >> > |
> >> >> > | I read in a blog 50M lines in Vista and 24 hour builds.  That's insane :|
Dale - 19 Nov 2006 20:37 GMT
Remeber Creative Writing 101 in college?  I'm sure your instructor told you
to write your story and then go back and cut it to about 25% of the words you
started with.

The same applies to code though the percentages get better, just as in
writing, when we've learned by cutting out code so much that we write less
code that we will just have to remove later.

Dale
Signature

Dale Preston
MCAD C#
MCSE, MCDBA

> :-)  Good one.  I have felt this pain recently.  Another reason to just do a
> very thin skeleton first and get basic things working to test your design
[quoted text clipped - 76 lines]
> | > thousand rows of data. This guy shaved a few microseconds off a program
> | > that ran for five minutes.
William Stacey [C# MVP] - 19 Nov 2006 21:23 GMT
Agree.  Some wise words:

- A protocol design is not complete when there is nothing left to add, it is
complete when there is nothing left to remove.

- Any one can make something complicated, what is hard is to make it simple.

Signature

William Stacey [C# MVP]

| Remeber Creative Writing 101 in college?  I'm sure your instructor told you
| to write your story and then go back and cut it to about 25% of the words you
[quoted text clipped - 86 lines]
| > | > thousand rows of data. This guy shaved a few microseconds off a program
| > | > that ran for five minutes.
Dave Sexton - 19 Nov 2006 21:36 GMT
Hi Dale,

> Remeber Creative Writing 101 in college?

No.

Good "conditioning" analogy though :)

Signature

Dave Sexton

> Remeber Creative Writing 101 in college?  I'm sure your instructor told you
> to write your story and then go back and cut it to about 25% of the words you
[quoted text clipped - 86 lines]
>> | > thousand rows of data. This guy shaved a few microseconds off a program
>> | > that ran for five minutes.
Ebbe Kristensen - 20 Nov 2006 11:06 GMT
> So to the
> group, if you had to post a list of "never ever do this..." or "always
> do this..." what would that include?

If you don't already, start using a a revision control system. If nothing
else is mandated, I use Subversion (http://subversion.tigris.org). It's
platform-agnostic and free.

As for don'ts, the site http://www.thedailywtf.com contains loads of
examples.

Ebbe

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.