答:
Why not make FromYouTubeAsync be a pure function (e.g. which takes a String representing the HTML of the youtube video page, and which returns the URL of the mp4 video file it finds within), and then have the caller worry about how exactly to download bytes over the internet, if you think they want to have as much control over the process as you're implying?

You could always provide one default implementation using HttpClient for those clients who don't really care how it gets downloaded and just want as low-effort an API as possible.

Title: Using delegates to avoid duplicate creation of resources

Body:
I'm writing a PCL that uses an HttpClient to go visit a few sites and extract data from them. My initial code looked like this:

```<code>public static class Download
{
    public async static Task<byte[]> FromYouTubeAsync(string videoUri)
    {
        using (var client = new HttpClient())
        {
            string source = await client
                .GetStringAsync(videoUri);

            // get links...

            return await client
                .GetByteArrayAsync(links.First());
        }
    }
}
</code>```

I realized, however, that this would be a waste of resources if the user of my library already had a HttpClient in hand. For example:

```<code>// Caller code
using (var client = new HttpClient())
{
    // Do some work with client

    byte[] bytes = await Download.FromYouTubeAsync("some://uri"); // A second HttpClient is created here, wasting resources and time...

    // Do some work with client and bytes
}
</code>```

One solution could be to add an overload taking an HttpClient as a parameter, but what if the user wanted to use a WebClient instead? Or an HttpWebRequest? Or some other kind of client that wasn't availsble in the PCL?

My solution

I decided to take delegate parameters to get the page source and to download the file based on the URI. My code now looks like this:

```<code>public static class Download
{
    public async static Task<byte[]> FromYouTubeAsync(string videoUri)
    {
        using (var client = new HttpClient())
        {
            return await FromYouTubeAsync(
                () => client
                .GetStringAsync(videoUri),
                uri => client
                .GetByteArrayAsync(uri));
        }
    }

    public async static Task<byte[]> FromYouTubeAsync(
        Func<Task<string>> sourceFactory, Func<string, Task<byte[]>> downloadFactory)
    {
        string source = await sourceFactory();

        // get links...

        return await downloadFactory(links.First());
    }
}
</code>```

The problem

Hm. Wait, what if the caller is using a client that doesn't support async, like HttpWebRequest? Better add another overload taking a Func<string> and a Func<string, byte[]>. What if s/he wants to make the first call synchronous, but the second call async? Or vice versa? OK, just add 2 more overloads. Wait, what if the caller wants the whole operation to be synchronous? Let's go add 4 Download.FromYouTube overloads wrapping each of the async methods. Whoops, it turns out we need to visit another page of YouTube in case the video's signature is encrypted! Sure, let's go add 8 mor-

I think you can see where this is going. How do I maintain this flexibility without scaring the user away when Visual Studio says that there are 17 different overloads for this method?


问:
How do I distinguish derived code from original code?
I'm working on a Python project which extends the functionality of some other python modules. Some are part of the standard Python library and mostly use the PSF License agreement while some are provided by a third-party and mostly use BSD licenses. All allow redistribution of modified code with attribution.

I understand that in most cases I need to include the license in the source code or some place where it is easily reference. My question concerns how best to separate modified code from the rest of the project since the rest of the project is unrelated and will carry a different license and copyright.

As far as I can tell, there are two ways to do this, in-line or by separating out derived code into it's own modules. In-line could be done through code comments or within class or function doc strings. The other option creates number of small, but technically unneeded modules, but would make it easier to distinguish derived code from the rest of the project and deprecate them if the modules later incorporated the missing capabilities.

Are these both valid options and is there a preferred or more Pythonic way?

答:
Generally speaking, you should not be modifying the source code files of third party code unless they're the type of changes for which it would make sense to contribute them back upstream (e.g. bug fixes).

Obviously, there are exceptional situations where you may wish to go ahead and modify those files anyway, but you usually want to avoid forking the project otherwise you'll suffer when you need to merge in new updates from upstream.

Make your extensions without actually modifying their code, and you'll sidestep this whole question.

OwnerDisplayName: aviso

ContentLicense: CC BY-SA 3.0


问:
What is a good design for allowing backwards compatibility of files between different versions of software?
What is a good design for allowing backwards compatibility of a file type between different versions of software?

For instance, how does microsoft get word 2007, 2010 and 2013 etc... to all open docx files, but different editions can save more / less data and save the data in slightly different ways, all to the same file type, and a file that is saved in one version can be opened in another, but certain elements of the file might not be available in older versions?

I mean, the really obvious way to do it is to have something like

```<code>private string openfile(string filename)
{
    File.Open(filename)

    ... some logic that gets a header from the file that will never change

    switch (fileversion)
        case 2007:
            .....
        case 2010
            .....
        case 2013
            .....
}
</code>```

but that seems incredibly monolithic, not very extensible, and likely to lead to a lot of copy / pasted code.

So I was thinking of using a base interface for all versions which defines the immutable structures, such as the header, that need to be present in the file, and methods that need to be available for serialisation / deserialisation, then multiple inheritance so that each new version's class that implements the interface inherits the old version, and only overrides stuff that has changed, since the file will be the same, for the most part.

I'm not really bothered about the structure of the file, since it's already decided that we'll be using XML, and the initial schema is, by and large, already decided. However there will no doubt be changes to it in the future, and I just want to be able to design the code in a way that makes it easy to accommodate these changes.

答: A way of doing this can be by using a base class and interface with the basic functions for your file handling. Then use classes for each version that extend from the base class to handle all version specific cases. Functions that can change can be virtual in you base class of abstract if there are only version specific implementations. When you need a class to handle the file, use a factory that gets the version specific implementation of the file handling interface.</p>


问:
What's special about "non-blocking" node.js?
In the short chapter, What is Node by McLaughlin, he writes:

"Node has no blocks, no threads competing for the same resource (Node
  is happy to just let things happen however they happen), nothing that
  has to start up upon request. Node just sits around waiting (quite
  literally; unused Node responders are sleeping). When a request comes
  in, it’s handled. This results in very fast code, without uber-
  programmers writing the server-side behavior."

While I think he's picking on PHP here - from what I understand there isn't a way for PHP to not call up new references to its databases, files, etc, whenever any php page is loaded - is this really different than other web technologies? Consider Django/Flask for example or a simple Python threaded-server using builtin methods - is it really any different than the no-blocks advantages of Node? You run a program and it binds a socket, running the function only as needed. (Doesn't Java also do this?)

答:
Let's immediately get the Turing-completeness disclaimer out of the way and say any language can probably approximate any runtime feature of any other language. Good? Good.

The main difference between the Node.js approach and a Python threaded-server (or a typical Java HTTP server implementation) is that Node.js is single threaded while the latter two are multithreaded. More specifically, the latter two will typically dedicate one thread per request. If, in the handling of that request, you need to do something slow like read from harddrive or connect to a remote database, the thread will sleep until the slow thing is done and the rest of the business logic is ready to proceed. In contrast, the Node.js approach is to schedule callbacks that are to be invoked once the slow thing is done; Node.js' single thread is never sleeping except if there are literally no requests to process.

The main difference between Node.js and PHP is that in Node.js, the code runs in a persistent context that exists as long as your Node.js server is running. So for example, if you write a value to a global variable in one request, and then read out the value of the global variable in another request, the read will see the value that was written by the write. In contrast, in PHP, a new context is created for each request, and so writes to globals in one request are lost when the script handling the request terminates.


问:
How to avoid tying these two methods together with global mutable state?
I'm in the process of reading Code Complete 2 and learned that using global state is a no-no.  What can I do to avoid using global state in this situation?

Some background information:  I have two different methods (formAdded and formRemoved), each of which is called depending on whether an Add button is clicked or a Remove button is clicked.  timesSignerIsRequired is a class variable, and these two methods are tied together by it, which seems bad.  Here is my code.

```<code>formAdded: function(ev) {
    var formId = $(ev.currentTarget).data('formid');
    var self = this;
    var forms = Forms;
    $.each(forms, function(index, form) {
        if (form.id == formId) {
            //found this form - now find all signer ID's required with the current accountTypeId
            var requiredSigners = form[this.appModel.get('accountTypeId')];
            for (var i = 0; i < requiredSigners; i++) {
                self.timesSignerIsRequired[i] += 1;

                if (!signerIsRendered(i)) {
                    //render signer into view
                }
            }
        }
    });
}

formRemoved: function(ev) {
    var formId = $(ev.currentTarget).data('formid');
    var self = this;
    var forms = Forms;
    $.each(forms, function(index, form) {
        if (form.id == formId) {
            //found this form - now find all signer ID's required with the current accountTypeId
            var requiredSigners = form[self.appModel.get('accountTypeId')];
            for (var i = 0; i < requiredSigners; i++) {
                self.timesSignerIsRequired[i] -= 1;

                if (self.timesSignerIsRequired[i] <= 0) {
                    //remove signer from the view
                }
            }
        }
    });
}
</code>```

Any suggestions?

答: timesSignerIsRequired is not global state, it's a field on your object. It's okay for the methods of an object to access the fields (including private fields) of that same object.


问:
Dealing with data containing integer overflow, rollover
I am writing a small matlab function to analyze data from a laser probe.

The data to be analyzed is basically:
x-data: Position.
y-data: Laser (light) intensity.

The sensor automatically changes the light intensity data from millivolts to arbitrary Intensity Units which go from 0 to 255. I noticed that with intense signals, the 256 range can be "rolled-over" several times.

Now, when the data rolls over just once, it is possible and easy to correct the variable overflow.

To do so, I am using this code:

```<code>Index255= find(UsefulData>=247);           % On intense-enough signals, the last data before breaking the 255 cap can be relatively low. (e.g. 249,261 --> 249,6)
ModeData = mode(UsefulData);                % To help with discerning if data does reset from zero
for In=1:1:length(Index255)-1
    RegionAverage = mean(UsefulData(Index255(In):Index255(In+1)));
    DataQuantiles = quantile(UsefulData,10);
    if RegionAverage < DataQuantiles(1)     % If the mean of the reigon is lower than the mode (assumed to be the base), then it's obviously under it.
             UsefulData(Index255(In)+1:Index255(In+1)-1)=UsefulData(Index255(In)+1:Index255(In+1)-1)+255;
    else
        continue
    end
end
</code>```

It might not be elegant, but it gets the job done (when only ONE integer overflow takes place).

However, I was not expecting a repeated variable overflow, and I am out of ideas on how to correct it.

How should this data be handled?

答:
I solved it. Again, it does not look elegant, but gets the job done.   </p>

```<code>while h<length(UsefulData)
       Diff=abs(UsefulData(h)-UsefulData(h+1))
       if abs(UsefulData(h)-UsefulData(h+1))>190
           UsefulData(h+1)=UsefulData(h+1)+256;
           plot(UsefulData(h-100:h+300), '-r'), hold on
           plot(101,UsefulData(h),'*','MarkerEdgeColor','b'), hold off
           continue
       end
       h=h+1;
end
</code>```

Body:
I solved it. Again, it does not look elegant, but gets the job done.

while h<length(UsefulData)
       Diff=abs(UsefulData(h)-UsefulData(h+1))
       if abs(UsefulData(h)-UsefulData(h+1))>190
           UsefulData(h+1)=UsefulData(h+1)+256;
           plot(UsefulData(h-100:h+300), '-r'), hold on
           plot(101,UsefulData(h),'*','MarkerEdgeColor','b'), hold off
           continue
       end
       h=h+1;
end


问:
How does condition_variable::notify_one() choose which thread to unblock?
notify_one() of C++ 11 thread library is used to unblock one of the waiting threads.

How does it choose which thread to unblock?

To begin with, I tried googling but could not find any appropriate answer (even on Stack Exchange network), though I read somewhere that the selection of thread is "unspecified".

So I decided to checkout the C++ specification and referred Section 30.5.1 of November 2014 working draft (available for free), it said

Effects: If any threads are blocked waiting for *this, unblocks one of those threads.

Clearly, even the specification does not mention what the selection is based on, then I decided to put up this question.

答:
Arbitrarily. The handles of waiting threads are pushed into a container and on notify_one it will pick one, wake it and remove it. Usually the one most easily found in the structure.

If you specify that the condition is fair then it's the oldest one. Often selected by keeping an ordered ring buffer that can grow as needed.


问:
Reducing boilerplate in class that implements interfaces through composition
I have a class: A that is a composite of a number of smaller classes, B, Cand D.

B, C, and D implement interfaces IB, IC, and ID respectively.

Since A supports all the functionality of B, C and D, A implements IB, IC and ID as well, but this unfortunately leads to a lot of re-routing in the implementation of A

Like so:

```<code>interface IB
{
    int Foo {get;}
}

public class B : IB
{
    public int Foo {get {return 5;}}
}

interface IC
{
    void Bar();
}

public class C : IC
{
    public void Bar()
    {
    }
}

interface ID
{
    string Bash{get; set;}
}

public class D: ID
{
    public string Bash {get;set;}
}

class A : IB, IC, ID
{
    private B b = new B();
    private C c = new C();
    private D d = new D();

    public int Foo {get {return b.Foo}}

    public A(string bash)
    {
        Bash = bash;
    }

    public void Bar()
    {
        c.Bar();
    }

    public string Bash
    {
         get {return d.Bash;}
         set {d.Bash = value;}
    }
}
</code>```

Is there a way to get rid of any of this boilerplate redirection in A? B, C and D all implement different but common functionality, and I like that A implements IB, IC and ID because it means I can pass in Aitself as a dependency matching those interfaces, and don't have to expose the inner helper methods.

答:
While it doesn't reduce boilerplate in the code itself, Visual Studio 2015 now comes with a refactoring option to automatically generate the boilerplate for you.

To use this, first create your interface IExample and implementation Example. Then create your new class Composite and make it inherit IExample, but do not implement the interface.

Add a property or field of type Example to your Composite class, open the quick-actions menu on the token IExample in your Composite class file and select "Implement interface through 'Example'" where 'Example' in this case is the name of the field or property.

You should note that while Visual Studio will generate and redirect all methods and properties defined in the interface to this helper class for you, it will not redirect events as of the time this answer was posted.


问:
Do binary trees serve a specific purpose in storing hierarchical data? What is their canonical use?
I understand the structure of binary trees and how to traverse them. However, I am struggling to realize their actual uses, purposes in programs and programming. When I think about 'real life' examples of hierarchical data they almost certainly have more than 2 children. For example, in a family tree, a mother may often have more than two children.

Are 'binary trees' really only useful to store linearly related data due to the faster processing times over arrays and lists? Alternatively, do they serve a specific purpose in storing hierarchical data? If so, what examples are there of the application of binary trees. What data is such that a node has at most 2 children?

答:
Any tree structure, where a node can have unlimited numbers of children, can be implemented using a binary tree.

For each node in your tree, replace it with a node with a right and left pointer. The left pointer goes to the first of the node's children. The right node goes to the next sibling of the node. All the children of a given node are in a linked list joined by their right pointers, with the head of the list pointed to by the left pointer of their parent.

Your complicated, n-ary tree has become a simple, binary tree.

I am sure this is in Knuth, Vol. 1 somewhere.


答: You might have a look at the PNG file format and how it handles version compatibility. Every block has an id describing what kind of block it is, and it has some flags that tell the software what to do if it cannot understand that id. For example "you cannot read the file if you don't understand this block", or "you can read the file but not modify it", or "you can modify the file but you have to delete this block". For backward compatibility, your software just needs to handle the situation when any expected data is not present. </p>

Title: What is a good design for allowing backwards compatibility of files between different versions of software?

Body:
What is a good design for allowing backwards compatibility of a file type between different versions of software?

For instance, how does microsoft get word 2007, 2010 and 2013 etc... to all open docx files, but different editions can save more / less data and save the data in slightly different ways, all to the same file type, and a file that is saved in one version can be opened in another, but certain elements of the file might not be available in older versions?

I mean, the really obvious way to do it is to have something like

```<code>private string openfile(string filename)
{
    File.Open(filename)

    ... some logic that gets a header from the file that will never change

    switch (fileversion)
        case 2007:
            .....
        case 2010
            .....
        case 2013
            .....
}
</code>```

but that seems incredibly monolithic, not very extensible, and likely to lead to a lot of copy / pasted code.

So I was thinking of using a base interface for all versions which defines the immutable structures, such as the header, that need to be present in the file, and methods that need to be available for serialisation / deserialisation, then multiple inheritance so that each new version's class that implements the interface inherits the old version, and only overrides stuff that has changed, since the file will be the same, for the most part.

I'm not really bothered about the structure of the file, since it's already decided that we'll be using XML, and the initial schema is, by and large, already decided. However there will no doubt be changes to it in the future, and I just want to be able to design the code in a way that makes it easy to accommodate these changes.


答:
Binary trees why use them?

In programming you work a lot with collections of same type data.

The two basic ways of storing this data are : linked lists and arrays.

They both come with up and downsides:
In a linked list it's easy to add elements at any position or remove elements. But access to a specific element is harder, because you have to go through the list until you're at the element you want.

It doesn't search efficiently but inserting and deleting is easy.

With an array access to a specific element is easy, but it's harder to insert or delete an element because inserting means: extend array by one, shift all elements before the insert position 1 to the right and insert the element.

It searches efficiently (if sorted) but inserting and deleting is hard.

So both the linked list and the array have downsides.

Binary trees are made to tackle both problems of the array and the linked list:

Easy insert and delete

Easy searching

So binary tree are made for when you have lots of data which changes regularly.

Title: Do binary trees serve a specific purpose in storing hierarchical data? What is their canonical use?

Body:
I understand the structure of binary trees and how to traverse them. However, I am struggling to realize their actual uses, purposes in programs and programming. When I think about 'real life' examples of hierarchical data they almost certainly have more than 2 children. For example, in a family tree, a mother may often have more than two children.

Are 'binary trees' really only useful to store linearly related data due to the faster processing times over arrays and lists? Alternatively, do they serve a specific purpose in storing hierarchical data? If so, what examples are there of the application of binary trees. What data is such that a node has at most 2 children?


问:
How are generics implemented?
This is the question from compiler internals perspective.

I am interested in generics, not templates (C++), so I marked the question with C#. Not Java, because AFAIK the generics in both languages differ in implementations.

When I look at languages w/o generics it is pretty straightforward, you can validate the class definition, add it to hierarchy and that's it.

But what to do with generic class, and more importantly how handle references to it? How to make sure that static fields are singular per instantiations (i.e. each time generic parameters are resolved).

Let's say I see a call:

```<code>var x = new Foo<Bar>();
</code>```

Do I add new Foo_Bar class to hierarchy?

Update: So far I found only 2 relevant posts, however even they don't go into much details in sense "how to do it by yourself":

http://www.jprl.com/Blog/archive/development/2007/Aug-31.html

http://www.artima.com/intv/generics2.html

答:
But what to do with generic class, and more importantly how handle references to it?

The general way in the front end of the compiler is to have two sorts of type instances, the generic type (List<T>) and a bound generic type (List<Foo>). The generic type defines what functions exist, what fields, and has generic type references wherever T is used. The bound generic type contains a reference to the generic type, and a set of type arguments. That has enough information for you to then generate a concrete type, replacing the generic type references with Foo or whatever the type arguments are. This sort of distinction is important when you're doing type inference and need to infer List<T> versus List<Foo>.

Instead of thinking of generics like templates (which build out various implementations directly), it may be helpful to instead think of them like functional language type constructors (where the generic arguments are like arguments into a function that gives you a type).

As for the back end, I don't really know. All of my work with generics has targeted CIL as the backend, so I could compile them into the supported generics there.

Title: How are generics implemented?


问:
How can a team apply the Scrum methodology without a clear customer?
Our team is trying to understand and adapt Scrum and other agile practices, but we can not figure out how to deal with customer feedback when there is no customer. Every document about the subject emphasizes how important is having the customer involved in every sprint and how having early feedback helps correcting problems fast and maximizes satisfaction to both sides. I understand clearly this point.

In our case, we have no single customer. We develop a website and a smartphone app for an already established and growing audience. I am sure this is a fairly common case so I would like to know some real world experience about how to apply and manage Scrum in this case. Do you just decide all features by yourselves or do incorporate user testing in the sprint? Any other solution?

答:
Depending on who your customer base is, and how much information you have about them (or can derive about them using companies like Lexis Nexis), one common way of addressing this dilemma is the use of "personas".  Basically you build representatives of your customers (some places actually create big cardboard mock-ups) based on data you have.  You name these personas.  You keep collecting information about their likes and wants.  Divide your customer base up into groups/tranches, and have a persona for each tranche.  If possible, you locate the real customers who most closely match each tranche and interview them.  Then, a person on the team tries to represent each persona.

It's imperfect, and it works better for information architecture (UI and stuff) than for deeper things, but it's at least an approach that has some value.  It's been around for at least 10 years now.

OwnerDisplayName: jordi


问:
In HTML and CSS what are the standard tab sizes?
I searched on Google but couldn't find an answer. I'd like to know what are the standard tab sizes for HTML and CSS. Is it 2 or 4?</p>

答:
Tabs have no innate size, their interpretation is up to the observer. If you're the observer, you get to decide how you want to view your tabs, or if you're using other kinds of whitespace, however many you want to have.

The syntax in the CSS specification uses [ \t\r\n\f]+ (one or more of the bracketed things) for whitespace, implying that the kind of whitespace is irrelevant.

Similarly, HTML5 defines space characters as:

```<code>Any of the following:
U+0020 SPACE
U+0009 CHARACTER TABULATION (tab)
U+000A LINE FEED (LF)
U+000C FORM FEED (FF)
U+000D CARRIAGE RETURN (CR)
</code>```

Body:
Tabs have no innate size, their interpretation is up to the observer. If you're the observer, you get to decide how you want to view your tabs, or if you're using other kinds of whitespace, however many you want to have.

The syntax in the CSS specification uses [ \t\r\n\f]+ (one or more of the bracketed things) for whitespace, implying that the kind of whitespace is irrelevant.

Similarly, HTML5 defines space characters as:

Any of the following:
U+0020 SPACE
U+0009 CHARACTER TABULATION (tab)
U+000A LINE FEED (LF)
U+000C FORM FEED (FF)
U+000D CARRIAGE RETURN (CR)


答: As I understand you're asking about indentation. There is currently no standard concerning indentation in HTML and CSS files. Nor should there be any. Let your editor software worry about the indentation.</p>

Title: In HTML and CSS what are the standard tab sizes?

Body: I searched on Google but couldn't find an answer. I'd like to know what are the standard tab sizes for HTML and CSS. Is it 2 or 4?</p>


问:
Where should PDOExceptions be dealt with?
I wrote a database wrapper class to help with queries and I'm trying to figure the best way to implement try-catch blocks.

In my database class, I have a helper function;

```<code>public function fetchAll($sql, $bind = array()) {

        $stmt = $this->query($sql, $bind);
        $result = $stmt->fetchAll($this->_fetch_mode);

        return $result;
    }
</code>```

that I can call outside of the class:

```<code>$this->db->fetchAll($sql,[$var1,$var2]);
</code>```

My question is where to put the try-catch blocks.  It would be "easier" if I just put them inside of my database class:

```<code>    public function fetchAll($sql, $bind = array()) {

            $stmt = $this->query($sql, $bind);
            try {
                  $result = $stmt->fetchAll($this->_fetch_mode);
                } catch (PDOException $e) {
                   //log the error...give some message
                }

            return $result;
        }
</code>```

instead of wrapping each call to the database class:

```<code>try {
     $this->db->fetchAll($sql,[$var1,$var2]);
    } (catch PDOException $e) {
     //log the error...give some message
    }
</code>```

However, my feeling (without being able to come up with a concrete example...) is that even though it would be more work to wrap each call to the database class, if I do hard code the try-catch block in my database class, it won't be as flexible in terms of handling errors.

Or, would it be better to have a "catch-all" in my index.php (I use MVC) to catch general exceptions (for example, PDO related), and then if I need more specific ones in my application (for example, those thrown when connecting to 3rd party services), then add additional try-block catches for them:

In my index.php, which all requests pass through via my .htaccess file:

```<code>try {
    $db = new Db();
    $db->connect(['host'=>DB_HOST,'dbname'=>DB_NAME,'username'=>DB_USERNAME,'password'=>DB_PASSWORD]);

    $bootstrap = new Bootstrap();

    $bootstrap->setControllerPath('application/controllers');
    $bootstrap->setModelPath('application/models');

    $bootstrap->init(); //load the controller/model/classes
} (catch Exception $e) {
    //log error
    //if ajax request return json_encoded message other redirect to error page
}
</code>```

答:
I would argue it's impossible to handle all exceptions the same way. I think you should let it propagate back to the calling code as only that ultimately knows how to handle the exceptional situation.

As an example imagine you have an blog. Every hour you query the database for new articles to populate a cache. But luck isn't with you and the database is temporarily unavailable. You could let the exception go unhandled and the whole site is down. You could also handle the exception inside the database abstraction layer and return a default result (effectively hiding the exception). But returning a default value would overwrite the cache with nothing. The site is up, but doesn't work properly. You could also log the error, rethrow the exception to let the calling code decide what to do. The calling code could reuse the already cached query results and extend the lifetime of the cache another hour hoping for the database to come back up. This way your users would experience minimal disruption of your site and may never even notice you had problems.

But it isn't always easy to determine what to do in exceptional cases and sometimes it would preferable to just let the exception bubble up until it hits a generic exception handler, which displays a generic error page. You can in addition implement some general procedures if a query fails, such as error logging in this generic exception handler. This would be preferable as this can be considered boilerplate code that doesn't change anything. And having unhandled exceptions emailed to you (as an example) lets you resolve the issue faster than if you were to discover the issue yourself.

You also get an additional bonus by grouping generic-logging code in a single place. You can handle specific logging alongside relevant code (where details are available) and if your generic logging procedure should change you only need to change it once.

Using your fetchAll() method the structure could look like:

```<code>public function fetchAll($sql, $bind = array()) {

    // Do something awesome before try-catch block.

    try {

        // Execute query...

    }catch(PDOException $exception) {

        $this->logger->log('SQL query failed: ' . $exception->getMessage());

        /*
         * Re-throw the exception to let the calling code
         * handle the situation.
         */
        throw $exception

    }

    // Continue as everything executed as expected.

}
</code>```

Body:
I would argue it's impossible to handle all exceptions the same way. I think you should let it propagate back to the calling code as only that ultimately knows how to handle the exceptional situation.

As an example imagine you have an blog. Every hour you query the database for new articles to populate a cache. But luck isn't with you and the database is temporarily unavailable. You could let the exception go unhandled and the whole site is down. You could also handle the exception inside the database abstraction layer and return a default result (effectively hiding the exception). But returning a default value would overwrite the cache with nothing. The site is up, but doesn't work properly. You could also log the error, rethrow the exception to let the calling code decide what to do. The calling code could reuse the already cached query results and extend the lifetime of the cache another hour hoping for the database to come back up. This way your users would experience minimal disruption of your site and may never even notice you had problems.

But it isn't always easy to determine what to do in exceptional cases and sometimes it would preferable to just let the exception bubble up until it hits a generic exception handler, which displays a generic error page. You can in addition implement some general procedures if a query fails, such as error logging in this generic exception handler. This would be preferable as this can be considered boilerplate code that doesn't change anything. And having unhandled exceptions emailed to you (as an example) lets you resolve the issue faster than if you were to discover the issue yourself.

You also get an additional bonus by grouping generic-logging code in a single place. You can handle specific logging alongside relevant code (where details are available) and if your generic logging procedure should change you only need to change it once.

Using your fetchAll() method the structure could look like:

public function fetchAll($sql, $bind = array()) {

    // Do something awesome before try-catch block.

    try {

        // Execute query...

    }catch(PDOException $exception) {

        $this->logger->log('SQL query failed: ' . $exception->getMessage());

        /*
         * Re-throw the exception to let the calling code
         * handle the situation.
         */
        throw $exception

    }

    // Continue as everything executed as expected.

}

ContentLicense: CC BY-SA 4.0


问:
What is your favorite whiteboard interview problem?
Just as the title says, what is your favorite whiteboard interview problem, and why has it proven effective for you?

Junior, senior, Java, C, Javascript, PHP, SQL, pseudo-code, etc.

答:
My favourite was one a friend of mine used.

Write me a function to generate/print/store the first "n" prime numbers and then explain how it works and how efficient it is.

It works well because:

It's an algorithmic question so it requires the interviewee to be able to think and then explain their thinking - so you can see how their brain works.

It's language independent.

Hardly anyone gets it completely right (there's usual an edge case they miss (1 or 2 normally), or they don't handle negative numbers, so you get to see how they handle bugs and being told that they're wrong.

Most do it as a simple but very slow sieve (e.g. 80% of people will check of n is a prime by dividing n by all integers less than n), which gives you lots of scope for conversations about how they could improve the algorithm based on space/time trade offs e.g. "why are you dividing a number by 4 if you already know it's not divisible by 2?" or "You've worked out that you only need to divide by all prime numbers less than sqrt(n), but that requires you to store those numbers somewhere, so what are the implications of that?")

There's no need for them to get the answer right. If someone can think and explain their thinking then they're a long way down the road for being a good candidate.

ContentLicense: CC BY-SA 2.5


问:
Using delegates to avoid duplicate creation of resources
I'm writing a PCL that uses an HttpClient to go visit a few sites and extract data from them. My initial code looked like this:

```<code>public static class Download
{
    public async static Task<byte[]> FromYouTubeAsync(string videoUri)
    {
        using (var client = new HttpClient())
        {
            string source = await client
                .GetStringAsync(videoUri);

            // get links...

            return await client
                .GetByteArrayAsync(links.First());
        }
    }
}
</code>```

I realized, however, that this would be a waste of resources if the user of my library already had a HttpClient in hand. For example:

```<code>// Caller code
using (var client = new HttpClient())
{
    // Do some work with client

    byte[] bytes = await Download.FromYouTubeAsync("some://uri"); // A second HttpClient is created here, wasting resources and time...

    // Do some work with client and bytes
}
</code>```

One solution could be to add an overload taking an HttpClient as a parameter, but what if the user wanted to use a WebClient instead? Or an HttpWebRequest? Or some other kind of client that wasn't availsble in the PCL?

My solution

I decided to take delegate parameters to get the page source and to download the file based on the URI. My code now looks like this:

```<code>public static class Download
{
    public async static Task<byte[]> FromYouTubeAsync(string videoUri)
    {
        using (var client = new HttpClient())
        {
            return await FromYouTubeAsync(
                () => client
                .GetStringAsync(videoUri),
                uri => client
                .GetByteArrayAsync(uri));
        }
    }

    public async static Task<byte[]> FromYouTubeAsync(
        Func<Task<string>> sourceFactory, Func<string, Task<byte[]>> downloadFactory)
    {
        string source = await sourceFactory();

        // get links...

        return await downloadFactory(links.First());
    }
}
</code>```

The problem

Hm. Wait, what if the caller is using a client that doesn't support async, like HttpWebRequest? Better add another overload taking a Func<string> and a Func<string, byte[]>. What if s/he wants to make the first call synchronous, but the second call async? Or vice versa? OK, just add 2 more overloads. Wait, what if the caller wants the whole operation to be synchronous? Let's go add 4 Download.FromYouTube overloads wrapping each of the async methods. Whoops, it turns out we need to visit another page of YouTube in case the video's signature is encrypted! Sure, let's go add 8 mor-

I think you can see where this is going. How do I maintain this flexibility without scaring the user away when Visual Studio says that there are 17 different overloads for this method?

答:
After thinking about it overnight and looking at Nebu's answer, here are the solutions I came up with:

Return only the URI, and accept one a/sync Func. While I do think it's reasonable for an API to return the URI to a video (in case the user doesn't want to download it just yet), I think it's a bit technical to ask him/her to download the source of the page and would tempt me to make more overloads (i.e. SourceToBytes, SourceToUri).

Make the HTML source downloader accept a string as a URI. In other words, instead of () => client.GetStringAsync("some://uri"), it would look like uri => client.GetStringAsync(uri). The reason I say this is because doing so would prevent me from having to provide 8 more overloads asking how to download the string from that other page on YouTube (in case the video's signature is encrypted), when s/he has already said it's client.GetStringAsync. TL;DR: This  would prevent me from having to create 8 more overloads.

Move all of the Func methods into an AdvancedDownload class. This will help not scare the user away when he clicks on the overloads for the method.

EDIT: After some more complications I also decided to wipe all of the static methods and inherit all except one of them from a base class. Despite having ~15 different boilerplate methods in the base, now I only have to override one method each time I want to expand to a new website (i.e. Vimeo).

Thanks @DanielS and @NebuPookins for the suggestions!


问:
What's the quickest and most maintainable way to implement SQL querying of disparate C++ variables?
I've recently joined a team working on radar observations.  The team has access to a repository of software (written in C++) used for dealing with these observations, but their work is focused towards research, not software development.

There are various variables associated to reading and analysing radar information, and they are all currently stored as local variables (somewhere) in the aforementioned C++ software.  It is common for a researcher to want to query some of these variables for their research.  Today, I found out that the standard practice here is to check out a new branch of the code, modify it to print out what you want, and then rebuild and run the code locally.  This modified code is never checked back in, since it is too localized to be useful to anyone else.

To me, this looks like very bad practice (though I've not been developing software that long, so maybe one of you will correct me).  I have always held the opinion that version control (they used a modified form of SVN) should be used primarily for making changes to the code that you intend to check back in, and should never be used for making one-off queries.  That is what SQL is for.

Unfortunately, I am not at all sure how easy it will be to implement an SQL front end for this program.  At the moment, all the information is stored away in local C++ variables in various modules throughout the program.  I have had two ideas for how to fix this:

Write a function (probably involving some rewriting of the existing code) that writes all of the information that might be useful into a database in some piece of database software, using standard libraries.  Then users can run this program over some data, and write SQL queries on the resulting database, using the database software's built-in parser.

The good thing about this approach is that the SQL side of things (as opposed to the database side) is entirely handled by existing software.  The downside is that it requires users to build the entire database even if they only want a small query.  The team frequently deals with large amounts of data coming in on an hourly basis, and it might be impractical to keep having to build enourmous databases when you're only interested in small queries.  The other problem is that the organization as a whole is reluctant to install new software on company computers, and it might be difficult to get appropriate database software installed.

Use a standard SQL parser library for C++ to query that somehow queries the relevant local variables in the C++ program as if they were stored in a database.  So if I passed in the SQL query

```
SELECT RadarID, Height, Signal Strength, Wavelength FROM RadarObservations
WHERE Date = '03/07/2015'
```

then the program would give me out all the specified fields for radar observations on 03/07/2015, without creating the whole database, but getting the values directly from the local variables in C++ where they are stored.

Of the two, option (2) looks the more attractive, but I doubt that it's even possible.  Has anyone any experience with either option, or can anyone suggest anything different?  One course of action might be to have a rolling build that builds the databases each day so people can query them, but people here are interested in historical data too, and holding on to all these databases might pose storage issues.

答:
Why not put configurable text logging into the C++ product.  You can then write a separate program (in any language) to consume the log offline, or in real time, and populate a SQL database. This will decouple the SQL/DB requirement from your C++ code base, and you won't have to compile & link against or even install SQL on most machines.

For the product, concentrate on defining a consistent and easy to use configuration to enable logging of the various bits of interest, and, make a consistent and easy to parse logging format for all those bits.  (These log entries could look like events, maybe: time-stamp, subject, property, value.)


答:
You shouldn't catch the PDOException at all.

Now, you should have some sort of last resort exception handler that catches all exceptions and logs them. PDOExceptions should be caught there. But you shouldn't catch them anywhere else.

In my understanding, PDOException indicates either that something has gone very wrong with the connection to the database, or you wrote bad SQL. Its only useful to attempt to handle the exception if you can do something useful as an alternative. But if you can't get the information from the database, your application is probably useless, and there is little point in trying to recover.

Title: Where should PDOExceptions be dealt with?

Body:
I wrote a database wrapper class to help with queries and I'm trying to figure the best way to implement try-catch blocks.

In my database class, I have a helper function;

```<code>public function fetchAll($sql, $bind = array()) {

        $stmt = $this->query($sql, $bind);
        $result = $stmt->fetchAll($this->_fetch_mode);

        return $result;
    }
</code>```

that I can call outside of the class:

```<code>$this->db->fetchAll($sql,[$var1,$var2]);
</code>```

My question is where to put the try-catch blocks.  It would be "easier" if I just put them inside of my database class:

```<code>    public function fetchAll($sql, $bind = array()) {

            $stmt = $this->query($sql, $bind);
            try {
                  $result = $stmt->fetchAll($this->_fetch_mode);
                } catch (PDOException $e) {
                   //log the error...give some message
                }

            return $result;
        }
</code>```

instead of wrapping each call to the database class:

```<code>try {
     $this->db->fetchAll($sql,[$var1,$var2]);
    } (catch PDOException $e) {
     //log the error...give some message
    }
</code>```

However, my feeling (without being able to come up with a concrete example...) is that even though it would be more work to wrap each call to the database class, if I do hard code the try-catch block in my database class, it won't be as flexible in terms of handling errors.

Or, would it be better to have a "catch-all" in my index.php (I use MVC) to catch general exceptions (for example, PDO related), and then if I need more specific ones in my application (for example, those thrown when connecting to 3rd party services), then add additional try-block catches for them:

In my index.php, which all requests pass through via my .htaccess file:

```<code>try {
    $db = new Db();
    $db->connect(['host'=>DB_HOST,'dbname'=>DB_NAME,'username'=>DB_USERNAME,'password'=>DB_PASSWORD]);

    $bootstrap = new Bootstrap();

    $bootstrap->setControllerPath('application/controllers');
    $bootstrap->setModelPath('application/models');

    $bootstrap->init(); //load the controller/model/classes
} (catch Exception $e) {
    //log error
    //if ajax request return json_encoded message other redirect to error page
}
</code>```


问:
Step-by-step functional testing automation
I have a basic class in C# from which I create inherited classes for databinding scenarios. You can think of it as a substitute for .NET's DataRow class.

I want to automate testing of a typical row's lifetime, making sure that things such as object state and changes detection remains coherent throughout.

My first thought was to simply use unit test class with a method that would do multiple operations and consequent assertions, like this:

```<code>/*
For the sake of keeping this as simple as possible, let's just assume
that new instances can be created with a public constructor, as "unchanged".
*/

var row = new PocoTest(); // Derived from aforementionned base class

Assert.AreEqual(RecordStates.Unchanged, row.RecordState);
Assert.IsFalse(row.IsPropertyModified("MyProperty"));

row.MyProperty = "this is a new value";

Assert.AreEqual(RecordStates.Modified, row.RecordState);
Assert.IsTrue(row.IsPropertyModified("MyProperty"));

row.AcceptChanges();

Assert.AreEqual(RecordStates.Unchanged, row.RecordState);
Assert.IsFalse(row.IsPropertyModified("MyProperty"));
</code>```

However, this doesn't feel right in the unit testing paradigm in which it is recommended to have only one thing at a time being tested.

So I'm kind of looking for some advice, here. Am I overthinking this? Should I just keep doing it this way? Or is there another, better and more adapted way to accomplish something like this?

答:
Remember many rules in programming are essentially recommendations you can follow. So sometimes this is acceptable.

Unit tests are called that way because they focus on testing single units of work. Generally, if you need more than one assertion per test case, you are

structuring your test inappropriately.

testing alternative code flows in the method, which are recommended to be tested with a separate test.

your method does too much and should be broken down.

I would break down your test into 3 tests, and name them appropriately. Next time you have 3000 unit tests in your system and one of them fails during automation build, you wouldn't see "TestRowLifeTme" Failed. You will see: "TestRowSetValue" failed, which is way more useful and specific.


问:
Agile development where customer collaboration is difficult
The agile manifesto states

Customer collaboration over contract negotiation

My question is how do you make this work effectively when there is distance between the developers and the users?

I currently work in a "waterfall" team maintaining a large enterprise system. The customer is in a different country but we do have people travelling out most weeks. Once onsite though it's rare you get to speak to end users as it's a large corporation with several layers of "the business" in the way. So the distance is not just geograhpical and it won't be the same people going out every week.

I know agile could help us to give a quicker turnaround but I'm trying to picture how we can replace these unwieldy documents today with short user stories that get fleshed out via customer collaborations. We do collaborate when writing specifications but meetings have to be organised usually with lots of people brought in which doesn't sound very agile.

I'm also not sure where the product owner would fit in. This is a large system and the customer has different contacts for each part of the system. The development is all one big team though and each release it will be different parts of teh system that get worked on.

I have worked in a scrum team at a different company but I'm not sure it was very agile. There the BA's wrote a spec for each story and then the developer would code from the spec. This approach seems to lack collaboration and is document heavy. It did at least break things down into smaller chunks which were more manageable (no bad thing) but it doesn't seem to meet the benchmarks set out in the agile manifesto.

答:
One of the main principals of Agile Development is customer being part of the team. It takes both parties to adopt Agile, so:

STEP 1

Get the customer on your side before proceeding. Agile is not about your team, it's about collaboration between YOUR TEAM and CUSTOMER.

Secondly, Agile is a methodology, it's not perfect, it's not a panacea, it's merely another way of doing things, that happens to work well for small teams with clients sitting beside them.

STEP 2

Learn the agile methodology and determine if it will REALLY make things faster and apply it to your customer. Large corporate clients are hard to get involved into projects, they tend to drag their feet and require comfort with plain old waterfall.

STEP 3

Probably go back to step 1 because your client is still not clear what is agile and why they need it... speaking from 6 years of experience with huge corporate client.

Some of the problems you are going to face:

Client has been working with waterfall for years and is not willing to change, because waterfall gives them sense of security when they have timelines in advance(although timelines get blown out of proportions by 300% by the end of the project, but no one seems to see that)

Client doesn't understand how agile works and agrees to it, then immedialy requires an L1 estimate provided by Monday next week.

Your team has no idea what agile is, and process will be a constant struggle to connect people together while trying to get client on board as well.

I know agile could help us to give a quicker turnaround but I'm trying to picture how we can replace these unwieldy documents today with short user stories that get fleshed out via customer collaborations.

You would spend a LOT of time trying to convert client to use agile and then the quality of user stories would be extremely low.

The right course of action in my opinion:

You know what Agile is and how it is applied very well.

You spend time and teach your corporate client overtime while helping them hire a couple BAs who are agile-savy.

BAs become product owners on the client's side and start EFFICIENTLY working with your team.

You break your team into smaller groups per area of the client's system and each team has own BA as product owner.

IT is also questionable if you can do agile remotely. I have seen some teams do it, and others have failed miserably.

Conclusion:
Take your time, and make changes slowly. Pick a hybrid process that works for you and YOUR client. Collaborate with client and don't try to stick to a methodology 100%. Where there are people, there are adjustments to be made to yield efficiency. Don't strive to switch to Agile because "all the cool kids do it".


问:
Creating two-way object references and keeping data integrity
I have two different classes; a Player and a Group. I need to be able to query a Player which Groups they are registered to (player.getGroups()), and which Players are registered to a Group (group.getPlayers()) quickly. The simplest solution is to have each Player keep a list of the Groups it's registered to, and each Group keep a list of all players registered to that group.

This introduces some data integrity problems (how do I ensure that if a Player references a Group, the Group also references the Player) and problems of how to add a Player to a Group.

A solution to the latter problem is to have a single method on either the Player or the Group:

```<code>public class Player {

    // ...

    public void registerToGroup(Group group) {
        this.groups.add(group);
        group.addPlayer(player);
    }

    // ...

}
</code>```

This works, but also introduces some side consequences of modifying the Group you pass, which you might not expect. But maybe this is the best (and relatively simple) way to do it?

What are the best practices for these kinds of problems? I would prefer not have to search through lists and filter out a certain Player to find out which Groups that Player is registered to, and I would prefer to not mix in databases as this is quite a small project but has to be able to be expanded on without much trouble.

答:
First and foremost, determine what is more common in your system - to list groups of a specific player or to list players of a specific group. Depending on which case is more common, implement it accordingly. If you try to implement both equally, you will take a performance hit for both.

So, assumption goes Player belongs to a group, so:

```<code>public class Group {
    public void AddPlayer(Player player) {

    }

    public void AddPlayers(IEnumerable<Player> player) {

    }
}
</code>```

Now that we got that out of the way.

```<code>public class Player {
    public Ienumerable<Group> GetRelevantGroups() {
        // Assuming you have some groups provider in this class. You should anyways.
        return this._groupsService.GetGroupsWithPlayer(this.Id);
    }
}
</code>```

Using both:

```<code>// Adding a bunch of players to a group.
myGroup.Add(players);

// Getting all groups for specific player.
var playerGroups = player.GetRelevantGroups();
</code>```

Body:
First and foremost, determine what is more common in your system - to list groups of a specific player or to list players of a specific group. Depending on which case is more common, implement it accordingly. If you try to implement both equally, you will take a performance hit for both.

So, assumption goes Player belongs to a group, so:

public class Group {
    public void AddPlayer(Player player) {

    }

    public void AddPlayers(IEnumerable<Player> player) {

    }
}

Now that we got that out of the way.

public class Player {
    public Ienumerable<Group> GetRelevantGroups() {
        // Assuming you have some groups provider in this class. You should anyways.
        return this._groupsService.GetGroupsWithPlayer(this.Id);
    }
}

Using both:

// Adding a bunch of players to a group.
myGroup.Add(players);

// Getting all groups for specific player.
var playerGroups = player.GetRelevantGroups();


问:
Is anything in programming truly evil?
So, there are a bunch of questions appearing asking is X evil, is Y evil.

My view is that there are no language constructs, algorithms or whatever which are evil, just ones which are badly used.  Hell, if you look hard enough there are even valid uses of goto.

So does absolute evil, that is something which is utterly incompatible with best practice in all instances, exist in programming?  And if so what is it?  Or is it just bad programmers not knowing when something is appropriate?

Edit: To be clear, I'm not talking about things programmers do (such as not checking return codes or not using version control - they're choices made by bad programmers), I mean tools, languages, statements, whatever which are just bad...

答:
Everything has it's own valid use, or it wouldn't be implemented. Things are called evil because people do not use it in a safe or proper manner. Consider this...

Is a sword evil? Nope. Just because someone may try to use a sword in a hospital does not mean it is evil.
Is a can opener evil? Nope.

Everything has it's proper use. Hence, no evil.

Title: Is anything in programming truly evil?


答:
Your biggest problem is that you're creating circular references which can lead to a world of problems, from endless loops to memory leaks.

In a database you'd create a separate entity (table) to contain the references, translated into code that's a class with a reference to both a Player and a Group.

Iterating over a collection of those will then give you all players in a group or all groups a player belongs to, but it can get to be an expensive operation.

Title: Creating two-way object references and keeping data integrity

Body:
I have two different classes; a Player and a Group. I need to be able to query a Player which Groups they are registered to (player.getGroups()), and which Players are registered to a Group (group.getPlayers()) quickly. The simplest solution is to have each Player keep a list of the Groups it's registered to, and each Group keep a list of all players registered to that group.

This introduces some data integrity problems (how do I ensure that if a Player references a Group, the Group also references the Player) and problems of how to add a Player to a Group.

A solution to the latter problem is to have a single method on either the Player or the Group:

```<code>public class Player {

    // ...

    public void registerToGroup(Group group) {
        this.groups.add(group);
        group.addPlayer(player);
    }

    // ...

}
</code>```

This works, but also introduces some side consequences of modifying the Group you pass, which you might not expect. But maybe this is the best (and relatively simple) way to do it?

What are the best practices for these kinds of problems? I would prefer not have to search through lists and filter out a certain Player to find out which Groups that Player is registered to, and I would prefer to not mix in databases as this is quite a small project but has to be able to be expanded on without much trouble.


问:
Cleanest way to write logically procedural software in an OO language
I'm an electrical engineer and I don't know what the hell I'm doing. Please save the future maintainers of my code.

Recently I've been working on a number of smaller programs (in C#) whose functionality is logically "procedural". For example, one of them is a program which collects information from different databases, uses that information to generate a sort of summary page, prints it, and then exits.

The logic required for all of that is around 2000 lines. I certainly don't want to stuff all of that in a single main() and then "clean it up" with #regions as some previous developers have been doing (shudder).

Here are some things I have already tried without much satisfaction:

Make static utilities for each coarse bit of functionality, e.g. a DatabaseInfoGetter, SummaryPageGenerator, and a PrintUtility. Making the main function look like:

```<code>int main()
{
    var thisThing = DatabaseInfoGetter.GetThis();
    var thatThing = DatabaseInfoGetter.GetThat();
    var summary = SummaryPageGenerator.GeneratePage(thisThing, thatThing);

    PrintUtility.Print(summary);
}
</code>```

For one program I even went with interfaces

```<code>int main()
{
    /* pardon the psuedocode */

    List<Function> toDoList = new List<Function>();
    toDoList.Add(new DatabaseInfoGetter(serverUrl));
    toDoList.Add(new SummaryPageGenerator());
    toDoList.Add(new PrintUtility());

    foreach (Function f in toDoList)
        f.Do();
}
</code>```

None of this feels right. As the code gets longer, both of these approaches start to get ugly.

What's a good way to structure stuff like this?

答:
I'm my opinion, you should change your code so that it is Simple, Consistent and Readable.

I wrote some blog posts on this topic, and trust me they are simple to understand:
What is good code

Simple: Just like a circuit board contains different pieces, each one with a responsibility. Code should be divided into smaller, simpler parts.

Consistent: Use patters, a standard for naming elements and things. You like tools that work the same way all the time and you want to know where to find them. It is the same with code.

Readable: Don't use acronyms unless they are widely used and known within the domain the software is targeting (mattnz), prefer pronounceable names that are clear and easy to understand.


问:
Is REST only limited to optimistic concurrency control?

Context

Due to the statelessness of the REST architectural style involving that each requests stands completely alone, leading server to never store any informations about client.

Thus, pessimistic concurrency control are not suitable because it would requires that server store which client gets the lock on a resource.
Optimistic concurrency control are then used, with the help of Etag header. (btw, as I asked there https://stackoverflow.com/questions/30080634/concurrency-in-a-rest-api)

Problem

The main problem with an optimistic concurrency control mechanism is that you allow all the time, all clients, to perform any operations.

And i would like to avoid that without breaking the statelessness principle of REST. I mean that all clients cannot perform any operation at any time.

Question

In my mind, it would possible with a semi-optimistic concurrency control mechanism, like that:

Clients can request a token

Only one token can be generated and has a limited period of validity

To perform operations on resources (such as POST or PUT), client must give this token as part of the body (or header?) of the request. Client that don't have the token cannot do these operations.

It is very similar to optimistic concurrency control, except that only one client can do some operations (the one that got the token)... at the opposite of "all clients can do all operations".

Does this mechanism is compatible with a REST architectural style ? Does it break any of its constraint ?
I was thinking to ask on SO, but this seems more an high-level question, related to software design.

答:
Using tokens is very common in APIs, these tokens are usually sent as a header and have a clear life cycle. Think for instance OAuth.

Regardless of your programming language or framework, REST APIs are similar.

I can think of several scenarios where you want to limit concurrency, two of them are:

Multiple clients updating the same resources like a database row.
For instance: two concurrent requests, one deletes a record and the other one tries to update the same record. Depending on your database and how you have set it up, you might get a lock on the records or invalid operation since the data will be different or will not exist.

A super user or administrator performing special actions with the API.

What to do in these cases?

Use transactions in the database, singletons, locks and similar mechanisms to synchronize the access to the resources.

The token might work, I think it will be better if you don't store information about the client, just about the token itself.
On one step you could validate the user and assign the token.
Then just validate that the token is alive and is valid.
Use a token that can be decrypted to obtain extra information.
You can store if this is an special token and allow only on at a time. This way you validate the token, not the user.

I hope this helps.


问:
Process string before or after insert into mysql
I have a device network where each device has different sensors (temperature, humidity, etc..). Each device uploads raw measured weather data every 10 seconds to a apache/php/mysql REST API. There are a few 100 devices sending data and this will become more over time. Currently the data is added to a mysql table without any processing.

The string format of the device message is something like:

```<code>&01AA*020122#
</code>```

Which means that sensor 01 measured AA and sensor 02 measured 0122.

I want to put the decimal converted hex values in a table so they can be processed further. What I do now is run a script every minute to see what new strings were added to the database. I will then convert these strings to decimal values and put them in a table.

Performance wise I have the feeling that polling new data every minute is pretty silly.

What would be the best way to do this?

have the rest api split the string and convert the hex values to decimal values and insert this immediately into the database

poll the database for new inserted strings and convert the hex values

use a mysql insert trigger to process the string by the database

any other ideas.

答:
Option 1. The string data should definitely be processed into more appropriate data types before insertion. That measurements are provided in hexadecimal is a detail of data transfer, and isn't important when it comes to storage (hexadecimal being merely a representation of numbers). Note that the script wouldn't be converting the string data to decimal (which is just another representation), but to integers (which is a data type).

Also, processing the data allows the table to be normalized. The raw string isn't even 1st normal form, as it contains a repeating group of compound data (sensor number & measurement).


答:
The best option is the one with the fewest things that can go wrong.  This is the "the web API converts the value and inserts it into the database." There is no significant amount of computation necessary, and changes are you don't even need to convert it into decimal first. You can insert using the unhex function in MySQL (docs).

If you were going for something a bit more enterprisy, this is the model for a message queue where something would be getting the data from the sensors, and sticking into a message queue as a producer, and then something else takes the messages from the queue as a consumer and does something with them (insert them into a database).  More on this at a RabbitMQ tutorial.

That said, that message queue is overkill and you can just stick it in the database from the start.  Do that.

Title: Process string before or after insert into mysql

Body:
I have a device network where each device has different sensors (temperature, humidity, etc..). Each device uploads raw measured weather data every 10 seconds to a apache/php/mysql REST API. There are a few 100 devices sending data and this will become more over time. Currently the data is added to a mysql table without any processing.

The string format of the device message is something like:

```<code>&01AA*020122#
</code>```

Which means that sensor 01 measured AA and sensor 02 measured 0122.

I want to put the decimal converted hex values in a table so they can be processed further. What I do now is run a script every minute to see what new strings were added to the database. I will then convert these strings to decimal values and put them in a table.

Performance wise I have the feeling that polling new data every minute is pretty silly.

What would be the best way to do this?

have the rest api split the string and convert the hex values to decimal values and insert this immediately into the database

poll the database for new inserted strings and convert the hex values

use a mysql insert trigger to process the string by the database

any other ideas.


问:
Nested POJOs, exposing collections, detecting changes
I have a couple plain object classes to represent data read from a file. They look something like this:

```<code>public class Command {
    private String name;
    private List<Message> messages;
}
public class Message {
    private Type type;
    private Map<String, Object> params;
}
</code>```

with getters and setters for the variables. However, I wasn't sure how to expose the messages list or the params map. At first I wanted to have a getter that just returns the list or map, to avoid having to write a bunch of add/remove/iterate methods for each list or map. The collection objects have these methods and many more, so it seemed most convenient to just return those objects. However, I also the objects to know when data is changed, so that a view can be updated accordingly, or to keep track of if a particular file has been modified and needs to be saved. This means I would have to implement delegates for add/remove/iterate/keyset/entrySet/etc for those fields, but would add a lot of extra methods and make the code much less convenient. What is the best practice here, when dealing with plain objects that can have lists, maps, sets, etc in them, while also being able to know when data is changed?

I also had a related question regarding whether a child object should have a reference to the object that created it. I was thinking about this because in the event that a Message object is modified, how does its parent Command object know about that change? Should the Message class have events that Command subscribes to with listeners, or should the Message object have a reference to the Command object it is contained in, and call a method/set a field in that Command object when data is modified? What is the best practice here, or should I completely change how file data is being stored as objects?

答:
I will take the risk and assume that you have modeled the data as close to the source as possible.

In this case:

The List messages is just a structure, and yes it has a lot of method that you can use, but this methods are technical methods, normal operations on a List class.

You want to create methods in the Command class that are more human readable by using a clear name, you could get some interesting things from this approach. F
for instance:

```<code>//pseudo-code here
public void AddMessage(Message message)
{
  ValidateMessageSefty(); // returns an exception if is not
  List.Add(message);
}
</code>```

I have some, easy to read articles about similar topics in my blog:
CodeJoy.blogspot.com
and particularly
What is Good Code

Body:
I will take the risk and assume that you have modeled the data as close to the source as possible.

In this case:

The List messages is just a structure, and yes it has a lot of method that you can use, but this methods are technical methods, normal operations on a List class.

You want to create methods in the Command class that are more human readable by using a clear name, you could get some interesting things from this approach. F
for instance:

//pseudo-code here
public void AddMessage(Message message)
{
  ValidateMessageSefty(); // returns an exception if is not
  List.Add(message);
}

I have some, easy to read articles about similar topics in my blog:
CodeJoy.blogspot.com
and particularly
What is Good Code


答:
So does absolute evil, that is something which is utterly incompatible with best practice in all instances, exist in programming? And if so what is it?

Yes; the standard C library function gets().  It's evil enough that the C standards committee has officially deprecated it, and it is expected to be gone from the next version of the standard.  The mayhem caused by that one library call is scarier than the prospect of breaking 30+ years' worth of legacy code -- that's how evil it is.

Title: Is anything in programming truly evil?

Body:
So, there are a bunch of questions appearing asking is X evil, is Y evil.

My view is that there are no language constructs, algorithms or whatever which are evil, just ones which are badly used.  Hell, if you look hard enough there are even valid uses of goto.

So does absolute evil, that is something which is utterly incompatible with best practice in all instances, exist in programming?  And if so what is it?  Or is it just bad programmers not knowing when something is appropriate?

Edit: To be clear, I'm not talking about things programmers do (such as not checking return codes or not using version control - they're choices made by bad programmers), I mean tools, languages, statements, whatever which are just bad...


问:
Composite Primary Key drawbacks
For website that I maintain (eCommerce) I now need to make a table that seems to require Composite Primary Key of 5 columns.
I never used 5 column as primary key before, only 2 and rarely 3.

I wonder whether there are any drawbacks to it, especially with regards to performance because this particular table is expected to have millions of records.

I haven't chosen a storage mechanism yet.

I am really rather interested in performance. How would for example "INSERT ... ON DUPLICATE KEY UPDATE" query work performance-wise in comparison to single column key? Are there any other performance drawbacks regarding to MySQL operations over this table?

答:
There should not be any significant performance impact

A clustered index is basically free - why not use it?

Insert check for unique is an index seek

It is very very fast for single or composite key

Even if there performance implication for me data integrity wins every time

If the data has a natural composite key then you should use a composite key

I assume you have FK on the those five columns

The FK table should be ID, Value

And the FK should reference the ID

So now the index seek is on ID

Create a view to expose the PK values

Fragmentation will effect performance of both clustered and non cluster indexes.  Try and arrange the order of the key to reflect the most common order of insert.  Try and insert in the order of the composite key.

Title: Composite Primary Key drawbacks


答:
This works, but also introduces some side consequences of modifying the Group you pass, which you might not expect. But maybe this is the best (and relatively simple) way to do it?

From a design perspective, to keep both lists in sync (if you want to handle it this way) is neither the responsibility of the player nor the group. You could design this with a mediator (here in the simplest form):

```<code>public class Group {

    List<Person> members;

    void addMember(Person p){
        members.add(p);
    }

}

public class Person {

    List<Group> groups;

    void addGroupMembership(Group g){
        groups.add(g);
    }
}

public class GroupingService {

    public void joinPersonToGroup(Person p, Group g){
        p.addGroupMembership(g);
        g.addMember(p);
    }

}
</code>```

But besides: is there any reason to implement it that way?

Body:
This works, but also introduces some side consequences of modifying the Group you pass, which you might not expect. But maybe this is the best (and relatively simple) way to do it?

From a design perspective, to keep both lists in sync (if you want to handle it this way) is neither the responsibility of the player nor the group. You could design this with a mediator (here in the simplest form):

public class Group {

    List<Person> members;

    void addMember(Person p){
        members.add(p);
    }

}

public class Person {

    List<Group> groups;

    void addGroupMembership(Group g){
        groups.add(g);
    }
}

public class GroupingService {

    public void joinPersonToGroup(Person p, Group g){
        p.addGroupMembership(g);
        g.addMember(p);
    }

}

But besides: is there any reason to implement it that way?


问:
What's special about "non-blocking" node.js?
In the short chapter, What is Node by McLaughlin, he writes:

"Node has no blocks, no threads competing for the same resource (Node
  is happy to just let things happen however they happen), nothing that
  has to start up upon request. Node just sits around waiting (quite
  literally; unused Node responders are sleeping). When a request comes
  in, it’s handled. This results in very fast code, without uber-
  programmers writing the server-side behavior."

While I think he's picking on PHP here - from what I understand there isn't a way for PHP to not call up new references to its databases, files, etc, whenever any php page is loaded - is this really different than other web technologies? Consider Django/Flask for example or a simple Python threaded-server using builtin methods - is it really any different than the no-blocks advantages of Node? You run a program and it binds a socket, running the function only as needed. (Doesn't Java also do this?)

答:
Unlike all other servers you have mentioned, Node is single threaded, but asynchronous - as some have mentioned here, it schedules callbacks instead of waiting for operation to execute and runs callbacks when the operation is complete, however a number of other operations might have been processed in between with the same thread.

Caveats:

Although it's a wonderland for asynchronous programming where you don't need to watch your variables being used by multiple threads, if one not familiar with the way Node works would be to apply regular synchronous approach, this can turn into a VERY very slow application as every request will pile up to wait till the first request will finish it's action...


问:
Big graph traversal with OOP
I'm trying to solve a algorithmic contest's problem. I have a matrix 2000x2000. I want to represent it as graph and traverse it with BFS/DFS. I have time limits on app running (2s). Simple vertices creation took more than 2s! Just think of it - I'll need to create Adjacency matrix + run BFS/DFS + do some business logic so time will be increased more! Here is my attempt to create Graph vertices:

Map<Integer, Vertex> allVertices = new HashMap<>();

final long b = System.currentTimeMillis();
for (int i = 0; i < n; i++)
{
    for (int j = 0; j < m; j++)
    {
        final int id = i * m + j + 1;
        Vertex v = allVertices.get(id);
        if (v == null)
        {
            v = new Vertex(id);
            allVertices.put(id, v);
        }
    }
}
final long a = System.currentTimeMillis();
final long d = a - b;
System.out.println(String.format("took <%s> ms", d));

class Vertex
{
    Color color;
    final int id;

    public Vertex(final int id)
    {
        this.id = id;
    }
}
```

I wonder is this common practice to use OOP to represent big graph's vertices or I need to use arrays only?

答:
Only because you want to do things OO does not mean that you must create the graph made out of nodes and vertices. It is absolutely fine to capture the full graph in a single object, and to create a nice, well contracted OO interface on top of it.

A classic idea that I have used is the concept of a cursor that works on top of a graph. That cursor knows about the internal representation, and can sit either on top of nodes and travel along vertices to other nodes, or it could sit even on vertices - its all a matter of design choice really.

The nice thing about such an interface is that it stays reasonably efficient, even if you are hiding to the user if the internal implementation of the graph is using adjacency matrix or adjacency lists.

Title: Big graph traversal with OOP


答:
Though any tool can be used for good and evil, some tools are evil because they often surprise programmers who don't use them frequently.

I consider the unsigned right shift operator (>>>) in Java evil (surprisingly improper) when working with integers that are shorter than 32 bit.

Say you have a byte b with value -1.

```<code>byte b = -1;  // binary: 1111 1111
</code>```

The unsigned right shift operator shifts zeroes into the leftmost bits. So one assumes a shift by 7 to result in 1.

```<code>b >>>= 7;  // binary: 0000 0001 ?
</code>```

But instead this operation does nothing at all. b is still -1.

Even all of the following 25 shifts do nothing:

```<code>byte b = -1;
for (int i = 0; i < 25; ++i) {
    b >>>= i;
    System.out.println(b); // always outputs -1
}
</code>```

This happens because b>>>=7 roughly translates to

```<code>                                  1111 1111

1) the byte gets widened to a 32 bit int to make shifting possible
    1111 1111 1111 1111 1111 1111 1111 1111

2) the shift happens
    0000 0001 1111 1111 1111 1111 1111 1111

3) the resulting int gets narrowed to a byte again
                                  1111 1111
</code>```

You would have to replace

```<code>b >>>= i;
</code>```

by

```<code>b = (b & 0xFF) >>> (i % 8);     // >> would also work this time
</code>```

to make it work as 'expected'.

Title: Is anything in programming truly evil?

Body:
Though any tool can be used for good and evil, some tools are evil because they often surprise programmers who don't use them frequently.

I consider the unsigned right shift operator (>>>) in Java evil (surprisingly improper) when working with integers that are shorter than 32 bit.

Say you have a byte b with value -1.

byte b = -1;  // binary: 1111 1111

The unsigned right shift operator shifts zeroes into the leftmost bits. So one assumes a shift by 7 to result in 1.

b >>>= 7;  // binary: 0000 0001 ?

But instead this operation does nothing at all. b is still -1.

Even all of the following 25 shifts do nothing:

byte b = -1;
for (int i = 0; i < 25; ++i) {
    b >>>= i;
    System.out.println(b); // always outputs -1
}

This happens because b>>>=7 roughly translates to

                                  1111 1111

1) the byte gets widened to a 32 bit int to make shifting possible
    1111 1111 1111 1111 1111 1111 1111 1111

2) the shift happens
    0000 0001 1111 1111 1111 1111 1111 1111

3) the resulting int gets narrowed to a byte again
                                  1111 1111

You would have to replace

b >>>= i;

by

b = (b & 0xFF) >>> (i % 8);     // >> would also work this time

to make it work as 'expected'.


问:
you don't know js book 'this' example
I am reading the book "you don't know js this & object" and I ran into below where they are explaining 'this'. Now, below example, I understand somewhat but I just do not understand what 'arguments' it is getting.

Is the word 'arguments' just some sort of place holder? I just tried it w/ argumentsX in place of arguments, and I get same console output of [ object Arguments]

If someone can point me to right direction, it will be greatly appreciated.

```<code>function foo(something) {
    console.log(this.a, something);
    return this.a + something;
}
undefined
// simple 'bind' helper
function bind(fn,obj) {
  return function() {
     return fn.apply(obj, arguments);
  };
}
undefined
var obj = {
   a: 2
};
undefined
var bar = bind(foo,obj);
undefined
bar
anonymous()
var b = bar(3);
2 3
undefined
console.log(b);
5

///// Ok, I want to know what the arguments here is... so I run another one where I log to console what arguments is

// simple 'bind' helper
function bind(fn,obj) {
  return function() {
     console.log("YESS " + arguments);
     return fn.apply(obj, arguments);
  };
}
undefined
var obj = {
   a: 2
};
undefined
var NEWvariable  = bind(foo,obj);
undefined
var b = NEWvariable(3);
YESS [object Arguments]
2 3
undefined
</code>```

答:
In JavaScript, a function's signature is not checked when called, so you can call a function of any arity with any number of arguments. arguments is a reserved variable provided to you by the language that holds all the arguments sent to the closest function, so you can get them even if they do not appear in the function's signature:

```<code>js> function foo() {
    return arguments;
}
js> foo('a', 'b', 'c');
({0:"a", 1:"b", 2:"c"})
</code>```

Title: you don't know js book 'this' example

Body:
In JavaScript, a function's signature is not checked when called, so you can call a function of any arity with any number of arguments. arguments is a reserved variable provided to you by the language that holds all the arguments sent to the closest function, so you can get them even if they do not appear in the function's signature:

js> function foo() {
    return arguments;
}
js> foo('a', 'b', 'c');
({0:"a", 1:"b", 2:"c"})


问:
What is business logic code and what is data access code, and what's the difference?
I ask this because it seems people usually consider the code that goes into a DAO or Repository implementation as "data access code", while the code that directly uses these DAOs/Repositories as "business logic code".

However, this doesn't seem to make logical sense.

Consider a business service method with code like the following (using Java, but it would be similar in C#, etc.):

```<code>public BigDecimal computeCustomerTotal(Customer customer) {
    List<Order> orders = orderRepository.findByCustomer(customer);

    BigDecimal total = orders.stream()
        .filter(Order::isActive).map(Order::getTotal).reduce(ZERO, BigDecimal::add);

    return total;
}
</code>```

Sure, the above implementation doesn't make "proper" use of a database's capabilities, but bear with me. The point here is that, I assume, everyone agrees it contains only business logic code, with Customer and Order being domain entities (ie, they are not "data" objects).

So, what if I want to make it right, by moving the whole computation to the database, instead of executing in the client program? If I follow "conventional wisdom", I would create a new method in the repository:

```<code>public BigDecimal getCustomerTotal(Customer customer) {
    BigDecimal total = performQuery(
        "select sum(o.total) from Order o where o.customer = ?1 and o.active",
        customer);

    return total;
}
</code>```

(Assume performQuery is a JPA-based utility method which takes a JPA-QL
sentence with optional arguments.)

The second implementation is obviously better than the first, as it probably performs better, and makes proper use of the underlying technologies (JPA/Hibernate, and a relational database).

The problem, though, is that business logic (the rule that only active orders should be considered) has been moved from a business service method to a (supposed) data access method, through a simple implementation change.
Also, this code which is now in the DAO/Repository does not actually know about data access concerns, such as the mapping from entity types and attributes to tables and columns, or about the actual SQL code that gets generated and sent to the DB engine.

Wouldn't it be more logical to regard both methods as containing only business logic code, and just get rid of the DAO/Repository/DAL altogether?

Personally, I don't see any data access code there, and to me things like DAOs, Repositories, or "data access layers" (DAL) are in reality anti-patterns. Note that even when using JDBC + SQL in a DAO/Repository implementation, it will inevitably contain business logic, typically in the form of "where" clauses.

答:
The database layer is intended to isolate the rest of the application from the details of the database -- how to make a connection, the syntax used to talk to the db engine, etc.

The .Net Entity Framework version of your code would be:

```<code>var id = customer.id;
var customerOrdersTotal = db.Orders.
                          Where(o => o.CustomerId == id && o.Active).
                          Sum(o => o.Total);
</code>```

Note that this would get translated into the sql your JPA version would use, but the exact same thing could be done without involving sql at all.

If you have a Customer that has Orders, then it would be:

```<code>var customerOrdersTotal = customer.Orders.
                          Where(o => o.Active).Sum(o => o.Total);
</code>```

And this version could be written regardless of whether Orders is a virtual list or an actual list of orders.

The business doesn't care where the data is kept or how it is retrieved.  The point of a DAL is to abstract away the "how is this bit of data retrieved" and turn it into just the necessary (i.e. unavoidable) "how to request the data I want".  Without a DAL you have to mix the two functions together, which means you can't change one without changing the other.

Note that about half the time, you don't ever end up making any changes...but when you do it's a real pain if they are mixed together.

Edit: I'll add that your implication that "where" clauses in the data access code is a business concern is incorrect.  The data layer exists to serve business needs.  The data can be carved up and served in an infinite number of ways, some more useful than others.  Moving a condition from the business layer to the data layer simply means that the request is common enough to be included in the ways that are usefully provided out of the box. Preventing new orders when existing active orders exceeds $X is a business decision.

Body:
The database layer is intended to isolate the rest of the application from the details of the database -- how to make a connection, the syntax used to talk to the db engine, etc.

The .Net Entity Framework version of your code would be:

var id = customer.id;
var customerOrdersTotal = db.Orders.
                          Where(o => o.CustomerId == id && o.Active).
                          Sum(o => o.Total);

Note that this would get translated into the sql your JPA version would use, but the exact same thing could be done without involving sql at all.

If you have a Customer that has Orders, then it would be:

var customerOrdersTotal = customer.Orders.
                          Where(o => o.Active).Sum(o => o.Total);

And this version could be written regardless of whether Orders is a virtual list or an actual list of orders.

The business doesn't care where the data is kept or how it is retrieved.  The point of a DAL is to abstract away the "how is this bit of data retrieved" and turn it into just the necessary (i.e. unavoidable) "how to request the data I want".  Without a DAL you have to mix the two functions together, which means you can't change one without changing the other.

Note that about half the time, you don't ever end up making any changes...but when you do it's a real pain if they are mixed together.

Edit: I'll add that your implication that "where" clauses in the data access code is a business concern is incorrect.  The data layer exists to serve business needs.  The data can be carved up and served in an infinite number of ways, some more useful than others.  Moving a condition from the business layer to the data layer simply means that the request is common enough to be included in the ways that are usefully provided out of the box. Preventing new orders when existing active orders exceeds $X is a business decision.


问:
Know file size with a base64 string
I do not have very clear what a base64 string is or how it is related to a file.

Right now I have a website that uploads an image as a base64 string to my server. This string is converted back into a bitmap and resized.
However, I would like the least amount of memory to be consumed in my server, therefore I would like to restrict the file size of the images being uploaded without processing them.

Is there a way to know the file of a size without actually saving the file into an stream?

答:
Edit: apologies for blatantly wrong info. Acording to this Wikipedia article, it will be a bit larger:

Very roughly, the final size of Base64-encoded binary data is equal to 1.37 times the original data size + 814 bytes (for headers).

You can use ContentLength property of the request to determine what the size is in bytes, although if you are uploading more then one image, it might be trickier.

Title: Know file size with a base64 string