问:
Close to knapsack problem?
I think that I am trying to solve a problem somewhat similar to a knapsack problem. I am not sure though.

Please see the input given below and my solution. There are three types of items and 4 buckets. The problem is to keep the items as uniformly as possible in the 4 buckets. i.e. the capacity of the buckets (or number of items in the bucket) should be equal (or close to equal). You can also keep the items from different types in the same bucket.

example input:

```<code>item-type   :   A   B   C
  #items    :   12  36  25
</code>```

One simple solution I am using currently is as follows:

Since there are 4 buckets, each bucket should get (sum of items)/4. For the above example input data, it is (12 + 36 + 25)/4, i.e. approximately 18 items. The capacity of each of the bucket should be "CLOSE" to 18.

Then start keeping the items of type A into the first bucket until the number of items become 18.  However, still we need 6 more items for the first bucket as the number of type A items are 12. Therefore keep 6 items of the type B into the first bucket so that the capacity of the first bucket will be 18. Like this fill the other three buckets also.

Finally the four buckets are as follows:

```<code>Bucket-1: (12 A items, 6 B items)
Bucket-2: (18 B items)
Bucket-3: (12 B items, 6 C items)
Bucket-4: (19 C items).
</code>```

I think that the time complexity of this problem is O(N^2), N is the number of buckets.

It would be great if someone helps me to improve the solution further.

Note-1: We can mix items of different types in the same bucket.

Note-2: We also need to somehow keep the information of type of the item in the bucket.

答:
For item quantities A, B, C and buckets quantity N, each bucket should keep close to A/N + B/N + C/N items, respectively of each kind. Actual putting the items should take O(N).

You can distribute A mod N + B mod N + C mod N items slightly unevenly among buckets using round-robin choice: put a remaining item of type A into bucket #1, remaining item of type B into bucket #2, etc, wrap to bucket #1 as needed. A clever use of mod operator should make this O(N), too.

Title: Close to knapsack problem?


答:
Wouldn't you want to iterate through the number of total items and not buckets^2?

For example,

```<code>item[0] goes into bucket 1.
item[1] goes into bucket 2.
item[2] goes into bucket 3.
item[3] goes into bucket 4.
item[4] goes into bucket 1.
</code>```

Some things to think about:
Does each item have a property, item-type as part of its definition?
For example, you know a red marble, is red. Are the items sorted?

ContentLicense: CC BY-SA 3.0

Title: Close to knapsack problem?

Body:
I think that I am trying to solve a problem somewhat similar to a knapsack problem. I am not sure though.

Please see the input given below and my solution. There are three types of items and 4 buckets. The problem is to keep the items as uniformly as possible in the 4 buckets. i.e. the capacity of the buckets (or number of items in the bucket) should be equal (or close to equal). You can also keep the items from different types in the same bucket.

example input:

```<code>item-type   :   A   B   C
  #items    :   12  36  25
</code>```

One simple solution I am using currently is as follows:

Since there are 4 buckets, each bucket should get (sum of items)/4. For the above example input data, it is (12 + 36 + 25)/4, i.e. approximately 18 items. The capacity of each of the bucket should be "CLOSE" to 18.

Then start keeping the items of type A into the first bucket until the number of items become 18.  However, still we need 6 more items for the first bucket as the number of type A items are 12. Therefore keep 6 items of the type B into the first bucket so that the capacity of the first bucket will be 18. Like this fill the other three buckets also.

Finally the four buckets are as follows:

```<code>Bucket-1: (12 A items, 6 B items)
Bucket-2: (18 B items)
Bucket-3: (12 B items, 6 C items)
Bucket-4: (19 C items).
</code>```

I think that the time complexity of this problem is O(N^2), N is the number of buckets.

It would be great if someone helps me to improve the solution further.

Note-1: We can mix items of different types in the same bucket.

Note-2: We also need to somehow keep the information of type of the item in the bucket.

Body:
Wouldn't you want to iterate through the number of total items and not buckets^2?

For example,

item[0] goes into bucket 1.
item[1] goes into bucket 2.
item[2] goes into bucket 3.
item[3] goes into bucket 4.
item[4] goes into bucket 1.

Some things to think about:
Does each item have a property, item-type as part of its definition?
For example, you know a red marble, is red. Are the items sorted?


问:
Should method names getX and setX only be used for fields and have no other effects?
Should I avoid using getX() and setX() as names for methods that aren't "traditional" getters or setters? (Let's define traditional as it only gets/sets the field and has no other effects.) I guess the question is does a method being named as such imply that it's a field of the object? Also, is it bad practice to do other things in them?

```<code>class Foo {
    private Object bar;
    private Object bazz;

    // "traditional"
    public Object getBar() {
        return bar;
    }

    // bad practice?
    public Object getBazz() {
        doSomeOtherThing();
        return bazz;
    }

    // not a field
    public Object getJazz() {
        return makeSomeJazz();
    }

    // ...
}
</code>```

If getX() isn't appropriate if it's not a field, what is? (Probably too broad, I realize.) Obviously something like makeJazz() is probably better than getJazz() if getJazz() is considered bad here.

I guess another question is does it really matter? I like the idea of "self documenting code", but maybe I'm just worrying too much about something minor.

答:
Requiring that getX methods always correspond to a field breaks encapsulation.

That the field exists at all is private information of the class, something no other class should rely on. Maybe you want to change to a different implementation some day. On the other hand, the getX method is a public interface. If it implied the existence of a field, would it have to be removed if the field was?

Let's say a person's full name is always of the form "Firstname Lastname" (obviously they aren't, but this is an example). You could store that with two fields, firstname and lastname, or with one field name. Both those implementations allow functions get_first_name, get_last_name and get_full_name to exist. That's fine.

A getX method should behave as if it were just a field lookup: it should be fast, it should be constant (if the object didn't change in the meantime), it should have no unnecessary side effects, and if a setX method exists then it should accept whatever value was returned by getX and behave like a setX method.


问:
Should I validate a method call's return value even if I know that the method can't return bad input?
I'm wondering if I should defend against a method call's return value by validating that they meet my expectations even if I know that the method I'm calling will meet such expectations.

GIVEN

```<code>User getUser(Int id)
{
    User temp = new User(id);
    temp.setName("John");

    return temp;
}
</code>```

SHOULD I DO

```<code>void myMethod()
{
    User user = getUser(1234);
    System.out.println(user.getName());
}
</code>```

OR

```<code>void myMethod()
{
    User user = getUser(1234);

    // Validating
    Preconditions.checkNotNull(user, "User can not be null.");
    Preconditions.checkNotNull(user.getName(), "User's name can not be null.");

    System.out.println(user.getName());
}
</code>```

I'm asking this at the conceptual level. If I know the inner workings of the method I'm calling. Either because I wrote it or I inspected it. And the logic of the possible values it returns meet my preconditions. Is it "better" or "more appropriate" to skip the validation, or should I still defend against wrong values before proceeding with the method I'm currently implementing even if it should always pass.

My conclusion from all answers (feel free to come to your own):

Assert when

The method has shown to misbehave in the past

The method is from an untrusted source

The method is used from other places, and does not explicitly state it's post-conditions

Do not assert when:

The method lives closely to yours (see chosen answer for details)

The method explicitly defines it's contract with something like proper doc, type safety, a unit test, or a post-condition check

Performance is critical (in which case, a debug mode assert could work as a hybrid approach)

答:
You definitely don't have to.

For instance, if you already know that a return can never be null - Why would you want to add a null check? Adding a null check is not going to break anything but it's just redundant. And better not code redundant logic as long as you can avoid it.


问:
Finding the first index in which the element and the index are the same
I am to build a python function that takes a sorted list of numbers and outputs the first index the is equal to the element in its place.

It should run on O(log n) which I know how it should be running, operationally, but I can't manage to implement it. I guess I should check if the first element fulfills the required. Then, check whether or not the middle index is bigger than its element or smaller, and then go to the middle index between the previous index and the first\last index(which is a recursive operation.).

My code for now(using @Stefan guidance ):

```<code>def Fyn(I,lo,hi):
    while lo < hi:
        if I[(lo+hi)/2]==(lo+hi)/2:
             if I[(lo+hi)/2-1]==(lo+hi)/2-1:
                    return Fyn(I,0, (lo+hi)/2)
             else:
                    return (lo+hi)/2
        if I[(lo+hi)/2]<(lo+hi)/2:
            return Fyn(I,(lo+hi)/2,hi)
        else:
            return Fyn(I,lo,(lo+hi)/2)
</code>```

What remains is actually finding the smallest index for which it happens. Right now it won't always do so. ()

答:
Don't slice your list, pass start and end indices instead</p>

```<code>def Fun(I):
    def Inner(I, L, R):
        for i in range(L,R):
            if i==[i]:
                return i
            elif I[len(I)/2-1]>len(I)/2-1:
                return Inner(I, L, L + (R/2))
            else:
                return Inner(I, L/2, R)
    return Inner(I, 0, len(I)-1)
</code>```

Body:
Don't slice your list, pass start and end indices instead

def Fun(I):
    def Inner(I, L, R):
        for i in range(L,R):
            if i==[i]:
                return i
            elif I[len(I)/2-1]>len(I)/2-1:
                return Inner(I, L, L + (R/2))
            else:
                return Inner(I, L/2, R)
    return Inner(I, 0, len(I)-1)


问:
MVC: How dumb should the view be?
creating an MVC structure and pondering this...

My view has an Add button. Click it and we send off a request to the controller (using pub/sub so view doesn't really know who its talking to). Having done this the view disables its Add button until it receives a "data added" event from the controller (again pub/sub so doesn't really know who from). At this points it updates its list and re-enables the add button.

My question is this. Should the view contain this logic about enabling/disabling the add button or should the controller? My thoughts so far are perhaps the controller because it is up to the controller whether multiple adds can be handled at any one time or whether they need to be serialised in the method described. however, it is easier to implement the current scheme in the view as don't have to expose so many methods to control the view.

Is there a preferred way that is the most MVC compliant? Thanks :)

答:
If you can thoroughly test the view, then there is no limit to how smart it can be.

If it is not practically feasible to test the view, then it should be so dumb that you can tell that it is correct by just looking at it.

Let's take an example.

Suppose that you have a model which deals with public opinion polls, exposing poll results as a list of floats, and you have a view which displays those results as customizable bar charts or pie charts.

If your view is rendering the charts in a web page, then it is testable, because you can produce various sets of sample data and display options, feed them to the view, capture the html output for each, and check to make sure that it is as expected.

If your view is rendering the charts on the screen, then it is not really testable, because even if you capture its output in a bitmap, it is extremely difficult to verify that the image contains what it is supposed to contain.

So, in the second case you need to dumb down your view, to the point where it is so simple that it does not require testing.  That would be a view which takes graphics primitives (polygons and texts) and simply sends them to a renderer.  Obviously, there is a gap here: on one hand you have a model which produces a list of float, and on the other hand you have a view which needs a list of polygons and texts.  So, your view would need to be hooked up not to your actual model, but to an intermediate model which transforms lists of poll results to sets of polygons and tests. This transformer is testable, so it can be as smart as it needs to be.

Title: MVC: How dumb should the view be?


答:
Here's a starting point - a function that checks whether that constraint can be met somewhere within that range...

```<code>def Can_Exist_In_Range (xs, lo, hi) :
  return not ((xs [hi] < lo) || (xs [lo] > hi))
</code>```

This is using inclusive bounds for convenience, which is actually pretty bad practice in general. If the lowest value is higher than the high index bound, or the highest value is lower than the low index bound, the ranges cannot overlap (assuming the bound indexes are in order, of course).

For divide and conquer, any range that could contain a matching element should be divided into non-overlapping subranges, and each part recursively checked (unless you get a match before checking all of them). Any range that can't contain a matching element, stop subdividing and searching that range.

This isn't binary search. Unless I'm missing something (particularly a rule requring all list elements to be unique integers would be significant), just because the lower-half subrange gets searched doesn't mean you can immediately eliminate the upper-half subrange, which is why I commented that I'm curious about a proof this can be done in O(log n) time. Actually, if you allow non-integer numbers, it's trivial to produce a counter-example that needs O(n) time...

```<code>[0.1, 1.1, 2.1, 3.1, 4.1, ...]
</code>```

Since each element is non-integer, no element can match its index. But since each element is only fractionally higher than its index, there are no non-overlapping ranges to eliminate until you get to the single-element level.

To explain why, even with (potentially non-unique) integers you may need to search both subranges, consider...

```<code>                *              *
Index:    0  1  2  3     4  5, 6, 7
        [ 1, 2, 2, 4,    5, 6, 6, 8 ]
</code>```

We have a match in both halves. We want the first one, so no need to search the second half, but we can't know that until after we've searched the first half. Neither subrange can be eliminated until that search is done. After all...

```<code>                               *
Index:    0  1  2  3     4  5, 6, 7
        [ 1, 2, 3, 4,    5, 6, 6, 8 ]
</code>```

... when we do the split, we don't know enough to prove that the first subrange contains a match - it might not, in which case the only match we have is the one from the second subrange.

Title: Finding the first index in which the element and the index are the same

Body:
I am to build a python function that takes a sorted list of numbers and outputs the first index the is equal to the element in its place.

It should run on O(log n) which I know how it should be running, operationally, but I can't manage to implement it. I guess I should check if the first element fulfills the required. Then, check whether or not the middle index is bigger than its element or smaller, and then go to the middle index between the previous index and the first\last index(which is a recursive operation.).

My code for now(using @Stefan guidance ):

```<code>def Fyn(I,lo,hi):
    while lo < hi:
        if I[(lo+hi)/2]==(lo+hi)/2:
             if I[(lo+hi)/2-1]==(lo+hi)/2-1:
                    return Fyn(I,0, (lo+hi)/2)
             else:
                    return (lo+hi)/2
        if I[(lo+hi)/2]<(lo+hi)/2:
            return Fyn(I,(lo+hi)/2,hi)
        else:
            return Fyn(I,lo,(lo+hi)/2)
</code>```

What remains is actually finding the smallest index for which it happens. Right now it won't always do so. ()

Body:
Here's a starting point - a function that checks whether that constraint can be met somewhere within that range...

def Can_Exist_In_Range (xs, lo, hi) :
  return not ((xs [hi] < lo) || (xs [lo] > hi))

This is using inclusive bounds for convenience, which is actually pretty bad practice in general. If the lowest value is higher than the high index bound, or the highest value is lower than the low index bound, the ranges cannot overlap (assuming the bound indexes are in order, of course).

For divide and conquer, any range that could contain a matching element should be divided into non-overlapping subranges, and each part recursively checked (unless you get a match before checking all of them). Any range that can't contain a matching element, stop subdividing and searching that range.

This isn't binary search. Unless I'm missing something (particularly a rule requring all list elements to be unique integers would be significant), just because the lower-half subrange gets searched doesn't mean you can immediately eliminate the upper-half subrange, which is why I commented that I'm curious about a proof this can be done in O(log n) time. Actually, if you allow non-integer numbers, it's trivial to produce a counter-example that needs O(n) time...

[0.1, 1.1, 2.1, 3.1, 4.1, ...]

Since each element is non-integer, no element can match its index. But since each element is only fractionally higher than its index, there are no non-overlapping ranges to eliminate until you get to the single-element level.

To explain why, even with (potentially non-unique) integers you may need to search both subranges, consider...

                *              *
Index:    0  1  2  3     4  5, 6, 7
        [ 1, 2, 2, 4,    5, 6, 6, 8 ]

We have a match in both halves. We want the first one, so no need to search the second half, but we can't know that until after we've searched the first half. Neither subrange can be eliminated until that search is done. After all...

                               *
Index:    0  1  2  3     4  5, 6, 7
        [ 1, 2, 3, 4,    5, 6, 6, 8 ]

... when we do the split, we don't know enough to prove that the first subrange contains a match - it might not, in which case the only match we have is the one from the second subrange.


问:
Data integrity, BinaryWriter, C#
I've developed a data acquisition system for a scientific experiment. An FPGA buffers the scientific data in a FIFO and a C# program empty this FIFO at a rate of ~45MB/s.

Because the duration of the experiment suddenly changed from seconds to days, the data now needs to be stored as binary data.

Not being a programmer or computer scientist, it seems to me that there is a huge potential for data corruption. Previously every packet was written to a new line. If, for some reason, one bit went missing it would corrupt one packet.

However, for one missing bit in binary data we might loose TBs of irreplaceable data.

Is there a way to assure that the data does not turn to garbage?
My first thought is to write a separate ASCII file of hamming code data, but I don't know if it's feasible.

答:
If you want resilience against errors in your file, store additional data that will help you resynchronize and detect errors.

If you have a table of offsets to data records, you can use that to find a record, no matter what the records contain.

If you have a header for a record with expected size and checksum, you can test integrity and know how to skip past it if it's uninteresting or defective. If you give your header a reasonably unique signature, it's likely that you will be able to resynchronize by just scanning forward until you find a valid header.

If you start having large amounts of data, consider some existing data storage format that has been battle-tested in reality. Any decent database ought to be Good Enough for storing blobs of binary data with acceptable integrity.

Title: Data integrity, BinaryWriter, C#


答:
per logical concept Store the data in a database.

Store datetimeoffset timestamp with the data.

Use 1 record per concept or period.

Keep the rows under 4k in general.

If validation can be done on the data before storing.

You could even persist checksum with the data.

Title: Data integrity, BinaryWriter, C#

Body:
I've developed a data acquisition system for a scientific experiment. An FPGA buffers the scientific data in a FIFO and a C# program empty this FIFO at a rate of ~45MB/s.

Because the duration of the experiment suddenly changed from seconds to days, the data now needs to be stored as binary data.

Not being a programmer or computer scientist, it seems to me that there is a huge potential for data corruption. Previously every packet was written to a new line. If, for some reason, one bit went missing it would corrupt one packet.

However, for one missing bit in binary data we might loose TBs of irreplaceable data.

Is there a way to assure that the data does not turn to garbage?
My first thought is to write a separate ASCII file of hamming code data, but I don't know if it's feasible.


问:
Pairing 40 people over 20 days such that no pair is the same twice or more
I'm trying to figure out what algorithm / method I can use to figure out how to pair people together such that the pairs aren't repeated

i.e. if a unique individual is denoted by Px where x is their "number"

```<code>Day 1 : P1-P2; P3-P4; P5-P6;
Day 2 : P1-P3; P2-P5; P4-P6;
Day 3 : P1-P4; P3-P6; P3-P5;
</code>```

I did the above manually and no pairs are repeated on subsequent days. I need to do this but for 40 people over 20 days.

Is there a quick method or algorithm I can use?

答:
You basically have a tournament.

In a round-robin tournament, 40 players can play 39 rounds and meet each player exactly once.

The Wikipedia article on round robin tournaments has a description of a simple algorithm to generate the pairings for each round. There are also pre-computed tables on the Internet (look for Berger tables, for instance), but not normally for 40 players as that is quite a lot.

Then you just pick your favorite 20 rounds out of the 39.


问:
building feature dependent version software
I have a program that is used by customers to do some computational work. And how long it takes always depends on how much data customer wants to compute. Some customers have a little data, some have a lot (bigger amount of customers have smaller data, but once with bigger data are richer).
It's possible to make a scalable version of that software, but that of course will cost to implement, and price of software would rise.
Marketing of course would like to have different options, like for customers that have a lot of data and can pay, they would suggest more expansive version, that supports scaling. For others, they would like to suggeest cheaper version, that will take longer.

But from development view, these are kind of 2 different version of same think. And therefore if something is implemented (like new dialog window), it should be implemented in both versions. Some parts of course can be just reused, but some might need special work (because how data access is implemented).
So it looks like have one version would be much cheaper from development perspective. It would be easy to fulfill marketing demands by building 2 version, where 1 have unlimited scalling posibility, another don't have that (assuming that it's possible to have some variable for that). But than it feels like giving worst program to poorer customers, just because they don't really really need it.

Is there some practice how this kind of versions are done and what's a reasoning doing that? Like you have windows for desktop and windows for servers, desktop version will have limits how many users can connect etc.

答:
Two versions of the same thing is to be avoided at all costs. Build one thing that can be configured to hide some windows/menus/features instead. The alternative is very very expensive.

Let's say you build two versions. Both versions needs to be written, written tests for, tested manually. Bug fixes in both versions. Both versions needs to be maintained.

Alternatively you build one version. Then you build some logic to turn some features off. So compared to what I listed above, a little logic to turn features off is clearly cheaper.  Also you get the benefit that when a feature is implemented you can turn it on in the "light" version for free if you so choose.


问:
Crawler - programming language choice
I'm currently developing a web crawler. The first version was developed in Node.js and runs pretty well.

The issues that I encountered with Node.js are in no particular order:

slow URL and query-string parsing library

slow HTTP packet parsing due to the C++ binding call overhead

slow DNS resolution due to synchronous call to getaddrinfo rather than the asynchronous  C-Ares library

lack of good Unicode support in RegExps

These are for the most part due to the fact that it's still very young and that it's a general purpose language with a small core developer team.

The proof-of-concept I have at the moment runs pretty well thanks to the asynchronous nature of Node.js and some monkey-patches that I had to add.

Now I'm starting to think about what comes after the proof-of-concept.

The question of the programming language is one I wasn't able to answer yet.

The essential qualities that I'm looking for:

I/O and concurrency management

fast string parsing capabilities for HTTP messages, HTML, JSON ...etc.

bindings to some kind of queuing / persistence layer

I thought about C, Erlang, Rust, Go and D.

I know this isn't usually the kind of questions that are accepted here on Stack Exchange but I'm not sure where else I could get an answer.

Update: Some more information:

we currently use 8 core, 3.2 Ghz machines.

the persistence layer is ensured by running a PostgreSQL database

the point is to crawl all kinds of news sites and blogs as well as RSS feeds

we currently forward the content to a processing pipeline through Apache Kafka

答:
I've developed a crawler in Python for educational purposes (TripAdvisor Scraper).

It's built upon Scrapy for crawling the web and I'd choose Python because it is good for Natural Language Processing techniques, you have a lot of toolkit and resources (NLTK).
Python is not so fast to process high volumes of data, and you have to deal with memory management to keep things fast. Scrapy isn't able to run JavaScript on web pages, so if you have something that changes at runtime via JS, you won't be able to retrieve it.

Now I'm writing a full asynchronous system, able to scale out and self repair, for crawling data from various sources.

I'm using Scala for functional programming (no side effect, easy to reach high parallelism) + Akka (actor based model, easy to isolate things and scale out the entire system. It provides supervisors strategies).
Akka paradigm is strongly inspired by Erlang actor model system.
To manipulate the DOM I'm using JSOUP, it's a full library to deal with HTML and it's able to execute JavaScript. So it's good to simulate real browser interaction.
The entire stack is based upon JVM.

This new stack is extremely fast compared to the first one and it's reactive and resilient.

I would suggest to go with the second solution, or if you want to try I think that Erlang is good enough to build a fast crawler and easy achieve parallelism.

Title: Crawler - programming language choice


问:
Injecting collections as constructor dependencies
We are using constructor dependency injection in our application. Following that approach we inject everything using an injection container so we are able to replace any dependency with a Mock.

Some colleagues complain about this procedure as they say that injecting collections of any type is going too far with injection and they prefer to call a new() inside the constructor.

What do you think about this? What procedure do you follow if you are using dependency injection?

One example of a real use case. We have a Dictionary with an special behavior, it allows to add the same key several times and it just increments an internal counter and it decreases the counter when removing elements from it only removing the element from memory when the counter is 0 (the class has been simplified a bit but I think you can get the idea):

```<code>public class ExtendedDictionary<TValue> : IExtendedDictionary<TValue>
{
    private readonly IDictionary<CustomKey, TValue> dictionary;

    public ExtendedDictionary(IDictionary<CustomKey, TValue> dictionary)
    {
        this.dictionary = dictionary;
    }

    public void Add(ParameterBase parameter, DriveInformation driveInformation, TValue data)
    {
        CustomKey key = new CustomKey (driveInformation, parameter.Index);

        if (this.dictionary.ContainsKey(key))
        {
            this.dictionary.Single(p => p.Key == key).Key.Count++;
        }
        else
        {
            this.dictionary.Add(key, data);
        }
    }

    public void Remove(ParameterBase parameter, DriveInformation driveInformation, TValue data)
    {
        CustomKey key = new CustomKey (driveInformation, parameter.Index);

        if (this.dictionary.ContainsKey(key))
        {
            this.dictionary.Single(p => p.Key == key).Key.Count--;
        }
        else
        {
            this.dictionary.Remove(key);
        }
    }
}
</code>```

答:
Pros and Cons

As with any situation where there's a judgement call to be made, it's worth weighing up the main pros and cons. I'll summarize them as I see them, then go into some aspects a little deeper to try to show why- although there are some situational aspects- in the large majority of cases the factors pretty heavily favor a particular answer.

Pros

Ease of writing the "arrange" phase of tests. When you are trying to test a class with heavily encapsulated state, it can be frustrating that arranging a particular situation often can't be done directly and instead involves multiple calls on the class. Being able to inject in state directly removes this hassle.

Cons

You are introducing a crack in your encapsulation- now, instead of external code never being able to access or mutate your contained state, it can mutate it once, when the object is created

You are increasing, by a great deal, the cognitive complexity of working out potential routes through your code. Instead of the single, simple, degenerative case of an empty collection, you now have to think about every possible way that the collection could be initialized and think about what error or corner cases this may introduce

Any new checks you need to introduce to deal with potentially invalid collections being passed in have to be runtime checks. By creating the collection yourself, you have a compile-time guarantee that certain error cases will never happen

You weaken your ability for your class to guarantee invariants- facts about its internal state which it can take responsibility for ensuring are always true.

It isn't really dependency injection. Ultimately, the Dictionary in this case is not conceptually a dependency. Rather than being a collaborator that your class uses to get access to behavior it doesn't want to take responsibility for itself, it's just a data structure which your class should be fully responsible for managing. (This isn't a con per se, but an indication that you shouldn't just shove this under the umbrella of DI and take that as justification). Note this won't always be the case for, e.g. all IEnumerable<T>s, but for a collection which exists purely to represent your class's internal state and not provide any special behaviour, it probably will be.

That's quite a few more cons than pros, but it's not fair to just count them up, since there's quite a lot of overlap- for example, not being able to guarantee invariants is very closely related to gaps in encapsulation. So it's worth going a bit deeper with reference to your ExtendedDictionary<TValue> class.

Adding Complexity to ExtendedDictionary<TValue>

Disclaimer: Your example isn't actually complete- some of the logic is wrong and some of the types aren't defined. So I may be making wrong assumptions in a couple of places. I'll try to be as clear about that as possible.

I looked at your example, and tried to think of ways I could break your class by injecting a bad starting state. Here are the ones I came up with:

Null Dictionary.

Zero or negative Count on one or more CustomKeys.

Custom equality comparer which will incorrectly identify unequal CustomKeys as equal, or vice versa

Possibly Count is a uint, which would remove one part of one of those possibilities, but otherwise I think those are all error cases. In fact, since you're using an IDictionary rather than a Dictionary it's really much worse- you could have it do more or less anything! But it would be easy enough to change it to a concrete class so I won't worry too much about that.

For the first two, these could be protected against relatively easily by adding guard clauses as the beginning of your constructor. But this isn't free:

You've replaced compile-time guarantees that none of these will happen with potential runtime failures

You have to work out all these possibilities. I know it took me a non-negligible amount of time to come up with those three, and there may well be more that I've missed altogether. If the class changes, new ones may arise, or old ones may become redundant. You've basically added a huge amount of cognitive complexity to reasoning about how this class works.

You have to write and maintain more tests, to cover these cases. Since the motivation behind this was testability, this is very counterproductive; the easiest test to write is no test at all!

You may argue that, given that the constructor is only meant to be used by your IOC container and tests, this is all irrelevant, because anyone injecting a non-empty, non-default-comparer Dictionary from anywhere outside the tests is something you're not allowed to do. But to me that's very similar to making all getters and setters public and saying it's just a rule that you're not actually supposed to use them outside of tests.

What About Testability?

Okay, so what about the pro? If you can't make sure a class actually works through testing it, what use is making sure it's all nicely encapsulated?

Well, first I'd say there are two aspects to "testability": the ease of writing tests, and the ease of maintaining tests.

One great thing about unit tests is it puts your api first- if a class is hard to use by its consumers, you learn that early on. But if you write a class that- for example- lacks encapsulation, you find that code which uses that class is easy to write, but hard to maintain. If you want to- for example- add some new business logic associated with a poorly-encapsulated class, that logic may not just require a change to that class, it may spray out across all the code that uses it.

Unfortunately, tests don't really let you detect maintainability (as opposed to writeability) issues early. More likely, when you make a change that requires lots of fixes to production code, you'll probably also have to make lots of fixes to test code too. The upshot is that giving yourself a backdoor into the class when you first start writing tests may make that stage quicker, but just like with production code, you may be creating a maintenance headache for yourself later.

Having said that, let's look again at ExtendedDictionary<TValue>. If we remove that constructor parameter, is it really that hard to test? Again, I can't say for certain since your code is missing parts, but wouldn't it be relatively easy to write a helper method:

```<code>private ExtendedDictionary<object> _extendedDictionary = new ExtendedDictionary<object>();
private void ArrangeState(Dictionary<CustomKey, object> state)
{
    foreach(var kvp in state)
    {
        for(int i=0; i<kvp.Key.Count;i++)
        {
            _extendedDictionary.Add(something, something, kvp.Value); //Not sure exactly what should go to those other parameters
        }
    }
}
</code>```

This doesn't seem all that painful, and I think usually for well-designed classes that aren't doing too much, setting up a particular state shouldn't be too much of a headache

But...

So all that said, I'm pretty heavily in favor of not doing this. However, I appreciate that the ExtendedDictionary<TValue> was chosen as a simple example, and it's probably not impossible that you may hit some situation where, for whatever reason, tests would be prohibitively difficult to write without using a technique like this.

If you are in that situation, I think the first course of action should be to consider that a design smell and give serious thought to refactoring your way out of the problem. If that fails, then injecting your collection may, ultimately be warranted.

So as a solution to a specific, rare problem, I would treat it with caution but not reject it outright. But I would certainly not do it proactively, as your question seems to indicate. You'll ultimately get a lot more benefit- in both tests and production code- from sealing up that encapsulation as tight as you can and writing a few extra test helper methods.

Body:
Pros and Cons

As with any situation where there's a judgement call to be made, it's worth weighing up the main pros and cons. I'll summarize them as I see them, then go into some aspects a little deeper to try to show why- although there are some situational aspects- in the large majority of cases the factors pretty heavily favor a particular answer.

Pros

Ease of writing the "arrange" phase of tests. When you are trying to test a class with heavily encapsulated state, it can be frustrating that arranging a particular situation often can't be done directly and instead involves multiple calls on the class. Being able to inject in state directly removes this hassle.

Cons

You are introducing a crack in your encapsulation- now, instead of external code never being able to access or mutate your contained state, it can mutate it once, when the object is created

You are increasing, by a great deal, the cognitive complexity of working out potential routes through your code. Instead of the single, simple, degenerative case of an empty collection, you now have to think about every possible way that the collection could be initialized and think about what error or corner cases this may introduce

Any new checks you need to introduce to deal with potentially invalid collections being passed in have to be runtime checks. By creating the collection yourself, you have a compile-time guarantee that certain error cases will never happen

You weaken your ability for your class to guarantee invariants- facts about its internal state which it can take responsibility for ensuring are always true.

It isn't really dependency injection. Ultimately, the Dictionary in this case is not conceptually a dependency. Rather than being a collaborator that your class uses to get access to behavior it doesn't want to take responsibility for itself, it's just a data structure which your class should be fully responsible for managing. (This isn't a con per se, but an indication that you shouldn't just shove this under the umbrella of DI and take that as justification). Note this won't always be the case for, e.g. all IEnumerable<T>s, but for a collection which exists purely to represent your class's internal state and not provide any special behaviour, it probably will be.

That's quite a few more cons than pros, but it's not fair to just count them up, since there's quite a lot of overlap- for example, not being able to guarantee invariants is very closely related to gaps in encapsulation. So it's worth going a bit deeper with reference to your ExtendedDictionary<TValue> class.

Adding Complexity to ExtendedDictionary<TValue>

Disclaimer: Your example isn't actually complete- some of the logic is wrong and some of the types aren't defined. So I may be making wrong assumptions in a couple of places. I'll try to be as clear about that as possible.

I looked at your example, and tried to think of ways I could break your class by injecting a bad starting state. Here are the ones I came up with:

Null Dictionary.

Zero or negative Count on one or more CustomKeys.

Custom equality comparer which will incorrectly identify unequal CustomKeys as equal, or vice versa

Possibly Count is a uint, which would remove one part of one of those possibilities, but otherwise I think those are all error cases. In fact, since you're using an IDictionary rather than a Dictionary it's really much worse- you could have it do more or less anything! But it would be easy enough to change it to a concrete class so I won't worry too much about that.

For the first two, these could be protected against relatively easily by adding guard clauses as the beginning of your constructor. But this isn't free:

You've replaced compile-time guarantees that none of these will happen with potential runtime failures

You have to work out all these possibilities. I know it took me a non-negligible amount of time to come up with those three, and there may well be more that I've missed altogether. If the class changes, new ones may arise, or old ones may become redundant. You've basically added a huge amount of cognitive complexity to reasoning about how this class works.

You have to write and maintain more tests, to cover these cases. Since the motivation behind this was testability, this is very counterproductive; the easiest test to write is no test at all!

You may argue that, given that the constructor is only meant to be used by your IOC container and tests, this is all irrelevant, because anyone injecting a non-empty, non-default-comparer Dictionary from anywhere outside the tests is something you're not allowed to do. But to me that's very similar to making all getters and setters public and saying it's just a rule that you're not actually supposed to use them outside of tests.

What About Testability?

Okay, so what about the pro? If you can't make sure a class actually works through testing it, what use is making sure it's all nicely encapsulated?

Well, first I'd say there are two aspects to "testability": the ease of writing tests, and the ease of maintaining tests.

One great thing about unit tests is it puts your api first- if a class is hard to use by its consumers, you learn that early on. But if you write a class that- for example- lacks encapsulation, you find that code which uses that class is easy to write, but hard to maintain. If you want to- for example- add some new business logic associated with a poorly-encapsulated class, that logic may not just require a change to that class, it may spray out across all the code that uses it.

Unfortunately, tests don't really let you detect maintainability (as opposed to writeability) issues early. More likely, when you make a change that requires lots of fixes to production code, you'll probably also have to make lots of fixes to test code too. The upshot is that giving yourself a backdoor into the class when you first start writing tests may make that stage quicker, but just like with production code, you may be creating a maintenance headache for yourself later.

Having said that, let's look again at ExtendedDictionary<TValue>. If we remove that constructor parameter, is it really that hard to test? Again, I can't say for certain since your code is missing parts, but wouldn't it be relatively easy to write a helper method:

private ExtendedDictionary<object> _extendedDictionary = new ExtendedDictionary<object>();
private void ArrangeState(Dictionary<CustomKey, object> state)
{
    foreach(var kvp in state)
    {
        for(int i=0; i<kvp.Key.Count;i++)
        {
            _extendedDictionary.Add(something, something, kvp.Value); //Not sure exactly what should go to those other parameters
        }
    }
}

This doesn't seem all that painful, and I think usually for well-designed classes that aren't doing too much, setting up a particular state shouldn't be too much of a headache

But...

So all that said, I'm pretty heavily in favor of not doing this. However, I appreciate that the ExtendedDictionary<TValue> was chosen as a simple example, and it's probably not impossible that you may hit some situation where, for whatever reason, tests would be prohibitively difficult to write without using a technique like this.

If you are in that situation, I think the first course of action should be to consider that a design smell and give serious thought to refactoring your way out of the problem. If that fails, then injecting your collection may, ultimately be warranted.

So as a solution to a specific, rare problem, I would treat it with caution but not reject it outright. But I would certainly not do it proactively, as your question seems to indicate. You'll ultimately get a lot more benefit- in both tests and production code- from sealing up that encapsulation as tight as you can and writing a few extra test helper methods.


答:
I mean you have two choices one of them being having both algorithms in one place and another one having them as separate software (light version, pro version etc).

As for the first case you won't have much flexibility on the price because most probably it will just cost a lot since it includes the scalable implementation.

Second, choice is something that I have used quite often while developing WordPress plugins/marketing software.

The way to battle code duplication since you will have it inevitably is to write shared libraries.

For example:

If you are doing your project in .NET and you think that your WPF forms will be looking exactly the same then have encapsulate those in a shared project and have both of your versions reference that.

If you are doing your project using JQuery then try using plugins that are shared between the two projects.

And so on with other programming/scripting languages.

This way if you decided to add a feature that you think belongs to both you will have that in the shared project, otherwise write it specifically for the version you want.

That way you will have some nice separation and also fight code duplication along the way.

Title: building feature dependent version software

Body:
I have a program that is used by customers to do some computational work. And how long it takes always depends on how much data customer wants to compute. Some customers have a little data, some have a lot (bigger amount of customers have smaller data, but once with bigger data are richer).
It's possible to make a scalable version of that software, but that of course will cost to implement, and price of software would rise.
Marketing of course would like to have different options, like for customers that have a lot of data and can pay, they would suggest more expansive version, that supports scaling. For others, they would like to suggeest cheaper version, that will take longer.

But from development view, these are kind of 2 different version of same think. And therefore if something is implemented (like new dialog window), it should be implemented in both versions. Some parts of course can be just reused, but some might need special work (because how data access is implemented).
So it looks like have one version would be much cheaper from development perspective. It would be easy to fulfill marketing demands by building 2 version, where 1 have unlimited scalling posibility, another don't have that (assuming that it's possible to have some variable for that). But than it feels like giving worst program to poorer customers, just because they don't really really need it.

Is there some practice how this kind of versions are done and what's a reasoning doing that? Like you have windows for desktop and windows for servers, desktop version will have limits how many users can connect etc.


问:
What languages should I know if I'm interested in building web applications?
I'm interested in developing for the web and I'm curious what languages should I know besides html, css, javascript, php/python/ruby. I don't know much about c# or java to make a decision whether to learn them or not, are they used much in the field I'm interested in?
What other technologies should I look into ?</p>

答:
Except the ones you listed, Java, ASP, .NET (and hence C#) and SQL are common in the Web world. However, it seems to me that many of those who use PHP and C#/.NET/ASP are not enjoying it. Few are fanatic of these technologies if they are also proficient in something else.

Also, you can't learn all of these properly in a reasonable amount of time. I'd recommend to test around, and stick with one web framework you like, and learn that properly first.

The one language of the above most likely to be useful in any case is SQL, so learn that first. As for choice of web framework, the obvious ones are Ruby on Rails, Django (Python), Pyramid (also Python). I don't know which exist on Java/.NET, sorry.

Google App Engine is cool, and uses a sort of almost-Django by default, but Pyramid runs on it also. And you can run Java, but I don't know what restrictions there are there.

Well, that's all the general recommendations I have. If you want me to get dogmatic I'd recommend you too use Python with Pyramid and SQLAlchemy for SQL access. But it's so much a matter of taste. :)

OwnerDisplayName: webguy

ContentLicense: CC BY-SA 2.5


问:
Agile Scrum - Importance of requirement gathering and documentation and suggestions to improve
We are using agile scrum methodologies for developing and maintaining a product. Since we are a product company, we do not work with customer directly on day to day basis, but instead communicate with BA who gives us the requirement.

We are facing following challenges and wondering how to overcome them.

The product we are working on is complex and the BAs and the team is relatively new. When we get the requirement it is very high level. The acceptance criteria is also very high level and involves lot of discovery during the sprint. There are many touch-points which were never considered in the acceptance criteria and they always come up when the developer looks at the code. Due to this the sprint planning becomes very hard and takes away lot of development and QA time in discussion with BA on what should happen for each scenario.

Is this acceptable in agile scrum? If so, how can we plan in a better way given very little clarity on "WHAT" has to be done. Most of the times if we are unable to complete the work in a given release, additional teams are brought in to complete the work and this complicates it further.

I believe most of it is due to very little documentation and whatever we have is scattered. When a team member leaves the company, he takes away the knowledge and due to little or no documentation, the new team member (BA) has no where to refer to, and due to this the entire team suffers. So how much documentation is essential?

In this case, is it the responsibility of the BA to give proper acceptance criteria or  the scrum team's(BA is also part of our scrum team) responsibility to navigate through such difficulties? The developers and the QAs are always in a time crunch due to the discovery process involved during the sprint. The quality also suffers due to this.

We also have to spare additional capacity for the bugs, customer cases. So at the end of it, it is usually the developers and QA who have to put extra hours to meet all the commitments.

答:
Is this acceptable in agile scrum?

Define "acceptable". In some environments, it's fine. In most, it is prohibitively problematic.

That problem is that most Business Analysts are horrrrrible. Even when they understand the product well, and even when they can define and scope features, they rarely understand much of the implications of the feature to the codebase. So while it's not great to have ill-defined feature requests, it's unlikely you'll be able to fix that.

So how much documentation is essential?

None.

I am perhaps in the minority here, but I find documentation - especially over the long term - to be less than worthless. Documentation is a poor communication method. Many people are bad at making it. It doesn't allow you to tailor your message to different target audiences. It doesn't allow interaction to clarify ideas.

Worse, it's invariably incorrect/out-of-date. So not only did you spend a bunch of time and effort making these documents, they're actively lying to your staff.

In this case, is it the responsibility of the BA to give proper acceptance criteria or the scrum team's(BA is also part of our scrum team) responsibility to navigate through such difficulties?

It varies. To me, BAs (and other stakeholders) don't get to dictate what is being done in a sprint. It is an agreement between the team and the stakeholders made during sprint kickoff. That kickoff meeting should be "hey, I would like XYZ feature." "Okay, what does that mean? Do you need Foo?" sort of back and forth until everyone can agree what work is going to be done in the sprint, usually in the form of "In 2 weeks, Bob will be able to open the App and order a Pina Colada".

We also have to spare additional capacity for the bugs, customer cases. So at the end of it, it is usually the developers and QA who have to put extra hours to meet all the commitments.

Why?

You're negotiating with stakeholders during sprint kickoff about the commitments you're taking on, right?

If you commit to too much, then that's what you get.
If you get told what you're doing this sprint, then maybe you should do agile.


问:
Upload files from mobile device to Amazon S3 architecture
I have an iOS app that communicates to a REST API I developed. I am using Amazon S3 for storage of image and audio files. Right now I am able to upload/download the files directly from the mobile app (client). I am currently facing some issues with the speed of upload/download of small files (15-20 kB), as well as other problems such as Amazon S3 not allowing a time skew of more than 15 min (meaning if my customers have their time zone or date-time configurations incorrect, they would not be able to upload/download these files from S3.

I am now facing an architectural decision towards these issues, that is, uploading/downloading the files from S3 to my own API, store them temporarily and upload/download them to the mobile client. In other words change from this:

```<code>S3 <----> iOS <----> REST API
</code>```

to this:

```<code>S3 <----> REST API <----> iOS
</code>```

Advantages:

All data is processed and handled by API

No problems if client and storage (S3) are out of sync (time skew)

Customer's phone communicates with only one web service

Disadvantages:

All file transfers are a two way process

API's web traffic increases substantially

Increased chance of data/packet loss thus having to repeat the whole process

What would be the best choice? Leave it as is, or start developing file transfers through my own API?

答:
The speed of downloading and uploading small files can easily be circumvented by putting several files into an archive (eventually with level 0 compression if files don't need to be compressed, which is the case for example for JPEG files). If the user uploads hundreds of files, this will result in two-three large files. If the user needs to upload only one or two files, then the speed won't matter much anyway.

The incorrect date and time configuration is the problem of the user (shouldn't synchronization be automatic on nearly every mobile device connected to internet?) Your application may verify if the time on the device is correct using NIST ITS. If, for some reason, this is impossible, use your own REST API to check if the time is correct. If the time is not correct, inform the user that the synchronization is impossible until the time is wrong, asking if he wants your app to correct the device's time automatically.

The choice between letting the app to interact directly with Amazon's S3 or using your REST API as a middle man depends mostly on those factors:

Who owns Amazon's API keys. Are you ready to pay S3 usage for your users or do you prefer that they pay themselves directly? In the first case, the middle man API makes sense. In the second case, go with a direct connection to S3.

Privacy, data sensitivity and legal issues. If the data is transitioning through your servers, this means that you can access it, and also means that you are uploading it to S3.

The first point is problematic if the data is sensitive, and you may end up being sued if customers were not informed properly that the data ends on your servers instead of Amazon's (which may become even more interesting, legally speaking, if your servers are in a different country than Amazon's).

The second point is problematic if users are uploading content which is against Amazon's policies. Imagine someone uploading child's pornography. For Amazon, you were uploading it, so you'll be the accountable person.

Bandwidth. If your servers bandwidth is expensive (and why wouldn't it be?), you'll end up paying much more for the bandwidth than you'll pay to Amazon. More importantly, you pay twice the actual size of the files, since you are first receiving them, and then sending them.


问:
PHP - How to structure this application
Sorry if the title is leading.. I'm looking for advice for the following system I am implementing:

The system is to provide quotes for customers, this is then exported to PDF and emailed to the customer. Basically, the "quote" consists of the following:

Font cover (Images - Pulled from a database)

Attributes - (This is the actual quote)

Header image - (Product images - Pulled from the database)

Customers information (Customers details for the quote)

T&C

I have made classes for each and declared an object. Which is then passed to the final object Quote but the problem is this:

I'm using PHP and the UI works so that each of these sections is a page. So, page 1 = front cover, Attributes = page 2 etc.. The final page, is where the Quote object is created and stored within the database. But, at each of the sections, the user has the ability to view the details up to this point.

I have been using sessions for this, but, now, I have 5 sessions and all of these form 1 object (which is stored as a session) - Is there a more practical way to do this? And if there is, how would I transfer the previous information to the next section?

Any advice, greatly appreciated.

答:
If you want to preserve data across the pages, there are several approaches:

The URI or the form. A wrong choice in most situations (some ASP.NET developers would disagree, pointing to view state as an example of preserving information in a form).

Sessions. This works well for most cases if the size of the preserved data is not very large.

Database (including Redis). The benefits, compared to sessions, is that:

The size of the data is irrelevant, and doesn't impact the memory footprint of your web application.

If the web application or the web server crashes, the data is still there.

The data is persisted forever (well, until you decide you don't need it any longer), which is ideal for cases where you want to keep the data even if the user leaves and comes back a few days, weeks or months later.

Failover/mirroring scenarios become much easier than with sessions.

From your description, it seems that you don't necessarily need the benefits of persistence inside a database. Sessions appear a good approach here.

Note that the number of sessions depends on the number of users, with one session per user. Two users—two sessions, independently of how much objects are stored in the sessions. This means that:

now, I have 5 sessions and all of these form 1 object

doesn't make too much sense. Either you are calling session something which are not an actual session, or you ended up thinking that there are multiple sessions, where there is only one.

Title: PHP - How to structure this application


问:
Best practices in managing nuget repositories
I've been involved in a project using Visual Studio 2012 that has multiple solutions and multiple projects. In most cases, one solution will have to refer to the library build in a different project. Since most projects have their own set of nuget repositories, we run into the issues of missing references even when we try to restore the packages from within VS.

Has anyone come across some best practices on that?

The system we are using are VS 2012 and our code is store in a GIT server.

答:
The issue you are encountering is weird. You may check twice the way NuGet dependencies are stored in source control.

What may help is:

To use one solution instead of several ones. Unless there are multiple teams in different departments of your company working on completely independent projects with a dependency on a common project, one solution may make things much easier, especially when it comes to dependencies.

If you are afraid that Visual Studio will perform badly with a solution containing a few hundreds of projects, you may be interested by this answer.

To deploy each project in a form of a NuGet package and to let NuGet handle the dependencies. If you work on closed source software, you can set up your private NuGet server for that.

Agreeing on common versions of third-party NuGet libraries may make things easier as well. For instance, if a team is upgrading to a new version of Entity Framework, another team may do it at the same time to avoid potential issues with two versions being used side by side.


问:
Why is negative zero important?
I'm confused about why we care about different representations for positive and negative zero.

I vaguely recall reading claims that having a negative zero representation is extremely important in programming that involves complex numbers. I've never had the opportunity to write code involving complex numbers, so I'm a little baffled about why this would be the case.

Wikipedia's article on the concept isn't especially helpful; it only makes vague claims about signed zero making certain mathematical operations simpler in floating point, if I understand correctly. This answer lists a couple of functions that behave differently, and perhaps something could be inferred from the examples if you're familiar with how they might be used. (Although, the particular example of the complex square roots looks flat out wrong, since the two numbers are mathematically equivalent, unless I have a misunderstanding.) But I have been unable to find a clear statement of the kind of trouble you would get into if it wasn't there. The more mathematical resources I've been able to find state that there is no distinguishing between the two from a mathematical perspective, and the Wikipedia article seems to suggest that this is rarely seen outside of computing aside from describing limits.

So why is a negative zero valuable in computing? I'm sure I'm just missing something.

答:
C# Double which conforms to IEEE 754

```<code>    double a = 3.0;
    double b = 0.0;
    double c = -0.0;

    Console.WriteLine(a / b);
    Console.WriteLine(a / c);
</code>```

prints:

```<code>Infinity
-Infinity
</code>```

actually to explain a little...

```<code>Double d = -0.0;
</code>```

This means something a lot closer to d= The Limit of x as x approaches 0- or  The Limit of x as x approaches 0 from the negatives.

To address Philipp's comment...

Basically negative zero means underflow.

There's very little practical use for negative zero if any...

for example, this code (again C#):

```<code>double a = -0.0;
double b = 0.0;

Console.WriteLine(a.Equals(b));
Console.WriteLine(a==b);
Console.WriteLine(Math.Sign(a));
</code>```

yields this result:

```<code>True
True
0
</code>```

To explain informally, All of the special values an IEEE 754 floating point can have (positive infinity, negative infinity, NAN, -0.0) have no meaning in the practical sense. They can't represent any physical value, or any value that makes sense in "real world" calculation. What they mean is basically this:

positive infinity means a an overflow at the positive end a floating point can represent

negative infinity means a an overflow at the positive end a floating point can represent

negative zero means a an underflow and the operands had opposite signs

positive zero may mean a an underflow and the operands had the same sign

NAN means your calculation is complacently undefined, like sqrt(-7), or it doesnt have a limit like  0/0 or like PositiveInfinity/PositiveInfinity

Title: Why is negative zero important?

Body:
C# Double which conforms to IEEE 754

    double a = 3.0;
    double b = 0.0;
    double c = -0.0;

    Console.WriteLine(a / b);
    Console.WriteLine(a / c);

prints:

Infinity
-Infinity

actually to explain a little...

Double d = -0.0;

This means something a lot closer to d= The Limit of x as x approaches 0- or  The Limit of x as x approaches 0 from the negatives.

To address Philipp's comment...

Basically negative zero means underflow.

There's very little practical use for negative zero if any...

for example, this code (again C#):

double a = -0.0;
double b = 0.0;

Console.WriteLine(a.Equals(b));
Console.WriteLine(a==b);
Console.WriteLine(Math.Sign(a));

yields this result:

True
True
0

To explain informally, All of the special values an IEEE 754 floating point can have (positive infinity, negative infinity, NAN, -0.0) have no meaning in the practical sense. They can't represent any physical value, or any value that makes sense in "real world" calculation. What they mean is basically this:

positive infinity means a an overflow at the positive end a floating point can represent

negative infinity means a an overflow at the positive end a floating point can represent

negative zero means a an underflow and the operands had opposite signs

positive zero may mean a an underflow and the operands had the same sign

NAN means your calculation is complacently undefined, like sqrt(-7), or it doesnt have a limit like  0/0 or like PositiveInfinity/PositiveInfinity


答:
Create just one application which has on/off switches for various features. Developing and maintaining one application is much easier than doing the same with two apps. You can think that it won't be a big problem since two applications will be very similar, so there will be a lot of code reuse and as a benefit you'll get two applications, each will be simpler because of lack of switches (which often significantly increase complexity). But code bases eventually diverge (if extreme care is not taken) and code reuse will become more and more difficult.

Having said that, there might be cases where two separate apps are better:

scalability is not the same thing as performance. More scalable application can be slower (even significantly) than non-scalable one on smaller datasets.

scalability and/or other features may complicate deploying/managing the application. For example scalable application may require external database which can be easily scaled. Non-scalable may suffice with just internal database which doesn't have to be deploayed/managed separately

more features often mean also more "scalable" user interface. This might complicate using the light-weight application because it still has the interface which is meant to support the extra features, only without those extra features.

switches for various features greatly increase complexity of the project - now you basically need to test 2^(number of switches) versions of the application. This complexity might be too big to manage and having two separate applications might be simpler. This is quite extreme case though.

Often you can take a middle way - externalize the differences into a separate module which abstracts the differences (basically strategy pattern). For example you can modules with scalable core and non-scalable core which both have the same interface and can be exchanged at will. Other example is that you can have different frontends (user interfaces) which work with the same processing core, each frontend optimized for different use cases (light version vs. feature heavy version).


问:
Do I need to provide the source code for GPL dependencies?
Let's say there is a software that is build on basis of a GPL v2 licensed software. This software is being distributed in binary form. GPL requires that the source code and build instructions are made available for anyone. But is it legal to provide instructions that state something like this: "To compile you also have to download libraries X, Y and Z from their websites"?

Or must all dependencies be included in the source code distribution?

答:
From A Practical Guide to GPL Compliance (emphasis mine):

The most important component to maintaining GPL compliance is inclusion of the complete and corresponding source code in any distributions that you make of GPL’d software.

That means you include the source code for any libraries that you use.  They go on to say that:

Knowing at all times what sources generated a given binary distribution is paramount.  In an unfortunately large number of our enforcement cases, the violating company’s engineering team had difficulty reconstructing the precise sources for a given binary distributed by the company.

In addition:

Too many software projects rely on only one or a very few team members who know how to build and assemble the final released product. Such knowledge centralization not only creates engineering redundancy issues, but it also endangers GPL compliance, which requires you to provide build scripts.

So I'd say that it's pretty clear that the intent of the GPL license is to make it possible for anyone to avail themselves of the freedoms of the license (use, modify, redistribute, etc.) while encountering the fewest practical hurdles.


答:
The GPL v2 has this to say:

3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of
  Sections 1 and 2 above provided that you also do one of the following:

a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1
  and 2 above on a medium customarily used for software interchange; or,

b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of
  physically performing source distribution, a complete machine-readable
  copy of the corresponding source code, to be distributed under the
  terms of Sections 1 and 2 above on a medium customarily used for
  software interchange; or,

c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed
  only for noncommercial distribution and only if you received the
  program in object code or executable form with such an offer, in
  accord with Subsection b above.)

The source code for a work means the preferred form of the work for
  making modifications to it. For an executable work, complete source
  code means all the source code for all modules it contains, plus any
  associated interface definition files, plus the scripts used to
  control compilation and installation of the executable. However, as a
  special exception, the source code distributed need not include
  anything that is normally distributed (in either source or binary
  form) with the major components (compiler, kernel, and so on) of the
  operating system on which the executable runs, unless that component
  itself accompanies the executable.

The relevant language here is that you must include whatever modules the program requires except those for the compiler or kernel. That is an archaic way of saying "no need to distribute standard libraries that are part of a standard development toolchain."

This indicates that you must convey (at least make available) whatever you need to compile the code except for the compiler, associated tools, and standard libraries.

To answer your question directly: yes, you are required to make available third-party libraries so that anyone can download your program and compile it without needing to grab libraries from other sources.

In my experience this rarely happens. My educated guess is "nobody cares" because it is so easy to go grab other libraries, or they may come preinstalled in a typical Linux system.

Title: Do I need to provide the source code for GPL dependencies?

Body:
Let's say there is a software that is build on basis of a GPL v2 licensed software. This software is being distributed in binary form. GPL requires that the source code and build instructions are made available for anyone. But is it legal to provide instructions that state something like this: "To compile you also have to download libraries X, Y and Z from their websites"?

Or must all dependencies be included in the source code distribution?


问:
Understanding blocking and non-blocking frameworks
Could someone help me understand what is the difference between blocking and non-blocking frameworks?

So far, I understand that when request comes to a blocking framework it creates new thread for that request and processes it, if second request comes to server before the first one has finished it execution it will create another thread.

Now for non-blocking server when one request comes to a web server it wont create a thread but will wait for a request to complete and in case another request comes to a framework before first has finished executing it wont be able to process it before it returns result of first request.

Another question I have is what exactly happens with second request in non-blocking framework does browser just waits for server to respond till request expires or something else happens?

If non-blocking frameworks can process one request at a time, how can web apps built with non-blocking frameworks process more requests than ones built with blocking frameworks (assuming that above statement is correct). Does that mean that for each instance of blocking framework on our server we would actually deploy multiple instances of non-blocking framework?

答:
What is a Non-Blocking framework?

Explain it like I'm 5:  Imagine you want to make a deposit to your bank account.  You walk in and notice there is no queue of people waiting line.

The sign over the bank teller says: "Non-blocking teller".  You walk up and ask the teller to process your deposit, the teller responds: "I'm busy with another transaction, try again later".

You wait some amount of time, and try again later. You make your own decision if you want to keep trying to get your transaction processed, or not.  You try and the 5th time you try, your transaction is processed immediately.

The "Blocking teller" would have told you to "stand in line as a FIFO queue".  The non blocking teller says: "try again later".  Which bank teller would you prefer to interact with, and why?

Definition: A non-blocking framework provides a service and returns a result immediately instead of expecting the other programs requesting a resource to wait.

In other words: When a client side program makes a call to the framework, the call will always return immediately with whatever response it has for you without expecting you to sit there waiting for something.  This guarentee is nice for programmers who don't want to worry about programming around the situation where the framework expects us to wait for 5 minutes.

A concrete programming example: Suppose your framework wants to provide mutually exclusive access to a file saved on the server side.  An example of a "non blocking call" would be try_lock.  If the client side wants access to the file, the framework responds: "No, try again later", rather than putting you in a queue and expecting you to sit there waiting.

The client side keeps trying for the lock, until it gets it, once it gets it, it does its business and unlocks it.  The benefit of this is that whatever you try has an immediate effect.

Drawbacks to non blocking frameworks:  When there is too much work to be done for a non-blocking framework, clients are denied, and fairness is not enforced, only the clients who badger the server the most get access to the service.   It's not fair.

ContentLicense: CC BY-SA 4.0


问:
How to add a property to an object you can't change?
Let's say that I'm using a library and I would like to add a property that doesn't exist to an existing class. In this case, I'd like to add Color as a property of Fruit.

```<code>namespace Library
{
    public class Fruit : System.IDisposable
    {
        public string Name { get; set; }

        public void Dispose() { }
    }
}
</code>```

Is there any way to do that in C#?

Example that won't work

My first thought was partial classes but that would require that both classes would have the word partial in them. So that's not doable.

```<code>namespace Library
{
    public partial class Fruit
    {
        public Color Color { get; set; }
    }
}
</code>```

This seems awful

I could inherit Fruit and create a MyFruit class. This seems clunky to me. I'd much rather call Person.Fruits.FirstOrDefault().Color than to have to create a MyPerson object that inherits from Person and then call MyPerson.MyFruits.FirstOrDefault().Color.

```<code>namespace Library
{
    public class MyFruit : Fruit
    {
        public Color Color { get; set; }
    }
}

namespace Library
{
    public class MyPerson : Person
    {
        public MyFruit MyFruit { get; set; }
    }
}
</code>```

答:
You don't.

The closest you can get to is Java style properties via extension methods, though if you actually need to store the color (as opposed to map it in the function) it can be difficult to do correctly since the extension class would hold references to the actual class if you do it the naive way.

The alternative is using standard inheritance or composition to extend the class, which you already mentioned.


答:
You need to keep in mind that in FPU arithmetics, 0 doesn't necessarily has to mean exactly zero, but also value too small to be represented using given datatype, e.g.

```<code>a = -1 / 1000000000000000000.0
</code>```

a is too small to be represented correctly by float (32 bit), so it is "rounded" to -0.

Now, let's say our computation continues:

```<code>b = 1 / a
</code>```

Because a is float, it will result in -infinity which is quite far from the correct answer of -1000000000000000000.0

Now let's compute b if there's no -0 (so a is rounded to +0):

```<code>b = 1 / +0
b = +infinity
</code>```

The result is wrong again because of rounding, but now it is "more wrong" - not only numerically, but more importantly because of different sign (result of computation is +infinity, correct result is -1000000000000000000.0).

You could still say that it doesn't really matter as both are wrong. The important thing is that there are a lot of numerical applications where the most important result of the computation is the sign - e.g. when deciding whether to turn left or right at the crossroad using some machine learning algorithm, you can interpret positive value => turn left, negative value => turn right, actual "magnitude" of the value is just "confidence coefficient".

Title: Why is negative zero important?

Body:
I'm confused about why we care about different representations for positive and negative zero.

I vaguely recall reading claims that having a negative zero representation is extremely important in programming that involves complex numbers. I've never had the opportunity to write code involving complex numbers, so I'm a little baffled about why this would be the case.

Wikipedia's article on the concept isn't especially helpful; it only makes vague claims about signed zero making certain mathematical operations simpler in floating point, if I understand correctly. This answer lists a couple of functions that behave differently, and perhaps something could be inferred from the examples if you're familiar with how they might be used. (Although, the particular example of the complex square roots looks flat out wrong, since the two numbers are mathematically equivalent, unless I have a misunderstanding.) But I have been unable to find a clear statement of the kind of trouble you would get into if it wasn't there. The more mathematical resources I've been able to find state that there is no distinguishing between the two from a mathematical perspective, and the Wikipedia article seems to suggest that this is rarely seen outside of computing aside from describing limits.

So why is a negative zero valuable in computing? I'm sure I'm just missing something.

Body:
You need to keep in mind that in FPU arithmetics, 0 doesn't necessarily has to mean exactly zero, but also value too small to be represented using given datatype, e.g.

a = -1 / 1000000000000000000.0

a is too small to be represented correctly by float (32 bit), so it is "rounded" to -0.

Now, let's say our computation continues:

b = 1 / a

Because a is float, it will result in -infinity which is quite far from the correct answer of -1000000000000000000.0

Now let's compute b if there's no -0 (so a is rounded to +0):

b = 1 / +0
b = +infinity

The result is wrong again because of rounding, but now it is "more wrong" - not only numerically, but more importantly because of different sign (result of computation is +infinity, correct result is -1000000000000000000.0).

You could still say that it doesn't really matter as both are wrong. The important thing is that there are a lot of numerical applications where the most important result of the computation is the sign - e.g. when deciding whether to turn left or right at the crossroad using some machine learning algorithm, you can interpret positive value => turn left, negative value => turn right, actual "magnitude" of the value is just "confidence coefficient".


问:
Using System.err.println() for debugging in Java

System.err

The "standard" error output stream. This stream is already open and
  ready to accept output data. Typically this stream corresponds to
  display output or another output destination specified by the host
  environment or user. By convention, this output stream is used to
  display error messages or other information that should come to the
  immediate attention of a user even if the principal output stream, the
  value of the variable out, has been redirected to a file or other
  destination that is typically not continuously monitored.

So, I usually use System.err.println() while debugging (mostly web apps) in my development environment. In most IDEs like Eclipse. System.out and System.err are both print in the same console and since System.err are uniquely highlighted in a different color (red), I find it easier to print out values using System.err as they are visually highlighted and I don't have to go through the whole log to search for my outputs. This always doesn't require me to start my server in debug mode and for cases where I can just determine looking at certain values that I've outputted and not actually break the execution to inspect for states.

Is this a really bad practice, should I not do it? I've been doing this for a long time and I find this very helpful, at times.

答:
I don't think logging is helpful. I think it's a waste of time, since what actually matters, uncaught exceptions, is already printed.

From your quote about System.err:

By convention, this output stream is used to display error messages or other information that should come to the immediate attention of a user even if the principal output stream, the value of the variable out, has been redirected to a file or other destination that is typically not continuously monitored.

Well, we are developers who just want to check some variables while debugging. Those are not errors.
That's why I use System.out, which is still readable if you avoid useless output.


答:
The best answer is you shouldn't.  But if you don't mind a performance overhead, you can do something similar with ConditionalWeakTable<,>:

```<code>public static class ExtraFruitProperties
{
    private static readonly ConditionalWeakTable<Fruit, Color> _colors = new ConditionalWeakTable<Fruit, Color>();

    public static void SetColor(this Fruit @this, Color color)
    {
         _colors.Remove(@this);
         _colors.Add(@this, color);
    }

    public static Color GetColor(this Fruit @this)
    {
         Color color;
         if(_colors.TryGetValue(@this, out color)) return color;
         return default(Color);
    }
}
</code>```

Title: How to add a property to an object you can't change?

Body:
Let's say that I'm using a library and I would like to add a property that doesn't exist to an existing class. In this case, I'd like to add Color as a property of Fruit.

```<code>namespace Library
{
    public class Fruit : System.IDisposable
    {
        public string Name { get; set; }

        public void Dispose() { }
    }
}
</code>```

Is there any way to do that in C#?

Example that won't work

My first thought was partial classes but that would require that both classes would have the word partial in them. So that's not doable.

```<code>namespace Library
{
    public partial class Fruit
    {
        public Color Color { get; set; }
    }
}
</code>```

This seems awful

I could inherit Fruit and create a MyFruit class. This seems clunky to me. I'd much rather call Person.Fruits.FirstOrDefault().Color than to have to create a MyPerson object that inherits from Person and then call MyPerson.MyFruits.FirstOrDefault().Color.

```<code>namespace Library
{
    public class MyFruit : Fruit
    {
        public Color Color { get; set; }
    }
}

namespace Library
{
    public class MyPerson : Person
    {
        public MyFruit MyFruit { get; set; }
    }
}
</code>```

Body:
The best answer is you shouldn't.  But if you don't mind a performance overhead, you can do something similar with ConditionalWeakTable<,>:

public static class ExtraFruitProperties
{
    private static readonly ConditionalWeakTable<Fruit, Color> _colors = new ConditionalWeakTable<Fruit, Color>();

    public static void SetColor(this Fruit @this, Color color)
    {
         _colors.Remove(@this);
         _colors.Add(@this, color);
    }

    public static Color GetColor(this Fruit @this)
    {
         Color color;
         if(_colors.TryGetValue(@this, out color)) return color;
         return default(Color);
    }
}


问:
Algorithm to generate parallel path inside polygon
A path is defined as a list of 2D points (x,y) (and may be open or closed).

Given a path p, what is an algorithm which generates a path p' which is "inside" p and at constant offset to p i.e. each line segment of p' is parallel to the corresponding line segment of p.

答:
I don't know of a formal algorithm. I implemented this once long ago. The algorithm depends on not only the enumeration of line segments, but also on the "side" of the line segments that the parallel path is meant to follow. Assume you know the "side".

My set of line segments was (as you describe) a list of 2d points (x,y).

For each point p, I looked at the two vectors formed by using its
previous and subsequent points in the enumeration.

I found the angle between these two vectors using a dot product

I split that angle in half, making a third vector that bisected the first two.
(This is where knowing "what side of the path" comes in)

By setting the magnitude of this third vector to the "constant offset", you come up with a point p' on the "parallel path" that corresponds to point p

For the endpoints of a path that is not closed, I simply used the constant offset to set the magnitude of a vector perpendicular to the vector formed by the endpoint and its neighboring point.


答:
The reason is simpler than usual

Of course there is a lot of hacks which are looking really nice and they are useful (like rounding to -0.0 or +0.0 but assume we have a representation of signed int with a minus/plus sign at the beginning (i know that is resolved by U2 binary code in integers usually but assume a less complex representation of double):

```<code>0 111 = 7
^ sign
</code>```

What if there is negative number?

```<code>1 111 = -7
</code>```

Okay, that simple. So let's represent 0:

```<code>0 000 = 0
</code>```

That's fine too. But what about 1 000? Is it has to be a forbidden number? Better no.

So let's assume there is two types of zero:

```<code>0 000 = +0
1 000 = -0
</code>```

Well, that will simplify our calculations and fortuately give a some rounding up additional features. So the +0 and -0 are coming from just binary representation issues.

Title: Why is negative zero important?

Body:
The reason is simpler than usual

Of course there is a lot of hacks which are looking really nice and they are useful (like rounding to -0.0 or +0.0 but assume we have a representation of signed int with a minus/plus sign at the beginning (i know that is resolved by U2 binary code in integers usually but assume a less complex representation of double):

0 111 = 7
^ sign

What if there is negative number?

1 111 = -7

Okay, that simple. So let's represent 0:

0 000 = 0

That's fine too. But what about 1 000? Is it has to be a forbidden number? Better no.

So let's assume there is two types of zero:

0 000 = +0
1 000 = -0

Well, that will simplify our calculations and fortuately give a some rounding up additional features. So the +0 and -0 are coming from just binary representation issues.


答:
I think what you are looking for is extreme programming.

Most of the times if we are unable to complete the work in a given release

The triangle of time / scope / cost allows you to increase one if you reduce another, not just force all three to be better, this results in the hidden fourth variable taking a hit i.e. quality.

additional teams are brought in to complete the work and this
  complicates it further

Adding more software developers to an already late software project does not make it finish sooner, see Brooks - Mythical Man Month. The hard part about building software is not typing the solution into the computer, but the people surrounding the problem space.

When a team member leaves the company, he takes away the knowledge and due to little or no documentation, the new team member (BA) has no where to refer to, and due to this the entire team suffers

By pair programming you can spread knowledge around your team. The idea is to have specialists, who are experts in a field, but not roles, where people are forced to stay within their designated areas.

In this case, is it the responsibility of the BA to give proper acceptance criteria or the scrum team's responsibility to navigate through such difficulties?

It is the responsibility of everyone on the team to collectively take ownership of solving the problem. It sounds like the Product Owner is not doing a good enough job on breaking down customer requirements into user stories but this might mean you just need to help them understand your technology more.

The developers and the QAs are always in a time crunch due to the discovery process involved during the sprint. The quality also suffers due to this.

Of course the quality suffers, this isn't rocket science. XP has a strict 40 hours work week. A crunch is sometimes necessary, although a lot less than most 1980's MBAs think, when an external deadline moves. But most of the time if you are being forced into a crunch then your business analysts aren't doing their job properly and are tring to get you to help cover that up for them and it will result in burnt out developers who produce worse code and are less productive in the long run.

Title: Agile Scrum - Importance of requirement gathering and documentation and suggestions to improve

Body:
We are using agile scrum methodologies for developing and maintaining a product. Since we are a product company, we do not work with customer directly on day to day basis, but instead communicate with BA who gives us the requirement.

We are facing following challenges and wondering how to overcome them.

The product we are working on is complex and the BAs and the team is relatively new. When we get the requirement it is very high level. The acceptance criteria is also very high level and involves lot of discovery during the sprint. There are many touch-points which were never considered in the acceptance criteria and they always come up when the developer looks at the code. Due to this the sprint planning becomes very hard and takes away lot of development and QA time in discussion with BA on what should happen for each scenario.

Is this acceptable in agile scrum? If so, how can we plan in a better way given very little clarity on "WHAT" has to be done. Most of the times if we are unable to complete the work in a given release, additional teams are brought in to complete the work and this complicates it further.

I believe most of it is due to very little documentation and whatever we have is scattered. When a team member leaves the company, he takes away the knowledge and due to little or no documentation, the new team member (BA) has no where to refer to, and due to this the entire team suffers. So how much documentation is essential?

In this case, is it the responsibility of the BA to give proper acceptance criteria or  the scrum team's(BA is also part of our scrum team) responsibility to navigate through such difficulties? The developers and the QAs are always in a time crunch due to the discovery process involved during the sprint. The quality also suffers due to this.

We also have to spare additional capacity for the bugs, customer cases. So at the end of it, it is usually the developers and QA who have to put extra hours to meet all the commitments.


问:
What is the minimum practical definition for the Scheme language?
What is the smallest practical set of primitives that can be used to define the Scheme language?

For example, map can be defined as

```<code>(define (map proc lis)
   (cond ((null? lis)
          '())
         ((pair? lis)
          (cons (proc (car lis))
                (map proc (cdr lis))))))
</code>```

Can the functions in this definition be similarly reduced to smaller primitives?

答: The answer to this question is going to depend almost entirely on your italicized term, practical. You probably feel very different than I do about what exactly constitutes practicality. Obivously, you can strip everything down to the lambda calculus, and you wind up with a turing-complete language including only lambda, varrefs, and application.


答:
Use a separate hash table to add properties to objects you don't control, and use
your own access functions to wrap whatever behavior you want to appear to be uniform
with respect to your added properties.

If garbage collection is likely to be a concern (your extra properties implemented
this way will prevent the object from being garbage collected) this is perhaps the
only good use of weak eq hash tables.


答:
I don't see what's so "awful" about your last solution. You're given a definition of a Fruit, but you want it to be more descriptive. So you define an extension of the original definition with your Color addition.

Really, why do you prefer:

```<code>    Person.Fruits.FirstOrDefault().Color
</code>```

over:

```<code>    MyPerson.MyFruits.FirstOrDefault().Color
</code>```

Only difference is the 'My' prefix which is purely cosmetic. You could also have a more descriptive name like ColoredFruit.

Body:
I don't see what's so "awful" about your last solution. You're given a definition of a Fruit, but you want it to be more descriptive. So you define an extension of the original definition with your Color addition.

Really, why do you prefer:

    Person.Fruits.FirstOrDefault().Color

over:

    MyPerson.MyFruits.FirstOrDefault().Color

Only difference is the 'My' prefix which is purely cosmetic. You could also have a more descriptive name like ColoredFruit.