473,396 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Clean code vs. efficiency

Yesterday I changed some code to use std::vectors and std::strings
instead of character arrays. My boss asked me today why I did it, and
I said that the code looks cleaner this way. He countered by saying
that he was regarded the dyanamic allocation that C++ STL classes
perform as being very inefficient. He also said that he wasn't
particularly interested in clean code (!). My question to the group:
In what situations, if any, would you use fast but hackish C-style
code in favor of the convenient STL classes? Would your answer change
if you were forced to use a questionable implementation (such as,
not-so-hypothetically, Borland 4)?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #1
17 1795
Christopher Benson-Manica wrote:

Yesterday I changed some code to use std::vectors and std::strings
instead of character arrays. My boss asked me today why I did it, and
I said that the code looks cleaner this way. He countered by saying
that he was regarded the dyanamic allocation that C++ STL classes
perform as being very inefficient.
Really?
Has he tried it?
Does he have some performance data?

In most cases there is next to no difference in execution speed.
He also said that he wasn't
particularly interested in clean code (!). My question to the group:
In what situations, if any, would you use fast but hackish C-style
code in favor of the convenient STL classes?
Never
Would your answer change
if you were forced to use a questionable implementation (such as,
not-so-hypothetically, Borland 4)?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
Christopher Benson-Manica wrote:
Yesterday I changed some code to use std::vectors and std::strings
instead of character arrays. My boss asked me today why I did it, and
I said that the code looks cleaner this way. He countered by saying
that he was regarded the dyanamic allocation that C++ STL classes
perform as being very inefficient. He also said that he wasn't
particularly interested in clean code (!). My question to the group:
In what situations, if any, would you use fast but hackish C-style
code in favor of the convenient STL classes?


Exactly in this case: when the boss say that you must do it.

--
Salu2
Jul 22 '05 #3
Christopher Benson-Manica wrote:
Yesterday I changed some code to use std::vectors and std::strings
instead of character arrays. My boss asked me today why I did it, and
I said that the code looks cleaner this way. He countered by saying
that he was regarded the dyanamic allocation that C++ STL classes
perform as being very inefficient. He also said that he wasn't
particularly interested in clean code (!). My question to the group:
In what situations, if any, would you use fast but hackish C-style
code in favor of the convenient STL classes? Would your answer change
if you were forced to use a questionable implementation (such as,
not-so-hypothetically, Borland 4)?


Well, you don't say if you're talking about some type of inner-loop, or
whatever, but still... in general...

Your boss is wrong.

Writing messy code is VERY expensive. You'll spend more time debugging
it. It will be less maintainable. It will lead to hacks upon hacks.
Vicious circle.

Clean code should ALWAYS be the first priority. Performance driven
(less clean, hacky) code should NEVER be implemented first. Only
optimize when you have to, and the minimum you have to. This is what a
profiler is for. Has your boss run a profiler and determined that your
new STL usage is his hot spot? Probably not.

Write clean code. When you're finished, if it's fast enough, then
you're done! [and you have easier-to-debug, more-maintainable,
nice-code to boot]. If it's NOT fast enough, profile it, and optimize
your hot spots.

Meanwhile, I'd try to tactfully explain this to your boss. If he
doesn't understand, then, well, don't say anything to him, but you
should probably start looking for another job because you're working for
a dumb ass.

--Steve

[NOTE: I am a game programmer. Have been (professionally) for 8 years.
We require the utmost of performance. While we don't use STL in our
game code, we use our own somewhat similar stuff. Anyway, it doesn't
matter that he decided to use STL as his example, he is JUST PLAIN WRONG..]
Jul 22 '05 #4
Karl Heinz Buchegger wrote:

Christopher Benson-Manica wrote:

Yesterday I changed some code to use std::vectors and std::strings
instead of character arrays. My boss asked me today why I did it, and
I said that the code looks cleaner this way. He countered by saying
that he was regarded the dyanamic allocation that C++ STL classes
perform as being very inefficient.


Really?
Has he tried it?
Does he have some performance data?

Since when does the boss have to prove things to subordinates?

Christopher is the one who wants to introduce changes to the code base,
it's up to him to program it both ways and perform benchmark testing on
it. A nice boss would let him try that on company time, a hardheaded one
would suggest that weekends are made for such experiments.

Brian Rodenborn
Jul 22 '05 #5
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
Yesterday I changed some code to use std::vectors and std::strings
instead of character arrays. My boss asked me today why I did it, and
I said that the code looks cleaner this way. He countered by saying
that he was regarded the dyanamic allocation that C++ STL classes
perform as being very inefficient. He also said that he wasn't
particularly interested in clean code (!). My question to the group:
In what situations, if any, would you use fast but hackish C-style
code in favor of the convenient STL classes? Would your answer change
if you were forced to use a questionable implementation (such as,
not-so-hypothetically, Borland 4)?


By char arrays, do you mean
char array[200];
array = new char[200]; delete[] array;
array = static_cast<char*>(malloc(200u)); free(array);
array = ::operator new(200u); ::operator delete(array);

As regards memory allocation, the first way may be slightly faster, but not
always. The 2nd, 3rd, and 4th ways are usually the same. The default STL
allocators use the 4th way.

Which way is faster depends very much on the details of your scenario, such
as the size of the array, number of objects created, etc. If you create
lots of small arrays, then the 1st way may be faster.

In general, the STL containers are extremely fast, and might be faster than
your home-grown containers due to specializations, fancy algorithms, etc.

The STL allocators let you use pool allocators, but you can also that by
overloading Class:operator new. In the first case, the pool may be per
container; and in the second, the pool is shared across containers.

The STL containers also destruct their data by calling allocator.destroy on
each element, though in a good compiler this would be optimized away when
the element is a POD, as the statement has no effect.

The STL containers also default initialize their members, if you provide a
size parameter (but don't confuse this with reserve).

But std::string implementations may also provide an optimization for small
strings, namely to contain an element like char[32]. I don't know much
about this topic though, or whether any real implementations actually do
this.

As regards the topic of clean code versus hackish code, my theory is that
hackish code is easier to write and therefore produces immediate business
value (ie. profits). But it is harder to maintain and so over the long term
is more costly. How businesses fare with either method is an interesting
topic, and I have not done any formal research into the subject.

But also be aware that usage of STL does not automatically mean your code is
cleaner.
Jul 22 '05 #6
In article <c7**********@chessie.cirr.com>, Christopher Benson-Manica wrote:
Yesterday I changed some code to use std::vectors and std::strings
instead of character arrays. My boss asked me today why I did it, and
I said that the code looks cleaner this way. He countered by saying
that he was regarded the dyanamic allocation that C++ STL classes
perform as being very inefficient. He also said that he wasn't
particularly interested in clean code (!). My question to the group:
In what situations, if any, would you use fast but hackish C-style
code in favor of the convenient STL classes? Would your answer change
if you were forced to use a questionable implementation (such as,
not-so-hypothetically, Borland 4)?

[-]
IMHO clean code tends to be faster than "hackish" C-style code plus
I'd like to add writing clean C code is as easy as writing unreadable
and arcane crap in C++.

Or to cite myself (since no-one ever else does) -- "If the compiler
loves my code I got it right".

Changes to an existing code base are an tricky issue, though and
just making some changes here and there may just be what you want
to avoid -- a quick and dirty hack.

Not to mention the whole thing needs to be re-tested and your
project manager needs to give his or her thumbs up and it may
result in an unpleasent surprise for others who rely on the original
design (if there's any, that is) and ...

Well, and last but not least although bosses ought to mature enough
to rely on their developers abilities it's never a good idea to
start an argument with your boss for *he* is the one who's in
a position to fire *you*.

Cheers,
Juergen

--
\ Real name : Juergen Heinzl \ no flames /
\ Email private : ju*****@manannan.org \ send money instead /
\ Photo gallery : www.manannan.org \ /
Jul 22 '05 #7
Siemel Naran wrote:
In general, the STL containers are extremely fast, and might be faster than
your home-grown containers due to specializations, fancy algorithms, etc.
There was a mildly interesting talk at the Game Developers' Conference
this year given by a guy from the Xbox ATG.

ATG is a group that Xbox developers (1st & 3rd party) turn to for
optimization. They've seen LOTS of games and TONS of code in the past
few years.

This speaker mentioned that in *every single case*, the STL (shipping
with Xbox DK's, probably Roguewave?) outperformed the home brew lists,
arrays, maps, trees, etc.

He also mentioned lots of other mostly retarded things they'd found.
Most were unbelievable stupid, but that's OT.
But also be aware that usage of STL does not automatically mean your code is
cleaner.


Excellent point.. Though, it probably generally helps. I know most
one-off tools I've written in the past 5 or so years (with STL) look
much cleaner than those I wrote prior to that (without STL).

--Steve
Jul 22 '05 #8
"Stephen Waits" <st***@waits.net> wrote
Siemel Naran wrote:
In general, the STL containers are extremely fast, and might be faster than
your home-grown containers due to specializations, fancy algorithms, etc.
There was a mildly interesting talk at the Game Developers' Conference
this year given by a guy from the Xbox ATG.

ATG is a group that Xbox developers (1st & 3rd party) turn to for
optimization. They've seen LOTS of games and TONS of code in the past
few years.

This speaker mentioned that in *every single case*, the STL (shipping
with Xbox DK's, probably Roguewave?) outperformed the home brew lists,
arrays, maps, trees, etc.


That this happens in some -- maybe even most -- cases, doesn't make it a rule
(I'm not claiming that you're passing it off as one). When I was working with
John Lakos, he had written a family of replacement containers that consistently
outperformed Standard Library containers by a substantial margin, at the expense
of ease of use and, in some specific cases, generality. I'll be the first to
argue that in most cases, the Standard Library is more than adequate and should
be the first choice, but it's by no means the most runtime-efficient way to do
things for all situations.
He also mentioned lots of other mostly retarded things they'd found.
Most were unbelievable stupid, but that's OT.


And specific to their particular code base.
But also be aware that usage of STL does not automatically mean
your code is cleaner.


Definitely. I've seen some mighty ugly code that used Standard containers.

Claudio Puviani
Jul 22 '05 #9
Christopher Benson-Manica wrote:
Yesterday I changed some code to use std::vectors and std::strings
instead of character arrays. My boss asked me today why I did it,
and I said that the code looks cleaner this way.
You broke the zero'th rule of code maintenance:

If it ain't broke, don't fix it.

Your observation that the code "looks cleaner" is far too subjective
to justify recoding working code and risking introduction of new bugs.
He countered by saying that he was regarded the dynamic allocation
that C++ STL classes perform as being very inefficient.
He is almost certainly wrong.
He also said that he wasn't particularly interested in clean code (!).
He probably doesn't know what you mean by "clean code".
I don't either.
You need to show that value was added by "cleaning up this code".
My question to the group: In what situations, if any,
would you use fast but hackish C-style code
in favor of the convenient STL classes?
You haven't shown that "hackish C-style code" is faster than
the STL code that replaces it.
Would your answer change
if you were forced to use a questionable implementation
(such as, not-so-hypothetically, Borland 4)?


No.

Jul 22 '05 #10
Claudio Puviani wrote:

That this happens in some -- maybe even most -- cases, doesn't make it a rule


Agree.

We have our own containers that are as good or better than STL (as it
pertains to us).
He also mentioned lots of other mostly retarded things they'd found.
Most were unbelievable stupid, but that's OT.


And specific to their particular code base.


These weren't specific to one code base. These were trends the Xbox AT
Group had noticed after optimizing several years worth of Xbox titles.

Anyway, most of them were dumb (or ignorant) enough that they had no
bearing on code base - they'd have slowed just about anything to a crawl.

--Steve
Jul 22 '05 #11
"Default User" <fi********@boeing.com.invalid> wrote in message
news:40***************@boeing.com.invalid...
Karl Heinz Buchegger wrote:

Christopher Benson-Manica wrote:

Yesterday I changed some code to use std::vectors and std::strings
instead of character arrays. My boss asked me today why I did it, and
I said that the code looks cleaner this way. He countered by saying
that he was regarded the dyanamic allocation that C++ STL classes
perform as being very inefficient.


Really?
Has he tried it?
Does he have some performance data?

Since when does the boss have to prove things to subordinates?

Christopher is the one who wants to introduce changes to the code base,
it's up to him to program it both ways and perform benchmark testing on
it. A nice boss would let him try that on company time, a hardheaded one
would suggest that weekends are made for such experiments.


I had a Boss once that explained the situation quite succinctly, "Don't do
work that doesn't show."

If you're not adding changes the user demands, then what are they paying you
for? Plus, any change can introduce bugs...

--
Mabden
Jul 22 '05 #12
Default User wrote:
Karl Heinz Buchegger wrote:
Christopher Benson-Manica wrote:
Yesterday I changed some code to use std::vectors and std::strings
instead of character arrays. My boss asked me today why I did it, and
I said that the code looks cleaner this way. He countered by saying
that he was regarded the dyanamic allocation that C++ STL classes
perform as being very inefficient.


Really?
Has he tried it?
Does he have some performance data?


Since when does the boss have to prove things to subordinates?

Christopher is the one who wants to introduce changes to the code base,
it's up to him to program it both ways and perform benchmark testing on
it. A nice boss would let him try that on company time, a hardheaded one
would suggest that weekends are made for such experiments.

Brian Rodenborn


Thanks, Brian.

Unfortunately, I also have Christopher's attitude. I was nearly put on
probation for creating a new design when the team (company) said just
document the old code. Now, I know that I can do new designs as long
as I do the old stuff first then prove that my new stuff will benefit
the company more than the old stuff.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 22 '05 #13
Christopher Benson-Manica wrote:
Yesterday I changed some code to use std::vectors and std::strings
instead of character arrays. My boss asked me today why I did it, and
I said that the code looks cleaner this way. He countered by saying
that he was regarded the dyanamic allocation that C++ STL classes
perform as being very inefficient. He also said that he wasn't
particularly interested in clean code (!). My question to the group:
In what situations, if any, would you use fast but hackish C-style
code in favor of the convenient STL classes? Would your answer change
if you were forced to use a questionable implementation (such as,
not-so-hypothetically, Borland 4)?


In embedded systems, we try not to use the Compiler's libraries unless
we have plenty of code space and variable (RAM) space. Many
applications have very little RAM to play with, so don't have a "heap"
or dynamic memory. Using the STL as it comes (with generic allocators)
would cause problems.

However, perhaps using the STL with custom allocators _may_ be a
safer route. I don't know on this part because the prevalent attitude
is not to use C++ in embedded systems. :-(
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 22 '05 #14
Thomas Matthews wrote:

Default User wrote:
Christopher is the one who wants to introduce changes to the code base,
it's up to him to program it both ways and perform benchmark testing on
it.

Unfortunately, I also have Christopher's attitude. I was nearly put on
probation for creating a new design when the team (company) said just
document the old code. Now, I know that I can do new designs as long
as I do the old stuff first then prove that my new stuff will benefit
the company more than the old stuff.

Right. Do your research and learn to present it in a way that the boss
doesn't become defensive.

Now, if you happen to be in a situation like I am currently, doing
software R&D, then exploring alternatives is generally encouraged. Even
if it turns out that what you tried blows, at least there's a datapoint
for the next time some bright young (ha!) engineer has the same idea.

I also have a boss who's not a software person, so she doesn't have
ideas about how things should be programmed. Of course our tech lead
does.

It all depends on your situation.

Brian Rodenborn
Jul 22 '05 #15

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c7**********@chessie.cirr.com...
Yesterday I changed some code to use std::vectors and std::strings
instead of character arrays. My boss asked me today why I did it, and
I said that the code looks cleaner this way. He countered by saying
that he was regarded the dyanamic allocation that C++ STL classes
perform as being very inefficient. He also said that he wasn't
particularly interested in clean code (!). My question to the group:
In what situations, if any, would you use fast but hackish C-style
code in favor of the convenient STL classes?


Frankly, it sounds like your manager doesn't *really* know what he's talking
about. There is far, far more money wasted on fixing and customizing and
maintaining hard-to-read code than there is lost on inefficient code. Now
it just could be that your compiler does a crappy job with these things and
it is slow. But on the other hand, it still might not matter! (Does it
really matter to the user if something takes .03 seconds or .01 seconds?)
Jul 22 '05 #16

"Default User" <fi********@boeing.com.invalid> wrote in message
news:40***************@boeing.com.invalid...
Karl Heinz Buchegger wrote:

Christopher Benson-Manica wrote:

Yesterday I changed some code to use std::vectors and std::strings
instead of character arrays. My boss asked me today why I did it, and
I said that the code looks cleaner this way. He countered by saying
that he was regarded the dyanamic allocation that C++ STL classes
perform as being very inefficient.


Really?
Has he tried it?
Does he have some performance data?


Since when does the boss have to prove things to subordinates?


Who said his boss had to prove anything?
Jul 22 '05 #17
jeffc wrote:

"Default User" <fi********@boeing.com.invalid> wrote in message
news:40***************@boeing.com.invalid...
Karl Heinz Buchegger wrote:

Christopher Benson-Manica wrote:
>
> Yesterday I changed some code to use std::vectors and std::strings
> instead of character arrays. My boss asked me today why I did it, and
> I said that the code looks cleaner this way. He countered by saying
> that he was regarded the dyanamic allocation that C++ STL classes
> perform as being very inefficient.

Really?
Has he tried it?
Does he have some performance data?


Since when does the boss have to prove things to subordinates?


Who said his boss had to prove anything?


Doesn't that flow from the questions I answered? What are those if not
requests for proof?

Brian Rodenborn
Jul 22 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bruce Duncan | last post by:
I have been starting to use Javascript a lot lately and I wanted to check with the "group" to get your thoughts on code efficiency. First, is there a good site/book that talks about good and bad...
10
by: saraca means ashoka tree | last post by:
The following code is the heart of a program that I wrote to extract html tags from a webpage. How efficient is my code ?. Is there still possible way to optimize the code. Am I using everything as...
15
by: Fady Anwar | last post by:
Hi while browsing the net i noticed that there is sites publishing some software that claim that it can decompile .net applications i didn't bleave it in fact but after trying it i was surprised...
14
by: Jack | last post by:
I like Python but I don't really like Python's installation. With PHP, I only need one file (on Linux) and maybe two files on Windows then I can run my PHP script. This means no installation is...
4
by: arak123 | last post by:
consider the following oversimplified and fictional code public void CreateInvoices(Invoice invoices) { IDbCommand command=Util.CreateDbCommand(); foreach(Invoice invoice in invoices) //lets...
239
by: Eigenvector | last post by:
My question is more generic, but it involves what I consider ANSI standard C and portability. I happen to be a system admin for multiple platforms and as such a lot of the applications that my...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
8
by: Ulysse | last post by:
Hello, I need to clean the string like this : string = """ bonne mentalit&eacute; mec!:) \n <br>bon pour info moi je suis un serial posteur arceleur dictateur ^^* \n ...
5
by: vamsioracle | last post by:
Hi all, I have a problem with the ult_smtp package. Let me explain how the structure of my code is procedure------------ begin declarations of variables and cursors...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.