问:
Scrum: relaxing "done" or using spikes?
Consider what it takes to completely finish a story in our organization.

Demo requirements were met

UI design finalized (layout, styles, fonts, controls, colors, etc...)

User text finalized and mistake-proofed

All text translated to Spanish, Germal, Italian and French.

User manual updated

All bugs deemed by PO as need fixing for release were fixed

Documents for FDA updated (SRS,STD,STP,STM,UFMEA, DFMEA, SDD, installation)

Requirements were written and approved by PO

UI spec was written and approved by PO

All tests (final acceptance and integration) written/updated and executed - synced and agreed with test manager

All FAT tests were approved by QPE

Regression testing was done

Code was reviewed and documented

Unit tests were written and executed - synced and agreed with software manager.

Refactoring, if needed, was done - synced and agreed with software architect.

Design was documented

Installer was updated (If needed)

With so much work needed to fully complete a story, the team stops being nimble and it becomes more difficult to try out new features quickly.

As far as I see there are 3 ways to solve this problem:

Do more "Spike" stories which don't meet the "Done" definition but serve as quick prototypes to gather feedback from users.

Get feedback from users all the time during the Sprint rather than at the end of it. So half-way through the story you could already gauge what users think of the story and quickly adapt before doing all the heavy stuff (translations, user manual and such).

Relaxing the "Done" definition to require just basic testing and bug fixing. This way the team won't have a potentially shippable product at the end of the Sprint, but it will be very quick to try new features and technical innovations without suffering from the burden of documenting everything, writing user manual, doing design polishes, etc...

Which option would you pick and why?

Thank you.

答:
I note that your question uses the tag "Scrum" and yet, your list of tasks to finish a story leads me to believe that you cannot be doing Scrum. Here's why:

Scrum doesn't require demo's. The closest we get is the Sprint Review, which is still very different from a demo

Scrum doesn't mandate requirements or specifications. The closest we get is Product Backlog items and the overhead in creating them is considerably smaller than for creating requirements and specifications

Scrum doesn't have a role of 'test manager'. If you're using an external one, you may be breaking a fundamental element of Scrum (that the team be fully cross-functional)

Ditto for the role of 'software manager'

Ditto 'software architect'

It seems to me that you have a variant that may be based on Scrum but is not Scrum. I suggest that your work flow has some process heavy elements applied to it that are dragging you down and hence your very understandable discomfort.

My advice would be to go back to the roots of Scrum, identify your dysfunctions, and then work hard to resolve them.

Title: Scrum: relaxing "done" or using spikes?


问:
DDD with a 'School' domain
I've been asked to draw out the Bounded Contexts (BC) and the possible aggregate and the roots of a School domain.

The domain consists of students who study modules on a course. The submit their assignment for a module which is then marked by an external tutor.

The 'Marking' firstly sticks out as a BC, but im unsure about the rest... Also a 'module' would appear on multiple Bounded Contexts so how would i handle this?

答:
I'm not sure, but I think you're not going about this entirely the right way.

The domain is the field your client works in, but what is most important is how they view that domain. There are a gazillion schools out there, but they don't all work the same. They don't all have the same rules, procedures or lingo.

So you have to find out how your client works: map out the behaviours. E.g. a student enrolls: what effect does this have across the entire school and all of its departments?

You also have to mark out where the same words start to have different meanings, because that's usually a sign you've found another bounded context. E.g. a student is something different to a teacher, than to the accounting department.

It's completely normal to have for example 2 student classes, with different or even the same properties in 2 bounded contexts. Focus on the behaviour, not the data; don't normalize prematurely.

If you have multiple bounded contexts, you generally want to be able to communicate between them. Most of the time, this is accomplished by sending domain events (e.g. StudentEnrolledInCourseEvent) over a message bus like RabbitMQ. You can then process these events on the other side however you wish.

If you have a map of the behaviours and marked different bounded contexts, you can start to look at what needs to be transactionally consistent; those groups will be your aggregates.

I can't think of a school related example right now, but consider a car with weels. You can control the wheels of the car through the api of the car, but not directly, because you want to avoid having one wheel pointing left and another weel pointing right.

Keep in mind that plenty of entities are aggregates on their own; not all aggregates are "nested" entities.

I hope this was of some help.

ContentLicense: CC BY-SA 3.0

Title: DDD with a 'School' domain


问:
Good strategy to implement a listener processing tasks provided by the database
Let's assume this conceptual scenario:

The database is constantly populated with "a ton" of tasks named PleaseSendAnEmail.

A scheduler/listener in background is aimed to handle those tasks.

Of course, rules are that a specific mail shouldn't be sent more than once and that the handling should be the fastest possible.

Possible status for the task are: New, InProgress(facultative regarding the strategies below), Complete and Fail.

Various strategies for the listener (I present one process iteration for each):

Single threaded solution:

Querying one task whose status is "New".

Sending the task to the worker EmailSender.

When the worker did the job, passing the status to Complete

Pros of this solution: Safety !

Cons: Slow, very slow..

Multi-threaded solution:

Querying several tasks at once whose status is "New".

Multiple EmailSender workers are instantiated: Dispatching all the tasks to them

As soon as a worker has handled one occurrence, its corresponding status is set to Complete.

Meanwhile the workers are processing, a new query is processed, then tasks dispatched to the worker queues etc.. the whole being asynchronous.

Pros: Wow! Fast!

Cons: Potential race conditions, since a same task being currently processed could be retrieve again by the next query since the current worker processing it might not completed yet => More than one mail occurrence could be sent! Bad!

Multi-threaded solution involving the InProgress status:

Querying several tasks at once whose status is "New".

Updating immediately those tasks with the status InProgress.

Multiple EmailSender workers are instantiated: Dispatching all the tasks to them

As soon as a worker has handled one occurrence, its corresponding status is set to Complete.

Meanwhile the workers are processing, a new query is processed, then tasks dispatched to the worker queues etc.. the whole being asynchronous.

Pros: multi-threaded and safety! No more mails than expected!

Cons: Increasing access to database...to mark status as InProgress..possible contention on database with a huge number of events.

Multi-threaded solution avoiding queries while at least one worker is still processing:

Querying several tasks at once whose status is "New".

Multiple EmailSender workers are instantiated: Dispatching all the tasks to them

As soon as a worker has achieve one task, its corresponding status is set to Complete.

Meanwhile the workers are processing, a new query is processed, then tasks dispatched to the worker queues etc.. the whole being asynchronous.

Pros: Multi-threaded, safety! No need to involve a locking status as InProgress!

Cons: If for any reasons, one mail takes 8 seconds to be sent, it will delay the next query!
Many jobs could be seen as idle, awaiting for the next query...

Which solution should I use?

Is there any other efficient solution?

答:
There are two key questions to consider. Once you answer those, the rest should fall into place.

First is: who blocks? In the ST solution, the scheduler queue blocks waiting for a single server to complete. In the MT solution the servers block when they run out of work to do and the listener blocks only when there is nothing for it to do. Maximum throughput requires MT plus excess servers. Other considerations may limit how many.

Second is: who knows? If the task status is public, known to all and updated by tasks then your database accesses will be limiting. If the task status is private, known only to and updated only by the listener then database updates are no longer a bottleneck (you can update them occasionally to tell the outside world).

So one listener with a private task status (in memory data structure) and enough email server tasks to minimise waiting times should meet all the requirements you've stated. No race conditions, no unnecessary waiting, completely safe, minimal controlled access to the database. Joy.


问:
Global request context - anti-pattern?
I was talking today to a colleague of mine about Python web frameworks and our impressions about them. I told him I think Flask having a global request smells badly and is an anti-pattern.

The docs say about request context:

In contrast, during request handling, a couple of other rules exist:

while a request is active, the context local objects (flask.request and others) point to the current request.

any code can get hold of these objects at any time.

I think I understand the idea behind this design decision -- to make the application simpler. It's just a compromise, like in the case of Thread Locals:

Yes it is usually not such a bright idea to use thread locals. They
  cause troubles for servers that are not based on the concept of
  threads and make large applications harder to maintain. However Flask
  is just not designed for large applications or asynchronous servers.
  Flask wants to make it quick and easy to write a traditional web
  application.

Is patching a global object with the current request information an anti-pattern?

I believe it is, because it is in the view of static code analyzer a global state, though it's not. And I as a programmer will not understand how it works without reading the docs carefully. And this has consequences on tests.

Isn't it a good practice to pass the request as an argument to views? I think it's more readable, explicit and easier to debug. And avoids global state.

答:
Many web frameworks have this same structure: a global request. In a sense, it's the right thing to do because hey, there really IS only one request at a time.

So is there any point in passing the request around as a parameter? No. The request is the request, and parameters are for passing in different things at different times.

The real problem comes as you start to consider lower levels of a larger application. With a global request there is the temptation to write code all over the place that accesses the request globally. That is a very bad thing. It produces coupling between different parts of the code, makes it hard to change things and makes it hard to test things.

So my answer is: keep the global request and live with it. However, wherever an individual module or function does not need the whole request, pass only the data it needs in as a parameter. Pass just the referrer, or the url, or the command tail and whatever bits you need into your functions. This will help keep the code modular, reduce coupling and improve testability.

For tiny programs it scarcely matters, but for bigger ones this can be a real lifesaver.

Title: Global request context - anti-pattern?


问:
Are "Stocks" a TDD/Unit testing anti pattern?
From Martin Fowler's Mocks Aren't Stubs he states that Stubs provide canned answers to calls made during the test while Mocks objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

In addition several sources state that Stubs shouldn't fail tests, but Mock could.

So what about this:

```<code>$validator= $this->getMock('ValidatorService');

        // Set up the expectation for the isValid() method
        // to be called only once and with the string 'textToBeValidated'
        // as its parameter and returns true or false.
        $validator->expects($this->once())
                 ->method('isValid')
                 ->with($this->equalTo('textToBeValidated'))
                 ->will($this->returnValue(true));
</code>```

This came up in a code review in a project I'm working on and we called it a Stock (St ub - M ock). Why you ask?!

It set an expectation for isValid() to be called only once and with certain input like a Mock.

It returns a canned answer like a Stub.

I get a feeling that this is wrong, but what if we used a pure Stub only:

```<code> $validator->expects($this->any())  //<-- can't fail the test now
                     ->method('isValid')
                     ->will($this->returnValue(true));   //<-- canned answer
</code>```

I can't verify that the method isValid was called only once with the right parameters.

If the test passed I'm not sure that it passed for the right reason. Maybe isValid() was called twice and that caused it to pass.

So let's use a pure Mock instead:

```<code>$validator->expects($this->once())
                     ->method('isValid')
                     ->with($this->equalTo('textToBeValidated'))
</code>```

I can't verify that my function works correctly, because I only verified that call to isValid() was successful. Maybe there is a bug in the rest of my code using the ValidatorService.

I know some people will talk about state vs behavior verification, but the way I see it the only way to be sure that the test passes for the correct reason is to to use a Stock (i.e. verify the behavior and final state).

Of course the other option is to write 2 test for every test criteria. One using a Mock and an identical test using a Stub. However, that will result in a TestOverflow (a huge number of nearly identical tests).

What is the correct way to have a concrete test of a function using a dependency?

答:
OK I did some research and finally found what I was looking for in the post Mocks for Commands, Stubs for Queries by Mark Seemann.

To make a long story short Stocks are bad. They are a code smell or better a unit testing smell indeed.

Having experienced the pain of over specifying interactions between classes in my code I find "J. B. Rainsberger" post to reflect this ugly truth:

When one uses a mock in place of a stub, one tends to over-specify the
  collaboration between a Client and its Suppliers, and that leads to
  brittle tests. These tests tend to make hyperactive assertions,
  meaning assertions that fail even when the underlying production code
  works as expected. Such hyperactive assertions play the role of the
  boy who cried “wolf!”, distracting us with needless concern. Even when
  these superfluous mocks don’t over-specify a collaboration, they often
  duplicate specifications in other tests.

So next time you find yourself writing "expects" and "returns" in the same fake object think again.


问:
Optionally Using GPL Library via System API in Closed-Source Application
Please consider the following situation:

You write a closed-source application, let's call it A. A depends on a system API (i.e. provided by the OS), that is in turn configurable to use different backends. Each backend is identified by a simple string value and also selected via this identifier. The API itself is not bound by any GPL-like requirements.

Now, a specific backend (B) would be preferred but is GPL-licensed. The application would run without it, and there are other backends available, just with worse performance or other downsides.

Is any of the following permitted?

Hard-coding the identifier to B

Automatically selecting any available backend, but preferring B if available

Letting the user select a backend (and strongly suggesting B)

Would it also depend on whether I ship A and B together, or just A and suggest the user that installing B would improve the application performance?

I would generally think that all three options are o.k. since passing a specific string to a system API would hardly qualify for a derived work (IMHO), but the FAQ of B basically state that it does not matter how you call a function of B, any usage makes your program a derived work. Are they right even in the three cases outlined above?

P.S.: It is not my decision to make A closed-source, so GPL'ing it is not an option.
P.S. 2: I omitted the names of the system API and B to keep the question more generic, but I can add them if it makes any difference.

edit: From another point of view: When does the user (as opposed to the programmer) create a derived work? In the third case, it should be clear that the user creates the derived work since he installed A and B and actively selected B. This would be in line with MSalters' arguments in his comment. The second case at least makes it clear that the work does not depend on B, so this could be o.k., too.

Probably it would be most in the spirit of the GPL to just contact the authors; however, I'm not sure if their FAQ entry is in line with the actual text of the GPL.

答:
If library B is using the plain GPL license (not the LGPL or a variant, like GPL with classpath or linking exception), then the FAQ for B is correct in so far that the Free Software Foundation (the ones who drafted the GPL licenses) consider a program a derived work from a piece of GPL code if that GPL code gets executed in the same process as the rest of the program.

To clarify the stance of the FSF, see these answers from their FAQ:

If a library is released under the GPL (not the LGPL), does that mean that any software which uses it has to be under the GPL or a GPL-compatible license? (#IfLibraryIsGPL)

Yes, because the software as it is actually run includes the library.

Does the GPL have different requirements for statically vs dynamically linked modules with a covered work? (#GPLStaticVsDynamic)

No. Linking a GPL covered work statically or dynamically with other modules is making a combined work based on the GPL covered work. Thus, the terms and conditions of the GNU General Public License cover the whole combination. See also What legal issues come up if I use GPL-incompatible libraries with GPL software?

Can I apply the GPL when writing a plug-in for a non-free program? (#GPLPluginsInNF)

If the program uses fork and exec to invoke plug-ins, then the plug-ins are separate programs, so the license for the main program makes no requirements for them. So you can use the GPL for a plug-in, and there are no special requirements.

If the program dynamically links plug-ins, and they make function calls to each other and share data structures, we believe they form a single program, which must be treated as an extension of both the main program and the plug-ins. This means that combination of the GPL-covered plug-in with the non-free main program would violate the GPL. However, you can resolve that legal problem by adding an exception to your plug-in's license, giving permission to link it with the non-free main program.

See also the question I am writing free software that uses a non-free library.

The fact that library B implements a non-GPL API might make a difference, but (seeing the last FAQ quoted above) doesn't seem to be considered different by the FSF.

You can make use of library B in your closed source program, but you are not permitted to distribute the combination of the two. If your program is written to work with a generic (non-GPL) API and works fine without library B, then you can distribute your program without the library just fine and users are also allowed to download library B separately and use it in combination with your application.

The big question is if you can actively refer to library B in your source code or documentation without it being considered a derived work of library B. This is a legal question that we don't have the expertise to answer. You will have to consult a lawyer for that.

Without mentioning library B, you would at least have the possible defense that you weren't aware of GPL-licensed implementations of the API, but only a lawyer can tell you how far that would get you in court.


问:
Does tweeting 9 to 5 hurt job applicants?
If you were looking into a job applicant's background and discovered that he or she has 1200 followers on Twitter and averages 50 tweets per day (more than half of which are during business hours), would it affect your hiring decision? How and why?

Personally, I'd be a little worried about the candidate's ability to focus on the job at hand if they're constantly checking in with their 'tweeps' thoughout the day. In non-tech jobs, a lot of companies simply block Twitter as an irrelevant distraction.  But it can be a useful resource---to programmers in particular. I just wonder how much is too much. At what point does it become a red flag?

答: There's a good chance you shouldn't hire this person.  If you've got to the stage of considering whether their Twitter profile is good enough reason to reject them, then I suspect you have some fairly serious doubts.</p>

ContentLicense: CC BY-SA 2.5


问:
How to develop an algorithm for brute-forcing / backtracking?
As a beginner programmer, I don't know how to conceptually think about brute-forcing. My mind can't really fathom how to write code that will try every possibility.

I have a problem that I want to solve. Here is a code snippet (there are other functions, but no reason to include them here, they just do background work):

```<code>main():
    step_segment = []           # contains current segment
        STATS = [250,0,0,0,13,0]        # contains 1 step (current step stats)
        # STATS = [step_id, danger, danger_limit, rnd, b_loop, enc]

    # Temporary Interface
    choice = None
    while choice != "0":
        print \
        "\n1 - Benchmark Run"
        choice = raw_input("Choice: ")
        if choice == "0":
            print "\nGoodbye."

        elif choice == "1":
            while (len(step_segment)) < 128:
                step_segment, STATS = take_step(step_segment, STATS)
                if STATS[5] == "B":
                    "Dont know what to do here. Try every possibility!?"
</code>```

The goal: I want to produce a list of every possible 'route' through a segment, along with how long it takes.

Description:

I will take a step in the route (There's only one direction: forward). Every time I take a step, my stats are updated. This takes a step and updates the stats: step_segment, STATS = take_step(step_segment, STATS)

A list of steps taken, along with the stats, are kept in
step_segment. This is so I can 'undo' an arbitrary amount of
steps, if I want. To undo a step call the function:
step_segment, STATS = undo_step(step_segment, STATS)

I can see how long my current route has taken by doing: time = frames(step_segment).

At some point, I will get into a Battle. I get into a Battle when
STATS[5] == "B"

When there is a battle, I simply have two choices: i. Fight the
battle, or, ii. Run away.

If I want to Fight, I do: step_segment = do_fight(step_segment, STATS). This also records that I chose to fight, along with the stats, in step_segment. (So I can undo it, if i want).

If I want to Run Away, I do: step_segment = run_away(step_segment,STATS).

Can someone advise me, how I can code a brute forcer for this problem?

Specifically, I would like to know how I can tell the computer to try every possibility; I simply cannot think how to... >.<'

I want to see every possible combination of Run & Fight (the only two choices, when I reach a battle).

There are only around 200 possibilities. I only need to take 128 steps, so there are a finite amount of possibilities, hence: while (len(step_segment)) < 128.

I just don't know how to accomplish such a thing in Python. My whole reason for learning how to program is to solve this problem..

I think I can explain the solution in English, as I have done above, but I don't know how to code it. I would be very appreciative if someone could advise me on this!!

Thank you.

答:
There are more "possibilities" than you might think, I will explain to you why:

As I understand your explanation of your scenario, you have effectively a tree structure of steps.
You start at the "root" (your first step) and then go up.

At every point where you encounter a battle and thus get to choose, you create a branch of possibilites:
Your path splits into a "do fight" path and a "don't fight" path, on which you will continue your journey.

These branches will branch again at every battle point. When you reach your "end", you will backtrack to the previous decision and take the opposite action.

From a programmers perspective, this would be an ideal situation for a recursive algorithm, as you know you have a finite amount of steps.

The keywords to look for are "recursive tree traversal". I can not help you with specific code, as I have no experience with python.


问:
Design for creating a menu based on user type
Problem

I am building a PHP application which has a main menu, which in turn contains various "sections" or "subheadings" with various links. There are several user types such as SuperAdmin, Admin, Teacher, Student, etc, and each type should see a potentially different menu structure. It seems that my client would like to add more types down the line.

Currently I have a huge if/elseif/... block that checks what user type you are and creates an array with arrays for all the sections, containing yet more arrays for each link (these arrays contain the url, class and caption of the url). It's about 700 lines long at the moment. As you can see, this is becoming quite complex and tiresome to get working. So what I've attempted to do is use some design patterns (of which my knowledge is not that amazing).

Failed attempts

Firstly I attempted to create a few Builder implementations: one for the link itsself, one for the "section", and finally one for the menu itself. Then I attempted implementing a Strategy pattern to check current user type, and add links to sections and these sections to the menu. The final attempt was to serialize this menu object to the DB and just read it from there (as some user types can have dynamically configured sections/links)

The real question

I have no clue whether my rationale is even remotely correct in this regard, because I've created and completely deleted all my code about 4 times now as it becomes complex to comprehend just what must go where. So I guess my actual question is this: If you had to build this kind of implementation for menus (or anything where multiple objects are built into another object, and multiple objects of that type are built into yet another object), how would you go about it? What patterns would be useful and how would one structure them to work together without killing your brain?

Thanks!

答:
It sounds like you have a few user types. Why don't you create a hierarchy of menu classes so at the top is an AbstractMenu and at the bottom are StudentMenu, TeacherMenu etc.? See also the accepted answer for this code review, which suggests to use an interface instead of an abstract class.

Besides that you may consider creating Menu and MenuFactory classes so that you could for example call MenuFactory->getStudentMenu() and obtain a concrete instance of Menu that suits a Student user. Then details of creating user-type-specific menus will be encapsulated in separate methods.

You can even combine these approaches and have static information populated in class constructor and dynamic information added by a factory.


答:
A person who Tweets much can implicitly mean that the person also reads many Tweets. This makes hiring even more uncertain.
For an employer who doesn’t know anything about Twitter makes it even worse.

Title: Does tweeting 9 to 5 hurt job applicants?

Body:
If you were looking into a job applicant's background and discovered that he or she has 1200 followers on Twitter and averages 50 tweets per day (more than half of which are during business hours), would it affect your hiring decision? How and why?

Personally, I'd be a little worried about the candidate's ability to focus on the job at hand if they're constantly checking in with their 'tweeps' thoughout the day. In non-tech jobs, a lot of companies simply block Twitter as an irrelevant distraction.  But it can be a useful resource---to programmers in particular. I just wonder how much is too much. At what point does it become a red flag?


问:
Does a repository belong in this project?
I have a project that gets all of its data from API calls.

The server that hosts this API connects with it's own database (not even sure what DB it is, etc).

More or less the idea of a repository is to separate data and map it, right? I shouldn't be concerned where my data comes from. But if, at the end of the day, they are API calls, should the repository itself be doing all the api calls within its methods?

For example if we had a FindBY method in the repository, should that method be the one making the API call?

答:
The gist of using a repository is to allow replacing the repo object with a mock repo for automatic testing purposes. If your API implementation allows to be replaced that easily (in-memory, without the need for a 2nd server, without any horrible overhead), then you don't need a repository.

For example, you want to write an automatic test for code which uses your FindBY method - can you easily create an environment in which the FindBY does not actually call a DB server? When the code under test calls "FindBY", it should get a predefined result set from your mock repository, so the automatic test runs fast, and if it fails, you know for sure it does not fail because the network is down or the data in the DB server was changed.


问:
How can you decompose a constructor?
Lets say I have a Enemy class, and the constructor would look something like:

```<code>public Enemy(String name, float width, float height, Vector2 position,
             float speed, int maxHp, int attackDamage, int defense... etc.){}
</code>```

This looks bad because the constructor has so many parameters, but when I create an Enemy instance I need to specify all of these things. I also want these attributes in the Enemy class, so that I can iterate through a list of them and get/set these parameters. I was thinking maybe subclassing Enemy into EnemyB, EnemyA, while hardcoding their maxHp, and other specific attributes, but then I'd lose access to their hardcoded attributes if I wanted to iterate through a list of Enemy (consisting of EnemyA's, EnemyB's, and EnemyC's).

I'm just trying to learn how to code cleanly. If it makes a difference, I work in Java/C++/C#. Any point in the right direction is appreciated.

答:
A code example to add to Rory Hunter's answer (in Java):

```<code>public class Enemy{
   private String name;
   private float width;
   ...

   public static class Builder{
       private Enemy instance;

       public Builder(){
           this.instance = new Enemy();
       }

       public Builder withName(String name){
           instance.name = name;
           return this;
       }

       ...

       public Enemy build(){
           return instance;
       }
   }
}
</code>```

Now, you can create new instances of Enemy like this:

```<code>Enemy myEnemy = new Enemy.Builder().withName("John").withX(x).build();
</code>```

Title: How can you decompose a constructor?

Body:
A code example to add to Rory Hunter's answer (in Java):

public class Enemy{
   private String name;
   private float width;
   ...

   public static class Builder{
       private Enemy instance;

       public Builder(){
           this.instance = new Enemy();
       }

       public Builder withName(String name){
           instance.name = name;
           return this;
       }

       ...

       public Enemy build(){
           return instance;
       }
   }
}

Now, you can create new instances of Enemy like this:

Enemy myEnemy = new Enemy.Builder().withName("John").withX(x).build();


答:
TL;DR: The FSF doesn't make a distinction between process and program, suggesting restrictions on your program which only apply to the resulting process.

The GPL works fundamentally via copyright. In particular, it's a distribution license (doesn't cover use) and is limited to works that are placed under the GPL.

Now one of the problems is when a work starts falling under the GPL, and what happens to it. I believe that the FSF is right, and your process falls under the GPL once it loads plugin B.

Your process. Not your program. Only the process in memory which combines the two, that is the derived work. Before the combination was made, no derived work exists in a legal sense. Therefore the GPL cannot dictate terms on your program. It simply has no legal basis.

Of course, you may use the derived work created by combining your program and plugin B in a single process. The GPL explicitly allows use. Now, if you ran this process in a VM, and cloned the VM, you wold be banned from distributing this VM image. Those rare things are covered. But usually, processes aren't copied so their legal protection under copyright is rather pointless.

Title: Optionally Using GPL Library via System API in Closed-Source Application

Body:
Please consider the following situation:

You write a closed-source application, let's call it A. A depends on a system API (i.e. provided by the OS), that is in turn configurable to use different backends. Each backend is identified by a simple string value and also selected via this identifier. The API itself is not bound by any GPL-like requirements.

Now, a specific backend (B) would be preferred but is GPL-licensed. The application would run without it, and there are other backends available, just with worse performance or other downsides.

Is any of the following permitted?

Hard-coding the identifier to B

Automatically selecting any available backend, but preferring B if available

Letting the user select a backend (and strongly suggesting B)

Would it also depend on whether I ship A and B together, or just A and suggest the user that installing B would improve the application performance?

I would generally think that all three options are o.k. since passing a specific string to a system API would hardly qualify for a derived work (IMHO), but the FAQ of B basically state that it does not matter how you call a function of B, any usage makes your program a derived work. Are they right even in the three cases outlined above?

P.S.: It is not my decision to make A closed-source, so GPL'ing it is not an option.
P.S. 2: I omitted the names of the system API and B to keep the question more generic, but I can add them if it makes any difference.

edit: From another point of view: When does the user (as opposed to the programmer) create a derived work? In the third case, it should be clear that the user creates the derived work since he installed A and B and actively selected B. This would be in line with MSalters' arguments in his comment. The second case at least makes it clear that the work does not depend on B, so this could be o.k., too.

Probably it would be most in the spirit of the GPL to just contact the authors; however, I'm not sure if their FAQ entry is in line with the actual text of the GPL.


问:
How does persistence fit into a purely functional language?
How does the pattern of using command handlers to deal with persistence fit into a purely functional language, where we want to make IO-related code as thin as possible?

When implementing Domain-Driven Design in an object-oriented language, it's common to use the Command/Handler pattern to execute state changes. In this design, command handlers sit on top of your domain objects, and are responsible for the boring persistence-related logic like using repositories and publishing domain events. The handlers are the public face of your domain model; application code like the UI calls the handlers when it needs to change domain objects' state.

A sketch in C#:

```<code>public class DiscardDraftDocumentCommandHandler : CommandHandler<DiscardDraftDocument>
{
    IDraftDocumentRepository _repo;
    IEventPublisher _publisher;

    public DiscardDraftCommandHandler(IDraftDocumentRepository repo, IEventPublisher publisher)
    {
        _repo = repo;
        _publisher = publisher;
    }

    public override void Handle(DiscardDraftDocument command)
    {
        var document = _repo.Get(command.DocumentId);
        document.Discard(command.UserId);
        _publisher.Publish(document.NewEvents);
    }
}
</code>```

The document domain object is responsible for implementing the business rules (like "the user should have permission to discard the document" or "you can't discard a document that's already been discarded") and for generating the domain events we need to publish (document.NewEvents would be an IEnumerable<Event> and would probably contain a DocumentDiscarded event).

This is a nice design - it's easy to extend (you can add new use cases without changing your domain model, by adding new command handlers) and is agnostic as to how objects are persisted (you can easily swap out an NHibernate repository for a Mongo repository, or swap a RabbitMQ publisher for an EventStore publisher) which makes it easy to test using fakes and mocks. It also obeys model/view separation - the command handler has no idea whether it's being used by a batch job, a GUI, or a REST API.

In a purely-functional language like Haskell, you might model the command handler roughly like this:

```<code>newtype CommandHandler = CommandHandler {handleCommand :: Command -> IO Result)
data Result a = Success a | Failure Reason
type Reason = String

discardDraftDocumentCommandHandler = CommandHandler handle
    where handle (DiscardDraftDocument documentID userID) = do
              document <- loadDocument documentID
              let result = discard document userID :: Result [Event]
              case result of
                   Success events -> publishEvents events >> return result
                   -- in an event-sourced model, there's no extra step to save the document
                   Failure _ -> return result
          handle _ = return $ Failure "I expected a DiscardDraftDocument command"
</code>```

Here's the part I'm struggling to understand. Typically, there'll be some sort of 'presentation' code which calls into the command handler, like a GUI or a REST API. So now we have two layers in our program which need to do IO - the command handler and the view - which is a big no-no in Haskell.

As far as I can make out, there are two opposing forces here: one is model/view separation and the other is the need to persist the model. There needs to be IO code to persist the model somewhere, but model/view separation says that we can't put it in the presentation layer with all the other IO code.

Of course, in a "normal" language, IO can (and does) happen anywhere. Good design dictates that the different types of IO be kept separate, but the compiler doesn't enforce it.

So: how do we reconcile model/view separation with the desire to push IO code to the very edge of the program, when the model needs to be persisted? How do we keep the two different types of IO separate, but still away from all the pure code?

Update: The bounty expires in less than 24 hours. I don't feel that either of the current answers has addressed my question at all. @Ptharien's Flame's comment about acid-state seems promising, but it's not an answer and it's lacking in detail. I'd hate for these points to go to waste!

答:
The general way to separate components in Haskell is through monad transformer stacks.  I explain this in more detail below.

Imagine we're building a system that has several large-scale components:

a component that talks with the disk or database (submodel)

a component that does transformations on our domain (model)

a component that interacts with the user (view)

a component that describes the connection between view, model, and submodel (controller)

a component that kickstarts the whole system (driver)

We decide that we need to keep these components loosely coupled in order to maintain good code style.

Therefore we code each of our components polymorphically, using the various MTL classes to guide us:

every function in the submodel is of type MonadState DataState m => Foo -> Bar -> ... -> m Baz

DataState is a pure representation of a snapshot of the state of our database or storage

every function in the model is pure

every function in the view is of type MonadState UIState m => Foo -> Bar -> ... -> m Baz

UIState is a pure representation of a snapshot of the state of our user interface

every function in the controller is of type MonadState (DataState, UIState) m => Foo -> Bar -> ... -> m Baz

Notice that the controller has access to both the state of the view and the state of the submodel

the driver has only one definition, main :: IO (),  which does the near-trivial work of combining the other components into one system

the view and submodel will need to be lifted into the same state type as the controller using zoom or a similar combinator

the model is pure, and so can be used without restriction

in the end, everything lives in (a type compatible with) StateT (DataState, UIState) IO, which is then run with the actual contents of the database or storage to produce IO.


问:
How do I licence my OS project, which uses other (differently-licenced) projects?
I've got a library up on GitHub, with some of the binaries also distributed on NuGet:
https://github.com/relentless/Http.fs

I'd ignored licencing up until now, but somebody's asking about it so I though I should put something on.  I want to keep it permissive, and having done a little research MIT seems like a good one to use.  However, my library references/uses three other libraries, all with different licences, so I'm not quite sure what I need to do.

It includes the binaries of:

FsUnit, which has it's own licence (similar to the BSD 3-clause): https://github.com/fsharp/FsUnit/blob/master/license.txt

NancyFx, which uses MIT: https://github.com/NancyFx/Nancy/blob/master/license.txt

And a source file from:

FSharpX, which uses Apache 2.0: https://github.com/fsprojects/fsharpx/blob/master/LICENSE.md

The MIT of Nancy doesn't seem to be a problem.  The Apache of fsharpx and the FsUnit one  both say I need to include the licence text in my project.

So my questions are:

What do I do with the other licences, just copy the files into the root of my source?  I assume I can rename them and add a comment specifying which part of my project this licence refers to.

Assuming I go for MIT, could I reasonably describe my project as having an MIT licence?  Or is the reality that it's a mixed MIT/Apache 2.0/Random licence?

答:
The good news is all of the licenses are compatible.  The most restrictive being the ASL 2.0 which requires that there are no patents in the library licensed by the ASL.  The open source projects I've seen that have had multiple licenses for the libraries follow a pattern like this:

Each license is in a file like this: LICENSE.{library_name}.txt

If there are a number of different licenses, they are in a folder named LICENSES

So with your library it would look something like this:

```<code>/LICENSE.txt
/LICENSE.Fsunit.txt
/LICENSE.NancyFx.txt
/LICENSE.FSharpX.txt
</code>```

LICENSE.txt would be the license for your project.  It's up to you what license you feel most comfortable with.  You would be perfectly fine with the MIT license for your source code.

Also, if the FsUnit library isn't directly linked to in your library's distributable, but only used to unit test your code, then you don't have worry about that license.  The only time you need to worry about the license is if the library is being redistributed with your code.

Body:
The good news is all of the licenses are compatible.  The most restrictive being the ASL 2.0 which requires that there are no patents in the library licensed by the ASL.  The open source projects I've seen that have had multiple licenses for the libraries follow a pattern like this:

Each license is in a file like this: LICENSE.{library_name}.txt

If there are a number of different licenses, they are in a folder named LICENSES

So with your library it would look something like this:

/LICENSE.txt
/LICENSE.Fsunit.txt
/LICENSE.NancyFx.txt
/LICENSE.FSharpX.txt

LICENSE.txt would be the license for your project.  It's up to you what license you feel most comfortable with.  You would be perfectly fine with the MIT license for your source code.

Also, if the FsUnit library isn't directly linked to in your library's distributable, but only used to unit test your code, then you don't have worry about that license.  The only time you need to worry about the license is if the library is being redistributed with your code.


问:
Is it bad practice to allow user defined fields?
Generally speaking, is it considered bad practice to allow for user created fields in a database for a webapp?

For example, I am making a home inventory webapp for my wife, and she is going to want to define her own fields for different items.  I was planning to allow her to create item categories, and add "features" to those categories.  Features would just be key/value stored as strings.  That way if she had a category called "Audio CDs" for example, she could add features for stuff like "artist", "tracks", etc.  But in another category like "furniture", she could add features for stuff like "material" (wood, plastic, etc).  Then any item could belong to one (or many) categories, adding those features to the item.

I can see issues where searching by these features requires string comparisons, there's no validation of data, etc.  Following agile methodology, maybe it would be better to just have her come up with new categories and attributes and I would just have to create new tables as we go.  In my example, it's a small userbase (2 of us) and the amount of records created would be small, so not too bad.

Generally speaking though, how do people handle something like this in "real life"?

答:
Is it bad practice to allow user defined fields?

No, it is not bad practice. It is quite common. In OO terms this is called inheritance. You have a base class inventoryItem and two inherited classes AudioCD and furniture.

Generally speaking though, how do people handle something like this in "real life"?

You have to decide how inventoryItem, AudioCD and furniture are stored in database.

If easy-query is most important for you and db-space/normalisation doesn't matter you would implement the "table-per-hierarchy" schema.

If space/normalisation is most important for you and more complicated queries are no problem for you would implement the "table-per-type" Schema.

For more details see dotnet table-per-type-vs-table-per-hierarchy-inheritance
or java hibernate inheritance.


问:
How to decouple simple factory and default implementation?
I have a simple factory <s>class</s> (FileResources) with static factory methods providing a  default implementation (DefaultFileResource).

```<code>public final class FileResources {
    private FileResources() {}
    public static FileResource get(Path path) {
        return new DefaultFileResource(path);
    }
}
</code>```

The problem with this class is that it is tightly coupled with the default implementation  (DefaultFileResource).

Given a new requirement that classes part of the API (FileResources) cannot directly depend on classes not part of the API (DefaultFileResource) I figured that the only solution is inversion of control, either dependency injection or a <s>service provider</s> service locator.

Is it possible to decouple FileResources from the default implementation (DefaultFileResource)?

As far as I know this is only doable by injecting the dependency into FileResources through a constructor, field or setter method.

The main problem is that the constructor of FileResources is private, furthermore it only has static factory methods.

The alternative is the factory method pattern. There would be an abstract factory (an API class), and there would be concrete factories (non-API classes) that could be even injected to decouple the API and non-API classes.

Either way a dependency container is necessary to handle the wiring.

What do you think? Am I overthinking this?

答:
What to do really depends on how far you want to go into abstractions. A simple way to take is to add a static .getInstance(Path path) method to the abstract FileResource class (if it's an interface, you would change it into an abstract class), which would then return a default instance. This may not exactly meet your needs (FileResource would now be dependent on the default implementation instead), and your app may run into inheritance issues if you change an interface into an abstract class.

The second option is to create a FileResourceFactory. It can either be a class or an interface with implementing classes. A single implementing class is simpler, but creates dependencies on implementations, while an interface means more complexity but potentially less coupling.

Since this is a static class, dependency injection through the constructor is not possible, but you can create a setter method for a static FileResourceFactory, and inject it when you need to. Two warnings arise here, though:

You cannot guarantee that the static factory has been set before your get method is called

You are wandering dangerously close to global variable territory with a static member on a class like this

I also want to point out that, at some point, someone eventually needs to grab something concrete. With the dependency injection approach, you can push this pretty far out, but eventually someone will be dependent on getting a concrete instance somewhere.


答:
No, an IOC container is not necessary to decouple the API and non-API classes. Because they're not really coupled.

FileResources is coupled to Path and to FileResource, but not to DefaultFileResource. That is, if you change Path or FileResource, there is a risk of breaking FileResources. Albeit not a large one. Those are pretty thin layers; that's an acceptable coupling.

However, if you change DefaultFileResource, as long as it still conforms to FileResource, you cannot break FileResources.

You wouldn't even break any tests for FileResorces. They test only that a DefaultFileResource is returned, under the default conditions, and that's fine.

Even more importantly, your calling code is not coupled to any particular implementation of FileResource either. That's good. In fact it's the whole point of a Factory Method. (And this is a Factory Method. There is no Factory Class distinction, in the context you've used it; it doesn't matter whether the Factory Method exists in another class.)

However, your calling code IS coupled to FileResources. Most notably, if you change the implementation of FileResources, there is every chance you would break any unit tests that are testing your calling code, because the get method cannot be stubbed out.

That is a much bigger concern than any knowledge FileResources has about DefaultFileResource.

The only reason you would use an IOC container for the factory is if you want to build up DefaultFileResource with its dependencies. I would avoid that unless you need it and, if you should need it, I would take a look around at the Service Locator pattern and why is should be avoided, before I decided on the use of that IOC container.

Title: How to decouple simple factory and default implementation?

Body:
I have a simple factory <s>class</s> (FileResources) with static factory methods providing a  default implementation (DefaultFileResource).

```<code>public final class FileResources {
    private FileResources() {}
    public static FileResource get(Path path) {
        return new DefaultFileResource(path);
    }
}
</code>```

The problem with this class is that it is tightly coupled with the default implementation  (DefaultFileResource).

Given a new requirement that classes part of the API (FileResources) cannot directly depend on classes not part of the API (DefaultFileResource) I figured that the only solution is inversion of control, either dependency injection or a <s>service provider</s> service locator.

Is it possible to decouple FileResources from the default implementation (DefaultFileResource)?

As far as I know this is only doable by injecting the dependency into FileResources through a constructor, field or setter method.

The main problem is that the constructor of FileResources is private, furthermore it only has static factory methods.

The alternative is the factory method pattern. There would be an abstract factory (an API class), and there would be concrete factories (non-API classes) that could be even injected to decouple the API and non-API classes.

Either way a dependency container is necessary to handle the wiring.

What do you think? Am I overthinking this?


问:
Does commercial mean when you earn money by putting a ad to support you or is it something else like for a big company?
I been reading a few things about using libraries and stuff but they all talk about not using it for commercial purposes.

Does commercial mean when you earn money by putting a ad to support you or is it something else like for a big company?

答:
You note in your comments that you're most interested in three.js, a JavaScript 3-D animation library.

The license file for three.js is the plain MIT License.  Examining the history makes it plain that, aside from a brief stint where it was released via GPL 3.0, the license has been MIT since the project began. Neither the MIT license nor the GPL prohibit commercial use, although the latter does require you to distribute source code to any users of your software who request it.

Are you thinking of some other library?

In general, software publishers who mark software "not for commercial use" really should spell out what they mean, even if it's only by referencing a particular law's definition.  Especially since Creative Commons is a thing now.

FWIW, even Creative Commons has had problems with their "commercial use" definition.  They even commissioned a study, and found a rather clear lack of consensus.  And that ambiguity is precisely what you don't want as a licensor, or even really a licensee.

If you encounter a "no commercial use" clause in a library you want to use, you can either contact the maintainer and ask about your specific use, or decide to be mean and just plow ahead with what you think you could convince a judge a reasonable person might interpret as "no commerical use."  (no points for guessing which one is the wiser move.)


答:
As none of the libraries has a copyleft license, you are free to choose any license you like for your own library. And if you choose to use the MIT license, then your library is MIT licensed. The fact that you use third-party libraries that have a different license does not affect that.

If you don't distribute those libraries along with your library, then there are no further considerations. The copyright licenses only come into effect when you distribute the library/program that the license applies to.

If you do distribute the libraries along with yours, then the easiest way to meet the requirements of the licenses is to put a complete copy of each library in a directory of its own (like you have done with your packages directory).
Then you have all the required license files for each of those libraries in your distributing in a place where they are easy to find and associate with the right library.

Title: How do I licence my OS project, which uses other (differently-licenced) projects?

Body:
I've got a library up on GitHub, with some of the binaries also distributed on NuGet:
https://github.com/relentless/Http.fs

I'd ignored licencing up until now, but somebody's asking about it so I though I should put something on.  I want to keep it permissive, and having done a little research MIT seems like a good one to use.  However, my library references/uses three other libraries, all with different licences, so I'm not quite sure what I need to do.

It includes the binaries of:

FsUnit, which has it's own licence (similar to the BSD 3-clause): https://github.com/fsharp/FsUnit/blob/master/license.txt

NancyFx, which uses MIT: https://github.com/NancyFx/Nancy/blob/master/license.txt

And a source file from:

FSharpX, which uses Apache 2.0: https://github.com/fsprojects/fsharpx/blob/master/LICENSE.md

The MIT of Nancy doesn't seem to be a problem.  The Apache of fsharpx and the FsUnit one  both say I need to include the licence text in my project.

So my questions are:

What do I do with the other licences, just copy the files into the root of my source?  I assume I can rename them and add a comment specifying which part of my project this licence refers to.

Assuming I go for MIT, could I reasonably describe my project as having an MIT licence?  Or is the reality that it's a mixed MIT/Apache 2.0/Random licence?


问:
How do I stop designing and start architecting this project as suggested by my lead?
I'm a junior developer (~3 years' exp.) and at my job we're in the process of architecting a new system. My lead developer will be the principal architect, however he's challenged me to try architecting the system myself (in parallel).

Over the course of a few iterations of brainstorming ideas and proposing what I saw as architecture suggestions, my lead has given me the feedback that most of what I've been doing was "designing" and not "architecting".

He described the difference as architecture being implementation-agnostic whereas a design is the description of an implementation. He said I need to take off my designer hat and put on my architect hat. He gave me a little bit of advice on how to do so, but I would like to ask you as well:

How do I get out of software designer mode and start thinking more like an architect?

Here are some examples of "designs" I came up with that weren't seen as relevant to the architecture by my lead:

I came up with an algorithm for loading and unloading resources from our system and my lead said that algorithms are categorically not architecture.

I came up with a set of events the system should be raising and in what order it should raise them, but this too didn't seem to cut it as architecture.

I seem to be getting caught up in the details and not stepping back far enough. I find that even when I come up with something that is at an architecture level, I often got there by trying out various implementations and mucking around in the details then generalizing and abstracting. When I described this to my lead, he said that I was taking the wrong approach: I needed to be thinking "top down" and not "bottom up".

Here are some more specific details about the project:

The project we're architecting is a web application.

I'm estimating around 10-100 thousand lines of code.

We're a start up. Our engineering team is about 3-5 people.

The closest thing I could compare our application to is a lightweight CMS. It has similar complexity and deals largely with component loading and unloading, layout management, and plug-in style modules.

The application is ajax-y. The user downloads the client once then requests data as it needs it from the server.

We will be using the MVC pattern.

The application will have authentication.

We aren't very concerned about old browser support (whew!), so we're looking to leverage the latest and greatest that is out there and will be coming out. (HTML5, CSS3, WebGL?, Media Source Extensions, and more!)

Here are some goals of the project:

The application needs to scale. In the near term our users will be on the order of hundreds to thousands, but we're planning for tens of thousands to millions and beyond.

We hope the application will be around forever. This isn't a temporary solution. (Actually we already have a temporary solution, and what we're architecting is the long-term replacement for what we have).

The application should be secure as it may have contact with sensitive personal information.

The application needs to be stable. (Ideally, it'd be stable around the level of gmail but it doesn't need to be at the extreme of a Mars rover.)

答:
First of all I'd say that the difference between architecture and design is mainly semantics. Some teams have check points between the two. Your technical lead defines the architecture as before the design and architecture as implementation agnostic. From that I assume we are talking about design as in the waterfall model and not Industrial Design which would help you design the product with a view to users before you get to the software architecture. I think architecture often slips into design and that is not necessarily a bad thing, it is often very helpful for the architect to have a deep knowledge of what is possible within the system at hand.

Having said all that, you need some advice for the situation you are in. There is a whole world of software architecture out there, papers, books, conferences but you are generally looking for patterns and abstractions. Without more details on the project I can only give a broad example. For instance, if you were working in integration there is the Service Oriented Architecture (SOA) pattern where you split up parts of the system into 'services' so you can work with each part in a defined way, in web program this is often then implemented as Web Services (although shouldn't be though of as limited to that) and more recently the rise of RESTful APIs with JSON, again I would say this is a design coming from the architecture of SOA. I would say Model, View, Controller (MVC) is another example of an architecture pattern in common use, splitting up responsibility of the components of a system to allow for parts to be swapped out, to contain errors and testing.

From a 10,000ft level, if you can draw it on a whiteboard and explain it to a competent programmer who doesn't work in your field and doesn't know your programming language and current implementation details then it is probably architecture. If you can write a book  about it that anyone outside of your company would care about then it is probably architecture. If you find your self explaining detail and can't generalise it to other code-bases / companies / industries then it is probably design.

I would agree that the two examples you give are code design and not architecture. The first because I think when you say you came up with an 'algorithm' for loading resources I think you mean you designed a set of instructions to accomplish that task, and not that you designed a new algorithm that they will be teaching in 1st year COMSC next year. In the second example, again I agree it is design. If you showed me either of these ideas I wouldn't be able to use them in my random software project. You have to go to a 'higher level', Object Oriented (OO) in Java rather than I want the Customer Class to be a sub-class of the Person Class. Even talking about Exceptions in general could be considered too low level (too close to the implementation).

To try to address the specifics that you list, I think what you should be thinking about is how to architect a web based CMS. Wordpress has a site architecture codex where they talk a lot about design implementation details but it is clear from the post that their main architecture centers around making Wordpress extensible with themes. Architecting a clear interface for a theme such that it could be written by someone out side of the company was clearly an architecture decision they took. These are the kinds of things it is good to get down on paper when architecting your 'long-term' (not temporary) solution so that all the design and implementation decisions that are made during development (by all the developers not just the architect) are in-line with this idea.

Other examples of architecture for your situation:

Putting the whole thing on virtual machines, hosted on a cloud provider or in house and having stateless machine instances, so that any machine failing can be replaced with a new instance of a virtual machine without having to copy across any state or losing any information.

Building in live production failure testing from the beginning with
chaos simians.

Maybe try drawing the whole system on a whiteboard. Try it at different levels of detail, first board could be GUI->dispatcher->backend->DB or something, and then drill down until you start using pronouns.


问:
Decoupling Server and Client using REST API
I was thinking about how I can decouple a web-application completely into a server-side and a client-side component. I want to decouple the app to the extent that I can host both components on seperate servers.

So, for example, I would have:

Server 1 (API Server): A server-side component running on Django on something like Heroku or EC2.

Server 2 (Static Server): A client-side component running on AngularJS on a static server like S3 or CloudFront.

The communication between these components will take place using a JSON REST API.

Questions:

Is this approach common? advisable?

Does a company like Facebook or Twitter mostly utilise the same API for the webapp as it does for its mobile apps or its open API?

Is it a good idea to use oAuth2 for the login process?

So the user is redirected to a login page on Server 1 (this is the only page on Server 1) and then redirected back to Server 2 with a token if authentication succeeds. Is this the best approach?  It seems like I am kinda "breaking the flow" if I do this. Is this normal?

The motivation for this is for me to be able to use the same API for my web, iOS and Android clients.

Thanks!

答:
This is very common and helps with when you want to design a mobile app. Since your application will already be "resource oriented" instead of being focused on generating data with the html/visual layer attached.

I recommend http://django-rest-framework.org/ for the Django side, I've contributed and also use it daily. Helps provide a lot of the building blocks on top of Django that make decoupling easy and powerful.

As for the oAuth, you can, you don't have to. If you check that website it even shows you to how to set up oAuth with the API. You can easily just use Username/Password authentication as it is defaulted to. Just have the angular app send the username/password combo to the endpoint, which will reply with a "token". As long as your app is hosted using good TLS () the token is safe. You can easily add a method for refreshing tokens for more security.

OwnerDisplayName: nknj

OwnerDisplayName: stormlifter


答:
Maybe this will help. I've always seen the seniority of a engineer as a question of how big a problem they can solve on their own.

Roughly, to convey the idea:

You can give someone new to programming small, contained tasks with lots of explicit instructions about how the task needs to integrate with other pieces

A mid-level dev is someone who can take a description of some portion of an application, and make it work within the context of that application.

A senior dev can build a medium-sized application from scratch, within the technical constraints of a shop.

A more senior dev can do that, and make technology choices along the way about what technologies to use to make it work well.

...but those aren't hard and fast rules. And some people come out the gate as "senior" IMHO, even if they have to spend some time with a different title.

What the architect is asking is to view the problem even more generally than that. If you had to put together a number of applications to make the system work:

What applications and services will you need?

What pieces interact with customers, and which interact with each other?

How will they communicate?

Where will they store data?

Where are the risks of failures?

How will you provide reliability?

How will you provide security?

So, in a sense technical architecture is like a building architecture. It's the layout, or the plan. It shows whats needed for the various parts, how they hold together, and just as importantly why.

(BTW, I've had a similar growth curve explained to me for architects, ranging from architecting a family of related applications or a set of very complex features, to setting technical direction for a group, to making strategic technical decisions for an entire organization.)

That said, I think most engineers at all levels have to do some "architecting" as well. It's not a bright line. It sounds like if you focus on the Big Picture first, and not get hung up on the implementation details, you'll be more in line with what he's looking for. BTW the ability to see the Big Picture as well as the Little Details is a huge asset in this industry, so this sounds like a great opportunity.

...Here's an analogy. Let's say you're asked to create a Magic Black Box. As an engineer, you're expected to obsess about the inner workings of the Magic Black Box. But as an architect, your focus is different. You might peek into the box out of curiosity, but you're expected to obsess about everything around all the Magic Black Boxes.

Similar to that analogy, you might think about the architecture role as viewing the whole system as the magic box. So if take a Gigantic Glass Box and you put the customers, the client apps, the firewall, the service tier, the database, and even the devops guys inside, then as architect you're to obsess about how to make that huge system box work well.

Title: How do I stop designing and start architecting this project as suggested by my lead?

Body:
I'm a junior developer (~3 years' exp.) and at my job we're in the process of architecting a new system. My lead developer will be the principal architect, however he's challenged me to try architecting the system myself (in parallel).

Over the course of a few iterations of brainstorming ideas and proposing what I saw as architecture suggestions, my lead has given me the feedback that most of what I've been doing was "designing" and not "architecting".

He described the difference as architecture being implementation-agnostic whereas a design is the description of an implementation. He said I need to take off my designer hat and put on my architect hat. He gave me a little bit of advice on how to do so, but I would like to ask you as well:

How do I get out of software designer mode and start thinking more like an architect?

Here are some examples of "designs" I came up with that weren't seen as relevant to the architecture by my lead:

I came up with an algorithm for loading and unloading resources from our system and my lead said that algorithms are categorically not architecture.

I came up with a set of events the system should be raising and in what order it should raise them, but this too didn't seem to cut it as architecture.

I seem to be getting caught up in the details and not stepping back far enough. I find that even when I come up with something that is at an architecture level, I often got there by trying out various implementations and mucking around in the details then generalizing and abstracting. When I described this to my lead, he said that I was taking the wrong approach: I needed to be thinking "top down" and not "bottom up".

Here are some more specific details about the project:

The project we're architecting is a web application.

I'm estimating around 10-100 thousand lines of code.

We're a start up. Our engineering team is about 3-5 people.

The closest thing I could compare our application to is a lightweight CMS. It has similar complexity and deals largely with component loading and unloading, layout management, and plug-in style modules.

The application is ajax-y. The user downloads the client once then requests data as it needs it from the server.

We will be using the MVC pattern.

The application will have authentication.

We aren't very concerned about old browser support (whew!), so we're looking to leverage the latest and greatest that is out there and will be coming out. (HTML5, CSS3, WebGL?, Media Source Extensions, and more!)

Here are some goals of the project:

The application needs to scale. In the near term our users will be on the order of hundreds to thousands, but we're planning for tens of thousands to millions and beyond.

We hope the application will be around forever. This isn't a temporary solution. (Actually we already have a temporary solution, and what we're architecting is the long-term replacement for what we have).

The application should be secure as it may have contact with sensitive personal information.

The application needs to be stable. (Ideally, it'd be stable around the level of gmail but it doesn't need to be at the extreme of a Mars rover.)


答:
As a developer, you are probably accustomed to providing solutions. That is a very good way of thinking, but may hinder you when it comes to thinking about architecture.

I find that it helps to describe the the problem that you are trying to solve first. What are the requirements? What are the constraints? Can you talk to stakeholders to find out these requirements?

It might help if you can describe your own design goals as well. Does your solution need to scale, or is a design for single user sufficient? How long does your solution need to remain valid? Is it a one-time fix, or a long term infrastructure solution? PErhaps as important as well: What are the accepted limits of your architecture?

And since this is a learning experience, do not be afraid to ask questions, even if they are silly.


问:
Is imperative style programming (say with Java/C) more error prone than something more declarative
I know programmers tend to get defensive with their paradigms and tools that they use.  But in your experience, with the most generic, typical pieces of code that you see with Java or C++ or C, is the code more error prone than a similar piece of code in an declarative or functional programming language.

For example, with Java there can be a lot of boilerplate and setup code need to call your target routine.  Usually developers may need to look at the implementation details to really understand what happens if they do or do not provide the correct dependencies.  Normally the developer never does that so you end up with NullPointerException bugs and other logic errors.

答:
An unnecessarily imperative style is more error-prone than a style relying more on pure functions and immutable values because mutable values have the potential for aliasing bugs. If a particular program can be implemented both ways, the imperative approach has a higher risk of bugs all else being equal.

However, not all languages support a pure functional style well. It's relatively difficult to make good use immutability in Java, for example, because neither the language nor the standard libraries support it.


答:
JSON/HTTP is a really good decoupling mechanism, and I'll throw out a couple of suggestions that will make it even more loosely coupled.

The rapid industry adoption of JSON/HTTP interfaces really speaks well about how people view the usefulness of that model.

Enforce a MUST IGNORE rule.

That is, when parsing the JSON (client or server), the app MUST IGNORE any fields it don't recognize.

XML went in the with idea that the app MUST UNDERSTAND each field or else the document was invalid. But that created problems with versioning, because with almost any change, clients needed to upgrade every time the server did. Even adding an informational field broke the spec. With MUST IGNORE, the server can add new fields any time, and as long as it doesn't remove or change the meaning of other fields (see below). Existing clients can just ignore the new fields. Rather, they MUST IGNORE the new fields.

A search on MUST IGNORE and MUST UNDERSTAND will reveal lots of good articles that talk about that.

Minimize breaking changes.

A "breaking change" is a change that will break existing clients. That is, removing a field the clients use. Or changing the meaning of a field (i.e. changing an amount field from dollars to Yen). That is, something that invalidates a client's assumptions about the data it's currently using.

With a breaking change, every client needs to make a change to support the new semantics or stop relying on missing fields. Do don't do that unless its necessary.

The next logical step gets kind of contentious -- but in the extreme you would never make a breaking change. That is, have full backward-compatibility for every release. That may or may not be realistic, and it may require carrying along baggage from early versions, but it will spare a lot of churn for the clients.

OAuth 2 is a really good bet for a well-thought out, standardized security protocol. You could sit down and design something simpler, depending on what compromises are OK. But OAuth is a good fleshed-out protocol that has undergone years of industry scrutiny, so they've had lots of time to work out the kinks. And standard libraries are readily available for both client and server. I used an OAuth plugin to DJango for one project and it worked out really well.

Because of the ubiquity of JSON parsers, maintaining a single API regardless of client will make life a lot easier. Sometimes it doesn't work out -- sometimes a client can only understand XML or some proprietary protocol, but starting simple & adding complexity  makes life easier.

Title: Decoupling Server and Client using REST API

Body:
I was thinking about how I can decouple a web-application completely into a server-side and a client-side component. I want to decouple the app to the extent that I can host both components on seperate servers.

So, for example, I would have:

Server 1 (API Server): A server-side component running on Django on something like Heroku or EC2.

Server 2 (Static Server): A client-side component running on AngularJS on a static server like S3 or CloudFront.

The communication between these components will take place using a JSON REST API.

Questions:

Is this approach common? advisable?

Does a company like Facebook or Twitter mostly utilise the same API for the webapp as it does for its mobile apps or its open API?

Is it a good idea to use oAuth2 for the login process?

So the user is redirected to a login page on Server 1 (this is the only page on Server 1) and then redirected back to Server 2 with a token if authentication succeeds. Is this the best approach?  It seems like I am kinda "breaking the flow" if I do this. Is this normal?

The motivation for this is for me to be able to use the same API for my web, iOS and Android clients.

Thanks!


答:
The exact difference between "design" and "architecture" is a bit subjective and there is some overlap. However, this is the difference as I understand it:

Architecture looks at high level concepts. Who are the actors in the system? What are the major objects and which ones are responsible for what? When I think architecture, I think Visio, not code.

For example, an event system might have an event manager that accepts incoming events and dispatches them to event handlers. The idea of threads, asynchronous v. synchronous, and other lower level concepts do not come into play here. MVC is another example: specific details are not specified at the high level of how the three pieces interact, only that there are three separate concerns handled by separate code packages.

Design involves prototyping, sketching out code interfaces, code skeletons, etc. It sits between the abstract architecture and the low level "code monkey" work.

In an event framework, the design might say "an event uses this interface," and "there is a thread pool which the event manager uses to dispatch events to workers." A design for MVC might be "use Hibernate for the model, Spring for the controller, and GWT for the view."

When I do design work I have sketch out interfaces and code skeletons, then hand off the results to programmers. Sometimes I am that programmer. But it is two separate phases, and both exist more toward the concrete than the architecture.

Putting on the architecture hat means clear your mind of the code and think objects on a whiteboard. Think objects, packages, frameworks, and the flow of messages between them. If you are thinking of even one line of code you are doing it wrong. Do not get bogged down in something like "oh, that message could be a string, or use SOAP, or whatever." At this level, the fact that communication is occurring is enough. Details are irrelevant.


问:
Testing a very specific function in a large, complex application
I'm new to testing but wholeheartedly realize how important it is. The main issue is that my company has no top-down support for testing at all. That is we don't have any unit testing and just a bit of human testing (I hope I'm using the right terminologies).

I have been trying to get testing started here and have to do it subtly because I'm often asked "when will feature X be done" and therefore can't start the week(s) long project of writing tests for all of our legacy code. Which would need to get rewritten just to provide seams for testing (if I have the concept of 'seams' right). I can, however, sneak a bit of time to test one function. Or at least I think I can.

I'm writing code that basically processes quite a bit of data and then renders it using D3. I would like to write a test for one of the most important functions, binData. It takes one optional argument (the date to start binning from) and accesses a few properties like this.rows and this.timestamps.

How would I go about doing that?

I think I'll need to create a mock object to hold this.data and this.timestamps (and the other accessed properties). In fact, I imagine several different mock objects which would allow testing of several different datasets. But other than that I'm kinda stuck. In terms of frameworks I've heard of Mocha and Jasmine and Vows but don't know which would be best.

I also don't know how to tie into the massive (and complex) class and module hierarchy and to meet each object's dependencies. That is, the function binData is in a file called, say, dataBinner.js which requires all sort of other functions in other files but doesn't explicitly include anything on it's own. Instead, it itself is included in a "manager" file which includes everything that dataBinner.js and the other modules will need. I suppose I'll need to find a way to instantiate everything necessary to test binData.

I'm not afraid to do a bit (or a lot) of reading to better understand things. Any help regarding this specific task of writing a test for binData or the larger question of how to slowly implement testing in a non-test-friendly place would be much appreciated.

CLARIFICATION

I'm 100% on board, testing-wise. I know it's important and I know that my company is incorrect by not having testing. You'd expect emails that say "fixed regression caused by fixing bug 1413" would trigger something. Or that things that shouldn't take that long take waaaaaay too long ('cause it's spaghetti legacy code).

What I'm asking is given that how do I test my one function which relies on the legacy spaghetti code. That is, how does someone inject a bit of testing into a place without it? I'm guessing that I'll have to write my own testing framework because I'll have to create mocks for the spaghetti legacy code. So that I can instantiate my object and call my function outside of the scope of the application.

答:
Your problem is larger than mocking a single function.

It doesn't seem like your company has a "culture of testing."  This will make it difficult to make changes "in the large," because your company expects things to happen in a certain time frame, because that's how long they took without testing.

Your best bet might be to:

Refactor old methods and add tests to them during modification or addition of new features, and/or

Wall-off the old code-base as much as possible, adding new features only as part of a Test-Driven methodology in a green field.  Talk to the old code, but don't change it.


问:
avoiding hard coding of knowledge of algorithm in tests?
I have a package of code which is suppose to tell me where to allocate elements.  When I add a new element it may require repositioning of existing elements to keep my 'tree' in a format that promises a log-n search.  Ultimately when I hand it a current system and new elements to add I should get a list of where to add new elements, and when/where old elements were moved to.

I want to test that the set returned is valid, but I'm not certain how to.  I can easily test if the elements are placed in valid locations, ie locations that won't break assumptions about my 'tree', but this doesn't ensure that I did the minimal and most efficent movement of elements.  However, I can't think of how to check the moves are efficent without essentially rewriting a large part of the package just to test it's results.

Since I wrote the package I know some implementation specific details.  I know that A will be moved to B because I know exactly what logic was used to decide the move, but this is implementation specific.  There are cases where I hae multuple equally valid choices and I pick one way when I could pick another.  Is it 'right' to do this sort of functional testing by utilizing my understanding of the algorithm to specify specifially what should have moved where?  if not how else would I write the test to have any idea that i'm not making the most inefficent, but valid, moves possible?

答:
It sounds like your tests need to cover two things:

The API of your package - how the outside world will use it

Internal details of your package - making sure that the current implementation works correctly

So I would write tests in both areas, and divide them up so that their purpose is clear. For example, separate classes, namespaces or packages. If you change the implementation, the first set of tests should be unaffected, ensuring your API is unbroken.


答:
The distinction between these two ideas is really important where I work.

What you call "architecture," we call "programming in English." This is partly important because if you can't describe it to a non-programmer, then something is wrong. It might be that you don't understand the problem well enough, OR it might be that you're solving a "phantom" problem (not discussed here).

The terms used for these two different aspects of design are often different, but the principles are readily recognized everywhere. One aspect (in your case the architect, in my case the designer) programs in English, whereas the other (in your case "designer," in my case "developer") programs in a particular language. They're also quite commonly distinguished as "design" and "implementation." "Design" being what it's supposed to accomplish, and "implementation" being the code that makes it happen.

Some examples from what I've worked on:

The architecture of one program is: we need a centralized Manager or hub that we can easily add modules to. This Manager would distribute events to all of the registered modules. A module can register itself with the Event Manager, and thereby publish events to the rest of the system, and receive events that it cares about. Each module has a "mailbox" that it can check and empty as it likes. This would let us accommodate new modules that we don't know we need yet.

No code there. Could be written in any language. Implementation isn't dictated by this description.

The architecture of another project is: we need a way to reliably start and stop other programs without waiting for them. We could have a manager that is responsible for a particular program. We can just tell it to start or stop its program and the manager takes care of it. If this other program is asked to stop and doesn't in a given amount of time, the manager knows how to force it to stop, and clean up the mess. The program isn't started or stopped by anything else, and the manager can be asked whether its program is running, stopped, or waiting to stop. This lets us carry on with the other things we need to do, while still getting these other programs started and stopped properly.

Again, nothing here dictates implementation, though some implementations are clearly more useful than others.

The difference between design (what we'd call patterns or implementation) and architecture (what we'd call design) is that one of them solves a coding/implementation problem and the other solves a real-world problem.


答:
It sounds like you're looking to make sure that:

Your interface is valid

Your implementation is efficient

You're not regressing

Valid interface

Write tests which verify that, for each interface method, the result is a valid one, and leave it at that. If you're expecting a valid list of locations as an output from a function, check that's what you're getting back. If you're expecting certain inputs to cause exceptions or raise errors, test that. Etc...

Efficient Implementation

You must have some sort of measure of efficiency which you're using to make sure that your solutions are, indeed, the most efficient possible. Sounds like "number of moves", in your case.

So, manually create some inputs, for which you manually check that the number of moves is optimal. Then, run the inputs through your algorithm, and check that the number of moves it produces exactly your expectations (maybe also check that the solution is valid, for sanity's sake). If the number is higher, something is wrong with your algorithm; if the number is lower, something is wrong with your hand-math ;)

Ie, provide a list of inputs and an appropriate, expected measurement of efficiency.

Design the inputs to stress corner cases for any general solution to your problem - inputs for which it's particularly difficult to come up with efficient solutions, or for which your algorithm might break (and continue executing infinitely, for example).

Regression

Generate/find/obtain a set of real-life reflecting inputs for your algorithm.

Run the algorithm against the inputs.

Record and store valid measurements for the results (measurements from the Efficient Implementation section, and others which are likely not to be dependent on your specific algorithm.

Write tests which make sure that each run of your algorithm on the real data, produces valid results, which exactly match the measurements of know, stable, previous runs.

Title: avoiding hard coding of knowledge of algorithm in tests?

Body:
I have a package of code which is suppose to tell me where to allocate elements.  When I add a new element it may require repositioning of existing elements to keep my 'tree' in a format that promises a log-n search.  Ultimately when I hand it a current system and new elements to add I should get a list of where to add new elements, and when/where old elements were moved to.

I want to test that the set returned is valid, but I'm not certain how to.  I can easily test if the elements are placed in valid locations, ie locations that won't break assumptions about my 'tree', but this doesn't ensure that I did the minimal and most efficent movement of elements.  However, I can't think of how to check the moves are efficent without essentially rewriting a large part of the package just to test it's results.

Since I wrote the package I know some implementation specific details.  I know that A will be moved to B because I know exactly what logic was used to decide the move, but this is implementation specific.  There are cases where I hae multuple equally valid choices and I pick one way when I could pick another.  Is it 'right' to do this sort of functional testing by utilizing my understanding of the algorithm to specify specifially what should have moved where?  if not how else would I write the test to have any idea that i'm not making the most inefficent, but valid, moves possible?


问:
How to support different API Versions
I am writing a Rest API and am wondering how best to handle supporting the different versions. By this I don't mean how to define a URI as V2 or V3, but rather how to structure the code given that it'd need to:

Support multiple versions at the same time, eg. V1 & V2 & V3 URIs must be live at the same time. I would retire V1 when say V4 comes in in order to constrain the amount supported at any one time.

Avoid as much code duplication as possible

Make it easy to add non-breaking changes to a version, without it impacting on other versions

It would seem that there are few approaches that could be taken:

Use Git to control the versions, with a branch for the different versions (and old versions  essentially having no new development work done on it). This would mean no code duplication as only the latest version is in the code, but previous versions would need to work with the new version of the DB until they are retired.

Duplicate code so that each version is handled in the same application and has a totally separate code path, but this would mean a lot of duplication

Re-use a lot of code across the versions, but this would make it harder to maintain as changing one version is more likely to impact a previous version

Is there any best practice to deal with this problem as all options seem to have their own problems?

答:
Do this:

Re-use a lot of code across the versions, but this would make it harder to maintain as changing one version is more likely to impact a previous version

but don't break the previous versions.

You should have tests that verify that all supported versions are working correctly.  If you don't have those tests, then you should first create them to cover any code you are changing.

Title: How to support different API Versions


问:
How do I mutate a value in a genetic algorithm using Gaussian distribution?
I've been reading 'Introduction to Evolutionary Algorithms'. This method is stated, but not described, and I can't find anything more specific online. p44/45 of 2nd Ed for reference.

adding to the current gene value an amount drawn randomly from a Gaussian distribution with mean zero and user-specified standard deviation

Would be great to get the step by step, from initial to mutated value, including how Gaussian distribution works (or a link to something that could fill that gap).

答:
A gaussian distribution is that favorite curve from math and stats also known as the bell curve or a normal distribution.

An example of this can be seen with the sum of rolling 10x 10 sided dice:

From AnyDice output 10d10

What this means is that the amount of mutation for a gene is most likely very little.  If you roll this set of dice 30 times, you'll get some data that looks like:

53, 38, 52, 62, 51, 50, 71, 44, 52, 55, 56, 45, 48, 67, 48, 63, 33, 40, 62, 60, 54, 48, 67, 60, 44, 63, 50, 50, 64, 56

Notice that most of these numbers are fairly close to the value of 55.  Thats the key to this - you want a middle value.

When doing a mutation on a gene that has a possible change of -10 .. +10 you want most of the chance to be about 0.  Maybe some 1s, or 2s, or -1s, or -2s... but you don't want a +10 to have an equal chance of a mutation as a +1.

So? How do you do this?

In Java, one would use java.util.Random#nextGaussian().  That will give you a number that has a normal distribution with a mean of 0.0.

You would take the value you get from nextGaussian(), and then... well, do math to it to get it into the range you want.  Just because you're getting a number with a mean of 0.0 doesn't mean that any given value will be in a certain range.

The specifics of the nextGaussian() for your chosen language is just a matter of finding the right library for the language.  Its there somewhere.  You may wish to browse the tag: normal-distribution on Stack Overflow filtered for your language to see if any of those questions specifically help you.


问:
How to build configurable forms?
I'm working on two separate projects now that are essentially just CMSs -- a bunch of forms for recording various bits of information. Each project is built to serve many clients, and each client seems to want to record slightly different information.

In one project we've handled this either by copying and pasting all the logic surrounding that form for each client so that we could make the changes without affecting the other clients, or we build a master form containing all possible inputs and then programmatically remove the inputs that the client doesn't want (our framework allows us to remove inputs (essentially from the 'controller') before the final template rendering). Neither solution is really clean and causes the forms to be riddled with "if" conditions based on which client we're serving.

The other project I'm just starting, and I don't want to run into the same pitfalls again, but I'm having trouble coming up with a good way to solve this problem.

I could list out all the input IDs for each form element in the database and then create a mapping between them and the clients, and then in my form template wrap each widget rendering with a check to see if the input is enabled for the current client. On the backend -- when the form is submitted -- I could save only the data that is submitted and forgo the conditionals for the most part. This creates a dependency on the IDs used in the template must match those recorded in the DB -- I don't want to create the entire form programatically as that wouldn't give me enough customizability. That's the best I can come up with.

Has anyone dealt with this sort of problem before? How did you approach it? What worked, what didn't?

For comparison, you might consider sites like Freshbooks, or Trello, or a bug tracker, or Facebook -- if any of those apps let each user customize his or her page to add or remove inputs so that they could record only the stuff they care about -- how would they manage this complexity from an architectural point of view?

答:
You need a structure to contain what inputs they want and how they go on the page. No "if" blocks or conditionals -- it needs to be set based, you have this list of elements, this is where they go, this is what they mean, this the validation for a particular element...

This shouldn't prevent you from customizing, it should in fact make it very flexible, but it will be a bit complex to work out.

Title: How to build configurable forms?