Click here to Skip to main content
11,578,524 members (60,434 online)
Click here to Skip to main content

Nine Reasons Not To Use Int

, 9 Feb 2004 151.7K 22
Rate this:
Please Sign up or sign in to vote.
A Parody

Nine Reasons Not To Use "int". A parody on "Nine reasons not to use serialization".
Suggested by Dirk Vandenheuvel.

For my first article, I thought we'd have a little fun. I hope nobody minds. I hope people take it with good humor! And I really hope that it gets listed in the ProductShowcase page!

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

ProductShowcase
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Paul M. Parks2-Feb-12 4:38
memberPaul M. Parks2-Feb-12 4:38 
GeneralOther related documents parts links - where are the links Pin
lucho_198131-Mar-08 16:07
memberlucho_198131-Mar-08 16:07 
GeneralWorthless, waste of time Pin
nlleach18-Sep-06 9:24
membernlleach18-Sep-06 9:24 
GeneralRe: Worthless, waste of time Pin
John Simmons / outlaw programmer29-Jul-07 1:56
mvpJohn Simmons / outlaw programmer29-Jul-07 1:56 
Get help here.[^]





"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

GeneralTo those who dislike this parody: Pin
Fletch F. Fletch8-Oct-05 7:15
sussFletch F. Fletch8-Oct-05 7:15 
GeneralRe: To those who dislike this parody: Pin
dojohansen29-Dec-08 1:50
memberdojohansen29-Dec-08 1:50 
GeneralNOT FUNNY TIME CONSUMING STUPID ARTICLE Pin
tmeg21-Aug-05 11:50
membertmeg21-Aug-05 11:50 
GeneralHeh heh! Funny! Pin
Matthew Hanna4-Jul-05 6:19
memberMatthew Hanna4-Jul-05 6:19 
GeneralI didn't get it at first... Pin
Spunk12-Dec-04 4:08
memberSpunk12-Dec-04 4:08 
General9 ERRONEOUS reasons Pin
LorenzoDV27-Apr-04 13:14
memberLorenzoDV27-Apr-04 13:14 
GeneralRe: 9 ERRONEOUS reasons Pin
Colin Angus Mackay27-Apr-04 13:33
memberColin Angus Mackay27-Apr-04 13:33 
GeneralRe: 9 ERRONEOUS reasons Pin
LorenzoDV28-Apr-04 21:35
memberLorenzoDV28-Apr-04 21:35 
GeneralRe: 9 ERRONEOUS reasons Pin
Colin Angus Mackay29-Apr-04 0:55
memberColin Angus Mackay29-Apr-04 0:55 
GeneralRe: 9 ERRONEOUS reasons Pin
LorenzoDV29-Apr-04 4:21
memberLorenzoDV29-Apr-04 4:21 
GeneralRe: 9 ERRONEOUS reasons Pin
Colin Angus Mackay29-Apr-04 5:36
memberColin Angus Mackay29-Apr-04 5:36 
GeneralRe: 9 ERRONEOUS reasons Pin
LorenzoDV29-Apr-04 12:56
memberLorenzoDV29-Apr-04 12:56 
GeneralRe: 9 ERRONEOUS reasons Pin
You Need A Vacation Lady24-Jun-04 14:25
sussYou Need A Vacation Lady24-Jun-04 14:25 
GeneralRe: 9 ERRONEOUS reasons Pin
frasse531-Dec-04 20:07
memberfrasse531-Dec-04 20:07 
GeneralRe: 9 ERRONEOUS reasons Pin
Anonymous21-Feb-05 16:44
sussAnonymous21-Feb-05 16:44 
GeneralRe: 9 ERRONEOUS reasons Pin
Baris Kurtlutepe1-May-04 3:07
memberBaris Kurtlutepe1-May-04 3:07 
GeneralGET SERIOUS!! Pin
GRENDIZER14-Apr-04 7:39
memberGRENDIZER14-Apr-04 7:39 
GeneralRe: GET SERIOUS!! Pin
asdfasdgasgdasdgasdg24-Jun-04 14:38
sussasdfasdgasgdasdgasdg24-Jun-04 14:38 
GeneralWow Pin
Navin25-Mar-04 5:14
memberNavin25-Mar-04 5:14 
GeneralRe: Wow Pin
darrenk7230-Mar-04 11:41
memberdarrenk7230-Mar-04 11:41 
GeneralRe: Wow Pin
Super Lloyd19-May-06 3:24
memberSuper Lloyd19-May-06 3:24 
GeneralSuggestions? :) Pin
surgeproof12-Mar-04 7:41
membersurgeproof12-Mar-04 7:41 
QuestionWhat is this JokeProject? Pin
John Lyon-Smith5-Mar-04 13:00
memberJohn Lyon-Smith5-Mar-04 13:00 
GeneralHeheh... Pin
Shog919-Feb-04 7:03
memberShog919-Feb-04 7:03 
GeneralRe: Heheh... Pin
Marc Clifton19-Feb-04 12:05
editorMarc Clifton19-Feb-04 12:05 
GeneralRe: Heheh... Pin
Maximilian Hänel24-Mar-04 12:46
memberMaximilian Hänel24-Mar-04 12:46 
GeneralRe: Heheh... Pin
Marc Clifton24-Mar-04 13:28
editorMarc Clifton24-Mar-04 13:28 
GeneralRe: Heheh... Pin
frasse531-Dec-04 20:20
memberfrasse531-Dec-04 20:20 
GeneralFunny Stuff Pin
Tony Gentilcore19-Feb-04 5:37
memberTony Gentilcore19-Feb-04 5:37 
GeneralRe: Funny Stuff Pin
Anonymous22-Feb-04 5:12
sussAnonymous22-Feb-04 5:12 
GeneralRe: Funny Stuff Pin
Anonymous23-Feb-04 13:39
sussAnonymous23-Feb-04 13:39 
GeneralRe: Funny Stuff Pin
Anonymous23-Feb-04 15:21
sussAnonymous23-Feb-04 15:21 
GeneralRe: Funny Stuff Pin
Anonymous23-Feb-04 15:21
sussAnonymous23-Feb-04 15:21 
GeneralWell, that sucked... Pin
Johann de Swardt18-Feb-04 20:19
memberJohann de Swardt18-Feb-04 20:19 
QuestionA joke right? Pin
Signal-918-Feb-04 18:41
memberSignal-918-Feb-04 18:41 
GeneralGenial! Pin
Marc Greiner17-Feb-04 22:07
memberMarc Greiner17-Feb-04 22:07 
GeneralRe: Genial! Pin
Jerod Edward Moemeka5-Mar-04 11:44
memberJerod Edward Moemeka5-Mar-04 11:44 
GeneralRe: Genial! Pin
Jerod Edward Moemeka5-Mar-04 11:45
memberJerod Edward Moemeka5-Mar-04 11:45 
General3 errors Pin
Tweety17-Feb-04 15:18
memberTweety17-Feb-04 15:18 
GeneralRe: 3 errors Pin
rudisaurus18-Feb-04 5:31
memberrudisaurus18-Feb-04 5:31 
GeneralRe: 3 errors Pin
Tweety18-Feb-04 15:09
memberTweety18-Feb-04 15:09 
GeneralRe: 3 errors Pin
Anonymous19-Feb-04 21:13
sussAnonymous19-Feb-04 21:13 
GeneralScary Pin
BadJerry17-Feb-04 5:32
memberBadJerry17-Feb-04 5:32 
Generalwe'll all be coding in real numbers Pin
John M. Drescher17-Feb-04 4:52
memberJohn M. Drescher17-Feb-04 4:52 
GeneralRe: we'll all be coding in real numbers Pin
Keith Nicholas17-Feb-04 16:49
memberKeith Nicholas17-Feb-04 16:49 
GeneralRe: we'll all be coding in real numbers Pin
John M. Drescher17-Feb-04 17:04
memberJohn M. Drescher17-Feb-04 17:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 10 Feb 2004
Article Copyright 2004 by ProductShowcase
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid