Topic: Nine Reasons Not To Use INT  (Read 3915 times)

0 Members and 2 Guests are viewing this topic.

Offline toasty0

  • Application.Quit();
  • Captain
  • *
  • Posts: 8045
  • Gender: Male
Nine Reasons Not To Use INT
« on: July 20, 2008, 11:01:53 am »
Introduction

If you want to know to get your application to save information in the form of numbers, then a quick skim through MSDN magazine or a quick search on newsgroups will give you the answer: int.

Just declare your variables as "int" and there you go. It's a simple matter of typing in three little letters: i-n-t, and couple seconds later it's done. Alternatively, you could use float, double, real, byte, char, short.

All very simple, but unfortunately all very wrong. There are a number of reasons why you not not opt for the simple approach. Here are nine important ones.

1. It forces you to design your classes a certain way
"int" only works with integers. This means that your class needs to manage only integers, not real things like "float" or "imaginary" or polar coordinates. You can not have numbers that have digits past the decimal point. And it forces restrictions on how you perform math--dividing one "int" by another "int" may not result in the correct value!

2. It is not future-proof for small changes
If you use "int", then all the stuff after the decimal point will get dumped. You have no control over this. If you change the name of the variable to something other than "int", then your code will break. You can get around this by implement the IInt interface. This gives you much better control of how data is stored and retrieved to and from an "int". Unfortunately...

3. It is not future-proof for large changes
An "int" is a type. If you change your "int" variable names or strong-name your assemblies, you're going to hit all sorts of problems. Even if you manage to code the necessary contortions to get round this, you're going to find that ...

4. It is not future-proof for massive changes
"int" isn't going to be around in five years or so. By then, we'll all be coding in real numbers (puns intended). If you start implementing the IInt interface in your code now, then its tendrils are going to be everywhere in five years' time. Your code is going to be full of little hacks to cope with version changes, class re-naming, refactoring, etc. Some time in the future, .NET will be superseded by something even more wonderful. Nobody knows what this something wonderful will be, but you can bet that writing code-read data serialized by version 1.1 of the .NET's "int" type is going to be a pig. I wrote some VB6 code 5 years ago and used "int" on a 16 bit processor, when "int" meant 16 bits. A neat, easy way of storing information to disk, I thought. And it was, until .NET came along and then I was stuck, because you see, on my new 32 bit processor, "int" now means 32 bits!

5. It is not secure
Using "int" is inherently insecure. It's bit format is widely known. In addition, "int" works by creating "bits", either a 1 or a 0. Disk files on disk containing 1's and 0's will pose a potential security risk. If, instead, you implement the IInt interface, then, even if you're not exposing bits through your classes, anyone can see your bits anyway, since "int" is a public type.

6. It is inefficient
"int" is verbose. It often has many more bits than you actually need. And, if you are using the IInt interface, more bits gets stored along with data. This makes "int" very expensive in terms of disk space.

7. It is a black box
The odds are you don't really know how "int" works. I certainly don't. Is it big-endian or little-endian? Is the MSB the first bit or the last bit? What does MSB mean anyways? All this means that there are going to have all sorts of quirks and gotchas that you can't even conceive of when you start using "int". Did you know that "int" actually uses the 32 bits? When you think you're creating a bunch of "int's", actually hardware is doing something. What are the implications of that? The only thing I know is that I will not know about them until it's too late.

8. It is slow
When I did some research for a previous article (http://www.programmersAgainstInt.com), I noticed a few interesting things. I wrote a class that contained two "int" values. I created 100,000 instances of this class, stored them to disk, and then read them back again. I did this two ways. First of all, I did it the "proper" way, by implementing two variables of type "int". Secondly, I did it the "dirty" way, by streaming out and back in 100,000 pairs of "int". Which way was faster? Perhaps not surprisingly, the dirty way. Lots faster. Surprised? I wasn't.

9. It is weird
"int" does a lot of cunning work. This means that it doesn't necessarily behave the way you might expect. When you divide by 0, for example, exceptions get thrown.

Have no regrets
Although .NET provides a number of quick and easy ways to use "int", do not use them. A week, a month, a year, or five years down the line you will regret it.
MCTS: SQL Server 2005 | MCP: Windows Server 2003 | MCTS: Microsoft Certified Technology Specialist | MCT: Microsoft Certified Trainer | MOS: Microsoft Office Specialist 2003 | VSP: VMware Sales Professional | MCTS: Vista

Offline marstone

  • Because I can
  • Commander
  • *
  • Posts: 3014
  • Gender: Male
  • G.E.C.K. - The best kit to have
    • Ramblings on the Q3, blog
Re: Nine Reasons Not To Use INT
« Reply #1 on: July 20, 2008, 11:49:25 am »
Introduction

If you want to know to get your application to save information in the form of numbers, then a quick skim through MSDN magazine or a quick search on newsgroups will give you the answer: int.

Just declare your variables as "int" and there you go. It's a simple matter of typing in three little letters: i-n-t, and couple seconds later it's done. Alternatively, you could use float, double, real, byte, char, short.

All very simple, but unfortunately all very wrong. There are a number of reasons why you not not opt for the simple approach. Here are nine important ones.

1. It forces you to design your classes a certain way
"int" only works with integers. This means that your class needs to manage only integers, not real things like "float" or "imaginary" or polar coordinates. You can not have numbers that have digits past the decimal point. And it forces restrictions on how you perform math--dividing one "int" by another "int" may not result in the correct value!

2. It is not future-proof for small changes
If you use "int", then all the stuff after the decimal point will get dumped. You have no control over this. If you change the name of the variable to something other than "int", then your code will break. You can get around this by implement the IInt interface. This gives you much better control of how data is stored and retrieved to and from an "int". Unfortunately...

3. It is not future-proof for large changes
An "int" is a type. If you change your "int" variable names or strong-name your assemblies, you're going to hit all sorts of problems. Even if you manage to code the necessary contortions to get round this, you're going to find that ...

4. It is not future-proof for massive changes
"int" isn't going to be around in five years or so. By then, we'll all be coding in real numbers (puns intended). If you start implementing the IInt interface in your code now, then its tendrils are going to be everywhere in five years' time. Your code is going to be full of little hacks to cope with version changes, class re-naming, refactoring, etc. Some time in the future, .NET will be superseded by something even more wonderful. Nobody knows what this something wonderful will be, but you can bet that writing code-read data serialized by version 1.1 of the .NET's "int" type is going to be a pig. I wrote some VB6 code 5 years ago and used "int" on a 16 bit processor, when "int" meant 16 bits. A neat, easy way of storing information to disk, I thought. And it was, until .NET came along and then I was stuck, because you see, on my new 32 bit processor, "int" now means 32 bits!

5. It is not secure
Using "int" is inherently insecure. It's bit format is widely known. In addition, "int" works by creating "bits", either a 1 or a 0. Disk files on disk containing 1's and 0's will pose a potential security risk. If, instead, you implement the IInt interface, then, even if you're not exposing bits through your classes, anyone can see your bits anyway, since "int" is a public type.

6. It is inefficient
"int" is verbose. It often has many more bits than you actually need. And, if you are using the IInt interface, more bits gets stored along with data. This makes "int" very expensive in terms of disk space.

7. It is a black box
The odds are you don't really know how "int" works. I certainly don't. Is it big-endian or little-endian? Is the MSB the first bit or the last bit? What does MSB mean anyways? All this means that there are going to have all sorts of quirks and gotchas that you can't even conceive of when you start using "int". Did you know that "int" actually uses the 32 bits? When you think you're creating a bunch of "int's", actually hardware is doing something. What are the implications of that? The only thing I know is that I will not know about them until it's too late.

8. It is slow
When I did some research for a previous article (http://www.programmersAgainstInt.com), I noticed a few interesting things. I wrote a class that contained two "int" values. I created 100,000 instances of this class, stored them to disk, and then read them back again. I did this two ways. First of all, I did it the "proper" way, by implementing two variables of type "int". Secondly, I did it the "dirty" way, by streaming out and back in 100,000 pairs of "int". Which way was faster? Perhaps not surprisingly, the dirty way. Lots faster. Surprised? I wasn't.

9. It is weird
"int" does a lot of cunning work. This means that it doesn't necessarily behave the way you might expect. When you divide by 0, for example, exceptions get thrown.

Have no regrets
Although .NET provides a number of quick and easy ways to use "int", do not use them. A week, a month, a year, or five years down the line you will regret it.



I disagree with most of the points here as they can be used against any of the formats.  Lets just love our little INT for he is old and familiar.
The smell of printer ink in the morning,
Tis the smell of programming.

Offline toasty0

  • Application.Quit();
  • Captain
  • *
  • Posts: 8045
  • Gender: Male
Re: Nine Reasons Not To Use INT
« Reply #2 on: July 20, 2008, 11:55:18 am »
Fwiw, it's a good thing you disagree...this article is satire.
MCTS: SQL Server 2005 | MCP: Windows Server 2003 | MCTS: Microsoft Certified Technology Specialist | MCT: Microsoft Certified Trainer | MOS: Microsoft Office Specialist 2003 | VSP: VMware Sales Professional | MCTS: Vista

Offline marstone

  • Because I can
  • Commander
  • *
  • Posts: 3014
  • Gender: Male
  • G.E.C.K. - The best kit to have
    • Ramblings on the Q3, blog
Re: Nine Reasons Not To Use INT
« Reply #3 on: July 20, 2008, 11:58:11 am »
Fwiw, it's a good thing you disagree...this article is satire.

I had figured so, was going to say that, but on this board.  You just never know.
The smell of printer ink in the morning,
Tis the smell of programming.

Offline toasty0

  • Application.Quit();
  • Captain
  • *
  • Posts: 8045
  • Gender: Male
Re: Nine Reasons Not To Use INT
« Reply #4 on: July 20, 2008, 11:59:46 am »
Fwiw, it's a good thing you disagree...this article is satire.

I had figured so, was going to say that, but on this board.  You just never know.

Too true. :D
MCTS: SQL Server 2005 | MCP: Windows Server 2003 | MCTS: Microsoft Certified Technology Specialist | MCT: Microsoft Certified Trainer | MOS: Microsoft Office Specialist 2003 | VSP: VMware Sales Professional | MCTS: Vista

Offline Centurus

  • Old Mad Man Making Ship Again....Kinda?
  • Captain
  • *
  • Posts: 8505
  • Gender: Male
Re: Nine Reasons Not To Use INT
« Reply #5 on: July 20, 2008, 04:31:06 pm »
*walks out of his fruit punch shower with a loin cloth on, a shower cap, and fuzzy blue bunny slippers, holding a toilet brush*

Don't you two start meddling in on my insanity territory!!!!

*grabs some liquid tide, puts it on the toilet brush, and scrubs his back as he walks back to the shower after throwing the loin cloth in the corner*
The pen is truly mightier than the sword.  And considerably easier to write with.

Online Nemesis

  • Captain Kayn
  • Global Moderator
  • Commodore
  • *
  • Posts: 13067
Re: Nine Reasons Not To Use INT
« Reply #6 on: July 20, 2008, 05:01:03 pm »
Fwiw, it's a good thing you disagree...this article is satire.


I had figured so, was going to say that, but on this board.  You just never know.


!!!Hey !!!

I don't pull pranks that often.  ;(
Do unto others as Frey has done unto you.
Seti Team    Free Software
I believe truth and principle do matter. If you have to sacrifice them to get the results you want, then the results aren't worth it.
 FoaS_XC : "Take great pains to distinguish a criticism vs. an attack. A person reading a post should never be able to confuse the two."

Offline Centurus

  • Old Mad Man Making Ship Again....Kinda?
  • Captain
  • *
  • Posts: 8505
  • Gender: Male
Re: Nine Reasons Not To Use INT
« Reply #7 on: July 20, 2008, 07:01:31 pm »
Fwiw, it's a good thing you disagree...this article is satire.


I had figured so, was going to say that, but on this board.  You just never know.


!!!Hey !!!

I don't pull pranks that often.  ;(


*rummages through his chest and pulls out a photograph of Nemesis dressed up as Sandra Bernhardt at D.net's Halloween Party 2006*

The pen is truly mightier than the sword.  And considerably easier to write with.

Online Nemesis

  • Captain Kayn
  • Global Moderator
  • Commodore
  • *
  • Posts: 13067
Re: Nine Reasons Not To Use INT
« Reply #8 on: July 20, 2008, 08:05:29 pm »
*rummages through his chest and pulls out a photograph of Nemesis dressed up as Sandra Bernhardt at D.net's Halloween Party 2006*

Do unto others as Frey has done unto you.
Seti Team    Free Software
I believe truth and principle do matter. If you have to sacrifice them to get the results you want, then the results aren't worth it.
 FoaS_XC : "Take great pains to distinguish a criticism vs. an attack. A person reading a post should never be able to confuse the two."

Offline Centurus

  • Old Mad Man Making Ship Again....Kinda?
  • Captain
  • *
  • Posts: 8505
  • Gender: Male
Re: Nine Reasons Not To Use INT
« Reply #9 on: July 20, 2008, 08:13:29 pm »
*rummages through his chest and pulls out a photograph of Nemesis dressed up as Sandra Bernhardt at D.net's Halloween Party 2006*



*takes a bow and struts around the forums while having Pink Floyd's Young Lust playing on the overhead speakers*
The pen is truly mightier than the sword.  And considerably easier to write with.

Offline Bonk

  • Commodore
  • *
  • Posts: 13298
  • You don't have to live like a refugee.
Re: Nine Reasons Not To Use INT
« Reply #10 on: July 21, 2008, 05:37:22 am »
I thought something was fishy as it contradicts itself, saying don't use int, but don't use any of the other variable types either. So what, no number variables at all? Do all math as strings? (The thought of that makes my brain hurt...)

Offline Javora

  • America for Americans first.
  • Commander
  • *
  • Posts: 3002
  • Gender: Male
Re: Nine Reasons Not To Use INT
« Reply #11 on: July 21, 2008, 07:12:09 am »
I thought something was fishy as it contradicts itself, saying don't use int, but don't use any of the other variable types either. So what, no number variables at all? Do all math as strings? (The thought of that makes my brain hurt...)

Oh damn, I think you just described Cobol...   :laugh:

Offline FPF-Paladin

  • 'Thou shalt not CAD.' - DH
  • Lt.
  • *
  • Posts: 588
  • Gender: Male
Re: Nine Reasons Not To Use INT
« Reply #12 on: July 21, 2008, 08:14:19 am »
Oh no... COBOL...

It's like someone sat down and intentionally worked out a way to have to type out 100 chars to do 3 chars of code work in the most inefficient way possible...

Mind you last time I used COBOL was COBOL/400 for an old IBM AS/400 system 38, if that tells you anything.

My hands (and my brain really) hurt just thinking about it.
~Life cannot find reasons to sustain it, cannot be a source of decent mutual regard, unless each of us resolves to breathe such qualities into it. ~

Offline Javora

  • America for Americans first.
  • Commander
  • *
  • Posts: 3002
  • Gender: Male
Re: Nine Reasons Not To Use INT
« Reply #13 on: July 21, 2008, 09:36:48 am »
You can blame Grace Hopper for the he|| that is Cobol.   ;)

http://en.wikipedia.org/wiki/Grace_Hopper