答:
The nice thing about callbacks is there's no global state there, and passing parameters to them is trivial. If you have a function download(URL, callback: (FileData)->void) then you can know that's a self-contained higher-order function which essentially lets you construct a "grab this and do that" function. You can be sure your code flow is exactly as you expect, because nobody else even has a handle on that callback, and that callback doesn't know about anything but the parent function's given parameters. That makes it modular and easy to test.

If you now want to download 5 files in parallel and do different things, you need only fire five of these functions off with the appropriate callback functions. In a language with good anonymous function syntax this can be incredibly powerful.

Events, on the other hand, are more designed for notifying 1..* users of some state change. If you fire a "download complete" event at the end of download(URL), which launches processDownload() which knows where to find the data, you're tying your implementations of things to a larger amount of state. How do you parallelise downloads now? How do you handle the different downloads differently? Is download(URL, eventId) elegant? Is downloadAndDoThing(URL) elegant? Hardly.

Of course, as with all things, you are making tradeoffs. Nested callbacks can make the order of execution of code more confusing, and the lack of easy global accessability makes it a poor choice for anything where you do have a 1..* relationship between producer and consumer. You have a harder time passing data around, but if you can get away with not having additional state then that's usually a benefit anyway.

Regardless. You're coding in node.js where callbacks are idiomatic, and the language and libraries are designed around their use. Whether you see advantages in one design or another, I think it's almost always true that writing idiomatic code in any language is going to have far greater support and make your life much easier than trying to circumvent it.


问:
Is there a math theory or a model behind programming languages design
I'm trying to understand if a programming languages design is built on some a theoretical model.

I came across few links that was describing Programming Language Theory, Lambda Calculus, Formal Languages and Turing machine.

I got lost in all that theory, what I understood from all that is that (any ..?) programming language is built on lambda calculus, that boils down to few language constructs.

is my understanding correct?

are there any models other than lambda calculus?

if the languages shares the same model, can we use that to convert a source code from one language to the other?

答:
You're partially right.

Lambda calculus is base for understanding how functional programming WORK.

Turing machine is base for understanding how imperative programming WORK.

Formal languages are ways of WRITING programms.

Lambda calculus and Turing machine are totally different, but both in some way do the same as sentence "if you add two and three, you get five" for math.

Formal languages are ways of saying "2+2 means 'add two and two'".

PS. @SK-logic: yes, you are right. This above is simplification, realy close to being false. Still, answer was written for complete newbie in topic, it was intended to give him SLIGHTEST idea, so he can choose what is more interesting for him, and where he want to start learning.

ContentLicense: CC BY-SA 3.0


问:
How should this relationship be structured in a relational database?
In a recent project I was asked to implement an events system. An Event had to have a Location which was originally specced out as simply a physical location with some optional extra notes. Then the spec changed (as they have a habit of doing) and we needed to have "online" events too. These would not have a physical address but would still need notes on how to attend (e.g. the URL, joining instructions).

We decided to adapt the existing Location table by adding an IsOnline field and it wound up looking like this:

```<code>+-----------------+     +---------------+
|      Event      |     |    Location   |
+-----------------+     +---------------+
| Id              |  .--+ Id            |
| Name            |  |  | Name          |
| Summary         |  |  | Address       |
| Date            |  |  | Postcode      |
| Capacity        |  |  | IsOnline      |
| LocationId      +--'  | Notes         |
+-----------------+     +---------------+
</code>```

An example of a physical and an online entry in the Location table look like this:

```<code>+----+----------------+--------------------------------------+----------+----------+---------------------------------------+
| Id |      Name      |               Address                | Postcode | IsOnline |                 Notes                 |
+----+----------------+--------------------------------------+----------+----------+---------------------------------------+
|  1 | Physical Event | 10 Downing Street, London            | SW1A 2AA |        0 | Ask the policeman to let you in       |
|  2 | Online Event   | http://programmers.stackexchange.com | null     |        1 | You will need a stackexchange account |
+----+----------------+--------------------------------------+----------+----------+---------------------------------------+
</code>```

It works (currently) for our simple use case but it is clearly a bit of a hack and it got me thinking - what would be the correct, normalized way to model this kind of relationship (Where an entity must have an A or a B but not both)?

答:
You shouldn't burden your relational model with distinctions it can't make.

By all means exploit all the power it brings you to ensure good design and good data; foreign keys, NULL constraints etc. are valid and useful components to build reliable systems on. However, a NULL constraint is a relatively crude language element; it can be on or off, and that's pretty much it. It is almost guaranteed that sooner or later your model will have to reflect more complicated conditions, and this is the point that you have just reached. It follows that you must enforce some conditions on a higher implementation level, and never mind that the data model cannot follow suit.

(That doesn't mean that you should abandon the possibilities that SQL gives you, even when they end up being double-checked by business logic. Defense in depth adds a very real value to many designs - use whatever is worthwhile to defend yourself!)


答:
You can keep the fields common to all locations in that table, but have multiple tables handling different location types. You could add a third location-type table like GeoSpacialAddress which would have Lat/Longs instead of physical addresses.

This prevents having a lot of null fields. You just have to be aware in your querying and may need to use a UNION to get a complete list of all types addresses. "IsOnline" can be determined by a location having one or more OnlineAddress records.

Location

ID

Name

Notes

OnlineAddress

ID

LocationID

URL

PhysicalAddress

ID

LocationID

Address

PostCode

GeoSpacialAddress

ID

LocationID

Lat

Long

Title: How should this relationship be structured in a relational database?

Body:
In a recent project I was asked to implement an events system. An Event had to have a Location which was originally specced out as simply a physical location with some optional extra notes. Then the spec changed (as they have a habit of doing) and we needed to have "online" events too. These would not have a physical address but would still need notes on how to attend (e.g. the URL, joining instructions).

We decided to adapt the existing Location table by adding an IsOnline field and it wound up looking like this:

```<code>+-----------------+     +---------------+
|      Event      |     |    Location   |
+-----------------+     +---------------+
| Id              |  .--+ Id            |
| Name            |  |  | Name          |
| Summary         |  |  | Address       |
| Date            |  |  | Postcode      |
| Capacity        |  |  | IsOnline      |
| LocationId      +--'  | Notes         |
+-----------------+     +---------------+
</code>```

An example of a physical and an online entry in the Location table look like this:

```<code>+----+----------------+--------------------------------------+----------+----------+---------------------------------------+
| Id |      Name      |               Address                | Postcode | IsOnline |                 Notes                 |
+----+----------------+--------------------------------------+----------+----------+---------------------------------------+
|  1 | Physical Event | 10 Downing Street, London            | SW1A 2AA |        0 | Ask the policeman to let you in       |
|  2 | Online Event   | http://programmers.stackexchange.com | null     |        1 | You will need a stackexchange account |
+----+----------------+--------------------------------------+----------+----------+---------------------------------------+
</code>```

It works (currently) for our simple use case but it is clearly a bit of a hack and it got me thinking - what would be the correct, normalized way to model this kind of relationship (Where an entity must have an A or a B but not both)?


问:
How to estimate cost with very little information?
I have a client who wants a web app that will let him do the following (as he told me word for word):

User registration

Recurring payments for users

Online conference room reservation

I am supposed to give him a cost estimate very fast and I don't have any time to do any deeper requirements analysis! How would you deal with that kind of situation? Should I just give a very broad estimate and work out the exact requirements once he has accepted the estimate? At which point do you usually gather requirements, after or before getting a contract?

答: Offer to do the analysis and design in 2 weeks for a fixed price (with obligations on their part to communicate and review in a timely manner); they can take the output from that and bid it to other vendors if they like, but giving them a number based on three bullet points will either be massive sticker shock to them, or serious underpayment for you.</p>

ContentLicense: CC BY-SA 2.5


问:
Doesn't dockerfile initial configuration defeat one of the key arguments of docker?
I'm very excited about Docker, if you're a developer in a big project you've suffered too much at the machine hands of environments and multi-platforms.

One of the key selling points of Docker is that by snapshotting/committing image state you avoid the risk of building an environment with a different version, possibly incompatible, of a given dependency. I get that, great!

Doing the tutorials on dockerfile, isn't this exactly the same concept as running npm, chef or bower. Of course unix libraries would be more stable than most of the ones found on these library stores I just mentioned but isn't this the same workflow that docker is fighting?

Isn't the goal to tailor a container to your needs and then commit it's state to then multiply at will?

Am I missing something trivial, or this is the case? Does docker still gives you a chance for building up images from script files and by that it steps on it's toes?

答:
I had a discussion at a conference this week about the same thing, having more or less the same question.

Because there seems to be some overlap, a good practice, based on my understanding is that with a Dockerfile you will get the container to an initial state from which you can then utilise a DevOps framework (or npm or bower).

Dockerfile might also speed the things up, since slices might inherit from a better initial state, in case this is required. Example if you have installed "g++" from within the Dockerfile, each instance you spawn, will not need to download it, which will be the case for Chef, running on a "bare metal" machine.

At last, I think that we need to consider where there is no overlap, for things that can be done only on from within the Dockerfile, like which ports to map, etc. So it is necessary at least for this. As an example:

```<code>FROM ubuntu
</code>```

I suppose you cannot say to a DevOps tool: "make this node an ubuntu instance".

I am a bit new in this territory and also this territory is new, so the validity of this answer might fluctuate with time.

Body:
I had a discussion at a conference this week about the same thing, having more or less the same question.

Because there seems to be some overlap, a good practice, based on my understanding is that with a Dockerfile you will get the container to an initial state from which you can then utilise a DevOps framework (or npm or bower).

Dockerfile might also speed the things up, since slices might inherit from a better initial state, in case this is required. Example if you have installed "g++" from within the Dockerfile, each instance you spawn, will not need to download it, which will be the case for Chef, running on a "bare metal" machine.

At last, I think that we need to consider where there is no overlap, for things that can be done only on from within the Dockerfile, like which ports to map, etc. So it is necessary at least for this. As an example:

FROM ubuntu

I suppose you cannot say to a DevOps tool: "make this node an ubuntu instance".

I am a bit new in this territory and also this territory is new, so the validity of this answer might fluctuate with time.


问:
Where should tests' log files go?
My application has a rather non-trivial, yet standard, log configuration.

It includes a console appender and a rolling size-based file appender.

When I'm running the tests, thru the IDE or with maven, where is the least surprising place to save the log files?

答:
If I'm checking out a logs on a server at my company I always go straight to the logs folder. In my work place the logs folder is the most intuitive location for logs.

My suggestion is follow how your workplace does it, and if you are starting a new project then create a logs folder.

Title: Where should tests' log files go?


问:
Why do modern websites still insist on archaic username/password requirements?
I was signing up for a website last night, and once again was greeted with the fact that my username cannot contain certain characters (including spaces), and neither could my password.

Is there a historic reason for this, or is there no particular reason?  Passwords that are phrases have higher entropy and are harder to crack than simple letter/number substitution (see image below):

And on the username front, no spaces?  Really?  It's just a string.  I could understand historic concerns against SQL Injection attacks but modern websites are passed that now (I hope!)

答:
Like the other answers stated, legacy reason.

Most web framework can escape it now and handle it.

I had a co worker that didn't know anything about prepared statement and so he coded a javascript regex that would just check for certain illegal characters and complains about it.

So it could also be the programmer lack of knowledge...


问:
How do we keep dependent data structures up to date?
Suppose you have a parse tree, an abstract syntax tree, and a control flow graph, each one logically derived from the one before. In principle it is easy to construct each graph given the parse tree, but how can we manage the complexity of updating the graphs when the parse tree is modified? We know exactly how the tree has been modified, but how can the change be propagated to the other trees in a way that doesn't become difficult to manage?

Naturally the dependent graph can be updated by simply reconstructing it from scratch every time the first graph changes, but then there would be no way of knowing the details of the changes in the dependent graph.

I currently have four ways to attempt to solve this problem, but each one has difficulties.

Nodes of the dependent tree each observe the relevant nodes of the original tree, updating themselves and the observer lists of original tree nodes as necessary. The conceptual complexity of this can become daunting.

Each node of the original tree has a list of the dependent tree nodes that specifically depend upon it, and when the node changes it sets a flag on the dependent nodes to mark them as dirty, including the parents of the dependent nodes all the way down to the root. After each change we run an algorithm that is much like the algorithm for constructing the dependent graph from scratch, but it skips over any clean node and reconstructs each dirty node, keeping track of whether the reconstructed node is actually different from the dirty node. This can also get tricky.

We can represent the logical connection between the original graph and the dependent graph as a data structure, like a list of constraints, perhaps designed using a declarative language. When the original graph changes we need only scan the list to discover which constraints are violated and how the dependent tree needs to change to correct the violation, all encoded as data.

We can reconstruct the dependent graph from scratch as though there were no existing dependent graph, and then compare the existing graph and the new graph to discover how it has changed. I'm sure this is the easiest way because I know there are algorithms available for detecting differences, but they are all quite computationally expensive and in principle it seems unnecessary so I'm deliberately avoiding this option.

What is the right way to deal with these sorts of problems? Surely there must be a design pattern that makes this whole thing almost easy. It would be nice to have a good solution for every problem of this general description. Does this class of problem have a name?

Let me elaborate on the troubles that this problem causes. This issue pops up in various places, whenever two parts of a project operate on graphs, with each graph being a different representation of the same thing that changes while the software is running. It's like making an adapter for an interface, but instead of wrapping a single object or a fixed number of objects, we need to wrap an entire graph of arbitrary size.

Every time I try this I end up with a confusing unmaintainable mess. The control flow of observers can be difficult to follow when it gets complicated, and the algorithm for converting one graph to another is usually tricky enough to follow when its laid out plainly and not spread across multiple classes. The problem is that there seems to be no way to use just a plain, straight-forward graph conversion algorithm when the original graph is changing.

Naturally we can't just use an ordinary graph conversion algorithm directly because that can't respond to changes in any way other than starting from scratch, so what are the alternatives? Perhaps the algorithm could be written in a continuation-passing style where each step of the algorithm is represented as an object with a method for each type of node in the original graph, like a visitor. Then the algorithm can be assembled by composing various simple visitors together.

Another example: Suppose you have a GUI that is laid out like you might in Java Swing, using JPanels and layout managers. You can simplify that process by using nested JPanels in place of complex layout managers, so you end up with a tree of various containers that includes nodes that exist only for layout purposes and are otherwise meaningless. Now suppose that the same tree that is used to generate your GUI is also used in another part of your application, but instead of laying the tree out graphically it is working with a library that will generate an abstract representation tree as a system of folders. In order to use this library, we need to have a version of the tree that doesn't have the layout nodes; the layout nodes need to be flattened into their parent nodes, but the library still needs to be notified each time the tree changes as though the two versions of the tree were a single data structure.

Another way to look at it: The very concept of working with mutable trees violates the Law of Demeter. It wouldn't really be a violation of the law if the tree were a value as parse trees and syntax trees normally are, but in that case there would be no problem since nothing would need to be kept up-to-date. So then this problem exists as a direct result of violating the Law of Demeter, but how do you avoid that in general when your domain seems to be about manipulating trees or graphs?

The Composite pattern is a wonderful tool for turning a graph into a single object and obeying the Law of Demeter. Is it possible to use the Composite pattern to effectively turn one kind of tree into another? Can you make a Composite parse tree so that it acts like an abstract syntax tree and even a control flow graph? Is there a way to do it without violating the single responsibility principle? The Composite pattern tends to cause classes to absorb every responsibility that they touch, but perhaps it could be combined with the Strategy pattern somehow.

答:
Another representation of problem - you have some data (graph) and different representations of it (e.g. layout panels/tree view). You want to be sure, that each representation is consistent with other representations.

So, why dont you try and come up with most basic representation and turn each other representation to view of basic one? Then it's enough to change basic one, and views will be still consistent.

On example of layout:
First representation is, lets say:

```<code>panelA(
    panelB(
        panelC(
            widget1
            widget2
        )
        panelD(
            widget3
        )
    )
    widget4
)
</code>```

So, you turn it to "simpler" representation, list of following tuples:

```<code>[
    (panelA, panelB, panelC, widget1),
    (panelA, panelB, panelC, widget2),
    (panelA, panelB, panelD, widget3),
    (panelA, widget4),
]
</code>```

Then, while using this graph with Swing, you create view, that turn representation above to specialized tree, and when using with tree view, you have view that returns to you only list of last elements of tuple.

What does "simple" or "basic" mean? Most important - it must be easy to turn to any view (so that computing each view is cheap). Also, it must be easy to modify from any view.

Lets say, that now we want to modify this structure using layout view. It must translate call "panelC.parent = panelD" to "find any list that has panelD in it, find all lists that contain panelC, replace all elements of those list, that go before panelC with part of first list before panelD with it".

As other people pointed out - Observer MAY be useful.

If we're talkin about parse trees/AST/control flow graphs, we dont have to notify any view that graph changed, because when using it you will inspect it, and inspection will dynamically turn "basic" representation to view representation.

If we're talkin Swing, change to one view has to be notified in other views, so things that user can see change.

In the end - this is very case-specific question. I'd say that full solution will strongly differ when you use it for layout and for language analysis, and fully generic sollution will be ugly and expensive as hell.

PS. Representation above is ugly, created ad-hoc, etc. It is intended only to show concept, not real-life solution.

Body:
Another representation of problem - you have some data (graph) and different representations of it (e.g. layout panels/tree view). You want to be sure, that each representation is consistent with other representations.

So, why dont you try and come up with most basic representation and turn each other representation to view of basic one? Then it's enough to change basic one, and views will be still consistent.

On example of layout:
First representation is, lets say:

panelA(
    panelB(
        panelC(
            widget1
            widget2
        )
        panelD(
            widget3
        )
    )
    widget4
)

So, you turn it to "simpler" representation, list of following tuples:

[
    (panelA, panelB, panelC, widget1),
    (panelA, panelB, panelC, widget2),
    (panelA, panelB, panelD, widget3),
    (panelA, widget4),
]

Then, while using this graph with Swing, you create view, that turn representation above to specialized tree, and when using with tree view, you have view that returns to you only list of last elements of tuple.

What does "simple" or "basic" mean? Most important - it must be easy to turn to any view (so that computing each view is cheap). Also, it must be easy to modify from any view.

Lets say, that now we want to modify this structure using layout view. It must translate call "panelC.parent = panelD" to "find any list that has panelD in it, find all lists that contain panelC, replace all elements of those list, that go before panelC with part of first list before panelD with it".

As other people pointed out - Observer MAY be useful.

If we're talkin about parse trees/AST/control flow graphs, we dont have to notify any view that graph changed, because when using it you will inspect it, and inspection will dynamically turn "basic" representation to view representation.

If we're talkin Swing, change to one view has to be notified in other views, so things that user can see change.

In the end - this is very case-specific question. I'd say that full solution will strongly differ when you use it for layout and for language analysis, and fully generic sollution will be ugly and expensive as hell.

PS. Representation above is ugly, created ad-hoc, etc. It is intended only to show concept, not real-life solution.


问:
Intersection of geometric entities
I was trying to design a small C++ geometric API for learning purposes, but came across a problem when dealing with intersections of geometric entities. For example, the intersection of a line and a sphere can have three different types: a couple of points, a point or nothing at all. I found several ways to deal with this problem, but I don't know which of them seems to be the best:

CGAL Object return type

I first tried to see what was done in other geometric libraries. The most complete I can found was CGAL. In CGAL, intersection functions return an Object which is a generic type that can hold anything (like boost::any). Then, you try to assign the Object to a value of another type, here is an example:

```<code>void foo(CGAL::Segment_2<Kernel> seg, CGAL::Line_2<Kernel> line)
{
    CGAL::Object result;
    CGAL::Point_2<Kernel> ipoint;
    CGAL::Segment_2<Kernel> iseg;

    result = CGAL::intersection(seg, line);
    if (CGAL::assign(ipoint, result)) {

        // handle the point intersection case.

    } else if (CGAL::assign(iseg, result)) {

        // handle the segment intersection case.

    } else {

       // handle the no intersection case.
    }
}
</code>```

By the way, the assign function uses dynamic_cast to check whether the two variables are assignable (everybody loves RTTI).

Union return type

A union can also be used as a return type, but it means using an id for every geometric type and having some kind of variant type to cover all the problems.

```<code>struct Variant
{
    int id;
    union
    {
        Point point;
        std::pair<Point, Point> pair;
    };
};

int main()
{
    Point point;
    std::pair<Point, Point> pair;

    Variant v = intersection(Line{}, Sphere{});
    if (v.id == ID_POINT)
    {
        point = v.point;
        // Do something with point
    }
    else if (v.id == ID_VARIANT)
    {
        pair = v.pair;
        // Do something with pair
    }
    else
    {
        // No intersection
    }
}
</code>```

Avoiding the "no intersection" problem

In order to split the "no intersection" case from the main problem, some libraries used to require the user to check whether there was an intersection before trying to find what was the intersection return type. It looked like this:

```<code>Line line;
Sphere sphere;

if (intersects(line, sphere))
{
    auto ret = intersection(line, sphere);
    // Do something with ret
}
</code>```

Another way to split the "no intersection" case from the rest of the problem would be to use an optional type:

```<code>std::optional<...> ret = intersection(Line{}, Sphere{});
if (ret)
{
    // Do something with ret
}
</code>```

Using exceptions to control the "return" type

I can already see some of you crying.

Yet another way to handle that problem of having different return types would be to throw the results instead of returning them. The "no intersection" return could still use one of the techniques from the previous paragraph:

```<code>try
{
    intersection(Line{}, Sphere{});
}
catch (const Point& point)
{
    // Do something with point
}
catch (const std::pair<Point, Point>& pair)
{
    // Do something with pair
}
// Can still catch errors (or "no intersection" special type?)
</code>```

Having a "main" return type, throwing the other ones

Another way would be to have a "main" return type for the intersection, let's say std::pair<Point, Point> and considering the other return types as "exceptional"; it gives more meaning to the use of exceptions while this is still not quite an error gestion. On the other hand, it could seem strange to handle a "main" type differently than the others...

```<code>try
{
    auto pair = intersection(Line{}, Sphere{});
    // Do something with pair
}
catch (const Point& point)
{
    // The chances for a sphere and a line to meet at
    // a single point are so small that the case can
    // already be considered exceptional.
    // ...
}
</code>```

I purposedly left the errors gestion and the "no intersection" case out of this last example since many techniques already described could be used to handle it and I don't want the number of examples to be exponential. Here is one though:

```<code>try
{
    // res is optional<pair<Point, Point>>
    if (auto res = intersection(Line{}, Sphere{}))
    {
        std::cout << "intersection: two points" << '\n'
                  << std::get<0>(*res).x() << " "
                  << std::get<0>(*res).y() << '\n'
                  << std::get<1>(*res).x() << " "
                  << std::get<1>(*res).y() << '\n';
    }
    else
    {
        std::cout << "no intersection" << '\n';
    }
}
catch (const Point& point)
{
    // Exceptional case
    std::cout << "intersection: one point" << '\n'
              << point.x() << " "
              << point.y() << '\n';
}
</code>```

Since the cases where the result is thrown are exceptional, it should not add any runtime overhead to the program if the underlying system uses zero cost exceptions instead of the old SJLJ exceptions system.

The actual question

Well, that was a pretty long introduction, so here is the question: is there an idiomatic solution to the problem in these examples? And if not, are there at least some of the examples which could be banned without a second thought (well, you could give a second thought to the exception examples though...)?

Note: something seemed evident to me but apparently is not: the entity returned by intersection between two geometric entities is not limited to a nothing, a point or a set of point. An intersection can pretty much return any geometric entity. Therefore, using a container to hold the values does not sound like a good idea.

Note 2: time machine note, it happens. After having given it some thought, I would ban exceptions (as most people would do), not because exceptions are inherently bad or not designed for this, but because it makes the expression intersection(...) == SomeShape throw an exception even when the both operands should be equal, which isn't elegant. I guess that I would use a bastard child of a variant instead.

答:
the solutions that I would use or have seen used are:

use a container to return intersection points

more expandable for other geometric entities that may have more intersections but has more overhead

return invalid points when no intersection

you need have a concept of validness for the data type, or have an optional type

use a callback function that gets called each time an intersection is found

this one allows for specific processing without needing to collect all points

the ones I would BAN out of hand are the ones using exceptions, exception handling is slow and many people with crucify you for doing this

Title: Intersection of geometric entities


问:
Which database schema should I pick?
I am planning a database scheme and I hesitate between two designs,
which one should I choose?

For the purpose of the question, assume we want to prepare a database
scheme for an application managing student information in a
university.  There is several modules in the application:

Registry, dealing with general informations, like date of birth,
 health insurance number, and the like.

Library, dealing with the properties useful to the library
 management, like the number of books withdrawn, quitus or
 penalties.

Exams, dealing with the exams taken and the grades.

The modules Library and Exams see the modules Registry, but apart from
that, are independant.

In the application scenario, there is a very large number of students,
data is written once, seldom updated and often read.  Furthermore, the
university expands its system each year, so that modules are added:
Sport, Campus, whatever.  Modules remain quite independant.

I hesitate between two database layouts.

First layout

In the first layout, a table MODULE is associated to each Module and
a UID is used as primary key.  Since each module needs the
properties under control of Registry, we also prepare views of the
join of REGISTRY and MODULE on UID, so that the database knows
we will use this join extensively.

When the system expands, we add a new table and a new view to reflect
this expansion.

Second layout

In the second layout, we create a table with a large number of columns
holding the properties of the various modules.

When the system expands, we add columns to the tables.

Comparison

How does these two approaches compare?

If, say, some software upgrade comes with a major bug, requiring to
downgrade the software and perform later a second upgrade, the first layout seems to
be more robust. Regarding perfomance, the second layout saves a lot of
join operations, but in the first layout, we defined views for the
join operations, thus advertising which complex operations are likely
to happen so that the database system can plan this.  I am far from a
database expert, but am I right if I think that if I put all relevant
information into the hands of the database system, it will be able to
properly carry operation?

If I did not see it used in industrial applications, I would never had
given 2 pences on the second layout.  But since I did, I would like to
get other advises.

答:
If I did not see it used in industrial applications

Seeing others do something in a certain way is not a motivation to do so yourself. Unless you know why those applications were modeled in that way, assume the worst: they did not know what they were doing and the model evolved into this monstrosity over time.

Denormalization has its place, but when used, it usually are very specific optimizations that should only be chosen after careful consideration.

So: do as you should, normalize, go with the first layout.

Title: Which database schema should I pick?


答:
Since you are working on a geometric API, I guess you are not only
interested in intersection of lines and spheres, but have also more
exciting projects.

A natural approach to solve your problem of representing values for
intersection would be to define a type for geometric figures so that
you can represent:

Points

Lines

Spheres

Intersections that you cannot compute (kind of symbolic intersection)

Union of figures

Once you have done that, your intersection algorithm can gobble two
figures and return a figure, computed by using

(A + B + ...).(C + D + ...) = A.C + A.D + B.C + B.D + ...

where I denote the union by + and the intersection by . and where
A, B, C, D and the dots can be a point, a sphere, a line or an
intersection you cannot compute.

To some extent, this approach gives satisfying results and may give
the feeling that it is the right solution to the problem.

There are however a lot of problems with this approach, that you
should be aware of:

Intersection that you cannot compute tend to absorb everything,
i.e. intersecting anything with an intersection that you cannot can
yield an intersection that you cannot compute.

If you intersect two spheres, it yields a circle, that you cannot
compute, not because you cannot find which circle it is, but
because you cannot represent circles.  If you choose to add circles
to your vocabulary, you have to figure out how to compute
intersections with all other figures you may have. So if you have
n base figures you should deal with n^2 type of
intersections. So, choosing a rich vocabulary of figures without
relying on ``intersections you cannot compute'' is not an option.

Even if you restrict yourself to the rassuring realm of algebraic
figures (defined by polynomial equations), computing intersections
is far from trivial.  What you are trying to do is to compute a
minimal set of generators of the reduced ideal defined by your
intersection, so do not expect to be able to do this on more than a
few examples. You should regard anything you are able to compute as
a lucky accident.

Title: Intersection of geometric entities

Body:
I was trying to design a small C++ geometric API for learning purposes, but came across a problem when dealing with intersections of geometric entities. For example, the intersection of a line and a sphere can have three different types: a couple of points, a point or nothing at all. I found several ways to deal with this problem, but I don't know which of them seems to be the best:

CGAL Object return type

I first tried to see what was done in other geometric libraries. The most complete I can found was CGAL. In CGAL, intersection functions return an Object which is a generic type that can hold anything (like boost::any). Then, you try to assign the Object to a value of another type, here is an example:

```<code>void foo(CGAL::Segment_2<Kernel> seg, CGAL::Line_2<Kernel> line)
{
    CGAL::Object result;
    CGAL::Point_2<Kernel> ipoint;
    CGAL::Segment_2<Kernel> iseg;

    result = CGAL::intersection(seg, line);
    if (CGAL::assign(ipoint, result)) {

        // handle the point intersection case.

    } else if (CGAL::assign(iseg, result)) {

        // handle the segment intersection case.

    } else {

       // handle the no intersection case.
    }
}
</code>```

By the way, the assign function uses dynamic_cast to check whether the two variables are assignable (everybody loves RTTI).

Union return type

A union can also be used as a return type, but it means using an id for every geometric type and having some kind of variant type to cover all the problems.

```<code>struct Variant
{
    int id;
    union
    {
        Point point;
        std::pair<Point, Point> pair;
    };
};

int main()
{
    Point point;
    std::pair<Point, Point> pair;

    Variant v = intersection(Line{}, Sphere{});
    if (v.id == ID_POINT)
    {
        point = v.point;
        // Do something with point
    }
    else if (v.id == ID_VARIANT)
    {
        pair = v.pair;
        // Do something with pair
    }
    else
    {
        // No intersection
    }
}
</code>```

Avoiding the "no intersection" problem

In order to split the "no intersection" case from the main problem, some libraries used to require the user to check whether there was an intersection before trying to find what was the intersection return type. It looked like this:

```<code>Line line;
Sphere sphere;

if (intersects(line, sphere))
{
    auto ret = intersection(line, sphere);
    // Do something with ret
}
</code>```

Another way to split the "no intersection" case from the rest of the problem would be to use an optional type:

```<code>std::optional<...> ret = intersection(Line{}, Sphere{});
if (ret)
{
    // Do something with ret
}
</code>```

Using exceptions to control the "return" type

I can already see some of you crying.

Yet another way to handle that problem of having different return types would be to throw the results instead of returning them. The "no intersection" return could still use one of the techniques from the previous paragraph:

```<code>try
{
    intersection(Line{}, Sphere{});
}
catch (const Point& point)
{
    // Do something with point
}
catch (const std::pair<Point, Point>& pair)
{
    // Do something with pair
}
// Can still catch errors (or "no intersection" special type?)
</code>```

Having a "main" return type, throwing the other ones

Another way would be to have a "main" return type for the intersection, let's say std::pair<Point, Point> and considering the other return types as "exceptional"; it gives more meaning to the use of exceptions while this is still not quite an error gestion. On the other hand, it could seem strange to handle a "main" type differently than the others...

```<code>try
{
    auto pair = intersection(Line{}, Sphere{});
    // Do something with pair
}
catch (const Point& point)
{
    // The chances for a sphere and a line to meet at
    // a single point are so small that the case can
    // already be considered exceptional.
    // ...
}
</code>```

I purposedly left the errors gestion and the "no intersection" case out of this last example since many techniques already described could be used to handle it and I don't want the number of examples to be exponential. Here is one though:

```<code>try
{
    // res is optional<pair<Point, Point>>
    if (auto res = intersection(Line{}, Sphere{}))
    {
        std::cout << "intersection: two points" << '\n'
                  << std::get<0>(*res).x() << " "
                  << std::get<0>(*res).y() << '\n'
                  << std::get<1>(*res).x() << " "
                  << std::get<1>(*res).y() << '\n';
    }
    else
    {
        std::cout << "no intersection" << '\n';
    }
}
catch (const Point& point)
{
    // Exceptional case
    std::cout << "intersection: one point" << '\n'
              << point.x() << " "
              << point.y() << '\n';
}
</code>```

Since the cases where the result is thrown are exceptional, it should not add any runtime overhead to the program if the underlying system uses zero cost exceptions instead of the old SJLJ exceptions system.

The actual question

Well, that was a pretty long introduction, so here is the question: is there an idiomatic solution to the problem in these examples? And if not, are there at least some of the examples which could be banned without a second thought (well, you could give a second thought to the exception examples though...)?

Note: something seemed evident to me but apparently is not: the entity returned by intersection between two geometric entities is not limited to a nothing, a point or a set of point. An intersection can pretty much return any geometric entity. Therefore, using a container to hold the values does not sound like a good idea.

Note 2: time machine note, it happens. After having given it some thought, I would ban exceptions (as most people would do), not because exceptions are inherently bad or not designed for this, but because it makes the expression intersection(...) == SomeShape throw an exception even when the both operands should be equal, which isn't elegant. I guess that I would use a bastard child of a variant instead.


答:
Basically the same reason why SQL injection is still so common -- people that don't know  better designing and building systems.

Just as with SQL injection, there are occassional exceptions, in the case of SQL injection simple bugs or legacy tools, in the case of username/password compatibility with other systems. But 99.99% of the time it's simply "didn't know better".

One reason to push for OpenId (google or facebook login), is to make a simple and easily understood system the default that everyone knows.

Title: Why do modern websites still insist on archaic username/password requirements?

Body:
I was signing up for a website last night, and once again was greeted with the fact that my username cannot contain certain characters (including spaces), and neither could my password.

Is there a historic reason for this, or is there no particular reason?  Passwords that are phrases have higher entropy and are harder to crack than simple letter/number substitution (see image below):

And on the username front, no spaces?  Really?  It's just a string.  I could understand historic concerns against SQL Injection attacks but modern websites are passed that now (I hope!)


答:
1st Layout

The absolute worst case I ever saw for something similar to the second layout you describe was as follows:

We had separate stored procedures for doing searches based on manufacturer, model and part. Each of these stored procedures were fine tuned to specific indexes.

Because it was deemed to difficult to maintain, someone decided to create one gigantic search stored procedure that branched to different internal sections based on criteria. It was a massive failure.

The query plan stored in memory for this "new gigantic search" stored procedure never got optimized because of the branching that took place inside the stored procedure itself. So in essence, this new stored procedure wound up doing full table scans each time it was called and it was called a lot. The system would often crash or timeout.

Morale of the story... don't take shortcuts.

Title: Which database schema should I pick?

Body:
I am planning a database scheme and I hesitate between two designs,
which one should I choose?

For the purpose of the question, assume we want to prepare a database
scheme for an application managing student information in a
university.  There is several modules in the application:

Registry, dealing with general informations, like date of birth,
 health insurance number, and the like.

Library, dealing with the properties useful to the library
 management, like the number of books withdrawn, quitus or
 penalties.

Exams, dealing with the exams taken and the grades.

The modules Library and Exams see the modules Registry, but apart from
that, are independant.

In the application scenario, there is a very large number of students,
data is written once, seldom updated and often read.  Furthermore, the
university expands its system each year, so that modules are added:
Sport, Campus, whatever.  Modules remain quite independant.

I hesitate between two database layouts.

First layout

In the first layout, a table MODULE is associated to each Module and
a UID is used as primary key.  Since each module needs the
properties under control of Registry, we also prepare views of the
join of REGISTRY and MODULE on UID, so that the database knows
we will use this join extensively.

When the system expands, we add a new table and a new view to reflect
this expansion.

Second layout

In the second layout, we create a table with a large number of columns
holding the properties of the various modules.

When the system expands, we add columns to the tables.

Comparison

How does these two approaches compare?

If, say, some software upgrade comes with a major bug, requiring to
downgrade the software and perform later a second upgrade, the first layout seems to
be more robust. Regarding perfomance, the second layout saves a lot of
join operations, but in the first layout, we defined views for the
join operations, thus advertising which complex operations are likely
to happen so that the database system can plan this.  I am far from a
database expert, but am I right if I think that if I put all relevant
information into the hands of the database system, it will be able to
properly carry operation?

If I did not see it used in industrial applications, I would never had
given 2 pences on the second layout.  But since I did, I would like to
get other advises.


问:
Reverse horizontal and vertical for a HTML table
There is a two-dimensional array describing a HTML table. Each element of the array consists of:

the cell content

rowspan

colspan

Every row of this two dimensional array corresponds to  cells of a
 of the table which my software should generate.

I need to "reverse" the array (interchange vertical and horizontal direction).

Insofar I considered algorithm based on this idea: make a rectangular matrix of the size of the table and store in every element of this matrix the corresponding index of the element of the above mentioned array. (Note that two elements of the matrix may be identical due rowspan/colspan.) Then I could use this matrix to calculate rowspan/colspan for the inverted table.

But this idea seems bad for me. Any other algorithms?

Note that I program in PHP.

答:
array_column() will take the nth element of every subarray in a multidimensional array and produce an array from that "column".  If you do this multiple times for increasing index n, you can construct a new multidimensional array where each of the rows is a column from the input array.  The example below accounts for the fact that not all of the subarrays might be of the same length, and uses the length of the longest one as the number of columns (to avoid losing data).
(Explanation added during edit.)

```<code>// Assumes source array is $input

// find the "longest" subarray
$max = 0;
foreach ($input as $subarray)
  $max = max($max, count($subarray));

// generate a transposed output array
$output = array();
for ($i = 0; $i < $max; ++$i)
  $output[] = array_column($input, $i);
</code>```

See http://www.php.net/manual/en/function.array-column.php

Body:
array_column() will take the nth element of every subarray in a multidimensional array and produce an array from that "column".  If you do this multiple times for increasing index n, you can construct a new multidimensional array where each of the rows is a column from the input array.  The example below accounts for the fact that not all of the subarrays might be of the same length, and uses the length of the longest one as the number of columns (to avoid losing data).
(Explanation added during edit.)

// Assumes source array is $input

// find the "longest" subarray
$max = 0;
foreach ($input as $subarray)
  $max = max($max, count($subarray));

// generate a transposed output array
$output = array();
for ($i = 0; $i < $max; ++$i)
  $output[] = array_column($input, $i);

See http://www.php.net/manual/en/function.array-column.php


问:
Code repetition vs multi responsible method
I try to follow the Single Responsibility Principle (SRP) and also to omit Code Repetitions. However there are often places where there are code repetitions that are nothing more than code blocks of invocations that are resistant to extracting them into at least meaningfull named method:

```<code>DoAction1();
DoAction2();

if (value)
    DoAction3();

DoAction4();
</code>```

What is the best way to extract such code into a method and how to name it?

答:
I think it's merely a question of properly naming the extracted method. I'm afraid there is no universal solution and you'll have to do this on a case by case basis. If you are repeating this exact same piece of code in more than 1 place, there is bound to be a meaning to your actions and you need to look hard (may be discuss) to find that meaning and give it a name.

If it's any consolation, the Spring winner for long class names is AbstractInterruptibleBatchPreparedStatementSetter which stands at 49 characters.

OwnerDisplayName: yBee

OwnerDisplayName: Chandranshu


答:
Actually you are suffering from the top hardest task to do by a programmer - Naming Things. Sorry for such an amature answer but I couldn't resist. You can find that many people suffer from this, even in internet you can find this type of essay.

Now if your repetitive code is all together resulting one work and those methods are not being used by other methods then they are simply Helpers method and then the resulting method can be named as doXXX or makeXXX... I think you get the point.

As Chandranshu said "If you are repeating this [...] meaning and give it a name." is to the point and appropriate.

Pic courtesy : CodeChef Facebook Page Photo

Title: Code repetition vs multi responsible method

Body:
I try to follow the Single Responsibility Principle (SRP) and also to omit Code Repetitions. However there are often places where there are code repetitions that are nothing more than code blocks of invocations that are resistant to extracting them into at least meaningfull named method:

```<code>DoAction1();
DoAction2();

if (value)
    DoAction3();

DoAction4();
</code>```

What is the best way to extract such code into a method and how to name it?

OwnerDisplayName: tintinmj


答:
I got lost in all that theory, what I understood from all that is that
  (any ..?) programming language is built on lambda calculus,

Not in the sense that a PL designer needs to know and start out with the lambda calculus, but see below.

are there any models other than lambda calculus?

Yes, sure. For example, the SKI combinator calculus. Or register machines (basically, the van Neumann model).

The point is that any turing complete system could serve as basic for programming languages. Or, to put it differently, you could explain any language in terms of any computational complete system.

if the languages shares the same model, can we use that to convert a source code from one language to the other?

It makes it certainly easier.
Yet, from what I said above, it is rather the case that we can convert any language to any other language. (It's just not practical in most cases to do so.)

Title: Is there a math theory or a model behind programming languages design

Body:
I'm trying to understand if a programming languages design is built on some a theoretical model.

I came across few links that was describing Programming Language Theory, Lambda Calculus, Formal Languages and Turing machine.

I got lost in all that theory, what I understood from all that is that (any ..?) programming language is built on lambda calculus, that boils down to few language constructs.

is my understanding correct?

are there any models other than lambda calculus?

if the languages shares the same model, can we use that to convert a source code from one language to the other?


答:
Characters limits are most probably because it is an easiest way to limit SQL injection and XSS attack surface. Proper escaping should be more comprehensive solution, and most modern frameworks support it, but given that data may be processed by different systems written by different developers in different environments, it is hard to ensure everything is done properly every time and nobody ever made a mistake or took a shortcut. It is much easier to just reduce the character set to a safe and predictable subset when possible.

With passwords though there's not much justification, since passwords are not supposed to be ever stored or displayed as-is. The only reason I can see to restrict character set is to deal with various encodings and bugs that may follow when developers or browsers do not handle them correctly. Within standard ASCII character set, there's absolutely no reason to not accept any string as a password.


问:
Javascript heavy page/application - best practice for handling errors
Consider an application that contains a number of pages with a relatively large amount of javascript present, or a predominantly JS powered application.  The script handles a number things, such as:

Client side validation

Showing/hiding entry sections when appropriate

Autocomplete when filling text fields

Date pickers

etc.

If we consider the case that javascript is a requirement to use the site (e.g. not public internet site), I am wondering what the best practice would be for handling an unexpected error that occurs at runtime that causes some of the functionality to no longer be present (e.g. sections remain hidden, calendar controls become empty textboxes etc).  The options that I can think of would be:

Try to fail gracefully and have a page that will respond with no script present (gets trickier the more complex a page/app becomes).

or

Try..Catch round all the critical code, and on error, display a message, or redirect to an error page.

I am curious if anyone has 'best practice' suggestions for this?

My motivation for looking into this comes after an obscure bug with an earlier version of jQuery for a user in IE10 running in compatibility mode.  This question is not about that, but the knock on affect was that a lot of the code in a document ready handler in jQuery was not loaded, resulting in significant degredation in usability.

答:
You should try weighting it on the amount of critical pieces of the application that has failed. Weighting may seem a bit tough, but think of it from an end-user point of view. Try making the app fail and see what functionality remains when certain pieces fail. You can then determine the critical failure level that should be used as the point of redirection to a 404 or a raw-HTML version of the application.

If an entire piece of JQuery just didn't load, you could either direct the user to a 404 error page, or do what Google does and use a raw HTML version as the redirect when the JS fails.

Consider an application that contains a number of pages with a relatively large amount of javascript present, or a predominantly JS powered application.

I also don't understand why you have page loads if your application is JS-heavy. Have you considered using a client-side MVC/MVVM JS framework that runs on a single 'page'? Eg. AngularJS or EmberJS

The script handles a number things, such as:

```<code>Client side validation
</code>```

I hope you're doing server-side validation as well, otherwise you could compromise the entire database and security of the application.

Body:
You should try weighting it on the amount of critical pieces of the application that has failed. Weighting may seem a bit tough, but think of it from an end-user point of view. Try making the app fail and see what functionality remains when certain pieces fail. You can then determine the critical failure level that should be used as the point of redirection to a 404 or a raw-HTML version of the application.

If an entire piece of JQuery just didn't load, you could either direct the user to a 404 error page, or do what Google does and use a raw HTML version as the redirect when the JS fails.

Consider an application that contains a number of pages with a relatively large amount of javascript present, or a predominantly JS powered application.

I also don't understand why you have page loads if your application is JS-heavy. Have you considered using a client-side MVC/MVVM JS framework that runs on a single 'page'? Eg. AngularJS or EmberJS

The script handles a number things, such as:

Client side validation

I hope you're doing server-side validation as well, otherwise you could compromise the entire database and security of the application.


答: Chances are that your client has seen a similar app at a competitor's website, so ask him to point you to that website (if I'm right); then examine said webapp and estimate the time to build something similar.</p>

Title: How to estimate cost with very little information?

Body:
I have a client who wants a web app that will let him do the following (as he told me word for word):

User registration

Recurring payments for users

Online conference room reservation

I am supposed to give him a cost estimate very fast and I don't have any time to do any deeper requirements analysis! How would you deal with that kind of situation? Should I just give a very broad estimate and work out the exact requirements once he has accepted the estimate? At which point do you usually gather requirements, after or before getting a contract?


问:
Audit trails and entity relationships
I'm working on an order system and implementing an audit log. Two main concerns are:

1) While auditing a line item , you should only see audits for the line item

2) While auditing an order, you should see audits pertaining to the header information AND line item.

For example, a line item would have an audit trail for what was changed to the line item fields, in addition any adding/removing/modification to comments, photos attached , etc would be also be logged.

An audit for a order would contain all the things above, plus changes to the actual order (shipping location changes, purchaser name changes, etc).

Instead of mirroring the relationships for the audit trails tables, I am planning on having only 2 tables, which are as followed

```<code>Audit_Log:
    -audit log id
    -entity type
    -entity id
    -audit timestamp
    -audit user
    -audit message (serialization of object changes most likely)

Audit_Relationship
    -audit log id 1
    -audit log id 2
    -is child of entity
</code>```

This will greatly simply queries and sorting of audit trails. Big plus is all the audit trails is in one table.  You can just sort by timestamp and get an idea of what exactly is going on in the system (although, i think audits are meant mainly to focus on a smaller area , ie a specific order, to see changes).

Has anyone tried a similar design? I'm concerned if this confuses entity relationships at all. (Ie, a comment audit long for a line item will also be attached to an order, even though that relationship doesn't exist in the business logic.) The "is child of entity" was used to make this a bit more clear...

*I am using php with Doctrine 2, but I think this is an abstract enough idea to work with any ORM.

**(ADDED) I am also using MySQL, and considering on using database triggers rather then doing this at the ORM library level.

**(ADDED) Using the method described above might be a terrible idea. When attaching a audit trail, we need to get all the entities that will be associated with the audit trail. In order to do so we would need to implement a method to get a list of effected id's. Maybe some special annotations Doctrine uses to figure which properties should be followed when the audit log is "bubbling up" and attaching?

Anyways, shifting to longer read times using joins sound much more reasonable then longer write time for this feature.

答:
Never  never never have only one table for all auditing. This becomes a hot spot in the database and is a guarnatee of poor performance. You can do something simliar to that but do it for each audited table (We have script that will create new audit tables in our structure as we need them. )

I prefer to have the relasionship make the details subordinate to one thing at defines the actual audited information which is the auditlogid and date time. That way it is easier to relate all records together for one action (say you had a bad delete that took out 25000 records instead of 10, it is much easier to get them back from the audit tables if they are all related together.  Make sure you think about how to get data out of the audit table to fix a mistake and write the script to do that.

Never perform auditing at any level except through triggers on the database. You want to catch all changes not just those made by one application. Any unauthorized changes made through a stealth method are even more important to capture even if you think the application is the only way to change the data. And I have never seen any database of any size that didn't have the need to run run ad hoc data changes outside the application.

Title: Audit trails and entity relationships


答:
I was signing up for a website last night, and once again was greeted with the fact that my username cannot contain certain characters (including spaces)

A lot of websites use usernames as keys to lookup records in a database, and traditionally keys don't have spaces. As for special characters that can be a wide range of reasons why they are excluded. The owners of the site simply might not want people named $%@#$% as a user.

Passwords that are phrases have higher entropy and are harder to crack than simple letter/number substitution:

That is a bias perspective from your point of view, and keep in mind that passwords are not cracked but websites are cracked.

Long form passwords based upon phrases actually make it easier to decrypt a hashing algorithm, then a short password containing upper, lower characters, numbers and special characters. The reason is that human language uses far fewer of the letters in the alphabet for most phrases. This allows a hacker to narrow the scope of their attack on a hash. If they know all passwords are 8 characters, but contain a possible range of 255 different characters than that is a massively huge range of possible hashes.

The most common attack on a website does not come from that website. It comes from weaknesses in another website. If I hack into website ABC and get a list of unhashed emails and passwords, then I can use that list to attack other websites.

Before a hacker uses his list on popular websites like Facebook, Twitter or Google. They will clean that list by attacking smaller websites to see which idiots re-used their passwords. Once they have a cleaned list the odds are that person re-used the password on a major website.

Often those websites are attacked without the attacker ever being noticed, because they walked right in using valid authentication.

So never re-use your password. Always use a randomly generated one and stop using phrases.

Use A Password Vault


问:
Highly available web service design?
I was wondering if any design gurus out there could help me understand how large web companies design their services to be highly available. The scenario I'm thinking of is:

Client A connects to Service A

Client A sends N requests

On the N+1 request, Service A blows up

Client A reconnects to Service B

Service B services requests N+1 onwards

The only design I could think of was using a "metadata"/"discovery" service that the Client could be statically aware of. This service would give information on the best available Service, which the Client would then connect to and begin his requests, and re-query the "metadata" service when he realizes Service A went down. The application service is now highly available, but...

...the glaring problem is that the "metadata"/"discovery" service is static, will come under high load, and is not highly available, which kind of defeats the whole purpose. I suppose I could throw a lot of hardware under this service, but that's not a very good solution.

How should I go to design a real highly available web service?

答:
I think it is important to note that the client does not really react to the destruction of service A. The web service itself must fix this situation.

You could provide a set of redundant load balancers. These will provide the servicing of requests to their collection of available computers.

In the scenario you described above, the load balancer would hand Client A off to Service A. Once Service A is unavailable, the load balancer hands Client A off the Service B, and so on.

Of course, load balancers can die too. There are a few solutions I'm aware of.

Use round-robin DNS. This is not great as some clients will be serviced and others won't.

Have the load balancers check each other's statuses and then take over when the other is gone.

Use a cloud service, like EC2 Elastic Load Balancer.

We use AWS, so I've learned a lot from their architecture page. It is available for the public as well. Of course, if you don't use AWS, you need to extrapolate the ideas.

Title: Highly available web service design?


问:
Approach to algorithm for assignment of resources
We have a requirement to be able to match people with locations based on availability and certain conditions.

Let's say I have a group of people each with 3 types of things they may need to park.  A car, a truck and a boat.  Each person also indicates their preferred parking location (choices 1, 2 and 3).

I also have a list of parking locations which can handle a certain number of each of those things.  The goal is to match everyone up so that we maximum usage.  The main requirement is that each individual person has to be able to park everything in the same location OR they don't get to park at all.

Example:

Starting with the parking locations I have:

```<code>Lot    Cars    Trucks    Boats
A       2       0         2
B       2       1         3
C       0       5         0
</code>```

For people I have:

```<code>Person    Cars    Trucks    Boats    Choice 1  Choice 2  Choice 3
Tom        2       1         0         A         B         C
Betty      1       0         1         B         C         A
Sam        0       2         0         C         A         B
Pat        3       2         1         A         C         B
</code>```

My initial thought is to simply sort the people by the number of items they have (largest to smallest).  Then attempt to place each one based on choice.  IE: I'd try to place Pat first (most items), but couldn't because no lot meets the requirements.  Then I'd try Tom, who would go into Lot B.  Etc.

Do you know of any issues with that approach?

Bonus points: is there a name for this type of problem?

答:
It's not quite that simple.  You'd have to do backtracking in the cases where one person matches multiple lots.  Let's say you have the same lots, but the following people:

```<code>Person    Cars    Trucks    Boats    Choice 1  Choice 2  Choice 3
Adam       2       0         1         B         A         C
Eve        1       1         0         B         C         A
</code>```

Adam could fit into B or A, so you let him have his first choice:  B.  Now Eve can't fit anywhere, even though if Adam was in A, she could fit in B.  So you need a means of backtracking to make a different choice for Adam.  It's also possible for the ideal solution to not allow someone with the largest request, even if they fit, so you need to take that into account as well.

I don't know if there's a well-known name for this exact problem.  The most common name I see for the general problem is "resource allocation."

OwnerDisplayName: Chris Lively

Body:
It's not quite that simple.  You'd have to do backtracking in the cases where one person matches multiple lots.  Let's say you have the same lots, but the following people:

Person    Cars    Trucks    Boats    Choice 1  Choice 2  Choice 3
Adam       2       0         1         B         A         C
Eve        1       1         0         B         C         A

Adam could fit into B or A, so you let him have his first choice:  B.  Now Eve can't fit anywhere, even though if Adam was in A, she could fit in B.  So you need a means of backtracking to make a different choice for Adam.  It's also possible for the ideal solution to not allow someone with the largest request, even if they fit, so you need to take that into account as well.

I don't know if there's a well-known name for this exact problem.  The most common name I see for the general problem is "resource allocation."


问:
Understanding parameters
I have read this statement:

"A parameter is used as a sort of temporary messenger, carrying data originating from outside the constructor or method and making it available inside it."

So, does that mean that values of parameters are stored at first in the constructors and then passed to fields?

I still didn't understand the meaning of that. Any good explanation?

答:
```<code>public class Whatever {
    public static int foo(int a){
        return a+2;
    }

    public static void main(String[] args){
        int x = 1;
        System.out.println(Whatever.foo(x));
    }
}
</code>```

"Parameter" is "a". It is a name that exists only in local scope (scope is part of code where local variables are defined - that is simplification). Even though you call real value "x", you can take it's value from "a" in foo, because you passed this value in PARAMETER. This is the meaning of this sentence.

Btw: where did you read that? I'd like to append that to my "never read this stuff, it confuses you" list.

Title: Understanding parameters

Body:
public class Whatever {
    public static int foo(int a){
        return a+2;
    }

    public static void main(String[] args){
        int x = 1;
        System.out.println(Whatever.foo(x));
    }
}

"Parameter" is "a". It is a name that exists only in local scope (scope is part of code where local variables are defined - that is simplification). Even though you call real value "x", you can take it's value from "a" in foo, because you passed this value in PARAMETER. This is the meaning of this sentence.

Btw: where did you read that? I'd like to append that to my "never read this stuff, it confuses you" list.


答:
So when you see a constructor:

Foo(int x){ this._x = x; }

The parameter x is a temporary, local variable that contains the value passed into the constructor (or any other function). You then assign it to another variable this._x but you don't have to.

They're not stored in the constructor per se, but they are a temporary local variable that exists for the duration of the function's execution.

Title: Understanding parameters

Body:
I have read this statement:

"A parameter is used as a sort of temporary messenger, carrying data originating from outside the constructor or method and making it available inside it."

So, does that mean that values of parameters are stored at first in the constructors and then passed to fields?

I still didn't understand the meaning of that. Any good explanation?


答:
Google's definition on the word "parameter":

a numerical or other measurable factor forming one of a set that defines a system or sets the conditions of its operation.

I think it's better for you to go with this definition. The method that you are invoking is the "operation", and the definition of the method is the system. Your parameters are the "measurable factor" that the operation operate around.

Also as general advice, even when you are reading technical books, accept the definition of the dictionary before what's on the book.

Title: Understanding parameters


答:
A parameter is an argument passed to a function or method (depending on the language, that allows for outside values to be copied or used by a given function or object.

For example:

Let's say that there's an object that's an array and it has a method called "addTwo". This method takes two indices and adds the values at those indices. But how does the array know what two indices it needs? This is where parameters come in.

```<code>int[] exampleArray = {1,2,3,4};

exampleSum = exampleArray.addTwo(0,3);
</code>```

exampleSum is now equal to 5 since 1+4 = 5, those being the values at indices 0 and 3 respectively.

Parameters allow you to work with non-global variables that are generated outside the method, effectively copying them inside and then doing something with them.

Title: Understanding parameters

Body:
A parameter is an argument passed to a function or method (depending on the language, that allows for outside values to be copied or used by a given function or object.

For example:

Let's say that there's an object that's an array and it has a method called "addTwo". This method takes two indices and adds the values at those indices. But how does the array know what two indices it needs? This is where parameters come in.

int[] exampleArray = {1,2,3,4};

exampleSum = exampleArray.addTwo(0,3);

exampleSum is now equal to 5 since 1+4 = 5, those being the values at indices 0 and 3 respectively.

Parameters allow you to work with non-global variables that are generated outside the method, effectively copying them inside and then doing something with them.


答:
Automated test when run should not really make a dent on the environment they run in. It is very annoying when you run the test suite on someone else's code and find a bunch of files have been created in random places. This can lead to accidentally adding the files into version control and non-repeatable tests.

With the example of logging I would dump everything out to the console because :

You can redirect the logs to a file when running Maven test when required, so mvn test > my_log

When running the tests through an IDE you can easily scroll through the output without worrying that your missing something.

If your logging a large amount of data then grep and sed are your friends.

Title: Where should tests' log files go?

Body:
My application has a rather non-trivial, yet standard, log configuration.

It includes a console appender and a rolling size-based file appender.

When I'm running the tests, thru the IDE or with maven, where is the least surprising place to save the log files?


问:
What is the use of Association, Aggregation and Composition?
I have gone through lots of theories about what is encapsulation and the three techniques of implementing it, which are Association, Aggregation and Composition.

What I found is:

Encapsulation

Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.

Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface.

The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.

Association

Association is a relationship where all object have their own lifecycle and there is no owner. Let’s take an example of Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers, but there is no ownership between the objects and both have their own lifecycle. Both can create and delete independently.

Aggregation

Aggregation is a specialized form of Association where all objects have their own lifecycle, but there is ownership and a child object cannot belong to another parent object. Let’s take an example of a Department and teacher. A single teacher cannot belong to multiple departments, but if we delete the department the teacher object will not be destroyed. We can think of it as a “has-a” relationship.

Composition

Composition is again a specialized form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object does not have their lifecycle and if parent object deletes all child object will also be deleted. Let’s take again an example of relationship between House and rooms. House can contain multiple rooms but there is no independent life of a room and any room cannot belong to two different houses. If we delete the house, the room will automatically be deleted.

The question is:

Now these all are real world examples. I am looking for some description about how to use these techniques in actual class code. I mean what is the point for using three different techniques for encapsulation, How these techniques could be implemented and How to choose which technique is applicable at time.

答:
I frankly feel that these notions being taught at academic do have their importance in the contexts of object orientation and class design. These concepts aid us a lot when it comes to modelling a system from the scratch. Association, Aggregation and Composition belong exclusively to UML's class diagram and are completely independent of technology constraints like memory issues.

Furthermore, you also have to consider the higher level or business goals of the system you are modelling. We have objects like House and Room in our system under consideration, but cannot be strongly related (via composition). For instance, if I am modelling a real estate system then I might have to know what room belongs to what house. But lets that I am modelling a surveying or census system where i want to know how many people are living in each room of the house in a certain area, then I just do not need to relate a room with a house via composition.

Another example could be of an orchard and certain kind of fruit. Lets say I can only consider an orchard when I have apple trees planted inside it. The bottom line is that the requirements of the overall system does matter a lot.

Encapsulation is one of the pillars of Object Oriented Design. You need to bundle your data and the operations you will perform on your data. also you have to hide certain attributes of your object from the outer world in order to enable that object to survive in a valid state. When 2 objects are interacting, they have to interact each other via an interface. And that's what encapsulation ensures about when we design our OO system.

Here is how these concepts are applied to the code:

ASSOCIATION: Association indicates the relationship between objects. It lets the programmer know what methods to write in their classes to make them interact with each other. You can find several examples of code and class diagrams to understand association. In your example of Teach and Student, there is a relationship of teaching and taught by. So you will simply write a set of methods (technically called interface) via which you can get to know which student has what teachers and which teacher has what students.
Association also enables the system modeller to aid the database designer about the attributes and fields that need to be kept in the database.

COMPOSITION:
If one object is an integral part of another object, then i might have to indicate this relationship in the constructor of the other object. For example, in your scenario of Houses and Rooms we can write the following code in case we want to know which room belongs to what house type.

```<code>class House{
          string _HouseType;
     public:
    void setHouseType(string house_type)
     {
        this. _HouseType = house_type;
     }

     string getHouseType()
    {
       return _HouseType;
    }
};

 House HouseObject = new House();

class Room{

 public:
 Room(string HouseType) {
       this._HouseType = HouseObject.getHouseType();  //as in my system a room cannot exist without a house

 }

};
</code>```

The programmer will also ensure while invoking destructors of the object, that the other object's destuctor also gets invoked. This is crucial.

AGGREGATION:
Lets say if the relationship between objects is weak then we for the programmer, it would mean to use an instance variable instead to indicate the relationship. And then write a mutator functon (setter) to provide value to that object from another one.

```<code>class Department{

 string dept_name;

public:
   void setDeptName(string name)
   {
        this.dept_name=name;
   }

   string getDeptName()
   {
        return dept_name;
   }

};

 Department DepartmentObject = new Department();

class Teacher{

 string dept_name;

public:

  setDeptName(string name)
  {
     this.dept_name = DepartmentObject.getDeptName();  //You only need to invoje this method when needed (aggregation)
  }
}

};
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I frankly feel that these notions being taught at academic do have their importance in the contexts of object orientation and class design. These concepts aid us a lot when it comes to modelling a system from the scratch. Association, Aggregation and Composition belong exclusively to UML's class diagram and are completely independent of technology constraints like memory issues.

Furthermore, you also have to consider the higher level or business goals of the system you are modelling. We have objects like House and Room in our system under consideration, but cannot be strongly related (via composition). For instance, if I am modelling a real estate system then I might have to know what room belongs to what house. But lets that I am modelling a surveying or census system where i want to know how many people are living in each room of the house in a certain area, then I just do not need to relate a room with a house via composition.

Another example could be of an orchard and certain kind of fruit. Lets say I can only consider an orchard when I have apple trees planted inside it. The bottom line is that the requirements of the overall system does matter a lot.

Encapsulation is one of the pillars of Object Oriented Design. You need to bundle your data and the operations you will perform on your data. also you have to hide certain attributes of your object from the outer world in order to enable that object to survive in a valid state. When 2 objects are interacting, they have to interact each other via an interface. And that's what encapsulation ensures about when we design our OO system.

Here is how these concepts are applied to the code:

ASSOCIATION: Association indicates the relationship between objects. It lets the programmer know what methods to write in their classes to make them interact with each other. You can find several examples of code and class diagrams to understand association. In your example of Teach and Student, there is a relationship of teaching and taught by. So you will simply write a set of methods (technically called interface) via which you can get to know which student has what teachers and which teacher has what students.
Association also enables the system modeller to aid the database designer about the attributes and fields that need to be kept in the database.

COMPOSITION:
If one object is an integral part of another object, then i might have to indicate this relationship in the constructor of the other object. For example, in your scenario of Houses and Rooms we can write the following code in case we want to know which room belongs to what house type.

class House{
          string _HouseType;
     public:
    void setHouseType(string house_type)
     {
        this. _HouseType = house_type;
     }

     string getHouseType()
    {
       return _HouseType;
    }
};

 House HouseObject = new House();

class Room{

 public:
 Room(string HouseType) {
       this._HouseType = HouseObject.getHouseType();  //as in my system a room cannot exist without a house

 }

};

The programmer will also ensure while invoking destructors of the object, that the other object's destuctor also gets invoked. This is crucial.

AGGREGATION:
Lets say if the relationship between objects is weak then we for the programmer, it would mean to use an instance variable instead to indicate the relationship. And then write a mutator functon (setter) to provide value to that object from another one.

class Department{

 string dept_name;

public:
   void setDeptName(string name)
   {
        this.dept_name=name;
   }

   string getDeptName()
   {
        return dept_name;
   }

};

 Department DepartmentObject = new Department();

class Teacher{

 string dept_name;

public:

  setDeptName(string name)
  {
     this.dept_name = DepartmentObject.getDeptName();  //You only need to invoje this method when needed (aggregation)
  }
}

};


问:
How to compare features of two curves?
I need to compare two curves f(x) and g(x). They are in the same x range (say -30 to 30). f(x) may have some sharp peaks or smooth peaks and valleys. g(x) may have the same peaks and valleys. If so I want a measure on how well these features coincide without visual inspection. I have tried to solve this problem in the following way.

Normalize both functions by dividing each data point by the total area of the function. Now the area of the normalized function is 1.0

At each x get the minimum value out of f(x) and g(x). This will give me a new function that is basically the overlapping area between f(x) and g(x).

When I integrate the resulting function of step 2 I get the total overlapping area out of 1.0

However this does not tell me whether the peaks and valleys coincide or not. I am not sure if this can be done but if someone knows a method I would appreciate your help.

==EDIT==
For clarification I have included an image.

The difference between the two curves (black and blue) may not be the same but
will have complementing shapes.

Background:
The functions are projected density of states (PDOS) of atomic orbitals of a compound. So I have states for s,p,d orbitals. I want to determine whether the material has s-p, p-d or d-d hybridizations (orbital mixing). The only data I have is the PDOS. If say the PDOS of s orbital (function f(x)) has the peaks and valleys as at the same energies (x values) of the PDOS of p orbital (function g(x)) then there is s-p mixing in that material.

答:
Brute force: find out smallest non-zero float value with this value as step, go through the whole domain and check if values are equal?

== EDIT ==

Hmmm... If by "the same shape" you mean g(x)=c*f(x), this solution should be modified - for each element of domain you calculate f(x)/g(x) and check whether result is the same for each point (of course, if g(x) == 0, then you check if f(x) == 0, you're not trying to divide).

If "the same shape" mean "local optimums and bending points are the same"... Well, find local optimums and bending points for f(x) and g(x) (as sets of domain elements) and check, if those sets are equal.

Third option: f(x) = g(x)+c. Just check if each element of domain has the same difference f(x)-g(x). It is almost identical as first case, but instead of division you have difference.

== YET ANOTHER EDIT ==

Well... Second approach from edit above may be useful. Also, you can merge it with comparing sign of first dervative (not symbolic, but calculated as df(x) = f(x) - f(x-step) ). If both functions have the same sign of derivative in the whole domain, check optimas and bending points, just to be sure. I'd say this conditions should be enough to do what you need.

Title: How to compare features of two curves?


答: This is just "off the top of my head", so I could be misunderstanding the problem entirely, but maybe you could apply a root-mean-square distance (RMSD) to the functions. If you're just interested in the peaks and valleys, then apply it to areas around those peaks and valley (that is, for some x +/- some epsilon where the derivative of either function is zero). If the RMSD of that range is near zero, then you have a good match, I think.</p>

Title: How to compare features of two curves?

Body:
I need to compare two curves f(x) and g(x). They are in the same x range (say -30 to 30). f(x) may have some sharp peaks or smooth peaks and valleys. g(x) may have the same peaks and valleys. If so I want a measure on how well these features coincide without visual inspection. I have tried to solve this problem in the following way.

Normalize both functions by dividing each data point by the total area of the function. Now the area of the normalized function is 1.0

At each x get the minimum value out of f(x) and g(x). This will give me a new function that is basically the overlapping area between f(x) and g(x).

When I integrate the resulting function of step 2 I get the total overlapping area out of 1.0

However this does not tell me whether the peaks and valleys coincide or not. I am not sure if this can be done but if someone knows a method I would appreciate your help.

==EDIT==
For clarification I have included an image.

The difference between the two curves (black and blue) may not be the same but
will have complementing shapes.

Background:
The functions are projected density of states (PDOS) of atomic orbitals of a compound. So I have states for s,p,d orbitals. I want to determine whether the material has s-p, p-d or d-d hybridizations (orbital mixing). The only data I have is the PDOS. If say the PDOS of s orbital (function f(x)) has the peaks and valleys as at the same energies (x values) of the PDOS of p orbital (function g(x)) then there is s-p mixing in that material.


问:
Multiple schemas vs aditional column per customer
If you have different database per customer as there is no need to share any data between them does the same apply if a customer has multiple stores ?

I believe that it is easier to do queries for statistics (some require 4-6 table join) if they are at a single schema rather than multiple schemas.

But everyone (haven't done anything yet) in team believes that cross schema queries is the way to go.They claim that postgres supports it well.
Which is the way to go?

答:
Separating clients by database makes sense: security, uniquely manage backups/restores, customizations.

Having each location under a separate schema would give you ways to: group objects, security and possibly manage separate backups, but unless you see no other way to achieve this separation, I think it makes querying and maintenance unecessarily difficult.

It seems easier to have locations managed by the data. You can always handle record selection security by having a security table indicating which location(s) a user has access.

You may feel there are few queries/reports that combine locations at this time, but once a client gets one report that does this, they'll want more. These clients are usually paying higher licensing fees and will put additional pressure on your sales team (Which will put even more pressure on the dev team.).


问:
Is it possible to be good at both programming and graphic design?
The stereotypical view of a programmer can't do graphics very well, from what I've read and seen. However, I love programming (preferably OOP, PHP, C++, Objective-C) and can't deny the fact I have a unique taste in web design and others have said I am doing well at it (CSS). I thought to myself "Hey, wait, I'm a programmer - how can I design well?". Question is: is it possible to be good at programming and designing? Does anyone here feel the same?

For the record: actual images I have created have been called programmer art several times before by friends

答:
Well, why not?  Lots of people have multiple talents.

But the amount of time that you devote to a particular skill does make a difference.  Spending more time one one skill means you have to spend less time on another, and spending less time means being less competent.

For my part, I have spent the vast majority of my time on coding, not design.  As such, I am a pretty good programmer, but have stick-figure design skills (although I do believe I know good design when I see it).

Good design means more than just looking pretty; it also means making an application that is intuitive and easy to use.


问:
Django REST + Backbone/Ember/Angular Implementation Method
https://stackoverflow.com/questions/10941249/separate-rest-json-api-server-and-client

In light of this post, I wanted to ask questions regarding Django and specifically the implementation methods of getting one of these client side technologies to work with Django.

Currently, I have a backend that is setup that uses Django Rest Framework to serialize the data. My problem though, is I'm not sure how to get the client side to access the backend.

Django is a MVC framework. Originally I thought that I could just take out the View, and just implement the model and controller. Then, I would write complete separate View code so to speak using Backbone/Ember/Angularjs. Then the client would access the REST resources. How would I combine these two later if I wanted to deploy this to Heroku? Heroku only takes a whole Django application. How can I get the client code on there as well then?

The other option, which I've seen before, is to NOT take out the View completely, but to use Django templates WITH Backbone/Ember/Angularjs. Then, I can just simply put these js files in the "static" folder so to speak. But then that seems weird, because then I'd have a View on the server side (correct me if I'm wrong), that accesses my own REST resources. I tried this, but for some reason even though my page retrieves the javascript files, it does not seem to be working as expected.

答:
In order to get this type of app to work, you need to firstly go more in-depth into how the API setup works. APIs are different to the normal apps you can build in frameworks like Django or Rails (even though there's elements of APIs in these frameworks).

In order to explain a bit more, you can run your client-side JS framework being hosted on a static host (something like: http://www.divshot.io/)

You can then interact with Heroku (your backend) via API calls ('GET', 'POST', 'UPDATE' requests). This interaction would normally occur through something like JSON (where the elements are 'pulled' from the API, the client 'does something' to the element and the updated element is 'pushed' back via the API - making the database update).

Take a look at this question I asked a while back:

https://stackoverflow.com/questions/17760128/is-a-restful-api-and-a-backend-service-like-parse-the-same-thing

And also take a look at this guys tutorials concerning APIs:

http://coenraets.org/blog/2012/10/nodecellar-sample-application-with-backbone-js-twitter-bootstrap-node-js-express-and-mongodb/

These will help you somewhat in getting to understand APIs (those tutorials are especially useful).

If I remember from those tutorials, you can host your static client-side JS framework together with your backend on the same setup.

Just ask the guys at Heroku to clarify that for you, as I'm sure it will be possible to host the static JS/HTML/CSS on the same setup too.


答:
This is a common and often difficult problem in analytical chemistry, physics, spectroscopy, etc. The approaches used can range from simple RMSD comparison to very sophisticated methods. If the task isn't easy to do by visual inspection (humans are exquisitely developed for feature recognition), then it will likely be hard to do computationally.

One approach is to try to remove the "baselines" so that the functions are zero-valued except where there are peak or valley features. This is best done with curve fitting using a low-order polynomial, or, better yet, a more appropriate principled model of what the baseline can and should look like. If the peaks are very sharp, you can simply smooth the function and subtract the smoothed function from the original function.

After removing the baseline, you can normalize and generate residuals or do RMSD (simple approachs) or try to detect peak/valley features by fitting a gaussian (or whatever model is appropriate) to eachfeature you seek. If you are able to fit the peaks, then you can compare the peak locations and half-widths.

Take a look at SciPy if you know Python.  Good luck.

Title: How to compare features of two curves?