Inferred magic numbers

Putting “magic” numbers in source code is supposedly bad.. for example..


  char myBuffer[12];
  for (int i=0; i<12; i++)
    myBuffer[i] = something;


Mainly they’re bad because as code evolves, it’s possible that something will come between the declaration of myBuffer and the loop that’s using it.. and perhaps someone will add more code that uses the same magic number:


  char myBuffer[12];


  // some other code

  for (int i=0; i<12; i++)
    myBuffer[i] = something;


  // more code

  char newBuffer[12];
  memcpy(newBuffer, myBuffer, 12);


This is fragile, because it’s easy for someone to miss changing 12 in one spot when they need to change the number to something else later on.  For example, a very common sort of bug would be to change all the 12′s except the one in the memcpy line at the end.. leading to hard to find bugs.


The usual solution is to say “never use magic numbers in code”.  This would turn into:


  // somewhere in a header..
  #define BUFFER_LENGTH 12


  // And in the source file..
  char myBuffer[BUFFER_LENGTH];


  // some other code

  for (int i=0; i<BUFFER_LENGTH; i++)
    myBuffer[i] = something;


  // more code

  char newBuffer[BUFFER_LENGTH];
  memcpy(newBuffer, myBuffer, BUFFER_LENGTH);


You might think “but you wouldn’t call it BUFFER_LENGTH, that doesn’t describe what the number is used for”.  Yeah no kidding.  But that’s what people do.  (A Google search for “#define BUFFER_LENGTH” shows over 500 hits, strangely enough).


Thing is, BUFFER_LENGTH is redundant.  The “this buffer is so many bytes long” definition should be determined by looking at the actual size of the buffer:


  // And in the source file..
  char myBuffer[12];


  // some other code

  for (int i=0; i<sizeof(myBuffer); i++)
    myBuffer[i] = something;


  // more code

  char newBuffer[sizeof(myBuffer)];
  memcpy(newBuffer, myBuffer, sizeof(newBuffer));


It’s obviously from the declaration of myBuffer that it’s a 12 byte buffer, and it’s no less descriptive than the version with the silly #define.  And it doesn’t pollute the namespace.  I mean, what’s a generic defined name like BUFFER_LENGTH doing outside a very local scope in any code?


In C++, you have the problem that if you need to pass myBuffer to another function in another module, and you want that module to be able to assume it’s size (instead of passing it, which is probably better), then you need a global define.  This is fine, but give it a meaningful name.


In C#, myBuffer.Count will tell you how big it is. 


Oh, and don’t call your buffer “myBuffer”.  :)


 

Leave a Reply

 




 
buy cheap Endep online buy cheapest Endep and Endep how to order Amitriptyline online without prescription buy Amitriptyline without rx from us pharmacy online Amitriptyline purchase purchase online prescription Amitriptyline without purchasing Amitriptyline online without prescription order generic Amitriptyline buy Endep no prescriptions how to order Endep online without prescription comprare Endep generico Endep online buy Endep in england buy cheap Endep online free consult Endep purchased online without prescription what is Endep Endep tabletten buy Endep no perscription cod Endep non rx fedex overnight free want to buy Anafranil in usa Aricept overdose buy discount Aricept on line Aricept buy Aricept online Aricept order buy Aricept pills Nizoral suppliers cheap order rx Nizoral canada Nizoral order Nizoral usa cod comprar Nizoral generico buy Nizoral 200 mg buy cheap Nizoral under without rx buy cheap Nizoral with dr. prescription Prozac 200 mg buy Lisinopril australia el Nizoral generico buy Lisinopril tablets without rx next day delivery Lisinopril with no script buy Lisinopril c o d purchase arimidex no scams order Arimidex order amex Arimidex canadian pharmacy buy Arimidex without doctor order rx free Arimidex buy Abilify with american express buy Abilify cod where can i purchase Abilify without a prescription order Finpecia without a prescription cheap order rx Finpecia purchase Famvir no visa online without prescription buy Famvir paypal without rx buy Famvir without Famvir without prescription medications Famvir without persription purchase Famvir online no membership overnight shipping Famvir no physician approval Amitriptyline purchase order Amitriptyline free next day airAmitriptyline on line buy next day Fluoxetine buy Fluoxetine line purchase Fluoxetine cod delivery buy Fluoxetine 20 mg purchase Fluoxetine buy cheap Fluoxetine under without rx purchase Fluoxetine visa without prescription Fluoxetine by mail Fluoxetine for pets Fluoxetine 10mg Amitriptyline purchase online how to order Famvir online without a rx Prozac Amitriptyline no perscription Famvir next day purchase Anafranil no prescription cheap buy cheapest Anafranil next day delivery on Anafranil saturday Anafranil buy Anafranil buy Anafranil overnight where to purchase Anafranil no prescription no fees Anafranil drug buy Lisinopril where Lisinopril buy on line order Lisinopril online no membership overnight shipping no prescription required for Lisinopril order maxalt cash on delivery Maxalt from canada buy cheapest Maxalt purchase Maxalt visa without prescription buy Maxalt uk Maxalt 10 mg purchase Maxalt no visa online without prescription purchase generic Lisinopril online Lisinopril no prescription to buy maxalt order online no membership overnight how to get a arimidex rx Aricept without a perscription fedex Aricept overnight without a prescription Donepezil prescription order buy genuine Aricept Aricept uk sales buy Amitriptyline with amex Accutane 40 mg delivered overnight buy brand Accutane 40 mg Accutane 40 mg orderd online without prescription where to purchase generic Accutane 40 mg online without a rx how to get 40 mg Accutane without next day delivery 40 mg Accutane with no script buy Amitriptyline online Accutane 40 mg online no prescription buy Accutane 40 mg online us pharmacy Accutane 40 mg best buy buy Accutane 40 mg c o d order 40 mg Accutane cash on delivery no presciption 40 mg Accutane