language-agnostic

How best to sum up lots of floating point numbers?

Imagine you have a large array of floating point numbers, of all kinds of sizes. What is the most correct way to calculate the sum, with the least error? For example, when the array looks like this: [1.0, 1e-10, 1e-10, ... 1e-10.0] and you add up from left to right with a simple loop, like sum = 0 numbers.each do |val| sum += val...

Determining if a lat-long rect and a circle on a sphere overlap

Suppose I have the following: A region defined by minimum and maximum latitude and longitude (commonly a 'lat-long rect', though it's not actually rectangular except in certain projections). A circle, defined by a center lat/long and a radius How can I determine: Whether the two shapes overlap? Whether the circle is entirely contai...

Code Golf New Year Edition - Integer to Roman Numeral

Write a program that take a single command line argument N and prints out the corresponding Roman Numeral. Eg N = 2009 should print MMIX. Let's say this should work for 0 < N < 3000. (Had fun playing my first ever round of code golf with the Christmas edition, and thought this could fit for New Year. Googled to see if this has come up...

Design By Contract and Test-Driven Development

I'm working on improving our group's development process, and I'm considering how best to implement Design By Contract with Test-Driven Development. It seems the two techniques have a lot of overlap, and I was wondering if anyone had some insight on the following (related) questions: Isn't it against the DRY principle to have TDD and ...

How do you do teamwork with a development team with newbie?

How do you distribute tasks and collaborate with your team, with a few newbie or not experienced people (or not as smart as you)? I hope you can understand my question. Pareto principle said that 20% people may complete 80% tasks. 80% of unexperienced people can only offer 20% in the total productivity. Is it truth in your working env...

How do I display the binary representation of a float or double?

I'd like to display the binary (or hexadecimal) representation of a floating point number. I know how to convert by hand (using the method here), but I'm interested in seeing code samples that do the same. Although I'm particularly interested in the C++ and Java solutions, I wonder if any languages make it particularly easy so I'm maki...

Is hard-coding literals ever acceptable?

The code base I'm currently working on is littered with hard-coded values. I view all hard coded values as a code smell and I try to eliminate them where possible...however there are some cases that I am unsure about. Here are two examples that I can think of that make me wonder what the best practice is: 1. MyTextBox.Text = someCondi...

Main concepts in OOP

I was once asked in an interview 'What are the 3 main concepts of OOP?'. I answered by saying that in my opinion there were 4 which are as follows: Inheritance Encapsulation Abstraction Polymorphism Was I correct? ...

What Exception should be thrown from Propery Set?

In .NET, what type of exception should be thrown if someone passes an illegal value to the set { } part of a property? Example: public string Provider { get { return _provider; } set { if (String.IsNullOrEmpty(value)) throw new Exception("Provider cannot be null or empty."); //what type of exception should be thrown...

Is ORM still the "Vietnam of Computer Science"?

I read this post last night, and I noticed it was from 2006. I could go either way on the ORM, database thing, but I was just wondering if everything bad Jeff said about ORM still applies even now considering the post is from 2006. ...

What have you used Regular Expressions for?

I have heard of regular expressions and only seen use cases for a few things so I don't think of using them very often. In the past I have done a couple of things and it has taken me hours to do. Later I talk to someone and they say "here is how to do it using a regular expression". So what are things for which you have used Regular E...

Relevance of Red Green Light Testing

I am starting, and loving, TDD, however wondering about the red green light concept. I do understand in theory the importance of ensuring you can fail a test before passing it. In practice, however, I am finding it somewhat a futile practice. I feel I can't properly write a failing or passing test without implementing the code I am inte...

SO Community code reviews?

I didn't see this in the official FAQ, and I'm rather new so I'm not sure of the community conventions. I am a IT guy trying to learn programming on my own. I don't have access to someone else to look at code that I've written, so I am curious if it is acceptable to the SO community to post code for review? For example, I'm working thr...

Flow Based Programming

I have been doing a little reading on Flow Based Programming over the last few days. There is a wiki which provides further detail. And wikipedia has a good overview on it too. My first thought was, "Great another proponent of lego-land pretend programming" - a concept harking back to the late 80's. But, as I read more, I must admit ...

Why are compilers so stupid?

I always wonder why compilers can't figure out simple things that are obvious to the human eye. They do lots of simple optimizations, but never something even a little bit complex. For example, this code takes about 6 seconds on my computer to print the value zero (using java 1.6): int x = 0; for (int i = 0; i < 100 * 1000 * 1000 * 1000...

With so much system resources available, how sure are you your code is tuned?

With CPUs being increasingly faster, hard disks spinning, bits flying around so quickly, network speeds increasing as well, it's not that simple to tell bad code from good code like it used to be. I remember a time when you could optimize a piece of code and undeniably perceive an improvement in performance. Those days are almost over....

What's your most controversial programming opinion?

This is definitely subjective, but I'd like to try to avoid it becoming argumentative. I think it could be an interesting question if people treat it appropriately. The idea for this question came from the comment thread from my answer to the "What are five things you hate about your favorite language?" question. I contended that classe...

Contains Test for a Constant Set

The problem statement: Given a set of integers that is known in advance, generate code to test if a single integer is in the set. The domain of the testing function is the integers in some consecutive range. Nothing in particular is known now about the range or the set to be tested. The range could be small or huge (but a solution ca...

Selling "downloadable" content - handling behind the scenes

I was thinking of throwing together a quick PHP or Django site for independently selling some "downloadable" content (music/mp3's) to which I am the artist/rights-holder. I began to wonder if there are already some decent best-practices on how to handle the downloads - like how long a link should last for a user, the process of allowin...

Finding the center of mass on a 2D bitmap

I'm coding a game, and I'd like to be able to find the center of mass of an arbitrary shape on a black and white bitmap such as this: 012345678 0.XX...... 1..XXX.... 2...XXX... 3..XXXXXXX 4...XXX... All "cells" have the same weight. Diagonally adjacent cells are not considered to be connected, and the shape will always be a single o...