Expert Beginner Advice

See Eric Dietrich’s column on the definition of Expert Beginners. His article documents how *badly* advanced beginners can hurt your organization.

Recently an expert beginner argued that it’s better to duplicate strings than to risk changing them to a #define implementation. To 2x-check this, a simple program was created:

#include “stdafx.h”
#include <string>
#define FOO “foo”

int main()
{

std::string a1(“abc”);
std::string a2(“abc”);  //Purportedly the optimum practice
std::string f1(FOO);
std::string f2(FOO);//In-efficient practice according to expert beginner
return 0;

}

Disassembly:

int main()
{
… MS setup trimmed…

std::string a1(“abc”);
00C84C80 68 44 C1 C8 00       push        offset string “abc” (0C8C144h)
00C84C85 8D 4D D0             lea         ecx,[a1]
00C84C88 E8 E4 C5 FF FF       call        std::basic_string<char,std::char_traits<char>, rest of constructor trimmed…
00C84C8D C7 45 FC 00 00 00 00 mov         dword ptr [ebp-4],0

std::string a2(“abc”);
00C84C94 68 44 C1 C8 00       push        offset string “abc” (0C8C144h)
00C84C99 8D 4D AC             lea         ecx,[a2]
00C84C9C E8 D0 C5 FF FF       call        std::basic_string<char,std::char_traits<char>…
00C84CA1 C6 45 FC 01          mov         byte ptr [ebp-4],1

std::string f1(FOO);
00C84CA5 68 48 C1 C8 00       push        offset string “foo” (0C8C148h)
00C84CAA 8D 4D 88             lea         ecx,[f1]
00C84CAD E8 BF C5 FF FF       call        std::basic_string<char,std::char_traits<char>…
00C84CB2 C6 45 FC 02          mov         byte ptr [ebp-4],2

std::string f2(FOO);
00C84CB6 68 48 C1 C8 00       push        offset string “foo” (0C8C148h)
00C84CBB 8D 8D 64 FF FF FF    lea         ecx,[f2]
00C84CC1 E8 AB C5 FF FF       call        std::basic_string<char,std::char_traits<char>…  return 0;

}

So the expert beginner argued that re-defining “abc” as a #define was introducing a global variable in the program space. Therefore, it was more efficient to duplicate the constant.

Obviously, this is a unique perspective on computing and is completely wrong. The definition of #define from the original C days is for the pre-processor to replace the strings prior to compilation. The simple test program here illustrates that modern compilers recognize and can correct much of what may have been the case some decades ago.

What is interesting about this example is how wrong an expert beginner can be. Note how much research and experimentation is necessary to test & correct this error in logic. Experimentation is very helpful!

Advertisements
Expert Beginner Advice

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s