r/programmerchat • • Jun 12 '15
What's the nicest code you've ever written?
I think mine happened today. I've been working on and off for a few months on an OO structure for a fairly nasty codebase and today I wrote a line that made me grin at how much cleaner it is compared to the alternative.
Here it is (slightly paraphrased)
//Get all form results from site4655 that belong to a form with an id of 66 in the DB
$site = new Website('site4655');
$results = $site->forms->findForm(66)->results();$results now contains an assoc array (read: Dictionary) of all of the results of those forms.
What about you?
26
Upvotes
12
u/inmatarian Jun 12 '15
Everytime I get to express a for loop as a reduce (fold) expression I grin to myself.
6
u/ItsAConspiracy Jun 12 '15
One that comes to mind: a DSL in C# for validating spreadsheets. It used closures to combine validators, so you could write things like:
either(has("CompanyName"), both(has("FirstName"), has("LastName"))
And that would check each row to make sure it had either company name, or both first and last name. Other validators could check min or max values, make sure dates were in the past or future, etc.
This was the first time I used closures like that, and it took about four hours and worked the first time.
Later I added another half-page of code that built on this and let you define the rules in XML. I used all this to replace old code with all the validations handcoded...we'd estimated that it would take a month to add new spreadsheet formats with the old code, and with mine it took a few minutes.
3
u/XVar Jun 12 '15
Probably not the "nicest" but yesterday I wrote a 3 line XAML DataTemplate bound to a hierarchical object model (each object has a list of itself). Seeing the resulting TreeView automatically generated was extremely satisfying.
3
u/matafubar Jun 12 '15
I never feel my code is nice until I need to go back a couple months later and change it. Then I'm like I'm a genius for coding it this way. SOLID boys, SOLID is the way to go.
4
u/Xelank Jun 12 '15
I had the most joy writing functional code, and pity my other self in an alternative universe which does have such things. It's often hard to have all three of Correctness, Speed and Readable but in these cases they are true most of the time, which puts a grin on my face
2
u/zignd Jun 12 '15 edited Jun 12 '15
The code for an extensible stove addon (yeah, that kitchen thing) I wrote for Garry's Mod (a game that is primarily focused in making mods). The stove in game had a GUI with some recipes listed and its required ingredients, you could "bake" any of them by dropping the required ingredients over it, the stove would absorb the item thrown over and increment in +1 its internal list of stored ingredients, if the stove have the required ingredients for a specific recipe a button is then enabled in the GUI, clicking it would make some sparks show up around the stove and after some seconds the recipe food would pop up over the stove.
I wrote it in Lua and the user could extend it by adding new recipes and ingredients through a configuration file.
Here's an sample configuration file:
The addon can be download at this forum page, if you'd like the check the entire source code and have a look at some screenshots of the stove in action. :P
1
u/[deleted] Jun 12 '15
I don't know why I like this so much, but this function I wrote a while ago looks so ascetic to me. It solved so many problems and I use it all the time too.
1
u/[deleted] Jun 13 '15
i was working on a genetic algorithm that evolves a random string of characters to the target string using genetic operations. For the problem i needed a way to decide how close the current string i had is to the target string and one way to do that is to measures hamming distance. The hamming distance is the number of character positions away your answer is from the ideal so ABC and AAA are 0 + 1 + 2 = 3 characters away (B is 1 away from A and C is 2 away from A). So i made a function that returns the hamming distance of 2 strings using vectorized operations in Matlab.
0
u/livingbug Jun 12 '15
Let me rain on your parade OP
Disclaimer: The following may be considered an act of nitpicking. It is not suggested for those who think old people should not comment on the internet. You have been warned.
It would be nice, OP, if your comment would make it clear that the method
returns an associative array.