The Most Reusable Piece Of Code Ever…Period
Filed Under CodeEven 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 } }
Comments
36 Responses to “The Most Reusable Piece Of Code Ever…Period”
Only if you’re in the US… 🙂
yeah not much used here in Europe 🙂
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 😀
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… 😉
You’re missing the US overseas territories, possessions and military postal codes.
Also, you’ve been smart-quoted.
[…] The Most Reusable Piece of Code Ever… Period (Max Pool) […]
@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…
Americans *sigh*. You have to laugh.
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?
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*).
Despite the fact of US states the code itself is really useful if applied to local issues.
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.
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…
@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.
Regardless of locality, this code is pointless.
A data model should exist in a database not hard coded in code.
For the picky, database = any external / modifiable storage mechanism
I’m American, embarrassing as that is to admit, and I think this is utter crap.
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.
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.
“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”…
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.
hum… i use SQL
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.
int x = 0;
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
I think my most useful piece of code might be this:
public interface ICommand
{
void Execute();
}
Keep data in the code?
Worse practice ever!
Here is my most usable statement.
I bet it’s used most of all:
;
You can’t beat System.out.println(“Hello world!”);
Well if it is the most reusable piece of code, it should be part of some library.
[…] The Most Reusable Piece Of Code Ever…Period (tags: .net code java snippets) […]
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…
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.
there is life out of the US
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.
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.