问:
Question about mocking externals
at company we're developing quite a big project and we're arguing at the testing strategy. The question is: should all of the tests be executed in isolation of external services like database or APIs (facebook etc) or just part of them? Of course what I have in mind is using mockups.
We discussed following strategies:

Write all of the tests in isolation of external services - mock externals everywhere

Write part of the tests in isolation and create single bigger functional test for every feature that tests it using externals (without mocking anything) - using fixtures

Run all tests communicating with externals (of course that's never appliable to unit tests so they're out of scope in that case)

I know it can start quite a discussion but I think that's what it is all about, I'd like to find pros and cons that I didn't think of already.

答:
If done right, your second strategy can have the best of both worlds:

Write part of the tests in isolation

Isolating most tests from external services is a good idea because you have no control over that service. The worst-case scenario is to have unit tests fail intermittently, because eventually you'll start to ignore the failures, at which point the tests are useless. Creating a mock that provides consistent responses to your tests makes it much easier to verify how your code reacts to a given response from the external service.

create single bigger functional test for every feature that tests it using externals

This can provide important insight into the behavior of the system closer to actual operating conditions, allowing you to recognize and handle different issues than you'll encounter when external services are mocked, such as network issues, or input or output data formatting errors.

sometimes you don't know how external source might react in some conditions, it's hard to mock it (for example Solr with complex query)

sometimes you can forget about software-specific case (for example switching from MySQL to Postgres, while using ORM, might return different results and cause some other problems)

If you can dependably reproduce some response from the external service, and you want to verify that your code reacts to it in a certain way, then it makes sense to test it. The important thing is that you don't allow intermittent failures in your tests that e.g. run with every build. I've seen very good developers switch off the tests because a build needed to be made and they didn't have the time to fix an obscure, complex test. That's a bad situation to be in, because the tests are no longer serving their purpose, and confidence in them is reduced.

Title: Question about mocking externals


问:
How do you guard against hidden overflows
consider the following:

```<code>(i+j)/2
</code>```

i and j are both 32 bit integers and the result should also be.
But in the little equation, there's a hidden overflow, i+j could become larger then a 32 bit integer even though the result would always be a 32 bit integer.

```<code>public class Sum{
     public static void main(String []args){
        int i = 2147483647;
        int j = 2147483647;
        System.out.println((i + j)/2);
     }
}
</code>```

Gives me -1 as a result.

How do you guard against situations that can overflow but are not always obvious.
Is there a pattern that describes this?

(apart from declaring everything long)

I'm looking for a general answer, the java example is just to  provide an example, my Delphi does the same.

答:
You have three choices, none of which are ideal for all situations:

Detect Overflows.  Most languages have a way to determine the largest possible value for integer data types.  The addition in your example will result in an overflow whenever j > (max_value_of_type - i).  You will have to check every use for overflows or build a library or class to does it for you.

Use Larger Types.  If there are larger integers available, use those to do your intermediate calculations and convert back to the smaller type when finished.  The hitch here is that you must understand whether the results of those calculations are guaranteed to fit the smaller size if that's what you'll be returning.  Your example of (i + j) / 2 fits, but (i * j) / 2 doesn't.

Restrict the Input Values.  This is a lot like both of the above in that you're limiting the inputs to values you know won't cause an overflow and at the same time making the integers large enough to hold the result.

ContentLicense: CC BY-SA 3.0


问:
Real time data synchronization techniques between two systems
I need to come up with a design for real-time data update from a COTS product (a Point of Sales system) to a custom-built .NET application (an Inventory Management system).

Particularly, any sales transaction happening in POS system needs to update the inventory database (present in Inventory Management system) immediately (in real time). The only way any other system can communicate to the POS system is via its API exposed as web services.

I have thought about introducing a Service Bus (or any such EAI tool) in between the two systems and taking advantage of the publish-subscribe model so that any sales depletion happening in POS system will trigger data update to the IM system via the service bus. My questions are:

Is it a good/feasible solution?

Do you have any other suggestions for such real-time data synchronization between different systems?

答:
I think there are several ways to make synchronization between two system and I guess it is similar with that how to share memory in clustering.

You may sync data in system as below ways

share memory using memcached

share memory using DB

update data to the IM right away.

Save data only in one system and either system use the data just by calling API
you may be getting timing issue if you use 3 or 4

If you want to reduce timing issue, I recommend you'd better use way 1 or 2


问:
How to collect industry usage of data persistence technologies?
In our organization, we are using various data access/persistence technologies in different projects. The ones used till now are Hibernate, JPA, Spring Data JPA, JDBC, Spring JDBC, MyBatis, and QueryDSL.

How do I find out what is the industry usage of these technologies? i.e. which is used more widely than others - or even a pie chart showing the distribution of such technologies.

Also curious to know which technologies are being used by major players - like big silicon valley companies, or prominent investment firms. Do you know a way to find this data?

Can the firms like Forrester or Gartner would have such a data?

Thanks in advance.

答:
why do you even care?

you won't get a response from most companies as they'd consider it a company secret what they use

what are your criteria? Many companies would have investigated a lot of things, before maybe adopting one or two of them. And then they may no longer be used in new projects.

why do you even care? Use what's appropriate for your project, not what's "popular".


问:
Would these two scenarios be good candidates for a NoSQL database?
I've checked a few other threads around the topic and search around, I am wondering if someone can give me a clear direction as to why should I consider NoSQL and which one (since there are quite a few of them each with different purposes)

Why NoSQL over SQL?

Is MongoDB the right choice in my case?

https://softwareengineering.stackexchange.com/questions/5354/are-nosql-databases-going-to-take-the-place-of-relational-databases-is-sql-goin

Like many others - I started with relational databases and been working on them ever since, thus when presented with a problem, the first instinct is to always think of "I can create these tables, with these columns, with this foreign keys", etc

My overall goal is How to get into "NoSQL" mindset? ie getting away from the inclination of always thinking about tables/columns/FKs (I understand that there are cases where RDBMS is still the better way to go)

I am thinking of 2 scenarios for example just to get more concrete direction

Scenario 1

Imagine a database to model building a furniture instructions (think of IKEA instructions) where you would have the object "furniture" which would have a list of "materials" and have a list of "instructions"

Furniture - would simply have a name that have a list of Materials and Instructions

Materials - would be a name + quantity, may be we can even have "Material Category" table as well

Instructions - would simply be an ordered list of texts

My first instinct would go the RDBMS way:

Create a table called "Furniture", "Material" and "Instruction" and the approppriate columns

Create the appropriate JOIN tables as necessary and FKs

The use of this system can include searching based on materials or may be combination of materials. And may be think of extending the data stored to include information on how many people are required to build it? Difficulty level? how much time it would take?

Would something like this be a good candidate for a NoSQL database?

Scenario 2

Imagine a database to model a User database with basic information (eg. name, email, phone number, etc), but you also want to have the flexibility of being able to add any custom fields as you wish.

Think of different systems consuming this user database, each system will want to have their own custom attribute to be attached to the user

My inclination would go the RDBMS way:

Create a table for "USER" with columns: ID, name, email, phone

Create a table for "USER_ATTRIBUTE" with columns: ID, USER_ID, attr_name, attr_type, attr_value

The USER_ATTRIBUTE will allow that customization and flexibility without having to shut down the system, alter the database and restart it.

Would something like this be a good candidate for a NoSQL database?

答:
NoSQL isn't a very well defined term and all the solutions that run under this name have very different features, so a lot may be possible or not depending on what exactly you are planning to do with it.

Basically you could use some of the more general solutions like maybe MongoDB or Cassandra to simply replace your current relational database. In some cases this makes more sense in others less, but it will work once your team got used to it. Certain things will be easier then, others will be more difficult and you must weight those options against each other and decide (which often enough will mean that there are no advantages big enough and the simple fact that everybody in the team feels most comfortable with relationals and SQL will make the decision easy)

Other NoSQL solutions that are more specialised are not really good candidates to replace your relational DB, like graph databases or simple key value stores. So lets from here talk mainly about those databases that are at least to some degree similar to relational databases.

Scenario 1

Where I work we have exactly this scenario, though quite more complex with a lot of different attributes per article. Some of those attributes in hierarchies like Apple -> iPad -> Air.

The data is still stored in a relational database. But: searching this in real time became a pain. With SQL it was slow and code would have been terribly complex. Selects over many tables, with the additional option to exclude certain attributes like "not blue".

In this case Apache Solr or Elastic Search are a solution. Though of course data is duplicated from the relational database.

But from here our experience with this kind of document store showed that it can handle certain problems very well and we will consider to replace part of the existing relational structure with some other kind of storage. So not the whole database where we also store all the transactional data like orders etc, but for example take out all the attribute information which can be handled much better in the aggregate like data structures of NoSQL.

Scenario 2

Difficult to say, since what you describe is most likely only a very small part of your user handling. Having schemaless storage is an advantage with many NoSQL databases. But some relational databases allow to store such data too (as long as you don't need to query it via SQL in most cases).

Cassandra for example would allow you to define column families in such a case, where your first set of attributes would be one such family and the variable attributes another one.

As somebody said: NoSQL is less about storage and more about querying. So the question is what will be the typical use case for those queries.

A typical problem would be the transactional data here. If you want to store orders, one way would be a schema where users and their orders form an aggregate (kind of user document that contains the orders as subdocuments). This would make getting a user together with his orders very simple and fast, but would make it very difficult to retrieve all orders from last month for sales statistics.

Also strengths of NoSQL solutions are that it can be easier to run them on multiple clusters if you have to work with very large datasets.

Conclusion: Both your scenarios could be modelled with certain NoSQL solutions, but I don't think that (assuming they have to run in a larger environment) they really justify a large extra effort in learning, training and implementation and maybe some other additional disadvantages because both are not specific enough to really leverage the strengths of NoSQL. At least not in that simple form you describe it. Things may become very different once some aspects you describe would be very, very prominent in your usage scenario, like in scenario one the attribute data becomes very complex or in scenario two the variable fields become the largest part of data you store with every user.


问:
How does a search functionality fit in DDD with CQRS?
In Vaughn Vernon's book Implementing domain driven design and the accompanying sample application I found that he implemented a CQRS approach to the  iddd_collaboration bounded context.

He presents the following classes in the application service layer:

CalendarApplicationService.java

CalendarEntryApplicationService.java

CalendarEntryQueryService.java

CalendarQueryService.java

I'm interested to know if an application will have a search page that feature numerous drop downs and check boxes with a smart text box to match different search patterns; How will you structure all that search logic?

In a command service or a query service?

Taking a look at the CalendarQueryService.java I can see that it has 2 methods for a huge query, but no logic at all to mix and match any search filters for example.

I've heard that the application layer shouldn't have any business logic, so where will I construct my dynamic query? or maybe just clutter everything in the Query service?

答:
In CQRS you create different models for commands and queries, so it's fine to create separate model for your query service and do business logic using it.    </p>

<p>Anyway, if searching is a big part of your application, then I would create another bounded context for it. </p>

Body:
In CQRS you create different models for commands and queries, so it's fine to create separate model for your query service and do business logic using it.

Anyway, if searching is a big part of your application, then I would create another bounded context for it.


问:
Is there anything groundbreaking about NoSQL?
I am a very solid Relational Database guy and understand all the way to 3rd normal form, appreciate the algebraic set theory roots of SQL, and can probably relationalize a broken heart (or not).

I haven't figured out a relational database structure FOR date nights with my wife, but I HAVE thought about relational database projects ON date nights with my wife..

Now I'm hearing about NoSQL, and researching it.  Cutting to the chase, is there anything about NoSQL that is ground breaking, mathematically novel, or a "hey you don't even really need to organize your data relationally, this is so much easier" type of approach?

Is NoSQL like a super shell to the data structure? In my mind, data must ultimately have structure to be retrieved and the retrieval must be defined in a language of some sort.

答:
Snowman's answer correctly describes how SQL and NoSQL differ in their data structures and how these are accessed. However, a probably even more important difference is their respective problem domain.

NoSQL is not a successor of SQL. Rather, the various branches of NoSQL sacrifice some qualities of SQL in order to be better at others. The CAP theorem states that it is impossible for any distributed database system to satisfy all of the following properties:

Consistency

Availability

Partition tolerance

Thus, some NoSQL variants follow the BASE principle instead, which relaxes the always-full-consistency constraint of ACID, which is the basis for classical SQL databases. By losing some consistency guarantees, they gain the possibility of combining high availability and partition tolerance in widely distributed systems, such as for websites with high amounts of data and user queries, but little demand for perfect consistency. Thus, such NoSQL databases are at the heart of Google, Facebook, and Amazon. So, to answer your question: Yes, NoSQL is ground-breaking in that it pretty much enables such massive web services.

This is only one example, as NoSQL is a diverse field, and its variants cover pretty much all possible combinations of parameters within the CAP triangle.


问:
Which pattern is suitable to send multiple type of responses through REST web service?
I have bunch of REST style web services which support XML and JSON type responses. Now I have to modify existing web services such that they provide Protobuf type of responses.

Developer who has developed these services has created three layers. Service layer, DAO layer and Processing layer.

Service layer calls one of the method of processing layer and processing layer calls DAO methods and returns either JSON or XML type response (POJO) to the service. Service layer then sends back this POJO to client.

For example, if service to add list of items is called then that code roughly looks as shown below:

```<code>@Path("/item")
class ShoppingItemService {
    // annotations
    public ItemAddResponse addList(/* parameters required for this method */) {
        ItemProcessor processor = new ItemProcessor();
        ItemAddResponse response = processor.process(/* pass some data to processing layer */);
        return Response.ok().format("XML").build(response);
    }
}
</code>```

Here ItemAddResponse is already annotated with JAXB type annotations for XML response and other annotations for JSON response.

I cannot reuse same ItemAddResponse because Protobuf generates its own classes using schema files.

Processor class has all the business logic and If I want to add support for Protobuf  then I have create new method in processor and copy-paste almost all the business logic, but just return object of protobuf generated class.

This approach is time consuming and there atleast 50+ similar methods where I have to do copy-paste work. I want to avoid this and would like to design some elegant solution.

I would like to know how can I add support for Protobuf with minimal amount of changes?

I am reading Head First Design patterns book but this book is really huge and it will take lot of time to find proper design pattern.

Is there any design pattern which can be useful in this type of situations?

Thanks,

Chandrashekhar

答:
I wonder each input parameter is different or not.
Anyway you may be able to return result as Object and cast it. Also you may be able to get parameter as Object.

You may be looking for that how to generate methods in runtime.
https://stackoverflow.com/q/6680674

I recommend you'd better make all methods. I think It may be good for readability.


问:
Optimizing code by using registers, but what if they are not enough in number?
One way to optimize code is to minimize the access to arrays and use variables instead, because that way we use registers instead of loading data to cache memory.

For example, if in a loop I'm going to use 3 times a data from an array, say radius[i], I'd better declare a variable tmp, and give it the value of radius[i]. Every time I need to call radius[i] I'll use tmp instead.

The problem is, the number of registers is limited (to 8 in some cases, it depends on the architecture).

What if I have more variables than the number of registers? Will the usage of variables in this case slow down my program more than having access to cache memory?

I am coding in c language.

答:
There are good answers to the question you asked, but it's important to keep in mind that as software gets bigger and more serious, this kind of optimization gets less and less relevant.
The reason is that the program traces out a call tree, and time is mainly spent in the leaves, and the leaves are less and less likely to be in code that the compiler sees.

Instead, time becomes more and more likely to be spent in library functions the compiler doesn't see, and some of which do I/O, which no compiler can optimize.

Such a program can certainly be optimized, by a large amount, but only by the programmer.
To do that, you need a way to tell what the program is doing that might not strictly need to be done, like a lot of memory allocation.
Here's an example.


问:
Level order sorted binary tree from a binary tree
Lets suppose we have a binary tree . Tree node structure is as

```<code>struct node
{
   int val ;
   struct node *left , *right ;
}
</code>```

Now we have to sort the tree in level order manner . For example lets suppose we have original tree like this

```<code>root = 6
root->left = 1
root->right = 9
root->left->left = 8
root->right->right = 2
</code>```

Then after applying level order sorting , our tree will be

```<code>root = 1
root->left = 2
root->right = 6
root->left->left = 8
root->right->right = 9
</code>```

One way of doing this problem is just take all the values in a array and apply any of the standard sorting algorithm and then put back the values in tree in level order traversal manner. This solution is not efficient in term of space and time both.

What algorithm can I use to decrease operations and memory usage from my proposed plan?  Is there a more efficient algorithm to do this without the use of a sorted array?

Level order sorting :

Level order sorting means if you do level order traversal of the tree then you should get a sorted non-decreasing sequence .

Constraint :

Shape of the tree should not alter that is if in original tree left child of the root does not have right child (as in my example) then after sorting too, it should not has right child .

答:
Assume that you have a forward-only iterator that will visit the tree in level order, this can be accomplished using an insertion-style sort, two iterators and one location of temporary storage.

```<code>For each node (using Iterator1)
  Put the value into temp
  For each node up to Iterator1 (using Iterator2)
    Skip until temp >= value
    Insert temp and move values down one position
</code>```

Very efficient in space, not so much in speed but easy to code.

If there is enough space to copy all the values, then there is a faster algorithm by copying them out, sorting them and copying them back in again.

By way of clarification, a 'forward only iterator' means a pointer or index or other mechanism that allows every node to be visited in a pre-defined order, but does not allow reversing or random access. This is the commonest way to traverse trees, and probably what you're already doing.

With two of these and a single temporary you can implement an in-place sort. The first (outer) iterator does a single pass from start to end and taking out one value each time. The second (inner) does multiple passes from beginning to outer iterator, first finding where to put the value that was removed, and then shuffling the remaining values down. That's an insertion sort (but quite similar to a bubble sort). It's probably 5-10 lines of code, depending on language.

OwnerDisplayName: sdream

Body:
Assume that you have a forward-only iterator that will visit the tree in level order, this can be accomplished using an insertion-style sort, two iterators and one location of temporary storage.

For each node (using Iterator1)
  Put the value into temp
  For each node up to Iterator1 (using Iterator2)
    Skip until temp >= value
    Insert temp and move values down one position

Very efficient in space, not so much in speed but easy to code.

If there is enough space to copy all the values, then there is a faster algorithm by copying them out, sorting them and copying them back in again.

By way of clarification, a 'forward only iterator' means a pointer or index or other mechanism that allows every node to be visited in a pre-defined order, but does not allow reversing or random access. This is the commonest way to traverse trees, and probably what you're already doing.

With two of these and a single temporary you can implement an in-place sort. The first (outer) iterator does a single pass from start to end and taking out one value each time. The second (inner) does multiple passes from beginning to outer iterator, first finding where to put the value that was removed, and then shuffling the remaining values down. That's an insertion sort (but quite similar to a bubble sort). It's probably 5-10 lines of code, depending on language.


问:
Property-level value transformation for indirect object casting
Does any programming language exist to support the explicit, property-level object copy?

For example, assume this code:

public class Student
{
    public string Name { get; set; }

    public string Code { get; set; }

    public List<string> Interests { get; set; }
}

public class Item
{
    public string Name { get; set; }

    public string Code { get; set; }
}
```

<p>I wonder if there is a compiler (or a language) out there that can indirectly cast two objects to each other, based on property-level matching and a lossy transformation algorithm?</p>

<ol>
<li>Create an instance of the target class</li>
<li>Using reflection, find all properties of the target class</li>
<li>For each property, try to find a match in the source class, using name and type</li>
<li>If found, transfer the value</li>
<li>Otherwise, initialize to the default value</li>
</ol>

<p>Of course, this should be done <strong>explicitly</strong> with the full intention of the developer. For example, a C# pseudo-syntax might look like:</p>

<pre class="lang-cs prettyprint-override"><code>Student student = new Student();
student.Name = "Saeed";
student.Code = "513223";
Item item = copy student; // assume "copy" to be a keyword
// Here, item's name is "Saeed" and its code is "513223"
</code>```

答:
I know of languages with this feature, or something like it.

One that is best forgotten is COBOL's MOVE CORRESPONDING. Look it up if you care enough.

But the more important one is the dynamic languages like Ruby and JavaScript that use dictionaries to represent structures. The kind of operations you describe are commonplace, but implemented as library functions rather than keywords in the language. Assignments are done strictly by name matching, regardless of type. They can be used (for example) in Rails to move bundles of attributes from an incoming request through program logic to database access, all by name matching.


问:
I'm always reimplementing observer/subject code in Java. Is there a better option?
I'm always writing observer/subject interfaces in a particular Java project, e.g.:

```<code>/**
 * Registers the receiver to the dispatcher.
 * When data arrives that the receiver can process,
 * it will be passed to the receiver.
 * ...
 */
void addReceiver(IDataReceiver receiver);

/**
 * @return
 *      True, if the receiver has been registered, false otherwise.
 * ...
 */
boolean hasReceiver(IDataReceiver receiver);

...
</code>```

When a class implements these interfaces, I have to implement the code, test it, debug it, etc. It feels so futile to keep reimplementing such similar logic. Is there any better option in Java?

答:
There are some abstractions in the Java API for the Observer Pattern. For example the java.beans.PropertyChangeListener. In combination with the java.beans.PropertyChangeSupport it's possible to abstract your problem a little bit.

This example is from the Oracle Doc of Java:

```<code>public class MyBean {
 private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);

 public void addPropertyChangeListener(PropertyChangeListener listener) {
     this.pcs.addPropertyChangeListener(listener);
 }

 public void removePropertyChangeListener(PropertyChangeListener listener) {
     this.pcs.removePropertyChangeListener(listener);
 }

 private String value;

 public String getValue() {
     return this.value;
 }

 public void setValue(String newValue) {
     String oldValue = this.value;
     this.value = newValue;
     this.pcs.firePropertyChange("value", oldValue, newValue);
 }

 [...]
}
</code>```

With the String in firePropertyChange you can define, which property is changed. Of course, because of the use of the String the type safety is lost.

You won't get rid of this part before Java8:

```<code> private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);

 public void addPropertyChangeListener(PropertyChangeListener listener) {
     this.pcs.addPropertyChangeListener(listener);
 }

 public void removePropertyChangeListener(PropertyChangeListener listener) {
     this.pcs.removePropertyChangeListener(listener);
 }
</code>```

The only possibility is, to inherit it from an abstract base class. But if you do that, you can not have another base class.

Since Java8 you can define default methods in interfaces. You can define an interface like that:

```<code>public interface Listenable {
    PropertyChangeSupport getPropertyChangeSupport ();

    default void addPropertyChangeListener(PropertyChangeListener listener) {
        getPropertyChangeSupport ().addPropertyChangeListener(listener);
    }

    default void removePropertyChangeListener(PropertyChangeListener listener) {
        getPropertyChangeSupport ().removePropertyChangeListener(listener);
    }
}
</code>```

The getter is needed, because Interfaces can't have members directly.

Then it should be possible to avoid a little bit more boilerplate code:

```<code>public class MyBean implements Listenable {
 private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
 @Override
 public PropertyChangeSupport getPropertyChangeSupport () {
     return pcs;
 }

 private String value;

 public String getValue() {
     return this.value;
 }

 public void setValue(String newValue) {
     String oldValue = this.value;
     this.value = newValue;
     this.pcs.firePropertyChange("value", oldValue, newValue);
 }

 [...]
}
</code>```

Well, I don't see a possibility in Java, to do it completely without boilerplate code.

Body:
There are some abstractions in the Java API for the Observer Pattern. For example the java.beans.PropertyChangeListener. In combination with the java.beans.PropertyChangeSupport it's possible to abstract your problem a little bit.

This example is from the Oracle Doc of Java:

public class MyBean {
 private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);

 public void addPropertyChangeListener(PropertyChangeListener listener) {
     this.pcs.addPropertyChangeListener(listener);
 }

 public void removePropertyChangeListener(PropertyChangeListener listener) {
     this.pcs.removePropertyChangeListener(listener);
 }

 private String value;

 public String getValue() {
     return this.value;
 }

 public void setValue(String newValue) {
     String oldValue = this.value;
     this.value = newValue;
     this.pcs.firePropertyChange("value", oldValue, newValue);
 }

 [...]
}

With the String in firePropertyChange you can define, which property is changed. Of course, because of the use of the String the type safety is lost.

You won't get rid of this part before Java8:

 private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);

 public void addPropertyChangeListener(PropertyChangeListener listener) {
     this.pcs.addPropertyChangeListener(listener);
 }

 public void removePropertyChangeListener(PropertyChangeListener listener) {
     this.pcs.removePropertyChangeListener(listener);
 }

The only possibility is, to inherit it from an abstract base class. But if you do that, you can not have another base class.

Since Java8 you can define default methods in interfaces. You can define an interface like that:

public interface Listenable {
    PropertyChangeSupport getPropertyChangeSupport ();

    default void addPropertyChangeListener(PropertyChangeListener listener) {
        getPropertyChangeSupport ().addPropertyChangeListener(listener);
    }

    default void removePropertyChangeListener(PropertyChangeListener listener) {
        getPropertyChangeSupport ().removePropertyChangeListener(listener);
    }
}

The getter is needed, because Interfaces can't have members directly.

Then it should be possible to avoid a little bit more boilerplate code:

public class MyBean implements Listenable {
 private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
 @Override
 public PropertyChangeSupport getPropertyChangeSupport () {
     return pcs;
 }

 private String value;

 public String getValue() {
     return this.value;
 }

 public void setValue(String newValue) {
     String oldValue = this.value;
     this.value = newValue;
     this.pcs.firePropertyChange("value", oldValue, newValue);
 }

 [...]
}

Well, I don't see a possibility in Java, to do it completely without boilerplate code.


问:
Custom use of Authorization header in a REST API
I'm building a REST api where clients are authenticated using client certificates. A client in this case is not an individual user, but some sort of a presentation layer. Users are authenticated using a custom approach and it's the responsibility of the presentation layer to see that this is properly done (note: I know this is not the proper approach, but the api is not public).

I would like to pass the user name for each request (not the password), but I'm not sure where to do this. Would it be a good idea to use the Authorization header?

答:
Using the Authorization header seems like the right thing to do. It's the entire purpose of the Authorization header.

From https://www.rfc-editor.org/rfc/rfc7235#section-4.2 :

The "Authorization" header field allows a user agent to authenticate
itself with an origin server -- usually, but not necessarily, after
receiving a 401 (Unauthorized) response.  Its value consists of
credentials containing the authentication information of the user
agent for the realm of the resource being requested.

If you have your own auth scheme document it, but there's no need to reinvent the wheel.


答:
Common use cases of NoSQL are groundbreaking in their productivity gains as compared to common SQL-based databases.  There are several factors in this.

One is housekeeping.  Most NoSQLs are open source and can be installed on a workstation or a VM with a few commands, and work with reasonable defaults out of the box.  In my experience, even Postgres and MySQL are not that way; configuration is usually necessary to get started even on a workstation for development purposes.

Another is convenient development, as other answers have described in detail.  The JSON indexing capabilities of Mongo, or the key/value semantics of redis and Riak, may be all certain webapps need to get the job done, and the APIs are easy.  Some NoSQLs provide their own RESTful APIs, whereas with SQL you typically have to write them yourself.

These factors make NoSQL databases attractive for small-scale projects.  The pick-up time tends to be low.  Sure, when you go to production you have to configure for security and scale, but the ability to start coding and collaborating quickly is powerful and, I argue, groundbreaking.

Also, related to the above, for small-scale applications (like company-internal or app-to-app services), a team may be able to stand up a production NoSQL database without involving their DBA teams, and without suffering performance or integrity issues as a result.  Professional DBAs may not like this, but developers who see DBAs as a source of obstruction (right or wrong) sometimes see NoSQL as a way to bypass having to deal with them.  I confess to this - I once changed a small-scale application from Postgres to SQLite to cut out the adversarial DBA, and I have chosen to implement on Mongo rather than Oracle to avoid DBA approval processes and access restrictions.  With no adverse consequences in either case.

Title: Is there anything groundbreaking about NoSQL?

Body:
I am a very solid Relational Database guy and understand all the way to 3rd normal form, appreciate the algebraic set theory roots of SQL, and can probably relationalize a broken heart (or not).

I haven't figured out a relational database structure FOR date nights with my wife, but I HAVE thought about relational database projects ON date nights with my wife..

Now I'm hearing about NoSQL, and researching it.  Cutting to the chase, is there anything about NoSQL that is ground breaking, mathematically novel, or a "hey you don't even really need to organize your data relationally, this is so much easier" type of approach?

Is NoSQL like a super shell to the data structure? In my mind, data must ultimately have structure to be retrieved and the retrieval must be defined in a language of some sort.


问:
What's the most absurd myth about programming issues?
To put it another way... What is the most commonly held and frustrating misunderstanding about programming, you have encountered?

Which widespread and longstanding myths/misconceptions do you find hard for programmers to dispel/correct.

Please, explain why this is a myth.

答: That comments are actually useful</p>

ContentLicense: CC BY-SA 2.5

Body: That comments are actually useful


问:
How to organize repository branches?
I am trying to find a way to organize the branches in my repository in a sensible way. I would like the main branch to contain only "clean" commits: features that are (mostly) complete, no superfluous code that has no role in the current version of the application, etc.

I thought that for each feature I should have a separate development branch, and once that feature is complete, it should be merged into the main branch.

It this a valid way to organize your repository? Can you recommend a good naming scheme for the development branches? What other ways to organize your repo can you recommend?

答:
I would model something off of Vincent Driessen's Git branching model.

http://nvie.com/posts/a-successful-git-branching-model/

There are plugins for git and mercurial. Bitbucket's program source tree even has the mercurial version build in.

The main idea is that you have separate "swim lanes" for different parts of the development phase. You have one for your main, one for testing, and one for development.

For development there can be a new branch for each new feature that gets added to the product. Once complete and the code passed local testing it can be moved into the testing branch and the feature can be closed. Each feature starts with the main feature branch.

The testing branch is the only branch that should be merged into the main branch. The goal is that every feature is merged into testing and tested before being merged into the main branch.

The goal of the main branch is that it is always release ready. This means that you can deploy from it at any time. Additionally you can then have a separate branch off of the main branch to address any quick fixes if issues are discovered in production. (Read: not major issues.) i.e. typos and such.

The main problem with this model is that there is a lot of merging that happens. In most cases there should only be clean merges though it can still look confusing on a graph. I tend to like this model however because it keeps an always "clean" branch and its not too difficult to use and convert a project to using.

Take a look at the plugin for mercurial, hgflow:
https://bitbucket.org/yujiewu/hgflow/wiki/Home

Or if you prefer git-flow:
http://scottchacon.com/2011/08/31/github-flow.html

Title: How to organize repository branches?


答:
I've been using document dbs (ravendb to be specific) as my data store of choice for 3+ years now and I really don't want to look back.

At least for that sort of nosql databases the biggest question is "what goes in this document? What goes in another document? What goes in a related document?" Unfortunately there isn't a lot of good guidance on this. Then again RDBs are a 30+ year old technology so there is a pretty massive body of work there but there still are not perfect answers to all problems -- for example I would reject any entity-attribute-value solution like your scenario #2 without real, real good reasons to go EAV -- I would rather model data extensions as sub-type-tables or using some sort of extensions field comprising serialized data.

Anyhow, there are no perfect principles but there are some good guiding principles one can follow. The two that have helped me the most are:

Model your documents around transaction boundaries. Joins are much more expensive to work out and use with objects so being able to select a Foo by ID and getting all of foo makes a ton of sense and makes things easier to work with on many levels. Now, this is not to say everything need be some massive document -- transaction boundaries can be more confined than "everything to do with a piece of furniture". In the case of your scenario #1 I would probably look at the transaction boundaries as the Furniture including Materials and then a separate Instructions document. The logic being you probably manage furniture and materials together but the instructions likely come from somewhere else. Keep in mind that aggregation on the front end is pretty cheap. Categories is an interesting example which leads me to . . .

Data duplication is a-ok if you manage it right. A major underlying principle of RDBMS is "don't duplicate data" largely because it grew up in a world where disk storage was orders of magnitude more dear than it is in 2014. For document-style databases it can make sense to have copies of things within your transaction boundaries. For example let's take the furniture categories from scenario #1 -- I would probably have a FurnitureCategoryDocument that would have all the information about the category. I would also have some key information -- ID and name at least -- embedded in the documents for ease of use. This is just fine so long as you can cascade updates, which requires more code than ON UPDATE CASCADE, in your app.

Hope this helps demystify things a bit.

Title: Would these two scenarios be good candidates for a NoSQL database?

Body:
I've checked a few other threads around the topic and search around, I am wondering if someone can give me a clear direction as to why should I consider NoSQL and which one (since there are quite a few of them each with different purposes)

Why NoSQL over SQL?

Is MongoDB the right choice in my case?

https://softwareengineering.stackexchange.com/questions/5354/are-nosql-databases-going-to-take-the-place-of-relational-databases-is-sql-goin

Like many others - I started with relational databases and been working on them ever since, thus when presented with a problem, the first instinct is to always think of "I can create these tables, with these columns, with this foreign keys", etc

My overall goal is How to get into "NoSQL" mindset? ie getting away from the inclination of always thinking about tables/columns/FKs (I understand that there are cases where RDBMS is still the better way to go)

I am thinking of 2 scenarios for example just to get more concrete direction

Scenario 1

Imagine a database to model building a furniture instructions (think of IKEA instructions) where you would have the object "furniture" which would have a list of "materials" and have a list of "instructions"

Furniture - would simply have a name that have a list of Materials and Instructions

Materials - would be a name + quantity, may be we can even have "Material Category" table as well

Instructions - would simply be an ordered list of texts

My first instinct would go the RDBMS way:

Create a table called "Furniture", "Material" and "Instruction" and the approppriate columns

Create the appropriate JOIN tables as necessary and FKs

The use of this system can include searching based on materials or may be combination of materials. And may be think of extending the data stored to include information on how many people are required to build it? Difficulty level? how much time it would take?

Would something like this be a good candidate for a NoSQL database?

Scenario 2

Imagine a database to model a User database with basic information (eg. name, email, phone number, etc), but you also want to have the flexibility of being able to add any custom fields as you wish.

Think of different systems consuming this user database, each system will want to have their own custom attribute to be attached to the user

My inclination would go the RDBMS way:

Create a table for "USER" with columns: ID, name, email, phone

Create a table for "USER_ATTRIBUTE" with columns: ID, USER_ID, attr_name, attr_type, attr_value

The USER_ATTRIBUTE will allow that customization and flexibility without having to shut down the system, alter the database and restart it.

Would something like this be a good candidate for a NoSQL database?


问:
Which layer should service errors be handled in?
I am coding in a multitier architecture in Java, and I perform a query to a web service.

Is it better form for the errors returned by the service be handled in the data access layer or in the business layer?

For example, the service records a response something like this:

```<code><?xml version="1.0" encoding="UTF-8"?>
    <ReturnCode>[integer]</ReturnCode>
    <ReturnMessage>[String correponding to ReturnCode]</ReturnMessage>
    <UpdateTimestamp>[dateTime - only present if ReturnCode == 0]</UpdateTimestamp>
    <UpdateUser>[string - only present if ReturnCode == 0]</UpdateUser>
</xml>
</code>```

If I handled the errors in the data access layer, my Java code would like something like this: Error logic in DAO. (notice the data type doesn't have returnCode or returnMessage fields)

If I handled the errors in the business layer, the code would look like this: Error logic in BO. (notice the data type has returnCode and returnMessage fields)

答: I'd say that Fail-fast approach is also applicable in your situation. That is, you should handle the error as close to its source as you can. If you do this webservice call from the business layer and then want to save results of that call in the database, then you should handle error in the business layer. If it's done as a part of data access layer, handle it there.</p>


答:
The underlying layers -- service / DAO in your parlance --  should just throw exceptions when they hit them. Let the UI layer (web services in this scenario) handle how to deal with it for the client.

The main issue is if you try and handle it down low then your service / dao need to have more intimate knowlege of the top end of the stack then they really should leading to a whole host of other design issues. Being able to fire and forget exceptions like this is what makes them much more powerful than other error management techniques.

Title: Which layer should service errors be handled in?

Body:
I am coding in a multitier architecture in Java, and I perform a query to a web service.

Is it better form for the errors returned by the service be handled in the data access layer or in the business layer?

For example, the service records a response something like this:

```<code><?xml version="1.0" encoding="UTF-8"?>
    <ReturnCode>[integer]</ReturnCode>
    <ReturnMessage>[String correponding to ReturnCode]</ReturnMessage>
    <UpdateTimestamp>[dateTime - only present if ReturnCode == 0]</UpdateTimestamp>
    <UpdateUser>[string - only present if ReturnCode == 0]</UpdateUser>
</xml>
</code>```

If I handled the errors in the data access layer, my Java code would like something like this: Error logic in DAO. (notice the data type doesn't have returnCode or returnMessage fields)

If I handled the errors in the business layer, the code would look like this: Error logic in BO. (notice the data type has returnCode and returnMessage fields)


问:
Has there really not been one thing in the past 20 years that provided huge software development gains?
In No Silver Bullet, Fred Brooks makes a variety of predictions about the future of software engineering, best summed up by:

There is no single development, in either technology or in management technique, that by itself promises even one order-of-magnitude improvement in productivity, in reliability, in simplicity.

His argument is very convincing. Brooks was writing in 1986: was he right? Do developers in 2014 produce software at a rate less than 10x faster than their counterparts in 1986? By some appropriate metric -- how large has the gain in productivity actually been?

答:
While one might argue with specific metrics (ie, have things improved by a factor of 9.98?), I (speaking as something of an old-timer) have to agree with the general sentiment of Brooks' comment.

First off, there has been very little truly new technology invented since maybe 1970.  Yes, integrated circuits have gotten longer, lower, wider, and glass fiber has improved communications speeds, but for every step forward there's one back.

Compiler technology has permitted about a 10x improvement in programmer "productivity" vs 1970, when one figures function produced divided by actual coding time, but the proliferation of new or "revised" programming languages and environments means that the average programmer is spending more and more time in "catch up" mode, and less in productive activity.  Apple, Google, and Microsoft all spew out new and substantially incompatible "upgrades" to their environments at a rate that is just below that that would provoke revolt among their serfs ... er, programming customers.  Similarly, HTML/CSS/Javascript/whatever keeps getting more complex.

At one time the rate at which documentation could be produced and propagated would have limited and corralled all this "innovation", but, thanks to the Internet, rigorous documentation is no longer really necessary -- just spew the functions out and rely on bloggers to ferret out the details and make them available.

Added:  I've been thinking about this since yesterday, and in particular thinking about the project I worked on from about 1978 until 2008.  This project (the IBM System/38 and its successors) was somewhat unique in that from the start efforts were made to control the complexity of it (one being the division of the software into two roughly equal parts, with a "machine" interface between them).  In the particular area where I worked, several of my coworkers similarly were dedicated to controlling complexity (though we didn't use that term much at the time).  The result was a product that (at first) was quite robust and a "hit" with the customers pretty much from the git-go.  And it was a pleasure to work on -- like playing in a well-trained orchestra.

Of course, over the years complexity crept in, usually at the behest of market planners and managers who had no appreciation for controlling complexity (which is somehow different from just maintaining simplicity).  I don't have the feeling that this was inevitable, but it was impossible to prevent in this case without a manager (like Glenn Henry did originally) pushing back on the forces of confusion.


答:
If I want to add support for Protobuf then I have create new method in processor and copy-paste almost all the business logic

NO!

An object oriented approach

ItemProcessor "has all the business logic". Don't pollute the class, keep it strictly business logic

Your data/response structure is universal. JSON, XML, protobuf are just different syntactical representations of the same thing.

All your existing layers operate on the same data structure, all they need is to receive or send in their desired form (JSON, XML, protobuf)

You need something that transforms between the various formats so that the client, ItemProcessor, whatever, all get the format they expect.

OO Design Sketch

A ItemProcessor instance needs a Translator object to get the data to or from, or both, the desired form.

ItemProcessor maybe needs different Translators for interacting w/ the client and DAO respectively.

So we need to make a ItemProcessor/Translator composite at run time. creational design patterns help us do that.

It may be that you end up designing a single, static, Translator class.

Design patterns do not dictate your design. To start with a blank sheet of paper and say "I want to use the xyz design pattern" is wrong, wrong, wrong.

Fitting a pattern to your design

You still need to design things. Only you can define the various parts overall. From there, a creational pattern shows us how to decouple the creation of an object from its user. In this way we avoid the problem as quoted at the beginning of this answer.

Look at creation design patterns  like factory, abstract factory, and builder.

Which design pattern you use is generally driven by the complexity of building the given object. That there are 3 kinds of factory has to do with complexity.

You will tell the factory, etc. to build you something with "JSON - protobuffer" transformation for example. So maybe you get back a ItemProcessor that has a JSONprotobufTranformer object, or XMLprotobufTransformer, or ... whatever.

Title: Which pattern is suitable to send multiple type of responses through REST web service?

Body:
I have bunch of REST style web services which support XML and JSON type responses. Now I have to modify existing web services such that they provide Protobuf type of responses.

Developer who has developed these services has created three layers. Service layer, DAO layer and Processing layer.

Service layer calls one of the method of processing layer and processing layer calls DAO methods and returns either JSON or XML type response (POJO) to the service. Service layer then sends back this POJO to client.

For example, if service to add list of items is called then that code roughly looks as shown below:

```<code>@Path("/item")
class ShoppingItemService {
    // annotations
    public ItemAddResponse addList(/* parameters required for this method */) {
        ItemProcessor processor = new ItemProcessor();
        ItemAddResponse response = processor.process(/* pass some data to processing layer */);
        return Response.ok().format("XML").build(response);
    }
}
</code>```

Here ItemAddResponse is already annotated with JAXB type annotations for XML response and other annotations for JSON response.

I cannot reuse same ItemAddResponse because Protobuf generates its own classes using schema files.

Processor class has all the business logic and If I want to add support for Protobuf  then I have create new method in processor and copy-paste almost all the business logic, but just return object of protobuf generated class.

This approach is time consuming and there atleast 50+ similar methods where I have to do copy-paste work. I want to avoid this and would like to design some elegant solution.

I would like to know how can I add support for Protobuf with minimal amount of changes?

I am reading Head First Design patterns book but this book is really huge and it will take lot of time to find proper design pattern.

Is there any design pattern which can be useful in this type of situations?

Thanks,

Chandrashekhar


问:
Debugging memory corruption
First off, I do realize this is not a perfect Q&A style question with an absolute answer, but I can't think of any wording to make it work better. I don't think there is an absolute solution to this and this is one of the reasons why I'm posting it here instead of Stack Overflow.

Over the last month I have been rewriting a fairly old piece of server code (mmorpg) to be more modern and easier to extend/mod.
I started with the network portion and implemented a 3rd party library (libevent) to handle stuff for me.
With all the re-factoring and code changes I introduced memory corruption somewhere and I have been struggling to find out where it happens.

I can't seem to reliably reproduce it on my dev/test environment, even when implementing primitive bots to simulate some load I don't get crashes any more (I fixed a libevent issue which caused some stuff)

I have tried so far:

Valgrinding the hell out of it - No invalid writes until the thing crashes (which may take 1+ day in production.. or just an hour) which is really baffling me, surely at some point it would access invalid memory and not overwrite stuff by chance? (Is there a way to "spread out" the address range?)

Code-Analysis tools, namely coverity and cppcheck. While they did point out some.. nastiness and edge cases in the code there was nothing serious.

Recording the process until it crashes with gdb (via undodb) and then working my way backwards. This /sounds/ like it should be doable, but I either end up crashing gdb by using the auto-complete feature or I end up in some internal libevent structure where I get lost since there's too many possible branches (one corruption causing another and so on).
I guess it would be nice if I could see what a pointer originally belongs to/where it was allocated, that would eliminate most of the branching-issues. I cant run valgrind with undodb though, and I the normal gdb record is unusably slow (if that even works in combination with valgrind).

Code review! By myself (thoroughly) and having some friends look over my code, though I doubt it was thorough enough. I was thinking about maybe hiring a dev to do some code review/debugging with me, but I cant afford to put too much money in it and I wouldn't know where to look for someone who'd be willing to work for little-to-no money if he doesn't find the issue or anyone qualified at all.

I should also note: I usually get consistent backtraces.
There are a few places where the crash happens, mostly related to the socket class becoming corrupted somehow. Be it an invalid pointer pointing to something which isn't a socket or the socket class itself becoming overwritten (partially?) with gibberish. Although I suspect it's crashing there the most since that's one of the mostly used parts, so it's the first corrupted memory which gets used.

All in all this issue has had me busy for nearly 2 month (on and off, more of a hobby project) and is really frustrating me to the point where I become grumpy IRL and think about just giving up.
I just can't think about what else I am supposed to do to find the issue.

Are there any useful techniques I missed?
How do you  deal with that? (It can't be that common since there isn't much information about this.. or I'm just really blind?)

Edit:

Some specs in case it matters:

Using c++(11) via gcc 4.7 (version supplied by debian wheezy)

The codebase is around 150k lines

Edit in response to david.pfx post: (sorry for the slow response)

Are you keeping careful records of crashes, to look for patterns?

Yes, I still have dumps of the recent crashes lying around

Are the few places really similar? In what way?

Well, in the most recent version (they seem to change whenever I add/remove code or change related structures) it would always get caught in an item timer method. Basically an item has a specific time after which it expires and it sends updated info to the client.
The invalid socket pointer would be in the (still valid as far as I can tell) Player class, mostly related to that.
I am also experiencing loads of crashes in the cleanup phase, after the normal shutdown where it's destroying all the static classes that haven't been explicitly destroyed (__run_exit_handlers in the backtrace). Mostly involving std::map of one class, guessing that's just the first thing that comes up though.

What does the corrupt data look like? Zeros? Ascii? Patterns?

I haven't found any patterns yet, seems somewhat random to me. It's hard to tell since I don't know where the corruption started.

Is it heap-related?

It's entirely heap-related (I enabled gcc's stack guard and that didn't catch anything).

Does the corruption happen after a free()?

You're going to have to elaborate a bit on that one.
Do you mean having pointers of already free'd objects lying around?
I'm setting every reference to null once the object gets destroyed, so unless I missed something somewhere, no. That should show up in valgrind though which it didn't.

Is there something distinctive about the network traffic (buffer size, recovery cycle)?

The network traffic consists of raw data. So char arrays, (u)intX_t or packed (to remove padding) structs for more complex things, each packet has a header consisting of an id and the packet size itself which is validated against the expected size.
They are around 10-60bytes with the biggest (internal 'bootup' packet, fired once at startup) having a size of a few Mb.

Lots and lots of production asserts. Crash early and predictably before the damage propagates.

I once had a crash related to std::map corruption, each entity has a map of it's "view", each entity that can see it and vice versa is in that.
I added a 200byte buffer in front and after, filled it with 0x33 and checked it before each access. The corruption just magically vanished, I must've moved something around which made it corrupt something else.

Strategic logging, so you know accurately what was happening just before. Add to the logging as you get closer to an answer.

It works.. to an extend.

In desperation, can you save state and auto-restart? I can think of a few pieces of production software that do that.

I somewhat do that. The software consists of a main "cache" process and some other worker ones which all access the cache to get and save stuff. So per crash I don't lose much progress, it still disconnects all the users and so on, it's definitely not a solution.

Concurrency: threading, race conditions, etc

There's a mysql thread to do "async" queries, that's all untouched though and only shares information to the database class via functions with all lock.

Interrupts

There's an interrupt timer to prevent it from locking up that just aborts if it didn't complete a cycle for 30 seconds, that code should be safe though:

```<code>if (!tics) {
    abort();
} else
    tics = 0;
</code>```

tics is volatile int tics = 0; which is increased each time a cycle is completed. Old code too.

events/callbacks/exceptions: corrupting state or the stack unpredictably

Lots of callbacks are being used (async network I/O, timers), but they shouldn't do anything bad.

Unusual data: unusual input data/timing/state

I've had a few edge cases related to that. Disconnecting a socket while packets are still being processed resulted in accessing a nullptr and such, but those have been easy to spot so far since every reference gets cleaned up right after telling the class itself it's done. (Destruction itself is handled by a loop deleting all the destroyed objects each cycle)

Dependency on an asynchronous external process.

Care to elaborate? This is somewhat the case, the cache process mentioned above.
Only thing I could imagine off the top of my head would be it not finishing quick enough and using garbage data, but that's not the case since that's using network too. Same packet model.

答:
Use a debugging version of malloc/free.  Wrap them and write your own if necessary. Lots of fun!

The version I use adds guard bytes before and after every allocation, and maintains
an "allocated" list which free checks freed chunks against.  This catches most buffer
overrun and multiple or rogue "free" errors.

One of the most insidious sources of corruption is continuing to use a chunk after
it has been freed.  Free should fill the freed memory with a known pattern (traditionally,
0xDEADBEEF ) It helps if allocated structures include a "magic number" element, and
liberally include checks for the appropriate magic number before using a structure.

Title: Debugging memory corruption


问:
How to ensure the success of processes after a deadlock exception occurs?
No matter the programming language is and whatever the database is, the concept should be the same:

I have 2 threads, each locking some database entities and inserting new data in database.

Let's suppose the fact that those threads run simultanously may cause deadlock exceptions.

Obviously, the main action we can do first of all is to analyse the code deeply to find a good alternative avoiding any deadlocks.

But sometimes, we really can't avoid them.

Should I catch the exception and retry the concerned process?

At first glance, it would be acceptable, but...what if those conflicting processes are replayed at the same time, other deadlocks again (the snake biting its tail)?

What is a good way to ensure that the processes eventually succeed?

答:
Implement some sort of timeout or dead-man mechanism.

For example, here is a (simplified) Enqueue method on a blocking queue:

```<code>public void Enqueue(T item)
{
    int count = 0;

    lock (_locker)
    {
        while (queue.Count >= MaximumSize)
        {
            Monitor.Wait(_locker), 100) // Wait for 1/10 of a second.
            count++;
            if (count >= 100) // Try for 10 seconds
            {
                throw new DeadlockDetectedException();
            }
        }

        queue.Enqueue(item);
        Monitor.PulseAll(_locker);
    }
}
</code>```

Body:
Implement some sort of timeout or dead-man mechanism.

For example, here is a (simplified) Enqueue method on a blocking queue:

public void Enqueue(T item)
{
    int count = 0;

    lock (_locker)
    {
        while (queue.Count >= MaximumSize)
        {
            Monitor.Wait(_locker), 100) // Wait for 1/10 of a second.
            count++;
            if (count >= 100) // Try for 10 seconds
            {
                throw new DeadlockDetectedException();
            }
        }

        queue.Enqueue(item);
        Monitor.PulseAll(_locker);
    }
}


答: Naive solution: Have both threads wait a random amount of time before trying again. Eventually one thread will go before the other (probably sooner rather than later.)</p>

Title: How to ensure the success of processes after a deadlock exception occurs?

Body:
No matter the programming language is and whatever the database is, the concept should be the same:

I have 2 threads, each locking some database entities and inserting new data in database.

Let's suppose the fact that those threads run simultanously may cause deadlock exceptions.

Obviously, the main action we can do first of all is to analyse the code deeply to find a good alternative avoiding any deadlocks.

But sometimes, we really can't avoid them.

Should I catch the exception and retry the concerned process?

At first glance, it would be acceptable, but...what if those conflicting processes are replayed at the same time, other deadlocks again (the snake biting its tail)?

What is a good way to ensure that the processes eventually succeed?


问:
Scrum: Short VS long sprint
We were trying to figure out the optimal sprint length for our project. After working on a 3-weeks basis we thought that cutting the sprint to 2-weeks would provide better velocity.

The advantages were clear - shorter feedback loop, small stories (with user value) and so on.
On the other hand, there are a lot of disadvantages such as ceremonies (planning, retrospective) and so on in which we don't produce and now happen more often.

I was wondering how, for a new team, can we decide on optimal sprint length?

答:
Since you asked about a new team, I wanted to add some thoughts.  I have been working with Scrum and other Agile methods for over 15 years and now I always recommend that new teams start with 1 week long Sprints.  There are three critical reasons for this:

Shorter Sprints help teams get to a high-performance state faster. Long Sprints usually mean that it takes 18 months to two years to get to that state.  Short one-week Sprints help a team get to that high-performance state in just two or three months.  Of course, other things need to be in place as well to get to high performance (see "The Wisdom of Teams" by Katzenbach and Smith).

As others have mentioned, a shorter Sprint increases the feedback cycle.  A one week long Sprint seems to be optimal for most teams doing software development or product development.  The feedback should mostly be during the Sprint Review which should take the place of your traditional UAT process.

Finally, shorter Sprints put pressure on your team to deal with organizational obstacles to frequent delivery.  That pressure is very uncomfortable at first, but is essential to improving your overall development processes including planning, compliance, releases, etc.

I wrote an article called 21 Tips for Choosing a Sprint Length that might be of interest.

Title: Scrum: Short VS long sprint


问:
How come centralized source control solutions don't do branching in place
With centralized source control like Perforce and SVN, when they create a branch, they create a whole new directory. However, git and other distributed source control solutions, in my experience, are able to update the branch in place as it were. That is, they apply the differences between the two branches to the files.

This seems to me to be the far superior option as it saves huge amount of duplication for large projects, quicker to switch branches etc...

What is the reason for this? I can't see any reason why there being a centralized authority affects the clients ability to diff the two branches and modify the changes.

答:
Legacy software isn't designed, it evolves.  Svn was designed to improve CVS, which was designed to improve RCS.  What did RCS replace?  Think about how you would maintain branches without any version control system at all.  You put them into separate directories.

Putting those separate directories under version control requires a smaller leap in your mental model than git's system, which seems like an obvious improvement over svn, but is far from obvious when compared to no VCS at all.

Also keep in mind that branch switches are much faster on distributed version control systems because they don't have to go to the network.  At work where you probably have a gigabit connection to your server, that's not such a big deal.  When CVS was invented, 9600 baud was considered a high speed connection.  Disks and CPUs were also much slower then.  A git checkout of a project the size of the Linux kernel might have taken several minutes on computers of that era.

That being said, a lot of your criteria for calling git's model "superior" aren't necessarily accurate.  My development computer at work has a terabyte disk drive on it.  I have four branches checked out of a 60,000 file project, and am still only using 80 GB, and that includes my OS and everything else.  Clearly, duplication isn't a problem.  As far as switching branches, git is fast, but a cd is still faster.

Over time software tends to borrow the best features from competitors, and I think in-place checkouts will eventually be an option on most centralized version control systems.  There's nothing inherently preventing it today, just history.  In fact, it might already be available and you just aren't using it.  For example, Perforce streams allow in-place branching.


问:
Should I place functions that are only used in one other function, within that function?
Specifically, I'm writing in JavaScript.

Let's say my primary function is Function A. If Function A makes several calls to Function B, but Function B is not used anywhere else, then should I just place Function B within Function A?

Is that good practice? Or should I still put Function B at the same scope as Function A?

答: You should not nest it, because otherwise the nested function will be re-created every time you call the outer function.</p>


问:
Log within try-catch or after?
Should one log success of an operation within a try-catch-block, or after it?

Example:

```<code>try
    do x
    log('successful')
catch
    log('fail')
end
</code>```

or is this better:

```<code>try
    do x
catch
    log('fail')
end
log('successful')
</code>```

I would say that a logger should never fail, but what if, for some reason, it does anyway?

In this specific case, I  had a logfile on a network drive which disconnected due to actions of do(x), which however did not fail -- obviously my try failed, but my operation worked.
However, I find the latter example harder to read, and I try to keep my code as tight as possible for readability.

答:
I would say that a logger should never fail, but what if, for some reason, it does anyway?

That makes in your scenario no sense:

```<code>try
    do x
    log('successful')
catch
    log('fail')
end
</code>```

If your first logger fails, an exception is thrown and you are going to the catch block. But if the logger fails in the first place, it would do so in the second place. If one takes your path you would have to write an infinite amount of try-catch if you want to make sure one of the loggers throws an exception and also an infinite amount of logger.

Your second approach is misleading as others have written.

Though it makes sense doing it the first way: but with different log-levels:

```<code>try
    do x
    log.info('operation successful')
catch
    log.error('something went terribly wrong') #or log.warn()
end
</code>```

But there is another programming paradigm, which is helpful:
http://en.wikipedia.org/wiki/Aspect-oriented_programming
With AOP you could separate your concerns better.

Title: Log within try-catch or after?

Body:
I would say that a logger should never fail, but what if, for some reason, it does anyway?

That makes in your scenario no sense:

try
    do x
    log('successful')
catch
    log('fail')
end

If your first logger fails, an exception is thrown and you are going to the catch block. But if the logger fails in the first place, it would do so in the second place. If one takes your path you would have to write an infinite amount of try-catch if you want to make sure one of the loggers throws an exception and also an infinite amount of logger.

Your second approach is misleading as others have written.

Though it makes sense doing it the first way: but with different log-levels:

try
    do x
    log.info('operation successful')
catch
    log.error('something went terribly wrong') #or log.warn()
end

But there is another programming paradigm, which is helpful:
http://en.wikipedia.org/wiki/Aspect-oriented_programming
With AOP you could separate your concerns better.


问:
Should CountDownLatch be used for waiting for an event?
I have a situation here, where I have a loop that loops x amount of times. The value x is determined by the user. Everytime it loops, I want it to wait for a button click before continuing the loop. ie. something like this (pseudocode):

```<code>loop(x times){
Button b;
  wait until a click
  when clicked{
    b.onclicklistener{
      setText(x);
    }
  }
}
</code>```

I am using this for an android app, so with Thread.wait()/Thread.notify() what I learned was that it interrupts the main UIthread, so I cannot use that.

So what I believe the only two options that I can use to kind of "pause" the loop is CountDownLatch.await() or AsyncTask (I don't know what that does, have not researched about it)

Would CountDownLatch.await()/CountDownLatch.countDown() be the best methods and class to use in this example, or should I look into AsyncTask?

答:
Instead of thinking of it as the button "unpausing" a loop, I would prefer to think of it as something running every time you click the button, like:

```<code>b.onclicklistener {
   return (or remove listener) if loops > x
   loops++
   setText(x)
   restOfLoop()
}
</code>```

That being said, if you absolutely require your loop to run in a separate thread, a regular Semaphore would suffice in your situation.  Just call acquire() in the loop where you want it to pause, and release() in the click listener.

Body:
Instead of thinking of it as the button "unpausing" a loop, I would prefer to think of it as something running every time you click the button, like:

b.onclicklistener {
   return (or remove listener) if loops > x
   loops++
   setText(x)
   restOfLoop()
}

That being said, if you absolutely require your loop to run in a separate thread, a regular Semaphore would suffice in your situation.  Just call acquire() in the loop where you want it to pause, and release() in the click listener.


问:
How to approach program design with respect to data strucutres and algorimths - is there an equivelant of the OO design process for d.s.'s + algs?
My applogies for probably the worst written body of text I have produced in my life and many thanks to those willing to plough through it all.

I was (and still am) not able to clearly express what I was asking for, since by the nature of my question I cannot be clear about what exactly it is I am after. Fundementally I strongly suspect something in my knowledge of software development is missing, but because "I don't know, what I don't know", I cannot specify what exactly I am looking for (and indeed can't be sure whether such a thing exists) - hence I suppose I am asking for some direction in this matter. I kind of thought if proffessional developers did not know of/ did not use such a thing in the process of desingin code - then there is nothing to know in this respect (and I can stop worrying).

Essentially I fear something akin to having learned to program, but having remained unaware of the subject of proper OO software design. I have studied standard data strucutres and algorithms (to the extent of coverage found in typical undergraduate text books), but can't help feeling that there should be something more. I hence can't help feeling like I've learned the very basics, but not become aware of the data structures and algs. equivelant of OO design. In essence I sort of imagine there is some "process" to solution design (in the context of the d.s.s and algs. side of programming - like there is to the OO design side of coding software - I see them both as being part of the same whole in programming) that informs one about how to go about the task of writing "good" code (in the d.s. and algs aspect of the program code written). I fear in absence of knowledge of such a process/ subject, I would be incompetent as a graduate developer (rather like I would consider a new employee, if his code turned out to be uninformed by the OO design process/ principles). I would consider the code produduced by someone lacking in knowledge of OO design principles/ processes to be "bad code" - and hence him to be incompetent. At the moment I fear my knwolege with respect to the d.s. + algs. side of coding puts me in a similar position. Thus, can anyone tell me what I might yet need to be aware of in this respect?

(Another way of putting my question might be if I were to say: "Object Oriented design principles and patterns, is to programming, what "...", is to data strucutres and algorithms". Could anyone tell me what one might complete this sentence with, or whether there is nothing to consider here)

答:
First things first, OOD isn't something you can approach systematically. There's no checkboxes for something that will vary based on who is doing it and what environment you're in. So your assumption that efficiency will follow a similar path is flawed.

In practice, very few companies care very much about efficient code. Those blurbs are (very often) put in by HR people or middle managers who have no idea what it means. Companies want your software to work. They want it to keep working. And unless your company actually has hard requirements on performance and scalability, you have a lot of leeway about how your solutions are implemented.

But let's for a moment consider what it does actually mean in the minority of jobs where it matters. You are right that "efficient code" is focused primarily on the time and space complexity of the algorithms and data structures in your books. Unfortunately, it is seldom clear cut what your inputs will look like, or what metric you're optimizing for (can you take up more memory in exchange for faster performance? Can you sacrifice accuracy for disk space?). In practice, the choices you face (both in efficiency and OOD) will be a series of trade-offs. There will be no check boxes to mark off. There is very often no single correct answer.

You'll need to make some assumptions about what your input looks like, and will eventually look like. You'll need to make some assumptions about what is important to (most of) your users. And then you'll need to make your best guess about the appropriate solution to balance meeting all of your demands while using as little of your resources.


问:
Does Java's Object.wait() really achieve nanosecond accuracy?
I was checking out the Java 8 standard library source code just out of curiosity, and found this in java/lang/Object.java. There are three methods named wait:

public final native void wait(long timeout): This is the core of all wait methods, which has a native implementation.

public final void wait(): Just calls wait(0).

And then there is public final void wait(long timeout, int nanos).

The JavaDoc for the particular method tells me:

This method is similar to the wait method of one argument, but it allows finer control over the amount of time to wait for a notification before giving up. The amount of real time, measured in nanoseconds, is given by:

```<code>1000000*timeout+nanos
</code>```

But this is how the method achieves "finer control over the amount of time to wait":

```<code>if (nanos >= 500000 || (nanos != 0 && timeout == 0)) {
    timeout++;
}

wait(timeout);
</code>```

So this method basically does a crude rounding of nanoseconds to milliseconds. Not to mention that anything below 0.5ms will still be rounded up to 1ms.

Is this piece of code bad/unnecessary, or am I missing some unseen virtue of declaring this method, and its no argument cousin as the way they are?

答:
I think what happens is that sub-millisecond accuracy is around the corner and they are thinking forward.

Since a new method is needed, instead of going for microseconds the designers decided to go straight to nanoseconds.  Not because there is a need today for this level of accuracy, but to be ready for the future.

The implementation that rounds the time to milliseconds is there for compatibility.  You will be able to take advantage of the new method on faster computers and JVMs, while the same code will sill run on older JVMs that map the call to millisecond accuracy.


问:
Real time data synchronization techniques between two systems
I need to come up with a design for real-time data update from a COTS product (a Point of Sales system) to a custom-built .NET application (an Inventory Management system).

Particularly, any sales transaction happening in POS system needs to update the inventory database (present in Inventory Management system) immediately (in real time). The only way any other system can communicate to the POS system is via its API exposed as web services.

I have thought about introducing a Service Bus (or any such EAI tool) in between the two systems and taking advantage of the publish-subscribe model so that any sales depletion happening in POS system will trigger data update to the IM system via the service bus. My questions are:

Is it a good/feasible solution?

Do you have any other suggestions for such real-time data synchronization between different systems?

答:
First, as the comments point out, you aren't really talking about realtime here -- that has a pretty strict definition and implies lots of stuff that isn't really on the table. You are talking about connecting two systems in a near realtime sort of manner.

Anyhow, yes, the best bet would be some sort of message queueing solution here. The POS system would push updates into the queue and the inventory system would read the queue. Pub/sub could come into play here as well depending on how you want to do things on the back-side.

The reason you want message queuing and not some really hard, realtime sort of dependency is because you probably don't want to stop taking transactions if/when the inventory management goes down for whatever reason. Rather you can record things (message queue) and then replay them when it comes back up.

The real question is does that POS system have the ability to push data out somehow or are you reduced to some sort of middleware polling solution grabbing transactions and stuffing them into the message queue.


问:
Managing non-domain application behaviour in CQRS
Here's a scenario.

I am building QueueUnderflow, a community-edited Q&A site for people who haven't yet grasped the basics of data structures. My users don't like it when people edit their posts so, because I want to encourage as much conflict as possible, I want to send email notifications whenever a user's post is edited so he can log in and insult the editor.

I am experimenting with CQRS. I'm not using event sourcing and have a single data store but I am using event handlers to deal with the persistence of any state changes.

In a normal N-Tier/CRUD app this might be triggered by an application facade or service layer. In my CQRS architecture, I can see a few options:

The event handler: I like this idea because it seems that sending a notification email is a concern that could well lie with an independent part of the system that wants to listen for an appropriate time to send an email and then send it, transparent to the rest of the system. I dislike this idea because Greg Young (reasonably) points out in his videos that if I were to introduce event sourcing, this would result in the same notification emails being sent out potentially many times.

The command handler: This is a more explicit approach that still makes sense to me; however, it seems that the general consensus is that command handlers should just be for coordinating domain objects and not directly performing behaviour.

The domain: This seems plain wrong. The sending of notification emails is a concern of the application, not so much the domain which is concerned primarily with business logic.

The presentation layer (or potentially some intermediate application facade that simply hands a command off to its handler and performs other application-specific actions such as sending notifications).

The question is - what part of my architecture is responsible for sending emails?

答:
The command handler should not perform behavior that modifies the internal state, but adding  behavior that is necessary to complete the command is acceptable.

For instance: A ChargeCreditCard command must contact the merchant provider to determine if the charge is successful, so it must be part of the command handling.

The command handler would then send the result as a CreditCardCharged or CreditCardDeclined event that then modifies the application state.

If there is a requirement that a notification be issued for each charge, it must also be part of the command handling.


问:
Old Browsers and Polyfills

Polyfilling, when is it too much?

I've summed up this question into one sentance.

Is it practical to polyfill all of the IE8 deficiencies with one solid javascript file, as in, would there be any drawbacks (performance) or blockages (impossibility to polyfill some features)?

Question Deprecation: (history)

I've been working with my company for about half a year now, and we've constantly been fighting with clients on upgrading their browsers. Everyone is of the mind that moving to a newer browser is only going to introduce more problems than solutions (they have internal, old web applications designed for in-house work)

I'm at the point where I've had to hunt down and polyfill so many things with IE8 that I'm wondering if there is any draw-back to polyfilling every deficiency that IE8 has (or at least making it so that the javascript doesn't error/fail)

Is it unreasonable to expect to be able to polyfill all of the deficiencies in older outdated browsers such as IE6-IE8, or would that be a bad idea (performance? stability?)...

If this is a possibility, how come there are no full-feature polyfill libraries, everyone will point to "modernizr", that is FEATURE DETECTION, and offers no polyfills of it's own.

So, wheres the IE8 master/full polyfill library hiding, or am I gonna have to slave over this?

Edit: the reason why I'm not just picking out the parts I need, is because there might be deficiencies we are not aware of, I'd love to have a fix-all for Internet Explorer, so if this doesn't exist I'm going to make it myself.*

答:
Is it practical to polyfill all of the IE8 deficiencies with one solid
  javascript file ...?

Not really, No. As an example, start with <canvas>. There are several "polyfills" available, but most farm out the work to a browser plugin like Flash or Silverlight. This is a decidedly different approach than simply monkey patching the JavaScript Array prototype to add an indexOf function. A faux canvas depends on third party software. That may be better than nothing, but it's certainly not bullet-proof. It probably doesn't make sense to include it in a master IE8 polyfill since everyone's goals and technical restrictions vary.

SVG polyfills share the same problem. Some target VML, others <canvas>, and at least one targets Flash. HTML5 audio and video polyfills fall back to Flash. Geolocation and Web Sockets polyfills employ very different mechanisms than the native API. They might work great for you. Then again, they might not.

You may have better luck with ECMAScript shims, but even there you may run into issues. As an example, take a look at the "may fail" API of the es5-shim. It's just not possible to guarantee perfect behavior without native support.

In the end, it's best for everyone to pick and choose what they'll use and when. It's a pain, but your users and support staff will thank you for it.

Title: Old Browsers and Polyfills


问:
Why were magic methods implemented in C#?
In C#, I started seeing all these magic methods popping up, without being backed up by an interface. Why was this chosen?

Let me explain.

Previously in C#, if an object implemented the IEnumerable interface, it would automatically be iterable by a foreach loop. That makes sense to me, since it's backed up by an interface, and if I were to have my own Iterator function inside the class being iterated through, I could do that without worrying that it would magically mean something else.

Now, apparently, (not sure when), these interfaces are no longer required. It just needs to have the right naming conversions.

Another example is making any object awaitable by having a method named exactly GetAwaiter which has a few specific properties.

Why not make an interface like they did with IEnumerable or INotifyPropertyChanged to back this "magic" up statically?

More details on what I mean here:

http://blog.nem.ec/2014/01/01/magic-methods-c-sharp/

What are the pros and cons of magic methods, and is there anywhere online where I can find anything on why these decisions were made?

答:
In general “magic methods” are used when it's not possible to create an interface that would work the same way.

When foreach was first introduced in C# 1.0 (that behavior is certainly nothing recent), it had to use magic methods, because there were no generics. The options basically were:

Use the non-generic IEnumerable and IEnumerator that work with objects, which means boxing value types. Since iterating something like a list of ints should be very fast and certainly shouldn't create lots of garbage boxed values, this is not a good choice.

Wait for generics. This would probably mean delaying .Net 1.0 (or at least foreach) by more than 3 years.

Use magic methods.

So, they chose option #3 and it stayed with us for backwards compatibility reasons, even though since .Net 2.0, requiring IEnumerable<T> would have worked too.

Collection initializers can look different on each collection type. Compare List<T>:

```<code>public void Add(T item)
</code>```

and Dictionary<TKey, TValue>:

```<code>public void Add(TKey key, TValue value)
</code>```

You can't have a single interface that would support only the first form for List<T> and only the second for Dictionary<TKey, TValue>.

LINQ methods are usually implemented as extension methods (so that there can be just a single implementation of e.g. LINQ to Object for all types that implement IEnumerable<T>), which means it's not possible to use an interface.

For await, The GetResult() method can return either void or some type T. Again, you can't have a single interface that can handle both. Though await is partially interface-based: the awaiter has to implement INotifyCompletion and can also implement ICriticalNotifyCompletion.

ContentLicense: CC BY-SA 4.0

Body:
In general “magic methods” are used when it's not possible to create an interface that would work the same way.

When foreach was first introduced in C# 1.0 (that behavior is certainly nothing recent), it had to use magic methods, because there were no generics. The options basically were:

Use the non-generic IEnumerable and IEnumerator that work with objects, which means boxing value types. Since iterating something like a list of ints should be very fast and certainly shouldn't create lots of garbage boxed values, this is not a good choice.

Wait for generics. This would probably mean delaying .Net 1.0 (or at least foreach) by more than 3 years.

Use magic methods.

So, they chose option #3 and it stayed with us for backwards compatibility reasons, even though since .Net 2.0, requiring IEnumerable<T> would have worked too.

Collection initializers can look different on each collection type. Compare List<T>:

public void Add(T item)

and Dictionary<TKey, TValue>:

public void Add(TKey key, TValue value)

You can't have a single interface that would support only the first form for List<T> and only the second for Dictionary<TKey, TValue>.

LINQ methods are usually implemented as extension methods (so that there can be just a single implementation of e.g. LINQ to Object for all types that implement IEnumerable<T>), which means it's not possible to use an interface.

For await, The GetResult() method can return either void or some type T. Again, you can't have a single interface that can handle both. Though await is partially interface-based: the awaiter has to implement INotifyCompletion and can also implement ICriticalNotifyCompletion.


答: Depending on what you are doing performance can be a big issue. Polyfills can fix many programming / api issues but you can't make the old javascript engine anywhere near as performant as modern browsers. Things like filtering large lists client side will cause noticeable lag on IE but will work fine on modern browsers for example.</p>

Title: Old Browsers and Polyfills

Body:
Polyfilling, when is it too much?

I've summed up this question into one sentance.

Is it practical to polyfill all of the IE8 deficiencies with one solid javascript file, as in, would there be any drawbacks (performance) or blockages (impossibility to polyfill some features)?

Question Deprecation: (history)

I've been working with my company for about half a year now, and we've constantly been fighting with clients on upgrading their browsers. Everyone is of the mind that moving to a newer browser is only going to introduce more problems than solutions (they have internal, old web applications designed for in-house work)

I'm at the point where I've had to hunt down and polyfill so many things with IE8 that I'm wondering if there is any draw-back to polyfilling every deficiency that IE8 has (or at least making it so that the javascript doesn't error/fail)

Is it unreasonable to expect to be able to polyfill all of the deficiencies in older outdated browsers such as IE6-IE8, or would that be a bad idea (performance? stability?)...

If this is a possibility, how come there are no full-feature polyfill libraries, everyone will point to "modernizr", that is FEATURE DETECTION, and offers no polyfills of it's own.

So, wheres the IE8 master/full polyfill library hiding, or am I gonna have to slave over this?

Edit: the reason why I'm not just picking out the parts I need, is because there might be deficiencies we are not aware of, I'd love to have a fix-all for Internet Explorer, so if this doesn't exist I'm going to make it myself.*