elegance

Perl Challenge - Directory Iterator

You sometimes hear it said about Perl that there might be 6 different ways to approach the same problem. Good Perl developers usually have well-reasoned insights for making choices between the various possible methods of implementation. So an example Perl problem: A simple script which recursively iterates through a directory structur...

PHP templates - with PHP

Hi, What's the most elegant templating (preferably in pure PHP!) solution you've seen? Specifically i'm interested in handling: Detecting in a repeating block whether it's the first or last element Easy handling of odd/even cases, like a zebra striped table, or similar Other modulos logic, where you'd do something every n'th time. ...

How best to modify my model in Spring MVC if I care about IOC

I am building an application using Spring MVC. I want to make certain changes to my Model for every Controller in the application. In particular, I want to insert certain extra data into the model which will be present for all pages of the application. I could do this several ways: just add the data at the end of every Controller, use a...

What is an elegant way to track the size of a set of objects without a single authoritative collection to reference?

Update: Please read this question in the context of design principles, elegance, expression of intent, and especially the "signals" sent to other programmers by design choices. I have two "views" of a set of objects. One is a dictionary/map indexing the objects by a string value. The other is a dictionary/map indexing the objects by an ...

Generating an XML file from a script

I've recently have a reason to include into our build script the creation of an XML configuration file. The most straightforward way I can think of to do this is to hard-code the XML content as a string into the script and then to simply create a file and write that XML string to the file (named appropriately etc). Is there a more elegan...

Is returning early from a function more elegant than an if statement?

Myself and a colleague have a dispute about which of the following is more elegant. I won't say who's who, so it is impartial. Which is more elegant? public function set hitZone(target:DisplayObject):void { if(_hitZone != target) { _hitZone.removeEventListener(MouseEvent.ROLL_OVER, onBtOve...

What is "elegant" code?

I see a lot of lip service and talk about the most "elegant" way to do this or that. I think if you spend enough time programming you begin to obtain a sort of intuitive feel for what it is we call "elegance". But I'm curious. Even if we can look at a bit of code, and say instinctively "That's elegant", or "That's messy", I wonder if any...

Combining recursive iterator results: children with parents

I'm trying to iterate over a directory which contains loads of PHP files, and detect what classes are defined in each file. Consider the following: $php_files_and_content = new PhpFileAndContentIterator($dir); foreach($php_files_and_content as $filepath => $sourceCode) { // echo $filepath, $sourceCode } The above $php_files_and_c...

What are some elegant features or uses of Perl?

What? Perl Beautiful? Elegant? He must be joking! It's true, there's some ugly Perl out there. And by some, I mean lots. We've all seen it. Well duh, it's symbol soup. Isn't it? Yes there are symbols. Just like 'math' has 'symbols'. It's just that we programmers are more familiar with the standard mathematical symbols....

Passing functions which have multiple return values as arguments in Python

So, Python functions can return multiple values. It struck me that it would be convenient (though a bit less readable) if the following were possible. a = [[1,2],[3,4]] def cord(): return 1, 1 def printa(y,x): print a[y][x] printa(cord()) ...but it's not. I'm aware that you can do the same thing by dumping both return value...

Need an elegant way to invoke arbitrary code on a specified interval

Ok, I have a game server running in Java/Hibernate/Spring/Quartz. The game clock ticks with a Quartz timer, and that works just fine. However, I have many other things that need to happen at specific, tweakable intervals (in game time, not real time). For instance, every 24 hours game time (~ 47 minutes real time, depending on the ser...

How to elegantly ignore some return values of a MATLAB function?

I was wondering if it was possible to get the nth return value from a function without having to create dummy variables for all n-1 return values before it. Let's say I have the following function in MATLAB: function [a,b,c,d] = func() a = 1; b = 2; c = 3; d = 4; Now suppose that I'm only interested in the third return value. This ca...

chop unused decimals with javascript

I've got a currency input and need to return only significant digits. The input always has two decimal places, so: 4.00 -> 4 4.10 -> 4.1 4.01 -> 4.01 Here's how I'm currently doing it: // chop off unnecessary decimals if (val.charAt(val.length-1) == '0') { // xx.00 val = val.substr(0, val.length-1); } if (val.charAt(val.length...

What to do with over-complicated inherited code

Ever since I gained experience at a software house I have found it difficult to tolerate code not neatly structured, without any form of architecture or beauty or clarity whether or not it works i.e. does what its supposed to do. I find I am always caught up in refactoring code like this sometimes "at my own expense" is where my comfor...

Custom sorting problem, always force 0 to back of ascending order?

Premise This problem has a known solution (shown below actually), I'm just wondering if anyone has a more elegant algorithm or any other ideas/suggestions on how to make this more readable, efficient, or robust. Background I have a list of sports competitions that I need to sort in an array. Due to the nature of this array's populati...

How can I use ?? to combine these two lines into one?

I would like to get the two attribute-assignment lines below into one line since I'm going to build them into an application where they will be numerous. Is there a way to express those two lines in one line of elegantly constructed C#, perhaps with a ?? operator like this? string nnn = xml.Element("lastName").Attribute("display").Valu...

Seeking Modularity In SQL Server 2005 - Returning Multiple Recordsets To A Stored Procedure

Hi, I've had this problem a few times and solved it the same way. But it's not pretty and I was wondering if anyone knew of anything a bit more elegant... The Basic Aim: - I have a piece of code that is common through-out many Stored Procedures. - For maintenance purposes I like modularity. - So I want to put that common code in it's o...

Is there an elegant way to bridge two devices/streams in ASIO?

Given two stream-oriented I/O objects in Asio, what is the simplest way to forward data from one device to the other in both directions? Could this be done with boost::iostreams::combination or boost::iostreams:copy perhaps? Or is a manual approach better--waiting for data on each end and then writing it out to the other stream? In other...

If we create code which isn't elegant, have we failed?

The question of 'what is elegance?' has been asked before, and rather than arguing about that, I think we could probably all agree we know it when we see it. But I'd like to know is elegance a goal of programming (an essential aim), or merely a desirable side-effect? (or technique) If we create code which isn't elegant, have we failed...

What's the most elegant method for determining if an array is associative or not?

I have some code that may be passed either an indexed array of values, or an associative array of value pairs. (The parameter is then used to construct radio buttons.) What's the most elegant (least resource hungry?) method for determining which I have? EDIT: One slight problem with this: what if the array to be tested is as follows.......