答:
There are several answers here, and all of them talk about what async methods do, but none of them answer the question, which is why async is needed as a keyword that goes in the function declaration.

It's not "to direct the compiler to transform the function in a special way"; await alone could do that.  Why?  Because C# already has another mechanism where the presence of a special keyword in the method body causes the compiler to perform extreme (and very similar to async/await) transformations on the method body: yield.

Except that yield isn't its own keyword in C#, and understanding why will explain async as well.  Unlike in most languages that support this mechanism, in C# you can't say yield value; You have to say yield return value; instead.  Why?  Because it was added in to the language after C# already existed, and it was quite reasonable to assume that someone, somewhere, might have used yield as the name of a variable.  But because there was no pre-existing scenario in which <variable name> return was syntactically correct, yield return got added to the language to make it possible to introduce generators while maintaining 100% backwards compatibility with existing code.

And this is why async was added as a function modifier: to avoid breaking existing code that used await as a variable name.  Since no async methods already existed, no old code is invalidated, and in new code, the compiler can use the presence of the async tag to know that await should be treated as a keyword and not an identifier.

Title: Why do we need the async keyword?

Body: I just started playing around with async/await in .Net 4.5. One thing I'm initially curious about, why is the async keyword necessary? The explanation I read was that it is a marker so the compiler knows a method awaits something. But it seems like the compiler should be able to figure this out without a keyword. So what else does it do?</p>


问:
Why (most?) retro computer emulators are single threaded?
All real computer hardware works in parallel. So why most emulators of retro systems (like NES) are single threaded (or I'm not right?)? You have main loop and you counting CPU cycles and after certain cycles activating other hardware like PPU.

Same thing applies to IBM PC and (MDA/CGA/VGA) emulation?

答:
I've actually asked myself this question.

The accepted answer makes some reasonable statements, especially regarding the systems and acronyms mentioned by the OP. For sure, true multithreaded emulators don't give us much at the emulation level.

However, I've been looking at getting truer emulation of a KIM-1 board, with more complete RRIOT emulation, with usable maskable and non-maskable interrupts for the purpose of timers (i.e., timers that can be scaled, restarted, and accessed by the programmer).

One approach there is an implementation of multitasking in the emulator (which doesn't have to actually be truly parallel) so we have independent timers (or a single timer) that can be scaled, hooked up to "interrupts", etc. That is, user code could run that checks memory locations for timer info that is supposed to be ticking over even as our own program statements are being stepped through. More importantly, it might be a good way to emulate interrupts that a program subscribes to, allowing our own interrupt handlers to work as expected.

The intention is to emulate more closely the machinery of the CPU and support chips so that the registers, programmable interrupts, and read-only memory locations that change as the hardware ticks behave more as expected. But doing it without coding it at the tricky per opcode load-and-execute level.

I think it might be easier to emulate the target CPU speed a little closer, too, or at least get timer resolution pretty accurate. That is, what happens between the various timer location updates and interrupts is not that important as long as those ticks and interrupts are correct when user and ROM routines access them.

Yeah, still a work in progress.

ContentLicense: CC BY-SA 3.0


问:
Elasticsearch and relational database combination
Imagine I have a marketplace application - where users can search for products (we concentrate on clothes). Every product has an ID, name (text), description (text), price (numeric), size (numeric), brand, condition and so on.

The users can search for clothes. Right now the data is stored in a relational database (PostgreSQL). There is an Elasticsearch instance running which is used for searching in the name and description fields (as they are text fields).

Problem:
I want to give the user the option to narrow down the search by using all parameters - so that the user can search for a specific size, condition AND description for example.

There are two approaches that I see:

Implementing a mixture of Elasticsearch and database search. This would mean that I would filter the data at one place and continue with the filtered data at the other place to filter it again.

Advantage: Using Elasticsearch for full-text search and using the database for specific 'column-search' as this is what both are good at. Like getting the best of both worlds.

Disadvantage: How to determine where to start the search? The idea is of course to start the search at the place where I can eliminate most of the data so that the second search is performed over a smaller data set.

Note also that the Elasticsearch instance will be definitely on a different machine that the PostgreSQL so we are talking about network overhead and higher response times.

Going only with Elasticsearch. I am totally aware of the PostgreSQL ability to perform text-search but this is nowhere near powerful as Elasticsearch and also there is no guarantee that PostgreSQL will be always the DB of choice.

Advantage: All search happens in one place. No intermediate results or similar.

Disadvantage: Is Elasticsearch as strong as a the relational databases in terms of when filtering data based on a predicate. Please note that we are talking about fields like size and price - numeric fields where text-search is not in its prime but a simple WHERE clause is super fast.

Are there more advantages or disadvantages of both approaches that I am missing. Something crucial that would speak for or against the one or the other way?

答:
ElasticSearch is effective enough for the searches you are looking for; ElasticSearch had sustained the benchmarks I've done(100 user/sec about 3 days); but from persistence perspective you need to hold one step back, if one of the node went down then it need Hugh time to recover and again it depend on the cluster configuration(take keen decision).
It's able to hold good enough size payloads(2mb) , indexing 40+ fields, storing 40Million+ orders on 9 node cluster(nodes=27, replication factor=3)


问:
Multiple variable assignment vs HashTable lookup
I'm writing a CUIT ( Coded UI Test ) and I'm replacing variables with data fetched from a csv.

Example:

```<code>this.UIMap.IDBParams.UITboxFsEditText = TestContext.DataRow["num_fs"].ToString();
this.UIMap.IDBParams.UIArmGlCheckBoxChecked = Convert.ToBoolean(TestContext.DataRow["arm_gl"]);
</code>```

But I'm having to define over 100 variables.

My concept of how I would implement a hash table here is :

On start-up fetch all the variables on each test method and make a new HashMap with the variables as key. Ex:

```<code>{
 this.UIMap.IDBParams.UITboxFsEditText :  ,
 this.UIMap.IDBParams.UIArmGlCheckBoxChecked : ,
} //indented just for visual clarity
</code>```

Fetch the csv columns and assign them as value for each key. Ex:

```<code>{
 this.UIMap.IDBParams.UITboxFsEditText :
    TestContext.DataRow["num_fs"].ToString() ,
 this.UIMap.IDBParams.UIArmGlCheckBoxChecked :
    Convert.ToBoolean(TestContext.DataRow["arm_gl"]) ,
} //indented just for visual clarity
</code>```

Is this the best approach ?

Does reading a HashTable index ( ex: MyTable['UiTboxFsEditText'] ) acts the same as the line this.UIMap.IDBParams.UITboxFsEditText = TestContext.DataRow["num_fs"].ToString(); ?

答:
Seems to me you're doing the same amount of work either way. In one place you're filling a HashMap, and the other you're doing variable assignment. Maybe one is fewer lines of code? Almost certainly the version using the HashMap will be somewhat more complicated (due to the indirection) than the version using simple assignment. There will be almost no difference in runtime performance.

You have to ask yourself if it's really worth spending the effort to design the HashMap version when there's little to gain. Especially in test code, which you should strive to make simple.


问:
Would it be possible to transfer an HTML file using a protocol different from HTTP?
I am reading about internet and stuff and just learned that web pages are really just HTML files.

Also, I have learned that HTTP is just a transfer protocol.

So my question is, would it be possible to view a web page using another protocol, say FTP, since a webpage is really just a file with instructions in it telling the browser how to render the page?

If it would be possible, could you describe how it would be possible?

Example: Let's say that a server hosts a webpage on port 20 (that is: FTP port). If I connected to the server with: ftp://<hostname>/<path_to_webpage>, would I be able to display it as expected?

Yet more interestingly, in the same scenario, if I connected to the server with: http://<hostname>:20/<path_to_webpage>, would it work in this scenario as well?

答:
Earlier web browsers supported multiple protocols, including ftp and gopher for example.  With the advent of AJAX, the web has been much more closely linked to http(s) as the required protocol.

HTML itself however, has no requirement regarding the transport protocol.


答:
I think the main reason that emulators tend to be single-threaded is that you are managing a very complex state engine; emulators may be the most complex state engines in general.

When you add in multi-threading, you then have to add in locking mechanisms to keep that state engine in proper synchronization.  As emulators tend to be emulating older hardware anyway, the performance gain is seldom enough to justify the huge increase in complexity.

Title: Why (most?) retro computer emulators are single threaded?

Body:
All real computer hardware works in parallel. So why most emulators of retro systems (like NES) are single threaded (or I'm not right?)? You have main loop and you counting CPU cycles and after certain cycles activating other hardware like PPU.

Same thing applies to IBM PC and (MDA/CGA/VGA) emulation?


问:
Please explain the benefit(s) of using an XML-based syntax (e.g. XAML) instead of normal source code (e.g. WinForms)?
First off, please note that this question is not about WPF vs. WinForms.

What are the highest-ranking benefits that led Microsoft to invent XAML in favour of the “old” approach of generating compilable C# code?

My impression is that other developers seem to find a lot of downsides to it, but Microsoft went with it regardless, which must mean that there is a benefit that I am not seeing. I would like you to explain to me what it is that I am missing.

答:
XAML Describes Data in a Concise, but Human and Machine Comprehensible, Way

In theory every new language is something you need to learn. Quite many people would
prefer to speak many / multiple languages - as it's considered as language richness. This exists and it's
related to natural languages, but not necessary to programming languages.

I by myself have programming in quite many languages including batch files, perl, lua, python,
c++ and c#. Do I remember all of them ? No, I can switch using another language, but it takes
time. Just writing now in C#, then switch to C++, you can easily mix up is it NULL, null, nil or
nullptr depending on programming language and it's "dialect". (c++ normal, c++ managed)

xaml is xml "dialect". Is it easy to learn ? based on quite many question on other forums I would say no.

Does it replace C# ? No.

Answer is quite obvious - I would prefer to have one programming language, which is well
designed (C# - e.g. data type reflection), has a good peformance (like C++) and scriptable
(C#-script, C++/cling or even better)

Currently C++ and C# don't fully satisfy my needs, but xaml is some odd data format, which I
would not consider as worth of learning (only wrapping around, and hiding it from developer's eyes)

At the end I would like to recombine natural language (spoken) with programming language
(artificially constructed language) into same one language -
so it can be used for communication and by programmers - this is later on into future, when
programming language "architecture" or "design patterns" will start to be documented.

XAML allows for drawing Scalable Vector Graphics (SVG) Images and transformations.

...

XAML indeed introduces some new improved features over winforms, so it's worth of considering
to use it. But eventually if you want you user interface to become fully dynamic -
xaml needs to be replaced by similar run-time code.

Eventually user interface should be simple to use - and that means minimum set of controls,
controls looks similar ( cancel, ok , shortcuts, same width / height and so on )
which means that UI can be more or less auto-generated.

Btw - is it possible to embedd into xaml C# code directly ? Meta data, string, comments ?

It's sometimes neat to misuse or abuse language in way noone imagined it to be used.
Perl does this a lot, but I prefer somehow more clean syntax rather than cryptic one.
But C# versus xaml - I would chose C#.


问:
What Lisp data structure to use?
I am studying different language games and trying to implement them in Common Lisp. Currently, I am studying a game which studies the relation between forms and meanings. An agent needs to store the association between these forms and meanings. One form can have multiple meanings (ambiguity) and one meaning can have multiple forms (synonymy). On top of that, the agent needs to keep track of the score of each form-meaning pair in order to decide which ones to use.

Now, I was wondering how I could create a data-structure in Lisp that could hold all of this information. It should be able to search both on meaning and form and also add new form-meaning pairs.

My first idea was the following. Create a struct which holds the form, meaning and score and basically keep a list of all of these struct-instances. This is quite simple to implement, but searching on form or meaning becomes a pain, since I would have to go through each element of the list.

My second idea is based on a table like this:

```<code>|          || form1   | form2   | form3   | ...
|----------||---------|---------|---------| ...
| meaning1 || score11 | score12 | score13 | ...
| meaning2 || score21 | score22 | score23 | ...
| meaning3 || score31 | score32 | score33 | ...
</code>```

where score11 is the score for form-meaning pair (form1 . meaning1). The score_ij would be empty if there is no form-meaning pair (form_i . meaning_j). Would this kind of design make it easier to search on both meanings and forms? Also, I don't quite know what kind of Common Lisp data structure I could use for this.

Suggestions on what data structure to use for this, or other design ideas are welcome.

答:
Typically I would use some embedded relational store for that. Some kind of Prolog in Lisp might do.

A self-written data structure could be like this:

a CLOS class for the triple: meaning, form, score

a hash-table mapping from a meaning to a list of triples

a hash-table mapping from a form to a list of triples

The lists of triples could be sorted by score.

The I would write the retrieval and insert functions for that.

Title: What Lisp data structure to use?


问:
JIT compiler for C, C++, and the likes
Is there any just-in-time compiler out there for compiled languages, such as C and C++? (The first names that come to mind are Clang and LLVM! But I don't think they currently support it.)

Explanation:

I think the software could benefit from runtime profiling feedback and aggressively optimized recompilation of hotspots at runtime, even for compiled-to-machine languages like C and C++.

Profile-guided optimization does a similar job, but with the difference a JIT would be more flexible in different environments. In PGO you run your binary prior to releasing it. After you released it, it would use no environment/input feedbacks collected at runtime. So if the input pattern is changed, it is probe to performance penalty. But JIT works well even in that conditions.

However I think it is controversial wether the JIT compiling performance benefit outweights its own overhead.

答:
Yes, there are a couple of JIT compilers for C and/or C++.

CLing (as you might guess from the name) is based on Clang/LLVM. It acts like an interpreter. That is, you give it some source code, give a command for it to run, and it runs. The emphasis here is primarily on convenience and fast compiling, not maximum optimization. As such, although technically an answer to the question itself, this doesn't really suit the OP's intent very well.

Another possibility is NativeJIT. This fits the question somewhat differently. In particular, it does not accept C or C++ source code, and compile it and execute it. Rather, it is a small compiler that you can compile into your C++ program. It accepts an expression which is basically expressed as an EDSL inside your C++ program, and generates actual machine code from that, which you can then execute. This fits much better with a framework where you can compile most of your program with a normal compiler, but have a few expressions that you won't know until run-time, which you want to execute with something that approaches optimum execution speed.

As for the apparent intent of original question, I think the basic point of my original answer still stands: while a JIT compiler can adapt to such things as data that varies from one execution to the next, or even varying dynamically during a single execution, the reality is that this makes relatively little difference at least as a general rule. In most cases, running a compiler at run time means you need to forego quite a bit of optimization, so about the best you usually even hope for is that it's close to as fast as a conventional compiler would produce.

Although it's possible to postulate situations where information available to a JIT compiler could allow it to generate substantially better code than a conventional compiler, instances of this happening in practice seem to be pretty unusual (and in most cases where I've been able to verify its happening, it was really due to a problem in the source code, not with the static compilation model).

[Also, see edit history for quite a different answer that's now basically obsolete.]

Title: JIT compiler for C, C++, and the likes

ContentLicense: CC BY-SA 4.0


问:
Should Linked Lists always have a tail pointer?
My understanding...

Advantages:

Inserting at the end is O(1) instead of O(N).

If the list is a Doubly Linked List, then removing from the end is also O(1) instead of O(N).

Disadvantage:

Takes up a trivial amount of extra memory: 4-8 bytes.

The implementer has to keep track of the tail.

Looking at these advantages and disadvantages, I can't see why a Linked List would ever avoid using a tail pointer. Is there something I'm missing?

答:
You are correct, a tail pointer never hurts and can only help. However, there is a situation where one does not need a tail pointer at all.

If one is using a linked list to implement a stack, there is no need for a tail pointer because one can guarantee that all accesses, insertions, and removals occur at the head. That being said one might use a doubly-linked list with a tail pointer anyway because that is the standard implementation in a library or platform and memory is cheap, but one does not need it.


答: Linked lists are very commonly persistent and immutable.  In fact, in functional programming languages, this usage is ubiquitous.  Tail pointers break both of those properties.  However, if you don't care about immutability or persistence, there is very little downside to including a tail pointer.</p>

Title: Should Linked Lists always have a tail pointer?

Body:
My understanding...

Advantages:

Inserting at the end is O(1) instead of O(N).

If the list is a Doubly Linked List, then removing from the end is also O(1) instead of O(N).

Disadvantage:

Takes up a trivial amount of extra memory: 4-8 bytes.

The implementer has to keep track of the tail.

Looking at these advantages and disadvantages, I can't see why a Linked List would ever avoid using a tail pointer. Is there something I'm missing?


问:
Alternative to coding directly on server?
We have a large website made in PHP running in Apache on Linux. There are programmers as well as non-technical users modifying the website every day and we do it directly on the test server.

Is there any alternative to modifying the files directly on the server?

Sure, the programmers could all setup an Apache instance on their machine and manage it, but it's a bit of trouble as sometimes we add new extension to PHP or change configuration in Apache which means that everyone need to change those things manually. Also, it's not realistic to expect our non-technical users to manage their own Apache instance.

Another problem is that we are all on Windows computer, but the website runs under Linux. The code is not compatible with Windows so that is another issue. Switching completely to Linux is not an option either as we are using many programs that only runs on Windows for other tasks, so it would need to be a dual boot or a VM.

It feels strange to all work directly on the server, but is it the best way to go in our case?

答: Everyone altering the application should be doing it in source control, and you should have an automated process for deploying a specific version from source control to your test server.  It may require some persuasion to get your non-technical people to use source control, but their really is no alternative to keeping a non-toy system running reliably.  If the step to check-in and deploy the code is simple enough, it won't matter that no one is working directly on the server.  People like using git for this because git is fast and has developed an ecosystem of good end-user tools.  Having people work directly on the server is a recipe for disaster.</p>


答:
Use vagrant to set up the environment which mirrors the settings on the server, and let them play there.

How to get things back to the server, it's about deployment, that part is answered fine by @antlersoft, with the caveat: I don't believe you can persuade non-technical users to use source control (and git is one of the harder ones). If you need non-technical users to change web, invest time and money to find appropriate CMS and use that.

Title: Alternative to coding directly on server?

Body:
We have a large website made in PHP running in Apache on Linux. There are programmers as well as non-technical users modifying the website every day and we do it directly on the test server.

Is there any alternative to modifying the files directly on the server?

Sure, the programmers could all setup an Apache instance on their machine and manage it, but it's a bit of trouble as sometimes we add new extension to PHP or change configuration in Apache which means that everyone need to change those things manually. Also, it's not realistic to expect our non-technical users to manage their own Apache instance.

Another problem is that we are all on Windows computer, but the website runs under Linux. The code is not compatible with Windows so that is another issue. Switching completely to Linux is not an option either as we are using many programs that only runs on Windows for other tasks, so it would need to be a dual boot or a VM.

It feels strange to all work directly on the server, but is it the best way to go in our case?


问:
Is this avoiding inheritance correctly?
I want to add capabilities for an abstract Model class to send events whenever it updates. To be able to send events, it needs to have access to an EventManager. So, the intent were first, to be able to handle events (e.g. save operation) and second to send Events to the system event handler.

My approach is to implement a new interface EventManagerAware in ExistingModel class, which needs this behaviour, like this:

interface EventManagerAware {

    public function handleEvent(EventManager em);
}

class ExistingModel extends Model implements EventManagerAware {

    public function handleEvent(EventManager em) {
        em.sendEvent(Event::SAVE);
    }
}

abstract class Model() {

    public function save() {

        if (this instanceOf EventManagerAware) {
            this.handleEvent(this.getServiceManager().getEventManager());
        }
}
```

<p>After review, someone argued that it's better to just extend the <code>Model</code> class, override the <code>save()</code> method and change <code>ExistingModel</code> from extending <code>Model</code> to <code>EventPublisherModel</code> class, like so:</p>

<pre class="lang-java prettyprint-override"><code>class EventPublisherModel extends Model {

    public function save() {
        this.getServiceManager().getEventManager().sendEvent(Event::SAVE);
    }
}

class ExistingModel extends EventPublisherModel {

}
</code>```

FYI, the existing Model class looks like this:

abstract class Model {
    protected ServiceManager serviceManager;

    public function __construct(ServiceManager sm) {
        this.serviceManager = sm;
    }

    public function getServiceManager() {
        return this.serviceManager;
    }

    public function save() {

    }
}
```

One of the arguments put for having EventPublisherModel class was because doing in the first approach, all Model objects would check for EventManagerService when doing save() operation, and that's unnecessary.

I've never really thought about why the first approach would be better, but did think that it I have Segregated the Interface and didn't think inheritance is needed because sending events is in different domain from the Model class.

Which is the best way of doing this and why?

答:
It seems like the sending of events is a secondary concern for your Model class, so why not make it the job of a separate class entirely?

```<code>public interface Model {
    void save();
}

public class ExistingModel implements Model {
    public void save() {
        //save stuff
    }
}

public class EventPublishingModel implements Model {
    private Model delegate;
    private EventManager events;

    public EventPublishingModel(Model delegate, EventManager events) {
        this.delegate = delegate;
        this.events = events;
    }

    public void save() {
        delegate.save();
        events.sendEvent(Event::SAVE);
    }

    //other methods can just call the delegate
}
</code>```

And then you can have the simple model class, without any changes:

```<code> Model model = new ExistingModel();
</code>```

But decorate it with the event-publishing functionality where appropriate:

```<code> model = new EventPublishingModel(model, eventManager);
</code>```

Title: Is this avoiding inheritance correctly?

Body:
It seems like the sending of events is a secondary concern for your Model class, so why not make it the job of a separate class entirely?

public interface Model {
    void save();
}

public class ExistingModel implements Model {
    public void save() {
        //save stuff
    }
}

public class EventPublishingModel implements Model {
    private Model delegate;
    private EventManager events;

    public EventPublishingModel(Model delegate, EventManager events) {
        this.delegate = delegate;
        this.events = events;
    }

    public void save() {
        delegate.save();
        events.sendEvent(Event::SAVE);
    }

    //other methods can just call the delegate
}

And then you can have the simple model class, without any changes:

 Model model = new ExistingModel();

But decorate it with the event-publishing functionality where appropriate:

 model = new EventPublishingModel(model, eventManager);


答:
A fleet of virtual machines on a host, every user has his own instance to play with, then push from there to the main repository and make the real test server update from there.

Don't try to introduce git to non-technical users, you might end up with an axe in your skull one day. :-) Even to power developers who use it every day it adds such a high level of complexity that they fail. Tools like SourceTree are nice, but they still don't protect you from screwing up.

The most user friendly version control I experienced so far is TortoiseSVN, even non-technical users understand the svn up and svn commit commands when they get it presented in a clean graphical interface.


问:
Database design and performance impact
I have a database design issue that I'm not quite sure how to approach, nor if the benefits out weigh the costs. I'm hoping some P.SE members can give some feedback on my suggested design, as well as any similar experiences they may have came across.

As it goes, I am building an application that has large reporting demands.  Speed is an important issue, as there will be peak usages throughout the year.

This application/database has a multiple-level, many-to-many relationship.

eg

```<code>object a
object b
object c
object d

object b has relationship to object a
object c has relationship to object b, a
object d has relationship to object c, b, a
</code>```

Theoretically, this could go on for unlimited levels, though logic dictates it could only go so far.

My idea here, to speed up reporting, would be to create a syndicate table that acts as a global many-to-many join table. In this table (with the given example), one might see:

```<code>+----------+-----------+---------+
| child_id | parent_id | type_id |
+----------+-----------+---------+
|    b     |     a     |    1    |
|    c     |     b     |    2    |
|    c     |     a     |    3    |
|    d     |     c     |    4    |
|    d     |     b     |    5    |
|    d     |     a     |    6    |
+----------+-----------+---------+
</code>```

Where a, b, c and d would translate to their respective ID's in their respective tables. So, for ease of reporting all of a which exist on object d, one could query

```<code>SELECT * FROM `syndicates` ... JOINS TO child and parent tables ... WHERE parent_id=a and type_id=6;
</code>```

rather than having a query with a join to each level up the chain.

The Problem

This table grows exponentially, and in a given year, could easily grow past 20,000 records for one client. Given multiple clients over multiple years, this table will VERY quickly explode to millions of records and beyond.

Now, the database will, in time, be partitioned across multiple servers, but I would like (as most would) to keep the number of servers as low as possible while still offering flexibility.

Also writes and updates would be exponentially longer (though possibly not noticeable to the end user) as there would be multiple inserts/updates/scans on this table to keep it in sync.

Am I going in the right direction here, or am I way off track. What would you do in a similar situation?  This solution seems overly complex, but allows the greatest flexibility and fastest read-operations.

Sidenote 1 - This structure allows me to add new levels to the tree easily.

Sidenote 2 - The database querying for this database is done through an ORM framework.

答:
Despite not being 100% sure about the ORM framework, it is probably a good idea to use MPTT to create your relationships between objects, your syndicate table.

This will allow unlimited levels of relationships and easy access to records, since you'll mostly be doing primary key lookups after getting the relationship information from this table.

The queries might not be very simple or easy to understand, please see this link for more information and examples.

Millions of records are not a problem, I've had experiences with tables around 10 million rows and with reasonable performance.

Despite the links pointing to the MySQL site, the concepts apply to any database (I just like that article).

ContentLicense: CC BY-SA 2.5

Title: Database design and performance impact


答:
First, your method naming can be improved. handleEvent should better be called sendSaveEvent, because it's responsibility is not to "handle" any event, it is for sending events, and it is not for sending generic events, only for "save" events.

Second, your example does not show the actual "saving" code. If you had added a placeholder for this, it becomes obvious why the second solution is not really a good idea - when each subclass needs to override the save method just for adding "even sending logic", the result is at least some duplicated code. This is not necessary, the code can be kept in one place (in the abstract model base class).

This in mind, I would consider to solve this with the template method pattern:

abstract class Model{

    public function save() {
       // ... here is the actual saving code ...

       sendSaveEvent(this.getServiceManager().getEventManager());
    }
    public function sendSaveEvent(EventManager em) {
          // empty default implementation
    }
}
```

<p>and in the derived class: </p>

<pre class="lang-java prettyprint-override"><code>class ExistingModel extends Model {

    // overridden function
    public function sendSaveEvent(EventManager em) {
        em.sendEvent(Event::SAVE);
    }
}
</code>```

As you see, the code is simpler, the EventManagerAware interface is not necessary any more, the EventPublisherModel is not necessary, and each derivation of Model can simply choose if and how to send a SAVE event.

UPDATE: using dependency injection together with the null object pattern can make your desing even more simple:

  class Model  // maybe abstract
  {

    // Initialize this variable with a value by constructor
    // injection either directly in the Model class,
    // or in a subclass, whatever suits your needs.
    EventManager _myEventManager;

    public Model()
    {
        // The "EmptyEventManager" has an empty "sendEvent" method
        // (some kind of "null" object).
        _myEventManager = new EmptyEventManager()
    }

    public Model(EventManager em)
    {
       _myEventManager=em;
    }

    public function save() {
       // ... here is the actual saving code ...

       _myEventManager.sendEvent(Event::SAVE); // sendEvent is a virtual method
    }
}
```

This does not only eliminate your "service locator", it also allows you to switch between "no event sending" and "event sending" just by providing a different EventManager subclass to your Model object (without the need for introducing a class like "ExistingModel", but if you need this subclass for different reasons, you can still add it, if you like). The pattern applied is is the well known strategy pattern, with the EventManager as strategy object.

As a final remark: I do not know your "real" use case, but would it not make more sense to provide a default implementation in the Model base class where the SAVE event is actually sended?

Title: Is this avoiding inheritance correctly?

Body:
I want to add capabilities for an abstract Model class to send events whenever it updates. To be able to send events, it needs to have access to an EventManager. So, the intent were first, to be able to handle events (e.g. save operation) and second to send Events to the system event handler.

My approach is to implement a new interface EventManagerAware in ExistingModel class, which needs this behaviour, like this:

interface EventManagerAware {

    public function handleEvent(EventManager em);
}

class ExistingModel extends Model implements EventManagerAware {

    public function handleEvent(EventManager em) {
        em.sendEvent(Event::SAVE);
    }
}

abstract class Model() {

    public function save() {

        if (this instanceOf EventManagerAware) {
            this.handleEvent(this.getServiceManager().getEventManager());
        }
}
```

<p>After review, someone argued that it's better to just extend the <code>Model</code> class, override the <code>save()</code> method and change <code>ExistingModel</code> from extending <code>Model</code> to <code>EventPublisherModel</code> class, like so:</p>

<pre class="lang-java prettyprint-override"><code>class EventPublisherModel extends Model {

    public function save() {
        this.getServiceManager().getEventManager().sendEvent(Event::SAVE);
    }
}

class ExistingModel extends EventPublisherModel {

}
</code>```

FYI, the existing Model class looks like this:

abstract class Model {
    protected ServiceManager serviceManager;

    public function __construct(ServiceManager sm) {
        this.serviceManager = sm;
    }

    public function getServiceManager() {
        return this.serviceManager;
    }

    public function save() {

    }
}
```

One of the arguments put for having EventPublisherModel class was because doing in the first approach, all Model objects would check for EventManagerService when doing save() operation, and that's unnecessary.

I've never really thought about why the first approach would be better, but did think that it I have Segregated the Interface and didn't think inheritance is needed because sending events is in different domain from the Model class.

Which is the best way of doing this and why?

Body:
First, your method naming can be improved. handleEvent should better be called sendSaveEvent, because it's responsibility is not to "handle" any event, it is for sending events, and it is not for sending generic events, only for "save" events.

Second, your example does not show the actual "saving" code. If you had added a placeholder for this, it becomes obvious why the second solution is not really a good idea - when each subclass needs to override the save method just for adding "even sending logic", the result is at least some duplicated code. This is not necessary, the code can be kept in one place (in the abstract model base class).

This in mind, I would consider to solve this with the template method pattern:

abstract class Model{

    public function save() {
       // ... here is the actual saving code ...

       sendSaveEvent(this.getServiceManager().getEventManager());
    }
    public function sendSaveEvent(EventManager em) {
          // empty default implementation
    }
}

and in the derived class:

class ExistingModel extends Model {

    // overridden function
    public function sendSaveEvent(EventManager em) {
        em.sendEvent(Event::SAVE);
    }
}

As you see, the code is simpler, the EventManagerAware interface is not necessary any more, the EventPublisherModel is not necessary, and each derivation of Model can simply choose if and how to send a SAVE event.

UPDATE: using dependency injection together with the null object pattern can make your desing even more simple:

  class Model  // maybe abstract
  {

    // Initialize this variable with a value by constructor
    // injection either directly in the Model class,
    // or in a subclass, whatever suits your needs.
    EventManager _myEventManager;

    public Model()
    {
        // The "EmptyEventManager" has an empty "sendEvent" method
        // (some kind of "null" object).
        _myEventManager = new EmptyEventManager()
    }

    public Model(EventManager em)
    {
       _myEventManager=em;
    }

    public function save() {
       // ... here is the actual saving code ...

       _myEventManager.sendEvent(Event::SAVE); // sendEvent is a virtual method
    }
}

This does not only eliminate your "service locator", it also allows you to switch between "no event sending" and "event sending" just by providing a different EventManager subclass to your Model object (without the need for introducing a class like "ExistingModel", but if you need this subclass for different reasons, you can still add it, if you like). The pattern applied is is the well known strategy pattern, with the EventManager as strategy object.

As a final remark: I do not know your "real" use case, but would it not make more sense to provide a default implementation in the Model base class where the SAVE event is actually sended?


问:
Unit Testing: Structured vs Unstructured Programming
Is it possible to write effective unit tests for unstructured code?

I was reading through the various answers to this question. It explains the implications of unit testing procedural and OOP code, but there is very little mention about unstructured code.

In my personal experience, I've only written unit tests for functions and class methods. I imagine that it would be difficult indeed to test unstructured code, but I'm also curious to know if it's possible, and what the best practices are for unit testing such code.

答:
You are basically talking about one very big unit. Normally, you would write lots of small unit tests for small units, but in this case it will likely be a lot of elaborate tests for one big unit.

At my work, we have the same kind of problem. In some of our products we use SOUP (Software of Unknown Provenance), which we have to 'unit test' to comply with rules and regulations pertaining to our products.

SOUP can be a library, e.g. a Bluetooth stack, or a an execution framework, e.g. QP/C. We write tests that execute the API or write test applications that integrate the SOUP and test its (internal) functions.

One important function of these tests is to detect regression or undesirable changes when the SOUP is updated. But most of the time, these tests are used to verify that the SOUP does what it is expected to do and that there are no nasty surprises or corner-cases.

Developing 'unit' tests for a piece of unstructured software is also very useful if you want to restructure it or replace it.


问:
Public and Private Constructors with Equivalent Signatures
Here is the problem illustrated using an example of an immutable class.  A Book must have at least one of a title and an ISBN.

```<code>public class Book
{
    private readonly string _title;
    private readonly int? _isbn;

    public Book(string title)
        : this(title, null, true)
    {
        ... throw exception if title is null
    }

    public Book(int isbn)
        : this(null, isbn, true)
    {
        ... throw exception if isbn < 1
    }

    public Book(string title, int isbn)
        : this(title, isbn, true)
    {
        ... throw exception if title is null
        ... throw exception if isbn < 1
    }

    private Book(string title, int? isbn, bool privateConstructor = true)
    {
        _title = title;
        _isbn = isbn;
        ... more work (beyond the scope of this example)
    }

    ... public properties, etc.
}
</code>```

See that privateConstructor boolean?  I had to throw that in to differentiate between the public Book(string, int) and the private Book(string, int?).  While this is fairly harmless since the extraneous parameter is in a private constructor, its use seems smelly to me.  Reordering the parameters of the private constructor would work but that would make the constructor initializers appear non-intuitive (in my real work, there's more than just two parameters in play).  What is a better way?

答:
This looks like C# with optional parameters.

Since you're already throwing exceptions in the public constructors, you can dispense with all those constructors except for the private one.  Make it public, make title optional, remove the boolean parameter, and have your callers use named parameters.

The other thing you can do is just make the private constructor an ordinary method call instead, and give it a different name.  That way, you don't need the bool parameter to disambiguate it.


答:
There isn't really a different way.

Constructors always have a pre-determined name (either something like __init__ as in Python, or the class name as in C++/Java/C#), so that can't be varied to differentiate the two constructors. That means that only the argument list can be used to create different overloads.

If you want to delegate some work to another (private) constructor that happens to need a signature compatible with that of a public constructor, then you have essentially 3 options:

Add a dummy parameter to the private constructor, so that it becomes a separate overload

Merge the public and private constructor with the equivalent signatures

Instead of delegating to a private constructor, delegate to a private function, and accept that the member-initializations need to be duplicated.

Neither of these options is really elegant, so you should decide on a case-by-case basis which is the least ugly.

Title: Public and Private Constructors with Equivalent Signatures

Body:
Here is the problem illustrated using an example of an immutable class.  A Book must have at least one of a title and an ISBN.

```<code>public class Book
{
    private readonly string _title;
    private readonly int? _isbn;

    public Book(string title)
        : this(title, null, true)
    {
        ... throw exception if title is null
    }

    public Book(int isbn)
        : this(null, isbn, true)
    {
        ... throw exception if isbn < 1
    }

    public Book(string title, int isbn)
        : this(title, isbn, true)
    {
        ... throw exception if title is null
        ... throw exception if isbn < 1
    }

    private Book(string title, int? isbn, bool privateConstructor = true)
    {
        _title = title;
        _isbn = isbn;
        ... more work (beyond the scope of this example)
    }

    ... public properties, etc.
}
</code>```

See that privateConstructor boolean?  I had to throw that in to differentiate between the public Book(string, int) and the private Book(string, int?).  While this is fairly harmless since the extraneous parameter is in a private constructor, its use seems smelly to me.  Reordering the parameters of the private constructor would work but that would make the constructor initializers appear non-intuitive (in my real work, there's more than just two parameters in play).  What is a better way?


问:
How to do data modeling when the model is partially initialized from DB?
Consider data modeling in Scala(the other language is the same), we often want to composite one model inside another, e.g a Post may contain an author field whose type is a User.

When we retrieve the data from db, we might want to retrieve some basic information due to the performance consideration and complete the others missing parts when it is needed.  The only way I can do is to make those fields as some Options in model.

But this makes the model dirty in semantics and not convenient to use since those fields are actually mandatory in model. In the above case, every Post has an author, but I has no choice except to implement it as an Option.

Any advices on this?

答:
You could achieve this using laziness.

```<code>scala> def fetchPost(id: Int): (Int, String) = { println("fetching content") ; val userId = 42; (userId, "content of post") }
fetchPost: (id: Int)(Int, String)

scala> def fetchUser(userId: Int): (String, String) = { println("fetching user"); ("firstname", "lastname") }
fetchUser: (userId: Int)(String, String)

scala> class Post(id: Int) {
    | lazy val (userId, content) = fetchPost(id)
    | lazy val user = fetchUser(userId)
    | }
defined class Post

scala> val post1 = new Post(1)
post1: Post = Post@2362f559

scala> post1.user
fetching content
fetching user
res0: (String, String) = (firstname,lastname)

scala> val post2 = new Post(2)
post2: Post = Post@17d919b6

scala> post2.content
fetching content
res1: String = content of post

scala> post2.user
fetching user
res2: (String, String) = (firstname,lastname)
</code>```

The fetching is done on demand when needed. On the other hand, you shouldn't do these kinds of optimizations until you have performance data that explicitly tells you you have a problem. Especially if you can get your database to fetch all the data from different tables in one go, you might be micro-optimizing just to get worse performance in the end.

Body:
You could achieve this using laziness.

scala> def fetchPost(id: Int): (Int, String) = { println("fetching content") ; val userId = 42; (userId, "content of post") }
fetchPost: (id: Int)(Int, String)

scala> def fetchUser(userId: Int): (String, String) = { println("fetching user"); ("firstname", "lastname") }
fetchUser: (userId: Int)(String, String)

scala> class Post(id: Int) {
    | lazy val (userId, content) = fetchPost(id)
    | lazy val user = fetchUser(userId)
    | }
defined class Post

scala> val post1 = new Post(1)
post1: Post = Post@2362f559

scala> post1.user
fetching content
fetching user
res0: (String, String) = (firstname,lastname)

scala> val post2 = new Post(2)
post2: Post = Post@17d919b6

scala> post2.content
fetching content
res1: String = content of post

scala> post2.user
fetching user
res2: (String, String) = (firstname,lastname)

The fetching is done on demand when needed. On the other hand, you shouldn't do these kinds of optimizations until you have performance data that explicitly tells you you have a problem. Especially if you can get your database to fetch all the data from different tables in one go, you might be micro-optimizing just to get worse performance in the end.


答:
No. Unless you want to seriously twist the meaning of the term "unit testing" in such a way that it essentially becomes synonymous with "system testing", unit testing a system that has no individual units to test, is meaningless.

You can of course do system testing. If the code performs multiple functionalities, you can do functional testing. If the code integrates with other code, you can do integration testing. If you have some acceptance criteria for the code, you can do acceptance testing. You can do performance testing.

But you can't do unit testing, because unit testing tests individual independent units in isolation, and there are no individual independent units in unstructured code (that's pretty much what "unstructured" means).

Title: Unit Testing: Structured vs Unstructured Programming

Body:
Is it possible to write effective unit tests for unstructured code?

I was reading through the various answers to this question. It explains the implications of unit testing procedural and OOP code, but there is very little mention about unstructured code.

In my personal experience, I've only written unit tests for functions and class methods. I imagine that it would be difficult indeed to test unstructured code, but I'm also curious to know if it's possible, and what the best practices are for unit testing such code.


问:
IOS app updates that don't require App Store submission
Some apps, I've noticed that developers are able to update their software without submitting them to the App Store.

For example, Game of War will occasionally tell you that an update is available and to restart your device.  You can then watch the progress bar carefully and see that updates are being pushed to the app.

I'm assuming they are using XML to change certain attributes and their location on the server.  That's a guess but I don't know.  They are able to make significant feature changes to the game, like adding hyperlinks to map locations,  push sales promotions, add certain events etc. These methods don't require compilation, as that would obviously require resubmitting the app.

What technologies allow this? Are they all server side changes with XML objects being updated on the client? What is apple's policy in the developer terms?

答:
Apple now explicitly allows interpretted code like Javascript to be 'Hot Code Pushed' to a device without an update.

3.3.2 An Application may not download or install executable code. Interpreted code may only be used in an Application if all scripts, code and interpreters are packaged in the Application and not downloaded. The only exception to the foregoing is scripts and code downloaded and run by Apple's built-in WebKit framework, provided that such scripts and code do not change the primary purpose of the Application by providing features or functionality that are inconsistent with the intended and advertised purpose of the Application as submitted to the App Store.

See here:
Apple Allows Hot Code Push - Meteor


问:
Checked vs unchecked exception when validating documents in this service
I have a service that allow users to add dynamic content to a repository. So basically I have a generic Document class that contains a list of property for that specific object depending on what type of document the user is adding (eg. an invoice document has an invoice number property while a wiki document has an author property, and so on).

The service is comprised of different layers and at some point I have a class that has to check if the document to be added is compliant to the rules configurer, evaluating if all required properties are provided, if they are all of the right type, etc. If any of these validations fail I want to throw a custom exception which contains the validation status.

The question is:

Should my ValidationException be checked or unchecked?

I read a lot of best practice on how to decide what kind of exception use. I was thinking about using RuntimeException but in this case the exception is not caused by error in coding or stuff like that but just by the user input...

On the other hand using a checked exception would mean to propagate the "throws" syntax in all the above layer of the application and probably in the 90% of the service's methods making the code much less readable and maintainable.

答:
You should use a checked exception so that the compiler always reminds you to catch it.

If the validation fails you want to catch that error anyway so why use unchecked exceptions and maybe forget that this exception can be thrown.

For more details on this particular case you can also read: http://learnjava.today/2015/11/checked-vs-unchecked-exceptions/


答:
Remove your private constructor and do the logic in the last public constructor:

```<code>public class Book
{
    private readonly string _title;
    private readonly int? _isbn;

    public Book(string title)
        : this(title, 0)
    {
    }
    public Book(int isbn)
        : this(null, isbn)
    {
    }

    public Book(string title, int isbn)
    {
        ... throw exception if title is null AND isbn < 1
        ... throw exception if isbn < 0

        _title = title;
        _isbn = isbn > 1 ? isbn : null;
        ... more work (beyond the scope of this example)
    }
}
</code>```

I would use the 0 to indicate the ISBN is not given. Alternatively, it might be better to make _isbn not nullable, because the initial value 0 would indicate there is no ISBN. So:

```<code>public class Book
{
    private readonly string _title;
    private readonly int _isbn;

    public Book(string title)
        : this(title, 0)
    {
    }
    public Book(int isbn)
        : this(null, isbn)
    {
    }

    public Book(string title, int isbn)
    {
        ... throw exception if title is null AND isbn < 1
        ... throw exception if isbn < 0

        _title = title;
        _isbn = isbn;
        ... more work (beyond the scope of this example)
    }
}
</code>```

Better for readability.

Body:
Remove your private constructor and do the logic in the last public constructor:

public class Book
{
    private readonly string _title;
    private readonly int? _isbn;

    public Book(string title)
        : this(title, 0)
    {
    }
    public Book(int isbn)
        : this(null, isbn)
    {
    }

    public Book(string title, int isbn)
    {
        ... throw exception if title is null AND isbn < 1
        ... throw exception if isbn < 0

        _title = title;
        _isbn = isbn > 1 ? isbn : null;
        ... more work (beyond the scope of this example)
    }
}

I would use the 0 to indicate the ISBN is not given. Alternatively, it might be better to make _isbn not nullable, because the initial value 0 would indicate there is no ISBN. So:

public class Book
{
    private readonly string _title;
    private readonly int _isbn;

    public Book(string title)
        : this(title, 0)
    {
    }
    public Book(int isbn)
        : this(null, isbn)
    {
    }

    public Book(string title, int isbn)
    {
        ... throw exception if title is null AND isbn < 1
        ... throw exception if isbn < 0

        _title = title;
        _isbn = isbn;
        ... more work (beyond the scope of this example)
    }
}

Better for readability.


问:
Python - Cache function and decorator
I am playing with cache functions using decorators.

First, I use a generic function

```<code>def memorize(func):
    cache = {}
    print "printing cache"
    print cache
    print "cache printed"
    def decor_func(*args):
        if args in cache:
            print "cached value is found"
            return cache[args]
        else:
            print "calculating the value"
            val = func(*args)
            cache[args] = val
            return val
    return decor_func
</code>```

Then I use it to decorate a function called test

```<code>def test(n):
    return n

test_decorated = memorize(test)
</code>```

Then I get the following results

```<code>>>> test_decorated(1)
calculating the value
1
>>> test_decorated(2)
calculating the value
2
>>> test_decorated(1)
cached value is found
1
>>>
</code>```

You can see that the second test_decorated(1) won't actually do any calculation, because we have already cache the result.

Now I want understand further, so I define

```<code>def test_cached(n):
    cache = {}
    print "printing cache"
    print cache
    print "cache printed"
    if n in cache:
        print "cached value is found"
        return cache[n]
    else:
        print "calculating the value"
        val = n
        cache[n] = val
        return val
</code>```

I thought that I will get the same results as above when I call test_cached.
However, the answer is NO, and the function test_cached didn't remember any calculated values. Here is the result:

```<code>>>> test_cached(1)
printing cache
{}
cache printed
calculating the value
1
>>> test_cached(2)
printing cache
{}
cache printed
calculating the value
2
>>> test_cached(1)
printing cache
{}
cache printed
calculating the value
1
>>>
</code>```

As you can see, the second test_cached(1) re-calculate again the result.

I would like to know why these two approaches give different results, i.e.
why the first approach successfully caches the results, but the second fails

答:
cache is initialised when you do cache = {} which happens every time you call test_cached. Thus, the case is always empty (and useless) when you want to use it.

In your original code, cache is also initialised when you do cache = {} but you only do this when you call memorize which is done only once : when you do : test_decorated = memorize(test).

When you call test_decorated, it kind of remembers which cache object is to be used (and modified if needed).

(If you want to learn more about this, it might be interesting to look up for closures in Python).

Title: Python - Cache function and decorator


问:
Database integration of Rails and Laravel apps
I have some very small apps on a VPS running ruby on rails and a co-worker also have some of his own apps running on laravel at another VPS.
So far we dont needed to share any data among the apps, but we are currently developing something that would have some overlaping database tables like a Users table.
How would you make that integration? Create a mysql database for all apps to connect?
Is there someway to "replicate" Userupdates on a database to another one?

答: I would suggest creating web service to store such users. It's easier to maintain and test with such soulution. You don't want to coordinate development of every tool each time you need to update DB schema, do you?</p>


答:
The answer to your problem is twofold.

TL;DR: Use DTAP and implement a VCS.

Firstly, in an enterprise environment you never want to be coding directly on the server. Even if it's not the live environment, having multiple people editing files on the same environment gives you a very high chance of conflicting changes, which leads to unpredictable results. Fully implementing DTAP will help you solve this problem.

DTAP stands for "Development, Test, Acceptance, Production" and is used to describe a system where you have 4 separate locations where the code can be. The separation is designed to protect Production from as many possible issues as possible:

First the code is run and tested on Development (which can be a separate machine but is often simply the developer's own PC).
If the developer is satisfied with his work, the code moves on to the Test environment, where someone else (ideally a dedicated tester but another developer can work) will try the code out.
If this phase of testing finds no issues, the code moves on to Acceptance, where the "business" side of the company will test the code. This can be your customer if your company makes a product for another party, or it can be a different department that is in touch with what the product should do in order to match the end-customers' needs.
Only if they accept the code will the code move on to the Production environment, which is the live environment.

Secondly, to keep track of the changes and to prevent problems that arise when multiple people modify the same area of the code at the same time, you should implement a VCS (Version Control System) in your organisation that will keep track of all the changes to all the files (examples are SVN, Git and Mercurial). These systems will also allow you to roll back changes if it turns out you made a mistake. To make life easier, it might be worth using your system of choice with an account at a service like Bitbucket which will allow you to use their interface for the process of combining one developer's work with another. All of your developers will have to agree on the specific strategy to use for merging the code into the state that will be moving on from development to the next environment, but I won't overload my answer with that.

When you say that non-technical people are modifying the test environment, I hope you mean they do changes to the configuration and/or content, not to actual files. Non-technical people should never be touching the code of the website, after all if they have no idea what they are doing they might accidentally break something!

With regards to the issue of compatibility and the statement that non-technical people cannot be expected to maintain their own Apache server, there are several options I can see. One option is to have one person manage the installation of these servers for all the parties involved, but this can be a full-time job and it might not fit your organisation to have such a person. The alternative is to use one of the available tools like Vagrant or Docker, which will allow one technical person to create an "image" that will have the working environment (which will typically be Linux based) and that image can be distributed to all the developers so they can run it on their own machine. If updates to the architecture need to be done, this person updates the image, and distributes it to the developers again.

Personal opinion:
From the original post I infer that your company is past the state of "only 2 people are working on the site anyways so it doesn't matter what we do". These concepts I propose are widely accepted as good practices, as you can see from my explanation you will gain a LOT of added checks of the quality of the product, at the tradeoff of introducing some relatively slight additional complexity to your process. A version control system like Git or Mercurial may seem intimidating, but in my opinion, with some training literally anyone can learn how to work with it properly. All it requires is an attitude of willingness to learn instead of seeing the system like an enemy that is preventing you from getting your work done, the latter of which is sadly a lot more common than it should be. By learning how to use the tool properly, you are saving yourself the time that you would have to spend on correcting mistakes and manually sorting out how the work from multiple people should be combined into the code.


问:
How to scrub Twitter and Facebook posts for many users
I'm making an Rails app that in theory should scrub new posts from the users facebook and twitter accounts and put them in a timeline for an analyst to analyze and determine if they are good or bad posts, if the user posted "bad content" the analyst marks it as so and warns the user.

I've made the authentication with omniauth and have made both facebook and twitter apps, users can already sign in and in return I get a oauth key and secret, with these I can make the respective API calls.

Now for the real question, how am I supposed to check for new updates?
I've thought using resque or sidekiq to queue each user's facebook and twitter accounts periodically, but wouldn't it be too much if I had many users, making a new API client (I'm thinking Koala and Twitter gems) with each user's key and secret every certain time?

What's the best way to do this?

And once I got the users' posts should I store them in the DB or just put them in another resque or sidekiq queue for them to be analyzed and discarded once they are?

答:
There's actually no other way to get this info than fetching the data.

You would have to dive into each API to look for ways to minimize your work. Some could accept ETag, or "Last-Modified" so your request ends quickly if nothing is there ;)

Facebook for example allows you to ask for few resources at once (https://developers.facebook.com/docs/graph-api/using-graph-api/v2.5#multirequests)

Depending on your app, to minimize requests number you could

analyze statistically how different users post, and modify priorities
accordingly (depending on their location most of them will not tweet
when they sleep - some will, when using solutions like bufferapp)

fetch data when user enters your page. You can also call some of those api's on client side and then push information back to your system to analyze them.


答:
First of all, version control is a must. But moreover any changes need to be tested before they are committed. And for those changes each user need their own test environment. So coding directly on the server is not a good idea.

How much separation you need between the test environment of each user depends on your specific needs. For some simple tasks you may achieve sufficient separation by having a shared test server running a vhost per user. If you need more separation you can configure a VPS per user.

For the most demanding tasks you may need a dedicated physical machine per user. If that means you need to purchase another physical machine for some users, you have to decide whether you want the other machine to be located at the user's desk or in a server rack.

I have seen users mess up commits in every version control system I have ever worked with. Some people are able to learn how to work with a version control system correctly, other people are not. If you have non-technical people who need to change some of the code base, but cannot learn how to use version control, there is another option worth considering.

You can let the developers have access to the non-technical person's test machines (either as a shell login or through a network file system), such that once a change is ready the non-technical person can ask a developer to commit the change.


问:
Is it considered a bad coding practice to write methods/properties that support (only) testing?
When I write (unit) tests I always try to avoid for example microsoft fakes because then the edit&continue feature no longer works. However this often requires refactoring... and sometimes methods or properties that exist only to support (unit) tests. An example:

With fakes I could do in each test method simply this:

```<code>ShimMyClass.InitializedGet = () => false;
</code>```

but without fakes I need to write an internal method ResetInitialized() visible only to the (unit) test assembly.

As to:

Would you rather make private stuff internal/public for tests, or use some kind of hack like PrivateObject?

Is it a bad practice to modify code strictly for testing purposes

I won't make any private methods public to support unit testing but write them only for unit testing. Such methods would not have any other pupose but to be called in a unit test. ResetInitialized would never be called during a normal workflow but only by a unit test.

In other words: if I didn't create the unit test I would never have to write this additional helper method/property. It will never be called by anything other then a unit test.

Example

I have a class that I initialize when the application starts and subsequent initializations are skipped. The initialization takes place internally when the first feature is used. In this case I'm not able to test more then one test case at a time. I need to be able to reset it to let the tests run automatically.

Would it be bad to make it actually also resetable but the only user of this feature would be the unit test?

答:
In other words: if I didn't create the unit test I would never have to
  write this additional helper method/property. It will never be called
  by anything other then a unit test.

A quick answer - you should not modify your source code to cater your tests.

Also rule of thumb: if a piece of code/method/class/functionality will never be called - remove it. Don't comment out, don't make it private - remove it.

However it is perfectly fine to write new mocks/stubs or fake method in your tests that would monkeypatch/overload/replace the original functionality.

Testsing/implementation should follow 80/20 rule meaning that approximatelly 80% of your development time will go on writing tests (unit, intergration, end-to-end, call any..) And it is completely obvious that if you are chaning the interface of your methods, then you will have to update your fakers or unit test structure.


答:
Another answer: yes, you should absolutely modify your source code to cater to your tests.

Testing fulfills the function of improving and maintainig the quality of your code base. Therefore, making tests easier, or possible, to write, is a valid goal with positive business value, just like writing business code. It's logically inconsistent to spend time writing tests and simultaneously claiming that it is never justifiable to change business code for the sake of the test suite.

As always, this is a matter of trade-offs. Obviously you shouldn't make your code much less maintainable for a small improvement in test writing. But a small or zero expansion of the business code for a big improvement in test writing can absolutely be justified.

Lots of developers make the mistake of thinking that since test code isn't shipped to the customer, it's somehow not really part of the code base and needn't be as well-written and reliable as the business code. This couldn't be more wrong; we do testing solely because it helps us create better programs, so it cannot have zero value - otherwise it would be negligent of us to write any tests at all.

Title: Is it considered a bad coding practice to write methods/properties that support (only) testing?

Body:
When I write (unit) tests I always try to avoid for example microsoft fakes because then the edit&continue feature no longer works. However this often requires refactoring... and sometimes methods or properties that exist only to support (unit) tests. An example:

With fakes I could do in each test method simply this:

```<code>ShimMyClass.InitializedGet = () => false;
</code>```

but without fakes I need to write an internal method ResetInitialized() visible only to the (unit) test assembly.

As to:

Would you rather make private stuff internal/public for tests, or use some kind of hack like PrivateObject?

Is it a bad practice to modify code strictly for testing purposes

I won't make any private methods public to support unit testing but write them only for unit testing. Such methods would not have any other pupose but to be called in a unit test. ResetInitialized would never be called during a normal workflow but only by a unit test.

In other words: if I didn't create the unit test I would never have to write this additional helper method/property. It will never be called by anything other then a unit test.

Example

I have a class that I initialize when the application starts and subsequent initializations are skipped. The initialization takes place internally when the first feature is used. In this case I'm not able to test more then one test case at a time. I need to be able to reset it to let the tests run automatically.

Would it be bad to make it actually also resetable but the only user of this feature would be the unit test?


答:
You need to make your code testable. That's rule number one. Anything else is pandering to the rabble who say, "we don't need no stinkin' tests."

If you need to re-initialize something, there is a possibility that the lifecycle for your code-under-test is the problem. When you create a new object instance, does it not cleanly initialize to the state you want?

If you have hidden state, you may have to add state-monitoring variables that are only used for testing.


问:
Image resolution in a RESTful interface
Background

I have a RESTful HTTP API.

I have a resource.

```<code>GET /my/cat HTTP/1.1
</code>```

Clients may want to retrieve the image representation of this resource.

```<code>GET /my/cat HTTP/1.1
Accepts: image/png
</code>```

Problem

Clients have a very wide variety of needed resolutions, and it is important they receive the correct resolution. If it is too large, it will consume excessive bandwidth.  If it is too small, it won't look good. (Not to mention the downsampling/antialiasing the client will have to do to try to get it to fit what it wanted, with questionable results.)

Options

Query parameter

```<code>GET /my/cat?resolution=100 HTTP/1.1
Accepts: image/png
</code>```

This works, but it is a little weird. If you change to accepting text/xml at that same location (URL), the it no longer makes much sense. And it requires URL mangling, which isn't really RESTful.

MIME type parameter

```<code>GET /my/cat HTTP/1.1
Accepts: image/png; resolution=100
</code>```

This makes some sense, as it very directly relates to the particular format I am requesting, similar to adding a charset parameter for text responses. I haven't seen this done before, though.

Custom header

```<code>GET /my/foo HTTP/1.1
Accepts: image/png
X-Image-Resolution: 100
</code>```

I can't see much wrong with this. Custom headers are completely arbitrary, and can basically relate to anything about a request, and it can be tempting to overuse them.

What is most standard way to do this? Or at least the most "normal" or "expected" way?

答:
What is most standard way to do this? Or at least the most "normal" or "expected" way?

I don't have statistics, but I can tell you what I would expect and what I have seen in more or less RESTful interfaces.

First of all, I wouldn't expect a cat image to be just another representation of the /my/cat resource. I would expect it to be a subresource (a kind of component) of the cat resource. In HTTP environments, images usually look like files, with an extension indicating their formats. So I would expect /my/cat/image.jpg and my/cat/image.png.

With regard to the resolution, it is common to offer an image in two or three resolutions, targeting at the desktop versus mobile platforms (two) or at the desktop, tablet and smartphone devices (three). This is expressed in their file names, e.g. /my/cat/image-large.jpg and /my/cat/image-small.jpg.

Well, this sounds really traditional, huh? No fancy use of HTTP headers etc. Yes, that's how HTTP is being used, that's what I think most people expect.

By the way, if you have a RESTful interface, I would expect /my/cat to return a JSON or XML response (maybe depending on the Accept header, indeed), which includes links to the images:

```<code>   {
      "name" : "Tom",
      "image" : {
         "large" : { "href" : "/my/cat/image-large.jpg" },
         "small" : { "href" : "/my/cat/image-small.jpg" }
      }
   }
</code>```

See JAREST for more details about what I recommend for RESTful APIs.

Title: Image resolution in a RESTful interface

Body:
What is most standard way to do this? Or at least the most "normal" or "expected" way?

I don't have statistics, but I can tell you what I would expect and what I have seen in more or less RESTful interfaces.

First of all, I wouldn't expect a cat image to be just another representation of the /my/cat resource. I would expect it to be a subresource (a kind of component) of the cat resource. In HTTP environments, images usually look like files, with an extension indicating their formats. So I would expect /my/cat/image.jpg and my/cat/image.png.

With regard to the resolution, it is common to offer an image in two or three resolutions, targeting at the desktop versus mobile platforms (two) or at the desktop, tablet and smartphone devices (three). This is expressed in their file names, e.g. /my/cat/image-large.jpg and /my/cat/image-small.jpg.

Well, this sounds really traditional, huh? No fancy use of HTTP headers etc. Yes, that's how HTTP is being used, that's what I think most people expect.

By the way, if you have a RESTful interface, I would expect /my/cat to return a JSON or XML response (maybe depending on the Accept header, indeed), which includes links to the images:

   {
      "name" : "Tom",
      "image" : {
         "large" : { "href" : "/my/cat/image-large.jpg" },
         "small" : { "href" : "/my/cat/image-small.jpg" }
      }
   }

See JAREST for more details about what I recommend for RESTful APIs.


问:
How are hypermedia rel values specified in the REST uniform interface?
I am learning a bit about REST and am intrigued by the concept of a uniform interface, and the ability of a client to follow hypermedia links in order to detect service capabilities.

As I understand it, the RESTful server will return a hypermedia document "representation" of an "entity" that contains relevant links.  If using the HTML media type, for example, the links will be decorated with a "rel" attribute that specifies the meaning of the link-- the client should not look at the link's href, which is an opaque resource identifier.  The client then follows the opaque link to another resource which returns a new representation that is related to the current one in a manner described by the rel attribute.

What I don't understand is how the client knows what rel values to look for.  Seems that these still need to be agreed between the client and server.  How is this agreement communicated?  Is there a RESTful equivalent of, say, a WSDL?

答:
One of the requirements that should be satisfied before you can call an API 'RESTful' is that it should be possible to write a generic client application, on top of that API. With the generic client, a user should be able to access all the API's functionality. A generic client is a client application that does not assume that any resource has a specific structure beyond the structure that is defined by the media type.

In your example, the client application would offer the user the ability to follow the links. The rel attributes are displayed to the user and the client application expects the user to understand these values.

The above is known as the REST requirement to have "Hypermedia as the engine of application state" (HATEOAS). The benefit is, that the client won't break when the back end changes its rel values. Without HATEOAS, the interface is not RESTful.

Now, suppose we have a HTTP/JSON API for a web shop and we want to build a HTML/CSS/JavaScript client that gives our customers an excellent user experience. Would it be a realistic option to let that client be a generic client application? No. We want to provide a specific look-and-feel for every specific data element and every specific application state. We don't want to include all knowledge about these presentation-specifics in the API, on the contrary, the client should define the look and feel and the API should only carry the data. This implies that the client has hard-coded coupling of specific resource elements to specific layouts and user interactions.

Is this the end of HATEOAS and thus the end of REST? Yes and no.

Yes, because if we hard-code knowledge about the API into the client, we lose the benefit of HATEOAS: server-side changes may break the client.

No, for two reasons:

Being "RESTful" is a property of the API, not of the client. As long as it is possible, in theory, to build a generic client that offers all capabilities of the API, the API can be called RESTful. The fact that clients don't obey the rules is not the API's fault. The fact that a generic client would have a lousy user experience is not an issue. Why is it important to know that it is possible to have a generic client, if we don't actually have that generic client? This brings me to the second reason:

A RESTful API offers clients the option to choose how generic they want to be, i.e. how resilient to server-side changes they want to be. Clients which need to provide a great user experience may still be resilient to URI changes, to changes in default values and more. Clients doing batch jobs without user interaction may be resilient to other kinds of changes.

If you are interested in practical examples, check out my JAREST paper. The last section is about HATEOAS. You will see that with JAREST, even highly interactive and visually attractive clients can be quite resilient to server-side changes, though not 100%.


问:
Object Oriented Late Binding
In the Alan Kays Definition Of Object Oriented there is this definition that partially I don't understand:

OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme LateBinding of all things.

But what does "LateBinding" means? How can I apply this on a language like C#?
And why is this so important?

答:
Late binding refers to how objects communicate with each other. The ideal that Alan is trying to achieve is for objects to be as loosely coupled as possible. In other words that an object needs to know the minimum possible in order to communicate with another object.

Why? Because that encourages the ability to change parts of the system independently and affords it to grow and change organically.

For example, in C# you might write in a method for obj1 something like obj2.doSomething(). You can look on this as obj1 communicating with obj2. For this to happen in C#, obj1 needs to know a fair bit about obj2. It will have needed to know its class. It would have checked that the class has a method called doSomething and that there is a version of that method that takes zero parameters.

Now imagine a system where you're sending a message across a network or similar. you might write something like Runtime.sendMsg(ipAddress, "doSomething"). In this case you don't need to know a lot about the machine you're communicating with; it presumably can be contacted via IP and will do something when it receives the "doSomething" string. But otherwise you know very little.

Now imagine that's how objects communicate. You know an address and that you can send arbitrary messages to that address with some kind of "postbox" function. In this case, obj1 doesn't need to know very much about obj2, just it's address. It doesn't even need to know that it understands doSomething.

That's pretty much the crux of late binding. Now, in languages that use it, such as Smalltalk and ObjectiveC, there's usually a bit of syntactic sugar to hide the postbox function. But otherwise the idea is the same.

In C# you could replicate it, sort of, by having a Runtimeclass that accepts an object ref and a string and uses reflection to find the method and invoke it (it'll start getting complicated with arguments and return values but it would be possible though ugly).

Edit: to allay some confusion with regard to the meaning of late binding. In this answer I am referring to late binding as I understand Alan Kay meant it and implemented it in Smalltalk. It is not the more common, modern use of the term which generally refers to dynamic dispatch. The latter covers the delay in resolving the exact method until runtime but still requires some type information for the receiver at compile time.

Title: Object Oriented Late Binding


问:
Aggregate root with many fields
I'm struggling with this problem for a long time now.

Lets say that I'm trying to model some business that has Customers, Companies, Contracts, Invoices etc. All of them looks like good candidate for aggregate root for me. The problem is that, lets say Contract would have a huge list of fields, and most of them would be required. So most of the time I end up with big aggregate root (a lot of data, some logic and invariants) with huge constructors. Even though I will break that into value objects its just minimizing a damage (instead of 20 i have 7 arguments) and most of the time those value objects are super artificial, does not event make much sense business vise, they are just some kind of data containers.

Second problem is updating this data. Should I add single method to update that, lets say UpdateAllThisJunkData(,,,,,,) or write one method for each ?

I also feel that aggregates like this are more CRUD kind and don't have much to do with DDD, but I also believe that we need to persist data somehow in the system even though its not using it to process any logic, just for historical and display purpose (user can make decisions base those values). Should I let say move that kind of data to different AR or VO ie. ContractData ?

There is also option that I'm paranoid and all is fine with my AR ;)

I would appreciate any thoughts

答:
Not all data is required for every bounded context

While a Contract might indeed have a large number of fields, not every field will be relevant for every bounded context. For example, if you have an Invoice for a Customer that was created as part of a Contract it is very likely that there is information for the Contract that is not relevant for invoicing. Hence, you would have a bounded context where an Invoice has a reference back to a Contract, but where there are very few fields on the Contract because the process of invoicing has no need for this information.

If you feel like the Contract is more like a data container, I'm pretty sure this is the case. There is a bounded context where you need 20 or so fields to be entered so you have the data, but other places in your application will very likely not need these fields.

'Required' is relative

It's very easy to say that a Contract cannot exist without a huge list of fields, but are you SURE that it can't? It might not be valid to perform a certain action on a Contract if some fields are missing, but that does not mean that they have to be present when you create a contract.

Think of it this way: while you order something from an e-commerce website you might be filling out your order in several distinct steps, going through a number of screens to enter the products, shipping information, payment information, etc... The order is saved in between steps, even though it is lacking in the needed information to fullfill the order! The only difference is that before an order can be shipped it needs that information. It does not mean that it cannot be created.

Title: Aggregate root with many fields


问:
Should Repositories return IQueryable?
I have been seeing a lot of projects that have repositories that return instances of IQueryable. This allows additional filters and sorting can be performed on the IQueryable by other code, which translates to different SQL being generated. I am curious where this pattern came from and whether it is a good idea.

My biggest concern is that an IQueryable is a promise to hit the database some time later, when it is enumerated. This means that an error would be thrown outside of the repository. This could mean an Entity Framework exception is thrown in a different layer of the application.

I have also run into issues with Multiple Active Result Sets (MARS) in the past (especially when using transactions) and this approach sounds like it would lead to this happening more often.

I have always called AsEnumerable or ToArray at the end of each of my LINQ expressions to make sure the database is hit before leaving the repository code.

I am wondering if returning IQueryable could be useful as a building block for a data layer. I have seen some pretty extravagant code with one repository calling another repository to build an even bigger IQueryable.

答:
I've faced such choice as well.

So, let's summarize positive and negative sides:

Positives:

Flexibility. It's definitely flexible and convenient to allow client side to build custom queries with just one repository method

Negatives:

Untestable. (you will be actually testing the underlying IQueryable implementation, which usually your ORM. But you should test your logic instead)

Blurs responsibility. It's impossible to say, what that particular method of your repository does. Actually, it is a god-method, that can return anything you like in your entire database

Unsafe. With no restrictions on what can be queried by this repository method, in some cases such implementations can be unsafe from security point.

Caching problems (or, to be generic, any pre- or post-processing problems). It is generally unwise to, for example, cache everything in your application. You will try to cache something, that cause your database significant load. But how to do that, if you have only one repository method, that clients use to issue virtually any query against the database?

Logging problems. Logging something at the repository layer will make you inspect IQueryable first to check should this particular call be logged or not.

I would recommend against using this approach. Consider instead creating several Specifications and implement repository methods, that can query database using those. Specification pattern is a great way to encapsulate a set of conditions.

Title: Should Repositories return IQueryable?


问:
What parameters to use to compare GUI frameworks / toolkits?
I'm doing some research on the best GUI toolkit to use for future products at the company. We're talking about a fairly large organizations with quite a bit of code and a complete rewrite project in planning. Don't ask.

Anyway, I'm trying to create a list relevant parameters to judge the toolkits. What would you use to drive the comparison?

Here's what I've got so far:

Maturity

Ease of development

Ease of prototyping

Ease of maintenance

Size of hiring pool

Available knowledge at the company

Training costs

Community size

Community level of expertise (how hard to find good answers to complex problems)

Amount of expert-level books available

Ability to interface to other technologies

Deployment considerations

Visual aesthetics

Ability to access OS resources

Multiple monitor support (something that might come in handy in our particular application)

答:
I would add extensibility and versatility (or power) because I consider it different from "ease of development". YMMV.

For ex QT3 (or was it the version before that Oh Well) is very easy to use as compared to MFC for typical cases, but where you wanted to do something atypical it came up short. I hear they have changed this in QT4.

Good list BTW.