Subscribe via RSS

The Most Reusable Piece Of Code Ever…Period

Filed Under Code

Even though this example is in C#, whether it is C++, Java, or VB some deviation of this code just has to be the most reusable piece of code ever. Seriously…can anyone else think of a cleaner, more highly used piece of “pocket code” than the good old State enumeration that they have used time and time again in projects? What is your most reusable piece of code?

using System;
using System.ComponentModel;

namespace Codesqueeze.Entities
{
    public enum State
    {
        [Description("Alabama")]
        AL,
        
        [Description("Alaska")]
        AK,
        
        [Description("Arkansas")]
        AR,
        
        [Description("Arizona")]
        AZ,
        
        [Description("California")]
        CA,
        
        [Description("Colorado")]
        CO,
        
        [Description("Connecticut")]
        CT,
        
        [Description("D.C.")]
        DC,
        
        [Description("Delaware")]
        DE,
        
        [Description("Florida")]
        FL,
        
        [Description("Georgia")]
        GA,
        
        [Description("Hawaii")]
        HI,
        
        [Description("Iowa")]
        IA,
        
        [Description("Idaho")]
        ID,
        
        [Description("Illinois")]
        IL,
        
        [Description("Indiana")]
        IN,
        
        [Description("Kansas")]
        KS,
        
        [Description("Kentucky")]
        KY,
        
        [Description("Louisiana")]
        LA,
        
        [Description("Massachusetts")]
        MA,
        
        [Description("Maryland")]
        MD,
        
        [Description("Maine")]
        ME,
        
        [Description("Michigan")]
        MI,
        
        [Description("Minnesota")]
        MN,
        
        [Description("Missouri")]
        MO,
        
        [Description("Mississippi")]
        MS,
        
        [Description("Montana")]
        MT,
        
        [Description("North Carolina")]
        NC,

        [Description("North Dakota")]
        ND,

        [Description("Nebraska")]
        NE,

        [Description("New Hampshire")]
        NH,

        [Description("New Jersey")]
        NJ,

        [Description("New Mexico")]
        NM,

        [Description("Nevada")]
        NV,

        [Description("New York")]
        NY,

        [Description("Oklahoma")]
        OK,

        [Description("Ohio")]
        OH,

        [Description("Oregon")]
        OR,

        [Description("Pennsylvania")]
        PA,

        [Description("Rhode Island")]
        RI,

        [Description("South Carolina")]
        SC,

        [Description("South Dakota")]
        SD,

        [Description("Tennessee")]
        TN,

        [Description("Texas")]
        TX,

        [Description("Utah")]
        UT,

        [Description("Virginia")]
        VA,

        [Description("Vermont")]
        VT,

        [Description("Washington")]
        WA,

        [Description("Wisconsin")]
        WI,

        [Description("West Virginia")]
        WV,

        [Description("Wyoming")]
        WY

    }
}
RSS Icon

  Don't miss a drop! Subscribe now via RSS or email.

Comments

36 Responses to “The Most Reusable Piece Of Code Ever…Period”

  1. Symon Rottem on April 16th, 2008 2:48 am

    Only if you’re in the US… 🙂

  2. Lee Provoost on April 16th, 2008 7:23 am

    yeah not much used here in Europe 🙂

  3. Lee Provoost on April 16th, 2008 7:24 am

    you could help us out with making something like that for all the 20 or 30 something European countries with all their states… now that you’re in your “being reusable” mode anyway 😀

  4. Max Pool on April 16th, 2008 7:43 am

    Alright, alright – so it isn’t very useful in Europe, but I do think my argument still holds based on 1 very weak assumption – outsourcing.

    Because more overseas countries receive outsourcing work from the US (than the US receives from other countries), other countries would benefit more from this code than any other list of states or providences.

    Again though…that is a pretty weak assumption… 😉

  5. Adam Vandenberg on April 16th, 2008 7:52 am

    You’re missing the US overseas territories, possessions and military postal codes.

  6. Adam Vandenberg on April 16th, 2008 7:53 am

    Also, you’ve been smart-quoted.

  7. Dew Drop - April 16, 2008 | Alvin Ashcraft's Morning Dew on April 16th, 2008 7:56 am

    […] The Most Reusable Piece of Code Ever… Period (Max Pool) […]

  8. Max Pool on April 16th, 2008 7:58 am

    @Adam –

    What does smart-quoted mean? Sounds pretty cool, but I hope it isn’t something that gives me a big head as I have to take off my shirt tonight…

  9. notamerican on April 16th, 2008 8:53 am

    Americans *sigh*. You have to laugh.

  10. Max Pool on April 16th, 2008 8:56 am

    Alright…I see a lot of non-US people saying that my opinion is lame…regardless of nationality…

    What is your most reusable piece of code?

  11. ryan on April 16th, 2008 11:38 am

    I just skinned this cat today in Grails with a custom taglib. Grails makes me all giddy. It even includes the ability to pass in a selected state by default. Rought 8 lines of code, (minus the actual key:value pair Map of course, but suppose I could have put that all on one line *wink*).

  12. João Prado on April 16th, 2008 2:45 pm

    Despite the fact of US states the code itself is really useful if applied to local issues.

  13. Bas Geertsema on April 16th, 2008 3:12 pm

    Hmm, as in not really re-using the code, but merely re-using the same concept in different projects and languages I think I must go for a custom tree-iterator. I.e. a iterator that iterates over all nodes in a tree. I often have the need to implement tree structures (no, not compulsive), and most of the times the environment only supports one-dimensional iterators.

  14. w7ngman on April 16th, 2008 5:28 pm

    I like your example… *for a high level language*. For low level languages, the re-usable pieces are often standard high-level things that are “missing” from the language. Quicksort. Implementation of common abstract data types. If you’re into ASM programming contests, it’s probably Floyd-Warshall.

    It could even be a macro, like the iconic:

    #define TRUE 1
    #define FALSE 0
    #define NULL 0

    or for code that gets real down and dirty with integers, maybe something like:

    #define BIT_AT(num, pos) …shifty stuff here…

  15. Max Pool on April 16th, 2008 7:49 pm

    @w7ngman –

    Great point! If we are talking low level I would vote string casting in C/C++. With all those different ways of creating a string pocket code to convert those back and forth is definitely helpful.

  16. Scott on April 16th, 2008 8:25 pm

    Regardless of locality, this code is pointless.

    A data model should exist in a database not hard coded in code.

  17. Scott on April 16th, 2008 8:26 pm

    For the picky, database = any external / modifiable storage mechanism

  18. Stephen Waits on April 16th, 2008 11:20 pm

    I’m American, embarrassing as that is to admit, and I think this is utter crap.

  19. Kris Hofmans on April 17th, 2008 4:50 am

    Yeah pretty useless, when I clicked the link I expected a piece of perl code like (#$%^123-=[;’.? ) that turns dogcrap into gold or something … I would reuse that one over and over.

  20. What About Thad? on April 17th, 2008 9:18 am

    Intelligent people can disagree about the best way to handle a list of geographical areas, but I’m pretty sure Mr. Pool is not an idiot and was aware when he published this that it only contains the 50 states of the U.S.A. Guess what world citizens? Believe it or not, there are thousands of organizations that only deal with the 50 states and, gasp, they use software. I know, how short-sited and primitive, ignoring their potential world market (never mind that some business models are constrained by, oh, um… law, from operating outside of certain jurisdictions). That said, in cases where the problem domain dictates only the choice of one of the 50 states, you’re going to need a way to render only that.

  21. Christopher McCulloh on April 17th, 2008 11:52 am

    “I’m American, embarrassing as that is to admit,”

    I’m an American (as in from the US) and I’m darn thankful for it. I am however embarrassed that there are people who hate America so much they choose to keep living here even though they are embarrassed to admit it. I’m not sure if that says the country is super awesome and they are embarrassed that they live here while others live in abject poverty, or if that says the country is horrible but they are such an idiot they won’t get out and are embarrassed to admit it…

    I thought the post was funny. There is no such thing as the most reusable piece of code ever.

    I take that back, what about the opening html tags? Oh wait, purists would say that’s not “code”…

  22. Per Lundholm on April 18th, 2008 2:22 am

    Does it not need to include behavior to be “code”?

    And I can’t remember writing anything reusable, albeit I belive some of it being usable.

  23. Henry on April 18th, 2008 9:09 am

    hum… i use SQL

  24. Sean on April 18th, 2008 2:28 pm

    How about the most useless string of comments ever. That seems to be fulfilled on this page. Well, here’s another useless comment to add to the pile.

  25. IHateAllRepublicans on April 18th, 2008 2:51 pm

    int x = 0;

  26. Kirill Chilingarashvili on April 19th, 2008 4:48 am

    You are hardcoding the states!
    What if after some time US will have more states? You will have to rewrite all the places where you used this code
    (just joking) 🙂

    Seriously – my most reusable snippet is Logger helper class that I am moving to every new project. I think it’s good point to move it to the class library

  27. Jeremy Wiebe on April 21st, 2008 7:37 am

    I think my most useful piece of code might be this:

    public interface ICommand
    {
    void Execute();
    }

  28. Alex on April 22nd, 2008 6:23 pm

    Keep data in the code?
    Worse practice ever!

    Here is my most usable statement.
    I bet it’s used most of all:

    ;

  29. quangntenemy on April 22nd, 2008 6:52 pm

    You can’t beat System.out.println(“Hello world!”);

  30. BlackAdder on April 23rd, 2008 12:40 am

    Well if it is the most reusable piece of code, it should be part of some library.

  31. links for 2008-04-24 « Tathata - d’ Observer on April 23rd, 2008 8:25 pm

    […] The Most Reusable Piece Of Code Ever…Period (tags: .net code java snippets) […]

  32. hansi on April 24th, 2008 12:49 pm

    sorry, but this post is terrible.

    the weirdest argument is the one about “more people receiving outsourcing work from the us”.

    i mean… yea, whatever…
    that’s all i gotta add to this…

  33. Zkr on April 24th, 2008 7:56 pm

    Wait, wait wait I got it.
    What about thisssss……

    ================================
    using System;
    using System.ComponentModel;

    namespace Codesqueeze.Entities
    {
    public enum Boolean
    {
    [Description(“true”)]
    TRUE,
    [Description(“false”)]
    FALSE
    }
    }

    Ahh??? Ahh?? Ahh??
    That works everywhere!!! America, Europe, Asia, Africa, Oceania.

    he he he.

    Oh… United Steterns… ( I mean Americans )

    Nice try.

  34. asdfadfadfad on April 27th, 2008 6:57 am

    there is life out of the US

  35. social democrat on November 4th, 2008 3:45 pm

    Went googling for exactly this. Data in code, possibly bad, taking it under advisement. Not clear why good design principles require me to go over the wire to query some data store and/or deserialize this info if I have a map application that’s going to work this set over and over and over again and I just want to have the damn thing on hand and someone else shipped it in an assembly already. Yes — *sigh* americans.

    I do like the idea of limiting the number of states. It’s anti-imperialist. But it locks our current set in, and there’s a few I’d rather be rid of.

  36. Janar on October 16th, 2011 11:21 pm

    I would say my most re-usable code is fallowing:

    function pre($data) {
    echo ” . print_r($data, true) . ”;
    }

    Which I use widely for debugging/assuring data where var_dump is not needed.

Max Pool - © 2017 - {codesqueeze}. Sycorr Banking Solutions