答:
It sounds like you're aware of your tendency to think about everything that can go wrong.

Experienced Cautious Developers often learn to follow the mantra YAGNI, you ain't gonna need it, when they try to return to a lean, agile and productive workflow after getting too choked up in the weeds of failure-mode-analysis-gone-amok.

However, if you are indeed writing something in a domain where that level of care is no less than what Professionalism demands, then you should realize that your "velocity", your "productivity", in net terms, is measurable by how much good (or harm) you are doing to your company, your customers, and the software suite, or product family you are building or maintaining.

Remember to:

Include total cost of maintenance, total cost of ownership, and total cost of deploying and maintaining solutions when you consider changes in your approach.  Going faster and making more mistakes may or may not make things better.

If you work in a good company, you can probably discuss this in your own team, and with your own supervisor, without it being a Career Limiting Move. If you can't, now is a good time to find that out, and find a new job.


问:
Overcoming slow problem solving due to increased knowledge of what might go wrong
This has been troubling me for some time, and I'd really appreciate the input of other professionals.

Short background: I started programming when my parents bought me my first computer in 1988 (at age 14, I'm 39 now). I followed a couple of other career paths before finally becoming a professional programmer in 1997. Late bloomer, perhaps, but that's how it was. I'm still happy with my choice, I love programming, and I consider myself good at what I do.

Lately, I've been noticing that the more experience I gain, the longer it takes me to complete projects, or certain tasks in a project. I'm not going senile yet. It's just that I've seen so many different ways in which things can go wrong. And the potential pitfalls and gotchas that I know about and remember are just getting more and more.

Trivial example: it used to be just "okay, write a file here". Now I'm worrying about permissions, locking, concurrency, atomic operations, indirection/frameworks, different file systems, number of files in a directory, predictable temp file names, the quality of randomness in my PRNG, power shortages in the middle of any operation, an understandable API for what I'm doing, proper documentation, etc etc etc.

In short, the problems have long since moved from "how do I do this" to "what's the best/safest way of doing it".

The upshot is that it takes me longer to finish a project than a novice. My version may be rock solid, and as impenetrable as I know how to make it, but it takes longer.

The "create file" example above was just that, an example. Real tasks are obviously more complex, but less suited for a generic question like this one. I hope you understand where I'm going with this. I have no problem coming up with efficient algorithms, I love math, I enjoy complex subjects, I have no difficulties with concentration. I think I do have a problem with experience, and consequently with a fear of errors (intrinsic or extrinsic).

I spend almost two hours a day reading up on new developments, new techniques, languages, platforms, security vulnerabilities, and so on. The conundrum is that the more knowledge I gain, the slower I am in completing projects.

How do you deal with this?

答:
Think about the practical consequences of a bug as compared to all the other problems that need solving.

Consider the following consequences of creating a poorly written piece of code:

Entire database gets dumped every other month. 48 hours of downtime while the backups are restored.

Customer records get cross-linked. $200 worth of orders get shipped to the wrong customers per month.

A order gets stuck in a wrong status once a week. Order ships but warehouse to has to call helpdesk every time it happens.

Once very two weeks or so, the app crashes and user has to re-enter 2 minutes worth of data.

Once a month, the app hangs on startup. User has to kill process and start over.

The first one is obviously unacceptable. #2 - #5 may or may not be, depending on the nature of the business. #2 - #5 need to evaluated in the context of other problems the business is facing.

Ideally, #2 - #5 would never, ever happen. In real life, with conflicting priorities, the people signing your paycheck might want you to be working on other things rather than writing perfect code that never, ever has a problem.
They aren't going to be impressed if #5 gets fixed at the expense of not fixing a more serious bug in another program.

ContentLicense: CC BY-SA 3.0


问:
Workflow versioning
I believe I have a fundamental misunderstanding when it comes to workflow engines which I would appreciate if you could help me sort out. I'm not sure if my misunderstanding is specific to the workflow engine I'm using, or if it's a general misunderstanding. I happen to use Windows Workflow Foundation (WWF).

TLDR-version

WWF allows you to implement business processes in long-running workflows (think months or even years). When started, the workflows can't be changed. But what business process can't change at any time? And if a business process changes, wouldn't you want your software to reflect this change for already started 'instances' of the business process? What am I missing?

Background

In WWF you define a workflow by combining a set of activites. There are different types of activities - some of them are for flow control, such as the IfElseActivity and the WhileActivty while others allows you to perform actual tasks, such as the CodeActivity wich allows you to run .NET code and the InvokeWebServiceActivity which allows you to call web services. The activites are combined to a workflow using a visual designer. You pretty much drag-and-drop activities from a toolbox to a designer area and connect the activites to each other. The workflow and activities have input paramters, output parameters and variables.

We have a single workflow which sometimes runs in a matter of a few days, but it may run for 5-6 months. WWF takes care of persisting the workflow state (what activity are we currently executing, what are the variable values and so on).

So far I think WWF makes sense. Some people will prefer to implement a software representation of a business process using a visual designer over writing all of it in code.

So what's the issue then?

What I don't really get is the following:

WWF is designed to take care of long-running workflows. But at the same time, WWF has no built-in functionality which allows you to modify the running workflows. So if you model a business process using a workflow and run that for 6 months, you better hope that the business process does not change. Because if it do, you'll have to have multiple versions of the workflow executing at the same time. This seems like a fundamental design mistake to me, but at the same time it seems more likely that I've misunderstood something.

For us, this has had some real-world effects:

We release new versions every month, but some workflows may run for a year. This means that we have several versions of the workflow running in parallell, in other words several versions of the business logics. This is the same as having many differnt versions of your code running in production in the same system at the same time, which becomes a bit hard to understand for users. (depending on on whether they clicked a 'Start' button 9 or 10 months ago, the software will behave differently)

Our workflow refers to different types of entities and since WWF now has persisted and serialized these we can't really refactor the entities since then existing workflows can't be resumed (deserialization will fail

We've received some suggestions on how to handle this

When we create a new version of the workflow, cancel all running workflows and create new ones. But in our workflows there's a lot of manual work involved and if we start from scratch a lot of people has to re-do their work.

Track what has been done in the workflow and when you create a new one skip activites which have already been executed. I feel that this alternative may work for simple workflows, but it becomes hairy to automatically figure out what activities to skip if there's major refactoring done to a workflow.

When we create a new version of the workflow, upgrade old versions using the new WWF 4.5 functionality for upgrading  workflows. But then we would have to skip using the visual designer and write code to inject activities in the right places in the workflow. According to MSDN, this upgrade functionality is only intended  for minor bug fixes and not larger changes.

What am I missing?

答:
tl;dr: Long-running workflows are not expected to change during their lifetime.  If there's a possibility that they might change, break them into smaller and shorter workflows, and chain them together to make the larger workflow.

Changing Running Logic is Hard

Why does your computer system sometimes require a reboot?  Because you can't change out device drivers while they're in use.  How do you get around this?  By making things immutable and writing functional-style programs.

The Erlang programming language is specifically designed to be upgraded while programs are running.  How does it do this?  By maintaining immutable instances of functions in memory.   When you hot-patch a program in Erlang, Erlang simply maintains the existing functional instances until they finish executing.  Any new instances of the functions will use the new version of the function, and because the functions are immutable, stability is assured during the transition.

Note that this would not help you with your workflows, because you still wouldn't be able to hot-patch an existing workflow while it is running.  Hot patching refers to entire systems of functions, not individual instances of running functions.  Your workflow (essentially a long-running function) would still have to finish executing first.

As you've already pointed out, changes to running workflows are intended for minor tweaks, not wholesale changes to the workflow.

Title: Workflow versioning


问:
Where can I find inspiration for a good user interface?
I've got a mental block as far as desigining the main screen interface for a desktop application that I'm currently upgrading. When I first developed this program the screen resolution was 640 X 480. Today there are multiple screen resolutions out there.

Where can I find ideas for a good main screen layout for a desktop application?

答:
There are some fine books that have been recommended already. The UX Stack Exchange website is more well-equipped to handle questions like this, but it isn't totally off-topic either.

First and foremost, mediaqueri.es is a great resource for your specific problem because it is a gallery of websites and web applications that are responsively designed to fit different screen resolutions and user agents.

Also check up on Smashing Magazine. It's a blog that is front-end development heaven, with posts concerning UX/UI, HTML/CSS/Javascript, graphic design, and web design. They also have posts that study specific websites and marketing campaigns and see what they did right (and wrong) with their UI/UX decisions. Check this site regularly for daily tips regarding these topics.

Then when you've gotten some inspiration, take a look at some SO/SE questions I have starred specifically on this kind of topic. These are just general resources for anybody and not just OP:

Why is Good UI Design hard for some developers - to understand why it's hard for developers to also be designers

Must Read UI books - book recommendations on books to study designing user interfaces

Which UI/UX sketching software is recommended? - wireframing

Fluid vs. Responsive design- if you are going to develop for multiple user agents you should check this out

Should I use Yes/No or Ok/Cancel on a message box - Great post, useful when wireframing


问:
The Nature of Spring
Is it accurate to say that dependency injection manually using java (without spring) is nothing but implementing the strategy pattern (as per this example)?

Furthermore, is it correct to say that Spring just provides a declarative way of specifying the dependency as opposed to manual method whereby the dependency need to be created and 'set' using java code?

答:
Dependency injection (whether done through the constructor or with a DI framework) is one way to implement the strategy pattern, but so is a factory method.

In general, DI frameworks (like the one implemented by Spring) allow you to declare system-wide injection dependencies in a single location.

Title: The Nature of Spring


答:
Track what has been done in the workflow and when you create a new one
  skip activites which have already been executed. I feel that this
  alternative may work for simple workflows, but it becomes hairy to
  automatically figure out what activities to skip if there's major
  refactoring done to a workflow.

The problem is, it can become hairy even manually to  to figure out what activities to skip. For example, what if the current activity in the old workflow has no equivalent activity in the new one? So I suggest you should take a combined strategy:

use the "track & skip" approach where easily possible

use the "cancel & restart" where not possible

for special cases you could try to create sub-workflows to avoid a complete restart, specificially designed for continuing an already started (old) workflow at a specific activity, but without the need to restart from the complete beginning

try to keep the number of "transition points" small (the activities where the actors have to change from the old workflow version to the new ones) - make sure your actors reach one of those points until the old workflow gets replaced by the new version. The transition points should be always having a pendant in the new version. This will help in all of the above cases.

Title: Workflow versioning

Body:
I believe I have a fundamental misunderstanding when it comes to workflow engines which I would appreciate if you could help me sort out. I'm not sure if my misunderstanding is specific to the workflow engine I'm using, or if it's a general misunderstanding. I happen to use Windows Workflow Foundation (WWF).

TLDR-version

WWF allows you to implement business processes in long-running workflows (think months or even years). When started, the workflows can't be changed. But what business process can't change at any time? And if a business process changes, wouldn't you want your software to reflect this change for already started 'instances' of the business process? What am I missing?

Background

In WWF you define a workflow by combining a set of activites. There are different types of activities - some of them are for flow control, such as the IfElseActivity and the WhileActivty while others allows you to perform actual tasks, such as the CodeActivity wich allows you to run .NET code and the InvokeWebServiceActivity which allows you to call web services. The activites are combined to a workflow using a visual designer. You pretty much drag-and-drop activities from a toolbox to a designer area and connect the activites to each other. The workflow and activities have input paramters, output parameters and variables.

We have a single workflow which sometimes runs in a matter of a few days, but it may run for 5-6 months. WWF takes care of persisting the workflow state (what activity are we currently executing, what are the variable values and so on).

So far I think WWF makes sense. Some people will prefer to implement a software representation of a business process using a visual designer over writing all of it in code.

So what's the issue then?

What I don't really get is the following:

WWF is designed to take care of long-running workflows. But at the same time, WWF has no built-in functionality which allows you to modify the running workflows. So if you model a business process using a workflow and run that for 6 months, you better hope that the business process does not change. Because if it do, you'll have to have multiple versions of the workflow executing at the same time. This seems like a fundamental design mistake to me, but at the same time it seems more likely that I've misunderstood something.

For us, this has had some real-world effects:

We release new versions every month, but some workflows may run for a year. This means that we have several versions of the workflow running in parallell, in other words several versions of the business logics. This is the same as having many differnt versions of your code running in production in the same system at the same time, which becomes a bit hard to understand for users. (depending on on whether they clicked a 'Start' button 9 or 10 months ago, the software will behave differently)

Our workflow refers to different types of entities and since WWF now has persisted and serialized these we can't really refactor the entities since then existing workflows can't be resumed (deserialization will fail

We've received some suggestions on how to handle this

When we create a new version of the workflow, cancel all running workflows and create new ones. But in our workflows there's a lot of manual work involved and if we start from scratch a lot of people has to re-do their work.

Track what has been done in the workflow and when you create a new one skip activites which have already been executed. I feel that this alternative may work for simple workflows, but it becomes hairy to automatically figure out what activities to skip if there's major refactoring done to a workflow.

When we create a new version of the workflow, upgrade old versions using the new WWF 4.5 functionality for upgrading  workflows. But then we would have to skip using the visual designer and write code to inject activities in the right places in the workflow. According to MSDN, this upgrade functionality is only intended  for minor bug fixes and not larger changes.

What am I missing?


问:
DDD Bounded Contexts & Domains?
I've been working in a relatively complex application with 10's of database tables (Aggregates, Entities/Value Objects) and applying DDD. At this point it appears to be basically DDD-Lite meaning that there are Application/Domain Services, the Domain Model (Entities, Value Objects), and Repositories.

I picked up a book Implementing DDD and the first thing he is mentioning is DDD-Lite and Bounded Contexts and Domain Events missing as first mistakes which are usual when beginning DDD.

Currently I've tried organizing the Domain Model by Aggregate relationships, and using namespaces to demonstrate it.

I'm failing to see the benefit/downfalls relating to separating the Domain Model project into separate Bounded contexts (yet). Perhaps it will become apparent later on but I'd like some real life feedback on Bounded Contexts (and possibly sub domains etc. if they tie into it).

答:
I can give you another example. Consider you have some ecommerce system. You would have products there, however products will be part of at least two different domains:

Product catalog, where you keep your product description and all attributes

Inventory, where you have product stock level

If you have one bounded context for both domains, your solution can quickly become a big ball of mud because you will start cross referencing it. At the end you will not have two domains anymore. Your product inventory will be spoiled with product catalog references and vice versa. As the result of this you will not be able to change one domain without touching another even you fully realize this is not required. Your models become dependant on each other and tightly coupled, and dependant in a bad way - being dependant on implementation.

If you, however, have two bounded context, all changes you do in one domain will not affect the other as soon as you keep your communication channels clear. It will mean you need to have data duplication but this is the least cost to pay for losely coupled component based application. Your domains can talk to each other using domain events. Even if you do not plan to have SOA based application in the beginning you will be able to scale up to that level when you need with relatively low effort since you just change the transport for your domain events, leaving the idea behind it intact.

Update:
There is a good skillscast on SkillsMatter, from Eric Evans. He gives an analogy of the old story, when several blind man describe an elephant from their perspective. Since each man can only touch a part of the elephant, they describe it as a "tree", "wall", "snake", "rope". And each of those man is right within their context. Bounded context is where ubiquitous language lives. Outside the context, these terms might have completely different meaning but inside the context, the language is the same across multiple domains. Greg Young strongly suggests to start reading the blue book from chapter 11, since the most important, fundamental concepts are explained there. The focus on tactical patterns in the beginning of the book makes this "DDD-light" approach very common, when people stop reading somewhere around chapter 8 and miss the main things, that is somewhat buried at the second half.

Title: DDD Bounded Contexts & Domains?


问:
How to loosely couple modules in service oriented architecture
We're developing a total solution, and we've chosen service-oriented architecture to loosen subsystems. However, now we have a problem.

When customer orders something, Order Subsystem is responsible to get that order, and register it. However, there are some other modules which are responsible too, in order registration process, and each module should do something on it:

Customer Accounting module should check customer's balance, and prevent order registration if there is not enough money in customer's account.

Asset Management module should increase/decrease customer's assets

Rule Engine should apply some business rules dynamically on it (like preventing a specific customer, or notifying an operator, etc.)

CRM should view customer's history, and give good customers some discount

...

Right now, we're doing it this way:

```<code>public void RegisterOrder(Order order)
{
    RuleEngineService.ApplyBusinessRules(order);
    // some code here
    CrmService.GiveDiscountIfApplicable(order);
    // some code here
    CustomerAccountingService.CheckBalance(order);
    // some code here
    AssetManagementService.ChangeAssetCount(order);
    // some code here
}
</code>```

This means that if we add another subsystem (module) that needs to do something on order, we have to recompile the code of our OMS (Order Management System) to support it. Also we're coupled to some extent to other subsystems here, and if one of them fail, this registration process fails.

One way of making this piece of code dynamic and loosely coupled is to use interfaces or contracts, like:

```<code>public void RegisterOrder(Order order)
{
   // Finding a list of all IOrderProcessors.
   // Looping over each, calling orderProcessor.Process(order);
}
</code>```

However, it has two problems:

We can't use interfaces over SOAP and HTTP (we don't have access to other subsystems' DLL files)

Those need to run in a specified order (rule engine the first one)

What should we do here? What are the known patterns and practices?

答:
I would use some ServiceBus framework that supports Saga pattern.

Plus, you still can stick to your own solution with interfaces. I don't see any issue with having DI container to resolve your interfaces into a collection. Proxy classes that implement interfaces will talk to the actual services and these classes are local. You can have separate DLLs per proxy class and register it either by assembly scanning or by configuration. You can have a property for execution order in your interface that will easily allow you to sort your collection to run proxies in the proper sequence.


问:
How to use the unit of work and repository patterns in a service oriented enviroment
I've created an application framework using the unit of work and repository patterns for it's data layer. Data consumer layers such as presentation depend on the data layer design. For example a CRUD abstract form has a dependency to a repository (IRepository).

This architecture works like a charm in client/server environments (Ex. a WPF application and a SQL Server). But I'm looking for a good pattern to change or reuse this architecture for a service oriented environment.

Of course I have some ideas:

Idea 1: The "Adapter" design pattern

Keep the current architecture and create a new unit of work and repository implementation which can work with a service instead of the ORM. Data layer consumers are loosely coupled to the data layer so it's possible but the problem is about the unit of work; I have to create a context which tracks the objects state at the client side and sends the changes to the server side on calling the "Commit" (Something that I think the RIA has done for Silverlight). Here the diagram:

```<code>----------- CLIENT----------- | ------------------ SERVER ----------------------

[ UI ] -> [ UoW/Repository ] ---> [ Web Services ] -> [ UoW/Repository ] -> [DB]
</code>```

Idea 2: Add another layer

Add another layer (let say "local services" or "data provider"), then put it between the data layer (unit of work and repository) and the data consumer layers (like UI). Then I have to rewrite the consumer classes (CRUD and other classes which are dependent to IRepository) to depend on another interface.

And the diagram:

```<code>----------------- CLIENT ------------------ | ------------------- SERVER ---------------------

[ UI ] -> [ Local Services/Data Provider ] ---> [ Web Services ] -> [ UoW/Repository ] -> [DB]
</code>```

Please note that I have the local services layer on the current architecture but it doesn't expose the data layer functionality. In another word the UI layer can communicate with both of the data and local services layers whereas the local services layer also uses the data layer.

```<code>|    |      |                 |      |      |
|    | ---> |  Local Services | ---> |      |
| UI |      |                 |      | Data |
|    |                               |      |
|    | ----------------------------> |      |
</code>```

答:
If you stop worrying about your data and start thinking about your domain model it will become much more clear. I now see you are stuck with unit of work, repositories, data operations, layers and adapters but you forget about your business.

What I would suggest to do is to have proper rich domain model designed and have a service layer that will play together with your domain to do work for your business. When this is done. All the UI would need to do is to send this service layer DTOs with input models and get DTOs back with view models.


问:
Subsumption architecture vs. perceptual control theory
I'm a new person to AI field and I have to research and compare two different architectures for a thesis I'm writing.

Before you scream (homework thread), I've been reading on these two topics only to find that I'm confusing myself more.. let me first start with stating briefly what I know so far.

Subsumption is based on the fact that targets of a system are different in sophistication, thus that requires them to be added as layers, each layer can suppress (modify) the command of the layers below it, and there are inhibitors to stop signals from execution lets say.

PCT stresses on the fact that there are nodes to handle environmental changes (negative feedback), so the inputs coming from an environment go through a comparator node and then an action is generated by that node, HPCT or (Hierarchical PCT) is based on nesting these cycles inside each other so a small cycle to avoid crashing would be nested in a more sophisticated cycle that targets a certain location for example.

My questions, am I getting this the right way? am I missing any critical understanding about these two models? also any idea where I can find simplified explanations for each theory (so far been struggling trying to understand the papers from Google scholar).

Edit:

The acceptable result would be to compare and contrast subsumption with PCT as alternative control structures in behaviour-based robotics.

The experiments will be done on Mindstorm NXT 2.0 robot and the implementation will be done using PureData.

I'm not seeking complex behavior, only several models that will demonstrate capabilities of PCT and Subsumption and come up with weaknesses and strengths about each approach.

As for the other points 'rwong' mentioned, I don't believe they're part of my research scope.

my main target here is to get he concepts fully understood as this is my critical phase to implement a fully correct solution that would allow the comparison

答:
Yes, you have the basic concept for both of these correct. And as you have found, they are very similar. In some ways, you can consider PCT a particular implementation of Subsumption. So, here is a quick breakdown of their similarities and differences:

Similarities:

Have nodes or layers that typically attempt to accomplish one task. In the case of a robot, that task can be obstacle avoidance. With PCT, your design would explicitly use negative feedback (eg maximize distance to other objects).

Lowest level layers/nodes are typically connected directly to a sensor(s) (on a robot).

Higher level layers/nodes typically have inputs from a lower level(s) and potentially another sensor. Think wander behavior - your robot wants to move around AND has to avoid obstacles. Given that with simple obstacle avoidance, there is always something to avoid, wander needs to integrate the relative immediacy of obstacle avoidance with the higher level goal of exploration. Negative feedback approach to wander (a simple example for PCT purposes) would be to maximize the sum of your position differences and heading - picking constants that let the position grow faster than heading.

Differences:

PCT pretty much asks you to explicitly design negative feedback behavior.

Subsumption can require more information from downstream layers for decision making (yes, I need to avoid an obstacle but is the obstacle right next to me or 100 ft away?)

You could probably write out a single transfer function that combines all the hierarchies of PCT and do some normal control system analysis (stability, controllability, etc), subsumption's flexibility on your layers can make this harder since you can mix/match algorithms on how each one accomplishes it's goals


问:
Should I fork for a major re-write that uses a small amount of the original code?
I'm writing a library. It's a completely rewritten version of another one, to suit my needs (PCL compatibility, mainly). However, the API will be completely rewritten, as I'll need to change a lot of stuff around for PCL compliance. Also, as it is a rewrite, I won't be able to just start from the library and just change it bit by bit, as I typically see with forks. I tried that, but it just didn't work.

So what should I do? Should I fork here or should I make a new library?

答: Given that you are essentially re-writing everything from scratch, you should create a new project. Forking would imply that the two share enough similarities that sometime down the line, you could share features, take some new addition here or give some fix there. That's clearly not going to be the case.</p>


问:
Is it crazy to develop a TUI today?
I'm starting to develop a new program. It's basically a database interface, run a few queries, show the data in a nice way and be able to easy input new data (like this website).

Thinking about which programs I use daily I realise that only my web-browser is a GUI program, the rest is TUI (in this case TUI is Text User Interface, as in ncurses). So I plan to write this in ncurses with a vim/mutt-like interface. This program will only be used by experienced users and speed is much wanted.

I've found one study about this:
http://www.ncbi.nlm.nih.gov/pmc/articles/PMC2655855/

All other studies I've found has only looked at novice users and for them GUI is better. For an experienced user that will work daily with the program what is best?

This is sligthly similar but the difference is that this question was about a non-interactive program:

Is the development of CLI apps considered "backward"?

答:
I don't think it's at all crazy. It all depends on who your target audience is.  If you write an app and expect an average user to use it, you are probably better off with a GUI.

If your app is a for developers, especially those that are used to CLI. Or if your app is targeting a sys admin who sits at his workstation and SSH's into 30+ other machines on a daily basis, your tool might just hit the sweet spot for them.

In all my jobs, I always had some kind of "console utilities" module that would make it super easy to query on a command line for different data types from the user and had some kind of menu driver.  I'd write that thing in 2 days and then make gradual improvements over time.  I've found such utilities to be extremely useful if for nothing else then for quickly spinning up developer tools.  Whenever I'd show a new guy how to use them, I'd get a chuckle at non-GUI approach but the bottom line is that those tools accomplished exactly what I needed them to do and non-GUI approach was so lightweight that I could actually introduce and maintain these tools.

Title: Is it crazy to develop a TUI today?


问:
How does aggregation work when databases are involved?
So I'm reading a book on Design Patterns (Design Patterns Explained 2nd Edition), and all throughout the book it says to "favor aggregation over inheritance".

I'm trying to wrap my head around trying to design something, and I feel so stupid.

So conceptually, I have Recruiters. I figured these recruiters can be an abstract class, because I have a couple different types of recruiters... full time and part time. The thing is, I want each recruiter to be able to have references to other recruiters because they might work together and need to share and combine information.

Each Recruiter would have one or more Regions that they oversee, perhaps sharing it with other Recruiters. All the region is a conceptual aggregate of SubRegions. The SubRegions define places that fall along certain geographical boundaries. These contain some numerical statistic information about recruitments for that SubRegion as a whole.

The Subregions consist of Recruitments, which contains detailed information about particular recruiting events for a SubRegion. They also contain other statistical information that makes sense to be under Recruitments(because they're very specific to the Recruitment), but not under SubRegions. Every time some information from the recruitment is updated, it needs to create a new record of that transaction so I can keep a history of how the numbers are adjusting.

Now, I want all of these to be able to be stored in a database. I am very naive when it comes to database design and structuring, especially when it comes to dealing with abstract classes and interfaces which are new to me.

Everything in the database is going to be linked in some way, among different tables. Say, the Recruiters are going to be a table, and there is going to be a Region table, a SubRegion table, and a Recruitment table.

Where do I need to put the logic, and how do I put it, such that I can make sure I can save everything out to the database, keyed together nicely, but loosely coupled in the code?

答:
I think you may be confusing objects in a programming language with tuples in a database. You don't have object inheritance in a database, unless it's an object-oriented database. While you can simulate inheritance in a table-oriented database, it's usually not thought of in that way.  When it is, we designate it a 1:1 relationship, where Recruiter is the main table, and AdvancedRecruiter (or something similar) is the linked table with the additional information that belongs to the descendant object.

The thing you have to ask yourself when designing something like this in a database is the same thing that you would ask yourself when you're designing an inheritance hierarchy in a programming language.  The question is, is there some collection of data or behavior associated with some form of "enhanced" recruiter that does not belong in the main recruiter object, but is a natural subset of the main recruiter object?  If you have that (which I suspect you don't), then you have a candidate for inheritance.  If you don't have that, then just create a Recruiter table and put everything about a recruiter in there (except for lists of things, which are separate tables with a 1:many relationship to Recruiter).

What you're calling "favor aggregation over inheritance" I have always heard called something else: "favor composition over inheritance."  What it means is that you should, whenever possible, compose objects; that is, combine them together in useful ways by having them talk to each other through their respective interfaces, or by having one object hold one or more references to another object; rather than having the objects inherit from each other.  Composition favors more loosely coupled and more highly cohesive code.  But again, none of this really has much to do with databases.


问:
Algorithm to infer tag hierarchy
I'm looking for an algorithm to infer a hierarchy from a set of tagged items.

E.g. if the following items have the tags:

```<code>1 a
2 a,b
3 a,c
4 a,c,e
5 a,b
6 a,c
7 d
8 d,f
</code>```

Then I can construct an undirected graph (or graphs) by tallying the node weights and edge weights:

```<code> node weights  edge weights

 a 6           a-b 2
 b 2           a-c 3
 c 3           c-e 1
 d 2           a-e 1  <-- this edge is parallel to a-c and c-e and not wanted
 e 1           d-f 1
 f 1
</code>```

The first problem is how to drop any redundant edges to get to the simplified graph? Note that it's only appropriate to remove that redundant a-e edge in this case because something is tagged as a-c-e, if that wasn't the case and the tag was a-e, that edge would have to remain. I suspect that means the removal of edges can only happen during the construction of the graph, not after everything has been tallied up.

What I'd then like to do is identify the direction of the edges to create a directed graph (or graphs) and pick out root nodes to hopefully create a tree (or trees):

```<code> trees

    a        d
  // \\      |
 b     c     f
        \
         e
</code>```

It seems like it could be a string algorithm - longest common subsequences/prefixes - or a tree/graph algorithm, but I am a little stuck since I don't know the correct terminology to search for it.

答: There's a family of data mining algorithms called decision tree analysis or decision tree learning. The Wikipedia page explains how it is used to create a graph that represents a given data set.  Once the algorithm creates the model, it can be used to predict missing characteristics of new data points.</p>

Title: Algorithm to infer tag hierarchy


问:
Implementing basic data structures in programming interviews
I have been preparing to do my first technical interview in a month, and I have a question about implementing common data structures like stacks or linked lists. I plan to do the interview in either Python or Java but I am not sure which one to use yet.

I was practicing and tried to implement a stack in Python, using already built-in lists. However, lists in Python already have all stack methods, while there is still some work to do when arrays in Java are used to implement a stack.

If I am asked to implement a stack and my language of choice is Python, what should I do? Should I define Node class? Would I not even be asked to implement such low-level data structure in Python anyway?

答:
They'll probably be more interested in if you understand what a stack/linked list is, how they work, and so on.  Basically, could you implement one in C, C++ if you had to do it, assuming you knew the language?  If those things are a standard part of the languages you listed, you probably wouldn't be asked to implement them in a raw way.

But like I mentioned, they'll want you to prove that you understand the logical concepts behind the data structures if the question is coming up.  For example in answering the question on stacks, stacks are LIFO structures, where what you add last is taken off first.  It's like a stack of plates where you can only add or remove the top plate.  Then if you go into implementation, you simply take an array of a certain size, keep track of the capacity, and only operate off of that index value.  For instance, Delphi/Pascal implementation:

```<code>const
  maxstacksize = 500;
type
  TIntegerStack = class
  private
    elements: array[1..maxstacksize] of integer;
    capacity: integer;
  public
    procedure place(element: integer);
    function remove: integer;
  end;

procedure TIntegerStack.place(element: integer);
   begin
     if capacity = maxstacksize then
       // error, trying to place element on full stack, i.e. stack overflow
     else
       begin
         inc(capacity);
         elements[capacity] := element;
       end;
   end;

function TIntegerStack.remove: integer;
   begin
     Result := -1;
     if capacity = 0 then
       // error, trying to remove element from empty stack
     else
        begin
          Result := elements[capacity];
          dec(capacity);
        end;
   end;
</code>```

Most interviewers probably won't need you to take it that far to prove you know what stacks are and how they work, but it gives you an idea of what the interviewer is expecting.

Body:
They'll probably be more interested in if you understand what a stack/linked list is, how they work, and so on.  Basically, could you implement one in C, C++ if you had to do it, assuming you knew the language?  If those things are a standard part of the languages you listed, you probably wouldn't be asked to implement them in a raw way.

But like I mentioned, they'll want you to prove that you understand the logical concepts behind the data structures if the question is coming up.  For example in answering the question on stacks, stacks are LIFO structures, where what you add last is taken off first.  It's like a stack of plates where you can only add or remove the top plate.  Then if you go into implementation, you simply take an array of a certain size, keep track of the capacity, and only operate off of that index value.  For instance, Delphi/Pascal implementation:

const
  maxstacksize = 500;
type
  TIntegerStack = class
  private
    elements: array[1..maxstacksize] of integer;
    capacity: integer;
  public
    procedure place(element: integer);
    function remove: integer;
  end;

procedure TIntegerStack.place(element: integer);
   begin
     if capacity = maxstacksize then
       // error, trying to place element on full stack, i.e. stack overflow
     else
       begin
         inc(capacity);
         elements[capacity] := element;
       end;
   end;

function TIntegerStack.remove: integer;
   begin
     Result := -1;
     if capacity = 0 then
       // error, trying to remove element from empty stack
     else
        begin
          Result := elements[capacity];
          dec(capacity);
        end;
   end;

Most interviewers probably won't need you to take it that far to prove you know what stacks are and how they work, but it gives you an idea of what the interviewer is expecting.


问:
Best Practices PHP mvc routing
I have a custom MVC framework that is in a constant state of evolution. There's a long standing debate with a co-worker how the routing should work. Considering the following directory structure:

```<code>/core/Router.php
/mvc/Controllers/{Public controllers}
/mvc/Controllers/Private/{Controllers requiring valid user}
/mvc/Controllers/CMS/{Controllers requiring valid user and specific roles}
</code>```

The question is:
"Where should the current User's authentication be established: in the Router, when choosing which controller/directory to load, or in each Controller?"

My argument is that when authenticating in the Router, an Error Controller is created instead of the requested Controller, informing you of your mishap; And the directory structure clearly indicates the authentication required.

His argument is that a router should do routing and only routing. Leave it to the Controller to handle it on a case by case basis. This is more modular and allows more flexibility should changes need to be made by the router.

https://codereview.stackexchange.com/questions/26345/php-mvc-custom-routing-mechanism alluded to it, but the topic was of a different nature.

Alternative suggestions would be welcomed as well.

答:
I would go with your colleagues suggestion of leaving the Router to do its duty and not take others responsibility. I think its a good programming practice and would help others to understand the same as well.

Now, for the controllers, suggest you go ahead and use inheritance. Have a parent controller, Base_Controller, which can do all the chores which is common across all controllers.
From Base, derive Secure_Controller and NonSecure_Controller, which does what the name states.

You can always throw an exception from the controller, which can well be handled by hooks to display the view gracefully.

OwnerDisplayName: dukeofweatherby

Title: Best Practices PHP mvc routing


答:
I'd have another component in between that determines the access rights.
This component can have a set of mappings of user roles to resources, where controllers and/or actions are the resources.
The execution would then go like this:

```<code>Router -> Authorization -> Controller

Router: determines which controller and action to execute

Auth: find out which user role is logged in and check
if the user has access to the controller/action,
if not, throw an exception that will get caught by the error handler,
or redirect to login page for guests, or whatever

Controller: if the authorization component didn't complain, do work as usual
</code>```

If you want to get fancy, you can use php doc comments and reflection to annotate your methods and controllers on the spot instead of having a big list of allowed resources for user roles. Something like:

```<code>class Controller {
    /**
     * @allow({'admin','user'})
     * @prevent({'guest'})
     */
    public function someAction(){}
}
</code>```

The auth component can then read the annotations to determine access rights, instead of having a big list of role->resource mappings

Title: Best Practices PHP mvc routing

Body:
I have a custom MVC framework that is in a constant state of evolution. There's a long standing debate with a co-worker how the routing should work. Considering the following directory structure:

```<code>/core/Router.php
/mvc/Controllers/{Public controllers}
/mvc/Controllers/Private/{Controllers requiring valid user}
/mvc/Controllers/CMS/{Controllers requiring valid user and specific roles}
</code>```

The question is:
"Where should the current User's authentication be established: in the Router, when choosing which controller/directory to load, or in each Controller?"

My argument is that when authenticating in the Router, an Error Controller is created instead of the requested Controller, informing you of your mishap; And the directory structure clearly indicates the authentication required.

His argument is that a router should do routing and only routing. Leave it to the Controller to handle it on a case by case basis. This is more modular and allows more flexibility should changes need to be made by the router.

https://codereview.stackexchange.com/questions/26345/php-mvc-custom-routing-mechanism alluded to it, but the topic was of a different nature.

Alternative suggestions would be welcomed as well.

Body:
I'd have another component in between that determines the access rights.
This component can have a set of mappings of user roles to resources, where controllers and/or actions are the resources.
The execution would then go like this:

Router -> Authorization -> Controller

Router: determines which controller and action to execute

Auth: find out which user role is logged in and check
if the user has access to the controller/action,
if not, throw an exception that will get caught by the error handler,
or redirect to login page for guests, or whatever

Controller: if the authorization component didn't complain, do work as usual

If you want to get fancy, you can use php doc comments and reflection to annotate your methods and controllers on the spot instead of having a big list of allowed resources for user roles. Something like:

class Controller {
    /**
     * @allow({'admin','user'})
     * @prevent({'guest'})
     */
    public function someAction(){}
}

The auth component can then read the annotations to determine access rights, instead of having a big list of role->resource mappings


问:
How to release project under public domain?
I'm currently working on project, which is going to demonstrate how widgets can be done with Context Framework. I will also publish the source code.

In this case I want to give users as much freedom as possible, so that they can freely copy-paste, modify and also relicense whatever code they reuse.

So, how do I mention that in the project? Are the some ready-made licences etc. that are suitable for this?

答:
A ready-made license suitable for this is Unlicense. You can add the unlicense text to a project to put it under public domain:

The Unlicense is a template for disclaiming copyright monopoly interest in software you've written; in other words, it is a template for dedicating your software to the public domain. It combines a copyright waiver patterned after the very successful public domain SQLite project with the no-warranty statement from the widely-used MIT/X11 license...


问:
Graph to show decreasing desirability when taking shortcuts
I'm responsible for creating a presentation to management that details how a system should be refactored or rewritten. We have an existing system that has had production failures and the code base is not in a desirable state.

Here are the current possible options:

Rewrite the system using developers that provide the best code quality

Rewrite the system using developers that write less-than-ideal code quality

Leave the system as is and just maintain it

Rewrite the system using cheaper labor on the other side of the world

The issue isn't which one to choose, because I believe #1 provides the best long-term value to the business. The issue is how to present this to management so they understand the costs to each shortcut they take from #1.

For example, I would start with presenting #1... then what? My original idea is to somehow show each shortcut and what that means in terms of long-term cost/benefit. So, perhaps management wants to use cheaper labor. I need to show what that does to quality, time, etc. How do I show that? Perhaps management wants to use a hybrid of the above options. How do I show that? There could be 20 different permutations of the above options, and I'd like to show that if they take the short-term approach of low-cost everything, what that eventually means to the quality of the product.

Are there graphs for this type of thing? A recommended approach? I could just start typing, but I don't want management's eyes to glaze over from too many words. Hope that all makes sense.

An idea is to present something like this for each option:

答:
The business is interested in time, money, and emotion (depending on the product).  I dealt with a similar problem, and presented it from a few different perspectives:

It is costing us 'X' per month to maintain TerribleApp, adding no value to the business.  If we spent 'X' up front, this project would pay itself off in 'X' time, and be much more reliable, easier to use, etc.

TerribleApp is taking up 'X%' of the development teams time, which means we are that much slower getting our new projects, such as AwesomeApp out the door.

Remember the last time TerribleApp crashed catastrophically and took 'X' days to get back up and running?  This caused dissatisfaction to all our customers, reflected the organisation badly, applied pressure to the executives and entire IT team, caused John to leave the organisation etc, and potentially lost us X% market share, who are less likely to by AwesomeApp when we release it. These outages will recur no matter how much work we do supporting this application, and will continue to damage our brand.

Our development team are of a much higher quality than those that built TerribleApp, as you can see from the recently released GreatApp.  If it is built by a good team who are trusted and know what they are doing, the risk of TerribleApp2.0 (might want to rename this) suffering the same fate is very low.  If we outsource or use unknown developers we increase the risk of failure, or high maintenance costs again.  If it is built off-shore project management will be much more expensive from the local contact, and the risk of another failure is higher.


问:
Python decorators and Lisp macros
When looking Python decorators someone made the statement, that they are as powerful as Lisp macros (particularly Clojure).

Looking at the examples given in PEP 318 it looks to me as if they are just a fancy way of using plain old higher-order functions in Lisp:

def attrs(**kwds):
    def decorate(f):
        for k in kwds:
            setattr(f, k, kwds[k])
        return f
    return decorate

@attrs(versionadded="2.2",
       author="Guido van Rossum")
def mymethod(f):
    ...
```

I haven’t seen any code transforming in any of the examples, like described in Anatomy of a Clojure Macro. Plus, Python’s missing homoiconicity could make code transformations impossible.

So, how do these two compare and can you say they are about equal in what you can do? Evidence seems to point against it.

Edit: Based on a comment, I’m looking for two things: comparison on “as powerful as” and on “as easy to do awesome things with”.

答:
It's certainly related functionallity, but from a Python decorator it's not trivial to modify the method being called (that would be the f parameter in your example). To modify it you could go crazy with the ast module), but you'd be in for some pretty complicated programming.

Things along this line have been done though: check out the macropy package for some really mind-bending examples.

Title: Python decorators and Lisp macros


答:
In Python (the language) the decorators cannot modify the function, only wrap it, so they are definitely far less powerful than lisp macros.

In CPython (the interpreter) the decorators can modify the function because they have access to the bytecode, but the function is compiled first and than can be fiddled with by the decorator, so it's not possible to alter the syntax, a thing lisp-macro-equivalent would need to do.

Note, that modern lisps don't use S-expressions as bytecode, so macros working on S-expression lists definitely work before bytecode compilation as as noted above, in python the decorator runs after it.

Title: Python decorators and Lisp macros

Body:
When looking Python decorators someone made the statement, that they are as powerful as Lisp macros (particularly Clojure).

Looking at the examples given in PEP 318 it looks to me as if they are just a fancy way of using plain old higher-order functions in Lisp:

def attrs(**kwds):
    def decorate(f):
        for k in kwds:
            setattr(f, k, kwds[k])
        return f
    return decorate

@attrs(versionadded="2.2",
       author="Guido van Rossum")
def mymethod(f):
    ...
```

I haven’t seen any code transforming in any of the examples, like described in Anatomy of a Clojure Macro. Plus, Python’s missing homoiconicity could make code transformations impossible.

So, how do these two compare and can you say they are about equal in what you can do? Evidence seems to point against it.

Edit: Based on a comment, I’m looking for two things: comparison on “as powerful as” and on “as easy to do awesome things with”.


问:
Why is there an alternative download page of python for China?
On http://python.org there are two download pages, http://python.org/download/ for international users and http://python.org/getit/ for Chinese users.

When accessed from a country other than china, the second link redirects to the normal download page.

Why is that so?

答:
As people have correctly guessed in the comments, the Great Firewall of China is to blame here. Martin v. Loewis clarifies this in Python-list, Python's general discussion list:

It's intentional. Notice that it goes to a different URL than the English download link.

Which is a synonym for the English download link (/getit is /download at present)
    Perhaps a translated page is planned?

No, translation is not the motivation at all.

Chinese readers will know when to use it.

Why a special link just for Chinese and not several other languages?

Because only Chinese users may have to bypass URL filters that selectively censor content from python.org. Let's see how long censors need to pick up this discussion and ban more of Python across the Great Firewall :-(

Regards,
  Martin

Source: https://mail.python.org/pipermail/python-list/2011-March/599359.html

The /getit url seems more like a proactive measure than anything else. If it gets blocked, then all python.org's administrators need to do is create another url for their Chinese users, without affecting any of their other users. Until of course the Chinese censors decide to block all of python.org. </sigh>


问:
When is a BIG Rewrite the answer?
Just read the question about the Big Rewrites and I remembered a question that I've been wanting answered myself.

I have a horrible project passed down to me, written in old Java, using Struts 1.0, tables with inconsistent relationships, or no relationships at all and even tables without primary keys or fields meant to be primary keys but aren't unique at all. Somehow most of the app "just works". Most of the pages are reused (copy pasted code) and hard-coded. Everyone who's ever worked on the project has cursed it in one form or the other.

Now I had long considered to propose to upper management a total rewrite of this horrendous application.  I'm slowly attempting to on personal time but I really feel that this deserves some dedicated resources to make it happen. Having read the articles on big rewrites I'm having second thoughts. And that's not good when I want to convince my superiors to support my rewrite. (I work in a fairly small company so the proposal has the possibility of being approved)

TL;DR
When is a big rewrite the answer and what arguments can you use to support it?

答:
There's the classic joke about "if I was going to X I wouldn't start from here".

I took a job once, where the employer's prime motivation was to bring talent aboard to launch a long-overdue rewrite of a business-critical app. The question I failed to ask was, why have you ended up in this situation in the first place? It should have been a red flag, whether the cause was

too much pressure to add functionality to maintain and incrementally refactor the beast,

too little ability in the department,

too little buy-in from clients or management for incremental work,

or whatever, and this cause festering away until the problem reached an intolerable level.

So I'm going to agree with Joel in that a massive rewrite is probably a very bad idea - unless you have firm evidence that all the underlying reasons behind why a major rewrite seems so necessary have been dealt with, you're in all likelihood going to repeat the problem in due course.

ContentLicense: CC BY-SA 2.5

Title: When is a BIG Rewrite the answer?


答:
It is quite hard to use Python decorators to introduce new control flow mechanisms.

It is bordering-on-trivial to use Common Lisp macros to introduce new control flow mechanisms.

From this, it probably follows that they're not equally expressive (I choose to interpret "powerful" as "expressive", since I think that what they actually mean).

Title: Python decorators and Lisp macros


问:
Could a reflog replace tags for bookkeeping?
My repository has a branch production and is filled with countless tags of the format production-20130101-1234 that point to what has been actually shipped on the given datetime.

I think this solution is somewhat messy and unwieldy and I wonder if reflogs would skip in here better. The objective is "make sure you can always find out what version has been deployed and when exactly".

Tags do the job, but reflogs sound even nicer because:

They are made automatically,

They allow a good amount of awesome, like in the form of git checkout origin/production@{3 months ago}.

That said, I don't know that much about reflogs and I'm not sure about possible downsides of such approach.

I assume origin/production has its own reflog totally independent of my local remote-tracking production - is that correct?

Doing 3 consecutive no-ff merges from an integration branch to my local production branch would make 3 entries in my local reflog, but pushing it to the central repo (as a part of the deployment procedure) would make 1 new entry in the "oracle" origin/production reflog, correct?

I'd like to maintain the "1 deployment = 1 record" invariant.

How to make sure a reflog of origin/production isn't pruned and continues to provide complete historical information for everyone?

Any downsides or possible problems I didn't think of?

答:
The main problem is that reflog is local thing and never propagated anywhere. So the data would be somewhat difficult to get at.

One alternative of tags that comes to my mind is a branch with subproject. Git tree can contain a link to a commit. So you can create a repository above the actual project and when you deploy, commit with the correct submodule revision. I am not sure whether it would be possible to have is somewhere on the side with the subproject included via symlink. I am sure though that it would be possible to write a utility for creating such revisions in the repository without actually having a work tree at all (git creates commits from state of index only and you can manipulate the index directly via commands and you can tell git to use different index file). It would take some work though.


问:
Java "module" size
Is it sensible to have micro-modules, say with only a very few (perhaps one) class in it? Or should I store things in bigger repositories?

I am long-time Java developer with a software-engineering background. I try and write well encapsulated, focussed OO code. I have previously written code in large modules/projects/repositories with main module depending upon "common" modules.

"Common" modules seem to be a code-smell in the same way as common/util classes - a sign that someone hasn't thought what the real, clean encapsulation is.

I am now wanting to publish some articles on a blog and back them up with code on BitBucket, with 1 article with its own module & repository.

I like clean dependencies, hate duplication, etc and there are natural downward dependencies from 1 module to "common" code.

Java doesn't make dependency management very easy (hopefully fixed in Java 9 with Jigsaw), but interestingly Google Dart has a very nice mechanism and seems to recommend small, tight modules (and presumably repository per module).

I'm thinking of small modules of Java, with Maven dependencies. Each module in its own repository. Jar's published on some maven site.

EDIT:
1) I want to write some blog posts. Each post will be about a different topic. Each topic will relate to a cohesive module of code. I want readers to be able to download the module of code for the post.

2) I am using Bitbucket as my git repository. Point 1) implies (?) that I need a repository per module/blog article.

3) I'm trying to do things reasonably correctly: don't need perfection

4) Blog articles are only part of it: I want a good decomposition into modules for sound software engineering reasons.

5) I'm not ready to make my repositories public yet, they are incomplete, but these are the modules I currently plan: -
5a) Validation - just a single class with useful validation (I like DbC-like stuff!)
5b) HTML generator - nice module to generate HTML from Java - depends on Validation
5c) Swing "panning" component - again depends on Validation

6) Each of the modules in 5) depend upon Validation and are also discrete modules.

So, should I have
6a) 3 x repositories (allowing people to easily pick and mix)
6b) 1 x repository building a single jar
6c) 1 x repository building 3 x jar, 1 per "module"

答:
When I think of Java I tend to think in packages, not "modules", but I guess I'm too back-end/API oriented. Modules are front-end/use-case oriented and should be mostly in the view/controles layer.

In the business logic/model layer you should not worry about "module" size, since classes, interfaces etc, would be in packages.

In the presentation/view-controller you can think of "module" size, but then it's not clear what can be considered "large". For me, large is about lines of code, not number of classes. What worry me is the size of classes, not the number of them.

The size of a module is mainly dictated by the use-cases and the features the application will have. Do you feel a given screen is doing too much ?

In conclusion, if you are doing object oriented design well, you should not worry about number of class. Worry about classes having too many lines of code or screens/applications doing too much.

EDIT:
I'm really meaning encapsulation of larger ideas than a single class, and re-use of code across multiple projects.

An example package could be a Swing dialog box which has one main public class that client-code should use and any number of implementation classes which client code should not see. Java makes this difficult (until Jigsaw). Should this package be a module (own jar) if its shared over projects, or should they all be bundled up into a large "shared-dialogs" project?

Title: Java "module" size


答:
I'm not convinced a Text-UI is any different than a tab-oriented GUI, you've simply changing the display technology from pretty pictures to ugly pictures (YMMV :-) )

However, a text-based interface that can have a GUI slapped on top is an excellent idea. Think of all the text-based linux config files, then think of Webmin that allows you to view them as web pages. You can still edit the files by hand if you prefer or if its easier/quicker, but you also get the benefit of the easy-access web view.

So I'd say develop a CLI by all means, but a TUI.. no, no point in it at all. You'd be better off with a web UI first, a touch-app UI second, or a thick-client GUI third.

Title: Is it crazy to develop a TUI today?

Body:
I'm starting to develop a new program. It's basically a database interface, run a few queries, show the data in a nice way and be able to easy input new data (like this website).

Thinking about which programs I use daily I realise that only my web-browser is a GUI program, the rest is TUI (in this case TUI is Text User Interface, as in ncurses). So I plan to write this in ncurses with a vim/mutt-like interface. This program will only be used by experienced users and speed is much wanted.

I've found one study about this:
http://www.ncbi.nlm.nih.gov/pmc/articles/PMC2655855/

All other studies I've found has only looked at novice users and for them GUI is better. For an experienced user that will work daily with the program what is best?

This is sligthly similar but the difference is that this question was about a non-interactive program:

Is the development of CLI apps considered "backward"?


问:
Best techniques for an AI of a card game
I’m trying to develop an AI for a card game and I’m a bit stuck about the technique/algorithm I should use. Here are a few assumptions about the game:

After the cards are distributed to players, there is no randomness. I mean here that every player can choose which cards he plays but no random process occurs as when distributing the cards at the beginning of the game.

There is restriction about the cards which can be played when a card was already played.

The player which wins the trick, plays then first. E.g. Player 1 plays a card, Player 2 plays a card and wins. Then Player 2 plays a card and then Player 1 plays.

I know a lot of hints/rules (e.g. if I know the player has cards A, B, C then I should play D) which helps me to win to the game. Thus I first wanted to use a Bayesian network to describes those rules. The problem is that I don’t know any probabilities to assign, but I could compute an heuristic using the history of played games (against a human). Second problems, it is very likely that I don’t know all rules and that there is some implicit rules which are needed by the AI to find the optimal play.

I’m unsure if this would be a good way to develop an AI for such a card game?

I am also wondering if there is others techniques which would best fit to the problem. For instance, I had a look at minimax (maybe with a pruning algorithm), but would be a good option for this problem? I’m quite unsure since the most important plays are at the beginning of the game when there is the highest unknown parameters (most of cards are not played yet).

答:
I think it depends on the rules of the game.

Here's what I understand from your question :

The game is played in rounds, with each player playing one card per round

The player who goes first can play any card he wants

The player who goes second can only play certain cards, depending on what was played first

The player who wins the round goes first the next round

All the cards are distributed before the first round

Assumptions :

With full knowledge of the other player's cards, the player going first can decide, for each of his, whether a card will win the round or not (first player can play a sure win card)

If card A and B will both win when played first this round, playing A this round (and winning) then playing B the following round means B will win too (cards don't lose value)

With full knowledge of the other player's cards, the player going second can decide if a card can win this round, but would lose if played first the following round (chose the worst winning card)

Example game that follows these rules :

First player plays a card. Second player has to play a card of the same suite or lose. If suites match, highest card wins.

Now, this game is decided by luck of the draw and by being able to memorize what cards have been played in order to know your opponents hand.

In this situation, I'd make the AI only partially remember what cards were played, i.e. randomly remove from the remembered list some percentage of the cards played (lower number = higher difficulty AI), but not important ones like Aces or Kings. This way, for example, the AI will know it's safe to play a Queen of Hearts because he will remember the opponent doesn't have the Ace or the King, but will have to calculate a probability if he wants to then play the 10, because he might not remember if the Jack is still in play.

This mimics human attention span.

TL;DR

Limit how much the AI knows so its decisions are not perfect, just good enough.


问:
Overcoming slow problem solving due to increased knowledge of what might go wrong
This has been troubling me for some time, and I'd really appreciate the input of other professionals.

Short background: I started programming when my parents bought me my first computer in 1988 (at age 14, I'm 39 now). I followed a couple of other career paths before finally becoming a professional programmer in 1997. Late bloomer, perhaps, but that's how it was. I'm still happy with my choice, I love programming, and I consider myself good at what I do.

Lately, I've been noticing that the more experience I gain, the longer it takes me to complete projects, or certain tasks in a project. I'm not going senile yet. It's just that I've seen so many different ways in which things can go wrong. And the potential pitfalls and gotchas that I know about and remember are just getting more and more.

Trivial example: it used to be just "okay, write a file here". Now I'm worrying about permissions, locking, concurrency, atomic operations, indirection/frameworks, different file systems, number of files in a directory, predictable temp file names, the quality of randomness in my PRNG, power shortages in the middle of any operation, an understandable API for what I'm doing, proper documentation, etc etc etc.

In short, the problems have long since moved from "how do I do this" to "what's the best/safest way of doing it".

The upshot is that it takes me longer to finish a project than a novice. My version may be rock solid, and as impenetrable as I know how to make it, but it takes longer.

The "create file" example above was just that, an example. Real tasks are obviously more complex, but less suited for a generic question like this one. I hope you understand where I'm going with this. I have no problem coming up with efficient algorithms, I love math, I enjoy complex subjects, I have no difficulties with concentration. I think I do have a problem with experience, and consequently with a fear of errors (intrinsic or extrinsic).

I spend almost two hours a day reading up on new developments, new techniques, languages, platforms, security vulnerabilities, and so on. The conundrum is that the more knowledge I gain, the slower I am in completing projects.

How do you deal with this?

答: In the words of Edsger Dijsktra: “If debugging is the process of removing software bugs, then programming must be the process of putting them in. ” You are just doing less of the latter so you have to do less of the former. It's just a question of learning to spend time coding it right. I am still a relatively young programmer (read 20ish) and I aspire to be able to code something completely right once. An hour of planning and 10 minutes of coding is way better than an 10 minutes of planning, an hour of coding, and three of debugging.</p>


答:
Your example sounds similar to Bridge. Top Bridge-playing systems use Monte Carlo methods to select moves. At a high level:

Determine the probabilities of each card being in a given hand. You know with certainty which cards are in your hand and which cards have been played. Determine the probability of all other cards based on cards that have been played and possibly a player's bid if there's bidding involved. To start, you could just use a naive and equal probability that a card is in some player's hand.

Now, run through as many "virtual" games as you can. Simulate playing a card from your hand and then determine your opponents' responses using the rules of the game and your probabilities. For each virtual game, use your probabilities to assign cards to a player and then quickly simulate the game. Assume each player will play to the best of their ability. You know all the cards in your virtual game so you can make each player play perfectly.

When you have a solid sampling (or you run out of time), pick the legal move that gave you the best outcome most often.

Once you get something working, you can add all sorts of enriched strategies. For instance, vary your probabilities based on a player's historic plays, vary probabilities based on a player's style (passive, cautious, aggressive), or even consider the effects of specific players playing together.

Edit per LaurentG's comment:

Ultimately, you may want to scrap the idea of perfect play for all players and substitute something more realistic. Conceptually, separate the probabilities for a card being in someone's hand (card distribution) from the probability of a player playing a given legal card during a hand (card selection).

Card selection is ripe for learning. If you track plays across games, you can learn how a given player, or players in general, tend to play based on the cards in their hand and the cards that have been played. You could even get fancy and model their assumptions about cards hidden from them.

There are also learning opportunities for card distribution. A player's past bids and card selection during a hand might reveal a "tell" about what's hidden in their hand. You could use historic data to adjust probabilities when building each virtual game.

Title: Best techniques for an AI of a card game

Body:
I’m trying to develop an AI for a card game and I’m a bit stuck about the technique/algorithm I should use. Here are a few assumptions about the game:

After the cards are distributed to players, there is no randomness. I mean here that every player can choose which cards he plays but no random process occurs as when distributing the cards at the beginning of the game.

There is restriction about the cards which can be played when a card was already played.

The player which wins the trick, plays then first. E.g. Player 1 plays a card, Player 2 plays a card and wins. Then Player 2 plays a card and then Player 1 plays.

I know a lot of hints/rules (e.g. if I know the player has cards A, B, C then I should play D) which helps me to win to the game. Thus I first wanted to use a Bayesian network to describes those rules. The problem is that I don’t know any probabilities to assign, but I could compute an heuristic using the history of played games (against a human). Second problems, it is very likely that I don’t know all rules and that there is some implicit rules which are needed by the AI to find the optimal play.

I’m unsure if this would be a good way to develop an AI for such a card game?

I am also wondering if there is others techniques which would best fit to the problem. For instance, I had a look at minimax (maybe with a pruning algorithm), but would be a good option for this problem? I’m quite unsure since the most important plays are at the beginning of the game when there is the highest unknown parameters (most of cards are not played yet).


问:
Keywords for defining constructs in programming languages
I'm designing a language conceptually similar to Go.

In Go, for example, the keyword func is used for defining functions:

```<code>func abs (a int) uint { ... }
</code>```

Same for defining types.

However, I was thinking of a grammar that doesn't consider these keywords.

For example:

```<code>abs (a int => uint) { ... }
</code>```

Or, for structures:

```<code>Tree {size int; age int}
</code>```

What's wrong with such a syntax?  Are there some potential problems that could be encountered?

For example, I thought that if there was a construct that would use the same grammar as a struct, such as an interface, it would be vague (is this a struct or an interface)?

答:
What's wrong with such a syntax?

There are two basic things that can be wrong with such a syntax:

It's ambiguous - identifier, parens, block is a common syntax that can be used for blocks, class definitions and other things. You're likely to run into cases where your grammar cannot distinguish between function declarations and these other things. Or if it can, it uses dirty tricks. Or that it can, but does really poorly in partial-parsing situations (intellisense, debugging/error reporting).

It's confusing - even if the compiler can grok the source, humans cannot. If the function declaration looks too similar to other things, people will get confused. If people get confused, they'll either not use your language or start adopting (conflicting) idioms to clarify the syntax.


问:
Where does Objective-C fall in the language performance continuum?
There seems to be a lot of discussion of the various speed merits to C or C++ as compared to say Java or Python, but I rarely see Objective-C mentioned. Roughly where does it fall in terms of language performance?</p>

答:
The fundamental speed differences between Obj-C and C/C++, as Oliver says below, are due to dynamic method dispatch.

This article profiles this overhead in Obj-C
http://it.toolbox.com/blogs/macsploitation/bypassing-objectivecs-message-passing-mechanism-for-speed-24946

It also provides a very nice trick for optimizing your Obj-C code when you determine method dispatch (i.e. objc_msgSend) is the limiting factor -- obtain a pointer to the function once, and use it to call the function many times.  It shouldn't help too much as Obj-C runtimes do this optimization automatically.

Note that the true cost of dynamic method dispatch is due to cache misses, because it breaks CPU branch prediction.  These are hard to profile and it may be that the code cited above does not measure true cache miss cost.

Some more useful discussion is here: http://www.cocoabuilder.com/archive/cocoa/106535-instance-variable-access.html#106605

Bottom line: the biggest differences between languages are your algorithms.  Beyond that, there is a fundamental speed difference between Obj-C, C, and C++, due to dynamic or virtual method dispatch.  This second point does not appear to be large.  And the article above gives a trick to optimize it, if you can find hot spots via profiling, which may be difficult due to CPU cache misses.


答:
To solve this problem it is important to consider how you want to represent the Graph. In your sketch you have assigned a weight both to nodes and to edges, but it seems that the edge weight is irrelevant. A good representation might be a pair of the weight and an array of neighbours:

Node(weight: Int, name: String, neighbours: Set of Node)
```

<p>During the building of the initial undirected graph, you maintain a dictionary of nodes by their name.</p>

<p>For each path in the input, you look up or create all nodes in the dictionary. You then increment the weight of all nodes, and connect the nodes on each pair of the graph to each other:</p>

<pre class="lang-scala prettyprint-override"><code>var nodes = path.split("-").map(s => dict.lookupOrCreate(s));
// increment the weight
for var node <- nodes { node.weight++ }
// add the connections
for var i <- (1 upto nodes.length) {
  nodes[i  ].neighbours.add(nodes[i-1]);
  nodes[i-1].neighbours.add(nodes[i  ]):
}
</code>```

We now have an undirected, unconnected graph with weighted nodes:

```<code>2   6   3   1   2   1
b ↔ a ↔ c ↔ e   d ↔ f
     ↖_____↗
</code>```

It is now that you will want to remove cycles. Your example contains the subgraph

```<code>  e 1        I.  a6 → e1 → c3     ↗ e1
 / \    ==>                     a6     III.
a 6 |        II. a6 → c3 → e1     ↘ c3
 `- c 3
</code>```

As you can see, there are three possibilites to eliminate this cycle while starting from a. The 3rd possibility is the easiest to implement.

If you want one of the other orders, you'll have to do more expensive reachability tests: For each two child nodes, skip (but not mark as seen) one of those child nodes if it is reachable by the other without using a path that uses the current node.

Next, we want to get the node with the highest weight. This is a simple sort.

```<code>var [..., highest] = dict.values.sortBy(n => n.weight);
</code>```

Now, we traverse the whole graph starting from that node and build the DAG. We can either build a completely new graph (preferable) or remove the current node from the neighbour set. If this set then contains any nodes that were already visited during DAG buidling, a cycle was detected (which we break by skipping that node). Subgraphs that are not reachable will be forgotten, this would eliminate the d-e part. We can work around of this by deleting any nodes from the dictionary, and creating DAGs until the dict is empty. This would create a forest.

var forest = collect until dict.isEmpty {
  var [..., highest] = dict.values.sortBy(n => n.weight);

  def recurse(node: Node): DagNode {
    // remove current node from dict
    dict.delete(node.name);

    // remove seen childs – eliminates cycles
    var childs = node.neighbours.filter(c => dict.contains(c.name));
    // mark all new childs as seen
    for var child <- childs { dict.delete(child.name) }

    // return the DAG subgraph (here: tree) and recurse into each child
    return DagNode(weight: node.weight,
                   name:   node.name,
                   childs: childs.map(c => recurse(c)));
  }

  yield recurse(highest);
}
```

<p>This should create the forest</p>

```  _a_    d
 / | \   |
b  c  e  f
```

Title: Algorithm to infer tag hierarchy

Body:
I'm looking for an algorithm to infer a hierarchy from a set of tagged items.

E.g. if the following items have the tags:

```<code>1 a
2 a,b
3 a,c
4 a,c,e
5 a,b
6 a,c
7 d
8 d,f
</code>```

Then I can construct an undirected graph (or graphs) by tallying the node weights and edge weights:

```<code> node weights  edge weights

 a 6           a-b 2
 b 2           a-c 3
 c 3           c-e 1
 d 2           a-e 1  <-- this edge is parallel to a-c and c-e and not wanted
 e 1           d-f 1
 f 1
</code>```

The first problem is how to drop any redundant edges to get to the simplified graph? Note that it's only appropriate to remove that redundant a-e edge in this case because something is tagged as a-c-e, if that wasn't the case and the tag was a-e, that edge would have to remain. I suspect that means the removal of edges can only happen during the construction of the graph, not after everything has been tallied up.

What I'd then like to do is identify the direction of the edges to create a directed graph (or graphs) and pick out root nodes to hopefully create a tree (or trees):

```<code> trees

    a        d
  // \\      |
 b     c     f
        \
         e
</code>```

It seems like it could be a string algorithm - longest common subsequences/prefixes - or a tree/graph algorithm, but I am a little stuck since I don't know the correct terminology to search for it.

Body:
To solve this problem it is important to consider how you want to represent the Graph. In your sketch you have assigned a weight both to nodes and to edges, but it seems that the edge weight is irrelevant. A good representation might be a pair of the weight and an array of neighbours:

Node(weight: Int, name: String, neighbours: Set of Node)

During the building of the initial undirected graph, you maintain a dictionary of nodes by their name.

For each path in the input, you look up or create all nodes in the dictionary. You then increment the weight of all nodes, and connect the nodes on each pair of the graph to each other:

var nodes = path.split("-").map(s => dict.lookupOrCreate(s));
// increment the weight
for var node <- nodes { node.weight++ }
// add the connections
for var i <- (1 upto nodes.length) {
  nodes[i  ].neighbours.add(nodes[i-1]);
  nodes[i-1].neighbours.add(nodes[i  ]):
}

We now have an undirected, unconnected graph with weighted nodes:

2   6   3   1   2   1
b ↔ a ↔ c ↔ e   d ↔ f
     ↖_____↗

It is now that you will want to remove cycles. Your example contains the subgraph

  e 1        I.  a6 → e1 → c3     ↗ e1
 / \    ==>                     a6     III.
a 6 |        II. a6 → c3 → e1     ↘ c3
 `- c 3

As you can see, there are three possibilites to eliminate this cycle while starting from a. The 3rd possibility is the easiest to implement.

If you want one of the other orders, you'll have to do more expensive reachability tests: For each two child nodes, skip (but not mark as seen) one of those child nodes if it is reachable by the other without using a path that uses the current node.

Next, we want to get the node with the highest weight. This is a simple sort.

var [..., highest] = dict.values.sortBy(n => n.weight);

Now, we traverse the whole graph starting from that node and build the DAG. We can either build a completely new graph (preferable) or remove the current node from the neighbour set. If this set then contains any nodes that were already visited during DAG buidling, a cycle was detected (which we break by skipping that node). Subgraphs that are not reachable will be forgotten, this would eliminate the d-e part. We can work around of this by deleting any nodes from the dictionary, and creating DAGs until the dict is empty. This would create a forest.

var forest = collect until dict.isEmpty {
  var [..., highest] = dict.values.sortBy(n => n.weight);

  def recurse(node: Node): DagNode {
    // remove current node from dict
    dict.delete(node.name);

    // remove seen childs – eliminates cycles
    var childs = node.neighbours.filter(c => dict.contains(c.name));
    // mark all new childs as seen
    for var child <- childs { dict.delete(child.name) }

    // return the DAG subgraph (here: tree) and recurse into each child
    return DagNode(weight: node.weight,
                   name:   node.name,
                   childs: childs.map(c => recurse(c)));
  }

  yield recurse(highest);
}

This should create the forest

  _a_    d
 / | \   |
b  c  e  f


问:
ASP.NET MVC create a draft without persisting to DB
I have a requirement to allow users to suggest updates to their profiles on a site I'm working. They would be able to view their profile details, then click on and "edit" button, which would basically make a copy of all their data contained in a handful of one-to-many related DB tables. They can then add or remove items and update various text fields before saving the draft. Upon saving, an administrator would have to approve this change before the new data is actually published onto the live site.

I currently have an identical set of tables for live data and drafts (and history, but that's not important). My difficulty is that the client doesn't want an actual draft entry created in the drafts db structure until the user has hit "save". A user would be able to perform CRUD operations against all the data, including uploading new images, but I am not allowed to touch the DB unless they're ready to commit these updates.

My great problem is that I have no idea how to begin developing what seems to the client to be a very simple thing, even though it's technically quite challenging. I also can't seem to make the client understand that hitting the "Cancel" button next to the save button would remove the entry plus all its related data from the DB, making the problem moot in my humble opinion.

So, the question is such: is there a way to phrase my objection in such a way that the client can understand what I'm getting at. Or failing that, what would be a good way of developing such a session-stored object?

答: You could try storing client drafts in a cookie or the browser's web storage, that way the clients have the drafts 'persisted' but they aren't in the database.</p>


问:
Why use in-memory database for integration testing?
I'm really confused when I see a lot of in-memory database implementation used for testing, because I also heard a lot from integration testing best practices that the environment running the test should resemble as closely as possible the production environment, including operating system, library, database engine, etc.

What am I missing here?

答:
I guess it's a trade-off speed/matching the environnement. Tests should be run often, and that mean they got to be fast. Especially unit tests, which should not take more than a few seconds.

Integration tests will run slower, but when they're fast you can run them more often. For example, before every commit. Of course, it's not as complete as a full environment, but at least you're testing the mapping layer, the generated SQL, how the part talk to each other, etc. In the case of costly database you also ensure that you don't need to buy a licence for everyone. You may catch more error covering 90% of the code with tests ran once per hour than covering 100% of the code testing once per day or worst, week.

That being said, of course you need test with the real database and a fully integrated environment. You may not run those tests as often, but since your previous test already gave you confidence, all that is left is weird platform specific bug.


答:
For doing simple tests, mocking the database access layer is perfectly acceptable.  You call getName(), it calls the DAO thats been mocked and returns "John" for the first name and "Smith" for the last name, assembles them and everything is perfect.  No need to actually unit test a database there.

Things becomes a bit more when logic becomes a bit more complex.  What if you had a method "createOrUpdateUser(...)".  If you mocked the database you can verify that a given method has been called once with a certain parameter when the mock returns no objects and a different method is invoked on the database when it returns back an existing object.  This starts getting to that fuzzy line where it might be easier (especially if it was already there) to spin up a specialized in memory database and test that code with preconfigured data.

In some actual code I worked on (point of sales), we had a resumeSuspededTransaction(...) method.  This would pull the transaction from the database into an object (and its components) and update the database.  We had it mocked and a bug lurked in the code somewhere with the serialization and deserialization of the data going to the database (we changed a type which was serialized differently on the database).

The mock didn't show us the bug because it was returning its happy path - serialize the transaction, store it in the mock, deserialize it from the mock, test that they are equal.  However, when you serialize an object with a leading zero to the database it was dropping them and then recombining it back to a string without the zeros.  We caught the bug without the database through troubleshooting (it wasn't that hard to spot once we knew it was there).

Later, we put a database in there and realized that the bug would have never gotten through that junit test if we were instead going to an in memory database.

In memory databases have the advantages:

they can be spun up quickly (without needing a DBA to set up accounts, tables and such) for testing

the data can be preconfigured for that test

the test doesn't need to worry about rolling back the test when done

each test has its own in memory database so you don't have to worry if two tests are running simultaneously

they can be run on systems that don't have connectivity to the real databases

Title: Why use in-memory database for integration testing?

Body:
I'm really confused when I see a lot of in-memory database implementation used for testing, because I also heard a lot from integration testing best practices that the environment running the test should resemble as closely as possible the production environment, including operating system, library, database engine, etc.

What am I missing here?


答: Rather than storing changes in a series of tables, store them as a document that your application will know how to render. When the admin views the draft, the application will render it (and possibly highlight the differences between the original and the proposed change). Finally when the draft is approved, the document is applied to the DB, pushing the changes from the draft into the permanent data.</p>

Title: ASP.NET MVC create a draft without persisting to DB

Body:
I have a requirement to allow users to suggest updates to their profiles on a site I'm working. They would be able to view their profile details, then click on and "edit" button, which would basically make a copy of all their data contained in a handful of one-to-many related DB tables. They can then add or remove items and update various text fields before saving the draft. Upon saving, an administrator would have to approve this change before the new data is actually published onto the live site.

I currently have an identical set of tables for live data and drafts (and history, but that's not important). My difficulty is that the client doesn't want an actual draft entry created in the drafts db structure until the user has hit "save". A user would be able to perform CRUD operations against all the data, including uploading new images, but I am not allowed to touch the DB unless they're ready to commit these updates.

My great problem is that I have no idea how to begin developing what seems to the client to be a very simple thing, even though it's technically quite challenging. I also can't seem to make the client understand that hitting the "Cancel" button next to the save button would remove the entry plus all its related data from the DB, making the problem moot in my humble opinion.

So, the question is such: is there a way to phrase my objection in such a way that the client can understand what I'm getting at. Or failing that, what would be a good way of developing such a session-stored object?