问:
How long do I have to provide the source code for a LGPL-library?
In case I would dynamically link to a LGPL-licensed library (in this case Qt) in a commercial application: Which way do I have to provide the library's source code?

Is a link to the project homepage sufficient? Or would I need to host the source code on our servers? For which period of time after distribution of the software would this website need to be kept up? (or would "on request" be also compliant with the LGPL?)

Is it still necessary to offer the source code in a physical way (e.g. on CD via snail mail) for 3 years (any difference between LGPL 2.1 and LGPL 3 here?)?

I've found this in the GNU FAQ:

Can I put the binaries on my Internet server and put the source on a
different Internet site?

Yes. Section 6(d) allows this. However, you must provide clear instructions people can follow to obtain the source, and you must take
care to make sure that the source remains available for as long as you
distribute the object code.

I've highlighted the relevant part as bold text. However, I cannot believe, that this is the complete truth. If I distribute an application only once, then I could put the source code online and take it down a day later. So this still doesn't answer my question...

There is another question with a related topic here, but it has a different attempt than mine. It only asks for alternatives to shipping the source code directly with the binaries, while my question aims to clarify which circumstances and conditions I would have to consider / respect when sharing the LGPL library source code.

答: As a rule of thumb, I'd say you should make the source available for as long as you're providing support for the software that uses it, or (preferably) as long as you can tell it's being used. It's difficult to give any solid guidelines without knowing more about the software you're producing, and what area you're in. To be on the safe side, I'd include a link somewhere in your documentation to the project home page and a note regarding what version of the software your application uses.</p>


问:
How important is Discrete Mathematics for a Computer Scientist?
As the title says,

How important is Discrete Mathematics for a Computer Scientist?
Background: I'm pursuing a Master's degree with a focus on fundamentals such as Algorithms, Complexity and Computability Theory and Programming Languages to get a good foundation for working in the field of Parallel Computing.

Some more background: My university grants a lot of freedom in the choices of courses for my Master's degree. It's officially called "Software Engineering", but due to a the broad range of electives, a different focus is possible. Interestingly, none of the electives is a lecture in Math! I'm thinking about doing a course about Discrete Mathematics that would take half a semester to complete successfully, even if I can't use it for my degree. So with this question I'm trying to find out if the effort is justifiable.

答:
Actually Discrete Mathematics is the backbone of Computer Science or I say the Theoretical Computer Science. Without The study of Discrete mathematics, We miss the crux of Computer Science-Logic Development. It brings rational clarity to your solutions and a formal way of analysing a problem.

Every field in computer science is related to discrete objects whether it is databases, Nueral networks, Computer Organization, Compilers, Network Programming etc. I would say it is the 'parent subject' of computer science and every one should study this subject.

ContentLicense: CC BY-SA 3.0


问:
Why isn't it possible to switch between header and cpp in Visual Studio?
For the last eight years I wait for Microsoft to add the basic C++ functionality of switching back and forth between .h and .cpp files, and it keeps astonishing me it is still not supported in Visual Studio 2010. Does anyone know why they refuse to add this essential feature?  </p>

答:
I've not used Visual Studio for a few years, but IIRC the "Go To Definition" and  "Go To Declaration" functions do this (albeit badly, particularly when it comes to constructors)

I imagine that one reason for them not implementing it on a filename basis is that there is nothing about C++ that enforces a convention on the arrangement of classes in files.

e.g.  The "obvious" way to arrange a class called "MyClass" in a C++ project is to put the class in MyClass.h and the methods in MyClass.cpp.

Another, totally plausible way is to put the class definition in "elephant.h" and the methods in "mouse.cpp"

You could put all of your classes in one "classes.h" file and define all of your methods in one method per file.

I'm not saying that you should arrange your projects in this way, just that you could, and that's why jumping between .cpp and .h files is implausible, unless done in the same manner in which it is already implemented.

ContentLicense: CC BY-SA 2.5


问:
Why is it bad if view engines encourage logic in templates
I once read this, because I wanted to see if handlebars is the right view engine for me or rather something else.

A thing that I wonder since then is the following, why is it considered bad, if a view engine

encourages logic in templates?

答:
Separation of concerns.

The concern of templates is visual display of information.  Business logic doesn't play any part in that.

User interfaces can contain short loops that iterate over lists.  It can also contain validation logic and things like AJAX that improve interactivity.  Beyond that, it's generally all server-side, unless the application is self-contained.


问:
What is the appropriate way to handle implicit requirements?
I'm in R&D working on a new software product.

Management understandably focuses on the primary features that most obviously give a customer an advantage. But there are many requirements that can be seen as important as well (ex. performance, future extensibility, data traceability, security, smooth UI). These implicit requirements are probably the larger in number for any product, and if unfulfilled can lead to an unhappy customer.

I'm afraid that it is somehow expected that during development these things will become automatically implemented. For me though, everything is an atomic aspect that requires it's own attention and development effort.

I have a feeling that management may be too busy to pay too much attention to such aspects. For the sake my own developer pride, quality control, and being able to account the effort I spend, when and how should I document and communicate implicit requirements?

(i.e. features that exist in a product, but which haven't been explicitly talked about with either the customer or management)

Clarification

Thanks for the interest in the question, the main gist of the answers up to now seems to be:

"You need to make implicit requirements explicit."

Rats... that didn't occur to me.

The requirements I mean can be of the following types:

The customer can't hear about these requirements, because  I will be presenting a long list of ways in which the product can fail, instead of talking about how it will make them happy.

Busy management feels that I'm wasting their time, when I talk about "obvious" features.

I will only be able to describe some of these requirements during implementation. During planning I may just have a gut feeling that a particular issue might require focused effort during development.

I'm not at the necessary height in the company totem-pole to dictate my working conditions.

I'm asking for guidelines how to semi-formalize [implicit] requirements, while putting in less effort than for full-blown requirements, but keeping me prepared for judgement day, so that I'm not caught empty handed.

答:
Making implicit requirements explicit is the correct answer.

About your follow up constraints:

The customer can't hear about these requirements, because I will be presenting a long list of ways in which the product can fail, instead of talking about how it will make them happy.

Imagine you're an architect, you design buildings for big companies.

Big Co. Inc. asks you to build a skyscrapper for them by the shore. It has to have a swimming pool on the roof, and another pool on the 10th floor. It has to have a wall garden covering the outside wall from the 15th floor to the 17th.

This are the customer's requirements. Would you hesitate to add to the requirements list the requirements to have the building support its own weight plus two pools full of water and a hughe wall garden?

Would you hesitate to add that it has to have plumbing, electrical wiring and a system to fill the pool with water and keep the water clean?

You are the architect, you know what is necessary for a building, that's what they're paying you for.

You know that a building with more than three floors will need an elevator, you don't need anyone to tell you that.

Busy management feels that I'm wasting their time, when I talk about "obvious" features.

So the elevator is obvious. As obvious as it is, it still has an impact in the overall cost, the overall workforce required, the ETA for the project, and probably other aspects.

I will only be able to describe some of these requirements during implementation. During planning I may just have a gut feeling that a particular issue might require focused effort during development.

This is where the architect analogy breaks, mainly because of the fluid nature of IT. My only advice here is that you may benefit from an iterative process that allows you to plan for the unplanned. Agile seems pretty popular.

I'm not at the necessary height in the company totem-pole to dictate my working conditions.

Your company should have someone responsible of making sure the requirement list is complete.

If they don't they have a big problem, try to get them to fix it or start polishing your resume.

If they do but that person didn't do his job right, call him on it and escalate as necessary.

If they do and that person is you, but you weren't given the authority to do your job, then they're asking you to do something impossible, your best move is not to play (aka try to get them to fix it or start polishing your resume).

OwnerDisplayName: Rafael Cichocki


问:
How can I specify user stories for processes with third-party involvement?
Suppose we have a process like the following:

Super user receives an internal request to initiate a process.

Super user approves the request.

An external process belonging to a third-party is triggered.

Once third-party process is complete, super user receives the result and either accepts or rejects it.

It is easy to see how to formulate user stories for 1, 2 and 4 but 3 is a bit trickier. Do I:

Skip it because it is external to our system and treat 3 as an implementation detail of 4 (after all, 4 is dependent on 3 happening); this may require external documentation of the actual process or potentially bloated user stories/tasks for 4.

Treat the external system or one of its users as a user and write the user story from their perspective ("as a [system x] user, I want to process..."); this seems like it should be out of the scope of our functional requirements.

Treat it as a "task" against 2; this doesn't seem quite right as the functionality of approving something doesn't really imply the triggering of some other process.

Something else entirely.

It seems that the act of the user approving the request and approving the response should be seen (functionally) as independent of the fact that the process happened in the middle but - without specifying that somewhere - 4 could conceivably be "implemented" even though it will never actually happen (i.e. if the response is available, the user sees it but a response will never actually be available because the third-party process is never actually triggered).

Is there a prescribed or conventional way for dealing with these types of scenarios?

答:
One could also argue that the case described above is a single user story if the business value to the super user is not derived until step 4 in the process is complete.

Consider a higher level user story such as:

AS A super user I WANT TO perform XYZ business process SO THAT I can accept/reject the results.

1,2,4 then become Acceptance Criteria to validate that the Super user can do these steps in the system...validate the super users receives request, validate super user can approve/deny the request, validate the super user can, reject/approve the result.

3 could also be considered an acceptance criteria...something like: "Validate the internal system consumes the output from the external system"

If the high level user story is not granular enough to fit within an an iteration, Jay S' answer provides a great breakdown.


问:
Scrum: Is it okay for the development manager to be the Product Owner
My company is going through an agile rollout and is currently considering options for what that means.

The ScrumMaster role is somewhat straightforward, but Product Owner has many candidates: Product Managers, UX, Software Analysts. The favored candidate for the moment is for each development manager to be the team Product Owner.

However, using development managers as product owners seems like it will discourage self-organization.  What's the "right" way to select product managers in an Agile way?

答:
Look for the person on the team that is strongest in these qualities:

Knows the general business (not just the customer's business)

Has the relationship with the customer

Good at logically breaking down customer wants into little business-value oriented stories (Pretty much the Agile BSA mentality)

Has process improvement skills

Has change management skills

As far as self-organization; a PO generally shouldn't have any direct reports on the delivery team as this can be a conflict in interest in terms of the team challenging the PO on requirements, priorities, etc.

A great PO will be a BSA at heart, but should always be considering the process and change management impacts that go along with delivering a technical solution.  After-all, building a solution that doesn't fit within a current or future process, or that no one adopts doesn't provide much value to the customer.


问:
How to propagate data changes between dev, qa, and production
I have tools for propagating database definition changes, but any data changes I make I write scripts for and run them in each environment by hand.

There are some data changes/additions that have to be made routinely and I would like to create a GUI for handling this, but if I did that then I no longer have the sql scripts to run in other environments (unless I generate them from the GUI tool, but then I'm still left having to run those changes by hand in other environments).

One idea I'm mulling over is to write the GUI tool in such a way that it generates the xml files for objects I've added or changed and include these xml files in the publication of the web project. Then the application can look at these xml files and update the database accordingly.

Are there any standard practices or tools for pushing data changes automatically along with your db definition and/or code changes?

EDIT:

Based off the first answer I received it appears like I might have made it sound as though I want a tool to compare dev databases to production databases and propagate all changes. This is DEFINITELY NOT what I was trying to communicate.

One example of what I'm dealing with is language texts that have to be added (no, I can't use resource files for this because end users can modify the texts through the application at runtime). Instead of having to hand write scripts every time I need to add texts for a new item, it would be great if I could just create a GUI tool to simplify the process.

I think it's pretty obvious why I would prefer to use a GUI over hand writing SQL statements, and so the next question is why would I want to propagate this the same way we propagate code changes. Well, this isn't like other test data. This is data that will ALWAYS be propagated to each environment. The same sort of concerns that lead us to use tools to simplify the process of pushing code or database changes to each environment apply to the kind of data I'm talking about here.

答:
Why would you want to propagate changes to the data along with the promotion of definition or code changes from Dev to QA to Prod?

I wouldn't think there is something like that out there because if anything, it is better to be able to refresh Dev and QA with data from Prod...not the other way around.
In a situation where you need to create new data in Dev or QA because you are testing a new function that deals with something existing data doesn't contain, the thing is it is just that...TEST data.  QA best practices dictate the data going in Prod must be clean.

If you must, best practices in my experience are what you have already - use scripts (there are some data migration tools out there that are meant for such a thing), or put them in manually.


问:
In an Agile Environment, who is responsible for software architecture
In an agile team, who is responsible for making high-level architecture and design decisions that affect the entire system, not just the work being done in the current sprint?

Maybe product owner, scrum master, the scrum team or someone else?

答:
Depends on the organizational design of the organization and if the product is in a portfolio.  Larger, role-oriented organizations are may appoint senior architects to lead these type of activities.

Generally an senior architect will facilitate and guide design decisions as they don't just affect a product backlog, they may affect several products in a portfolio of products.

Smaller, agile companies may rely on developers that are system experts to lead the development team to align on architectural design.

Ultimately, architectural decisions need to be agreed upon by the delivery team that works on the product.  Experienced Architects and tech leads will be more focused on facilitating the discussion around what the design should look like, rather than dictating the design.


问:
User story - design constraint
How can I put in the design constraint into a user story?

The system to be developed will generate a invoice when a payment is made, and will send the invoice to xMail (an emailing tool) which will then email the invoice to the customer.

How should I put the information where the system will send the invoice to xMail?

Note: xMail is just a random name as I could not reveal the actual name

答:
Consider what the key steps of the process are and when the value to the customer is derived.  Try and capture that in the user story.  Then use your acceptance criteria to flush out the requirements further.  There is nothing wrong with putting a constraint in a story; not everything can/needs to be negotiable.

Example:

As the system I WANT TO generate & send an invoice to xMAIL SO THAT xMAIL can send the invoice to the customer.

Validate invoices are generated & sent when a payment occurs

Validate xMAIL receives the invoice

Validate the customer receives the invoice via xMAIL (this may considered outside the scope of the story, since xMAIL is an external system.  However it is still part of the business process and a QA candidate)

Title: User story - design constraint


问:
How to represent a Rubik's Cube in a data structure
If I am attempting to simulate a Rubik's Cube, how would you create a data structure to store the cube's state in memory, with X number of tiles per side?

Things to consider:

the cube can be of any size

it is a Rubik's cube, so layers can be rotated

答:
I found an x-y-z coordinate system to be a simple way of addressing a Rubik's cube, and rotation matrices a simple, generic way of implementing the rotations.

I created a Piece class containing a position vector (x, y, z). A Piece can be rotated by applying a rotation matrix to its position (a matrix-vector multiplication). The Piece also keeps tracks of it colors in a tuple (cx, cy, cz), giving the colors facing along each axis. A small amount of logic ensures these colors are updated appropriately during a rotation: a 90 degree rotation in the X-Y plane means we would swap the values of cx and cy.

Because all of the rotation logic is encapsulated in the Piece class, the Cube can store an unordered list of Pieces, and rotations can be done in a generic fashion. To do a rotation of the left face, select all pieces with an x-coordinate of -1 and apply the appropriate rotation matrix to each Piece. To do a rotation of the entire cube, apply the same rotation matrix to every piece.

This implementation is simple and has a couple of niceties:

A Piece object's position will change, but its colors do not. This means you can ask for the red-green piece, hang on to the object, do some rotations, and check the same object to see where the red-green piece ended up.

Each type of Piece (edge, center, corner) has a unique coordinate pattern. For a 3x3 cube, a corner piece has no zeros in its position vector ((-1, 1, 1)), an edge has exactly one zero ((1, 0, -1)), and a center piece has two zeroes ((-1, 0, 0)).

The rotation matrices that work for a 3x3 cube will work for an NxN cube.

Downsides:

Matrix-vector multiplication is slower than swapping values in arrays.

Linear-time lookups for Pieces by position. You'd have to store Pieces in an external data-structure and update that during rotations for constant-time lookups by position. This defeats some of the elegance of using rotation matrices, and leaks rotation logic into your Cube class. If I was implementing any kind of search-based solving algo, I'd use another implementation.

Pattern analysis (during solving) is not as nice as it could be. A Piece has no knowledge of its adjacent Pieces, and analysis would be slow due to the above performance issues.


问:
How should I break this user story into tasks
I am trying to understand how to create user stories and tasks. I have read articles stating that you should create small user stories and they should be a "vertical slice" of functionality instead of a "horizontal slice".

I have a user story:

As a buyer I want to add items to the shopping cart so that I later can buy the items.

From my understanding tasks should be the things you have to do in order to complete this user story. Am I right if the tasks should also be created as "vertical slices" instead of "horizontal". Instead of creating a task for "create shopping cart model" I would have a task "Update shopping cart service to be able to add products".

But with that task I only have one task, is it to big, should it be broken down in another way? Is it bad that a user story only have one task?

答:
Assuming you're a product owner or business analyst, don't.  Tasks are generally not created by PO's or BAs because they focus on technical details, not requirements.

If you're in a dev or QA role think about tasks as tools for the development team to understand the work steps needed to complete the user story.  Formal tasking can be a good way of getting development teams to start work on a story and get past the initial story point estimate.

Depending on your team, tasks may be non-existent to extremely granular in your Agile tracking tool.  Its the thought process that counts.  Generally you'll see and think about several categories of tasks depending on how detailed your team likes to get.

Research tasks
Implementation tasks
Code quality tasks
Testing tasks

If your task isn't helping you fulfill acceptance criteria or the team's definition of done, consider if it should really be part of the story.


问:
Is a new change request on a user story a different user story
We have completed a user story for a sprint. Near the end of the sprint there was a new functional addition requested for the same user story.

Here the user story will remain the same. ie
As a <user> i want to <> so that <> will remain the same. Only difference is that there is a new value add that is to be done.

So should this be taken as a new user story for the next sprint or should this be taken as a backlog?

We have met the done definition we had for the user story and this new feature is a new requirement which wont have any change in the user story definition.

I am adding an example to make it more clear.

Take a feature say create profile, in that user can update his name,address, phone no, photo etc and its done. now if the customer wants to have facebook link or add country code to phone number, is that change request a new user story?

答:
In terms of documentation and handling the change in a strict Scrum framework it should be handled as a new user story and not allowed into the existing iteration but...

Let's be practical and not create unnecessary overhead...that's not Agile.  If the coding/testing complexity/effort is so minimal that it takes longer to create, prioritize, and manage the new story rather than modify and complete under the existing story, go the leaner route and just update the existing story.

Remember, once you bump an enhancement out, your developers may need to warm up to the code again before making the change.  Sometimes its more efficient just to make the modification on the fly and track the change against the existing story.


问:
Scrum task over estimation
In my current company we're using Scrum with 2 week iterations and a regular planning session.
How planning normally works in our company is that we take a predefined , prioritized (by PO before the planning) product backlog of user stories / PBIs, take PBIs from the top one by one  and break it down to tasks to then estimate them in hours (using poker planning and fibonacci) until we reach a given team's capacity.

While this works for most of the teams, there is one 2-person team where I'm a SM, that tends to systematicaly  overestimate the tasks by far (as those developers are being paid by an hour in this project, and from the record of working with them in another project I know for a fact they have a far better velocity).
Not being a part of this team as a developer, trying to adhere to Scrum I'm refraining from influencing those estimates by any means, however as we approach the deadlines in the project this situation is becoming more and more troublesome.

My question is - is there a way / good practice in such situations, to keep the team on the edge of their velocity and not influence their estimations, or maybe we're doing sth wrong in the process ?

答:
Scrum master is a part of the team and has the right to ask: why did you estimate it that way?

Unmotivated, yet proficient teams start to slip over time.  Looking for and discussing indicators other than an artificial velocity can help surface this issue.

Is their code coverage slipping? Why?
Are automated tests failing frequently?
Is the build often broken?
Is defect load being managed?
What is the quality of defect documentation?
Are features being continuously delivered within the iteration?
Are the team members continuing to set goals and suggest improvements during retrospective?

If they task and hour estimate their stories, it can also be helpful to discuss their effort estimates.  Look for spike/research tasks that drag on for days and ask to understand how they determined the necessity and duration of these tasks.

Finally look at the variation on how long it takes to finish similar sized stories.  Teams that are managing their story point estimates generally have large variances on how long it takes to complete a story.  Was that 8 point story that you thought would take 5-10 days to complete suddenly picked up and finished at the end of the sprint in 3 days?  Nothing wrong with asking how they achieved such success and pulled of the last minute win ;).

Title: Scrum task over estimation


问:
How to ORM non-disjoint specializations
Consider this highly simplified sample of relational data (each table may be involved in one-to-many and many-to-many relationships not shown here):

```<code>people
+-------+------+--------+
| name  | born | gender |
+-------+------+--------+
| Alice | 1980 |      F |
| Bob   | 1975 |      M |
| Carl  | 1994 |      M |
| Dot   | 1942 |      F |
+-------+------+--------+

workers                         students
+-------+---------+-------+     +-------+-------------+-------+
| name  | company | since |     | name  | institution | since |
+-------+---------+-------+     +-------+-------------+-------+
| Alice | Soylent |  2003 |     | Alice | Western     |  2001 |
| Bob   | HAL     |  1999 |     | Carl  | Eastern     |  2012 |
+-------+---------+-------+     +-------+-------------+-------+
</code>```

Some people are workers (Bob), some are students (Carl), some are neither (Dot).

Some are both (Alice).

Mapping to OOP, it would be straightforward to have three classes, like:

```<code>class Worker extends Person

class Student extends Person
</code>```

But that's ok as long as the specialization is disjoint: object alice can't be instance of Worker and Student at the same time.
If you get a list of Person objects, you cannot cast the same element to both subclasses.

First of all, is there any specific name for this (likely common) impedance mismatch case?

Next, suppose you can't remodel the schema. I may have my own way to workaround with OOP, but I'm mainly interested in yours. Is there some sort of best-practice guideline?

This example has only two ISA relations, but my impression is, the more non-disjoint sub-entities, the more OOP gets uncomfortable.

答:
I don't know how it's modeled in your application, but in the real world, people cannot only be both student and worker,  they can be workes at different companies at the same time. Now looking closer at the tables shows that they look like N:N join tables, don't they?

So what I would do is rename Student to Enrollment and Worker to Contract. Person now has two N:N relations,  one with Institution,  and one with Company. In terms of OOP:

```<code>class Person {
    Collection<Enrollment> enrollments;
    Collection<Contract> employmentContracts;
}
</code>```

Title: How to ORM non-disjoint specializations

Body:
I don't know how it's modeled in your application, but in the real world, people cannot only be both student and worker,  they can be workes at different companies at the same time. Now looking closer at the tables shows that they look like N:N join tables, don't they?

So what I would do is rename Student to Enrollment and Worker to Contract. Person now has two N:N relations,  one with Institution,  and one with Company. In terms of OOP:

class Person {
    Collection<Enrollment> enrollments;
    Collection<Contract> employmentContracts;
}


问:
Why do code-bases in n-tier development have an equal amount of, if not more, JavaScript code now?
I've been doing web programming for a long time now, and somewhere, I lost track of why we are doing what we are doing today (or how did we come to do things this way)?

I started out with basic ASP web development, and very early on, display and business logic were mixed on the page. Client-side development varied wildly (VBScript, different flavors of JavaScript), and we had plenty of warning about server-side validations (and so I stayed away from client-side logic).

I then moved to ColdFusion for a while. ColdFusion was probably the first web development framework that separated display and business logic using their tags. It seemed very clear to me, but very verbose, and ColdFusion was not in high market demand, and so I moved on.

I then jumped on the ASP.NET band wagon and started using their MVC approach. I also realized Java seemed to be an ivory tower language of enterprise systems, and also tried their MVC approach. Later on, ASP.NET developed this MVVM design pattern, and Java (precisely, J2EE or JEE) also struggled and came out with its MVC2 approaches.

But today, what I have discovered is that backend programming is not where the excitement and progress is anymore. Also, server-side based MVC practices seem to be obsolete (do people really use JSTL anymore?). Today, in most projects that I am on, I found out that JavaScript frameworks and client-side development is where all the exciting and innovative progresses are being made.

Why has this movement from server to client-side development taken place? I did a simple line count of one of my JEE projects, and there are more lines of code in JavaScript than Java (excluding third-party libraries). I find that most backend development using programming languages such as Java or C# is simply to produce a REST-like interface, and that all the hard effort of display, visualization, data input/output, user interactions, etc... are being addressed via client-side framework like Angular, Backbone, Ember, Knockout, etc...

During the pre-jQuery era, I saw plenty of diagrams where there was a clear, conceptual line between the M, V, and C in MVC in n-tier development. Post-jQuery, where are these lines drawn? It seems MVC and MVVM are all right there in JavaScript code, client-side.

What I want to know is, why did we make such a transition (from the emphasis of server-side programming to client-side, from favoring compiled languages to scripting languages, from imperative to functional programming, all of these seem to have occurred simultaneously) and what problems did this transition/shift solve?

答:
Shifting the computing load between the server and the client is a cyclical phenomenon, and has been so for quite some time.

When I was in community college the Personal Computer was just getting a head of steam.  But Ethernet was not in widespread use yet, and nobody had a local area network.  Back then, the college had a mainframe that handled student records and served as a platform for programming classes.  The administration had terminals that were connected to the mainframe on a time sharing basis, but the students had to punch cards to get their programming assignments done, an arduous process.  Eventually, they put in a lab where the students could sign up for time on a terminal, but it still took maybe a half hour or so to get your half-inch thick printout of errors.  All of the processing work was done on the mainframe (the server).

But mainframes were expensive, so companies started putting up local area networks, and the processing load shifted from the server to the individual client machines, which were powerful enough to run individual word processing, spreadsheet and line of business applications, but not powerful enough to share their processing power with others.  The server was a similar machine with similar capabilities (perhaps more memory and hard drive space), but was mostly used to share files.  This was called Client/Server.  Most of the processing had shifted to the client computers.

One of the drawbacks of doing all of the processing on the client machines was that you got locked into this perpetual cycle of software installation and upgrades, and all of the headaches that go with that.  The programming model of these machines (event-based, code-behind user interfaces) encouraged the creation of messy, difficult to maintain programs (big balls of mud).  Most end-users didn't have the skills to maintain their hardware and software properly, necessitating armies of IT maintenance personnel.

As the computers became increasingly more powerful, divisions of labor became possible.  Now you could have file servers, database servers, web servers, print servers and so on.  Each machine could be somewhat optimized for the task it was provided, and maintained by someone with the requisite expertise.  Programs could be written that ran in the web browser, so client installations were no longer required.  This was called Multi-Tier or n-Tier.   Browsers were essentially used as dumb terminals, just like in the mainframe days, though the method of communicating with the server was more sophisticated, less proprietary, and based on interrupt mechanisms rather than time-sharing and polling.  Processing had shifted back to the server(s).

However, web development came with a whole new set of headaches.  Most line of business applications written for the browser were static forms and reports.  There was very little interactivity available in the UI.  Javascript hadn't found its second wind yet, and there were major problems with browser incompatibilities that discouraged its widespread adoption.  However, things have gotten much better.  HTML5 and CSS3 provide substantial new capabilities to the browser programming model, jQuery came out and helped a whole generation of programmers discover how useful Javascript could be.  New front-end UI frameworks emerged.  It became possible to write highly-interactive UI's in the browser, even complete games.  Processing shifted back to the client again.

Today, you can buy processing power in the cloud, as much or as little as you like, and run programs on the server.  I'd say we're now in a place where, as a software developer, you have lots of choices about where you can execute your processing power, both on the client and on the server.


答:
Because users have always wanted the same functionality, bells and whistles with their web apps (not just web sites) that they had with desktop apps. Making this all run in a browser (actually multiple browsers) isn't like the old days when you could link a VB form to a database with little code. This is easier to accomplish when you don't have to make trips back to the server.

most backend development using programming languages such as Java or
  C# is simply to produce a REST-like interface, and that all the hard
  effort of display, visualization, data input/output, user
  interactions, etc... are being addressed via client-side framework
  like Angular, Backbone, Ember, Knockout, etc...

Maybe the backend programming/services seems like the same old thing, but it's the heart of the application. The coding practices may be more efficient in these areas. The tools, languages, browsers and frameworks are still evolving, so the UI/UX is difficult to develop. They are the new things that the old ASP didn't have. If we could get away with user interfaces with simple forms and html tables, there wouldn't be much interest/hype in those areas either, but users want drag and drop, animations, transitions, pop-ups, etc.

Title: Why do code-bases in n-tier development have an equal amount of, if not more, JavaScript code now?

Body:
I've been doing web programming for a long time now, and somewhere, I lost track of why we are doing what we are doing today (or how did we come to do things this way)?

I started out with basic ASP web development, and very early on, display and business logic were mixed on the page. Client-side development varied wildly (VBScript, different flavors of JavaScript), and we had plenty of warning about server-side validations (and so I stayed away from client-side logic).

I then moved to ColdFusion for a while. ColdFusion was probably the first web development framework that separated display and business logic using their tags. It seemed very clear to me, but very verbose, and ColdFusion was not in high market demand, and so I moved on.

I then jumped on the ASP.NET band wagon and started using their MVC approach. I also realized Java seemed to be an ivory tower language of enterprise systems, and also tried their MVC approach. Later on, ASP.NET developed this MVVM design pattern, and Java (precisely, J2EE or JEE) also struggled and came out with its MVC2 approaches.

But today, what I have discovered is that backend programming is not where the excitement and progress is anymore. Also, server-side based MVC practices seem to be obsolete (do people really use JSTL anymore?). Today, in most projects that I am on, I found out that JavaScript frameworks and client-side development is where all the exciting and innovative progresses are being made.

Why has this movement from server to client-side development taken place? I did a simple line count of one of my JEE projects, and there are more lines of code in JavaScript than Java (excluding third-party libraries). I find that most backend development using programming languages such as Java or C# is simply to produce a REST-like interface, and that all the hard effort of display, visualization, data input/output, user interactions, etc... are being addressed via client-side framework like Angular, Backbone, Ember, Knockout, etc...

During the pre-jQuery era, I saw plenty of diagrams where there was a clear, conceptual line between the M, V, and C in MVC in n-tier development. Post-jQuery, where are these lines drawn? It seems MVC and MVVM are all right there in JavaScript code, client-side.

What I want to know is, why did we make such a transition (from the emphasis of server-side programming to client-side, from favoring compiled languages to scripting languages, from imperative to functional programming, all of these seem to have occurred simultaneously) and what problems did this transition/shift solve?


问:
Best way to handle delimited files
So typically a CSV file uses a comma and the return character as it's field and line delimiters.

This runs into obvious problems with text which can contain both these characters.

Obviously there are options there (escaping them) but how do people handle this?  Use different characters - pipes or tildas?  Escape them?  Not use delimited files, after all it's 2010 and we have XML now?

Looking at least effort for a decent chance of not seeing problems.

(Just to be clear, this is a question out of curiosity rather than something more solid - it's something I've come up against time and time again playing with data, always got round it but it normally feels a bit, well, dirty, and wondered what other people's experience was).

答: Forget CSV, use JSON. Easy to write, easy to parse. XML is so 2005.

Title: Best way to handle delimited files


问:
How do you go about understanding others' code?
What do you do to understand some code that you didn't write? Or code that you wrote long time ago and don't remember what it does anymore.

Do you have some technique that you go about? Do you analyze the structures first, or the public methods, or do you draw flow charts, etc.? Or do you fire up the debugger and just step through it? Or do you just ad-hoc your way through until you understand it?

答:
When you're reading through a class, look at the attributes and behaviors that it has (methods + fields). For methods, take mental note of the method name, it's input parameters, and it's return type. If you don't know the method name (because it's obfuscated), then sometimes you can get this information by looking at the input, output, and implementation.

Take this partially obfuscated method as an example:

```<code>public double f(double d0, double d1, double d2)
  {
    double d3 = this.locX - d0;
    double d4 = this.locY - d1;
    double d5 = this.locZ - d2;

    return MathHelper.sqrt(d3 * d3 + d4 * d4 + d5 * d5);
  }
</code>```

It accepts three doubles, returns a double, but the telling part is the mathematical equation: square-root of a-squared plus b-squared plus c-squared: That's distance!

Now, you'll want to refactor f() to distance(). Decypher the easy ones first. Once you have more and more of the puzzle solved, it'll give hints into the harder to decypher areas.

My favorite trick is control+F. That's the search function. And you can use to search entire projects too (not just individual files). How do you even know what to search for, you ask ? Well, by using the application or library in some way... it'll give you hints as to what to search for. First, you have a general idea of what particular feature you wanna look for. Then, if you've used the project even just a little bit, you can get more specific clues to narrow your search.

For example, I'm working on a project now that I've inherited from an inactive author (an open-source Minecraft plugin with 418 files and 46k lines of code). And we had a user open a ticket: "What is the permission node to use join signs ?" Heck, I have no idea. And the documentation didn't mention it either. No sweat, control+F to the rescue. (My weapon of choice).

One of the things you can't always 100% decypher is the question of their design: "Why did they design it like this ?" One can only speculate.

Body:
When you're reading through a class, look at the attributes and behaviors that it has (methods + fields). For methods, take mental note of the method name, it's input parameters, and it's return type. If you don't know the method name (because it's obfuscated), then sometimes you can get this information by looking at the input, output, and implementation.

Take this partially obfuscated method as an example:

public double f(double d0, double d1, double d2)
  {
    double d3 = this.locX - d0;
    double d4 = this.locY - d1;
    double d5 = this.locZ - d2;

    return MathHelper.sqrt(d3 * d3 + d4 * d4 + d5 * d5);
  }

It accepts three doubles, returns a double, but the telling part is the mathematical equation: square-root of a-squared plus b-squared plus c-squared: That's distance!

Now, you'll want to refactor f() to distance(). Decypher the easy ones first. Once you have more and more of the puzzle solved, it'll give hints into the harder to decypher areas.

My favorite trick is control+F. That's the search function. And you can use to search entire projects too (not just individual files). How do you even know what to search for, you ask ? Well, by using the application or library in some way... it'll give you hints as to what to search for. First, you have a general idea of what particular feature you wanna look for. Then, if you've used the project even just a little bit, you can get more specific clues to narrow your search.

For example, I'm working on a project now that I've inherited from an inactive author (an open-source Minecraft plugin with 418 files and 46k lines of code). And we had a user open a ticket: "What is the permission node to use join signs ?" Heck, I have no idea. And the documentation didn't mention it either. No sweat, control+F to the rescue. (My weapon of choice).

One of the things you can't always 100% decypher is the question of their design: "Why did they design it like this ?" One can only speculate.


答:
Interesting question, I don't think I've ever documented the process before but here are a few things I like to do.

assessing

maybe I'm just extending one aspect of the author's code. If I can correctly limit my task to a class or group of dependencies this means I can use find / replace within a file, or dir search in my OS to check what folders and files contain a target string (i.e. class, var name etc.) and limit my code reading to the most essential parts and help isolate their dependencies.

doing grep -l  [text to find] [files to look in] is sweet for this kind of thing in Mac or Linux. No idea how this would be accomplished in Windows.

There might be API or dev docs even if it's a small project. If there are score. If not, it's time to really search out useful comments in the code.

researching

Taking the example that it's an internal project I've inherited--maybe from a long departed colleague who I've never met:

After reading code and documentation. I realize it's super annoying to do this, but now is possibly a good time to look at the tickets, issue tracking, bug reports or whatever project management tool / system you're company was using at the time the author's code was created. Maybe the issue you've been asked to fix is a long standing issue with a documented history thread.

manual labour

The previous steps were all designed to prep you for the intense amount of manual labour that is inherently involved in reverse engineering someone else's code. Up to now, most of the work has been focused on reading.

As others have suggested, moving stuff around and breaking it in a controlled way (i.e. ctrl + z is your friend in failed compiler tests) , stepping through with the debugger. Extending classes, plugins, etc. All great strategies you can use to limit the amount of re-writing and re-factoring that needs to be done.

large scale refactoring should be almost always be avoided. That said, refactoring small sections of code with well documented comments maybe even stating you are not the original developer can be quite helpful.

Substantial refactoring should only be conducted if the state of the code system actually requires it (i.e. it's running really slow, it's become a "big ball of mud", etc.) What you want to avoid is simply trading in one bug for a new one. In my experience, refactoring a project just to wrap your mind around it has unforseen consequences, and these consequences may only become evident at a much later date. Even with automated unit tests, in a large system it can be difficult to re-test every single aspect of a development cycle.

Title: How do you go about understanding others' code?

Body:
What do you do to understand some code that you didn't write? Or code that you wrote long time ago and don't remember what it does anymore.

Do you have some technique that you go about? Do you analyze the structures first, or the public methods, or do you draw flow charts, etc.? Or do you fire up the debugger and just step through it? Or do you just ad-hoc your way through until you understand it?


问:
Is possible to create and download a video file via JavaScript, only on the client side?
Is possible to create and download a video file only on the client side?

Suppose that we have an animated canvas like in this JSFIDDLE. Please see this snippet:

```<code>...
var stop = false;
setTimeout(function () {
    stop = true;
}, 1860);
...
if (!stop) { requestAnimationFrame( draw, 10 ); }
</code>```

So, my animation duration is 1860ms.

We can generate text files for download, I know that we can generate images from canvas, but is possible to render and download a video file?

Would adding sound possible?

答:
Disclaimer: I haven't done this myself so all the information I am providing is only what I have found by researching

In theory, yes it is possible but not easy or practical.

Overview

So basically you are looking at having more-or-less a video encoder in-browser which can take the images you get form the canvas and convert it to video.

I had a few concerns first whether it was possible to actually do this, mainly:

Is there a limit on Blob size? According to the W3C Blob spec, there doesn't seem to be.

How the browser will handle the amount of memory required to do encoding?

In your circumstance, the animation duration is short enough that I do not believe that memory will be an issue (at least in terms of storing the images and video).

Let's get encoding!

I mentioned that you are basically having a video encoder. There are really two options for this, either build your own encoder or hope that someone has built some sort of encoder already.

An awesome answer on GameDev (and later found something similar on StackOverflow) explains that FFmpeg can create a video slideshow from images.

For those who don't know, FFmpeg is a free software project that produces libraries and programs for handling multimedia data

I know what you might be thinking, how is FFmpeg going to help me, that isn't JavaScript... right?

Well, before today I would have thought the same thing until I found videoconverter.js which apparently is a working copy of FFmpeg in JavaScript!

What's the catch?

Well, the FFmpeg JS file is kind of large weighing in at ~6MB gzipped (or ~24 MB uncompressed). The browser memory footprint is bound to be quite large!

I will note I am also making a few big assumption here:

That the FFmpeg JS can actually read an image blob so we can build the video slideshow

That FFmpeg JS actually supports all the same functions as the standard FFmpeg application

Besides FFmpeg, what is another way?

You can pick the format of video you want to output and write your own encoder!

To get you started, you could look at the spec for AVI (Audio Video Interlaced) or possibly Theora. You would be writing the information to a blob like you did in your text download example.

This stuff really is out of my skill set so I will leave the finer details of this up to you.

This isn't practical! This is a mountain of effort for a small video!

I said from the beginning that it wasn't going to be easy. What @ShivanDragon mentioned in the comments is the way I would personally go about it. Pass the images off to a server, get it generated and then downloaded.

There are some reasons why you might want it in-browser but I really don't see it being practical.

Edit: Hang on! I also asked whether sound is possible!

Whoops, I missed that part! Yes, adding sound is definitely possible though will add even more complexity into the mix. You can add audio as a second input file for FFmpeg which will insert it into the output video.

In terms of making your own video encoder, it will add a decent amount of extra effort for adding an audio stream. Probably best place to start is another few links to specs for audio like Vorbis or WAV.


问:
Skeletal/Abstract class just to store shared state?
Given an interface for the Strategy pattern like so:

```<code>public interface Strategy {
  public Output execute(Input input);
  public Output getLastExecutionOutput();
}
</code>```

if I want to store the last execution result of any Strategy implementation is it correct to define an abstract class like this

```<code>public abstract class AbstractStrategy implements Strategy {
  protected Output lastOutput;
  public abstract Output execute(Input input);
  public abstract Output getLastExecutionOutput();
}
</code>```

so the implementations can inherit this state?

If this is ok then should the abstract class also implement the getLastExecutionOutput method, in which case the field should become private?

答:
IMHO you can do this, and there may be situations where this is the most simple solution (for example, when you are forbidden to change the interface in any way), but to my experience a more SOLID approach is not to make the getLastExecutionOutput part of the strategy interface. That would eliminate the need for the AbstractStrategy class at all.

Instead, look at the place (typically a template method) where execute is called. It is probably a better idea to let the caller store the last output by itself. The whole idea of the strategy pattern is to separate the parts of "different strategies" from the parts which are not different in each strategy. This makes your strategy objects simpler, with a better distribution of responsibilities. The interface in your example assigns each strategy object two mostly unrelated responsibilities (beyond the responsibility for providing the individual strategy code, it has the additional responsibility to cache the last output) and so implies a violation of the SRP.

Assumed you cannot change the interface and must implement the methods above: in this case I would consider to implement this in a slightly different way (I am not a Java guy, so please forgive me any potential syntactical errors):

```<code>public abstract class CachingStrategy implements Strategy {
  private Output lastOutput;
  public Output execute(Input input)
  {
       lastOutput = executeInternal(input);
       return lastOutput;
  }

  protected abstract Output executeInternal(Input input);

  public Output getLastExecutionOutput()
  {
       return lastOutput;
  }
}
</code>```

Now, when deriving from this class, you have to implement executeInternal instead of execute in each child class. The notable difference is that your child classes now don't have to provide code any more for setting lastOutput correctly, this responsibility is now completely in the CachingStrategy class.

As I wrote above, I think this is only the 2nd best solution. When you have two almost equal solutions, one with use of inheritance and one without, as a rule of thumb, try to use the one without inheritance first. Using inheritance here does not seem to be a big problem at a first glance, but think about what might happen when you get additional requirements "for all strategies" like "firing an event whenever execute was called", "caching not only the last output, but also the last input", and so on. The straightforward solution then seems to implement all these new functionalities into your "common base class", which might end up in a big bloat.

Body:
IMHO you can do this, and there may be situations where this is the most simple solution (for example, when you are forbidden to change the interface in any way), but to my experience a more SOLID approach is not to make the getLastExecutionOutput part of the strategy interface. That would eliminate the need for the AbstractStrategy class at all.

Instead, look at the place (typically a template method) where execute is called. It is probably a better idea to let the caller store the last output by itself. The whole idea of the strategy pattern is to separate the parts of "different strategies" from the parts which are not different in each strategy. This makes your strategy objects simpler, with a better distribution of responsibilities. The interface in your example assigns each strategy object two mostly unrelated responsibilities (beyond the responsibility for providing the individual strategy code, it has the additional responsibility to cache the last output) and so implies a violation of the SRP.

Assumed you cannot change the interface and must implement the methods above: in this case I would consider to implement this in a slightly different way (I am not a Java guy, so please forgive me any potential syntactical errors):

public abstract class CachingStrategy implements Strategy {
  private Output lastOutput;
  public Output execute(Input input)
  {
       lastOutput = executeInternal(input);
       return lastOutput;
  }

  protected abstract Output executeInternal(Input input);

  public Output getLastExecutionOutput()
  {
       return lastOutput;
  }
}

Now, when deriving from this class, you have to implement executeInternal instead of execute in each child class. The notable difference is that your child classes now don't have to provide code any more for setting lastOutput correctly, this responsibility is now completely in the CachingStrategy class.

As I wrote above, I think this is only the 2nd best solution. When you have two almost equal solutions, one with use of inheritance and one without, as a rule of thumb, try to use the one without inheritance first. Using inheritance here does not seem to be a big problem at a first glance, but think about what might happen when you get additional requirements "for all strategies" like "firing an event whenever execute was called", "caching not only the last output, but also the last input", and so on. The straightforward solution then seems to implement all these new functionalities into your "common base class", which might end up in a big bloat.


答:
i usualy stick to the standard and escape them. in most programming languages there is good builtin support or a good library available.

it depends on the situation which format will be used and CSV is a reasonable format to exchange simple data format structures.

Title: Best way to handle delimited files

Body:
So typically a CSV file uses a comma and the return character as it's field and line delimiters.

This runs into obvious problems with text which can contain both these characters.

Obviously there are options there (escaping them) but how do people handle this?  Use different characters - pipes or tildas?  Escape them?  Not use delimited files, after all it's 2010 and we have XML now?

Looking at least effort for a decent chance of not seeing problems.

(Just to be clear, this is a question out of curiosity rather than something more solid - it's something I've come up against time and time again playing with data, always got round it but it normally feels a bit, well, dirty, and wondered what other people's experience was).


问:
Optimizations in Compiler Design
While studying about Optimization in Compiler Design  I went through an article that tells about 'Algebraic Simplification And Reassociation' in optimization.It tells that using this(algebraic simplication) we could ignore code statements such as X = X * 1.

But I would like to know whether anyone uses these type of statements because naturally no one might never do such an multiplication.Or whether it shows any indirect optimization like breaking a huge code into statements then atlast getting these(X = X * 1) types of code statements.Could anyone help me.

答:
The examples in the document you're linking to are (and are intended to be) simplified examples of a particular type of optimization.  In the real world, of course, it isn't particularly likely that a developer is going to explicitly write a statement like x = x * 1 so it isn't particularly important that a compiler optimizes that specific statement.  On the other hand, there are plenty of more complex transforms of this type that a compiler can implement that would be much more likely to come in handy for actual programs.

The article that you links to talks about how applying algebraic simplification and reassociationis frequently used to complement other optimizations.  It can be used to rearrange expressions in order to implement operator strength reduction or constant folding or loop-invariant code motion.  A lot of the algebraic transforms that the compiler is going to apply will be with the goal of doing things like collecting all the constant terms of an expression together so they can be factored out, collecting all the loop invariant terms together so that they can be moved outside the loop, etc.

One nice example of this is the "Algebraic Simplifications of Addressing Expressions" section in this word doc.

If you want an example of a case where a very simple transform would be used, imagine something like

```<code>[(4*(a + b))/ (4*a)] * c
</code>```

doing algebraic simplification, you get

```<code>[(4*a + 4*b)/ (4*a)] * c
[(4*a)/(4*a) + (4*b)/(4*a)] * c
[1 + b/a] * c
(1*c) + (b/a)*c
c + (b*c)/a
</code>```

The simplification 1*c = c comes toward the end and only after several other algebraic simplifications and transforms created that expression.  Generally, it's not human developers that are writing that, it's the compiler applying other transforms that ultimately rely on the simple rules.

Title: Optimizations in Compiler Design

Body:
The examples in the document you're linking to are (and are intended to be) simplified examples of a particular type of optimization.  In the real world, of course, it isn't particularly likely that a developer is going to explicitly write a statement like x = x * 1 so it isn't particularly important that a compiler optimizes that specific statement.  On the other hand, there are plenty of more complex transforms of this type that a compiler can implement that would be much more likely to come in handy for actual programs.

The article that you links to talks about how applying algebraic simplification and reassociationis frequently used to complement other optimizations.  It can be used to rearrange expressions in order to implement operator strength reduction or constant folding or loop-invariant code motion.  A lot of the algebraic transforms that the compiler is going to apply will be with the goal of doing things like collecting all the constant terms of an expression together so they can be factored out, collecting all the loop invariant terms together so that they can be moved outside the loop, etc.

One nice example of this is the "Algebraic Simplifications of Addressing Expressions" section in this word doc.

If you want an example of a case where a very simple transform would be used, imagine something like

[(4*(a + b))/ (4*a)] * c

doing algebraic simplification, you get

[(4*a + 4*b)/ (4*a)] * c
[(4*a)/(4*a) + (4*b)/(4*a)] * c
[1 + b/a] * c
(1*c) + (b/a)*c
c + (b*c)/a

The simplification 1*c = c comes toward the end and only after several other algebraic simplifications and transforms created that expression.  Generally, it's not human developers that are writing that, it's the compiler applying other transforms that ultimately rely on the simple rules.


答:
Statements like x = x * 1 or y = y + 0 are more common than you might think. They may not be written exactly in that form in the (high-level) source code, but they occur frequently after converting the high-level code to three-address code.

Other optimisations, such as constant propagation, may also result in such trivial expressions.

A very common construct that can yield the expression y = y + 0 in three-address code is accessing the very first member of a compound data structure or class (in OO languages, this is often the vtable used for virtual dispatch).

Title: Optimizations in Compiler Design

Body:
While studying about Optimization in Compiler Design  I went through an article that tells about 'Algebraic Simplification And Reassociation' in optimization.It tells that using this(algebraic simplication) we could ignore code statements such as X = X * 1.

But I would like to know whether anyone uses these type of statements because naturally no one might never do such an multiplication.Or whether it shows any indirect optimization like breaking a huge code into statements then atlast getting these(X = X * 1) types of code statements.Could anyone help me.


问:
How to handle configuration of Python modules, especially when used standalone and in frameworks like Django
I am trying to package a Python module for pip, following the guide here.

One area I would like feedback on is best practices or convention for making my module configurable. The module is a library for talking to federated RESTful services / local datastores, and we use it both as a command-line library as well as a Django app. So I would like it to be manually configurable as well as configurable from Django's settings.py file.

Our current method of doing this is horrible -- it relies on a set of settings.py files inside of the library, which are overwritten whenever we pull from git, not changeable during runtime, etc.

My idea for a solution is to wrap the entire library in its own class, and in the __init__ method, do something like:

```<code>try:
    from django.conf import settings
    self._remote_host = settings.REMOTE_HOST
except:
    self._remote_host = DEFAULT_HOST
</code>```

But that only seems to take care of Django and seems cumbersome -- what if someone wants to use our library with Flask, or something else? Is there a more universal way to make a Python module configurable by external tools plus have a default? Or is this a lost cause, and I should stick to configuration on init via arguments?

答:
There are two problems come together here:

Your package is a singleton although there exist many different configurations.

Where to store the data and how to migrate - which database to use.

Singleton

I did a fun project with lots of modules which needed configuration themselves. The project was intended to run once per computer.

The alternative is to put everything into classes and instantiate them with a configuration. Tist would allow many different configurations to exist within one program. When you may have this need you should restructure your whole code.

Database

Maybe your project is for a company and shall be developed for a longer time and the configuration shall not be thrown away. Then you may need to keep in mind many previous configurations when changing the default values and updating the configuration. Proper databases have solved that problem.

My tradeoff solution

In my case

The configuration can be thrown away if my model changes.

The package runs not only once per process but also once per computer.

Only one thread accesses the configuration.

There is a module called config.py with the following methods:

```<code>import config
config.load()
config.save()
</code>```

You use it like this (Example1):

```<code>config.load()
config.my_value = 'test'
config.save()
</code>```

There is also a file called constants.py which should better be called default_config.py. It has the functions

```<code>import constants
constants.default_configuration()
constants.config_file_name() # where to store the config data
</code>```

And for (Example1) the constants.py should look like this:

```<code>def default_configuration():
    return {'my_value' : 'default'} # to avoid attribute errors
</code>```

The config module saves and loads the configuration using pickle. If no configuration is found for a variable the default configuration is used.

You will need a coding style that always fetches the configuration from the config.py. It shall not be stored in a local variable or attribute over a longer time since it can change.

My previous version is called runningConfiguration. It has no explicit load and save and also no default attributes.

Body:
There are two problems come together here:

Your package is a singleton although there exist many different configurations.

Where to store the data and how to migrate - which database to use.

Singleton

I did a fun project with lots of modules which needed configuration themselves. The project was intended to run once per computer.

The alternative is to put everything into classes and instantiate them with a configuration. Tist would allow many different configurations to exist within one program. When you may have this need you should restructure your whole code.

Database

Maybe your project is for a company and shall be developed for a longer time and the configuration shall not be thrown away. Then you may need to keep in mind many previous configurations when changing the default values and updating the configuration. Proper databases have solved that problem.

My tradeoff solution

In my case

The configuration can be thrown away if my model changes.

The package runs not only once per process but also once per computer.

Only one thread accesses the configuration.

There is a module called config.py with the following methods:

import config
config.load()
config.save()

You use it like this (Example1):

config.load()
config.my_value = 'test'
config.save()

There is also a file called constants.py which should better be called default_config.py. It has the functions

import constants
constants.default_configuration()
constants.config_file_name() # where to store the config data

And for (Example1) the constants.py should look like this:

def default_configuration():
    return {'my_value' : 'default'} # to avoid attribute errors

The config module saves and loads the configuration using pickle. If no configuration is found for a variable the default configuration is used.

You will need a coding style that always fetches the configuration from the config.py. It shall not be stored in a local variable or attribute over a longer time since it can change.

My previous version is called runningConfiguration. It has no explicit load and save and also no default attributes.


答:
You seem to be mixing two very different concepts:

Separating presentation and business logic (MVC) => increase maintainability

Assigning execution to a node => increase efficiency

Back in the days Client/Server computing was often confused to imply the first because clients generally did not offer much computing power, as compared to servers. So it seemed natural to move the "heavy" business logic computation (M) to servers, while keeping the "light" view processing (V) to clients. In turn you had to implement some kind of arbitrator (C) to translate between the two.

With clients now easily featuring process prowess that once implied some expensive server hardware the lines have blurred as to where to execute business logic -- client-side or server-side. Really, the answer depends on your specific use case and your choice of trade-offs, e.g.:

client latency v.s. complexity: server-side processing keeps systems simpler because no code needs to be deployed/downloaded to the client, however it comes at the cost of client-side latency during computation.

complexity v.s. server load: client-side computing may increase system complexity but it may also help to reduce server load.

decentralized application resilience v.s. central execution: in a world of mobile app, it may be important to keep clients working despite a network disconnect. However, this comes at the cost of managing multiple deployed versions of business logic.

This is a non-exhaustive list of many trade-offs.


问:
Client vs Creator in Factory Method and Abstract Factory patterns
This page descibes one important difference between Factory Method and Abstract Factory:

http://architects.dzone.com/articles/factory-method-vs-abstract

The difference, according to this page, is that in Factory Method pattern the Creator (i.e. the entity creating new objects) and the Client (i.e. the entity using the Creator) are the same class. More precisely, this pattern only defines a method, so the rest of the class is the Client. In Abstract Factory however, the Creator and the Client are separate classes. The Creator's only purpose is to create objects so only a separate class can be the Client.

Is this distinction correct? If so, why can't the Creator method in the Factory Method be put in a separate class? Would it create any problems? Similarly, why can't the Creator class in the Abstract Factory be the same class as the Client? Would this create any problems?

Update:

Just as a side note: it's SHOCKING to me too see that when I ask about such well known and (what should be) well defined design patterns I get actually contradictory answers. And it's not just this thread - the more and more I ask people (or search online resources) about the basic design patterns the more I start to think that people don't actually TRULY understand them even though they're using them on a daily basis and they believe they understand them.

答:
By having a method in Factory Method, you make it easy to customize the behaviour of a single class by simply deriving a new class from it and overriding a single method.  This is simpler than needing to create an entirely new factory class to perform the same customization.

However, if multiple classes need to be customized the same way, using a separate Factory object is simpler, because then you only need to create a single new class and inject objects of that class into all of the different clients.

In reality, Factory is a much more useful pattern than Factory Method because in most situations there is more than one client and therefore the Factory can easily be shared without creating a large number of subclasses in a variety of different places in the inheritance heirarchy.  It also favours composition over inheritance, which is a commonly-recommended design strategy these days.  Factory Method is therefore falling out of favour and is relatively rarely used in comparison to Factory.


问:
Significance of NFA in Compiler Design
While I was studying about Compiler Design it tells that we need 'finite automata' while designing a lexical analyzer like DFA or NFA. So I would like to know whether NFA is only used for conversion of (regular expressions to NFA and then to DFA). Is it possible to realize NFA practically? Or whether NFA is used because it is efficient than DFA?</p>

答: Realizing an NFA in practice means backtracking, since your program flow cannot be in multiple different states simultaneously. (There are parallel processors, of course, but it's hard to map the irregular and chaotic behaviour of user-written regexes to the straightforward, regular data flow that vectorization units are designed for.) Therefore, for deciding a pure "Does this match?" question, it's almost always better to transform the NFA further into a DFA and run that. But NFAs are still used in practice because DFAs cannot do some things that users definitely want, such as capturing subgroups of an expression.</p>

Title: Significance of NFA in Compiler Design


问:
What is the utility of the predictable SHA's in git?
I am learning git.

I am wondering why the git spec is explicit about how the commit SHA's are calculated? It is my understanding that the delta blob (the unit-of-work) is calculated into this SHA as well as information about the committing author.

Why is this? Wouldn't a GUID suffice?
Does the git architecture design for having two different commits at two different times to produce the same SHA?
Does the SHA serve as checksum to see that the data payload is not corrupted?

Can somebody also confirm that the SHA calculation is only considering the delta and not the complete resulting code structure?

答:
SHA vs. GUID

From The Git Object Model, emphasis is mine:

Git can quickly determine whether two objects are identical or not, just by comparing names.

Since object names are computed the same way in every repository, the same content stored in two repositories will always be stored under the same name.

Git can detect errors when it reads an object, by checking that the object's name is still the SHA1 hash of its contents.

So, yes, it is a protection against corruption. And that's why GUID won't work - it is random and does not depend on contents.

Checksums of what?

Also from the same article:

It is important to note that this is very different from most SCM systems that you may be familiar with. Subversion, CVS, Perforce, Mercurial and the like all use Delta Storage systems - they store the differences between one commit and the next. Git does not do this - it stores a snapshot of what all the files in your project look like in this tree structure each time you commit. This is a very important concept to understand when using Git.

Git stores binary snapshots, but not deltas, in blob objects. Directory structure and file names are stored in tree objects. Good explanation can be found here: Git Internals - Git Objects

Git calculates SHA checksums of this objects, not deltas or original files. blobs contain only contents of original files, while names of those files go into trees.

Learning Git

If you want to learn git as a user, you don't have to know about internals. At the moment, I've been using Git for over 3 years and haven't used any of this information in practice even once. It's good to know, but not required.

If you're interested in general architecture of Git, read this: Git in The Architecture of Open Source Applications.


答:
I'm assuming you have something like this:

```<code>Foo,Baz,,,"Foo,Baz"
</code>```

If strings that contain the delimiter aren't quoted or escaped, you have no real reliable way of parsing the file.

You can, however examine the data to parse and draw conclusions like:

Comma separated floats should be treated as a string

If the line before or after this contains less delimiters, skip parsing this line and log it

Treat ' like "

You do have to write a parser to handle stuff like that, but it doesn't have to be complicated.

In my experience, importing massive dumps from something like Excel always results in having to go back and review some oddballs. Your challenge is to give your program just enough common sense regarding the data so that it doesn't do a crazy insert. Then review what was logged and wash/rinse/repeat.

I once handled an internal FAQ for a small company that used all Ubuntu workstations. A part of the FAQ gave 'shell shortcuts', and it came to me pipe delimited. Well, the answers were also typically pipe delimited (i.e. grep foo | something) and not quoted or escaped. I feel that pain :)

Title: Best way to handle delimited files

Body:
I'm assuming you have something like this:

Foo,Baz,,,"Foo,Baz"

If strings that contain the delimiter aren't quoted or escaped, you have no real reliable way of parsing the file.

You can, however examine the data to parse and draw conclusions like:

Comma separated floats should be treated as a string

If the line before or after this contains less delimiters, skip parsing this line and log it

Treat ' like "

You do have to write a parser to handle stuff like that, but it doesn't have to be complicated.

In my experience, importing massive dumps from something like Excel always results in having to go back and review some oddballs. Your challenge is to give your program just enough common sense regarding the data so that it doesn't do a crazy insert. Then review what was logged and wash/rinse/repeat.

I once handled an internal FAQ for a small company that used all Ubuntu workstations. A part of the FAQ gave 'shell shortcuts', and it came to me pipe delimited. Well, the answers were also typically pipe delimited (i.e. grep foo | something) and not quoted or escaped. I feel that pain :)


答:
Linus Torvalds explains the reason for using a SHA hash in his git presentation at Google (By the way: I recommend anyone who wants to understand what git is all about to watch it completely):

|video|transcript|

Having a good hash is good for being able to trust your data, it happens to have some other good features, too, it means when we hash objects, we know the hash is well distributed and we do not have to worry about certain distribution issues. Internally it means from the implementation standpoint, we can trust that the hash is so good that we can use hashing algorithms and know there are no bad cases. So there are some reasons to like the cryptographic side too, but it's really about the ability to trust your data. I guarantee you, if you put your data in git, you can trust the fact that five years later, after it is converted from your harddisc to DVD to whatever new technology and you copied it along, five years later you can verify the data you get back out is the exact same data you put in. And that is something you really should look for in a source code management system.

One of the reasons I care is we actually had for the kernel a break-in on one of the BitKeeper sites, where people tried to corrupt the kernel source code repository, and BitKeeper actually caught it. BitKeeper did not have a really fancy hash at all, I think it is only 16-bit CRC, something like that. But it was good enough that you could actually see clumsy attempt, it was not cryptographically secure but it was hard enough in practice to overcome that it was caught immediately. But when that happens once to you, you got burned once, you do not ever want to get burned again. Maybe your projects aren't that important, my projects, they are important. There is a reason I care.

[...]

So maybe I am a cuckoo, maybe I am a bit crazy, and I care about security more than most people do. But the whole notion that I would give the master copy of source code that I trust and I care about so much I would give it to a third party is ludicrous. Not even Google. Not a way in Hell would I do that. I allow Google to have a copy of it, but I want to have something I know that nobody touched it. By the way I am not a great MIS person so disc corruption issue is definitely a case that I might worry about because I do not do backups, so it's Ok if I can then download it again from multiple trusted parties I can verify them against each other that part is really easy, I can verify them against hopefully that 20 bytes that I really really cared about, hopefully I have that in a few places. 20-byte is easier to track than 180MB. And corruption is less likely to hit those 20 bytes. If I have those 20 bytes, I can download a git repository from a completely untrusted source and I can guarantee that they did not do anything bad to it. That's a huge thing and that is something when you do hosted repositories for other people if you use subversion you are just not doing it right. You are not allowing them to sleep well at night. Of course, if you do it for 70... how many, 75,000 projects? Most of them are pretty small and not that important so it's Ok. That should make people feel better.

When you host your repository on a 3rd party site, you can use the cryptographic hash of every revision to make sure that they didn't tamper with it. When they just change a single byte, the hashes won't fit anymore. That means writing down the hash of your HEAD revisions from time to time prevents you from malicious manipulation of your codebase, even when you don't host your code yourself.

Title: What is the utility of the predictable SHA's in git?

Body:
I am learning git.

I am wondering why the git spec is explicit about how the commit SHA's are calculated? It is my understanding that the delta blob (the unit-of-work) is calculated into this SHA as well as information about the committing author.

Why is this? Wouldn't a GUID suffice?
Does the git architecture design for having two different commits at two different times to produce the same SHA?
Does the SHA serve as checksum to see that the data payload is not corrupted?

Can somebody also confirm that the SHA calculation is only considering the delta and not the complete resulting code structure?


问:
docker-izing a classical db-based webapp - single or multiple containers?
I have a classic Java webapp.  It is composed of a database (PostgreSQL), a servlet container (Tomcat) and my code (deployed on Tomcat as a *.war file).

I want to package/deploy it using Docker (mostly for testing for now), but I'm unsure what would be the best way to "map" it.

My initial idea was to have an app-in-a-box - define a container that has Java, Postgres and Tomcat on it, exposing just the http port.

Further reading of the Docker docs shows that this, although possible (install and run supervisord as the single foreground process, have it start both Postgres and Tomcat) is probably not the intended usage.  Going by the spirit of the tutorials I should probably create a container for Postgres, another for Tomcat, and a data-container to hold the application code (my *.war) and database files.  This would mean 3+ containers (should the db files and *.war share the same data container?)

What's the common practice here?

Since I have no previous experience with Docker, what pitfalls can I expect from each approach?

Is there some other approach I'm missing?

答:
Docker or not, I'd say don't put both webapp and db server in the same container.
Initially, it may work, but if/as application grows, you'll want to scale the frontend and the database individually.
Having different containers from the start will make it easier to move them around, duplicate them individually, etc.</p>


答:
Your distinction is not generally correct. For example, the Abstract Factory Pattern uses the Factory Method Pattern. In the Factory Method Pattern, the classes using and providing the factory method need not be the same class.

An Abstract Factory class has object creation as its sole concern. If an abstract factory instance were its own client, this would be a violation of the Single-Responsibility principle.

Below, you'll find a discussion of factory methods, the Factory Method Pattern, the Abstract Factory Pattern, and comparisons between them and other patterns.

Factories or Simple Factories or Factory Methods

A factory is any function that is used to create some data structure. In OOP languages, constructors are often special factory methods that can't be used freely (e.g. you often can't explicitly return something). By wrapping construction in an ordinary method, we can add validation, preprocessing, or further initialization code, and can return objects of different types depending on context. By using factory methods, users are insulated from the specifics of object construction – if we want to switch the type of some object, we only have to change the code inside the factory, and not all the code using that factory.

Simple factories are not a Go4 pattern, but form the basis of many patterns such as the Factory Method Pattern, Abstract Factory Pattern, and the Builder Pattern.

Factory Method Pattern

In the Factory Method Pattern, we have a class that contains an overridable factory method. Subclasses are encouraged to override this method to change the type of the returned object. Pseudocode:

```<code>interface Product { … }
class DefaultProduct implements Product { … }
class SpiffyProduct  implements Product { … }

class Creator {
  virtual Product create() { return new DefaultProduct() }
}

class OtherCreator extends Creator {
  override Product create() { return new SpiffyProduct() }
}

// client code:
Creator c = ...;
Product = c.create();
</code>```

Note that Creator::create is a factory method/simple factory. However, the identifying property of the Factory Method Pattern is the usage of inheritance to change the factory method.

The advantage of the Factory Method Pattern is that in order to change the factory method used in client code, we only have to change the line where the Creator c is obtained/created. This flexibility allows the Factory Method Pattern to be used for simple Dependency Injection tasks.

Variations

If we use the Factory Method Pattern and the client is in the same class as the factory method, then this can also be described as the Strategy Pattern applied as a creational pattern.

The root Creator class need not be instantiable, or provide some default product. Instead, it could be abstract or an interface.

One object can reasonably implement the Factory Method Pattern multiple times for different Products. If object creation is the main purpose of the creator and the different products are related, this is probably better described as the Abstract Factory Pattern.

The Factory Method Pattern is usually used as a helper in order to achieve that object's main concern (e.g. when used in combination with the Strategy Pattern). If the factory method is the only method of a class implementing the Factory Method Pattern, this can also be viewed as an example of the Command Pattern.

Abstract Factory Pattern

The Abstract Factory Pattern uses the Factory Method Pattern to create a palette of related objects. Or: the abstract factory instance is that palette.

While the Factory Method Pattern is often used as a helper to do something else, the main concern of a factory object in the Abstract Factory Pattern is always object creation. As such, the client is always supposed to be a different class, although an abstract factory could also use itself, e.g. to assemble a composite object.

Title: Client vs Creator in Factory Method and Abstract Factory patterns

Body:
This page descibes one important difference between Factory Method and Abstract Factory:

http://architects.dzone.com/articles/factory-method-vs-abstract

The difference, according to this page, is that in Factory Method pattern the Creator (i.e. the entity creating new objects) and the Client (i.e. the entity using the Creator) are the same class. More precisely, this pattern only defines a method, so the rest of the class is the Client. In Abstract Factory however, the Creator and the Client are separate classes. The Creator's only purpose is to create objects so only a separate class can be the Client.

Is this distinction correct? If so, why can't the Creator method in the Factory Method be put in a separate class? Would it create any problems? Similarly, why can't the Creator class in the Abstract Factory be the same class as the Client? Would this create any problems?

Update:

Just as a side note: it's SHOCKING to me too see that when I ask about such well known and (what should be) well defined design patterns I get actually contradictory answers. And it's not just this thread - the more and more I ask people (or search online resources) about the basic design patterns the more I start to think that people don't actually TRULY understand them even though they're using them on a daily basis and they believe they understand them.

Body:
Your distinction is not generally correct. For example, the Abstract Factory Pattern uses the Factory Method Pattern. In the Factory Method Pattern, the classes using and providing the factory method need not be the same class.

An Abstract Factory class has object creation as its sole concern. If an abstract factory instance were its own client, this would be a violation of the Single-Responsibility principle.

Below, you'll find a discussion of factory methods, the Factory Method Pattern, the Abstract Factory Pattern, and comparisons between them and other patterns.

Factories or Simple Factories or Factory Methods

A factory is any function that is used to create some data structure. In OOP languages, constructors are often special factory methods that can't be used freely (e.g. you often can't explicitly return something). By wrapping construction in an ordinary method, we can add validation, preprocessing, or further initialization code, and can return objects of different types depending on context. By using factory methods, users are insulated from the specifics of object construction – if we want to switch the type of some object, we only have to change the code inside the factory, and not all the code using that factory.

Simple factories are not a Go4 pattern, but form the basis of many patterns such as the Factory Method Pattern, Abstract Factory Pattern, and the Builder Pattern.

Factory Method Pattern

In the Factory Method Pattern, we have a class that contains an overridable factory method. Subclasses are encouraged to override this method to change the type of the returned object. Pseudocode:

interface Product { … }
class DefaultProduct implements Product { … }
class SpiffyProduct  implements Product { … }

class Creator {
  virtual Product create() { return new DefaultProduct() }
}

class OtherCreator extends Creator {
  override Product create() { return new SpiffyProduct() }
}

// client code:
Creator c = ...;
Product = c.create();

Note that Creator::create is a factory method/simple factory. However, the identifying property of the Factory Method Pattern is the usage of inheritance to change the factory method.

The advantage of the Factory Method Pattern is that in order to change the factory method used in client code, we only have to change the line where the Creator c is obtained/created. This flexibility allows the Factory Method Pattern to be used for simple Dependency Injection tasks.

Variations

If we use the Factory Method Pattern and the client is in the same class as the factory method, then this can also be described as the Strategy Pattern applied as a creational pattern.

The root Creator class need not be instantiable, or provide some default product. Instead, it could be abstract or an interface.

One object can reasonably implement the Factory Method Pattern multiple times for different Products. If object creation is the main purpose of the creator and the different products are related, this is probably better described as the Abstract Factory Pattern.

The Factory Method Pattern is usually used as a helper in order to achieve that object's main concern (e.g. when used in combination with the Strategy Pattern). If the factory method is the only method of a class implementing the Factory Method Pattern, this can also be viewed as an example of the Command Pattern.

Abstract Factory Pattern

The Abstract Factory Pattern uses the Factory Method Pattern to create a palette of related objects. Or: the abstract factory instance is that palette.

While the Factory Method Pattern is often used as a helper to do something else, the main concern of a factory object in the Abstract Factory Pattern is always object creation. As such, the client is always supposed to be a different class, although an abstract factory could also use itself, e.g. to assemble a composite object.