问:
In Unit Testing, why would I create a Repository twice?
The other day I was reading a little about Unit Testing and I saw some examples where people create a repository interface (i.e. IExampleRepository) and then create the real repository (public class ExampleRepository : IExampleRepository) and a repository to be used for unit testing (FakeExampleRepository : IExampleRepository).

In the IExampleRepository they were implementing the same methods as in the ExampleRepository, however with different Linq queries.

What is exactly the objective here? I thought one part of unit testing your code is make sure that a method works correctly? But when I use two totally different queries, one for 'real' and one in the test, how much sense does the test make?

答:
One of the aims of unit testing is to test just one thing at a time i.e. a single class and method. If the repository itself is not under test then you would normally mock this out in some way so as just to test the logic in your method/class.

That said you do also need to test with a 'real' repository*, but this would normally be done in an integration/system test

*obviously real as in repo set up for test, hopefully not e.g. the production DB.


问:
What language is most similar to JavaScript?
I am looking for a language that

has First class functions

is dynamic

has prototypical inheritance

does not have too many features (i.e is easy to get into and write first program).

I have been programming in JavaScript for quite some time and have gotten quite fond of it. Are there any other languages that have similar design?

Edit (just some clarification)

I know a bit of c#.After doing some programming in node js I really started to appreciate javascript and ease of its use.However outside of web development javascript falls short.Sure there are libraries like app.js that allows desktop programming but that's about it.Which is why I was looking for a more general purpose sripting language that has the above mentioned features.I listed those features to make the question more in tune with stack exchange policies other wise it would probably have been closed.

答:
The language that is most similar to JavaScript is JavaScript, by definition.

Learning something new by learning something that is exactly the same as what you already know is an oxymoron. If you want to learn something new, you have to, well learn something new. Something that is as different as possible, not as similar as possible to what you already know.

That being said, if you want to learn something about JavaScript, I suggest you learn some of the languages that influenced its design the most: Scheme (lexical scoping, nested, first-class and higher-order procedures, procedures/closures instead of objects as the unit of encapsulation, a single ubiquitous powerful data structure), NewtonScript (prototype-based OO) and Act1 (differential inheritance) as well as Self (the prototypical prototype-based OO language which inspired both NewtonScript and Act1).

ContentLicense: CC BY-SA 3.0


答: Since the other answers cite the languages that inspired Javascript, I'll cite the languages inspired by Javascript. TypeScript and CoffeeScript are very close to Javascript with a few improvements; as jk. pointed out in his comment, there is also ActionScript, which is a bit stricter than Javascript.</p>

Title: What language is most similar to JavaScript?

Body:
I am looking for a language that

has First class functions

is dynamic

has prototypical inheritance

does not have too many features (i.e is easy to get into and write first program).

I have been programming in JavaScript for quite some time and have gotten quite fond of it. Are there any other languages that have similar design?

Edit (just some clarification)

I know a bit of c#.After doing some programming in node js I really started to appreciate javascript and ease of its use.However outside of web development javascript falls short.Sure there are libraries like app.js that allows desktop programming but that's about it.Which is why I was looking for a more general purpose sripting language that has the above mentioned features.I listed those features to make the question more in tune with stack exchange policies other wise it would probably have been closed.


问:
Applications of heapsort
Heapsort is a sorting algorithm that has a time complexity of O(nlogn), and performs sorting using O(1) space complexity. However, I know that because it's unstable, it doesn't find many applications (f.e. compared to other sorting algorithms).

I know it's used for interval scheduling.

What are some practical applications of heapsort?

EDIT: As @AProgrammer pointed out, quick sort isn't stable either.

答: Heapsort is great when you need to know just the "smallest" (or "largest") of a collection of items, without the overhead of keeping the remaining items in sorted order. For example, a PriorityQueue.</p>

Title: Applications of heapsort


答: Quicksort isn't stable.  Quicksort has a lower constant factor than heap sort, but it's worst case complexity is O(N^2), so some variant switch back to heapsort in order to avoid that behavior (for instance introsort, but I'm pretty sure I've seen the idea applied in the early 90's).</p>

Title: Applications of heapsort

Body:
Heapsort is a sorting algorithm that has a time complexity of O(nlogn), and performs sorting using O(1) space complexity. However, I know that because it's unstable, it doesn't find many applications (f.e. compared to other sorting algorithms).

I know it's used for interval scheduling.

What are some practical applications of heapsort?

EDIT: As @AProgrammer pointed out, quick sort isn't stable either.


问:
Where to put Administrative Features
Right now we have our administrative features located at www.mysite.com/admin.  It is locked down by UN/PW but it still seems like an insecure place to have it.  Is there a better place to put it?  Perhaps at another domain?</p>

答:
Ensure that the area of a website is secure by having:

long passwords,

or a good security audit with the maximum number of failed login attempts before lock,

or a two-step verification,

or anything else which actually increases the security level,

no by:

moving the area somewhere else,

never documenting it, hoping that nobody will find it,

or any other "it's hidden, so it's secure" technique.

Title: Where to put Administrative Features


问:
User stories are too high level and conceptual, management expects developers to fill in the blanks
I am employed in a very brilliant company with a true intention of doing XP. Communication is good and management is open to constructive discussion but due to pressing time constraints, some certain things are considered too RUP to be discussed.

At the moment I'm a little troubled with the volume of change that becomes necessary while implementing the stories. I believe many of these discoveries (which take time and effort of course) are the responsibilities of the story writers (customers, end users and product owners) and not the developers. To put is short, user-stories are too conceptual and just convey the underlying intention but lack enough details (specially pre-conditions and post-conditions, relevance to other stories, dependencies and alike). The developer is expected to fill in the blanks at his own discretion by the virtue of XP developers being designers and analysts at the same time. The problem is many of these blanks are discovered after some wrong assumptions have made made their way into evaluation time and code since noticing added complexities emerge than initially anticipated. Even then finding the right thing to fill in takes time which is - to various degrees - considered as deviation from the initial estimations.

I'm looking for a constructive way of conveying these implications to the management in a way that would not pose me as someone who is trying to unnecessarily complicate things. I'm new and as yet I have not established much credibility.

You insights are most welcome.

Closely related and somehow gives an answer : How much detail about a user story can a developer expect?

答:
Though a developer could write a story/ detailed requirements i have not seen many who are good at it. we are good at pointing out issues, suggesting better flows but as an input to already well written case.

Have worked on new and existing products and with both had cases where the requirements where just 5- lines and we were expected to fill the blanks and make an 'understanding' or more elaborate document.

Projects moved much better then we had our own professional services person who helped with this (or in one case a VP who jumped in as there was no one else available). Either way its a waste of time to develop (unless no feedback is coming back and the deadline has not changed). so my suggestion : ask for more details, provide more, ask for time bound feedback to your assumptions and documentation and state that its a risk that there will be rework and delays if you do not get this information by x date.


问:
Exercise for implementing design patterns
I have started to study design patterns using some course in pluralsight and the gang of four book.
While I feel I understand what I have studied so far, I think it would be very helpful if I would have some way of practicing them.
I know that I will get the chance in time but I feel I would get a better understanding of each design pattern if I would do some exercises for each of them.

What I would like is if someone knows any websites or documents that provide exercises for implementing design patterns. I am particularly interested in ones where the author provides some source code and the problem that has to be solved and I have to implement a design pattern to solve it.

I am not interested in examples and theory; I already got that covered from what I am reading.

答:
The best exercise I can think is to to write a simple 2d game engine and something like tetris or pong on top of that.

Remember though, that it is important to know when not to use a patterns as well. Engineering just for the sake of it is not very cost effective.

Good luck!

(Incidentally, this interesting conversation is going on as well When should I use, and not use, design patterns)


问:
Round Table - Minimum Cost Algorithm
Problem Link - http://www.iarcs.org.in/zco2013/index.php/problems/ROUNDTABLE

It's dinner time in Castle Camelot, and the fearsome Knights of the Round Table are clamouring for dessert. You, the chef, are in a soup. There are N knights, including King Arthur, each with a different preference for dessert, but you cannot afford to make desserts for all of them.

You are given the cost of manufacturing each Knight's preferred dessert-since it is a round table, the list starts with the cost of King Arthur's dessert, and goes counter-clockwise.

You decide to pick the cheapest desserts to make, such that for every pair of adjacent Knights, at least one gets his dessert. This will ensure that the Knights do not protest.
  What is the minimum cost of tonight's dinner, given this condition?

I used the Dynamic Programming approach, considering the smallest of i-1 & i-2, & came up with the following code -

```<code>#include<cstdio>
#include<algorithm>
using namespace std;

int main() {
int n,i,j,c,f;
scanf("%d",&n);
int k[n],m[n][2];
for(i=0;i<n;++i) scanf("%d",&k[i]);
m[0][0]=k[0]; m[0][1]=0;
m[1][0]=k[1]; m[1][1]=1;
for(i=2;i<n;++i) {
                 c=1000;
                 for(j=i-2;j<i;++j) {
                                    if(m[j][0]<c) { c=m[j][0]; f=m[j][1];}
                 }
                 m[i][0]=c+k[i]; m[i][1]=f;
}
if(m[n-2][0]<m[n-1][0] && m[n-2][1]==0) printf("%d\n",m[n-2][0]);
else printf("%d\n",m[n-1][0]);
}
</code>```

I used the second dimension of the m array to store from which knight the given sequence started (1st or 2nd). I had to do this because of the case when m[n-2]<m[n-1] but the sequence started from knight 2, since that would create two adjacent knights without dessert. The problem arises because of the table's round shape.

Now an anomaly arises when I consider the case - 2 1 1 2 1 2. The program gives an answer 5 when the answer should be 4, by picking the 1st, 3rd & 5th knight. At this point, I started to doubt my initial algorithm (approach) itself!

Where did I go wrong?

答:
I dont understand the usage of c and f in your code. It would be great if you could explain a bit on that. Also i dont find the need to iterate i-2 and i-1 for every i. ( I am not able to comment on posts yet) Now i ll give you my algorithm to solve this problem similar to your approach.

Maintain a dp[n][2]. dp[i][0] is for cases starting from 0 and dp[i][1] for the ones starting from 1. Now for each i, you could just get the dp[i][0]th element as dp[i-2][0] + k[i] and dp[i][1] as dp[i-2][1] + k[i]. Trick is to do this only when appropriate. Here for cases where i%2 is equals 0, you can assume that those are the ones that will be covered when we start from 0 and rest of elements goes for the other case. Corner case will be to avoid the last element for the case of starting from 0 as the table is circular.

Here is my working code. It works for the case you just mentioned. Am not a member of the site so am not able to test for other cases though.

```<code>#include<vector>
#include<iostream>
#include<cmath>
using namespace std;

int main() {
int n, i, j, temp;
vector<int> k;
cin>>n;
for(i=0;i<n;i++) {
cin>>temp;
k.push_back(temp);
}
int dp[n][2];
/* 0 will contain the case of starting from 0
* 1 will contain the case of starting from 1 */
dp[0][0] = k[0];
dp[0][1] = 0;
dp[1][0] = k[0]; // assign the previous value itself
dp[1][1] = k[1];

for(i=2;i<n;i++) {
    if(i%2 == 0) {
        // case of strting from 0 .

        dp[i][0] = dp[i-2][0] + k[i];
        dp[i][1] = dp[i-1][1];
    }
    else{ // case of starting from 1
        dp[i][0] = dp[i-1][0];
        dp[i][1] = dp[i-2][1] + k[i];
    }
}
cout<<min(dp[n-1][0], dp[n-1][1]);
 }
</code>```

Title: Round Table - Minimum Cost Algorithm

Body:
I dont understand the usage of c and f in your code. It would be great if you could explain a bit on that. Also i dont find the need to iterate i-2 and i-1 for every i. ( I am not able to comment on posts yet) Now i ll give you my algorithm to solve this problem similar to your approach.

Maintain a dp[n][2]. dp[i][0] is for cases starting from 0 and dp[i][1] for the ones starting from 1. Now for each i, you could just get the dp[i][0]th element as dp[i-2][0] + k[i] and dp[i][1] as dp[i-2][1] + k[i]. Trick is to do this only when appropriate. Here for cases where i%2 is equals 0, you can assume that those are the ones that will be covered when we start from 0 and rest of elements goes for the other case. Corner case will be to avoid the last element for the case of starting from 0 as the table is circular.

Here is my working code. It works for the case you just mentioned. Am not a member of the site so am not able to test for other cases though.

#include<vector>
#include<iostream>
#include<cmath>
using namespace std;

int main() {
int n, i, j, temp;
vector<int> k;
cin>>n;
for(i=0;i<n;i++) {
cin>>temp;
k.push_back(temp);
}
int dp[n][2];
/* 0 will contain the case of starting from 0
* 1 will contain the case of starting from 1 */
dp[0][0] = k[0];
dp[0][1] = 0;
dp[1][0] = k[0]; // assign the previous value itself
dp[1][1] = k[1];

for(i=2;i<n;i++) {
    if(i%2 == 0) {
        // case of strting from 0 .

        dp[i][0] = dp[i-2][0] + k[i];
        dp[i][1] = dp[i-1][1];
    }
    else{ // case of starting from 1
        dp[i][0] = dp[i-1][0];
        dp[i][1] = dp[i-2][1] + k[i];
    }
}
cout<<min(dp[n-1][0], dp[n-1][1]);
 }


答:
There are three reasons why you might want to provide mock instance to unit test:

You want to limit the scope of the test, so that the test is not affected by bugs in the depndee, possibly because it is not finished yet or is not stable or you don't want bugs in somebody else's affect your tests.

The dependee is complicated to set up. For example data access layer is often mocked out, because the real one requires setting up a test database. You still need to test the real data access layer, but you can limit the costly setup when debugging other things.

To test that the dependent class properly reacts to various kinds of errors, you provide a mock version that returns all sorts of wrong answers. Because many failure modes are rather difficult to reproduce, but should still be tested.

Title: In Unit Testing, why would I create a Repository twice?

Body:
The other day I was reading a little about Unit Testing and I saw some examples where people create a repository interface (i.e. IExampleRepository) and then create the real repository (public class ExampleRepository : IExampleRepository) and a repository to be used for unit testing (FakeExampleRepository : IExampleRepository).

In the IExampleRepository they were implementing the same methods as in the ExampleRepository, however with different Linq queries.

What is exactly the objective here? I thought one part of unit testing your code is make sure that a method works correctly? But when I use two totally different queries, one for 'real' and one in the test, how much sense does the test make?


问:
Self Debuggable systems
For example, in case of GDB it is possible to debug itself by itself. I have not been able to find any JVM that can debug itself or even allows debugging on source level.
I was just wondering if someone can suggest on how the systems are made self debuggable. If you have references, please paste it here. I wanted to read on it in detail.</p>

答:
This has nothing to do with GDB being able to debug itself. GDB is a debugger for C. It can debug any C program. GDB also happens to be written written in C, therefore you can debug GDB with GDB.

The JVM Debug Interface is an interface for debugging Java. You can debug any Java program. Most JVMs aren't written in Java, and even if they are written in Java, they don't run inside of themselves.

There is however a JVM, which is written in Java and runs inside of itself: the Maxine Research JVM, and it can debug itself.

Title: Self Debuggable systems


问:
Python mutable default argument: Why?
I know that default arguments are created at the function initialisation time and not every time the function is called.  See the following code:

```<code>def ook (item, lst=[]):
    lst.append(item)
    print 'ook', lst

def eek (item, lst=None):
    if lst is None: lst = []
    lst.append(item)
    print 'eek', lst

max = 3
for x in xrange(max):
    ook(x)

for x in xrange(max):
    eek(x)
</code>```

What I do not get is why this was implemented this way.  What benefits does this behaviour offers over an initialisation at each call time?

答:
I think the real answer is: Python was written as a procedural language and only adopted functional aspects after-the-fact. What you're looking for is for parameter defaulting to be done as a closure, and closures in Python are really only half-baked. For evidence of this try:

```<code>a = []
for i in range(3):
    a.append(lambda: i)
print [ f() for f in a ]
</code>```

which gives [2, 2, 2] where you would expect a true closure to produce [0, 1, 2].

There are quite a lot of things that I'd like if Python had the ability to wrap parameter defaulting in closures. For example:

```<code>def foo(a, b=a.b):
    ...
</code>```

Would be extremely handy, but "a" isn't in scope at function definition time, so you can't do that and instead have to do the clunky:

```<code>def foo(a, b=None):
    if b is None:
        b = a.b
</code>```

Which is almost the same thing... almost.

Title: Python mutable default argument: Why?

Body:
I think the real answer is: Python was written as a procedural language and only adopted functional aspects after-the-fact. What you're looking for is for parameter defaulting to be done as a closure, and closures in Python are really only half-baked. For evidence of this try:

a = []
for i in range(3):
    a.append(lambda: i)
print [ f() for f in a ]

which gives [2, 2, 2] where you would expect a true closure to produce [0, 1, 2].

There are quite a lot of things that I'd like if Python had the ability to wrap parameter defaulting in closures. For example:

def foo(a, b=a.b):
    ...

Would be extremely handy, but "a" isn't in scope at function definition time, so you can't do that and instead have to do the clunky:

def foo(a, b=None):
    if b is None:
        b = a.b

Which is almost the same thing... almost.


问:
Term for accidental features
Despite the best intentions of programmers; oftentimes, in the real world, software systems contain "accidental features" that are accepted as part of a working system.

For example, there may be some process that runs out of memory and crashes before the process exceeds its quota for calls to an external system (which will cost the company money). Though the software team acknowledges that this is an issue, there is simply not enough perceived business value to fix the problem.

Advantageous accidental behavior like this is often lost in any type of significant technical migration.

My question: Is there a term for this type of "feature" that can serve to differentiate it from a run-of-the-mill bug?

答:
The Pragmatic Programmer calls these Accidents of Implementation. This term hopefully ensures that the situation will not be mistaken for anything but what it actually is.

"Accidents of implementation are things that happen simply because that’s the way the code is currently written. You end up relying on undocumented error or boundary conditions."

(I guess I should have consulted the developer canon before I asked this question)

Title: Term for accidental features


问:
What's the difference between an API and an SDK?
I was looking through various APIs and SDKs, when I realized that I couldn't really tell the difference between something called an API and something called an SDK.

Both of them are, conceptually, a way for your program to interface with and control the resources provided by another piece of software, whether that other software is a web service, an end-user app, an OS service or daemon, or a kernel device driver.

So, what is the semantic difference between an SDK and an API?

答: Here is a simple analogy... APIs are like words that you can use, and SDKs are like sentences that have been put together for you.</p>


问:
Git: rebasing and keeping current to a remote branch - accepted practices?
When developing on a long-running branch (here defined as living longer than one release), what's the most accepted practice(s) to keeping the branch current with its origin while keeping the history relatively clean prior to merging back?

As an example, consider the origin branch (m) and a (topic) branch (t) with the following history graph:

```<code> A[1]--------F[2]--H[3]--J[4] (m)
  \           \     \
   B----C-D-E--G-----I-----K (t)

[1] - Release 1.0
[2] - Release 2.0
[3] - Hotfix 2.1
[4] - Hotfix 2.2
</code>```

In branch t I've been merging from master (m) periodically to keep my topic branch up to date, but there are many small commits between B and K that I'd like to squash with a rebase.

I'm concerned if I squash those commits that I will change the hash of the merged commits from master and create problems for others when I merge my branch back into master for Release 3.0.

If I rebase -i the topic branch, I expect the graph would look like this:

```<code> A---------F---H---J (m)
  \
   B-----------------K (t)
</code>```

where K contains all the squashed commits between B and K including the merged changes from master (F and H).  If I then merge t back to master m I'm wondering if there likely be conflicts from F and H.  The graph should look like this then:

```<code> A---------F---H---J---L (m)
  \                   /
   B-----------------K   (t)
</code>```

t can now be deleted because it's no longer used.  I'd like to keep the history of where t was branched from, so rebasing B onto J would lose that information and probably solve my problem but the graph would look like:

```<code> A---------F---H---J---K' (m)
</code>```

Is this a valid concern?  What is the commonly-accepted practice to keep one's feature branch current with its origin while keeping a relatively clean history?

Note: the example here is simplified, there are actually hundreds of commits from the branch point to HEAD on the topic branch - over 300 - many of which could be squashed out of existence

答:
The best practice is to keep an accurate history and just do a straight merge.  That way, if you introduce a bug at C, someone investigating it can say, "Oh, that makes sense because it was done before we put feature F into origin, but not merged until today."  It also has the benefit of not changing any hashes.  There are flags you can use to hide the topic branch history when it doesn't interest you.

If you must squash your commits, for example if origin is controlled by another organization, then it depends on how many people use the topic branch, and how many of them need to do merges from your origin.  You're not going to be able to do it without inconveniencing someone.  Some teams just work with the original history, and one person deals with all the merges.  Some teams start over with a new topic branch every time it gets merged into origin.  Some teams continually rebase their topic branch to origin, and just make it common practice to always do a force pull.  You'll have to figure out what works for your team.


问:
How do you store "fuzzy dates" into a database?
This is a problem I've run into a few times. Imagine you have a record that you want to store into a database table. This table has a DateTime column called "date_created". This one particular record was created a long time ago, and you're not really sure about the exact date, but you know the year and month. Other records you know just the year. Other records you know the day, month and year.

You can't use a DateTime field, because "May 1978" isn't a valid date. If you split it up into multiple columns, you lose the ability to query. Has anyone else run into this, if so how did you handle it?

To clarify the system I'm building, it is a system that tracks archives. Some content was produced a long time ago, and all that we know is "May 1978". I could store it as May 1 1978, but only with some way to denote that this date is only accurate to the month. That way some years later when I'm retrieving that archive, I'm not confused when the dates don't match up.

For my purposes, it is important to differentiate "unknown day in May 1978" with "May 1st, 1978". Also, I would not want to store the unknowns as 0, like "May 0, 1978" because most database systems will reject that as an invalid date value.

答:
Generally, I still store them as dates to general query buisness is still possible even if slightly less accurate.

If it is important to know the accuracy I have in the past either stored an accuracy "window" either as a +/- decimal or as a lookup (day, month, year, etc).  In other cases instead of the window I just store the original date value as a string and convert what I can to a datetime, possibly 1978-05-01 00:00:00 and "May 1978" for your given example.


答:
You have a couple of options depending on branch t.

If branch t is also a remote branch with multiple developers working on it.  I think that best way is doing the merges that you are suggesting.  Rebasing and doing a forced push seems like a risky way to do things.  I try to avoid changing a pushed history.

If the branch is local, then you can do a git rebase origin/master this will bring in all the changes that have occurred in master and then append your commits after the last commit.  The advantage of this is you can easily find all your commits for squashing.  This also keeps all the changes that are related to the project in your commits rather than doing merge conflict resolution in a merge commit and keeps the branch clean when you merge it into master.

Title: Git: rebasing and keeping current to a remote branch - accepted practices?

Body:
When developing on a long-running branch (here defined as living longer than one release), what's the most accepted practice(s) to keeping the branch current with its origin while keeping the history relatively clean prior to merging back?

As an example, consider the origin branch (m) and a (topic) branch (t) with the following history graph:

```<code> A[1]--------F[2]--H[3]--J[4] (m)
  \           \     \
   B----C-D-E--G-----I-----K (t)

[1] - Release 1.0
[2] - Release 2.0
[3] - Hotfix 2.1
[4] - Hotfix 2.2
</code>```

In branch t I've been merging from master (m) periodically to keep my topic branch up to date, but there are many small commits between B and K that I'd like to squash with a rebase.

I'm concerned if I squash those commits that I will change the hash of the merged commits from master and create problems for others when I merge my branch back into master for Release 3.0.

If I rebase -i the topic branch, I expect the graph would look like this:

```<code> A---------F---H---J (m)
  \
   B-----------------K (t)
</code>```

where K contains all the squashed commits between B and K including the merged changes from master (F and H).  If I then merge t back to master m I'm wondering if there likely be conflicts from F and H.  The graph should look like this then:

```<code> A---------F---H---J---L (m)
  \                   /
   B-----------------K   (t)
</code>```

t can now be deleted because it's no longer used.  I'd like to keep the history of where t was branched from, so rebasing B onto J would lose that information and probably solve my problem but the graph would look like:

```<code> A---------F---H---J---K' (m)
</code>```

Is this a valid concern?  What is the commonly-accepted practice to keep one's feature branch current with its origin while keeping a relatively clean history?

Note: the example here is simplified, there are actually hundreds of commits from the branch point to HEAD on the topic branch - over 300 - many of which could be squashed out of existence


答:
This is really more of a requirements definition than a technical problem -- what you need to focus on is "how can we define the dates in the past" and the technical solution will flow.

The times I've had to approach something like this we've typically:

Define how to map things -- like MichaelT suggests, decide that anything that is defined as Month/Day becomes defined as midnight on the 1st of said month. This is typically good enough for most purposes -- if the exact date was that important you probably would have a record of it 35 years later, right?

Figure out if you need to track this -- IE, do records with slightly made up create dates need a flag saying so? Or is that just a user training issue so folks know and can act accordingly.

Sometimes one needs to do something like make the dates fuzzy -- for example, that may one date might need to respond to a query for anything in May 1978. This is doable -- just make your create_date 2 fields, old records get a 30 days spread as appropriate, new ones get 2 identical values.

Title: How do you store "fuzzy dates" into a database?

Body:
This is a problem I've run into a few times. Imagine you have a record that you want to store into a database table. This table has a DateTime column called "date_created". This one particular record was created a long time ago, and you're not really sure about the exact date, but you know the year and month. Other records you know just the year. Other records you know the day, month and year.

You can't use a DateTime field, because "May 1978" isn't a valid date. If you split it up into multiple columns, you lose the ability to query. Has anyone else run into this, if so how did you handle it?

To clarify the system I'm building, it is a system that tracks archives. Some content was produced a long time ago, and all that we know is "May 1978". I could store it as May 1 1978, but only with some way to denote that this date is only accurate to the month. That way some years later when I'm retrieving that archive, I'm not confused when the dates don't match up.

For my purposes, it is important to differentiate "unknown day in May 1978" with "May 1st, 1978". Also, I would not want to store the unknowns as 0, like "May 0, 1978" because most database systems will reject that as an invalid date value.

ContentLicense: CC BY-SA 4.0


答:
If you don't need to use this kind of data as regular date-time information, any simple  string format would do.

But if you need to keep all the functionality, there are two workarounds I can think of, both requiring additional information stored in the database:

Create min date and max date fields, which have different values for "incomplete" data, but will coincide for accurate dates.

Create types for each kind of inaccurate date (none _ 0, date_missing _ 1, month_missing _ 2, year_missing_4, etc _ so you can combine them). Add a type field to the records and keep which information is missing.


问:
Do I need to understand algorithms and data structures to be called a programmer?
It has been six years since I have been coding. Coding into all kinds of things like ActionScript, JavaScript, Java, PHP, Ajax, XML HTML, ASP, etc. I have used arrays, maps, linked lists, sets, etc and wherever I worked people like me. But whenever I am interviewed it is very likely that people ask me question on hashes, trees, stacks, and queues. Some questions are on juggling some sorting algorithms.  I don't know if I should really know them or should I stop calling myself a programmer. There is something in me which also tells me even if people who are asking all these questions select me, they will never be making me work of these things. Am I really required to know all this?</p>

答:
"If you want to be a good programmer you just program every day for two
  years. If you want to be a world class programmer you can program
  every day for ten years, or you could program every day for two years
  and take an algorithms class."

-Charles E. Leiserson

Good advice from Analysis of Algorithms By Charles E. Leiserson - MIT


问:
Immutable vs mutable object as returned parameter for class method
There is a class method (static method) in which I create and build some object. And for filling that object, I create it as mutable object.

My mutable object is a subclass of immutable object. So i can return it as super Class type.

That object is just created in class method and returns. It is not a part of object or class state.

Should I return mutable object or it immutable copy? (what solution will be correct in concept of encapsulation)

For example there is some pseudo code:

```<code>// MutableArray is subclass of Array
MutableArray : Array;

// Another class

static (Array *)getEngineers {
   Array *employees = Array.arrayWithContentsOfResource("employees.txt");
   MutableArray *engineers = new MutableArray;
   for (Employee *employee in employees) {
      if (employee.profession == "Engineer") {
         engineers.addObject(employee)
      }
   }
   return engineers; // should I return Array.arrayWithArray(engineers) instead of engineers ?
}
</code>```

Update:

I mean, if I have a mutable array that is a member of the object, then it makes sense to return immutable copy in getter method (to provide access control and encapsulation). But if I have method like in example. I need only immutable object for using. But creating immutable copy of array is additional operations. Does it make sense to make it immutable?

答:
Encapsulation doesn't have anything to do with immutability.  Well, maybe it does a little.  A more accurate term would be "access control."  An immutable object restricts access control because you cannot write it; you can only read it.

You make objects immutable when you are using them in a multi-threaded context, and you want to make your code more... "deterministic."  There is no right or wrong about this, no "correct" or "incorrect;" if you're not using multiple threads, you probably don't need to worry about immutability at all.

Make objects immutable when you want to guarantee "safe" access to the object via another thread.  An object is "safe" to access in a multi-threaded context when you can guarantee that another thread won't be writing an object while you're trying to read it.  You can do this with immutability, but you can also do it with locks, mutexes, semaphores and other concurrency mechanisms.


答:
The simplest way to denote if the date is accurate is to create an accuracy field  INT(1) with default NULL

If the date is accurate store date-time in "date_created" & leave accuracy NULL

If date is only accurate to month  store date-time as 1st of Month with accuracy value 1

If date is only accurate for year  store date-time 1st of January with accuracy  value 2

You can use different numbers to hold different values such as first quarter etc


问:
In what programming language did "let" first appear?
I was wondering about the origins of the "let" used in Lisp, Clojure, and Haskell.  Does anyone know which language it appeared in first?</p>

答:
I'd like to add a theoretical point of view: In classical lambda calculi, let is just syntactic sugar. For example

```<code>let x = N in M
</code>```

can be rewritten simply as

```<code>(λx.M)N
</code>```

So its first appearance in early (functional) languages isn't that interesting.

However, it become very important with the invention of Hindley-Milner type system and its type inference algorithm. In this type system let is indispensable, because it's polymorphic (unlike λ-abstraction in HM). For example, consider this simple expression:

```<code>let id = λx . x in id id
</code>```

Here id is polymorphic, it has type ∀α.α → α, and therefore id id type-checks - it's type is id id : τ → τ for arbitrary τ. (For the first id we assign τ → τ to α and for the second id we assign τ for α.)

However, we can't rewrite it using λ abstraction and application. Expression

```<code>(λid . id id)(λx.x)
</code>```

doesn't type-check, because within the first λ abstraction id must be assigned a monomorphic type id : σ for some σ, and there is no σ such that we could apply id : σ to id : σ.

You can try this yourself in Haskell. While let id = \x -> x in id id :: t -> t type-checks, typing (\id -> id id)(\x -> x) fails with

Occurs check: cannot construct the infinite type: t0 = t0 -> t0

    In the first argument of id, namely id

    In the expression: id id

    In the expression: \id -> id id

Body:
I'd like to add a theoretical point of view: In classical lambda calculi, let is just syntactic sugar. For example

let x = N in M

can be rewritten simply as

(λx.M)N

So its first appearance in early (functional) languages isn't that interesting.

However, it become very important with the invention of Hindley-Milner type system and its type inference algorithm. In this type system let is indispensable, because it's polymorphic (unlike λ-abstraction in HM). For example, consider this simple expression:

let id = λx . x in id id

Here id is polymorphic, it has type ∀α.α → α, and therefore id id type-checks - it's type is id id : τ → τ for arbitrary τ. (For the first id we assign τ → τ to α and for the second id we assign τ for α.)

However, we can't rewrite it using λ abstraction and application. Expression

(λid . id id)(λx.x)

doesn't type-check, because within the first λ abstraction id must be assigned a monomorphic type id : σ for some σ, and there is no σ such that we could apply id : σ to id : σ.

You can try this yourself in Haskell. While let id = \x -> x in id id :: t -> t type-checks, typing (\id -> id id)(\x -> x) fails with

Occurs check: cannot construct the infinite type: t0 = t0 -> t0

    In the first argument of id, namely id

    In the expression: id id

    In the expression: \id -> id id


答:
Store all dates in normal DATE field in the database and have additional accuracy field how accurate DATE field actually is.

```<code>date_created DATE,
date_created_accuracy INTEGER,
</code>```

date_created_accuracy: 1 = exact date, 2 = month, 3 = year.

If your date is fuzzy (e.g May 1980) store it at start of period (e.g. May 1st. 1980). Or if your date is accurate to year (e.g. 1980) store it as January 1st. 1980 with corresponding accuracy value.

This way can easily query in a somewhat natural way and still have notion how accurate dates are. For example this allows you to query dates between Jan 1st 1980 and Feb 28th 1981, and get fuzzy dates 1980 and May 1980.

Body:
Store all dates in normal DATE field in the database and have additional accuracy field how accurate DATE field actually is.

date_created DATE,
date_created_accuracy INTEGER,

date_created_accuracy: 1 = exact date, 2 = month, 3 = year.

If your date is fuzzy (e.g May 1980) store it at start of period (e.g. May 1st. 1980). Or if your date is accurate to year (e.g. 1980) store it as January 1st. 1980 with corresponding accuracy value.

This way can easily query in a somewhat natural way and still have notion how accurate dates are. For example this allows you to query dates between Jan 1st 1980 and Feb 28th 1981, and get fuzzy dates 1980 and May 1980.


问:
Looking to build a 2 java applications. should it be web-based application or standalone application?
The first application (web-based) will be located at site1 and once the information is processed, it will be sent to the second application(web-based) located at site2. Application2 needs to be able to do the same thing as application1. Because of this, there has to be a way to share the database to keep data in sync. I wanted to go web-based for these two applications, but the issue arises, what if the internet is down? If application1 goes down due to a blackout or lost of internet, application2 should still be able to do what it needs to. I was thinking the database/server will be on Amazon EC2.

Even if I go with a standalone application for site1 and site2, it still needs some sort of internet connection to talk to a shared database/server right, possiblity through tcp connection? Because of this, I dont see why it shouldn't just be a web-based applicatin then. This the disagreement I'm having with my team.

I want to know the pros and cons of making these two application either being a stand-alone or web-based.

答:
Can have a web based (runs on browser) but is hosted on your LAN (very fast and does not depend on net)

If you have enough servers for today and usage 2 years down the line at both locations and a good IT team to support the servers then this is the way to go. Both apps can sync up at pre defined intervals with error checking to retry every 15 or so minutes when net is down.

Stand alone means the app code is on the local computer of the client. This means java needs to be installed on all clients. Such apps are sometimes called thick clients if they talk to a central server. I do not think this is the way to do unless people work on the field on laptops without good connectivity. Can be applet or web start or stand alone app.


问:
In Unit Testing, why would I create a Repository twice?
The other day I was reading a little about Unit Testing and I saw some examples where people create a repository interface (i.e. IExampleRepository) and then create the real repository (public class ExampleRepository : IExampleRepository) and a repository to be used for unit testing (FakeExampleRepository : IExampleRepository).

In the IExampleRepository they were implementing the same methods as in the ExampleRepository, however with different Linq queries.

What is exactly the objective here? I thought one part of unit testing your code is make sure that a method works correctly? But when I use two totally different queries, one for 'real' and one in the test, how much sense does the test make?

答:
I agree with the two answers from jk. and Jan Hudec -- they give some really good information. But I thought I'd add a bit.

Your first question ("What exactly is the objective here?") is important. In the case you're describing, the real objective is to test the classes that are utilizing the IExampleRepository interface, not testing the repository implementations. Creating the FakeExampleRepository allows you to test those client classes without worrying about the details of the real repository class.

This is especially true if the object you're trying to set up makes testing difficult (e.g. accesses the file system, calls a webservice, or talks to a database). By using interfaces (and other such techniques), you keep the coupling low. Therefore, class X need only know about the interface and need not know about the implementation details. The goal is all about making sure that class X is doing the right thing.

Mocking (or stubbing, faking... there are nuanced differences) is a powerful tool for unit testing and TDD. But it can be a pain to manually create and maintain these implementations. Therefore, most languages now have mocking libraries to help. Since you're using C#, I'd recommend Moq because it is simple and very powerful. Then you can test against the interface without piling up extra code for the mock implementations.


答:
... there has to be a way to share the database to keep data in sync. I wanted to go web-based for these two applications, but the issue arises, what if the internet is down? If application1 goes down due to a blackout or lost of internet, application2 should still be able to do what it needs to.

This seems contradictory:
If the data is to be kept in sync then you would particularly want AppB to halt when it cannot connect to the same data store as AppA. Otherwise the data would likely be out of sync, at some point. Would it not?

You have to ask yourself:

Will the data be Read-only? If not, you'll need to devise a way to keep two databases in sync, even if the connection is lost. Would using a single Database server or cluster solve this and meet requirements?

Do I want to utilize web-frameworks to accomplish any portion of my project? Is it necessary, or appealing?

To be clear and in my opinion: making your project 'web based' has nothing to do with solving any of this, unless of course you require web-based interaction with your software or data. I think this question is too generalized and needs more information.

Title: Looking to build a 2 java applications. should it be web-based application or standalone application?

Body:
The first application (web-based) will be located at site1 and once the information is processed, it will be sent to the second application(web-based) located at site2. Application2 needs to be able to do the same thing as application1. Because of this, there has to be a way to share the database to keep data in sync. I wanted to go web-based for these two applications, but the issue arises, what if the internet is down? If application1 goes down due to a blackout or lost of internet, application2 should still be able to do what it needs to. I was thinking the database/server will be on Amazon EC2.

Even if I go with a standalone application for site1 and site2, it still needs some sort of internet connection to talk to a shared database/server right, possiblity through tcp connection? Because of this, I dont see why it shouldn't just be a web-based applicatin then. This the disagreement I'm having with my team.

I want to know the pros and cons of making these two application either being a stand-alone or web-based.


问:
Where can I get free public raw data?
In web development, a mashup is a web page or application that uses and combines data, presentation or functionality from two or more sources to create new services.

There are many sources for getting data (raw information). Governments are good examples.

However many of those cost and have complicated licensing.
For instance SMHI in Sweden sells temperature data meanwhile our neighbor country Norway gives it away for free.

One would say what kind of application are you building and what data do you need?  I would say give me some interesting data and I will do a good mashup of it. I could just plot it on Google maps for instance.

So the question is: Where can I get free data?

Note: I'm not interested in data mining using a webcrawler!

Please share your sources with me.

答:
For general information, a good place to start might be Wikipedia's Open Data page - specifically the bottom parts of the page (Organisations promoting open data, See also, and External Links)

I was going to turn the rest of this post into a list of data sources for country and regional based data ... but the Guardian UK Newspaper has already gone and compiled a World Government Data Store so no point duplicating the effort, instead I'll just point to...

guardian.co.uk/world-government-data

...for a catalog of a lot of different country and city level Open Data Initiatives.

(It's not UK based, though it does seem to focus on English-speaking countries.)

At time of edit, it contains data for UK, US, Australia, Canada, New Zealand, and Basque Spain.

One thing not listed on there but worth mentioning is the Ordnance Survey UK mapping data.

ContentLicense: CC BY-SA 2.5

Title: Where can I get free public raw data?


答:
Due to security, Java delivered via a browser is Not a Good Idea (TM). Sun has had a bad string of serious security holes that have resulted in a lot of patches. I'm concerned enough of about these security issues that I have VM setup just for browser the two sites I need to use Java for. When I wear my IT hat, I recommend that users totally uninstall Java. Most people don't actually browse sites that require it, but they are vulnerable to other sites that contain a malevolent Java payload.

http://www.techlicious.com/blog/critical-java-security-risk-requires-immediate-action/

Something else to consider is that both iPad and Windows 8 RT do not support Java in the browser at all.

If you do need to go with something delivered via the browser, I strongly recommend that you consider going with another technology.


问:
Integration testing in en example - confused
I know that integration testing means integrating unit tested moduls and testing for interface errors etc.
But in real world examples, I am kinda lost. Here is an example of our system:

Back end (Java).

Front end (Flex).

Back end is processind data heavily and we have a lot of tests for weekly automated regression. But is it a system test? I think so, but on the other hand, it is just back end part testing.
Or should I say it is system testing only when UI part is involved as well? But again, with testing UI we cannot test the logic in data processing.

答:
Your understanding of Integration Testing may be only partially correct.

You said:

I know that integration testing means integrating unit tested modules and testing for interface errors etc.

And yes, integration testing involves integrating together modules that have (preferably) been unit tested to verify they play nicely with each other.

The next portion is where I think there may be some confusion.

If by interface you mean user interface then your understanding is incorrect.  On the other hand, if you mean software Interface as is commonly used in OO programming, then your understanding is OK.

Integration testing is agnostic to front-end / back-end / middle-tier / n-layer / blah-blah-blah.  As the SWEBOK section on Integration testing puts it:

Integration testing is the process of verifying the interaction between software components. Classical integration testing strategies, such as top-down or bottom-up, are used with traditional, hierarchically structured software.

Modern systematic integration strategies are rather architecture-driven, which implies integrating the software components or subsystems based on identified functional threads. Integration testing is a continuous activity, at each stage of which software engineers must abstract away lower-level perspectives and concentrate on the perspectives of the level they are integrating. Except for small, simple software, systematic, incremental integration testing strategies are usually preferred to putting all the components together at once, which is pictorially called “big bang” testing.

Wikipedia's entry on Integration Testing isn't bad either:

Integration testing (sometimes called Integration and Testing, abbreviated "I&T") is the phase in software testing in which individual software modules are combined and tested as a group. It occurs after unit testing and before validation testing. Integration testing takes as its input modules that have been unit tested, groups them in larger aggregates, applies tests defined in an integration test plan to those aggregates, and delivers as its output the integrated system ready for system testing.

So I think some of your confusion stems from worrying about which layer is being involved and how that rolls into what your organization is calling "Integration Testing."  It may very well be that your organization is using a less-than-precise variation of the term.

Have a look at SWEBOK's section on Testing to get a better understanding of the various classifications of testing (including unit, system, and usability).  That should help sort out the remainder of your questions regarding which testing is which and what aspects should be tested at what stages of Testing.

SWEBOK stands for Software Engineering Body of Knowledge


答:
The first Revised Scheme report AIM-452 from January 1978 has LET. Page 9.

note that Lisp used earlier a different construct PROG to introduce local variables.

```<code>(let ((a 1)
      (b 1))
  (+ a b))
</code>```

would have been written earlier approximately as

```<code>(prog (a b)
  (setq a 1)
  (setq b 1)
  (+ a b))
</code>```

Title: In what programming language did "let" first appear?

Body: I was wondering about the origins of the "let" used in Lisp, Clojure, and Haskell.  Does anyone know which language it appeared in first?</p>

Body:
The first Revised Scheme report AIM-452 from January 1978 has LET. Page 9.

note that Lisp used earlier a different construct PROG to introduce local variables.

(let ((a 1)
      (b 1))
  (+ a b))

would have been written earlier approximately as

(prog (a b)
  (setq a 1)
  (setq b 1)
  (+ a b))


答:
In the past I've stored dates-with-accuracy as a start date and an end date. The day may21,2012 would be represented as start=12am,may21,2012 and end=12am,may22,2012. The year 2012 would be represented as start=12am,Jan1,2012 end=12am,Jan1,2013.

I'm not sure if I'd recommend this approach. When displaying the information to the user you need to properly detect that a date range exactly covers a day in order to show "may 25" instead of two over-specific endpoints (which means dealing with daylight savings and so forth).

However, when you're not trying to translate to human, programming with the endpoints is a lot easier than with center+accuracy. You don't end up with lots of cases. That's pretty nice.


问:
If functional testing is referred as black box..how can it be done on unit test level?
Preparing myself for ISTQB, I found a bit odd many things in their textbooks. E.g. they call black box testing as functional testing when you are not concerned with structured but only observe the ouput based on inputs.
But later they say that functional testing is done at levels..well, how can I do unit testing without the knowledge how it works if I can just see it (and must see it). It is white box but then it is in the conflict..</p>

答:
When you are doing unit testing, you do not care about the actual implementation of the solution, only that it gives the expected results.  The difference being the level of unit that you are testing.  If you are testing a class, you are not making sure that internal methods are called but that the correct response is returned.  How the unit gets the response is not what you are testing for.

The different levels of testing cover the interaction and logic of the pieces of your implementation but are agnostic of what they actually are.

UPDATE
Generally, developers do fine grain unit testing.  However, even a tester can do lower level unit testing.  Focusing on specific pieces of an operation, for example sending data to the server themselves rather than through the web interface (removing the web interface from the unit).  Or for testing the web interface, spoofing the response provided so that you are able to test its behavior in isolation.

Knowing the implementation would allow you to determine the lower level 'units', however the tests themselves do not know anything about the actual details of the solution.


答:
After several experiments on a clone of the repository and given the vastness of commits to be squashed, I've determined it to be rather impractical to squash them while retaining the original branch commit just for some traceability.

The cleanest/simplest solution seems to be what @Karl suggested - just merge the branch into master and be done with it.  While it does introduce a lot of unnecessary and sometimes spurious commit messages throughout the history, it is clear where the branch was created, refreshed from master, and finally merged back in.

In the end, traceability wins out over a pristine history.

The final graph looks like the following (using --no-ff on the merge):

```<code>A[1]--------F[2]--H[3]--J[4]--L (m)
 \           \     \         /
  B----C-D-E--G-----I-------K   (t)

[1] - Release 1.0
[2] - Release 2.0
[3] - Hotfix 2.1
[4] - Hotfix 2.2
</code>```

And from a git log standpoint it looks something like:

```<code>commit L (HEAD, m, origin/master)
  merge parents K, J
commit K (t)
commit J
commit I
  merge parents H, G
commit H
commit G
  merge parents F, E
commit F
commit E
commit D
commit C
commit B
commit A
</code>```

Body:
After several experiments on a clone of the repository and given the vastness of commits to be squashed, I've determined it to be rather impractical to squash them while retaining the original branch commit just for some traceability.

The cleanest/simplest solution seems to be what @Karl suggested - just merge the branch into master and be done with it.  While it does introduce a lot of unnecessary and sometimes spurious commit messages throughout the history, it is clear where the branch was created, refreshed from master, and finally merged back in.

In the end, traceability wins out over a pristine history.

The final graph looks like the following (using --no-ff on the merge):

A[1]--------F[2]--H[3]--J[4]--L (m)
 \           \     \         /
  B----C-D-E--G-----I-------K   (t)

[1] - Release 1.0
[2] - Release 2.0
[3] - Hotfix 2.1
[4] - Hotfix 2.2

And from a git log standpoint it looks something like:

commit L (HEAD, m, origin/master)
  merge parents K, J
commit K (t)
commit J
commit I
  merge parents H, G
commit H
commit G
  merge parents F, E
commit F
commit E
commit D
commit C
commit B
commit A


答:
if only your team is using this code in the same process then i would return the same object (mutable object) . keep it simple and fast. Your own team would not change something on the objects unless they needed to, and if they did then they would have to make another copy.

If I was making a API too I would not keep it immutable, simple because engineer looks like its an object that can change (like if the object had something like reporting To or degrees-held - that can change over time.

Objects should mimic their real world counter parts as much as possible. Some attributes could be immutable (like date of birth etc)

Good external docs or code comments could explain how to use the object where required

Title: Immutable vs mutable object as returned parameter for class method

Body:
There is a class method (static method) in which I create and build some object. And for filling that object, I create it as mutable object.

My mutable object is a subclass of immutable object. So i can return it as super Class type.

That object is just created in class method and returns. It is not a part of object or class state.

Should I return mutable object or it immutable copy? (what solution will be correct in concept of encapsulation)

For example there is some pseudo code:

```<code>// MutableArray is subclass of Array
MutableArray : Array;

// Another class

static (Array *)getEngineers {
   Array *employees = Array.arrayWithContentsOfResource("employees.txt");
   MutableArray *engineers = new MutableArray;
   for (Employee *employee in employees) {
      if (employee.profession == "Engineer") {
         engineers.addObject(employee)
      }
   }
   return engineers; // should I return Array.arrayWithArray(engineers) instead of engineers ?
}
</code>```

Update:

I mean, if I have a mutable array that is a member of the object, then it makes sense to return immutable copy in getter method (to provide access control and encapsulation). But if I have method like in example. I need only immutable object for using. But creating immutable copy of array is additional operations. Does it make sense to make it immutable?


答:
You can do black box or white box testing at any level of abstraction.  Consider the following function:

```<code>double getDistance(double side1, double side2) {
    return pythagorean(side1, side2);
}
</code>```

A black box test would check that you got the right answer.  A white box test would also check that pythagorean() got called, rather than lawOfSines(), for example.

The next level of abstraction up might have a function like:

```<code>boolean inRange(double x, double y) {
    return getDistance(x, y) <= getMaximumRange();
}
</code>```

A black box test at this layer would check certain values precalculated to be in or out of range.  A white box test would also check that getDistance() and getMaximumRange() were called with the correct values during the calculation.  That makes getDistance() a white box detail at this layer, even though it was a black box detail at a lower layer.

The vast majority of the time, unit tests should not care about the implementation at all. i.e. you should be doing black box tests.  Often white box unit tests are unavoidable, but they should be minimized, so the coupling of your tests to your implementation is minimized.

Title: If functional testing is referred as black box..how can it be done on unit test level?

Body:
Preparing myself for ISTQB, I found a bit odd many things in their textbooks. E.g. they call black box testing as functional testing when you are not concerned with structured but only observe the ouput based on inputs.
But later they say that functional testing is done at levels..well, how can I do unit testing without the knowledge how it works if I can just see it (and must see it). It is white box but then it is in the conflict..</p>

Body:
You can do black box or white box testing at any level of abstraction.  Consider the following function:

double getDistance(double side1, double side2) {
    return pythagorean(side1, side2);
}

A black box test would check that you got the right answer.  A white box test would also check that pythagorean() got called, rather than lawOfSines(), for example.

The next level of abstraction up might have a function like:

boolean inRange(double x, double y) {
    return getDistance(x, y) <= getMaximumRange();
}

A black box test at this layer would check certain values precalculated to be in or out of range.  A white box test would also check that getDistance() and getMaximumRange() were called with the correct values during the calculation.  That makes getDistance() a white box detail at this layer, even though it was a black box detail at a lower layer.

The vast majority of the time, unit tests should not care about the implementation at all. i.e. you should be doing black box tests.  Often white box unit tests are unavoidable, but they should be minimized, so the coupling of your tests to your implementation is minimized.


答: Without knowing more about the use of this mutable/immutable Array, perhaps one factor to consider is the lifetime of the Array. Are you going to store the Array object for later use or pass it around to other parts of your code? If so, then an immutable object might be better because it reduces the chance of accidentally (or perhaps maliciously) having the Engineer array changed. If this is a one-time use, then you might be able to get away with a mutable array because the exposure of that array is already minimal.</p>


答:
Here is my take on this:

Go from fuzzy date to datetime object (which will fit into a database)

```<code>import datetime
import iso8601

def fuzzy_to_datetime(fuzzy):
    flen = len(fuzzy)
    if flen == 4 and fuzzy.isdigit():
        dt = datetime.datetime(year=int(fuzzy), month=1, day=1, microsecond=111111)

    elif flen == 7:
        y, m = fuzzy.split('-')
        dt = datetime.datetime(year=int(y), month=int(m), day=1, microsecond=222222)

    elif flen == 10:
        y, m, d = fuzzy.split('-')
        dt = datetime.datetime(year=int(y), month=int(m), day=int(d), microsecond=333333)

    elif flen >= 19:
        dt = iso8601.parse_date(fuzzy)

    else:
        raise ValueError("Unable to parse fuzzy date: %s" % fuzzy)

    return dt
</code>```

And then a function that takes the datetime object, and moves it back into a fuzzy date.

```<code>def datetime_to_fuzzy(dt):
    ms = str(dt.microsecond)
    flag1 = ms == '111111'
    flag2 = ms == '222222'
    flag3 = ms == '333333'

    is_first = dt.day == 1
    is_jan1 = dt.month == 1 and is_first

    if flag1 and is_jan1:
        return str(dt.year)

    if flag2 and is_first:
        return dt.strftime("%Y-%m")

    if flag3:
        return dt.strftime("%Y-%m-%d")

    return dt.isoformat()
</code>```

And then a unit test. Did I miss any cases?

```<code>if __name__ == '__main__':
    assert fuzzy_to_datetime('2001').isoformat() == '2001-01-01T00:00:00.111111'
    assert fuzzy_to_datetime('1981-05').isoformat() == '1981-05-01T00:00:00.222222'
    assert fuzzy_to_datetime('2012-02-04').isoformat() == '2012-02-04T00:00:00.333333'
    assert fuzzy_to_datetime('2010-11-11T03:12:03Z').isoformat() == '2010-11-11T03:12:03+00:00'

    exact = datetime.datetime(year=2001, month=1, day=1, microsecond=231)
    assert datetime_to_fuzzy(exact) == exact.isoformat()

    assert datetime_to_fuzzy(datetime.datetime(year=2001, month=1, day=1, microsecond=111111)) == '2001'
    assert datetime_to_fuzzy(datetime.datetime(year=2001, month=3, day=1, microsecond=222222)) == '2001-03'
    assert datetime_to_fuzzy(datetime.datetime(year=2001, month=6, day=6, microsecond=333333)) == '2001-06-06'

    assert datetime_to_fuzzy(fuzzy_to_datetime('2002')) == '2002'
    assert datetime_to_fuzzy(fuzzy_to_datetime('2002-05')) == '2002-05'
    assert datetime_to_fuzzy(fuzzy_to_datetime('2002-02-13')) == '2002-02-13'
    assert datetime_to_fuzzy(fuzzy_to_datetime('2010-11-11T03:12:03.293856+00:00')) == '2010-11-11T03:12:03.293856+00:00'
</code>```

There is a corner case where an event that precisely occurred at 2001-01-01T00:00:00.333333 but the system will interpret as being just "2001", but that seems very unlikely.

Body:
Here is my take on this:

Go from fuzzy date to datetime object (which will fit into a database)

import datetime
import iso8601

def fuzzy_to_datetime(fuzzy):
    flen = len(fuzzy)
    if flen == 4 and fuzzy.isdigit():
        dt = datetime.datetime(year=int(fuzzy), month=1, day=1, microsecond=111111)

    elif flen == 7:
        y, m = fuzzy.split('-')
        dt = datetime.datetime(year=int(y), month=int(m), day=1, microsecond=222222)

    elif flen == 10:
        y, m, d = fuzzy.split('-')
        dt = datetime.datetime(year=int(y), month=int(m), day=int(d), microsecond=333333)

    elif flen >= 19:
        dt = iso8601.parse_date(fuzzy)

    else:
        raise ValueError("Unable to parse fuzzy date: %s" % fuzzy)

    return dt

And then a function that takes the datetime object, and moves it back into a fuzzy date.

def datetime_to_fuzzy(dt):
    ms = str(dt.microsecond)
    flag1 = ms == '111111'
    flag2 = ms == '222222'
    flag3 = ms == '333333'

    is_first = dt.day == 1
    is_jan1 = dt.month == 1 and is_first

    if flag1 and is_jan1:
        return str(dt.year)

    if flag2 and is_first:
        return dt.strftime("%Y-%m")

    if flag3:
        return dt.strftime("%Y-%m-%d")

    return dt.isoformat()

And then a unit test. Did I miss any cases?

if __name__ == '__main__':
    assert fuzzy_to_datetime('2001').isoformat() == '2001-01-01T00:00:00.111111'
    assert fuzzy_to_datetime('1981-05').isoformat() == '1981-05-01T00:00:00.222222'
    assert fuzzy_to_datetime('2012-02-04').isoformat() == '2012-02-04T00:00:00.333333'
    assert fuzzy_to_datetime('2010-11-11T03:12:03Z').isoformat() == '2010-11-11T03:12:03+00:00'

    exact = datetime.datetime(year=2001, month=1, day=1, microsecond=231)
    assert datetime_to_fuzzy(exact) == exact.isoformat()

    assert datetime_to_fuzzy(datetime.datetime(year=2001, month=1, day=1, microsecond=111111)) == '2001'
    assert datetime_to_fuzzy(datetime.datetime(year=2001, month=3, day=1, microsecond=222222)) == '2001-03'
    assert datetime_to_fuzzy(datetime.datetime(year=2001, month=6, day=6, microsecond=333333)) == '2001-06-06'

    assert datetime_to_fuzzy(fuzzy_to_datetime('2002')) == '2002'
    assert datetime_to_fuzzy(fuzzy_to_datetime('2002-05')) == '2002-05'
    assert datetime_to_fuzzy(fuzzy_to_datetime('2002-02-13')) == '2002-02-13'
    assert datetime_to_fuzzy(fuzzy_to_datetime('2010-11-11T03:12:03.293856+00:00')) == '2010-11-11T03:12:03.293856+00:00'

There is a corner case where an event that precisely occurred at 2001-01-01T00:00:00.333333 but the system will interpret as being just "2001", but that seems very unlikely.


问:
Static Create method -- pros and cons compared with constructors
What are the pros and cons of having static object creation methods over constructors?

```<code>class Foo {
  private Foo(object arg) { }

  public static Foo Create(object arg) {
    if (!ValidateParam(arg)) { return null; }
    return new Foo(arg);
  }
}
</code>```

Few that I can think of:

Pros:

Return null instead of throwing an exception (name it TryCreate). This can make code more terse and clean on the client side. Clients rarely expect a constructor to fail.

Create different kinds of objects with clear semantics, e.g. CreatFromName(String name) and CreateFromCsvLine(String csvLine)

Can return a cached object if necessary, or a derived implementation.

Cons:

Less discoverable, more difficult to skim code.

Some patterns, like serialization or reflection are more difficult (e.g. Activator<Foo>.CreateInstance())

答:
The biggest drawback with static 'creators' is probably limiting inheritance. If you or the user of your library derives a class from your Foo, then Foo::Create() becomes pretty much useless. All logic defined there will have to be rewritten again in the inherited Create().

I'd suggest a compromise: define a constructor with trivial object initialization logic that never fails/throws, then define creator(s) with caching, alternate construction etc.  That leaves the possibility of deriving and you benefit from having creators for a given class.


答:
Your subclass shouldn't violate the contract that the super-class has with its clients.

It isn't automatically true that returning something mutable when the client expects something immutable will violate that contract, but I'm assuming the super-class returns something immutable for a reason, and that clients therefore expect to have an immutable object returned.  So, unless you have specific knowledge that it's OK in this case, don't break any client code relying on immutability - return something immutable.

(An example of what might go wrong - maybe classes written to work with the super-class never check its state because they assume it was checked on creation and can't be modified.  Then it gets an instance of your class, and fails to verify it is currently in a valid state).


问:
User stories are too high level and conceptual, management expects developers to fill in the blanks
I am employed in a very brilliant company with a true intention of doing XP. Communication is good and management is open to constructive discussion but due to pressing time constraints, some certain things are considered too RUP to be discussed.

At the moment I'm a little troubled with the volume of change that becomes necessary while implementing the stories. I believe many of these discoveries (which take time and effort of course) are the responsibilities of the story writers (customers, end users and product owners) and not the developers. To put is short, user-stories are too conceptual and just convey the underlying intention but lack enough details (specially pre-conditions and post-conditions, relevance to other stories, dependencies and alike). The developer is expected to fill in the blanks at his own discretion by the virtue of XP developers being designers and analysts at the same time. The problem is many of these blanks are discovered after some wrong assumptions have made made their way into evaluation time and code since noticing added complexities emerge than initially anticipated. Even then finding the right thing to fill in takes time which is - to various degrees - considered as deviation from the initial estimations.

I'm looking for a constructive way of conveying these implications to the management in a way that would not pose me as someone who is trying to unnecessarily complicate things. I'm new and as yet I have not established much credibility.

You insights are most welcome.

Closely related and somehow gives an answer : How much detail about a user story can a developer expect?

答:
Regardless of the development methodology, if whatever you are using to define requirements makes the developer make assumptions, it needs to be kicked back to the business side. I often have a good idea of what answer I would prefer so I kick things back like this:

XYZ is unclear to me. Does it mean ABC? Or am I missing something? (Assume XYZ is the requirement, assume ABC is the assumption I would like to make as a developer)

It takes far less time to get things cleared up before you make bad assumptions than to redo. Developers who make guesses about requirements without getting confirmation that their guess is right tend to be bad developers and they cost their company a lot of money. If a bad manager won't let you kick things back, then explain to him how much more expensive in terms of time and money it is to do it wrong. If he still insists, then do what he says and when it fails UAT, the next time you want to kick something back, remind him of how costly it was the time he wouldn't let you. If he still won't listen, find a better boss.

The other value of kicking things back is that, gradually, the business will learn what you need and give you better requirements.


问:
Frame Pointer Explanation
In MIPS assembly, there is a register for the stack pointer, and another register for the frame pointer. What is the frame pointer and what is its purpose? How does it differ from the stack pointer?</p>

答:
In MIPS assembly, the stack pointer points to the top of the stack.  As you allocate space on the stack, the stack pointer ($sp) moves to point to the free memory.

When calling a subroutine in MIPS assembly (registers were at a premium in those days - register based parameters where unconventional), one writes the parameters to the stack and then advances the stack pointer.

When the method starts out, a parameter may be at an offset of 16($sp).  However, as variables are placed on the stack, the stack pointer moves and the same parameter may now be located at 24($sp) instead.  This can make the code a bit confusing.

The frame pointer ($fp) points to the start of the stack frame and does not move for the duration of the subroutine call.  This points to the base of the stack frame, and the parameters that are passed in to the subroutine remain at a constant spot relative to the frame pointer.

Realize that the frame pointer will need to be stored and restored with subroutine calls that modify it.

Further reading:

UWisc Madison CS 354 MIPS conventions

Chapter 3 MIPS Assembly Language

UMD CS 311 Understanding the Stack

UWash Seattle CSE 410 MIPS Calling Conventions Summary

Wikipedia Frame pointer

Title: Frame Pointer Explanation


问:
Software Design and architecture from scratch
I know a lot about programming syntactically. What I am unsure is about how should I go to build a software from scratch. How should be the objects be designed and how should they interact with each other.

To learn how good softwares are written I have tried to read code of some good open source softwares but could not get a slightes of idea, how they are designed as there is no availble documentation available regarding the code.

I want to become a more skilled programmer but I could not go any further, I am well versed in programming but now i am stuck as I have no way to go forward.

Please guide about how should I learn the software design and what should be next to improve the skills.

P.S : I am a hobbiest programmer, i have never worked in a software house neither am i looking forward to it.

答:
Start  building small "completable" projects from scratch using your existing knowledge on your own as side "projects" ..just make them up. Then refine / refactor the design by identifying and implementing Patterns / "good principles" you've learned from your reading / learning. Once the project has been completed and "refactored thuroughly", Do it again...

There are definitely some architectural "starting points" like keeping various "code layers" separate (DB access layer, Biz Logic layer, etc..). As well as trying to identify where "Interfaces" can be used so you can "extract" their "implementations" and begin introducing some kind of Dependency Injection framework... But that can be done during later passes of your "refactoring" efforts (if that's a path you even want to go down).

I would say there's no "silver bullet" or "this is how you do it" because projects vary so greatly. You can watch pod casts and read books, which all help becoming an expert, but until you are able to implement what you've learned, there's still a pretty large gap, and the only way to implement what you've learned is to either do it and learn from your own mistakes, or find a good mentor who can teach you what they've learned from their mistakes..

"Just having the map doesn't get you to your destination"


问:
why are noSQL databases more scalable than SQL?
Recently I read a lot about noSQL DBMSs. I understand CAP theorem, ACID rules, BASE rules and the basic theory. But didn't find any resources on why is noSQL scalable more easily than RDBMS (e.g. in case of a system that requires lots of DB servers)?

I guess that keeping constraints and foreign keys cost resources and when a DBMS is distributed, it is a lot more complicated. But I expect there's a lot more than this.

Can someone please explain how noSQL/SQL affects scalability?

答:
noSQL databases give up a massive amount of functionality that a SQL database gives you by it's very nature.

Things like automatic enforcement of referential integrity, transactions, etc. These are all things that are very handy to have for some problems, and which require some interesting techniques to scale outside of a single server (think about what happens if you need to lock two tables for an atomic transaction, and they are on different servers!).

noSQL databases don't have all that. If you need that stuff, you need to do it yourself, but if you DON'T need it (and there are a lot of applications that don't), then boy howdy are you in luck. The DB doesn't have to do all of these complex operations and locking across much of the dataset, so it's really easy to partition the thing across many servers/disks/whatever and have it work really fast.


问:
HTTP Session or Database approach
I am confused a little as what should be my approach, Working on a design of shopping cart and i need to store shopping cart either in session or in database but not sure which approach would be best.here are the use case

User is not logged in and adding product to cart (Anonymous user)

User is logged in and adding product to cart.

First case is more confusing to me, since there can be many cases where user just visiting web-shop and adding product without logging in and can be quite possible that he might not going for a checkout process.

But still we need to create a Shopping-Cart for this user, in order to create and save shopping-cart i have two option.

When user adding a product, create a cart in the database and associate this cart with this user, moment he logged in move this cart to logged in user.

Create Cart, add product to it and save it to the Session, when user logged in create Cart in the database and associated logged in user with this cart with User.

I know that both database driven Cart system as well Session based can have there positive as well negative aspects, but not sure which one might be the best approach taking in to account following points

Scalability

Flexibility

Extensibility

Application Should take care of speed

Looking for input on this aspect to decide the path.

答:
Let us consider the two use cases you have mentioned

User is not logged in and adding product to cart (Anonymous user)

In this case, you definitely want to save the user's cart information in a session to serve the user well during his/her session. If he/she does decide to log in/create an account, you can handle this based on the next use case. If he/she does not log in, you don't need to fill your database with this guest user's information as it was only used to serve the guest during the session. This data can be handled on a Stateless basis i.e. not the state is not saved from session to session.

User is logged in and adding product to cart.

In this case, you can handle it the same way as above (old school e-commerce sites) and also add this information to the database and associate it with the user. This is mainly used to provide stateful (state saved from session to session) information such as "Product Browsing History", "Recommendations" etc. i.e similar to Amazon.com.

Things to think about:

Is it necessary to save data?

If yes, what data is most critical to be saved to serve the user better?

Scalability + data storage - How will you save the cart information for fast lookup in your database to support many users?

OwnerDisplayName: Umesh Awasthi

Title: HTTP Session or Database approach


答: Create is a Factory method.  When I feel a need for this, I implement the Factory pattern, and define an interface to represent the contract for creating the object as well as an implementing class, which then gets injected where needed.  This retains the advantages of moving the creation responsibility out of the class, but avoids (or at least makes more obvious) the limitations of performing object creation outside the class constructor.</p>

Title: Static Create method -- pros and cons compared with constructors

Body:
What are the pros and cons of having static object creation methods over constructors?

```<code>class Foo {
  private Foo(object arg) { }

  public static Foo Create(object arg) {
    if (!ValidateParam(arg)) { return null; }
    return new Foo(arg);
  }
}
</code>```

Few that I can think of:

Pros:

Return null instead of throwing an exception (name it TryCreate). This can make code more terse and clean on the client side. Clients rarely expect a constructor to fail.

Create different kinds of objects with clear semantics, e.g. CreatFromName(String name) and CreateFromCsvLine(String csvLine)

Can return a cached object if necessary, or a derived implementation.

Cons:

Less discoverable, more difficult to skim code.

Some patterns, like serialization or reflection are more difficult (e.g. Activator<Foo>.CreateInstance())


答:
You are basically asking how to design programs. It turns out there is a book which teaches you that, and it is called … How to Design Programs (aka HtDP). It teaches you, well, how to design programs. And it does this by giving you recipes to follow for how to analyze problems, solve them, transform them into algorithms and further into working programs.

Note that "recipe" is basically another word for "program", so in other words, the book teaches you programs for humans to run in their heads in order to generate programs to be executed by computers. How cool is that? :-)

The authors are currently working on a draft for a second edition of HtDP, which you might want to read instead of the first edition. The material about imperative programming has been removed, and is going to be covered in the as-of-yet unwritten second volume How to Design Components, but you can read those missing chapters from the first edition.

Note that HtDP assumes no programming knowledge and is targeted at high school students. But don't let that stop you: it just means that you'll probably be able to finish some early chapters faster, but I don't think you will be bored.

Concrete Abstractions is also a good read along similar veins. Like HtDP, it doesn't assume any programming knowledge.

Another book that you might hear mentioned is Structure and Interpretation of Computer Programs aka SICP. It is one of the greatest programming books ever written, and again, it doesn't assume any programming knowledge.

It is, however, geared to complete newbies who study at MIT. And so, while it does not assume any programming knowledge, it does assume quite a bit of domain knowledge, e.g. in the fields of electrical engineering, physics and math. Note: these have nothing to do with the concepts being taught, they are just needed to understand the exercises and examples. So, it might be better to read HtDP or Concrete Abstractions first, and then read SICP.

Title: Software Design and architecture from scratch

Body:
I know a lot about programming syntactically. What I am unsure is about how should I go to build a software from scratch. How should be the objects be designed and how should they interact with each other.

To learn how good softwares are written I have tried to read code of some good open source softwares but could not get a slightes of idea, how they are designed as there is no availble documentation available regarding the code.

I want to become a more skilled programmer but I could not go any further, I am well versed in programming but now i am stuck as I have no way to go forward.

Please guide about how should I learn the software design and what should be next to improve the skills.

P.S : I am a hobbiest programmer, i have never worked in a software house neither am i looking forward to it.


问:
Forcing ExtJS application updates after deployment
I work on an ExtJS application with a Django backend, and we keep running into issues when we push new code. Our users seldom refresh their browsers, so after a code push we end up with users using the same (now outdated) version of our ExtJS application for days at a time instead of the new, updated version.

How do other people deal with this issue? (I haven't found anything online about this, so I probably haven't found the right keywords yet.)

答:
You are going to want to version you ext file when you deploy.
So instead of having:

```<code><script type="text/javascript" src="ext-all.js"></script>
</code>```

You can have:

```<code><script type="text/javascript" src="ext-all-4-0-2a.js"></script>
</code>```

It would have a cache header that never expires and when you update to another version the name will have changed and it will force the browser to load the new file.

When you upgrade your to Ext 4.2 for example it would be :

```<code><script type="text/javascript" src="ext-all-4.2.js"></script>
</code>```

Here is a really good write up on deployment level web devlopment. http://developer.yahoo.com/performance/rules.html  Make sure to check out the "Add an Expires or a Cache-Control Header" section which talks about versioning.  Versioning should also be done with your component/application level scripts as well.

This is a shameless plug but you can check out the source of my website http://www.coffeedig.com/coffee/ which is a django application written in Ext to see the versioning in action.

Body:
You are going to want to version you ext file when you deploy.
So instead of having:

<script type="text/javascript" src="ext-all.js"></script>

You can have:

<script type="text/javascript" src="ext-all-4-0-2a.js"></script>

It would have a cache header that never expires and when you update to another version the name will have changed and it will force the browser to load the new file.

When you upgrade your to Ext 4.2 for example it would be :

<script type="text/javascript" src="ext-all-4.2.js"></script>

Here is a really good write up on deployment level web devlopment. http://developer.yahoo.com/performance/rules.html  Make sure to check out the "Add an Expires or a Cache-Control Header" section which talks about versioning.  Versioning should also be done with your component/application level scripts as well.

This is a shameless plug but you can check out the source of my website http://www.coffeedig.com/coffee/ which is a django application written in Ext to see the versioning in action.


问:
Internship along with Google Summer of Code?
I am wondering about how much time Google Summer of Code typically takes.  I do have an internship lined up for this summer, but GSoC seems like quite an awesome experience.  I also really want to get into Open Source development.  It states in the FAQ that it's not a good idea to attempt both, but it doesn't give any specifics.  I'm wondering if it's feasible to do both.  Or would I be better off by trying to work on open source in my spare time?

Thanks for all your help!

答: Having done both an internship and GSOC, I'd agree with the FAQ that it is not a good idea to attempt to do both at the same time.</p>


问:
How do you store "fuzzy dates" into a database?
This is a problem I've run into a few times. Imagine you have a record that you want to store into a database table. This table has a DateTime column called "date_created". This one particular record was created a long time ago, and you're not really sure about the exact date, but you know the year and month. Other records you know just the year. Other records you know the day, month and year.

You can't use a DateTime field, because "May 1978" isn't a valid date. If you split it up into multiple columns, you lose the ability to query. Has anyone else run into this, if so how did you handle it?

To clarify the system I'm building, it is a system that tracks archives. Some content was produced a long time ago, and all that we know is "May 1978". I could store it as May 1 1978, but only with some way to denote that this date is only accurate to the month. That way some years later when I'm retrieving that archive, I'm not confused when the dates don't match up.

For my purposes, it is important to differentiate "unknown day in May 1978" with "May 1st, 1978". Also, I would not want to store the unknowns as 0, like "May 0, 1978" because most database systems will reject that as an invalid date value.

答:
If you split it up into multiple columns, you lose the ability to
  query.

Says who?  Here's what you do:

Have 3 columns, Day, Month, Year, each of int type, and a fourth column TheDate of DateTime type.

Have a trigger that uses the 3 columns Day, Month, Year to build TheDate if TheDate is left null but one or more of the Day, Month, Year fields has a value.

Have a trigger that populates Day, Month, Year fields when TheDate is supplied but these fields are not.

So if I do an insert like:  insert into thistable (Day, Month, Year) values (-1, 2, 2012); then TheDate will become 2/1/2013 but I will know its really an indeterminate date in 2/2012 because of the -1 in the Day field.

If I insert into thistable (TheDate) values ('2/5/2012'); then Day will be 5, Month will be 2, and Year will be 2012 and because none of them is -1 I will know this is the exact date.

I don't lose the ability to query because the insert/update trigger makes sure my 3 fields (Day, Month, Year) always produce a DateTime value in TheDate which can be queried.


问:
Constants and Big O
Are constants always irrelevant even if they are large?

For example is
O(10^9 * N) == O(N) ?

答:
Big O describes how an algorithm scales; not, strictly speaking, how long it takes to execute.

In your example, if the time it takes to execute over 1 item is big, then it's going to take a (non-trivially) long time to execute the algorithm, even if it's applied to one item.  But that algorithm will still increase in time linearly.  So if you hit it with 100 items, you can expect it to take 100 times as long.

The same cannot be said of an O(n^2) algorithm; no matter how small the linear constant is, it's still going to eventually overtake the time it takes to execute the linear algorithm (as you increase N).

The linear constant is therefore ultimately irrelevant.

Title: Constants and Big O


问:
Which .NET technology would work as an improvement for this client/server application?
I am helping a friend with his rewrite of his .NET application, currently using WinForms + sockets, which is a client/server program that currently has on average ~120 concurrent users (sending messages, using the chat). The client will grow and eventually must be able to handle a several thousand concurrent users.

A thread is created per user but this is not scalable due to hardware limitations (the server broke when stress-testing more users), not to mention the research I've done on the topic points to this being a bad practice.

Speaking of research, I've gathered the following possibilities for this rewrite:

Using asynchronous threads with a thread pool of 8 or so threads (will this be able to handle 8,000 users?)
or

Using WCF (this seems like the more .NET approach and more intuitive with its callbacks instead of using loops)

I'm leaning more towards WCF right now. Before anyone asks, I have mentioned how this could be a web application instead, but he said no.

Edit (answers to questions):

The threads currently read from the socket (the packet) and parse it (simple string parsing) and depending on the type payload, executes another function. The functions executed are not very resource-intensive.

答:
You can certanly go down the WCF Route.

However that would simply be a step sideways unless you decided to embrace Asynchronous Sockets.

Asynchronous is the way forward for high scalability

Essentially you should change your existing network code to be Asynchronous (if it isn't already).

Remove any blocking in your network code, accept more connections & messages and push off the parsing of your messages into a queue.  Limit the number of threads processing your messages at any one time to stop your server from thrasing your CPU.

WCF has built in support for Asynchronous Networking.

http://www.codeproject.com/Articles/234085/Fixing-WCF-to-build-highly-scalable-async-REST-API

However You don't need WCF to do Asynchronous Networking

Asynchronous C# sockets may be an easier refactor and may be a better fit for your specific project rather than refactoring your entire project and endpoints to use WCF.

https://stackoverflow.com/questions/319732/tips-techniques-for-high-performance-c-sharp-server-sockets

http://msdn.microsoft.com/en-us/library/bbx2eya8.aspx

WCF simply wraps the asynchronous sockets for you and you will end up with the same issues in your code that you had if you'd gone down the Asynchronous Sockets route.

So WCF is not going to be a silver bullet for your application.  You'll need to do more to identify the bottlenecks and perhaps even shard your servers to push off processing into a more horizontally scalable solution.

i.e. Multiple Servers in a round-robin (or equivalent) solution for CPU intensive tasks.


答:
It depends on what you are using your analysis to tell you.

If you are using it just to tell you how your algorithm scales up in the limit (i.e. as N goes to infinity), then the constants are not relevant.

If you are concerned (and you probably should be) with what the actual performance numbers are likely to be, then the constants are significant ... and the lower order terms could be significant too.

Big O is a way of simplifying the complexity analysis to answer one performance-related question.  There are other questions that are equally relevant.

Title: Constants and Big O

Body:
Are constants always irrelevant even if they are large?

For example is
O(10^9 * N) == O(N) ?