问:
Storing mutliple selected values in DB
In my Android app, there is a question like "Which of the following foods did you eat today?" (not the actual question), with a fixed list of foods (e.g. "Apples, Banana, Pizza, Burgers") from which the user can select all that apply.

I now want to store in the DB, which answers were selected on which day.
As I see it, I have several possibilities:

Store them together in one field as delimited text. This breaks if the user changes his language setting, so it's not really an option for my app.

Use a Bitmask to convert the selection to an integer and store that in the database. Keeps the DB schema nice and simple, and makes use of android's native ability to select the right string according to the set language, but makes it practically impossible to query for a specific answer. SQL queries are simple but additional data-mangling in the app is needed.

Do a proper normalization. Bloats the database, since there has to be an answers-table for every supported language, but queries for specific answers are possible. SQL queries are more complex but reduces the need for data-mangling in the app itself.

Which of these should I use?

Is there a solution I haven't thought of that's probably better?

Is there some sort of best practice or guideline regarding this sort of problem in android development?

答:
Option 3 by a country mile, once you've addressed the assumption about bloat in the database design...

It looks like the implementation would 'bloat' the database by creating per-language tables or additional fields per-language.  This is a 'red flag' for you to look out for in the future: when adding support for another language requires table creation and having to update queries etc. then that your design is storing DATA (the language/translation target) in the database DESIGN (the table structure) when the two clearly should not mix.

Once you've spotted the error then your solution is pretty straightforward and might contain two tables:

Foods (LanguageId, FoodId, Name) where only a combination of LanguageId and FoodId is unique.

Selections (UserId, FoodId, Timestamp) to log who chose what and when.

That's it, you're done with minimal fuss.  Processing is easy as the FoodId is per food rather than per-translation and if the user changes their language then nothing is locale specific.

Depending on your application design you could go a step further by folding the Foods table up into a wider L18N context... Something worth exploring in a multilingual application.  But obviously that depends on what your real application is.

Title: Storing mutliple selected values in DB


问:
How to update an event log in case of new events
In CQRS with Event Sourcing, if the current domain lacks a feature so it has to be extended with a new command (sent from a new saga) and an event (sent by the aggregate handler of that command), how can I deploy such feature to an already running application if the command was never sent before and so even the event was never part of the event log until the deployment.

The event is essential for the correct aggregate initialisation, so I need to have the event in the event log for every possible situation in the past where this command/event should have been sent.

Since event log should be append only. Is there a way how to add an event to the past event stream if the deployment of such feature requires it? Is it ok to do this manually? Or what is the way.

Example

To be more precise about my problem: consider a file system application, where I have aggregates for files (File) and folders (Folder). Individual files and folders (both called nodes) are aggregate roots. I cannot store the whole file system tree in a single root because of performance.

In the old version, if I create a new file in a folder, it is a command on File aggregate in which I set its parent folder (aggregate's property). Now I came to realization that I would like to have a list of IDs of nodes that a folder contains. This feature was not part of the original version so folders had no idea of the nodes that were stored in the folder.

My solution was to create a saga started by the NewFileCreated event, that would send a new AddFileToFolder command to the parent Folder aggregate. That command would apply an event (FileAddedToFolder) in Folder that would add the file's ID to its set of children nodes.

To deploy this feature, where I would like to have the correct children IDs in all folders, is restricted by the absence of these events in the original event log.

What should be the way to update the old event log in that case?

答:
I think in this case instead of adding this to your aggregate, your best bet would be to have a fully-consistent local database for your aggregates to use to keep track of their relationships. It could be embedded. It could be a key/value store or relational, or whatever is convenient. This is not an eventually-consistent view model, but a separate database which is kept consistent with your aggregates.

Actually, your node relationships are in the events already, they are just not in a usable form for your delete command. An event store is not a relational database, and relationships are prohibitively expensive to query. Using a view model isn't quite good enough because it is eventually consistent. So a local fully-consistent database is your best out here. This is also a way commonly used for satisfying uniqueness checks (e.g. email address) as part of handling commands.

You can populate this database with relationships from existing events (basically a one-off denormalizer which reads events and updates this database) during deployment so it is up to date when it rolls into production.

Whenever a new file is created under a folder, this database is updated as part of handling the command.

```<code>CreateFile folderId -> FileCreated (in folderId)
                       save events
                       update local database for folderId to add child
</code>```

When you go to process the delete command on the folder, you have the capability to lookup all subnodes of the folder from that database.

I question whether going event sourced is the best choice for this problem set. It's not optimal for the scenario described here. However, it still might be worth using (and overcoming issues like this) if auditability is a primary requirement.

ContentLicense: CC BY-SA 3.0

Body:
I think in this case instead of adding this to your aggregate, your best bet would be to have a fully-consistent local database for your aggregates to use to keep track of their relationships. It could be embedded. It could be a key/value store or relational, or whatever is convenient. This is not an eventually-consistent view model, but a separate database which is kept consistent with your aggregates.

Actually, your node relationships are in the events already, they are just not in a usable form for your delete command. An event store is not a relational database, and relationships are prohibitively expensive to query. Using a view model isn't quite good enough because it is eventually consistent. So a local fully-consistent database is your best out here. This is also a way commonly used for satisfying uniqueness checks (e.g. email address) as part of handling commands.

You can populate this database with relationships from existing events (basically a one-off denormalizer which reads events and updates this database) during deployment so it is up to date when it rolls into production.

Whenever a new file is created under a folder, this database is updated as part of handling the command.

CreateFile folderId -> FileCreated (in folderId)
                       save events
                       update local database for folderId to add child

When you go to process the delete command on the folder, you have the capability to lookup all subnodes of the folder from that database.

I question whether going event sourced is the best choice for this problem set. It's not optimal for the scenario described here. However, it still might be worth using (and overcoming issues like this) if auditability is a primary requirement.


问:
What features would you like to have in PHP?
Since it's the holiday season now and everybody's making wishes, I wonder - which language features you would wish PHP would have added? I am interested in some practical suggestions/wishes for the language. By practical I mean:

Something that can be practically done (not: "I wish PHP would guess what my code means and fix bugs for me" or "I wish any code would execute under 5ms")

Something that doesn't require changing PHP into another language (not: "I wish they'd drop $ signs and use space instead of braces" or "I wish PHP were compiled, statically typed and had # in it's name")

Something that would not require breaking all the existing code (not: "Let's rename 500 functions and change parameter order for them")

Something that does change the language or some interesting aspect of it (not: "I wish there was extension to support for XYZ protocol" or "I wish bug #12345 were finally fixed")

Something that is more than a rant (not: "I wish PHP wouldn't suck so badly")

Anybody has any good wishes?

Mod edit: Stanislav Malyshev is a core PHP developer.

答:
PHP needs an immutable Unicode string class. Ensuring input strings are valid, normalized UTF-8 and that it stays valid should be trivial.

```<code>$str = new Utf8String('āll īs ōk');
$str = $str->ucwords(); // Āll Īs Ōk
$str = $str->strtolower()->toAscii(); // all is ok
(string) $str; // UTF-8 bytes
</code>```

I made a prototype based on PHP-UTF8.

ContentLicense: CC BY-SA 2.5

Body:
PHP needs an immutable Unicode string class. Ensuring input strings are valid, normalized UTF-8 and that it stays valid should be trivial.

$str = new Utf8String('āll īs ōk');
$str = $str->ucwords(); // Āll Īs Ōk
$str = $str->strtolower()->toAscii(); // all is ok
(string) $str; // UTF-8 bytes

I made a prototype based on PHP-UTF8.


问:
What's the best RESTful URL structure for a recursive resource?
I'm creating a RESTfull service for a tree-like resource structure and was wondering what the best URL structure would be?

I have three requirements:

be able to get a collection of root
resources

be able to get an
individual resource

be able to get a
collection of child resources

My current thought is:

```<code>/rest/documents
/rest/documents/{id}
/rest/documents/{id}/documents
</code>```

I was also thinking of going the singular / plural routes to denote lists or individual elements, but I know I'm going to have a resource that is the same plural as it is singular, so decided against this.

Does anybody have any thoughts on the above? or have a different / better way of structuring this?

答:
What comes to my mind is: do not let you RESTful API reflect the recursivity in the URL itself. Come to think of it, your resource is only the documents.

If you have your documents stored physically according to the recursive structure, create a mapping to an unique ID, and use the ID in the URL:

```<code>/rest/documents/{id}
</code>```

Now, if you have your documents like this:

```
| DocumentName | DocumentPath | DocumentID |
--------------------------------------------
| abc          | /abc         | 1          |
| asd          | /abc/asd     | 2          |
| asd          | /asd         | 3          |
| boo          | /abc/asd/boo | 4          |
| hey          | /abc/asd/hey | 5          |
```

the request would consult this url for /abc/asd document

```<code>GET /rest/documents/2
</code>```

So, now you have to provide users of your API with means to traverse your structure with little effort. That could be done by wrapping your response payload (document) into an object, containing additional traversal information, like this:

```<code>{
   data: { /* your document goes here */ },
   parent: {"abc": 1 },
   children: [ { "boo": 4 }, { "hey": 5} ]
}
</code>```

provided that you expect users not to create too much documents in a single level, you can include a list of children into the response. If that's not the case you could offer user to retrieve child document IDs like so, allowing e.g. for paging the results via querystring parameters:

```<code>GET /rest/documents/2/children?page=2&size=50
</code>```

Finally, speaking of querystring parameters, you could also supply the path information directly through the querystring parameters:

```<code>GET /rest/documents?path=somepath&page=1&size=42
</code>```

All of the approaches mentioned expect that the plain GET /rest/documents returns only root documents.

Body:
What comes to my mind is: do not let you RESTful API reflect the recursivity in the URL itself. Come to think of it, your resource is only the documents.

If you have your documents stored physically according to the recursive structure, create a mapping to an unique ID, and use the ID in the URL:

/rest/documents/{id}

Now, if you have your documents like this:

| DocumentName | DocumentPath | DocumentID |
--------------------------------------------
| abc          | /abc         | 1          |
| asd          | /abc/asd     | 2          |
| asd          | /asd         | 3          |
| boo          | /abc/asd/boo | 4          |
| hey          | /abc/asd/hey | 5          |

the request would consult this url for /abc/asd document

GET /rest/documents/2

So, now you have to provide users of your API with means to traverse your structure with little effort. That could be done by wrapping your response payload (document) into an object, containing additional traversal information, like this:

{
   data: { /* your document goes here */ },
   parent: {"abc": 1 },
   children: [ { "boo": 4 }, { "hey": 5} ]
}

provided that you expect users not to create too much documents in a single level, you can include a list of children into the response. If that's not the case you could offer user to retrieve child document IDs like so, allowing e.g. for paging the results via querystring parameters:

GET /rest/documents/2/children?page=2&size=50

Finally, speaking of querystring parameters, you could also supply the path information directly through the querystring parameters:

GET /rest/documents?path=somepath&page=1&size=42

All of the approaches mentioned expect that the plain GET /rest/documents returns only root documents.


问:
Serialized values or separate table, which is more efficient?
I have a Rails model email_condition_string with a word column in it. Now I have another model called request_creation_email_config with the following columns

admin_filter_group:references

vendor_service:references

email_condition_string:references

email_condition_string has many request_creation_email_config and  request_creation_email_config belongs to email_condition_string.

Instead of this model a colleague of mine is suggesting that storing the word inside the same model as comma separated values is more efficient than storing it as a separate model. Is that all right?

答:
It really depends on how you plan to use this later.

You are anyway one migration (and some code changes ofc) from changing this solution from one to the other. So I would choose whatever, and later benchmark it in a way your users really use the system.

Even better - profile the app and look for bottlenecks first, instead of optimizing it prematurely.

If you use PostgreSQL or Oracle one more option is to use array type instead of serialized string. It could be it's more efficient with indexing such type. Or you may want to use full text search solution later.

To summarize - it depends on how you utilize this data and how big the data set is.


问:
Should I use my own public API for my web interface?
I am designing an API with django and its rest framework (and I am already not sure if it worth the hassle to version it or not).

I totally know the iOS and Android apps will call the API, get a token (maybe with expiration I need to read a bit more about it) and then use this token inside every subsequent calls to be able to authenticate.

But my main question is : should my web interface use the API ?

Because it does not seems very practical, I rather see myself using sessions and cookies and having the backend render templates passing data through it than getting a token and doing AJAX calls for everything.

答:
It's a valid approach to use an API directly by the web client.

This API needs to support a way to create a user session and return a token which the client will pass with every call.

I can see the following benefits to this solution

All yours clients (mobile, web, external) are using the same API, which can make maintaining and scaling it easier

Keeping the responsibility of HTML rendering entirely on the client
side, which is well supported by current javascript frameworks.

An alternative approach is make a layer on top of your web API, that does rendering, session management. I haven't done this because it seemed way more complicated than necessary.


问:
Composition over inheritance but
I'm trying to teach myself software engineering and coming up against some conflicting information which is confusing me.

I've been learning OOP and what abstract classes / Interfaces are and how to use them, but then I'm reading that one should 'favour composition over inheritance'. I understand composition is when one class composes / creates an object of another class to utilise / interact with that new object's functionality.

So my question is... Am I supposed to not use abstract classes and interfaces? To not create an abstract class and extend / inherit that abstract class's functionality in the concrete classes, and instead, just compose new objects to use the other class's functionality?

Or, am I supposed to use composition AND inherit from abstract classes; using them both in conjunction with each other? If so, could you provide some examples of how that would work and its benefits?

As I'm most comfortable with PHP, I'm using it to improve my OOP / SE skills before moving on to other languages and transferring my newly acquired SE skills, so examples using PHP would be most appreciated.

答:
I'm not familiar enough with PHP to give you concrete examples in that language, but here are a few guidelines that I have found useful.

Interfaces/traits are useful for defining behavior across many classes that may have very little else in common.

For example, if you are working on a JSON api, you may define an interface that specifies two methods: “toJson” and “toStatusCode”. This would allow you to write a helper that can take any object that implements this interface, and convert it into an Http response.

Most of the time, this is preferable to direct inheritance, because it's less likely to suffer from the fragile base class problem, as it tends toward shallow class hierarchies.

Direct Inheritance is best thought of as something you do when there are compelling reasons to do so, rather than something you do unless there are compelling reasons not to.

In languages that do not support default implementations in interfaces, it may be a reasonable way to share a set of base functionality, but it usually heavily restricts what you can do with the subclasses (multiple inheritance, when available, is rarely worth the pain). Often, I find it's worth the pain of writing the boilerplate redirect methods to use composition instead.

Composition should be your default strategy. As much as possible, compose with interfaces, and pass them in as constructor arguments. This will make your life much easier when writing tests.

Avoid working with global singletons as much as possible, as comparing expected and actual output is a right pain if part of the output depends on the state of the system clock.

This is, of course, not always possible. For example, the Play web framework provides a JSON library that's basically a domain specific language. Changing this would be a major undertaking, of which, replacing the calls to 'Json.prettyPrint' would be only a very minor part.

Title: Composition over inheritance but


答:
Composition over Inheritance means that when you want to re-use or extend functionality of an existing class, often it's more appropriate to create another class that will 'wrap' the existing class and use it's implementation internally. Decorator pattern is an example of this.

Inheritance isn't a good default way to deal with re-use scenarios because you often want to use just a part of a base class funtionality and the subclass can't support the whole contract of the base class in a way that satisfies Liskov substitution principle.

Template method is a good example of a case where inheritance is appropriate.

See this answer for guidelines on how to choose between composition and inheritance.

Title: Composition over inheritance but

Body:
I'm trying to teach myself software engineering and coming up against some conflicting information which is confusing me.

I've been learning OOP and what abstract classes / Interfaces are and how to use them, but then I'm reading that one should 'favour composition over inheritance'. I understand composition is when one class composes / creates an object of another class to utilise / interact with that new object's functionality.

So my question is... Am I supposed to not use abstract classes and interfaces? To not create an abstract class and extend / inherit that abstract class's functionality in the concrete classes, and instead, just compose new objects to use the other class's functionality?

Or, am I supposed to use composition AND inherit from abstract classes; using them both in conjunction with each other? If so, could you provide some examples of how that would work and its benefits?

As I'm most comfortable with PHP, I'm using it to improve my OOP / SE skills before moving on to other languages and transferring my newly acquired SE skills, so examples using PHP would be most appreciated.


问:
What structure has nodes with inputs and outputs?
If I have a bunch of nodes with differing numbers of inputs, but only one output, and each output can go to multiple inputs, but only one output per input; what do I call it?

Trying to name a class to represent the whole thing.

Assuming n1 produces a value, it flows to n2 and n3 which might perform different operations on that value, and then it continues until n5 uses two inputs to produce a final value.

答: This might be too general for what you're looking for, but it sounds like a kind of directed graph.</p>


答:
These are arguments for creating SPA that convince me:

Do you want you application to be usable offline? Take a look at how gmail behaves when there is no connection. Functionality is very limited, but, it's still usable. Things can be taken one step further since HTML5 brings many options for client-side storage. Server-side storage may be treated as only backup and synchronization component. It makes the client fault-tolarant.

How fast do you want your application to respond after each click? According to research done at Google, users feel the application as fast and responsive if there is a feedback after less than 200ms. If something may last longer, an indication of long-lasting operation should appear (spinner or progress bar). This things are doable only in SPA.

Why do you want to treat one type of clients in a special way? Implementing server-side code just for one client type will probably result in more code written. An issue of simplicity of system's architecture and probably even DRY.

API is a well defined boundary that slows down software entropy. Sole fact that there is an API to be designed forces a very good practice of actually designing the software. Lack of well defined boundaries may (in time) lower maintainability of a system, because of accidental inter-dependencies being added. This becomes significant in bigger code bases.

Moving presentation logic to the client reduces resources needed on the server. It may reduce cost of the project significantly, depending on the technology at use, size of the project, and how much the project is about presentation. I've seen a project in which half of server-side resources were consumed by presentation logic and session storage (it used JSF extensively).

So if none of above matters in your project and knowledge of JavaScript is limited in the team, it may be a good idea to stick to the old school server-side template rendering.

Your question motivated me to write a blogpost about SPA.

Title: Should I use my own public API for my web interface?

Body:
I am designing an API with django and its rest framework (and I am already not sure if it worth the hassle to version it or not).

I totally know the iOS and Android apps will call the API, get a token (maybe with expiration I need to read a bit more about it) and then use this token inside every subsequent calls to be able to authenticate.

But my main question is : should my web interface use the API ?

Because it does not seems very practical, I rather see myself using sessions and cookies and having the backend render templates passing data through it than getting a token and doing AJAX calls for everything.


问:
A little clarification needed with GNU GPL v3
I'm programmaing a visual novel which has the following parts:

Script file

Program code used for the events where parts of the scripts are displayed

2 different minigames

Images

Now one of the minigames is based on a open source code that uses the GPL v3 http://www.gnu.org/licenses/gpl-3.0.txt

I copy pasted parts of the code for that one minigame and rewrote parts of it so that it works with my code base.

So what I understand is that I need to make the minigame itself that is based upon the open source code open source itself.

What I'm not 100% sure about is: The scriptfile and the program parts that are not directly involved with the one minigame (aside from initializing it and starting it), the second minigame and the images: Do these all also have to be open source?

Note: A bit of explanation what the scriptfile is and a bit in regards to the open source minigame.

The script file is in essence a number of labels where scriptcode happens like "CharacterA with pose B says 'I'm not going to do this, no chance in hell!'".

The open source minigame: Depending on situations in the minigame specific events are triggered which then switch out of the minigame and onto displaying the script.

答:
Data processed by your application need not be licensed under the same terms as the software itself.

To decide whether something is data or program, think whether it could be replaced by any other file (using the same file format) and your application would still work. For example, if your application uses a logo icon, you can replace it with any other picture and your application will use it just as happily (I hope). This is important because logos often contain trademarks or other features that prohibit licensing them under the GPL. If the pixels of the picture somehow encode some action performed by the program, then it is probably more than just data because replacing it by some other picture would be difficult.

A requirement is, however, that you make it reasonably simple to replace the data. If your program reads ordinary files from a directory, this is fine. Anybody can replace the files therein. If you embed the binary data of the files into the source code as a huge array of integers, it is not.

There is also a remark on the question of pictures shipped with games in the Free System Distribution Guidelines.

Non-functional Data

Data that isn't functional, that doesn't do a practical job, is more of an adornment to the system's software than a part of it. Thus, we don't insist on the free license criteria for non-functional data. It can be included in a free system distribution as long as its license gives you permission to copy and redistribute, both for commercial and non-commercial purposes. For example, some game engines released under the GNU GPL have accompanying game information—a fictional world map, game graphics, and so on—released under such a verbatim-distribution license. This kind of data can be part of a free system distribution, even though its license does not qualify as free, because it is non-functional.

Please note that this quote applies to free distributions in general and is not an authoritative interpretation of the GPL.

For the source code of your application, I recommend you simply use the GPL v3 if there are no compelling reasons no to. Using this license puts you on the safe side and makes it easier for your project and the ones it interacts with to mutually benefit from each other.

If this is an option, dual-licensing the pictures under the GPL and a more permissive license (maybe some Creative Commons variant) could be useful too. Note that you may always impose less restrictions on the parts that you added – may it be data or code – as long as you also grant the rights spelled out in the GPL.


问:
dma allocation in kernel 4g patch theory
I was reading about that that old patch.
The summary of that link is to create a 4Gb kernel space, instead of 1Gb by default.

Since many dma capable devices can only access 32 bit addresses or 4Gb, would that mean that any free location in that custom kernel space (4G low memory instead of 1G) could be used without all the high memory mapping hassle?

Bonus question: Could IOMMU made faster with larger kernel low memory, too?

I think that it avoids bounce buffers, high-memory mappings and many complex things to happen -> better performance.

答:
Once upon a time (80386 era) most computers had less than 16 MiB of RAM, there were no memory mapped PCI devices, and 1 GiB of space sounded insanely huge (especially for a "temporary kernel" that was only expected to be used until GNU finished their own kernel). Mapping all of the physical RAM into kernel space sounded like a good idea; so that's what happened.

Time passed. Lots of code (device drivers, etc) was written that relied on "all physical RAM mapped into kernel space".

More time passed. PCI came along. Computers with > 1 GiB of RAM came along. Intel added PAE so computers could have almost 64 GiB of RAM. Kernel space was still limited to 1 GiB. The "all physical RAM mapped into kernel space" idea became very broken. However; by this time lots of code relied on "all physical RAM mapped into kernel space". It was too late to fix the root cause of the problem and do memory management properly (see note at the end).

This led to ugly hacks/work-arounds; starting with the "2 GiB/2 GiB split", which wasn't a bad compromise for systems with about 1 GiB of RAM (and didn't help when there's 2 GiB of RAM or more).

The "4 GiB/4 GiB" was the next silly hack. The problem here is that switching from one virtual address space to another is extremely expensive (due to TLB flushing and TLB misses); and to achieve "4 GiB/4 GiB" this had to happen every time the CPU switched from "user space" to "kernel space" (every kernel API call, every IRQ, etc) and every time the CPU switched back (from "kernel space" back to "user space").

The "high memory mapping" stuff is the third attempt at not fixing the actual cause of the problem.

More time passed. Eventually AMD introduced long mode, and kernel could have up to 128 TiB of space. Linux developer rejoiced - 128 TiB of space sounds insanely huge today (when computers typically have less than 128 GiB of RAM), just like 1 GiB of space sounded insanely huge almost 30 years ago. This solves the problem "forever", right?!

For DMA devices that can only use RAM in the first 4 GiB of the physical address space; for "4 GiB/4 GiB" scheme they can transfer data to/from kernel space easily enough. However almost all of that data has to be transferred to/from user space; which means that you still need bounce buffers and the "4 GiB/4 GiB" scheme doesn't help much at all.

IOMMU could've helped to avoid bounce buffers, but back before long mode existed most computers didn't have IOMMUs. Even now Intel doesn't provide it on all their CPUs (in an attempt to make people pay extra for CPUs that don't have features disabled). This means that for the kernel to work correctly on all computers it had to work correctly only computers without IOMMU.

Note: There's actually very little benefit to "all physical RAM mapped into kernel space" approach, because a kernel has no need to access the vast majority of RAM at all. However this approach seems easy to beginners that don't quite understand paging properly (and don't understand how the "powerful tricks" paging is capable of can be used to benefit the kernel just like they're used to benefit processes). Because of this the "all physical RAM mapped into kernel space" is a recurring design mistake among hobbyist kernel/OS developers. Please understand that Linus was a hobbyist kernel developer back when this mistake was made (and it's very easy to overlook this now when you see modern Linux).


问:
Why are self-hosting compilers considered a rite of passage for new languages?
I've heard in a number of places now that people expect languages to use, or at least have, a self-hosting compiler in order to deserve respect.

I'm curious as to why this is. A compiler seems like a very significant piece of software to write, and I imagine not all languages are well-suited to creating them. Wouldn't it make more sense to spend the effort working in something that will give better results?

答:
Clang is written in C++. It wouldn't be too hard to rewrite the Clang Objective-C compiler in Objective-C, but then it would be quite useless. Any change in the C++ compiler would have to be redone in Objective-C and vice versa. So why?

There's now a Clang Swift compiler. Surely that compiler could be rewritten in Swift. But what purpose would it be? To demonstrate that the language is powerful enough to write a compiler in it? Nobody cares if you can write compilers in Swift. People do care if you can write user interfaces in Swift, and you demonstrably can.

If you have a well tested compiler that can easily be adapted to compile different languages, it's quite pointless to rewrite it into different languages, unless the rewrite in one different language would make it easier to work with the compiler. And if it would make sense to write Clang in Swift, for example, then the Clang C, C++, and Objective-C compilers would all be written in Swift.

There are more important things to do than to prove that you can write a compiler in some programming language.


答:
Composition is when a class offers some functionality by instantiating a (possibly internal) class which already implements this functionality, instead of inheriting from that class.

So, for example, if you have a class which models a ship, and you are now being told that your ship should offer a helipad, it is not natural to derive your ship from a helipad, (duh!) instead, you should have your ship contain a helipad class and expose it via some Ship.getHelipad() method.

In years of old (a decade or so ago) people used to view inheritance as a quick and easy way to aggregate functionality, so there were many examples of the "ship inheriting from helipad" kind, which were, of course, very lame.

But the "Favor Composition over Inheritance" dictum has been carefully worded as to make it clear that this is just a suggestion, not a rule. The author of the dictum was careful enough to refrain from saying something like "thou shalt never use inheritance, only composition". This is basically bringing to the attention of the software engineering community the fact that inheritance has been overused, while in many cases, composition produces more clear, elegant, and maintainable designs than inheritance.

So, essentially, the "Favor Composition over Inheritance" dictum is suggesting that whenever you are faced with the "to inherit or to compose?" question, you should think hard what's the most suitable strategy, and that most chances are that the most suitable strategy will turn out to be composition, not inheritance.

But since this is not a rule, you should also keep in mind that there are many cases where inheritance is more natural. If you use composition there where you should have used inheritance, many evils will befall your code.

To go back to the ship example, if your ship needs to offer an interface for interacting with a FloatingMachine, then it is more natural to derive it from an abstract FloatingMachine class, which might very possibly in turn be derived from another abstract Machine class.

Here is the rule of thumb for the answer to the composition vs. inheritance question:

Does my class have an "is a" relationship to the interface that it
  needs to expose?  If yes, use inheritance.  If not, use composition.

A ship "is a" floating machine, and a floating machine "is a" machine.  So, inheritance is perfectly fine for those.  But a ship is not, of course, a helipad. So it better compose the functionality of the helipad.

Title: Composition over inheritance but


问:
Confusion over organization of services
I have the following services:

UserServices This has various methods: fetch_by_id(), create() etc.

SessionServices Session handling: create(), destroy(), various getters.

AnswerServices An answer is an entity, it represents an answer to a question. These are the services related to it.

Now the problem is I'd like to add a method to get all answers a user has given.

Do I put this into the UserServices or the AnswerServices? I have a number of methods similar to this for other entities, but I'm struggling to figure out where those methods belong.

Any advice would be appreciated.

答:
While you have not tagged this with REST, I think if you look up services in REST you can find quite a bit of consensus on how to handle this.

First of all, you are looking for answers, not users. The fact that you are filtering based on user is secondary to the fact that you need answers.

This method belongs on AnswerServices, with a filter by user.

```<code>AnswerServices.getForUser(UserId id);
</code>```

Title: Confusion over organization of services

Body:
While you have not tagged this with REST, I think if you look up services in REST you can find quite a bit of consensus on how to handle this.

First of all, you are looking for answers, not users. The fact that you are filtering based on user is secondary to the fact that you need answers.

This method belongs on AnswerServices, with a filter by user.

AnswerServices.getForUser(UserId id);


问:
How to optimize/parallelize the following clustering/joining algorithm:
I have relatively-small algorithm that takes up ~60% of the total run-time of my scientific code (57 lines of 3600), so I would like to find a way to optimize what I'm doing and make the code order-independent so that I can apply a cilk_for parallel strcture.

Here's what it does, verbally: I have an std::vector of pointers to custom objects called Segment (vector<Segment*> newSegment). Each Segment contains a std::vector of integers (mesh indices). In this function, I would like to find any Segment that overlaps with any another, with overlap being defined as the member indices overlapping on the number line. If they do overlap, I would like to join them together (insert the A.indices into B.indices) and delete one (delete A).

ex. 1:
A.indices={1,2,3} B.indices={4,5,6} do not overlap; do nothing

ex. 2:
 A.indices={1,2,4} B.indices={3,5,6} do overlap;  A= deleted B.indices={1,2,3,4,5,6}

The overlaps are sparse, but existent.

Here's the current code:

main algorithm:

```<code>//make sure segments don't overlap
for (unsigned i = 0; i < newSegment.size(); ++i) {
    if (newSegment[i]->size() == 0) continue;
    for (unsigned j = i + 1; j < newSegment.size(); ++j) {
        if (newSegment[i]->size() == 0) continue;
        if (newSegment[j]->size() == 0) continue;
        int i1 = newSegment[i]->begin();
        int i2 = static_cast<int>(newSegment[i]->end());
        int j1 = newSegment[j]->begin();
        int j2 = static_cast<int>(newSegment[j]->end());
        int L1 = abs(i1 - i2);
        int L2 = abs(j1 - j2);
        int dist = max(i1,i2,j1,j2) - min(i1,i2,j1,j2);

        //if overlap, fold segments together
        //copy indices from shorter segment to taller segment
        if (dist <= L1 + L2) {
            unsigned more, less;
            if (newSegment[i]->slope == newSegment[j]->slope) {
                if (value_max[i] > value_max[j]) {
                    more = i;
                    less = j;
                } else {
                    more = j;
                    less = i;
                }
            } else if (newSegment[i]->size() == 1) {
                more = j; less = i;
            } else if (newSegment[j]->size() == 1) {
                more = i; less = j;
            } else assert(1 == 0);
              while(!newSegment[less]->indices.empty()) {
                unsigned index = newSegment[less]->indices.back();
                newSegment[less]->indices.pop_back();
                newSegment[more]->indices.push_back(index);
            }
        }
    }

}//end overlap check

//delete empty segments
vector<unsigned> delList;
for (unsigned i = 0; i < newSegment.size(); ++i) {
    if (newSegment[i]->size() == 0) {                            //delete empty
        delList.push_back(i);
        continue;
    }
}
while (delList.size() > 0) {
    unsigned index = delList.back();
    delete newSegment.at(index);
    newSegment.erase(newSegment.begin() + index);
    delList.pop_back();
}
</code>```

Relevant Segment object class definition and member functions:

```<code>class Segment{

    public:
    Segment();
    ~Segment();

    unsigned size();
    int begin();
    unsigned end();
    std::vector<int> indices;
    double slope;
};

int Segment::begin() {
    if (!is_sorted(indices.begin(),indices.end()))      std::sort(indices.begin(),indices.end());
    if (indices.size() == 0) return -1;
    return indices[0];
}

unsigned Segment::end() {
    if (!is_sorted(indices.begin(),indices.end()))    std::sort(indices.begin(),indices.end());
    return indices.back();
}

unsigned Segment::size() {
    unsigned indSize = indices.size();
    if (indSize == 1) {
        if (indices[0] == -1) return 0;
    }
    return indSize;
}
</code>```

Ideas:

Since I don't care about the order of the Segment objects, they could be in an orderless container?

In my algorithm, I find overlap by looking at the first and last indices of each segment. I do an std::is_sorted (and then maybe a std::sort) when I fetch the indices because the list can change when more indices are inserted. Maybe I could put the indices in a std::set rather than std::vector to save the explicit sort-checking/sorting?

I'm pretty sure that by editing the indices as I go, this makes it order-dependent. Perhaps, I could break the code into the following organization using the concept of an undirected graph to make it order-independent:

edge discovery (without modifying indices)

join clusters of connected nodes (Segment objects that overlap) using a graph traversal

delete empty Segment objects

Questions

Are either of the ideas above worthwhile or negligible to performance?

How else can I optimize it?

How (if not the above) can I make the algorithm order-independent?

答:
The is_sorted() function is probably expensive, and so you should avoid it.  Why not sort everything in one go right at the beginning before entering the loops?

The best way to optimize your code is by inventing a new algorithm which avoids the nested loops of N, because that has a complexity of O(N^2) (see "big-Oh notation".)  See Bart van Ingen Schenau's comment below on how to achieve this.


问:
How is spinlock different from polling?
Is spinlock and polling the same thing?

Wikipedia:

a spinlock is a lock which causes a thread trying to acquire it to simply wait in a loop ("spin") while repeatedly checking if the lock is available

This sounds awfully lot like:

```<code>while(!ready);
</code>```

I was taught to avoid polling whenever possible as it was throughly sub-optimal. So, is spinlock a fancy name for bad old polling? How is a spinlock different from polling?

答:
Excessive polling is something you should not do because it wastes system resources. It follows that polling is fine if it doesn't waste system resources.

For example excessive polling will get CPU load to 100% in cases where the actual work would only result in a 2% load.

Polling can be used for other things than while(!ready). For example it means checking regularly if the user pressed a key. A sane implementation will check at most once every 15 milliseconds, so it's one check every ~20 million clock cycles. That kind of polling is great, because it doesn't waste system resources.

A SpinLock is not a special case. If we have a SpinLock and one thread enters the lock, and another has to wait, the SpinLock is the wrong choice if and only if the waiting thread wastes system resources. The waiting thread will waste system resources if and only if it has to wait for a significant amount of time before it can acquire the lock.

Therefore, using a SpinLock to protect a anything that requires a couple thousand clock cycles or more before it unlocks again (e.g. compiling and executing a piece of javascript on the fly) is bad, exactly for the reason you stated. But using a SpinLock to protect something that completes quickly, like accessing a properly implemented hash map is fine because the waiting thread will only spin 2 or 3 times and therefore doesn't waste system resources.

Title: How is spinlock different from polling?


答:
The difference is that a spinlock is (hopefully) only used in situations where it is appropriate, and in these situations it is very efficient.

You use a spinlock if you expect that a resource will only be locked for a very short time - for example if a lock is only used to update a variable. The spinlock polls the lock at maximum speed, but hopefully for less than microseconds. A normal mutex would require on OS call. IF the lock is only held for a tiny amount of time, then the spinlock only uses a tiny amount of CPU time, while the OS call will take longer. But if this expectation is wrong, then the spinlock is very inefficient - it will use 100% CPU time on one CPU, while the normal mutex only takes the time for entering the OS and returning.

Sometimes both are combined; you run the spin lock for a short time and switch to a different strategy if the spinlock doesn't work.

Title: How is spinlock different from polling?

Body:
Is spinlock and polling the same thing?

Wikipedia:

a spinlock is a lock which causes a thread trying to acquire it to simply wait in a loop ("spin") while repeatedly checking if the lock is available

This sounds awfully lot like:

```<code>while(!ready);
</code>```

I was taught to avoid polling whenever possible as it was throughly sub-optimal. So, is spinlock a fancy name for bad old polling? How is a spinlock different from polling?


问:
Are colons ok in a REST API?
I see many REST API examples with URIs like http://<whatever>/widgets or http://<whatever>/widget/123, for a specific thing. For most things I would just stick with the front slash, but when requesting a specific id I'd rather use a colon, like http://<whatever>/widget:123.

According to what I found here about best practices, there wasn't much about using the colon, specifically. It sounds like it's acceptable, but is it/what might be some pitfalls to using them instead of the slash?

Express.js seems fine with them (as far as code is concerned):

app.get('/widgets::id', function(req, res){ <...> });

Update: I just stumbled on this. It's sounding like colons are perfectly fine for this use.

答:
Answering this to close it now that I've finished the REST API and done some testing. Either colon or front slash did fine, however, it became apparent that every live example I could find used front-slash. I think MichaelT brings up the best point. Another library may not work with it. I'm going to extend that and say it's darn likely. Testing software may not be expecting it either since it's not usually done. I also remember somewhere in there thinking that I'd need extra code to handle the colon, but if I left it as a front-slash I could keep my source simpler.

All that being said, I'm going to say that it appears technically fine to use the colon in the way I described, but I'm also going to say it may be more trouble than it's worth down the road simply because it seems to go against a well accepted grain. In the end I went with the front-slash... for now. ;)

Title: Are colons ok in a REST API?


问:
Microeconomical simulation: coordination/planning between self-interested trading agents
In a typical perfect-information strategy game like Chess, an agent can calculate its best move by searching the state tree for the best possible move, while assuming that the opponent will also make the best possible move (i.e. Mini-max).

I would like to use this approach in a "game" modeling economic activity, where the possible "moves" would be to buy or sell for a given price, and the goal, rather than a specific class of states (e.g. Checkmate), would be to maximize some function F of the agent's state (e.g. F(money, widget) = 10*money + widget).

How to handle buy/sell actions that require coordination between both parties, at the very least agreement upon a price?

The cheap way out would be to set the price beforehand, maybe based upon the current supply -- but the idea of this simulation is to examine how prices emerge when freely determined by "perfectly rational" agents.

A great example of what I do not want is the trading algorithm in SugarScape -- paraphrasing from Growing Artificial Societies p101-102:

when a pair of agents interact to trade, they each compute their internal valuations of the goods, then a bargaining process is conducted and a price is agreed to. If this price makes both agents better off, they complete the transaction

The protocol itself is beautiful, but what it cannot capture (as far as I can tell) is the ability for an agent to pay more than it might otherwise for a good, because it knows that it can sell it for even more at a later date -- what appears to be called "strategic thinking" in this pape at Google Books Multi-Agent-Based Simulation III: 4th International Workshop, MABS 2003... to get realistic behavior like that, it seems one would either (1) have to build an outrageously-complex internal valuation system which could at best only cover situations that were planned for at compile-time, or otherwise (2) have some mechanism to search the state tree... which would require some way of planning future trades.

Note: The chess analogy only works as far as the state-space search goes; the simulation isn't intended to be "zero sum", so a literal mini-max search wouldn't be appropriate -- and ideally, it should work with more than two agents.

答:
I just came across this in Game Theory Evolving, H. Gintis, p45:

"In [the neoclassical model], prices move to eliminate excess demand in all markets before any trade actually takes place. Thus, market clearing is not brought about by markets at all, but rather by what later writers have called an "auctioneer" who calls out prices, measures the degree of excess supply and demand in all markets, adjusts prices accordingly, and repeats the process until equilibrium prices are determined. The auctioneer then freezes these equilibrium prices, and agents are allowed to trade freely at these prices. How ironic! Not the buzzing confusion of market competition, but the cool hand of the centralized state apparatus brings about market equilibrium.

"Well, you might reply, we've come a long way since Walrus wrote down his set of equations...Surely someone has provided a plausible decentralized, market-oriented equilibriation mechanism to replace the auctioneer. But in fact, no one has succeeded in producing a plausible dynamic model of market interaction in which prices move toward their market-clearing levels."

Of course, the "auctioneer" model is precisely what is done in SugarScape, and is precisely what I was hoping to avoid.

If true, this is discouraging...but it at least answers why, even ignoring the probably-intractable problem of planning with coordination between agents, I haven't been able to solve this.


问:
Shopping cart in PHP : Sessions or Database?
I am currently developing a simple shopping cart using PHP & HTML.
I've been having troubles with using the SESSIONS in Php like:

Sessions not continuing when I switch pages even though I called the session_start() at the top of  every page.

Is it a better approach to use sessions in keeping track of a clients cart?

My requirements are as follows:

Keep track of users cart when user is switching pages.

Save users cart to database when the user proceeds to check out.

Clear users cart(session) when it has been idle for 30 minutes.

答:
I don't know what your code looks like, here is mine and it works：

```<code>//page1.php
<?php
  session_start();
  $_SESSION['name'] = 'Bee';
?>
<a href="page2.php">next</a>

//page2.php
<?php
  session_start();
  echo 'Hello, '. $_SESSION['name'];
?>
</code>```

And I would rather use sessions, cause I think it's more simple to process the business tasks

Body:
I don't know what your code looks like, here is mine and it works：

//page1.php
<?php
  session_start();
  $_SESSION['name'] = 'Bee';
?>
<a href="page2.php">next</a>

//page2.php
<?php
  session_start();
  echo 'Hello, '. $_SESSION['name'];
?>

And I would rather use sessions, cause I think it's more simple to process the business tasks


答: Either way you will want to use sessions for persisting data through the customer experience since you're ultimately working across a stateless protocol. If I have to implement a database then I go ahead and move the sessions to the database (the client may still need a cookie depending on how you decide to go about that.) As for the cart, as soon as I had to use a database, I tended to move it to the database as well. When I'd use a framework like CodeIgniter then I'd let it make the call unless I had a compelling reason to do otherwise. Since to meet #2 you'll have to design the db to store a cart at checkout, then you may as well store it in the db across the user's experience. I'd take a look at this as well: https://stackoverflow.com/questions/12569568/shopping-cart-persistence-session-or-browser-cookie</p>

Title: Shopping cart in PHP : Sessions or Database?

Body:
I am currently developing a simple shopping cart using PHP & HTML.
I've been having troubles with using the SESSIONS in Php like:

Sessions not continuing when I switch pages even though I called the session_start() at the top of  every page.

Is it a better approach to use sessions in keeping track of a clients cart?

My requirements are as follows:

Keep track of users cart when user is switching pages.

Save users cart to database when the user proceeds to check out.

Clear users cart(session) when it has been idle for 30 minutes.


问:
Design pattern for fetching data in chunks
I am creating Qt application that uses database with huge amount of data to draw some charts. Fetching data from the database is time consuming, so is blocking the application thread or worker thread creating an unpleasant delay.

I have an idea that instead of doing everything at once I can fetch data and draw charts in chunks. How would I design this functionality?

I have no idea how can I design my interfaces. I want also want there to be a place for other database stuff, like caching, in clean way.

答:
Have a look at the Batch Iterator (or Chunky Iterator) Pattern from POSA5 (chapter 6.3). There is also a book if you want more detail.

Assuming you understand the Iterator pattern, the batch iterator does the chunking based on the size of the data structure it uses (the iterator's client is unaware of how chunking occurs).

ContentLicense: CC BY-SA 4.0


问:
Passing a list of choices with different arguments for each choice
I am writing a card game, in which I separated the "Core Logic" of the game from the UI. Communication with the player from the core logic part is done via callbacks which the UI implements and handles (in a separate thread if needed). The core logic library calls these callbacks, the UI handles them, passing an object representing the data that needs to be returned, and returns it.

What I'm currently stuck in is implementing the ability for the Core Logic to give the player a "choice" between possible actions. For example, the core logic might want the player to select from the following 3 game actions:

Draw 5 cards from Deck A

Draw 2 cards from Deck B

Allocate 4 points into bucket 1

The point being that each action can be pretty different from the other. How can I represent this information as an object in C#? I can think of 4 ugly ways...

Just pass a list of strings containing the choice text to the UI and have the UI display them, then the user selects between them:

This is the most simplest (and very tempting) approach, but that would be mixing the UI with the Core Logic, which goes against the main idea behind the design.

Pass an object containing an enum and an object, whose actual type depends on the value of the enum (The enum being something like Draw, Allocate, etc., and the object containing things like the number of cards and which deck for the Draw choice, and the number of points and what bucket for the Allocate command)

But that involves a lot of if statements and typecasting and reflection, among other problems, which is ugly. It also means that I would have to make a separate class for almost every type of choice enum, which is also ugly because each class would just be a list of values.

Pass a dictionary contaning string keys and values, the content of the dictionary depending on the type of choice that one can make.

This will still involve a lot of typecasting and I also lose type safety, but at least I wouldn't have to make a class for every single choice.

Pass an object containing an enum and a dynamic object containing an anonymous type whose fields depend on what the value of the enum is.

This makes handling the stuff in the UI a lot more straightforward and I wouldn't have to make multiple classes, but then I lose type safety and basic checks during compile time..

Is there any sort of pattern that people usually use when facing this problem, or should I just pick my poison and go for it? Perhaps I should rethink my entire approach to the whole thing?

In C, I would probably be using a union of structs for this sort of thing. I'm not sure how I would go about it in C#. Perhaps I'm thinking too much in C?

答:
I'm for 2, but with slight modification. I wouldn't use enum. You can just as fine use GetType(). If you were to add enum to the mix, it would duplicate the type information contained within the object.

On top of that, I would create an Visitor for the Choice class. This will remove the need for reflection and give UI type-safe way to create textual representation for each choice. It will also ensure that when you add new choice type, the compiler will force you to implement all related code, like creating a textual representation for it. And I don't see how having many classes is bad. It is actually good thing, as long as those classes are cohesive.

And I don't think those objects will contain only data. They can very well be objects doing the actual action. Just put Execute() method on them, and they can be the ones drawing the cards from decks or allocating points to buckets.


问:
How to let users share custom code content under the mobile app store restrictions?
Situation

Mobile application store owners are known to explicitly forbid some application extensibility scenarios.

Apple:

2.7 Apps that download code in any way or form will be rejected

2.8 Apps that install or launch other executable code will be rejected

https://developer.apple.com/app-store/review/guidelines/#functionality

Microsoft:

3.9 All app logic must originate from, and reside in, your app package
  Your app must not attempt to change or extend the packaged content through any form of dynamic inclusion of code that changes how the application behaves with regard to Store certification requirements. Your app should not, for example, download a remote script and subsequently execute that script in the local context of the app package.

http://msdn.microsoft.com/en-us/library/windows/apps/hh694083.aspx

Google (not sure if it is as restrictive):

An app downloaded from Google Play may not modify, replace or update its own APK binary code using any method other than Google Play's update mechanism.

https://play.google.com/about/developer-content-policy.html

Problem

Some applications such as Minecraft and Codea are meant to let users produce content (not just consume it). Codea projects are obviously an executable code and Minecraft is known to be Turing-complete.

Now they don't have any built-in online community gallery capabilities, but it would be easy to imagine them added in future. This is something users would definitely appreciate:

https://gaming.stackexchange.com/questions/148549/how-to-transfer-minecraft-pocket-edition-world-between-ipads

http://codea.io/talk/discussion/527/best-way-to-share-code-straight-from-ipad/p1

Microsoft has already done this with their Project Spark (PC/Xbox) and Media Molecule/Sony have this in their Little Big Planet (PS, also Turing-complete).

Questions

1) Obviously specific store owners make their own, often random and unfair decisions, but generally where is the line between 'code' and user-created content?

2) Is it a conscious decision to favour security over experience or a simple case of  short-sightedness?

3) What can be done to work around today? I am aware of the browser technology but am only interested in application development.

答:
For iOS apps, in their Developer agreement, Apple states an exception to rule 2.7 for interpreting downloaded code, if that code is Javascript, executed using public UIWebView or WebKit APIs (which can be a sub-component of a native iOS app), and which does not change the basic functionality of the app as per the app's description or as submitted for review.

The old-fashioned way (from Apple II days and earlier) to distribute code was to publish that code in print, and have the users (re)type the source code into their own computer (or a terminal).  Apple seems to allow programming language interpreters in the App store (many: BASIC, Lisp, Python, etc.) that allow running code that the users have typed in themselves.  So this old-fashioned way to share code will still work.

Just display the source code, or allow it to be printed out, and let the user manually type it into their mobile app.


问:
How to deal with constructors in large data classes
Several times now I have come across the situations where you have some kind of settings class that simply contains a mass of data. Often these classes are simply not valid without at least most of the data.

However a constructor like this:

public dataclass (int v1, int v2, int v3, int v4,
      string v5, double v6, string v7, int v8,
      Internalclass v9, string v10,
      double v11, double v12, int v13, int v14)
{
   this.v1=v1;
   this.v2=v2;
   ...
   this.v14=v14;
}
```

feels like it's not good code practice. Now I could try to group some of these variables together but often these variables only make sense in the context of the data class. So what is a good way to design such a data class?

答:
You are looking for the builder pattern.

Essentially it's an object with setters where you can set the parameters for construction and a createResult() method that will create the object you are looking for (or fail if it wouldn't be valid).

You can use it to contain the defaults for the properties and group some of them into a single setter.

```<code>DataClassBuilder builder = new DataClassBuilder();

builder.setV1(v1);
builder.setV3(v3);
builder.setV4(v4);
builder.setV5AndV6(v5, v6);
//...

DataClass data = builder.createResult();
</code>```

Body:
You are looking for the builder pattern.

Essentially it's an object with setters where you can set the parameters for construction and a createResult() method that will create the object you are looking for (or fail if it wouldn't be valid).

You can use it to contain the defaults for the properties and group some of them into a single setter.

DataClassBuilder builder = new DataClassBuilder();

builder.setV1(v1);
builder.setV3(v3);
builder.setV4(v4);
builder.setV5AndV6(v5, v6);
//...

DataClass data = builder.createResult();


问:
How can regular expression be ambiguous?
I am recoqnizing this pattern

```<code><.*>
</code>```

From string

```<code>Hi Baby
</code>```

Now, there are several match

href="hello world"> is a match

```<code>Hi Baby
</code>```

is also a match.

However, that's very confusing. I thought regular expression are solved with determined finite automata.

So I would imagine that the definite finite automata would go to each letter. one by one. However, it would somehow branch. It would think that the first > is art of the closing > in the pattern. It can also mean part of the . pattern.

So how does it decides?

In vb.net, it seems that the pattern that's recognized is the second one. That is why I have to replace the pattern with

```<code><[^>]*>
</code>```

if I want the pattern to match the first (say I want to eliminate all html tags from a string)

And why is that? What does vb actually do to select the second string as those that match the pattern?

I've heard that vb is "greedy". It matches the longest string that match the pattern instead of the first working pattern. So uhmm...is this inherently ambiguous or is there a way we can how this is actually implemented?

答:
There are 2 ways regex are matched against a string: Check if the entire string matches the pattern or find the first match of the pattern.

The first is often used for input validation.

The second would be used in parsing large portions of text to isolate interesting parts for further parsing. This will return the first match it can find.

Body:
I am recoqnizing this pattern

```<code><.*>
</code>```

From string

```<code><a href="hello world">Hi Baby</a>
</code>```

Now, there are several match

href="hello world"> is a match

```<code><a href="hello world">Hi Baby</a>
</code>```

is also a match.

However, that's very confusing. I thought regular expression are solved with determined finite automata.

So I would imagine that the definite finite automata would go to each letter. one by one. However, it would somehow branch. It would think that the first > is art of the closing > in the pattern. It can also mean part of the . pattern.

So how does it decides?

In vb.net, it seems that the pattern that's recognized is the second one. That is why I have to replace the pattern with

```<code><[^>]*>
</code>```

if I want the pattern to match the first (say I want to eliminate all html tags from a string)

And why is that? What does vb actually do to select the second string as those that match the pattern?

I've heard that vb is "greedy". It matches the longest string that match the pattern instead of the first working pattern. So uhmm...is this inherently ambiguous or is there a way we can how this is actually implemented?


问:
What would be an actual DFA/actual regex engine solve this simple regex pattern?
```<code>.*foo
</code>```

Academically, that is a regular expression. A string aaaaaaaaaaaaafoo will match that. Also string aaaaaaaaaaaaafooaaaaafoo will match that too.

I would imagine that an NFA that solve that will have 4 states. State 0 will have an arrow going back to itself. Then an arrow f going to state 1, o to state 2, and another o to state 3.

The NFA would keep looping through to state 0 before it magically uses it's psychic power to decide aha, now I am going to start looking for foo. However, I have a hard time understanding psychic computer.

What would be a DFA for that? I would imagine that the number of state would probably be 6. So a the DFA may contain a boolean variable whether you are still looping or still going. I still can't quite figure that out. So can anyone help me construct a DFA for that?

Also .*foo, and it's variant .*?foo and .*+foo are actual regular expression used nowadays in non academic sense.

How would an actual regex engine implement those 3 expressions? How close are those actual implementation to actual DFA? I mean I know real DFA don't look back. I also know that real regex engine do look back and forth.

For simplicity sake let's presume that the DFA can contains variable instead of just states. I think the states of DFA are just variable in memories anyway.

答:
With a simple conversion you can change a NFA to a DFA by taking each state of the DFA as a subset of the NFA states. The transition function would take each State of the input and get the set of next states and make the union of all of them. This models the computer being in multiple states at the same time.

As a result Your 4 state NFA would result in a 4 state DFA.

A starting state {s0}. It has a transition with f to the next state {s0, s1} and any other loops back to itself.

a state {s0, s1}. It has a transition with o to the next state {s0,s2}, a transition with f to itself and any other will go back to {s0}.

a state {s0,s2} It has a transition with o to the next state {s0, s3} a transition with f to {s0, s1} and any other will go back to {s0}.

and a finishing state {s0,s3} with  a transition with f to {s0, s1} and all others back to {s0}

More state combinations (a total of 2^4) are possible but none of the others are reachable from the starting state.

This is the method that a few regex engines use to avoid backtracking in the NFA constructed from the regex string though it won't be able to model capturing groups and backrefs. It is essentially a breath-first search into the NFA.

Other regex engines (most actually) basically do a depth-first search in the constructed NFA. Each time multiple options are available it will pick one and try that, if it fails then it backtrack to that decision and tries the other path. Which path it picks first is decided by the greediness modifier.


答:
Are all those parameters actually required? By "data class" do you mean a Domain Model, View Model or Data Transfer Object? Requiring such a large list of arguments for a simple "data class" starts to feel like a Code Smell telling you either:

This class is doing too much, and much of its work should be delegated to other classes that get passed in as a dependency

Not everything is needed. Optional values in data classes should not be passed in via the constructor

I think the question here is not "How do I handle a large list of constructor arguments" and instead is "How can I refactor my object model so I don't need two dozen parameters to a constructor."

The Builder Pattern will "fix" your problem, but I think it is putting a bandage on a larger problem that requires a different solution.

Title: How to deal with constructors in large data classes

Body:
Several times now I have come across the situations where you have some kind of settings class that simply contains a mass of data. Often these classes are simply not valid without at least most of the data.

However a constructor like this:

public dataclass (int v1, int v2, int v3, int v4,
      string v5, double v6, string v7, int v8,
      Internalclass v9, string v10,
      double v11, double v12, int v13, int v14)
{
   this.v1=v1;
   this.v2=v2;
   ...
   this.v14=v14;
}
```

feels like it's not good code practice. Now I could try to group some of these variables together but often these variables only make sense in the context of the data class. So what is a good way to design such a data class?


问:
When and how long should I cache data read from an external API?
I am working with an API but I am unsure what the best way to store the API return values.

Specifically, I am working with the RIOT API just as a small hobby project. I would like to store this information somewhere so that I can not only compare users with each other but also present user statistics without having to call the API each time.

It just doesn't seem right for me to store all this information when it is already there but again, I don't really see the need to constantly call the API for each game that I want to retrieve information from each time the user wants information.

答:
Remember the principles of caching. You cache

Data that is close spatially

Data that is close in time

The first is called spatial locality and the second is called temporal locality. A simple example of the first type of data is: If a user asks for a variable part of an object, is it good to fetch the entire object because it is likely that the user also is going to ask for data that is "close" (e.g. in the same object).

A simple example of the second type of caching is that data that is used often, is good to cache. If the user viewed variable A, then he is likely to view variable A again.

So cache your data using memcache, redis, a local hash table or similar, and use the principles of caching to choose which data you use.

You are also fortunate that you can actually measure the hit-rate of you caching once you have it in place. The hit-rate is simply number of hits divided by number of calls.

If you want to study the details of cache memories, a good book is Computer Organization & Design by Patterson /Hennesy.


问:
is this use case well structure?
I'm trying to create a use case that make sense, and i am a little bit confused with the alternate path i think i am doing it in the wrong way, the system should tell the admin when a shared playlist was opened from the receptor, i appreciate your help.</p>

```<code>notify when playlist is opened

1-the user share a playlist
2-the receiver get the shared playlist email
3-the receiver click the shared playlist link URL
4-the system get the request email link
    4.1 the system get an invalid link
        4.1.2 the link was already used
5-the system send an email to the admin to notify them
    5.1 the system show an invalid message
</code>```

答: You can create a UML sequence diagram to make your case more clear. If you make a sequence diagram then I believe that will help you. </p>

Title: is this use case well structure?


问:
Maintain hundreds of customized branches over master branch
Currently we have one master branch for our PHP application in a shared repository. We have more than 500 clients who are subscribers of our software, most of whom have some customization for different purposes, each in a separate branch. The customization could be a different text field name, a totally new feature or module, or new tables/columns in the database.

The challenge we face is that as we maintain these hundreds of customized branches and distribute to clients, from time to time we provide new feature and update our master branch, and we would like to push master branch changes to the custom branches in order to update them to the latest version.

Unfortunately this often results in many conflicts in the custom code, and we spend many hours going through every single branch to solve all the conflicts. This is very inefficient, and we've found that mistakes are not uncommon when solving these conflicts.

I am looking for a more efficient way to keep our client release branches up to date with the master branch that will result in less effort during merging.

答:
You are completely abusing branches! You should have the customisation powered by flexibility in your application, not flexibility in your version control (which, as you have discovered, is not intended/designed for this sort of use).

For example, make textfield labels come from a text file, not be hardcoded into your application (this is how internationalisation works). If some customers have different features, make your application modular, with strict internal boundaries governed by stringent and stable APIs, so that features can be plugged-in as needed.

The core infrastructure, and any shared features, then only need be stored, maintained and tested once.

You should have done this from the start. If you already have five hundred product variants (!), fixing this is going to be a huge job … but no more so than ongoing maintenance.


答:
This is one of the worst anti-patterns you can hit with any VCS.

The correct approach here is to turn the custom code into something driven by configuration, and then each customer can have their own configuration, either hardcoded in a config file, or in a database or some other location. You can enable or disable entire features, customize how responses look, and so on.

This allows you to keep one master branch with your production code.

Title: Maintain hundreds of customized branches over master branch

Body:
Currently we have one master branch for our PHP application in a shared repository. We have more than 500 clients who are subscribers of our software, most of whom have some customization for different purposes, each in a separate branch. The customization could be a different text field name, a totally new feature or module, or new tables/columns in the database.

The challenge we face is that as we maintain these hundreds of customized branches and distribute to clients, from time to time we provide new feature and update our master branch, and we would like to push master branch changes to the custom branches in order to update them to the latest version.

Unfortunately this often results in many conflicts in the custom code, and we spend many hours going through every single branch to solve all the conflicts. This is very inefficient, and we've found that mistakes are not uncommon when solving these conflicts.

I am looking for a more efficient way to keep our client release branches up to date with the master branch that will result in less effort during merging.


问:
Are there any compilers that attempt to fix syntax errors on their own?
I heard a while back that there used to be a compiler that attempted to fix syntax errors by analyzing context and inferring what was intended.

Does such a compiler really exist? Obviously it has little practical value, but would be very interesting to play with and learn from.

答:
Sounds really dangerous.  If a compiler tries to infer your intent, infers it wrong, fixes the code, and then doesn't tell you (or tells you in some warning that you, like everyone, ignore), then you're about to run code that may seriously do some damage.

A compiler like this is probably something that has very intentionally NOT been created.


问:
Object oriented programming - subobject vs globals (use case)
I am developing a nethack-like game, and I would like an advice  how to approach the design part. For now I got class like Location, Npc, Item etc. But I've got a problem how to easily access parts of location.

Lets say I have an object Door inside (not inherited). If player is inside location it is easy to check whether door are opened. But on the other hand (I got this solution with my previous non-object implementation) I had a script that at 0600 opened all shops. But now I need to iterate thru all locations, check whether are doors inside, and open them if location is a shop. Is it really optimized way to do it?

I could also do a globals (like singleton) with door states and quickly run through those - but it would be hardly an OOP.

What are the possibilities here?

答:
The usual way to do this in an object-oriented way is to put a reference to each of the shops in a Shop Collection, so that you only have to iterate through those specifically.

```<code>var shops = new list<Location>();

// When you add a location
if (location.Type = LocationTypes.Shop)
    shops.Add(location);

// Open the shop doors
foreach(var shop in shops)
    shop.OpenDoors();
</code>```

But you should time your code first to see if you even need this optimization.  You might find out that iterating through all locations is already fast enough.

Body:
The usual way to do this in an object-oriented way is to put a reference to each of the shops in a Shop Collection, so that you only have to iterate through those specifically.

var shops = new list<Location>();

// When you add a location
if (location.Type = LocationTypes.Shop)
    shops.Add(location);

// Open the shop doors
foreach(var shop in shops)
    shop.OpenDoors();

But you should time your code first to see if you even need this optimization.  You might find out that iterating through all locations is already fast enough.


答:
In the future, ask the Joel test questions in your interview. You'd be more likely not to walk into a trainwreck.

This is an, ah, how shall we say... really, really bad problem to have. The "interest rate" on this technical debt is going to be very, very high. It might not be recoverable...

How integrated with the "core" are these custom changes? Can you make them their own library and have a single "core" and each specific customer having its own "add-on?"

Or are these all very minor configurations?

I think the solution is a combination of:

Changing all the hardcoded changes into configuration based items. In this case everyone has the same core application, but users (or you) turn on/off functionality, set namings, etc. as needed

Moving "client specific" functionality/modules to separate projects, so instead of having one "project" you have one "core project" with modules you can add/remove easily. Alternatively you can make these configuration options too.

Neither will be trivial as if you ended up here with 500+ clients, you likely made no real distinction in this. I expect your changes in separating this is going to be a very time consuming task.

I also suspect you are going to have significant problems in easily separating out and categorizing all your client-specific code.

If most of your changes are specifically wording differences, I suggest reading questions like this about language localization. Whether you are doing multiple languages entirely or just a subset, the solution is the same. This is specifically PHP and localization.


答:
If your parameters are optional, the Builder pattern could be useful indeed! Though, the builder pattern can be a mess sometimes. If you use the Builder at many places, when you'll add a new parameter you'll need to make sure you don't forget to add it in every places because you won't have compile-time check.

If the parameters are not optional, you would need to verify everything has been set at the moment to call the Build method!

I'd like to propose another solution, the Introduce Parameter Object refactor.

The idea is to group your parameters (if possible) by some logic. Then, you'd have less parameters in this constructor and maybe 3-4 more "parameter classes". You could argue this moves the problem elsewhere, but it gives you more flexibility on how to create these parameter objects.

Maybe one of the "parameter object" can be created via an hardcoded factory, the other could be retrieved from a configuration file etc.

This way you create a better separation of concerns!


答:
The purpose of branches is to explore one possible avenue of development without risking to break stability of the main branch. They should eventually be merged back at a suitable time, or be discarded if they lead to a dead end. What you have are not so much branches, but much rather 500 forks of the same project and trying to apply the vital changesets to all of them is a sisyphean task.

What you should do instead is have your core code live in its own repository, with the necessary entry points to modify behavior through configuration and to inject behavior as allowed by inverted dependencies.

The different setups you have for clients can then either merely distinguish each other by some externally configured state (e.g. a database) or if necessary live as separate repositories, which add the core as a submodule.


问:
Conflict resolution for two-way sync offline sync
How do you manage two-way synchronization between a 'main' database server and many 'secondary' servers, in particular conflict resolution, assuming a connection is not always available(offline syncing)?

For example, I have an Android application which is use data from local database(sqlite). And user can deal with data even it is online or offline. In meantime when if it is online, then local database(sqlite) should sync with real database(mysql) in the server and resolve conflicts as a background service.

Is there any solution already implemented for this kind of scenario ?

I heard about Couchdb which is a NoSQL solution can be a solution for this. If it is so Can I use it with SQL ?

答: Basically you would need to take the commit logs from both databases and compare them, looking for instances of updates to the same table and row (and possibly column) in both databases. Any commit records for rows not in conflict can be sent to the appropriate database (i.e., a commit record from the master can be transformed into an appropriate CRUD statement and issued to the client), all rows in conflict will have to have their commits ordered temporally and the last commit issued would "win". If the commit logs are not retained, you'll have to keep them manually (probably by structuring your updates using the command pattern). This is a non-trivial exercise.</p>


答: It sounds like you need a thread pool of workers to which you can pass selection criteria (for the database call) and a reference to the chart area. Then you can have each worker query the database for a subset of the data, and have it plot its portion of the graph. You may have to mediate access to the graph area (I have no idea how QT is structured), but while the workers are busy the main thread can continue to serve user requests. </p>

Title: Design pattern for fetching data in chunks

Body:
I am creating Qt application that uses database with huge amount of data to draw some charts. Fetching data from the database is time consuming, so is blocking the application thread or worker thread creating an unpleasant delay.

I have an idea that instead of doing everything at once I can fetch data and draw charts in chunks. How would I design this functionality?

I have no idea how can I design my interfaces. I want also want there to be a place for other database stuff, like caching, in clean way.


答:
It sounds to me that if a compiler could fix incorrect syntax, then that syntax should be documented in the language.

The reason for syntax errors is because a parser could not create the abstract syntax tree out of the program.  This happens when a token is out of place.  In order to guess where that token should be, if it should be removed, or if some other token should be added to fix the error you would need some sort of computer that can guess the intent of a programmer.  How could a machine guess that:

```<code>int x = 5 6;
</code>```

Was supposed to be:

```<code>int x = 5 + 6;
</code>```

It could just as easily be any of the following: 56, 5 - 6, 5 & 6.  There is no way for a compiler to know.

That technology doesn't exist yet.

Title: Are there any compilers that attempt to fix syntax errors on their own?

Body:
I heard a while back that there used to be a compiler that attempted to fix syntax errors by analyzing context and inferring what was intended.

Does such a compiler really exist? Obviously it has little practical value, but would be very interesting to play with and learn from.

Body:
It sounds to me that if a compiler could fix incorrect syntax, then that syntax should be documented in the language.

The reason for syntax errors is because a parser could not create the abstract syntax tree out of the program.  This happens when a token is out of place.  In order to guess where that token should be, if it should be removed, or if some other token should be added to fix the error you would need some sort of computer that can guess the intent of a programmer.  How could a machine guess that:

int x = 5 6;

Was supposed to be:

int x = 5 + 6;

It could just as easily be any of the following: 56, 5 - 6, 5 & 6.  There is no way for a compiler to know.

That technology doesn't exist yet.


答:
Having 500 clients is a nice problem, if you had spent the time up front to avoid this problem with branches, you may never have been able to remain trading for long enough to get any clients.

Firstly, I hope you charge your clients enough to cover ALL the costs of maintaining their custom versions.   I am assuming that clients expect to get new versions without having to pay for their customizations to be done again.
I would start by finding all the files that are the same in 95% of your branches. That 95% is the stable part of your application.

Then, find all the files that only have a few lines different between the branches – try to introduce a configuration system such that these differences can be removed.  So, for example, rather than having 100s of files with textfield labels that are different, you have 1 config file that can override any text label.   (This does not have to be done in one go, just make a text field label configurable the first time a client wants to change it.)

Then move onto the harder issues using the Strategy pattern, dependency injection etc.

Consider storing json in the database rather than adding columns for client’s own fields – this may work for you if you don’t need to search these fields with SQL.

Everytime you check a file into a branch, you MUST diff it with main and justify every single change, including white space. Lots of changes will not be needed and can be removed before the checkin. This may just be down to one developer having different settings in their editor for how code is formatted.

You are aiming to first go from 500 branches with lots of files that are different, to most branches only having a few files that are different. While still making enough money to live.

You may still have 500 branches in many years time, but if they are a lot easier to manage, then you have won.

Based on the comment by br3w5:

You could take each class that is different between clients

Make a “xxx_baseclass” that define all the methods that are called in the class from outside of it

Rename the class so that xxx is called xxx_clientName  (as sub class of xxx_baseclass)

Use dependency injection  so that the correct version of the class is used for each client

And now for the clever insight br3w5 came up with! Use a static code analysis tool to find the now duplicated code, and move it into the base class etc

Only do the above after you have got the easy grain, and trail it with a few classes first.


答:
I can relate to this. I have taken many projects on. In fact, 90% of our development work is fixing such things.
Not everyone is perfect, so I suggest you use version control in the correct way and where you are, if possible you can do the following.

From now on, when a customer asks for an update, move them into new forked repository.

If you want to merge them to master do it as the first thing and resolve conflicts.

Then manage their issues and sprints with their repository and keep those in master that you want to launch in master. This might put more strain on release cycles, but that will save you over time.

Maintain a master branch of the main repository for new customers and the main repository should only have those branches you are working on for future stuff. Legacy branches can then be deleted once they are migrated to customer repositories.

I have personally imported a repository from GitHub with 40 branches to Bitbucket and created 40 repositories. It took only four hours. This was WordPress theme variations so push and pull was quick.

There are many reasons for "not doing it right first time", and I think those who accepts them quickly and move on to "do it right this time" would always be successful.


答:
The first thing that comes to my mind is Javascript's automatic semi-colon insertion. A horrible, horrible feature that should never have made its way into the language.

That's not to say that it couldn't have done a better job. If it looked ahead at the following the line, then it might be able to make a better guess as to the programmer's intention, but at the end of the day, if there's multiple valid ways the syntax could have gone, then there's really no substitute for the programmer being explicit.


问:
UML class diagram symbol for a top-level function
In UML class diagram notation, how do I denote a top-level function that doesn't have any encapsulating class or other structure?

e.g. I have a function used for retrieving data from a URL. Since it is a function (in the math sense) of only its parameters and a shared global state, the function is not put in a class but left as a top-level method.

Now though, I need to create a UML diagram for a program using it, and I can't find anything about how to denote a top-level function. How do I do this?

答:
Create a stereotype that you can attach to the "class" classifier, e.g., <<global function>>. You can then create a single stereotyped "class" for each global function containing one fully specified static operation.

The stereotype I propose tells the person reading the diagram to interpret the classifier as a function. The rectangle (with the stereotype) on a given diagram would represent a single function, while its dependencies represent the dependencies of that single function.


问:
How should you look at unit testing protected members used by the abstract base class?
I have the following class(es) that I want to write unit tests for:

```<code>public abstract class BaseClass
{
    public bool IsFooBar(Order order)
    {
        return order.IsNew && IsFooBarOverride(order);
    }

    protected abstract IsFooBarOverride(Order order);
}

public class SimpleDerivedClass : BaseClass
{
    protected override bool IsFooBarOverride(Order order)
    {
        // Return if the order is with a credit card.
    }
}

public class ComplexDerivedClass : BaseClass
{
    protected override bool IsFooBarOverride(Order order)
    {
        // Return a very complex statement
        // If the order is for a new customer
        // in New York, with an order amount of $500 or more
        // and has at opted for email subscriptions.
    }
}
</code>```

The problem I am running into is that my derived classes contain the majority of my logic for determining the return value of IsFooBar(Order order). I want to test this logic to ensure it is proper. I was under the impression that you should only test the public API of a class, which is IsFooBar(Order order), which belongs to the base class.

Should I redesign my class structure by marking IsFooBar virtual and hope that other developers on the team use base.IsFooBar(order) as part of their return statement?

Should I leave my class structure in tact and test variations of the IsFooBar(Order order) method per derived class as a way to test the logic in the protected class?

答:
Considering your doubts, I would argue that the choice of solving this with inheritance might be the wrong approach.

Perhaps composition would be more appropriate in this case? It is hard to tell without knowing the requirements.

To answer the question, as long as the template method is the way to go, test per derived class.


问:
How do I represent a schema diagram of my MongoDB database?
I have a MongoDB database that I want to properly document its schema design. I know that MongoDB is a NoSQL database and is schemaless by nature, but I do enforce a schema through my application and I want to represent it in a better way than a print of a findOne() result.

I see many people using ER or UML, but I don't feel that's conceptually right to represent my NoSQL database as a relational DB or, at least, it looks weird.

Example using UML: MongoDB: How to represent a schema diagram in a thesis?

I thought that people would be using different models. I've searched and the far that I've saw was MongoVUE that offers a nice Tree view to understand the schema, but its not printer-friendly.

Is there something else that I'm missing for the NoSQL world? Or should I rest and stick with traditional UML?

答: I would model the schema as a UML class diagram. Class diagrams are not specifically aimed at relational databases, but rather at object oriented environments. In my opinion, MongoDB conceptually matches UML better than a relational database. The question you refer to provides more information about how to use UML for MongoDB.</p>