Michael C said:
I'm sure you do some optimisations as you code. I'm not suggesting you
should always write everything to make it as fast as possible, just take it
into account on the way.
There are *some* things I take account of, but not very many. I use
StringBuilder when I don't know how many things I'll be appending,
because I know the results of not doing so can be catastrophic to
performance, for example. I don't try to put things in big methods to
avoid making method calls though - indeed, I positively split big
methods up into smaller ones, even if those smaller ones won't be
called by anything else. That hurts performance a tiny amount (if the
methods are still too big to be inlined) but improves readability.
A good example would be to put more commonly used
items at the top of a switch statement.
I certainly don't do that. I put them in the most logical way for the
reader to see them. Have you ever come across a situation (in C# - not
a different language which may have different characteristics) where
the order of the cases has made a significant difference? Have you
benchmarked it?
Don't get me wrong, I don't write everything for speed at the expense of
maintainability. In my opinion there is no maintainability lost accessing
the variable directly so I would chose the faster method. In fact I think it
is more maintainable. How many times would you accidentally step into a get
and then a set while debugging just to increment an int?
I wouldn't accidentally do it, because I'd be doing step over instead
of step into. However, I've gone into why you *would* want to use
properties earlier in the thread. If you've provided some validation
etc in the property setter, not performing that validation should be
the exception rather than the rule.
You're using a common newsgroup technique where you take something someone
says to the extreme and then conclude it is a bad idea because it is taken
to the extreme.
Well, I'm going by what you've said. Things like:
<quote>
I find that by writing the faster code to start with I save work
optimising later, which is going to be a significant saving over a few
rare changes
</quote>
That suggests that speed is the major contributor to your design
decisions, rather than maintainability. If that's not your actual
position, then we may well have less to argue about - but I hope you
can see how statements like the one above have led me to my belief.
Perhaps I should have taken more notice of your "two methods which are
practically the same" to start with.
However, do still believe that things like rearranging switch cases for
the sake of performance emphasises the wrong thing, and even if you
don't take it to an extreme, it can lead others into an extreme
position.
I agree, but that is with your over exaggeration of my position. I'm sure
most good programmers take speed into account to some degree when writing
code. Certainly an experienced coder will produce a faster app first time
than a beginner.
Not necessarily. The difference may often be that the beginner's app
will be very quick - but not actually work properly. I'd hope that an
experienced coder would concentrate on getting something working and
maintainable, only worrying about the speed of individual sections when
those sections proved to be significantly.
The areas of speed I take into account are ones of complexity - I will
try to avoid doing something O(n^2) instead of O(n) unless I know that
n will be very small. However, that is usually a case of design and
architecture rather than actual implementation, IME.