问:
How can I find the shortest path between two subgraphs of a larger graph?
I'm working with a weighted, undirected multigraph (loops not permitted; most node connections have multiplicity 1; a few node connections have multiplicity 2). I need to find the shortest path between two subgraphs of this graph that do not overlap with each other. There are no other restrictions on which nodes should be used as start/end points.

Edges can be selectively removed from the graph at certain times (as explained in my previous question) so it's possible that for two given subgraphs, there might not be any way to connect them.

I'm pretty sure I've heard of an algorithm for this before, but I can't remember what it's called, and my Google searches for strings like "shortest path between subgraphs" haven't helped. Can someone suggest a more efficient way to do this than comparing shortest paths between all nodes in one subgraph with all nodes in the other subgraph? Or at least tell me the name of the algorithm so I can look it up myself?

For example, if I have the graph below, the nodes circled in red might be one subgraph and the nodes circled in blue might be another. The edges would all have positive integer weights, although they're not shown in the image. I'd want to find whatever path has the shortest total cost as long as it starts at a red node and ends at a blue node. I believe this means the specific node positions and edge weights cannot be ignored.

(This is just an example graph I grabbed off Wikimedia and drew on, not my actual problem.)

答:
After struggling for a day trying to shoehorn fake nodes and edges into my poorly designed code, I'm beginning to think that SJuan76's suggestion is really quite elegant. I can't prove that it works in all cases, but I've run through a few examples by hand and it seems to do okay.

Start with plain old Dijkstra's Algorithm. Make the following minor changes:

The regular algorithm sets all initial distances to infinity other than "the source node." Instead, set all initial distances to infinity. Then, change the distance to zero for each node in the "start" subgraph. (For an undirected graph like mine, you can just pick either subgraph.)

Keep running the algorithm normally until the only nodes that remain in the unvisited set are nodes in the "end" subgraph. It's safe to stop at this point because they can only possibly make distances larger, not smaller. (If you have negative edge weights, this doesn't apply.)

Look through all of the nodes in the "end" subgraph, and pick the one that has the lowest total score. That's the end node.

Follow the path of saved "previous node" info until a node in the "start" subgraph is reached.


问:
Algorithm to generate N random numbers between A and B which sum up to X
This problem seemed like something which should be solvable with but a few lines of code.

Unfortunately, once I actually started to write the thing, I've realized it's not as simple as it sounds.

What I need is a set of X random numbers, each of which is between A and B and they all add up to X. The exact variables for the problem I'm facing seem to be even simpler: I need 5 numbers, between -1 and 1 (note: these are rational (floating point) numbers), which add up to 1.

My initial "few lines of code, should be easy" approach was to randomize 4 numbers between -1 and 1 (which is simple enough), and then make the last one 1-(sum of previous numbers). This quickly proved wrong, as the last number could just as well be larger than 1 or smaller than -1.

What would be the best way to approach this problem?

PS. Just for reference: I'm using C#, but I don't think it matters. I'm actually having trouble creating a good enough solution for the problem in my head.

I also wanted to provide my current solution to the problem, but do remember that it's quite imperfect and it was created as a quick fix to the initial problem!

Generate 4 random numbers between -1 and 1

Create a "final" number X=SUM(previousNumbers)

If the final number is >1 or <-1, then:

Get the "amount" over 1 / under -1 and make the last number a 1 / -1

Find another number which can accept this amount and still inside the brackets

If no number can take the amount (it's too large / too small) then divide the amount in half and try again for each half

Randomize the order of the generated numbers and return them

This works in the sense that this algorithm generates 5 numbers which are between -1 and 1 and their sum is 1. However the downside is that quite often one of the generated numbers is a 1 (or -1), which doesn't feel very random.

答:
Simple, as long as you know how many.

You need N numbers called V1 to Vn. The required sum is S.

Generate N random numbers (in any convenient range). They are R1 to Rn.

Calculate their sum as SR.

Scale each number so Vn = Rn * S / SR.

You may produce a tiny rounding error, but I doubt this will be a problem.

If the number N is supposed to be random, then choose that first.

My apologies, I missed the requirement for the numbers to be between A and B. The algorithm is exactly the same. Pick N random numbers, then scale them based on A, B, actual sum and required sum. I leave the rest as an implementation detail.

ContentLicense: CC BY-SA 3.0


问:
Ranking players depending on decision making during a game
How would I go about a ranking system for players that play a game? Basically, looking at video games, players throughout the game make critical decisions that ultimately impact the end game result.

Is there a way or how would I go about a way to translate some of those factors (leveling up certain skills, purchasing certain items, etc.) into something like a curve that can be plotted on a graph?

This game that I would like to implement this is League of Legends.

Example: Player is Level 1 in the beginning. Gets a kill very early in the game (he gets gold because of the kill and it increases his "power curve"), and purchases attack damage (gives him more damage which also increases his "power curve". However, the player that he killed (Player 2), buys armor (counters attack damage). This slightly increases Player 2's own power curve, and reduces Player 1's power curve.

There's many factors I would like to take into account. These relative factors (example: BECAUSE Player 2 built armor, and I am mainly attack damage, it lowers my OWN power curve) seem the hardest to implement.

My question is this: Is there a certain way to approach this task? Are there similar theoretical concepts behind ranking systems that I should read up on (Maybe in game theory or data mining)? I've seen the ELO system, but it doesn't seem what I want since it simply takes into account wins and losses.

答:
The basic approach is:

Identify all the factors.

Assign a weight to each factor.

If the factor itself has a scale, assign a scaling formula (eg linear, log, capped, whatever) based on additional weights.

If there are dependencies, express those as a formula based on additional weights.

Add up all the resulting values.

The problem now is that you have a complicated formula with a lot of weights and you just need to find the correct values for all those weights. Simply to say, very hard to do.

There are many approaches to optimising weights: linear programming, genetic algorithms, neural networks, Bayesian analysis, gut feel, etc.They all depend on being able to measure the goodness of the final result, and that was one thing that was missing from your question.

Do you actually know what end result you expect to get? Do you know what final scores or rankings your players should achieve? Do you have training data and comparative metrics to drive your algorithms? When you know how you can measure success, you will know how to get there.


答:
You can start with any 5 random numbers in the range, but then you just need to pay attention to which are negative and which are positive.

Case (A) : All 5 numbers are positive.

Here you can just divide by their sum. The sum is guaranteed to be greater than each individual number, since each number N is positive.  Then you have

```<code>0 < N < 1
</code>```

for each individual number N, and you have the normalized sum equal to 1.

Case (B) : Positives and negatives.

First normalize the negatives so that their sum is

```<code>-1 < (Sum of normalized negatives) < 0
</code>```

At this point we are sure that each negative number is still within the range

```<code>-1 < N_negative < 0.
</code>```

for each individual negative number N_negative.

Next normalize the positives so that

```<code>1 - (Sum of normalized positives) = (Sum of normalized negatives)
</code>```

Now we might have got into trouble taking one or more of the positive numbers outside of the range.  Check if that happens.  If not, then we are done.  If so, then renormalize the positive numbers so that the maximum individual value is 1.  We know that the sum of renormalized positives will be less than the sum of the first positive normalization, but also the renormalized sum will be greater than 1.  Therefore we can renormalize the negatives so that

```<code>1 - (Sum of *re*normalized positives) = (Sum of *re*normalized negatives)
</code>```

And now we are definitely done with case B.

Case (C) : All 5 numbers are negative.

This has no solution.

Title: Algorithm to generate N random numbers between A and B which sum up to X

Body:
This problem seemed like something which should be solvable with but a few lines of code.

Unfortunately, once I actually started to write the thing, I've realized it's not as simple as it sounds.

What I need is a set of X random numbers, each of which is between A and B and they all add up to X. The exact variables for the problem I'm facing seem to be even simpler: I need 5 numbers, between -1 and 1 (note: these are rational (floating point) numbers), which add up to 1.

My initial "few lines of code, should be easy" approach was to randomize 4 numbers between -1 and 1 (which is simple enough), and then make the last one 1-(sum of previous numbers). This quickly proved wrong, as the last number could just as well be larger than 1 or smaller than -1.

What would be the best way to approach this problem?

PS. Just for reference: I'm using C#, but I don't think it matters. I'm actually having trouble creating a good enough solution for the problem in my head.

I also wanted to provide my current solution to the problem, but do remember that it's quite imperfect and it was created as a quick fix to the initial problem!

Generate 4 random numbers between -1 and 1

Create a "final" number X=SUM(previousNumbers)

If the final number is >1 or <-1, then:

Get the "amount" over 1 / under -1 and make the last number a 1 / -1

Find another number which can accept this amount and still inside the brackets

If no number can take the amount (it's too large / too small) then divide the amount in half and try again for each half

Randomize the order of the generated numbers and return them

This works in the sense that this algorithm generates 5 numbers which are between -1 and 1 and their sum is 1. However the downside is that quite often one of the generated numbers is a 1 (or -1), which doesn't feel very random.

Body:
You can start with any 5 random numbers in the range, but then you just need to pay attention to which are negative and which are positive.

Case (A) : All 5 numbers are positive.

Here you can just divide by their sum. The sum is guaranteed to be greater than each individual number, since each number N is positive.  Then you have

0 < N < 1

for each individual number N, and you have the normalized sum equal to 1.

Case (B) : Positives and negatives.

First normalize the negatives so that their sum is

-1 < (Sum of normalized negatives) < 0

At this point we are sure that each negative number is still within the range

-1 < N_negative < 0.

for each individual negative number N_negative.

Next normalize the positives so that

1 - (Sum of normalized positives) = (Sum of normalized negatives)

Now we might have got into trouble taking one or more of the positive numbers outside of the range.  Check if that happens.  If not, then we are done.  If so, then renormalize the positive numbers so that the maximum individual value is 1.  We know that the sum of renormalized positives will be less than the sum of the first positive normalization, but also the renormalized sum will be greater than 1.  Therefore we can renormalize the negatives so that

1 - (Sum of *re*normalized positives) = (Sum of *re*normalized negatives)

And now we are definitely done with case B.

Case (C) : All 5 numbers are negative.

This has no solution.


答:
One option to solve this problem is to build a search tree.  The goal state will be the sum of all nodes along your current path equal X.  Each node is a different value between the range, so the number of nodes at each level will be abs(A) + abs (B).  Explore all the paths and get all the possible solutions.  Then choose one at random from the list of solutions.

The search space will become too large to calculate when N and abs(A) + abs (B) are large.  You can limit the search space by creating rules.  A simple rule here would be that if the path contains the same values, but in a different order, then they are considered the same.  That would eliminate a lot of paths on the search tree.  You can think up a lot more to limit the search space, but I will leave that exercise to you.

Note: This solution is overkill, but it's a fun exercise.


问:
How can a new programmer impress the software engineer (boss)?
I'm working at my first programming job. My boss is a very smart software engineer, and I feel
like I have very little to offer compared to him. Problem is, he is always busy, and needs someone to help him out. I feel like I'm not good enough, but I still want to succeed. I want to be a great programmer.

What can I do to impress him?

Thank you.

答: Write clear, solid code.</p>

ContentLicense: CC BY-SA 2.5

Body: Write clear, solid code.


问:
Finding "spare time" in a day from within a list of events
I have a list of events which is always sorted chronologically.  The start time is always followed by the end time.  Times are strings formatted as 'HHmmss'.

```<code>// list of events

var events = [
    '010000',  // start
    '013000',  // end...
    '053000',
    '060000',
    '161500',
    '184500']

// desired output

var spares = [
    '000000',  // start
    '010000',  // end...
    '013000',
    '053000',
    '060000',
    '161500',
    '184500',
    '235959']
</code>```

How can I programmatically create a new list of "spare time" from 000000 to 235959?

PS  I'm trying to do this in Javascript, but any conceptual answer or pseudo code would be helpful too.

答:
If "spare time" is defined as the time in between the events, then your list format makes it very easy to convert you list of events to a list of spare times:

Add midnight to both the start and end of your list

Go over the new list and remove any interval of less than 0 seconds (or whatever threshold you want).

With your example events, this would give you after step 1:

```<code>000000
010000
013000
053000
060000
161500
184500
235959
</code>```

and after step 2:

```<code>000000
010000
013000
053000
060000
161500
184500
235959
</code>```

Body:
If "spare time" is defined as the time in between the events, then your list format makes it very easy to convert you list of events to a list of spare times:

Add midnight to both the start and end of your list

Go over the new list and remove any interval of less than 0 seconds (or whatever threshold you want).

With your example events, this would give you after step 1:

000000
010000
013000
053000
060000
161500
184500
235959

and after step 2:

000000
010000
013000
053000
060000
161500
184500
235959


答:
As said before, this question actually doesn't have an answer: The restrictions imposed on the numbers make the randomness questionable at best.

However, you could come up with a procedure that returns a list of numbers like that:

Let's say we have picked the first two numbers randomly as -0.8 and -0.7. Now the requirement is to come up with 3 'random' numbers that sum up to 2.5 and are all in the range [-1,1]. This problem is very similar to the starting problem, only the dimensions have changed. Now, however, if we take a random number in the range [-1,1] we might end up with no solution. We can restrict our range to make sure that solutions still exist: The sum of the last 2 numbers will be within the range [-2,2]. This means we need to pick a number in the range [0.5,1] to make sure we can reach a total of 2.5.

The section above describes one step in the process.

In general: Determine the range for the next number by applying the range of the rest of the numbers to the required sum.
Pseudo-code:

```<code>function randomNumbers (number, range, sum) {
  restRange = range * (number - 1)
  myRange = intersection ([sum - restRange.upper, sum - restRange.lower], range)

  myNumber = random(myRange)

  rest = randomNumbers (number-1, range, sum - myNumber)

  return [myNumber, rest]
}
</code>```

So for the case described above

```<code>randomNumbers (3, [-1,1], 2.5)
  restRange = [-1,1] * (3-1) = [-2,2]
  myRange = intersection ([2.5-2,2.5-(-2)], [-1,1]) = intersection ([0.5,4.5],[-1,1]) = [0.5,1]
</code>```

A quick-and-dirty implementation in Java:

```<code>public class TestRandomNumberList
{

    @Test
    public void test()
    {
        double[] numbers = new double[5];
        randomNumbers(numbers, 0, -1, 1, 1);
        assertEquals(sum(numbers), 1.0, 0.00001);
        for (double d : numbers)
        {
            assertTrue(d >= -1 );
            assertTrue(d <= 1);
        }
    }

    private void randomNumbers(double[] numbers, int index, double lowerBound, double upperBound, double sum)
    {
        int next = index + 1;
        if (next == numbers.length)
        {
            numbers[index] = sum;
        }
        else
        {
            int rest = numbers.length - next;

            double restLowerBound = lowerBound * rest;
            double restUpperBound = upperBound * rest;

            double myLowerBound = Math.max(lowerBound, sum - restUpperBound);
            double myUpperBound = Math.min(upperBound, sum - restLowerBound);

            numbers[index] = random(myLowerBound, myUpperBound);
            randomNumbers(numbers, next, myLowerBound, myUpperBound, sum - numbers[index]);
        }
    }

    private double random(double myLowerBound, double myUpperBound)
    {
        double random = Math.random();
        return myLowerBound + random * (myUpperBound - myLowerBound);
    }

    private double sum(double[] numbers)
    {
        double result = 0;
        for (double num : numbers)
        {
            result += num;
        }
        return result;
    }

}
</code>```

Body:
As said before, this question actually doesn't have an answer: The restrictions imposed on the numbers make the randomness questionable at best.

However, you could come up with a procedure that returns a list of numbers like that:

Let's say we have picked the first two numbers randomly as -0.8 and -0.7. Now the requirement is to come up with 3 'random' numbers that sum up to 2.5 and are all in the range [-1,1]. This problem is very similar to the starting problem, only the dimensions have changed. Now, however, if we take a random number in the range [-1,1] we might end up with no solution. We can restrict our range to make sure that solutions still exist: The sum of the last 2 numbers will be within the range [-2,2]. This means we need to pick a number in the range [0.5,1] to make sure we can reach a total of 2.5.

The section above describes one step in the process.

In general: Determine the range for the next number by applying the range of the rest of the numbers to the required sum.
Pseudo-code:

function randomNumbers (number, range, sum) {
  restRange = range * (number - 1)
  myRange = intersection ([sum - restRange.upper, sum - restRange.lower], range)

  myNumber = random(myRange)

  rest = randomNumbers (number-1, range, sum - myNumber)

  return [myNumber, rest]
}

So for the case described above

randomNumbers (3, [-1,1], 2.5)
  restRange = [-1,1] * (3-1) = [-2,2]
  myRange = intersection ([2.5-2,2.5-(-2)], [-1,1]) = intersection ([0.5,4.5],[-1,1]) = [0.5,1]

A quick-and-dirty implementation in Java:

public class TestRandomNumberList
{

    @Test
    public void test()
    {
        double[] numbers = new double[5];
        randomNumbers(numbers, 0, -1, 1, 1);
        assertEquals(sum(numbers), 1.0, 0.00001);
        for (double d : numbers)
        {
            assertTrue(d >= -1 );
            assertTrue(d <= 1);
        }
    }

    private void randomNumbers(double[] numbers, int index, double lowerBound, double upperBound, double sum)
    {
        int next = index + 1;
        if (next == numbers.length)
        {
            numbers[index] = sum;
        }
        else
        {
            int rest = numbers.length - next;

            double restLowerBound = lowerBound * rest;
            double restUpperBound = upperBound * rest;

            double myLowerBound = Math.max(lowerBound, sum - restUpperBound);
            double myUpperBound = Math.min(upperBound, sum - restLowerBound);

            numbers[index] = random(myLowerBound, myUpperBound);
            randomNumbers(numbers, next, myLowerBound, myUpperBound, sum - numbers[index]);
        }
    }

    private double random(double myLowerBound, double myUpperBound)
    {
        double random = Math.random();
        return myLowerBound + random * (myUpperBound - myLowerBound);
    }

    private double sum(double[] numbers)
    {
        double result = 0;
        for (double num : numbers)
        {
            result += num;
        }
        return result;
    }

}


答:
Remember the scene in Aladdin where Aladdin wants to impress Jasmine, and the genie tells him he'd do better to just focus on being himself?  Same principle here.

If the boss is that much better than you and you know it, he probably knows it too.  He isn't expecting any great feats of programming rock-stardom out of you.  Since this is your first job, he most likely hired you because he saw the potential to become a good coder in you.  So if you really want to impress him, learn.  Learn the language, learn the system you're working on, learn the ins and outs and dark corners.  Focus on learning correct principles, learning them well and learning them quickly, in that order.

And remember that part of learning is copying knowledge that other people already have.  Don't be afraid to ask questions, either of your coworkers or on StackOverflow, or research things on Google.  Whatever you do, don't pretend you know something when you really don't, in an effort to avoid seeming dumb.  Any good developer will notice quickly, and that will make you look even stupider in their eyes.  Humility tends to still be considered a virtue among engineers.

Do a good job at that and it'll impress the boss.

Title: How can a new programmer impress the software engineer (boss)?

Body:
I'm working at my first programming job. My boss is a very smart software engineer, and I feel
like I have very little to offer compared to him. Problem is, he is always busy, and needs someone to help him out. I feel like I'm not good enough, but I still want to succeed. I want to be a great programmer.

What can I do to impress him?

Thank you.


问:
Developing configuration syntax - best practise/rules/methods?
I am currently developing a small application, which checks if provided data meets certain requirements. The requirements are actually a long list, and might be changing, so I defined a syntax which allows me to state all of the requirements briefly and in a seperate file.

Now the overall requirements for the application have changed, and I need to change my configuration syntax. Which leeds me to wonder if there is methodoloy or best practise for developing such syntaxes. Currently what I do is

I think about the requirements and come up with an initial syntax,

start configuring the first few items and see how it works.

If I come upon something that does not work well or not at all with the current syntax, I change the syntax, if possible in a backward compatible way.

This somehow works for me, but it feels a bit like fishing in troubled water. Also I feel it does not nessessarly lead to the most concise and easy to understand/use syntax.

So I was wondering what other people do, especially if there is a better approach to this.

答:
My experience is that it isn't worth spending too much time on perfecting a syntax for expressing requirements.

The requirements will change again, and very probably in ways that you didn't anticipate, so it's unlikely that you will hit on the perfect solution in any reasonable time frame. It is usually easier to just keep around the old parsing code as a fallback to call when dealing with legacy data, even a series of such fallbacks if necessary. It looks kind of ugly, but backwards compatibility is ugly, and I consider it OK if the code reflects that, as long as each alternative parsing strategy is reasonably nice on its own.


答:
I used a utility program many years ago which did a similar thing. Given a flat file, it could parse each record and check each column met certain requirements.

The basic blocks were types, ranges and values.

Types were very basic ones such as:

String

Int

Float

Date

For some of these columns, ranges could be defined. For example, you could say that for a certain column, values 101-999 were valid values.

For more complex columns, you could define the valid values. E.g. for gender you might have:

M

F

U

Simple but effective. We used it for all kinds of data files.

Title: Developing configuration syntax - best practise/rules/methods?

Body:
I am currently developing a small application, which checks if provided data meets certain requirements. The requirements are actually a long list, and might be changing, so I defined a syntax which allows me to state all of the requirements briefly and in a seperate file.

Now the overall requirements for the application have changed, and I need to change my configuration syntax. Which leeds me to wonder if there is methodoloy or best practise for developing such syntaxes. Currently what I do is

I think about the requirements and come up with an initial syntax,

start configuring the first few items and see how it works.

If I come upon something that does not work well or not at all with the current syntax, I change the syntax, if possible in a backward compatible way.

This somehow works for me, but it feels a bit like fishing in troubled water. Also I feel it does not nessessarly lead to the most concise and easy to understand/use syntax.

So I was wondering what other people do, especially if there is a better approach to this.


问:
Do I need to use an interface when only one class will ever implement it?
Isn't the whole point of an interface that multiple classes adhere to a set of rules and implementations?</p>

答:
There is not always a need to define an interface for a class.

Simple objects like value objects don't have multiple implementations.  They don't need to be mocked either.  The implementation can be tested on its own and when other classes are tested that depend on them, the actual value object can be used.

Remember that creating an interface has a cost.  It needs to be updated along the implementation, it needs an extra file, and some IDE will have trouble zooming in the implementation, not the interface.

So I would define interfaces only for higher level classes, where you want an abstraction from the implementation.

Note that with a class you get an interface for free.  Besides the implementation, a class defines an interface from the set of public methods.  That interface is implemented by all derived classes.  It is not strictly speaking an interface, but it can be used exactly in the same way.  So I don't think it is necessary to recreate an interface that already exists under the name of the class.

ContentLicense: CC BY-SA 4.0


答:
@Mason is right

listen

learn

ask relevant questions at respectful intervals (e.g. twice a day, not every 5 minutes)

relax and be yourself


答:
I'd start out by simplifying the problem. If you have N numbers from [A,B], they will add up to a value between N*A and N*B. Just subtract A from each number and N*A from the target X.

So, without loss of generality we can look for N numbers between [0, B'] which add up to X'. Again, this can be simplified by dividing X' by B'.

The resulting problem is looking for N numbers in [0,1] which add up to X" = (X-A)/(B-A).

What's the distribution of these random numbers? The requirement that they add up to X" means that they can't be independent distributions. I'll assume that we want the same distribution for all N numbers. Otherwise, it's trivial. If we know all distributions are the same, we do know that their mean has to be X"/N. This is not necessarily 1/2, so we have to come up with a possibly asymmetric distribution over [0,1] - a symmetric distribution does have mean 0,5 since p(x)==p(1-x). Yet all numbers should be possible if X"/N is not 0 or 1.

At this point, you can freely choose any parametrized distribution which allows for means in (0,1). An exponential distribution should work fine.

Finally, once you've drawn one number A(0) from this distribution, you should recurse and calculate a new distribution for the next number as you now need to find N-1 numbers which add up to X"-A(0). And of course for the last number you have no choice left.


问:
How would you rewrite/refactor this ?
Old application that is used by 50-60.000 paying customers.
Company is several hundred people big.
Application has a lot of business critical code (30% of all code) written in classic asp.
Application has a lot more .net code.
Application has a COM+ bridge for enabling asp to "talk" to .net
Organization lacks some/a lot knowledge on what is causing the 10-20% server-reset per day (might be due to COM+ ?)
There is no red line through the application; no architecture, no real patterns etc.
The application has been like this for at least 5 years.
The asp code base is increasing, slowly but certainly.

I have read refactoring stories and I have knowledge on why you some of the times should not re-write a system.

I would love for the old asp code to vanish as well as the COM+ component. But the pain is that no one really knows what is going on inside the asp classic code and the attitude inside all the teams are "this is just how it is".

Down the line, this causes a lot of other issues like recruiting, dev effeciency, business needs that cannot be met, scale etc.

With these little facts, does that justify a re-write of the asp code and the removal of the COM+ component ?

How would you go about it ?

答:
With these little facts you provided, it is impossible to tell.

In the end, this is a cost/benefit analysis.

What will the rewrite cost?

What cost does administration of the system create right now and what will it cost after the rewrite?

What's the current cost of doing a typical change to the system and what will be the cost after the rewrite?

Also keep in mind that even when you get some short-term personnel increase during the rewrite (like by hiring an external consulting firm to do it), the rewrite will cost you resources. That means the support situation will be worse while you do the rewrite. Also, forget about the wishful thinking that the new application will be perfect from day one. It will have bugs and design errors at first which will likely take several years to fix. It will take a while until it is just as productive as the old one.

When you keep all this in mind and quantify it in money, you should be able to create a cost/benefit analysis which says how long it will take until the investment has amortized itself. Provide this to management and have them decide if it is worth it or not.

You should also consider an alternative approach: Make the best out of what you have. You said "no one really knows what is going on inside the asp classic code". This sounds like a lack of documentation. Starting a project now to write a proper documentation for the system might help you in the future to make changes and do troubleshooting. While you do this you might also be able to identify the core problems with the system and find a fix for them without having to rewrite the code which works properly.

Title: How would you rewrite/refactor this ?


问:
How to handle business rules with a REST API?
I have a REST API to manage a booking system
I'm searching how to manage this situation :

A customer can book a time slot :
A TimeSlot resource is created and linked to a Person resource. In order to create the link between a time lot and a person, the REST client send a POST request on the TimeSlot resource

But if too many people booked the same slot (let's say the limit is 5 links), it must be impossible to create more associations.

How can I handle this business restriction ? Can I return a 404 status code with a JSON response detailing the error with a status code ?

Is it a RESTFul approach ?

<s>EDIT :

Like suggested below I used status 409 Conflict in addition to a JSON response detailing the error</s>

EDIT 2 :

Like detailed in the @Cormac Mulhall, the 403 Forbidden is the most accurate status code for this case

答:
You want to you 403 Forbidden. From the HTTP specs

403 Forbidden: The server understood the request, but is refusing to fulfill it. Authorization will not help and the request SHOULD NOT be repeated.

You shouldn't use 409, it implies that the client might be able to resolve the conflict, which in the case of over booking the client can't do as the conflict rules are server side.

Remember in the context of HTTP status codes "conflict" means a conflict in the state of the resource (in this case the book time slot), for example it expects time in one format and you sent it in the wrong format [bad example, better one would be two clients update a resource at the same time and the server cannot figure out which update to save, like on a version controlled system]. The resource is a time slot, if you change to a different time slot you have created a different resource, not resolved a conflict with this resource.

409 Conflict: The request could not be completed due to a conflict with the current state of the resource. This code is only allowed in
  situations where it is expected that the user might be able to resolve
  the conflict and resubmit the request.

The server should return 403 Forbidden with an explanation that it is refusing to save the state because that time slot is already full. Nothing the client can do in altering the state of the resource can change that. The client should not re-submit. They must instead create a new time slot instead (you could return the closest free slot as a suggestion).


问:
SVN: Working with branches using the same working copy
We've just moved to SVN from CVS. We have a small team and everyone checks in code on the trunk and we have never ever used branches for development.

We each have directories on a remote dev server with the codebase checked out. Each developer works on their own sandbox with an associated URL to pull up the app in a browser (something like the setup here: Trade-offs of local vs remote development workflows for a web development team).

I've decided that for my current project, I'll use a branch because it would span multiple releases. I've already cut a branch out, but I am using the same directory as the one originally checked out (i.e. for the trunk).

Since it's the same directory (or working copy) for both the branch and the trunk, if for e.g. a bug pops up in the app I switch to the trunk and commit the change there, and then switch back to my branch for my project development.

My questions are:

Is this a sane way to work with branches?

Are there any pitfalls that I need to be aware of?

What would be the optimal way to work with branches if separate working copies are out of the question?

I haven't had issues yet as I have just started doing this way but all the tutorials/books/blog posts I have seen about branching with SVN imply working with different working copies (or perhaps I haven't come across an explanation of mixed working copies in plain English).

I just don't want to be sorry three months down the road when its time to integrate the branch back to the trunk.

答:
You can work this way quite happily - I do nowadays and is the way to work with feature branches. There are 2 pitfall you need to be aware of though.

Any work you have uncommitted with remain in your working copy, and when you switch it will be merged. this can mean you may commit work intended for the other branch. Ultimately its a person problem - you need to be organised.

Occasionally I will do work on a branch and realise, d'oh, I was using the wrong branch. Its easy to switch to the right one, keeping your work (as it gets merged when you switch). Still, you need to be organised.

I used ot checkout a new WC for each branch, which was a good workflow for the organisation we had in the previous company. With spare checkouts this is quick and easy.
Now I use feature branches, all dev for any bug gets done on an individual branch and merged to trunk after various quality checks. This works well too except when you make a bugfix that doesn't get merged to trunk for some time (for "project" reasons typically). I can work on 4 or 5 branches a day with this workflow.


答:
Most languages have some sort of validation library available to you. In C#, for example, I've used FluentValidation, which allows you to specify your business rules with a fluent API.

```<code>public class Post
{
    public string Title;
    public string Body;
    public DateTime CreatedAt;
}
</code>```

And the validator:

```<code>public class PostValidator : BaseValidator<Post>
{
    public PostValidator()
    {
        RuleFor(p => p.Title)
            .NotEmpty()
            .Length(1,50);

        RuleFor(p => p.Body)
            .NotEmpty();
    }
}
</code>```

Something like this is recommended, especially since many of these validation libraries are unit testable.

Body:
Most languages have some sort of validation library available to you. In C#, for example, I've used FluentValidation, which allows you to specify your business rules with a fluent API.

public class Post
{
    public string Title;
    public string Body;
    public DateTime CreatedAt;
}

And the validator:

public class PostValidator : BaseValidator<Post>
{
    public PostValidator()
    {
        RuleFor(p => p.Title)
            .NotEmpty()
            .Length(1,50);

        RuleFor(p => p.Body)
            .NotEmpty();
    }
}

Something like this is recommended, especially since many of these validation libraries are unit testable.


答: This seems like the sort of situation where XML would work well. You can start with basic tags for things like being required, or being a given length, etc; and if later you get new requirements that need a new kind of constraint, you just add a tag and code to check for that tag.</p>


问:
How to know whether to create a general system or to hack a solution
I'm new to coding , learning it since last year actually.

One of my worst habits is the following:

Often I'm trying to create a solution that is too big , too complex and doesn't achieve what needs to be achieved, when a hacky kludge can make the fit.

One last example was the following (see paste bin link below)
http://pastebin.com/WzR3zsLn

After explaining my issue, one nice person at stackoverflow came with this solution instead
https://stackoverflow.com/questions/25304170/update-a-field-by-removing-quarter-or-removing-month

When should I keep my code simple and when should I create a 'big', general solution? I feel stupid sometimes for building something so big, so awkward, just to solve a simple problem. It did not occur to me that there would be an easier solution.

Any tips are welcomed.

Best

答:
A good guideline that helps knowing when to create a "large", general and abstract solution, and when to just solve the specific problem, is The Rule Of Three.

The Rule Of Three is often applied to duplication: i.e. "if code is duplicated more then twice, move it to a function". However it also applies to the abstraction vs. solving-the-specific-problem argument:

First time that you need to solve the problem, just solve it. Don't try to build a big system that can handle more general cases, as you're most likely wrong in your estimations, and you will only lose clarity and add complication.

Second time you come back to the same problem, refine the design a little but still don't rush into building a big system.

Third time the same problem appears, you can build a general system to handle problems of this kind.

Don't rush into general solutions as you will only gain complication and lose clarity and focus.

A personal note: it took me some time myself to realize that usually, simple is better. You can always come back and refactor when a more general system is needed. But insisting on creating a big system to solve every small problem that appears, doesn't only complicate the code for nothing but also drains the mental energy that you should invest in actually working on the application.


问:
Why does void in C mean not void?
In strongly-typed languages like Java and C#, void (or Void) as a return type for a method seem to mean:

This method doesn't return anything. Nothing. No return. You will not receive anything from this method.

What's really strange is that in C, void as a return type or even as a method parameter type means:

It could really be anything. You'd have to read the source code to find out.  Good luck. If it's a pointer, you should really know what you're doing.

Consider the following examples in C:

```<code>void describe(void *thing)
{
    Object *obj = thing;
    printf("%s.\n", obj->description);
}

void *move(void *location, Direction direction)
{
    void *next = NULL;

    // logic!

    return next;
}
</code>```

Obviously, the second method returns a pointer, which by definition could be anything.

Since C is older than Java and C#, why did these languages adopt void as meaning "nothing" while C used it as "nothing or anything (when a pointer)"?

答:
You can kind of think of the keyword void like an empty struct (In C99 empty structs are apparently disallowed, in .NET and C++ they take up more than 0 memory, and last I recall everything in Java is a class):

```<code>struct void {
};
</code>```

...which means it's a type with 0 length.  This is how it's working when you do a function call that returns void... instead of allocating 4 bytes or so on the stack for an integer return value, it doesn't change the stack pointer at all (increments it by a length of 0).

So, if you declared some variables like:

```<code>int a;
void b;
int c;
</code>```

... and then you took the address of those variables, then perhaps b and c might be located at the same place in memory, because b has zero length.  So a void* is a pointer in memory to the built-in type with zero length.  The fact that you might know that there's something immediately after it that might be useful to you is another matter, and requires you to really know what you're doing (and is dangerous).

Title: Why does void in C mean not void?

Body:
You can kind of think of the keyword void like an empty struct (In C99 empty structs are apparently disallowed, in .NET and C++ they take up more than 0 memory, and last I recall everything in Java is a class):

struct void {
};

...which means it's a type with 0 length.  This is how it's working when you do a function call that returns void... instead of allocating 4 bytes or so on the stack for an integer return value, it doesn't change the stack pointer at all (increments it by a length of 0).

So, if you declared some variables like:

int a;
void b;
int c;

... and then you took the address of those variables, then perhaps b and c might be located at the same place in memory, because b has zero length.  So a void* is a pointer in memory to the built-in type with zero length.  The fact that you might know that there's something immediately after it that might be useful to you is another matter, and requires you to really know what you're doing (and is dangerous).


答:
Some expert opinions on rewriting software. I especially like R.C. Martins "Big Redesign in the Sky".

Imho, even crappy software can improve over time. It will take time, it will be much effort, and it will certainly only work out if all team members want it to, because it requires that no new ugly code is written and that every component that is touched gets incrementally improved.

You might also wanna have a look at the Mikado-Method for Refactoring of large Legacy Software. It helps me a lot to remember that principle.

Hope this helps somewhat!

Title: How would you rewrite/refactor this ?

Body:
Old application that is used by 50-60.000 paying customers.
Company is several hundred people big.
Application has a lot of business critical code (30% of all code) written in classic asp.
Application has a lot more .net code.
Application has a COM+ bridge for enabling asp to "talk" to .net
Organization lacks some/a lot knowledge on what is causing the 10-20% server-reset per day (might be due to COM+ ?)
There is no red line through the application; no architecture, no real patterns etc.
The application has been like this for at least 5 years.
The asp code base is increasing, slowly but certainly.

I have read refactoring stories and I have knowledge on why you some of the times should not re-write a system.

I would love for the old asp code to vanish as well as the COM+ component. But the pain is that no one really knows what is going on inside the asp classic code and the attitude inside all the teams are "this is just how it is".

Down the line, this causes a lot of other issues like recruiting, dev effeciency, business needs that cannot be met, scale etc.

With these little facts, does that justify a re-write of the asp code and the removal of the COM+ component ?

How would you go about it ?


问:
how should I change the representation (not mimetype) of a resource?
I'm looking at how I can change the representation of a payload at runtime for varied potential advantages, but I'm not sure how to do it. Specifically collections.

Array of Pairs

```<code>[{ <resource_uri> : { <entity> }, ...}]
</code>```

Array of Objects

```<code>[<entity>,...]
</code>```

Array of Resources

```<code>[<resource_uri>]
</code>```

Map of entities

```<code>{ <resource_uri> : { <entity> }, ... }
</code>```

My problem is, I'm not sure if I should put these different representations of the sets at different URI's, give them slightly varied mime types, e.g. application/foomap+json or perhaps use an optional query parameter ?format=map, or resource /entities/map. The UI is going to hide this, this is for programmatic web service access only (which the "UI's JS will have to call).

答:
First things first you should use MIME/Content types. The client will (should) provide the server with all the data formats it can understand and the server will return the best representation, or complain that it doesn't support any of the client's data format requests. Query strings are generally used for filters on resources that don't fit nicely into URL heirachy (e.g. "/stationary/pens?color=black")

Secondly, it is not the best idea to have lots of similar but slightly different representations of the resource, and I can't really imagine why you would want to do this.

You can always repeat data in your response, if you have a resource with JSON data you can also include the URL to it (in fact you should do this for HATEOS). The client can ignore the data it is not interested in.


问:
protect purchased games to be downloaded on other pc
I want to make a downloads managing system on my website, that when you purchase a download, you can always re-download it again for free. (i have read that Steam does something similar)

The problem is, that someone would be able to create a account, purchase something and then give the account password to friends and family, so they can all download copies for free.

Is there a way to prevent this? Or is it impossible.

答:
In the age of the Internet, copy protection is a fool's errand, and it's never worth your time and effort to even try.

Consider Microsoft, who spends more resources on R&D in a single month than you'll ever have in your entire life put together.  They're obsessive about copy protection, and yet the latest version of Windows had a crack available before RTM.  If they can't pull it off, why would you ever think you have a chance?

The deck is stacked against you from the beginning.  First, we have the theory.  You can frame the basic issue here as the fundamental problem of cryptography: Alice wants to send a message to Bob, and ensure that Charlie is not able to read it even if it should fall into his hands.  Except in this case, Bob and Charlie are the same person.

Now we have the details.  If your system downloads something when a person gives the right account credentials, then a person with the right account credentials can download it.  It's really that simple, and any additional complexity you introduce will cause unhappy customers.

A central server makes sure that you're logged in properly before the software will launch?  Nice going, bozo.  Now anyone with a flaky Internet connection can't use what they legitimately bought and paid for.

A server ensures the user isn't logged in twice at the same time?  You just tossed a dozen very useful testing scenarios out the window.

Fingerprint the computer so they can't re-download to a different system?  Sounds great, right up until that computer dies and they get a new one.

Bottom line: don't even try.  It doesn't work.  Never has, never will, never can.  All you end up doing is wasting money, annoying your customers, and driving them away.  There is one and only one way to make money in software, and that's basic market economics:  Sell your product for less than what the prospective buyer sees as its value to him, and he will pay for it.


答:
The best thing I can think of is to detect if someone has logged in to the user account from multiple locations at the same time. If you detect that UserA has logged in from ComputerA, and then again from ComputerB, without logging out from ComputerA first, it's possible that two different people are both useing the account of UserA. In that case, you can prevent the login from ComputerB, until the account is logged out from ComputerA (or the session expires after a period of time). You'd have to identify the different computers by IP address or something like that.

Of course, it's also possible that the legitimate owner of UserA's account is using two machines near each other, but somehow appearing to connect to your server from two different locations. Maybe a home PC connected to the internet via an ISP, and a smartphone in their hand that's connected via the wireless phone company, not the same network as the PC.

And then there's also the problem of users behind a NAT sharing the account: you might only be able to identify two logins from the same IP address... what then? Is it two machines or one?

You could require the user change their password every now and then, but the owner of the account will just share that new password with their friends, and this could make the system even less convenient to use, and people might not want to use it.

I don't think there's any technological solution to this problem that can work 100%, so you'll have to think of another way to discourage users from doing this.

Maybe by offering users the chance to "gift" a few free files to their selected friends now and then (and maybe how much a user can gift is based on how much they spend ;) ), they'll be less discouraged to sharing full access to their accounts.

Title: protect purchased games to be downloaded on other pc

Body:
I want to make a downloads managing system on my website, that when you purchase a download, you can always re-download it again for free. (i have read that Steam does something similar)

The problem is, that someone would be able to create a account, purchase something and then give the account password to friends and family, so they can all download copies for free.

Is there a way to prevent this? Or is it impossible.


问:
Overloading methods that do logically different things, does this break any major principles?
This is something that's been bugging me for a bit now. In some cases you see code that is a series of overloads, but when you look at the actual implementation you realize they do logically different things. However writing them as overloads allows the caller to ignore this and get the same end result. But would it be more sound to name the methods more explicitly then to write them as overloads?

```<code>public void LoadWords(string filePath)
{
    var lines = File.ReadAllLines(filePath).ToList();

    LoadWords(lines);
}

public void LoadWords(IEnumerable<string> words)
{
    // loads words into a List<string> based on some filters
}
</code>```

Would these methods better serve future developers to be named as LoadWordsFromFile() and LoadWordsFromEnumerable()? It seems unnecessary to me, but if that is better what programming principle would apply here?

On the flip side it'd make it so you didn't need to read the signatures to see exactly how you can load the words, which as Uncle Bob says would be a double take. But in general is this type of overloading to be avoided then?

答:
This violates at least three principles of object-oriented programming, and potentially a fourth.

First, this violates the Principle of Least Astonishment. Let's assume we are given documentation for only one of the methods, and we have to infer what the other does. First:

```<code>// Reads words from a file
public void LoadWords(string)

// ???
public void LoadWords(IEnumerable<string>)
</code>```

It would be very easy to look at the first, and think that the second would read words from a list of files. That would be consistent behavior. It doesn't, though. Now the reverse:

```<code>// ???
public void LoadWords(string)

// Loads the IEnumerable of strings into words
public void LoadWords(IEnumerable<string>)
</code>```

It would now be very easy to look at the second, and think that the first would load a single word. Instead, I would get IO errors or potentially something totally unexpected (if the word happened to match a real file name) if I tried to use it as such. This method is hard to reason about because the behaviors are so different.

Second, this code violates the Law of Demeter. In a method, I now have to reach out and interact with the file system, which is a totally unrelated object at a completely different level of abstraction. Where before I was dealing with filtering and saving words, now I have to worry about file system code. I have to know how to get to the file system, open a file, read a file, close a file, tokenize the file contents, catch any exception any of those might incur, etc. This should not be this class's responsibility, which also shows it is a violation of the Single Responsibility Principle.

Finally, we can also violate the Open-Closed Principle. Say we need to overload the method to read from a network stream. We have to open up this class to add the additional functionality. This also exacerbates the Demeter and SRP violations.

If the methods are named more explicitly, we start to leak out implementation details about our class. In this case, the reason is that the responsibilities should not lie in our class. We should push those implementation decisions out to the class's clients. This allows them to make the decision, as they were already doing, but offers them far more flexibility and control, while alleviating your class of the burden of handling all those details internally.

back2dos' answer shows how to accomplish this through client code.

Body:
This violates at least three principles of object-oriented programming, and potentially a fourth.

First, this violates the Principle of Least Astonishment. Let's assume we are given documentation for only one of the methods, and we have to infer what the other does. First:

// Reads words from a file
public void LoadWords(string)

// ???
public void LoadWords(IEnumerable<string>)

It would be very easy to look at the first, and think that the second would read words from a list of files. That would be consistent behavior. It doesn't, though. Now the reverse:

// ???
public void LoadWords(string)

// Loads the IEnumerable of strings into words
public void LoadWords(IEnumerable<string>)

It would now be very easy to look at the second, and think that the first would load a single word. Instead, I would get IO errors or potentially something totally unexpected (if the word happened to match a real file name) if I tried to use it as such. This method is hard to reason about because the behaviors are so different.

Second, this code violates the Law of Demeter. In a method, I now have to reach out and interact with the file system, which is a totally unrelated object at a completely different level of abstraction. Where before I was dealing with filtering and saving words, now I have to worry about file system code. I have to know how to get to the file system, open a file, read a file, close a file, tokenize the file contents, catch any exception any of those might incur, etc. This should not be this class's responsibility, which also shows it is a violation of the Single Responsibility Principle.

Finally, we can also violate the Open-Closed Principle. Say we need to overload the method to read from a network stream. We have to open up this class to add the additional functionality. This also exacerbates the Demeter and SRP violations.

If the methods are named more explicitly, we start to leak out implementation details about our class. In this case, the reason is that the responsibilities should not lie in our class. We should push those implementation decisions out to the class's clients. This allows them to make the decision, as they were already doing, but offers them far more flexibility and control, while alleviating your class of the burden of handling all those details internally.

back2dos' answer shows how to accomplish this through client code.


答:
You might limit the number of downloads allowed per purchased item to prevent the scenario you describe.  This won't prevent piracy, of course, but it will prevent people from copying the game in large numbers by sharing accounts.  You might set a limit of 5 or 10 downloads (only count finished downloads), and then require they contact tech support if for some reason they need more than that.

I agree with others - you're not going to stop piracy. All it takes is one copy out on a torrent site and anyone who wants it can have it.  So the best strategy, imo, is to make casual copying of the program inconvenient enough to encourage people to purchase.


问:
Security Pattern to store SSH Keys
I am writing a simple flask application to submit scientific tasks to remote HPC resources. My application in background talks to remote machines via SSH (because it is widely available on various HPC resources). To be able to maintain this connection in background I need either to use the user's ssh keys on the running machine (when user's have passwordless ssh access to the remote machine) or I have to store user's credentials for the remote machines.

I am not sure which path I have to take, should I store remote machine's username/password or should I store user's SSH key pair in database?

I want to know what is the correct and safe way to connect to remote servers in background in context of a web application.

答:
SSH keys are usually better than passwords because:

You can configure a different key for different scenarios, e.g. for different client machines or different applications. If one becomes compromised, you can remove that key from the server but clients using the other key will continue functioning normally. With a password, you only have one, so you can't change it for some clients without forcing others to change as well.

Keys of reasonable length are much harder to crack than passwords.

People may write down their passwords on a post-it note and put it at risk of being seen by someone else. Keys are so long nobody will print them out.

Title: Security Pattern to store SSH Keys


问:
The ViewController design in iOS vs MVC in tradiational web apps design
As I came from the background of webapps and move to iOS programming, I am wondering why we can't just apply the MVC design approach to native apps,

i.e. in iOS, the view and controller are tightly coupled, are there any reason?

答:
In the context of web applications, Model View Controller is a fairly straightforward request/response paradigm.  You push a form out to the user (the "View"), they fill it in, and then they click a Submit button.

This paradigm can be elaborated on via Javascript, AJAX, client-side validation, additional user interactivity, and so forth, but these elaborations are not, strictly speaking, part of MVC proper, and they don't change the fundamental request/response nature of the paradigm.

For those environments where you need greater coordination between the View and the Controller, you either use a bastardized form of MVC, or something like MVVM or MVP where those interactions are more formally specified as part of the paradigm.


答:
Elaborating on @back2dos answer, there are two things going on in your code.

loading words into a List based on some filters

parsing words (in this case, from a file, splitting on new lines)

So I'd vote for two methods.  Note - I'm a Java guy so I'm guessing about the types...

```<code>  void filterWords(IEnumerable<string> rawwords) // note - why return void instead of List?
  List<string> parseWords(InputStream in)
</code>```

Whether these belong in separate classes or one class depends on how specific splitting on new lines is to this particular class.  If that happens other places in your code (or you can easily imagine it happening in the future) that should be split off.

By using a stream instead of a filename in parseWords you gain some flexibility and abstraction  (e.g., you could use an Http GET).  Perhaps stream is not the right abstraction, if so, think about what is right.

Title: Overloading methods that do logically different things, does this break any major principles?

Body:
This is something that's been bugging me for a bit now. In some cases you see code that is a series of overloads, but when you look at the actual implementation you realize they do logically different things. However writing them as overloads allows the caller to ignore this and get the same end result. But would it be more sound to name the methods more explicitly then to write them as overloads?

```<code>public void LoadWords(string filePath)
{
    var lines = File.ReadAllLines(filePath).ToList();

    LoadWords(lines);
}

public void LoadWords(IEnumerable<string> words)
{
    // loads words into a List<string> based on some filters
}
</code>```

Would these methods better serve future developers to be named as LoadWordsFromFile() and LoadWordsFromEnumerable()? It seems unnecessary to me, but if that is better what programming principle would apply here?

On the flip side it'd make it so you didn't need to read the signatures to see exactly how you can load the words, which as Uncle Bob says would be a double take. But in general is this type of overloading to be avoided then?

Body:
Elaborating on @back2dos answer, there are two things going on in your code.

loading words into a List based on some filters

parsing words (in this case, from a file, splitting on new lines)

So I'd vote for two methods.  Note - I'm a Java guy so I'm guessing about the types...

  void filterWords(IEnumerable<string> rawwords) // note - why return void instead of List?
  List<string> parseWords(InputStream in)

Whether these belong in separate classes or one class depends on how specific splitting on new lines is to this particular class.  If that happens other places in your code (or you can easily imagine it happening in the future) that should be split off.

By using a stream instead of a filename in parseWords you gain some flexibility and abstraction  (e.g., you could use an Http GET).  Perhaps stream is not the right abstraction, if so, think about what is right.


问:
Try and catch error trapping, why is it so significant?
I try to understand why should I "try" a method to catch errors.

It looks to me like the concept is: "letting a process run assuming its not properly
developed".

Should I always assume that a program based on classes and inheritance  is bound for
 unexpected errors, which I should handle with tools like try catch and throw?

Should all Java programs be included within try catch framework?

答:
it looks to me like the concept is:- ' letting a process run assuming its not properly developed.

Error conditions are often beyond the control of the programmer at the level where they occur. Exception handling is one way to handle problems that come up.

For example, consider a purchasing account that lets a user order products and pay for them out of an associated bank account. If the purchasing account was set up properly, it should work fine every time. But what if the account was created but no bank account was ever associated with the purchasing account? That's an error that the purchasing account can't (or probably shouldn't) deal with by itself.

Exception handling is designed to help errors propagate up to a level where they can be handled most effectively.

should i always assume that a program based on classes and inheritance is bound for unexpected errors

Any sufficiently complex software is likely to have potential for errors. Any interesting software that's used by people is almost certain to need to handle errors in some form. Inheritance and classes don't necessarily have anything to do with the issue -- exception handling can be done in procedural languages like C.

On the other hand, exception handling isn't the only way to handle problems -- Objective-C has an exception handling mechanism, but throwing an exception is usually reserved for fatal errors.

which i should handle with such a heavy tools like try catch throw throws ?

Use it when it's the right tool for the job. Whether it's the right tool or not depends in part on the conventions used in the context you're working in, the tools your language provides, your ability to handle the error condition yourself, etc.

should all java programs be included withing try catch framework?

Not necessarily -- some programs are simple enough not to need exception handling. But any skilled Java programmer will understand the mechanism, be prepared to handle exceptions thrown by any classes they use, and know when to throw an exception.


问:
Is method overloading anything more than syntactic sugar?
Is method overloading a type of polymorphism? To me it seems like simply the differentiation of methods with the same name and different parameters. So stuff(Thing t) and stuff(Thing t, int n) are entirely different methods as far as the compiler and runtime are concerned.

It creates the illusion, on the caller's side, that it's the same method that acts differently on different kinds of objects - polymorphism. But that's only an illusion, because actually stuff(Thing t) and stuff(Thing t, int n) are completely different methods.

Is method overloading anything more than syntactic sugar? Am I missing something?

A common definition for syntactic sugar, is that it is purely local. Meaning changing a piece of code to its 'sweetened' equivalent, or vice versa, involves local changes that don't affect the overall structure of the program. And I think method overloading precisely fits this criterion. Let's look at an example to demonstrate:

Consider a class:

```<code>class Reader {
    public String read(Book b){
        // .. translate the book to text
    }
    public String read(File b){
        // .. translate the file to text
    }
}
</code>```

Now consider another class that uses this class:

```<code>/* might not be the best example */
class FileProcessor {
    Reader reader = new Reader();
    public void process(File file){
        String text = reader.read(file);
        // .. do stuff with the text
    }
}
</code>```

Okay. Now let's see what needs to change if we replace the method overloading with regular methods:

The read methods in Reader change to readBook(Book) and readFile(file). Only a matter of changing their names.

The calling code in FileProcessor changes slightly: reader.read(file) changes to reader.readFile(file).

And that's it.

As you can see, the difference between using method overloading and not using it, are purely local. And that's why I think it qualifies as pure syntactic sugar.

I'd like to hear your objections if you have some, maybe I'm missing something.

答:
To answer this, you first need a definition for "syntactic sugar." I'll go with Wikipedia's:

In computer science, syntactic sugar is syntax within a programming
  language that is designed to make things easier to read or to express.
  It makes the language "sweeter" for human use: things can be expressed
  more clearly, more concisely, or in an alternative style that some may
  prefer.

[...]

Specifically, a construct in a language is called syntactic sugar if
  it can be removed from the language without any effect on what the
  language can do

So, under this definition, features such as Java's varargs or Scala's for-comprehension are syntactic sugar: they translate into underlying language features (an array in the first case, calls to map/flatmap/filter in the second), and removing them would not change the things you can do with the language.

Method overloading, however, is not syntactic sugar under this definition, because removing it would fundamentally change the language (you'd no longer be able to dispatch to distinct behavior based on arguments).

True, you can simulate method overloading as long as you have some way to access the arguments of a method, and can use an "if" construct based on the arguments that you're given. But if you consider that syntactic sugar, you'd have to consider anything above a Turing machine to likewise be syntactic sugar.


答:
For contemporary languages, it's just syntactic sugar; in a completely language-agnostic sort of way, it's more than that.

Previously this answer stated simply that it's more than syntactic sugar, but if you'll see in the comments, Falco raised the point that there was one piece of the puzzle that contemporary languages appear to all be missing; they don't mix method overloading with dynamic determination of which function to call in the same step.  This will be clarified later.

Here's why it should be more.

Consider a language that supports both method overloading and untyped variables.  You could have the following method prototypes:

```<code>bool someFunction(int arg);

bool someFunction(string arg);
</code>```

In some languages, you would be probably be resigned to knowing at compile time which one of these would be called by a given line of code.  But in some languages, not all variables are typed (or they're all implicitly typed as Object or whatever), so imagine building a dictionary whose keys map to values of different types:

```<code>dict roomNumber; // some hotels use numbers, some use letters, and some use
                 // alphanumerical strings.  In some languages, built-in dictionary
                 // types automatically use untyped values for their keys to map to,
                 // so it makes more sense then to allow for both ints and strings in
                 // your code.
</code>```

Now then, what if you wanted to apply someFunction to one of those room numbers?  You call this:

```<code>someFunction(roomNumber[someSortOfKey]);
</code>```

Is someFunction(int) called, or is someFunction(string) called?  Here you see one example where these are not totally orthogonal methods, especially in higher-level languages.  The language has to figure out - during runtime - which one of these to call, so it still has to regard these as being at least somewhat the same method.

Why not simply use templates?  Why not simply use an untyped argument?

Flexibility and finer-grained control.  Sometimes using templates / untyped arguments are a better approach, but sometimes they're not.

You have to think about cases where, for instance, you might have two method signatures that each take an int and a string as arguments, but where the order is different in each signature.  You may very well have a good reason to do this, as each signature's implementation may do largely the same thing, but with just a slightly different twist; the logging could be different, for example.  Or even if they do the same exact thing, you may be able to automatically glean certain information from just the order in which the arguments were specified.  Technically you could just use pseudo-switch statements to determine the type of each of the arguments passed in, but that gets messy.

So is this next example bad programming practice?

```<code>bool stringIsTrue(int arg)
{
    if (arg.toString() == "0")
    {
        return false;
    }
    else
    {
        return true;
    }
}

bool stringIsTrue(Object arg)
{
    if (arg.toString() == "0")
    {
        return false;
    }
    else
    {
        return true;
    }
}

bool stringIsTrue(string arg)
{
    if (arg == "0")
    {
        return false;
    }
    else
    {
        return true;
    }
}
</code>```

Yes, by and large.  In this particular example, it could keep somebody from trying to apply this to certain primitive types and getting back unexpected behavior (which could be a good thing); but let's just assume I abbreviated the code above, and that you, in fact, have overloads for all the primitive types, as well as for Objects.  Then this next bit of code really is more appropriate:

```<code>bool stringIsTrue(untyped arg)
{
    if (arg.toString() == "0")
    {
        return false;
    }
    else
    {
        return true;
    }
}
</code>```

But what if you only needed to use this for ints and strings, and what if you want it to return true based on simpler or more complicated conditions accordingly?  Then you have a good reason to use overloading:

```<code>bool appearsToBeFirstFloor(int arg)
{
    if (arg.digitAt(0) == 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool appearsToBeFirstFloor(string arg)
{
    string firstCharacter = arg.characterAt(0);
    if (firstCharacter.isDigit())
    {
        return appearsToBeFirstFloor(int(firstCharacter));
    }
    else if (firstCharacter.toUpper() == "A")
    {
        return true;
    }
    else
    {
        return false;
    }
}
</code>```

But hey, why not just give those functions two different names?  You still have the same amount of fine-grained control, don't you?

Because, as stated before, some hotels use numbers, some use letters, and some use a mixture of numbers and letters:

```<code>appearsToBeFirstFloor(roomNumber[someSortOfKey]);

// will treat ints and strings differently, without you having to write extra code
// every single spot where the function is being called
</code>```

This still isn't precisely the same exact code I would use in real life, but it should illustrate the point I'm making just fine.

But... Here's why it isn't more than syntactic sugar in contemporary languages.

Falco raised the point in the comments that current languages basically don't mix method overloading and dynamic function selection within the same step.  The way I previously understood certain languages to work was that you could overload appearsToBeFirstFloor in the example above, and then the language would determine at runtime which version of the function to be called, depending on the runtime value of the untyped variable.  This confusion partially stemmed from working with ECMA-sorts of languages, like ActionScript 3.0, in which you can easily randomize which function gets called on a certain line of code at runtime.

As you may know, ActionScript 3 doesn't support method overloading.  As for VB.NET, you can declare and set variables without assigning a type explicitly, but when you try to pass these variables as arguments to overloaded methods, it still doesn't want to read the runtime value to determine which method to call; it instead wants to find a method with arguments of type Object or no type or something else like that.  So the int vs. string example above wouldn't work in that language either.  C++ has similar issues, as when you use something like a void pointer or some other mechanism like that, it still requires you to manually disambiguate the type at compile time.

So as the first header says...

For contemporary languages, it's just syntactic sugar; in a completely language-agnostic sort of way, it's more than that.  Making method overloading more useful and relevant, like in the example above, may actually be a good feature to add to an existing language (as has been widely implicitly requested for AS3), or it could also serve as one among many different fundamental pillars for the creation of a new procedural / object-oriented language.

Body:
Is method overloading a type of polymorphism? To me it seems like simply the differentiation of methods with the same name and different parameters. So stuff(Thing t) and stuff(Thing t, int n) are entirely different methods as far as the compiler and runtime are concerned.

It creates the illusion, on the caller's side, that it's the same method that acts differently on different kinds of objects - polymorphism. But that's only an illusion, because actually stuff(Thing t) and stuff(Thing t, int n) are completely different methods.

Is method overloading anything more than syntactic sugar? Am I missing something?

A common definition for syntactic sugar, is that it is purely local. Meaning changing a piece of code to its 'sweetened' equivalent, or vice versa, involves local changes that don't affect the overall structure of the program. And I think method overloading precisely fits this criterion. Let's look at an example to demonstrate:

Consider a class:

```<code>class Reader {
    public String read(Book b){
        // .. translate the book to text
    }
    public String read(File b){
        // .. translate the file to text
    }
}
</code>```

Now consider another class that uses this class:

```<code>/* might not be the best example */
class FileProcessor {
    Reader reader = new Reader();
    public void process(File file){
        String text = reader.read(file);
        // .. do stuff with the text
    }
}
</code>```

Okay. Now let's see what needs to change if we replace the method overloading with regular methods:

The read methods in Reader change to readBook(Book) and readFile(file). Only a matter of changing their names.

The calling code in FileProcessor changes slightly: reader.read(file) changes to reader.readFile(file).

And that's it.

As you can see, the difference between using method overloading and not using it, are purely local. And that's why I think it qualifies as pure syntactic sugar.

I'd like to hear your objections if you have some, maybe I'm missing something.

Body:
For contemporary languages, it's just syntactic sugar; in a completely language-agnostic sort of way, it's more than that.

Previously this answer stated simply that it's more than syntactic sugar, but if you'll see in the comments, Falco raised the point that there was one piece of the puzzle that contemporary languages appear to all be missing; they don't mix method overloading with dynamic determination of which function to call in the same step.  This will be clarified later.

Here's why it should be more.

Consider a language that supports both method overloading and untyped variables.  You could have the following method prototypes:

bool someFunction(int arg);

bool someFunction(string arg);

In some languages, you would be probably be resigned to knowing at compile time which one of these would be called by a given line of code.  But in some languages, not all variables are typed (or they're all implicitly typed as Object or whatever), so imagine building a dictionary whose keys map to values of different types:

dict roomNumber; // some hotels use numbers, some use letters, and some use
                 // alphanumerical strings.  In some languages, built-in dictionary
                 // types automatically use untyped values for their keys to map to,
                 // so it makes more sense then to allow for both ints and strings in
                 // your code.

Now then, what if you wanted to apply someFunction to one of those room numbers?  You call this:

someFunction(roomNumber[someSortOfKey]);

Is someFunction(int) called, or is someFunction(string) called?  Here you see one example where these are not totally orthogonal methods, especially in higher-level languages.  The language has to figure out - during runtime - which one of these to call, so it still has to regard these as being at least somewhat the same method.

Why not simply use templates?  Why not simply use an untyped argument?

Flexibility and finer-grained control.  Sometimes using templates / untyped arguments are a better approach, but sometimes they're not.

You have to think about cases where, for instance, you might have two method signatures that each take an int and a string as arguments, but where the order is different in each signature.  You may very well have a good reason to do this, as each signature's implementation may do largely the same thing, but with just a slightly different twist; the logging could be different, for example.  Or even if they do the same exact thing, you may be able to automatically glean certain information from just the order in which the arguments were specified.  Technically you could just use pseudo-switch statements to determine the type of each of the arguments passed in, but that gets messy.

So is this next example bad programming practice?

bool stringIsTrue(int arg)
{
    if (arg.toString() == "0")
    {
        return false;
    }
    else
    {
        return true;
    }
}

bool stringIsTrue(Object arg)
{
    if (arg.toString() == "0")
    {
        return false;
    }
    else
    {
        return true;
    }
}

bool stringIsTrue(string arg)
{
    if (arg == "0")
    {
        return false;
    }
    else
    {
        return true;
    }
}

Yes, by and large.  In this particular example, it could keep somebody from trying to apply this to certain primitive types and getting back unexpected behavior (which could be a good thing); but let's just assume I abbreviated the code above, and that you, in fact, have overloads for all the primitive types, as well as for Objects.  Then this next bit of code really is more appropriate:

bool stringIsTrue(untyped arg)
{
    if (arg.toString() == "0")
    {
        return false;
    }
    else
    {
        return true;
    }
}

But what if you only needed to use this for ints and strings, and what if you want it to return true based on simpler or more complicated conditions accordingly?  Then you have a good reason to use overloading:

bool appearsToBeFirstFloor(int arg)
{
    if (arg.digitAt(0) == 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool appearsToBeFirstFloor(string arg)
{
    string firstCharacter = arg.characterAt(0);
    if (firstCharacter.isDigit())
    {
        return appearsToBeFirstFloor(int(firstCharacter));
    }
    else if (firstCharacter.toUpper() == "A")
    {
        return true;
    }
    else
    {
        return false;
    }
}

But hey, why not just give those functions two different names?  You still have the same amount of fine-grained control, don't you?

Because, as stated before, some hotels use numbers, some use letters, and some use a mixture of numbers and letters:

appearsToBeFirstFloor(roomNumber[someSortOfKey]);

// will treat ints and strings differently, without you having to write extra code
// every single spot where the function is being called

This still isn't precisely the same exact code I would use in real life, but it should illustrate the point I'm making just fine.

But... Here's why it isn't more than syntactic sugar in contemporary languages.

Falco raised the point in the comments that current languages basically don't mix method overloading and dynamic function selection within the same step.  The way I previously understood certain languages to work was that you could overload appearsToBeFirstFloor in the example above, and then the language would determine at runtime which version of the function to be called, depending on the runtime value of the untyped variable.  This confusion partially stemmed from working with ECMA-sorts of languages, like ActionScript 3.0, in which you can easily randomize which function gets called on a certain line of code at runtime.

As you may know, ActionScript 3 doesn't support method overloading.  As for VB.NET, you can declare and set variables without assigning a type explicitly, but when you try to pass these variables as arguments to overloaded methods, it still doesn't want to read the runtime value to determine which method to call; it instead wants to find a method with arguments of type Object or no type or something else like that.  So the int vs. string example above wouldn't work in that language either.  C++ has similar issues, as when you use something like a void pointer or some other mechanism like that, it still requires you to manually disambiguate the type at compile time.

So as the first header says...

For contemporary languages, it's just syntactic sugar; in a completely language-agnostic sort of way, it's more than that.  Making method overloading more useful and relevant, like in the example above, may actually be a good feature to add to an existing language (as has been widely implicitly requested for AS3), or it could also serve as one among many different fundamental pillars for the creation of a new procedural / object-oriented language.


答:
The term syntactic sugar typically refers to cases where the feature is defined by a substitution. The language doesn't define what a feature does, instead it defines that it is exactly equivalent to something else. So for example, for-each loops

```<code>for(Object alpha: alphas) {
}
</code>```

Becomes:

```<code>for(Iterator<Object> iter = alpha.iterator(); iter.hasNext()) {
   alpha = iter.next();
}
</code>```

Or take a function with variable arguments:

```<code>void foo(int... args);

foo(3, 4, 5);
</code>```

Which becomes:

```<code>void Foo(int[] args);

foo(new int[]{3, 4, 5});
</code>```

So there is a trivial substitution of syntax to implement the feature in terms of other features.

Let's look at method overloading.

```<code>void foo(int a);
void foo(double b);

foo(4.5);
</code>```

This can be rewritten as:

```<code>void foo_int(int a);
void foo_double(double b);

foo_double(4.5);
</code>```

But it is not equivalent to that. Within the model of Java, this is something different. foo(int a) doesn't implement a foo_int function to be created. Java doesn't implement method overloading by giving ambiguous functions funny names. To count as syntactic sugar, java would have to be pretending that you really wrote foo_int and foo_double functions but it doesn't.

Title: Is method overloading anything more than syntactic sugar?

Body:
The term syntactic sugar typically refers to cases where the feature is defined by a substitution. The language doesn't define what a feature does, instead it defines that it is exactly equivalent to something else. So for example, for-each loops

for(Object alpha: alphas) {
}

Becomes:

for(Iterator<Object> iter = alpha.iterator(); iter.hasNext()) {
   alpha = iter.next();
}

Or take a function with variable arguments:

void foo(int... args);

foo(3, 4, 5);

Which becomes:

void Foo(int[] args);

foo(new int[]{3, 4, 5});

So there is a trivial substitution of syntax to implement the feature in terms of other features.

Let's look at method overloading.

void foo(int a);
void foo(double b);

foo(4.5);

This can be rewritten as:

void foo_int(int a);
void foo_double(double b);

foo_double(4.5);

But it is not equivalent to that. Within the model of Java, this is something different. foo(int a) doesn't implement a foo_int function to be created. Java doesn't implement method overloading by giving ambiguous functions funny names. To count as syntactic sugar, java would have to be pretending that you really wrote foo_int and foo_double functions but it doesn't.


答:
You don't want to do a rewrite, as you point out there are problems with that.

What you should do is refactor as you are working on it -- whenever you do something with one of the ASP classic pages, turn it into .Net.  Basically you do the cleanup as you are working an issue.  This of course adds additional work to individual tasks, but it isn't a complete "change it all now" and when doing the update, do as little as possible to get it to be .Net, don't rewrite the whole page if you don't have to.

Eventually you will have everything as .Net and then you can start on whatever the next cleanup task is.

Title: How would you rewrite/refactor this ?


问:
How to measure algorithm accuracy?
I have a few optimization algorithms (for finding function minimum) and I'd like to check how good they are. Suppose I build test cases and compare the actual results to theoretical ones.
What measures should I use to estimate if the function minimum was actually found?

I thought about:

mean number of function evaluations (± standard deviation)

success rate (how often it actually finds minimum)

Are there any others that I have missed (let's say algorithm finishes 1e-4 from known solution. so is it success already or not?)

My main concern is not time complexity. It is algorithm accuracy in cases when exact solution might never be found (eg. multidimensional solution spaces). How do I calculate the rate of convergence?

答:
Figure out the complexity of the algorithm (its Big O)

Measure the time it takes to perform one iteration (if it is constant), or measure several iterations and amortize the time it takes.

That's it.

To get the running time for a particular n, just take the amount of time for 1 iteration, and plug it into your big O.  So, for example, if your Big O is O(n^2), and one iteration takes 100 milliseconds, then running time for n=6 will be 10 seconds.

Title: How to measure algorithm accuracy?


问:
Are there any scenarios where the 'Dispose Pattern' shouldn't be used on an Object which contains managed and un-managed resources?
MSDN says that to properly clean up an object which contains managed and un-managed resources, you need to implement the 'Dispose Pattern'.

Given that the class implements the IDisposable interface, and overrides the Object.Finalize() method (has a destructor), is there any reason why the compiler shouldn't automatically implement the pattern for you? For instance, compiling:

```<code>public class Example : IDisposable
{
    public void Dispose() {
        //Disposing managed objects implementation
    }

    ~Example() {
        //Disposing unmanaged objects implementation
    }
}
</code>```

As if it were typed as follows:

```<code>public class Example : IDisposable
{
    private bool previouslyDisposed = false;

    private void CleanResources(bool safeToCleanManagedResources)
    {
        if (!this.previouslyDisposed)
        {
            _finalize();
            if (safeToCleanManagedResources) {
                _dispose();
            }
            this.previouslyDisposed = true;
        }
    }

    public void Dispose() {
        CleanResources(true);
        GC.SuppressFinalize(this);
    }

    ~Example() {
        CleanResources(false);
    }

    private void _dispose() {
        //The contents of the Dispose() method are moved here.
    }

    private void _finalize() {
        //The contents of the ~Example() method are moved here.
    }
}
</code>```

From what I understand, this should always be safe and valid. I can not think of any scenarios where these rules would need to be broken. I know this isn't the most pressing feature the C# team is working on, but asides from that:

Are there any scenarios where an object containing managed and un-managed resources wouldn't want to follow the 'Dispose Pattern'?

I'm talking about the compiler generating a method which functions like CleanResources(), and moves the Dispose() and ~Example() methods to anonymous backing methods (similar to how auto-properties generate private backing fields). This would mean that any calls to Dispose() or ~Example() would automatically implement the 'Dispose Pattern'.

答:
Sure.

The core issue with this class is that you're not guaranteeing that the inheritor (which you note is a severe drawback) overrides the DisposeBlahResources. They should be abstract to enforce usage, and at that point I would question how that's any different/better/safer than implementing Dispose for its two cases.

Could resource management be done better? Sure. This though just seems like you're renaming the poop that's already there.


答:
Given that name-mangling works, doesn't it have to be nothing more than syntactic sugar?

It allows the caller to imagine he is calling the same function, when he isn't.  But he could know the real names of all his functions.  Only if it were possible to achieve delayed polymorphism by passing an untyped variable into a typed function and have its type established so that the call could go to the right version according to name would this be a true language feature.

Unfortunately, I have never seen a language do this.  When there is ambiguity, these compilers do not resolve it, they insist the writer resolve it for them.


问:
Multithreading: am I doing it wrong?
I'm working on an application that plays music.

During playback, often things need to happen on separate threads because they need to happen simultaneously. For example, the notes of a chord need to be heard together, so each one is assigned its own thread to be played in. (Edit to clarify: calling note.play() freezes the thread until the note is done playing, and this is why I need three separate threads to have three notes heard at the same time.)

This kind of behavior creates many threads during playback of a piece of music.

For example, consider a piece of music with a short melody and short accompanying chord progression. The entire melody can be played on a single thread, but the progression needs three threads to play, since each of its chords contains three notes.

So the pseudo-code for playing a progression looks like this:

```<code>void playProgression(Progression prog){
    for(Chord chord : prog)
        for(Note note : chord)
            runOnNewThread( func(){ note.play(); } );
}
</code>```

So assuming the progression has 4 chords, and we play it twice, than we are opening 3 notes * 4 chords * 2 times = 24 threads. And this is just for playing it once.

Actually, it works fine in practice. I don't notice any noticeable latency, or bugs resulting from this.

But I wanted to ask if this is correct practice, or if I'm doing something fundamentally wrong. Is it reasonable to create so many threads each time the user pushes a button? If not, how can I do it differently?

答:
One assumption you are making might not be valid: you require (among other things) that your threads execute simultaneously. Might work for 3, but at some point the system is going to need to prioritize which threads to run first, and which one wait.

Your implementation will ultimately depend on your API, but most modern APIs will let you tell in advance what you want to play and take care of the timing and queuing themselves. If you were to code such an API yourself, ignoring any existing system API (why would you?!), an event queue mixing your notes and playing them from a single thread looks like a better approach than a thread per note model.

Title: Multithreading: am I doing it wrong?


问:
What is the "best practice" for converting an external API's data structure?
When an API's data structure does not fit well with an angular app, I am trying to determine the best-practice in my specific design and implementation for converting it to a usable structure.

I was doing this on an 'as needed' basis within the app controllers/directives, which quickly lead to spaghetti code.

Now I'm thinking this belongs in the service/factory which interacts with the API. Are there drawbacks or benefits to the Factory pattern over a more service oriented architecture when it comes to handling data structure conversion?

答:
Yours is a common-enough problem that the solution is has a name:  an anticorruption layer.  This is a layer that translates between an external API with an undesirable structure and the desired internal structure of your app.  The idea is to limit the "corruption" of the undesired structure to as small a part of your code as possible.  </p>

<p>The external API doesn't have to be bad in an absolute sense, just not ideally suited for your particular application.  If you can think of a better API, by all means go for it.  The earlier you do, the cleaner your code will be.  </p>

Body:
Yours is a common-enough problem that the solution is has a name:  an anticorruption layer.  This is a layer that translates between an external API with an undesirable structure and the desired internal structure of your app.  The idea is to limit the "corruption" of the undesired structure to as small a part of your code as possible.

The external API doesn't have to be bad in an absolute sense, just not ideally suited for your particular application.  If you can think of a better API, by all means go for it.  The earlier you do, the cleaner your code will be.