问:
Feasibility of continuous delivery with a mixture of development platforms
What are the major obstacles (and potential solutions, if known) for implementing "continuous delivery" software development when an application relies on varied programming languages or modules... especially when the same version-control and/or automated testing packages do not exist for those disparate platforms?

For example, a Java web application that is deployed and running on a Linux host, talking to a DB2 database back-end via JDBC, with occasional calls to stored-procedures that wrap RPG or COBOL or C programs on that same back-end?  What if the code for all of that cannot be contained in the same version-control/source-control system?

Is it possible to get to the illusive "push-botton release" in this situation?  Or do you have to have EVERYTHING in the SAME VCS?  Additionally, how would one manage the creation/modification of database schema, and so-called soft-code control values in the database?

Is there a more-or-less "canonical" published work on this type of thing?

答:
Inedo's BuildMaster is a tool designed to handle scenarios like this. The "secret" is that it's not a project-centric tool (as traditional CI might be), but it models applications and their deployable components. As such, there are lots of users who have multiple VCS (from Git to ClearCase) to build applications on multiple platforms (Java, .Net, etc) on multiple operating systems (Windows or anything SSH-supporting).

There's a free version of BuildMaster that could probably handle a lot of the problems you're trying to solve. If you're familiar with The Daily WTF, Alex wrote a nice (but but long) article that introduces the platform. He's behind the site, and also the founder of Inedo.

(Disclaimer: I work at Inedo)


问:
Should I create a separate table for every derived type?
We are having a debate and I am struggling to decide which side to come down on.  Basically we have an event type lets say like like this (simplified example).

```<code>public class Event
{
    public virtual int EventId { get; set; }
    public virtual DateTime DateTimeOccurred { get; set; }
}
</code>```

Many different events in our system can inherit from this so we could have:

```<code>public class UserUpdatedEvent : Event
{
    public virtual User User { get; set; }
}
</code>```

So basically any event can inherit from the base event and then we can track what is happening in our system which we need to do as tasks occur off these events, etc.

My question is should we have a table in the DB for every event so we can enforce foreign keys in this example to the user using table per type with each sub type referencing the base Events table.  Or should we have a single table and a column, maybe linking to an enum saying what the event is and then another column containing the primary key to that object but we won't be able to enforce referential integrity (maybe with a trigger but not ideal and not full proof).

The pros for a single table are speed, no joins, simpler to build/maintain, etc.  The cons are no referential integrity, we are using EF and table per type does work very well, on the other hand we produce a lot of reports and these are direct SQL sprocs for speed and the joins will become pretty heavy.

How do people normally come down on a situation like this?

Many thanks for any insight into existing systems with similar problems and helping us settle this decision.

答:
A single table is a good approach when the subclasses don't have many attributes or associations with other classes.

Otherwise the table would be full of attributes that only make sense for some rows and you´ll need to add all kinds of constraints to check that they only contain values for rows of the appropriate "type".

As you are suggesting, this really is a tradeoff, and you should weigh the benefits and disadvantages of both solutions.

Another approach you didn't mention can be to use one table per type and not use a common table at all. This way you need to repeat the columns in the different tables.

You can have referential integrity

You don't need joins to retrieve the information for events (at the row-level).

You don't need type discovery in the application since you know which table you are querying.

Of course this also has some disadvantages:

Unique key of an event: since they live in different tables you can't use an autonumber. You could circumvent this problem by using a GUID as your unique key.

Querying an event of which you don't know the type.

Querying all events requires one query per table.

In the end there's no "best" solution over all, there's only a best solution for your case. You're the only that has all the data and the information needed to make that decision.

ContentLicense: CC BY-SA 3.0

OwnerDisplayName: Kenneth


答:
I will have to dissapoint you, but there is no simple thumb rule telling you how to model object inheritance in a relational database. However the widely used solutions are well documented and it is easy to find good articles about them listing the pros and cons.
The commonly used terms for your proposed solutions are (searching will be more efficient using these) :

Using one table: Single table inheritance

Multiple tables: Class Table Inheritance

Other solutions you didn't mention in your question:

Concrete Table Inheritance

Using Semistructured Data

You have to consider all the pros and cons to decide which suits your particular case best.

Title: Should I create a separate table for every derived type?

Body:
We are having a debate and I am struggling to decide which side to come down on.  Basically we have an event type lets say like like this (simplified example).

```<code>public class Event
{
    public virtual int EventId { get; set; }
    public virtual DateTime DateTimeOccurred { get; set; }
}
</code>```

Many different events in our system can inherit from this so we could have:

```<code>public class UserUpdatedEvent : Event
{
    public virtual User User { get; set; }
}
</code>```

So basically any event can inherit from the base event and then we can track what is happening in our system which we need to do as tasks occur off these events, etc.

My question is should we have a table in the DB for every event so we can enforce foreign keys in this example to the user using table per type with each sub type referencing the base Events table.  Or should we have a single table and a column, maybe linking to an enum saying what the event is and then another column containing the primary key to that object but we won't be able to enforce referential integrity (maybe with a trigger but not ideal and not full proof).

The pros for a single table are speed, no joins, simpler to build/maintain, etc.  The cons are no referential integrity, we are using EF and table per type does work very well, on the other hand we produce a lot of reports and these are direct SQL sprocs for speed and the joins will become pretty heavy.

How do people normally come down on a situation like this?

Many thanks for any insight into existing systems with similar problems and helping us settle this decision.

OwnerDisplayName: Hari


答: You can have a addtional XML column that will contain all the addtional data requirement for inherited classes. This might be option of you have lot of variations that you cannot handle using tables.</p>

OwnerDisplayName: DKP


问:
Designing application flow
I am creating a web application in java where I need to mock the following flow.

When user trigger a certain process (add product to cart), I need to pass through following steps

Need to see in HTTP Session if user is logged in.

Check HTTP Session if shopping cart is there

If user exist in HTTP Session and his/her cart do not exist in HTTP Session

Get user cart from the database.

add item to cart and save it to HTTP session and update cart in DB.

If cart does not exist in DB, create new cart and and save in it HTTP Session.

Though I missed a lot of use cases here (do not want question length to increase a lot), but most of the flow will be same as I described in above steps.

My flow will start from the Controller and will go towards Service Layer and than ends up in the DAO layer.

Since there will be a lot of use cases where I need to check HTTP session and based on that need to call Service layer, I was planning to add a Facade layer which should be responsible to do this for me like checking Session and interacting with Service layer.

Please suggest if this is a valid approach or any other best approach can be implemented here?

One more point where I am confused is how to handle HTTP session in facade layer? do I need to pass HTTP session object each time I call my Facade or any other approach can be used here?

答:
I would always want to code my application in simplest of manner . I would have done all these in my Service layer itself. Below is my noob pseudo-code :

```<code> /**
 * @param request HttpServletRequest
 * @param item Item to be added
 */
private void addItemToCart(HttpServletRequest request, Item item) {

    UserVO user = null;
    ShoppingCart cart = null;
    ShoppingCartDAO cartDAO = new ShoppingCartDAO();
    HttpSession session = request.getSession(false);
    // check if its an existing session
    if(session!=null){
        // get the user ifo from the session
        user = (UserVO)session.getAttribute("userVO");
        // try to get the cart from the session
        cart = (ShoppingCart)session.getAttribute("cart");

        if(cart==null) {
            // try to get the cart from DB
            cart = cartDAO.getShoppingCart(user);
            if(cart==null){
                // create a new cart
                ShoppingCart cart = new ShoppingCart(user);
            }
        }
        // add item to the  cart
        cart.addItem(item);
        // set it in session
        session.setAttribute("cart",cart);
        // update the DB
        cartDAO.updateShoppingCart(user);
    }

}
</code>```

EDIT : I believe you can create an Interface to wrap the request object and pass the implementation of the Interface as argument to the Service method .

Title: Designing application flow

Body:
I would always want to code my application in simplest of manner . I would have done all these in my Service layer itself. Below is my noob pseudo-code :

 /**
 * @param request HttpServletRequest
 * @param item Item to be added
 */
private void addItemToCart(HttpServletRequest request, Item item) {

    UserVO user = null;
    ShoppingCart cart = null;
    ShoppingCartDAO cartDAO = new ShoppingCartDAO();
    HttpSession session = request.getSession(false);
    // check if its an existing session
    if(session!=null){
        // get the user ifo from the session
        user = (UserVO)session.getAttribute("userVO");
        // try to get the cart from the session
        cart = (ShoppingCart)session.getAttribute("cart");

        if(cart==null) {
            // try to get the cart from DB
            cart = cartDAO.getShoppingCart(user);
            if(cart==null){
                // create a new cart
                ShoppingCart cart = new ShoppingCart(user);
            }
        }
        // add item to the  cart
        cart.addItem(item);
        // set it in session
        session.setAttribute("cart",cart);
        // update the DB
        cartDAO.updateShoppingCart(user);
    }

}

EDIT : I believe you can create an Interface to wrap the request object and pass the implementation of the Interface as argument to the Service method .


问:
Are animations and other eye candies considered non-functional requirements?
I've seen many lists on the internet that includes many 'ities' (maintainability, scalability, portability, etc), but I'm not sure if animations, screen transitions, and similar features are functional or non functional requirements.</p>

答:
Your question is like "Is the color of a real world object a functional or non-functional property?" and the answer is - it depends.

The color of a car is mostly non-functional, since it has no direct influence in the main functions of a car (you can have cars any color you like, they all can go from A to B, transport passengers etc). In contrast, the color of a traffic-light is obviously pretty functional - a traffic light with blue, white and pink lamps would not be of much use for most of us.

And so is "animations" or "screen transitions" - for example, if you create some kind of movie maker software, presentation software, or some kind of special visualizing software, animations and "eye candy" may be pretty functional. For other kind of software those things may not improve the core functions of the program and thus, they are non-functional.


问:
How can I decouple configuration data from the program that uses it?
I am a beginning programmer who has written a spider application in PHP. Currently there are three parts:

```<code>1) The Spider (spider.php)
2) The Harvester (harvest.php)
3) The Configuration file (for example, craigslist_config.php)
</code>```

I use the spider to search the web for items I want to buy. An item can be found on any website, like ebay, craigslist, etc. The Harvester provides three functions to spider so it can act on the data it finds - get_title_from($markup), get_description_from($markup), and get_price_from($markup).

Each web site that I want to spider has, of course, different markup surrounding the data that I want to extract. My config file contains a configuration array that holds the regex patterns for each of the items I want to find. The structure of the file is always the same, the only thing that changes is the regex patterns. So, I would have craigslist_config.php, ebay_config.php, etc.

```<code>$conf = array(
    'title' => ' specific_site title pattern',
    'description => 'specific_site description pattern',
    'price' => 'specific_site price pattern'
);
</code>```

My problem is when I want to add a new website.  I have to edit the Spider.php file and add to an ever-growing "if, elseif" statement that detects what site is currently being read, and load the correct config file, which in turn feeds the correct REGEX data to the harvester functions.

How can I decouple my configuration from my Spider.php file? What I have designed does not feel like a flexible, scalable solution, and I don't want to have to mess with spider.php everytime I want to add or take away a new site.

Ultimately, what I am trying to achieve is the ability to simply drop in a new configuration file into my config directory and move the 'if, elseif' logic somewhere else so that the spider and harverster functions never have to worry about what files are or are not included in the config directory. It's the "somewhere else" I am having trouble figuring out. Actually, it would be even better if I could get rid of the 'if else' logic all together so that everything just 'works.'

My current design is not an OOP approach, however I am not opposed to one. I am currently reading, "PHP Objects, Patterns, and Practice" to get up to speed on OOP and related design patterns, so feel free to suggest in that direction should you feel it a solution.

EDIT: Based on Doc Brown's direction, I have come up with the following. I have individual configuration files with content like so:

```<code>$conf['specificwebsite1.com'] = array(
    'title' => 'title pattern',
    'price' => 'price pattern',

    etc...
);
</code>```

In my Harvester file I have a new function called load_config($url, $config). As suggested, I loops through all the configuration files and load them into one large $conf array. Then, the load_config function checks if the key is a sub string of the url I'm currently reading. If so, then it loads all the necessary values to continue parsing.  This is the function:

```<code>function load_config($url, $config){
  foreach($config as $key => $value){
    if(stristr($url, $key) !== FALSE){
      ## see if a key in our config file
      ## is a substring of our url.
      $conf = $config[$key];
      break;
    } else {

      $conf = FALSE;
    }
  }

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

This is working really well, so I'll accept that as the answer. But please feel free to make suggestions for improvements in the comments or as another answer.

答:
Ultimately, what I am trying to achieve is the ability to simply drop in a new configuration file into my config directory

First, add the related web address to your config file:

```<code>$conf = array(
    'url' => ' specific_site url or url pattern',
    'title' => ' specific_site title pattern',
    'description => 'specific_site description pattern',
    'price' => 'specific_site price pattern'
);
</code>```

Now, change the code in your Spider.php that it checks all .php files in that config directory,  loads them all dynamically one-by-one, and store the "$conf" content in a dictionary where the url is used as a key. Then it should be easy to replace your "if-else" list by a simple loop over that dictionary.

Body:
Ultimately, what I am trying to achieve is the ability to simply drop in a new configuration file into my config directory

First, add the related web address to your config file:

$conf = array(
    'url' => ' specific_site url or url pattern',
    'title' => ' specific_site title pattern',
    'description => 'specific_site description pattern',
    'price' => 'specific_site price pattern'
);

Now, change the code in your Spider.php that it checks all .php files in that config directory,  loads them all dynamically one-by-one, and store the "$conf" content in a dictionary where the url is used as a key. Then it should be easy to replace your "if-else" list by a simple loop over that dictionary.


问:
Why is String immutable in Java?
I couldn't understand the reason of it. I always use String class like other developers, but when I modify the value of it, new instance of String created.

What might be the reason of immutability for String class in Java?

I know there are some alternatives like StringBuffer or StringBuilder. It's just curiosity.

答:
The reason for string's immutability comes from consistency with other primitive types in the language. If you have an int containing the value 42, and you add the value 1 to it, you don't change the 42. You get a new value, 43, which is completely unrelated to the starting values. Mutating primitives other than string makes no conceptual sense; and as such programs that treat strings as immutable are often easier to reason about and understand.

Moreover, Java really provides both mutable and immutable strings, as you see with StringBuilder; really, only the default is the immutable string. If you want to pass references to StringBuilder around everywhere you are perfectly welcome to do so. Java uses separate types (String and StringBuilder) for these concepts because it doesn't have support for expressing mutability or lack thereof in its type system. In languages which have support for immutability in their type systems (e.g. C++'s const), there is often a single string type which serves both purposes.

Yes, having string be immutable allows one to implement some optimizations specific to immutable strings, such as interning, and allows passing string references around without synchronization across threads. However, this confuses the mechanism with the intended goal of a language with a simple and consistent type system. I liken this to how everyone thinks of garbage collection the wrong way; garbage collection is not "reclamation of unused memory"; it is "simulating a computer with unlimited memory". The discussed performance optimizations are things that are done to make the goal of immutable strings perform well on real machines; not the reason for such strings to be immutable in the first place.

Title: Why is String immutable in Java?

ContentLicense: CC BY-SA 4.0


问:
Does any well-known license require to make modifications available when only derived *output* is published?
Is there a way to make sure that modifications to free software are released even when no binaries of modified code are conveyed? Though it may sound odd and from what I understand, e.g., GPL requires to distribute source only if binary is conveyed to other parties thus making it possible for unlimited private use as it does not fall into propagation.

I would like to make sure that if someone publishes a final finding in a scientific literature derived with the aim of a modified code, then those modifications are made available.

Does any well-known license have these provisions? If not, why would it be bad? From what I understand if I add those provisions explicitly, it would limit the freedom, e.g., rendering result non-GPLish.

From one of the answers that disappeared, I guess I can add an Additional Term but would not it be a further restriction that can be simply ignored

If the Program as you received it, or any part of it, contains a
  notice stating that it is governed by this License along with a term
  that is a further restriction, you may remove that term.

From what I understand it looks like it depends on how I state it. If I require to make private modification used to derived a result that was published to make available, then it would be okay, otherwise it would be a further restriction if I prohibit from privately using and not making modified code available.

Example

A implements a super duper algorithm but they is bad with BLAS and it takes a long time for the simulation. B claims that they refined the code using CUDA or whatever and can easily get results within second. Based on their work they found this and that from simulations and got it published. And that is it. No GPL violation and no better code for community.

Another example

Imagine if some animation studio took Blender, implemented an improved version of Cycles that allows them to render things way faster and with less problems. So now they have an advantage as they can deliver animations to clients faster. No software conveyance, no GPL violation, no improvements sharing.

答:
The simplest way is to modify one of the major licenses (eg GPL) to do that. I took a quick glance at it and very little discusses conveyed works. The modifications would be few. I'd advise getting skilled help on the legal language though. Here's a few ideas for you:

Pay a lawyer to do it.

Pay less to a top notch college law student (who already knows contracts) to do it.

Pay less to someone who knows contracts in and out, often handling them professionally, but isn't an expensive lawyer.

For the non-lawyers, you might give them incentive of buying them a really good lunch and/or mentioning it's an unusual thing they might like. Modifying the number one FOSS license is probably more interesting than the average homework assignment for many law students. ;)


问:
I have made extensive use of Apache Commons libraries. I am now to release my (non-profit) application. Is there anything I should worry about?
I have developed a Java program for academic purposes (non-profit, non-open-source).
In the process, I made extensive use of many apache common libraries.
The program is now to be set free on the net, so I was wondering what my precautions should be. My program will (preferably) consist of a single .jar file, having all the apache commons .jar files living inside it (I didn't modify them in any way).

Is the described approach just enough? Or do I have to take any more steps such that I don't commit an infraction?

Thanks

答:
Here's a nice lay summary of Apache license.

http://www.apache.org/foundation/license-faq.html#WhatDoesItMEAN

...here's what the Apache license says in layman's terms:

It allows you to:

freely download and use Apache software, in whole or in part, for personal, company internal, or commercial purposes;

use Apache software in packages or distributions that you create.

It forbids you to:

redistribute any piece of Apache-originated software without proper attribution;

use any marks owned by The Apache Software Foundation in any way that might state or imply that the Foundation endorses your distribution;

use any marks owned by The Apache Software Foundation in any way that might state or imply that you created the Apache software in question.

It requires you to:

include a copy of the license in any redistribution you may make that includes Apache software;

provide clear attribution to The Apache Software Foundation for any distributions that include Apache software.

It does not require you to:

include the source of the Apache software itself, or of any modifications you may have made to it, in any redistribution you may assemble that includes it;

submit changes that you make to the software back to the Apache Software Foundation (though such feedback is encouraged).

Judging from that, I'd say you need to include the license, do proper attribution, and leave the "prominent notices" if you change any Apache Commons libraries internals specifying what you changed. The license also has certain patent advantages as patent owners submitting contributions give you an implied patent license over them.


问:
Which approach to follow for developing applications targeting multiple platforms?
For applications targeting multiple platforms, I see mainly two development approaches-

Go for some JAVA-like development platform. Have one code solution and let the intermediate runtime handle different platforms. If something goes wrong in any platform, tweak the code a bit. But keep it same for all.

Make modular code separating core logic and UI. Develop separate UIs for respective platforms which will be calling same core libraries. Build the application separately for each of the target platforms.

So, which one to follow? I know, the answer will start with "It depends". But I want to hear your opinions on these approaches and the factors to be considered to choose any of them.

答:
As a real-time developer, I've successfully used an option similar to 2 - separate platform-specific modules with a common API used by the core logic.  However, nothing I've done has had a UI  - networking, audio, data streaming - in this case it is the low-level hardware interface that is platform specific.

I've done it this way for several reasons:

1) To get the optimum performance on each platform

2) To take advantage of features only offered on 1 platform

3) (J)VMs did not exist for some of the platforms (embedded systems, game consoles...)

ContentLicense: CC BY-SA 2.5


问:
How to optimise an sentiment analysis algorithm for larger data sets?
I am a noob to sentiment analysis and found a good resource for Bayesian Opinion Mining and a way to make it self improving. I was wondering though, if the optimum analysis is dependent upon the supplied data set and since self improvement would mean adding known patterns to the data set (my understanding), wouldn't the application become too overloaded with huge data set over due course of time with more and more patterns getting added to the data set every day ? What should be the proper approach to make the application scalable (if I am using the right term at the right place) ?</p>

答:
For other tasks such as part of speech tagging, condensation after self-training made the model smaller and better! If you identify a scalability issue, look this way first before trying to optimize your code.

The idea is that after self-training, you iteratively create a model which is initially empty. You then add data points to the new model only if they were not classified correctly. This avoids overfitting and keeps your model as small as it can be.


问:
boolean or Boolean?
I was doing edit reviews on Stack Overflow and found out that people keeps correcting the word "boolean", replacing it with "Boolean" when it appears in text (but not in source code, of course). And then I found out that the spelling checker in my browser agrees with them.

Is there any rationale behind this? It seems grammatically incorrect.

I think the word boolean is used in two different ways, either among programmers as a simple noun, "return a boolean", or as part of a mathematical term, "boolean logic", "boolean algebra". None of these cases seem to motivate a capital letter. You don't write "return a Float", "Fuzzy logic" or "Linear algebra" (unless of course it is at the beginning of a sentence).

I suppose that the capital letter in "Boolean" somehow refers to the name George Boole. But then that doesn't make sense either. I found this link which gives some excellent examples of similar cases, where some term is named after a person, but no capital letter is used.

Any consensus? What is correct? What should we use?

答:
I am not aware of any reference that advice boolean. You are correct, that it historically dates back to George Boole, but as far as I can tell, all spelling references agree on the adjective as well as the noun being written as Boolean.

Here are a few references:

dictionary.reference.com

Merriam Webster

Oxford Dictionaries

The Free Dictionary

Wictionary

Feel free to ask on english.stackexchange.com about the linguistic reasoning behind this, but there seems to be no official authority that accepts the lower-case version. They are all consent on the upper-case variant, so there is currently no reason for me to doubt the correctness of Boolean.

Title: boolean or Boolean?


问:
Is micro-optimisation important when coding?
I recently asked a question on Stack Overflow to find out why isset() was faster than strlen() in PHP. This raised questions around the importance of readable code and whether performance improvements of micro-seconds in code were worth even considering.

My father is a retired programmer, and I showed him the responses. He was absolutely certain that if a coder does not consider performance in their code even at the micro level, they are not good programmers.

I'm not so sure - perhaps the increase in computing power means we no longer have to consider these kind of micro-performance improvements? Perhaps this kind of considering is up to the people who write the actual language code? (of PHP in the above case).

The environmental factors could be important - the Internet consumes 10% of the world's energy. I wonder how wasteful a few micro-seconds of code is when replicated trillions of times on millions of websites?

I'd like to know answers preferably based on facts about programming.

Is micro-optimisation important when coding?

My personal summary of 25 answers, thanks to all.

Sometimes we need to really worry about micro-optimisations, but only in very rare circumstances. Reliability and readability are far more important in the majority of cases. However, considering micro-optimisation from time to time doesn't hurt. A basic understanding can help us not to make obvious bad choices when coding such as

```<code>if (expensiveFunction() || counter < X)
</code>```

Should be

```<code>if (counter < X || expensiveFunction())
</code>```

(Example from @zidarsk8) This could be an inexpensive function and therefore changing the code would be micro-optimisation. But, with a basic understanding, you would not have to, because you would write it correctly in the first place.

答:
Other answers are right on the money. But I'll add another point where one have to differentiate what's premature optimization/micro-optimization and writing performant code that reflects an understanding of the behaviour of the language/framework constructs (sorry, couldn't find a one word for the last). The last one is a good coding practice and one should generally do!

I will explain. Bad optimization (read premature/micro optimization) is not when you optimize sections of code without profiling to know if they are really the bottlenecks. It's when you optimize based on your assumptions, hearsays and undocumented behaviours. If it's documented and does something in a more efficient/logical manner however small it is, I call it good optimization. As others have stated, both of these have cons and almost no pros at all as far as earning you good business is concerned, but still I do the latter, not the former, if it doesn't totally defeat readability. Yes readability/maintainability is of utmost importance and it's about where you draw the line.

I'll reiterate the points made by others here as the futility of both good and bad optimizations:

Your dependencies to a specific problem can change and any time spent on optimizing before completing the logical section of your application is a waste of time. I mean optimizing at a relatively early stage. Today you have an List<T> and by the time your app ships you had to change it to LinkedList<T> and now all the benchmarking was a waste of time and effort.

Mostly the real bottleneck of your application (read as measurable difference) could be 5% of your code (mostly the sql ones) and optimizing the other 95% doesn't give your customers any benefit.

Usually "technically" better performing code means more verbosity which in turn means more error prone code which in turn means harder maintainability and more time spent which in turn means you earn less money.

The carbon footprint you save for the whole world thru 1% performance gain is easily dwarfed by greenhouse gases your team will have to emit on debugging and maintaining that code.

The negatives of bad optimization in particular are:

It doesn't often given you the performance you expect. See this question on SO, where optimizations have gone wrong. In fact it can have adverse effects. That's the problem with undocumented behaviour.

Mostly modern compilers will do it for you anyway.

I'll give some examples of bad and good optimization:

The bad -

use of smaller integer types instead of Int32.

++i used instead of i++

for instead of foreach (the worst I have seen, totally defeats logic)

avoiding closed out variables

```<code>string p;
foreach (var item in collection)
    p = ...;
</code>```

using char instead of string during string concatenation, like:

```<code>string me = 'i' + "me myself"; // something along that line - causes boxing
</code>```

The good (from .NET world. Should be self-explanatory) -

Double lookup

```<code>if (Dictionary<K, V>.TryGetValue(K, out V))
    do something with V
</code>```

instead of

```<code>if (Dictionary<K, V>.ContainsKey(K))
    do something with Dictionary<K, V>[K]
</code>```

Load 'em all

```<code>DirectoryInfo.EnumerateFiles();
</code>```

instead of

```<code>DirectoryInfo.GetFiles();
</code>```

Two stage casting:

```<code>s = o as string;
if (s != null)
    proceed
</code>```

instead of

```<code>if (o is string)
    s = (string)o;
</code>```

If the order doesn't matter

```<code>if (counter < X || expensiveFunction())
</code>```

instead of

```<code>if (expensiveFunction() || counter < X)
</code>```

Boxing

```<code>string me = 'i' + "me myself"; // something along that line - causes boxing
</code>```0

instead of

```<code>string me = 'i' + "me myself"; // something along that line - causes boxing
</code>```1

If you ask me if these give a noticeable performance benefits, I would say no. But I would suggest one should use them because it stems from an understanding of the behaviour of these constructs. Why make two calls when you can do just 1? From a philosophical standpoint its good coding practice. And 1 & 3 are slightly less readable too in strict terms, but do they trump readability? No, not much, so I use. Now that's the key - maintaining a decent performance to readability ratio. And when it's that, its about where you draw the line.

Body:
Other answers are right on the money. But I'll add another point where one have to differentiate what's premature optimization/micro-optimization and writing performant code that reflects an understanding of the behaviour of the language/framework constructs (sorry, couldn't find a one word for the last). The last one is a good coding practice and one should generally do!

I will explain. Bad optimization (read premature/micro optimization) is not when you optimize sections of code without profiling to know if they are really the bottlenecks. It's when you optimize based on your assumptions, hearsays and undocumented behaviours. If it's documented and does something in a more efficient/logical manner however small it is, I call it good optimization. As others have stated, both of these have cons and almost no pros at all as far as earning you good business is concerned, but still I do the latter, not the former, if it doesn't totally defeat readability. Yes readability/maintainability is of utmost importance and it's about where you draw the line.

I'll reiterate the points made by others here as the futility of both good and bad optimizations:

Your dependencies to a specific problem can change and any time spent on optimizing before completing the logical section of your application is a waste of time. I mean optimizing at a relatively early stage. Today you have an List<T> and by the time your app ships you had to change it to LinkedList<T> and now all the benchmarking was a waste of time and effort.

Mostly the real bottleneck of your application (read as measurable difference) could be 5% of your code (mostly the sql ones) and optimizing the other 95% doesn't give your customers any benefit.

Usually "technically" better performing code means more verbosity which in turn means more error prone code which in turn means harder maintainability and more time spent which in turn means you earn less money.

The carbon footprint you save for the whole world thru 1% performance gain is easily dwarfed by greenhouse gases your team will have to emit on debugging and maintaining that code.

The negatives of bad optimization in particular are:

It doesn't often given you the performance you expect. See this question on SO, where optimizations have gone wrong. In fact it can have adverse effects. That's the problem with undocumented behaviour.

Mostly modern compilers will do it for you anyway.

I'll give some examples of bad and good optimization:

The bad -

use of smaller integer types instead of Int32.

++i used instead of i++

for instead of foreach (the worst I have seen, totally defeats logic)

avoiding closed out variables

string p;
foreach (var item in collection)
    p = ...;

using char instead of string during string concatenation, like:

string me = 'i' + "me myself"; // something along that line - causes boxing

The good (from .NET world. Should be self-explanatory) -

Double lookup

if (Dictionary<K, V>.TryGetValue(K, out V))
    do something with V

instead of

if (Dictionary<K, V>.ContainsKey(K))
    do something with Dictionary<K, V>[K]

Load 'em all

DirectoryInfo.EnumerateFiles();

instead of

DirectoryInfo.GetFiles();

Two stage casting:

s = o as string;
if (s != null)
    proceed

instead of

if (o is string)
    s = (string)o;

If the order doesn't matter

if (counter < X || expensiveFunction())

instead of

if (expensiveFunction() || counter < X)

Boxing

void M<T>(T o) //avoids boxing
{

}

instead of

void M(object o)
{

}

If you ask me if these give a noticeable performance benefits, I would say no. But I would suggest one should use them because it stems from an understanding of the behaviour of these constructs. Why make two calls when you can do just 1? From a philosophical standpoint its good coding practice. And 1 & 3 are slightly less readable too in strict terms, but do they trump readability? No, not much, so I use. Now that's the key - maintaining a decent performance to readability ratio. And when it's that, its about where you draw the line.


问:
We have a large Ruby on Rails application (25 million monthly users), our management decided to rewrite in Node.js, am I crazy?
Please tell me if:

Node.js will make our site faster!

Node.js will consume fewer server resources, we can save money!

Node.js will make us more productive!

Node.js means we can share client and server side JavaScript code.

To clarify, we're rewriting a frontend server, which will talk to our existing Ruby on Rails application as an API. Meanwhile, we'll refactor our Ruby on Rails application into services.

More details on the existing architecture:

Memcached for HTML partials caching

Redis for session, and some structured data caching

MySQL single master, multiple slaves

There's one large table that accepts a lot of writes (imagine a poll)

Otherwise mostly reads.

MongoDB for some metadata

Ruby on Rails 3.0

nginx and Unicorn

答:
Well, I don't think that rewriting the application was a good idea unless it was performing poorly. To answer your questions:

Node.js is not magic. Your application has huge amount of users, so there is no way to be certain that it will make it faster.

Well, yes, Node.js does in fact consume fewer server resources. So not only can you save money on resources, but you can also do more with the existing ones. This is mostly because of single-threaded nature of Node.js. There is no overhead of additional threads.

Again Node.js is not magic. Having said that, there might be some truth in that. Node.js has a very active community that has created hundreds of modules for every possible task. So it is quite probable that most of the work has been done for you. You just need to fit the pieces together.

Theoretically, yes. Since Node.js is JavaScript, you can share code between the client and server. But I don't know exactly what it is that will be shared. I have not written any piece of code that was reusable on a client. Things that we do on the server usually has nothing to do with the client. More important to me is the lack of context switch. I find it easier to code on both client and server in a single language.

Since Node.js is single-threaded, unless explicitly configured to do so, it can't take advantage of multiple CPUs.

Take a look at the comments too. They provide some good insight into the workings of Node.js.


问:
What does it mean to perform an operation "In Place" for Interpreted Languages?
Programming question:

Reverse words in a string (words are separated by one or more spaces).
  Now do it in-place.

What does "in-place" mean in the above context for an interpreted language like PHP or JavaScript?

答:
In place will be something of this sort :

```<code>void reverse(char *str)
{
   if(!str)return;
   reverse(strtok(NULL," "));
   printf("%s ",revStr);
}
</code>```

"An in-place algorithm (or in Latin in situ) is an algorithm which transforms input using a data structure with a small, constant amount of extra storage space. The input is usually overwritten by the output as the algorithm executes."

Source:
In-place algorithm

Body:
In place will be something of this sort :

void reverse(char *str)
{
   if(!str)return;
   reverse(strtok(NULL," "));
   printf("%s ",revStr);
}

"An in-place algorithm (or in Latin in situ) is an algorithm which transforms input using a data structure with a small, constant amount of extra storage space. The input is usually overwritten by the output as the algorithm executes."

Source:
In-place algorithm


问:
Where can I get free public raw data?
In web development, a mashup is a web page or application that uses and combines data, presentation or functionality from two or more sources to create new services.

There are many sources for getting data (raw information). Governments are good examples.

However many of those cost and have complicated licensing.
For instance SMHI in Sweden sells temperature data meanwhile our neighbor country Norway gives it away for free.

One would say what kind of application are you building and what data do you need?  I would say give me some interesting data and I will do a good mashup of it. I could just plot it on Google maps for instance.

So the question is: Where can I get free data?

Note: I'm not interested in data mining using a webcrawler!

Please share your sources with me.

答:
Water Data

You can use NOAA to retrieve massive amounts of data to play with. I've used it several times and the API isn't half bad. If you don't care about what and simply how much that's where I would go.

Title: Where can I get free public raw data?


答:
Most of the questions you ask are not answerable without context, and are more or less moot given management has already made the choice for you... unless you are asking 'should I quit and find a new job in the face of all this change?'

If your going to tough it out I recommend you read this this post on the topic: How To Survive a Ground-Up Rewrite Without Losing Your Sanity.

I recently started down the path of rewriting a bit of server logic in node.js. The main reason was it is currently written in .NET and we are wanting to migrate away from MS environments down the track.

My experiences so far have been positive, you will have an initial learning curve with all the non-blocking-ness of it but once you get past that it's actually quite fun to code in.. I know, FUN!

It does have a dark side though, every man and his dog who has done some front end development with JavaScript - and that would be every front end developer I hope - gets a little excited when you mention that node.js is 'server side javascript' however that does not mean front end developers will have the experience required to write good server side apps.

For one thing you have consider is that a fatal error will bring down the whole app due to it's non threaded nature, so the stakes are a little higher and you have to explicitly check and catch everything.

For those who have done both front and back - and enjoy both - not having to switch mental contexts from front end to back end languages is a real bonus that I think will ultimately boost productivity in our team down the track.

Title: We have a large Ruby on Rails application (25 million monthly users), our management decided to rewrite in Node.js, am I crazy?

Body:
Please tell me if:

Node.js will make our site faster!

Node.js will consume fewer server resources, we can save money!

Node.js will make us more productive!

Node.js means we can share client and server side JavaScript code.

To clarify, we're rewriting a frontend server, which will talk to our existing Ruby on Rails application as an API. Meanwhile, we'll refactor our Ruby on Rails application into services.

More details on the existing architecture:

Memcached for HTML partials caching

Redis for session, and some structured data caching

MySQL single master, multiple slaves

There's one large table that accepts a lot of writes (imagine a poll)

Otherwise mostly reads.

MongoDB for some metadata

Ruby on Rails 3.0

nginx and Unicorn


问:
Is MVC ever considered/applied system-wide?
I was having a discussion with a colleague and he bought up the topic of MVC and the use of ViewModels in ASP.NET MVC.

The discussion was that, in an n-layer architecture, the UI, Business Layer and Data Access Layer are individual assemblies/projects and ViewModels are the UI's representation of a database/business object. The discussion on whether ViewModel's are useful moved into a discussion about how MVC should be applied.

In my version (which is what I am used to/have seen elsewhere) takes into account separation of concerns and waterfall dependencies, where the UI project references the BLL, the BLL references the DAL and possibly all of them reference an "Entities" project. "MVC" is restricted to the UI. That is, the notion of "Models" (in my version, they are technically just ViewModels), "Views" and "Controllers" are individual files/objects in the UI layer. The BLL talks in terms of database entities and the UI will generally contain helpers that map a ViewModel to an entity.

My colleague's version took this to the system/layer level, where the "Model" is the DAL, the "View" is the entire UI and the "Controllers" are the BLL as a whole.

Given that in an MVC triad, the Model is unaware of Views or Controllers, the Views know about Models and the Controllers know about both.. does the system-level application of MVC not violate modular programming concepts/code reusability (disregarding the possible circular dependency issues)?

Are patterns such as MVC applied at a system level often? My understanding was that MVC and related patterns were more often applied purely to the UI and not spread out across an entire n-layer system.

By the way, we had a look at this question: What is MVC, really?

..but we also seem to disagree on whose argument the answers to this question supports! :)

I could be just stuck in my little tiny .NET-oriented world and cannot see the bigger picture, so I would like to hear some thoughts on this.

答:
From my experience, Controllers hide all the business logic behind them.

Basically, when a page is requested, the Controller will perform all the necessary business logic operations, create the models and pass them to the views.

BLL (and DAL behind it) is usually encapsulated in a separate project/module, and the Controller will work with it via an API.

The models, frankly saying, have nothing to do with the DAL or BLL. They needn't be the objects used for working with the database, and they needn't be a "copy" of the table schemes either. In fact, such approach has many drawbacks, because models are designated to be used by specific views, and the data needed for the views mostly isn't simply a set of rows from tables. Models can be placed in the same project with the Controllers.

This approach helps to view the whole project as having the MVC pattern. Along with that, it helps creating all the familiar layers (DAL, BLL) without breaking the pattern.


答:
you're right - MVC is a pattern that can be conceptually applied as a kind of n-tier architecture. M = data tier, V = presentation tier, C = application tier. No problem there. The data tier doesn't know about who's going to consume the data that's exposed via a stored procedure "API", nor does the app tier know who's going to consume the webservices API that it exposes (for example).

Where I think you're getting confused is because you're thinking in "framework" mode, where you have a framework that is organised in a MVC way, and now everything looks like this in the frameworks realm. ASP.NET MVC is a framework that gives you a way of writing web apps in a particular fashion, however, it sticks to doing things in its own way and its not a full-on system architecture - WCF web services are not part of its remit, so it doesn't do them. You could think of it as a very simple system architecture, one for beginners before they architect larger systems that are more decoupled into the tiers. This is perhaps why you think of it (and therefore MVC as a whole) as something that effectively only applies to the presentation tier.

What this means in practice is... nothing :) You should continue the discussion down the pub for best results.

Title: Is MVC ever considered/applied system-wide?

Body:
I was having a discussion with a colleague and he bought up the topic of MVC and the use of ViewModels in ASP.NET MVC.

The discussion was that, in an n-layer architecture, the UI, Business Layer and Data Access Layer are individual assemblies/projects and ViewModels are the UI's representation of a database/business object. The discussion on whether ViewModel's are useful moved into a discussion about how MVC should be applied.

In my version (which is what I am used to/have seen elsewhere) takes into account separation of concerns and waterfall dependencies, where the UI project references the BLL, the BLL references the DAL and possibly all of them reference an "Entities" project. "MVC" is restricted to the UI. That is, the notion of "Models" (in my version, they are technically just ViewModels), "Views" and "Controllers" are individual files/objects in the UI layer. The BLL talks in terms of database entities and the UI will generally contain helpers that map a ViewModel to an entity.

My colleague's version took this to the system/layer level, where the "Model" is the DAL, the "View" is the entire UI and the "Controllers" are the BLL as a whole.

Given that in an MVC triad, the Model is unaware of Views or Controllers, the Views know about Models and the Controllers know about both.. does the system-level application of MVC not violate modular programming concepts/code reusability (disregarding the possible circular dependency issues)?

Are patterns such as MVC applied at a system level often? My understanding was that MVC and related patterns were more often applied purely to the UI and not spread out across an entire n-layer system.

By the way, we had a look at this question: What is MVC, really?

..but we also seem to disagree on whose argument the answers to this question supports! :)

I could be just stuck in my little tiny .NET-oriented world and cannot see the bigger picture, so I would like to hear some thoughts on this.


答:
MVC has become such a buzz-word that it gets applied to everything and the kitchen sink. If you can distinguish a triad with different responsibilities and their communication lines are not all over the map, it will get labelled with the term MVC.

Originally, MVC was a pattern for user interfaces, where the View was responsible for updating (portions of) the screen, the Controller responsible for interpreting the input events (originally key presses and mouse movement/clicks) and the Model contained all the rest.

If you apply this model of MVC to the n-tier system you describe in the question, the UI layer would contain the View and Controller and the BLL and DAL layers together would be the Model.

But, as I said, the term MVC has been eroded in the past few years due to its popularity and other uses have sprung up.


问:
Extreme Programming practices make an application more error prone?
I am conducting academic research on the topic of Extreme Programming and whether its practices lead to creating space for more errors and bugs in applications.

From the experiences I gathered from many, I have comments that fall on both sides.  Many support it and consider it a daily necessity, with dynamics that can facilitate changing project requirements.  Many others argue that it leads to many problems, such as:

Over-involvement with the customer in the process leads to the expression of customer wishes rather than needs

Many products have multiple customers which lead to conflicting needs
and opinions, creating unnecessary blockades

Many products don’t have any external customers, where the product
is made for internal use or to be sold in the future. In these cases,
the team itself is playing the user as well as the developer, hence
killing the effectiveness of the process.

Not many things exist in formal documentation, this informality
leads to vague vision and can create problems where the customer
might say that this is not what we asked etc. etc.

The questions is why such conflicting opinions exist regarding XP.  Is it a matter of different scenarios? Is there something else? To what extent is the claim (as written in the title) true?

I would like people who are working or have worked with XP here to contribute their learning and real world experiences.  It would be ideal if you have any facts or references to support you answer.

答:
A few thoughts:

Too much involvements of the customer in the process make him start
  expressing his wishes rather than his needs to the software

There is always a balance between having a detailed, stable specification and being responsive to the customer.  Extreme is meant to increase responsiveness to the customer, and of course it is possible to go too far in that direction.  So this is a legitimate concern (especially depending on how the project is billed: if it is a fixed-price contract, you obviously have to have it well-specified).

In my experience, however, no matter how good your specification is, you often have to change it to do "what the customer wishes" anyway.  Extreme helps you make those changes as soon as possible, rather than after you have built a huge, complicated program to specification.

Many products have multiple customers which lead to conflicting needs
  and opinions leading unnecessary blockades

Of course, resolving conflicting needs in such a situation will always be a problem that you need a good process to deal with.  If the process of getting customer feedback is time-consuming and complex, then it would make Extreme Programming less effective, so I think this is a legitimate concern.

Many products don’t have any external customers (products organization
  made for them or to be selling in future). In this case the team
  itself is playing the user as well as the developer hence killing the
  effectiveness of the process.

I don't think this is legitimate at all.  The idea behind Extreme it that customers don't realize what they want until you actually start making it.  This is just as true for internal "customers" as external ones.

And if you are developing something that has no customers yet (like a product not yet released) you must have someone (or some group) who is acting as the hypothetical customer and deciding what people will want.  Extreme works just as well with them acting as the customer.

I have worked on a product like this, which was intended for external customers but not yet released.  While we didn't label it "Extreme Programming", we used a similarly iterative development process without an extensive formal specification and with frequent builds.  I found it quite effective.

Not many things exists in formal documentation, this informality leads
  to vague vision and can create problems where the customer might say
  that this is not what we asked etc. etc.

Yes, anything that is not documented is a problem.  Extreme, since it is not driven by a formal specification, might make it easier to not document things.  But Extreme doesn't automatically mean "things are not documented".  You should still make documentation, but it is created alongside the program rather than beforehand.  And in some cases it will mean documenting the behavior after you implement it.  This is not a problem in and of itself.

When it comes to billing, you often need written documentation of exactly what will be delivered before you start the work.  This can be more difficult with Extreme Programming.

Conclusion: Extreme is a methodology that, like anything, has advantages and drawbacks.  You need to keep both in mind when using it (or teaching it).


问:
A better way of doing Regex?
I really dislike regular expressions, each time I come back to it I seem to have to relearn it.  It's also incredibly hard to maintain, modify and at a glance understand what it is doing.

Has anyone ever tried writing another layer on top of it that turns more semantic 'sql like' statements into regex?  I imagine it working along the lines of:

```<code>AnotherString = "coffee hello beep 15"

FindString.StartsWith string longer than 5
FindString.Contains "beep" after "hello"
FindString.EndsWidth int < 20
FindString.DoesntContain "no!!" and DoesntContain "what!"
Foreach FindString match in AnotherString
    ...
Next
</code>```

This is probably not the greatest example ever, but the idea is that the pattern is built with some sort of semantic meaningful language that can break down into traditional regular expressions.  The above would be a lot easier for a developer to modify.  I sort of invision it being like SQL/Linq to some degree.

It would make regex a lot more semantic and maintainable.  Has this been tried before, and is it a bad/good idea to try this?  Could it work?

Edit

Perhaps this is a better example (I know URL's are notoriously difficult to parse and this is over simplified):

```<code>string UserInputtedURL = "http://www.google.com/page.html?ID=5"

Protocols = {"http", "https"};
Domains = {"com", "net", "org"}
Rule.CaseSensitive = false;
Rule starts with Protocols OR starts with "www";
Rule followedby string endson "."
Rule followedby Domains
Rule if stringend or endswidth " " end else continuewith Ruleset2

RuleSet2.startswith "/"
etc...

if(UserInputtedURL.Matches(Rule)){
    // URL is valid!
}
</code>```

答:
The best way to do regular expressions is to learn and understand them, or don't use them at all. Using some other tool as an excuse to not learn regular expressions means you have to re-"learn" them every time you encounter them.

Spend a day, just one (full, undistracted) day to deeply study regular expressions and you'll be rewarded with a new tool you can use your whole career. You'll also have a much greater understanding of when they are appropriate and -- more importantly -- when they are not.

Title: A better way of doing Regex?


答:
MVC is a great way of implementing a web interface because it makes things clean and maintainable and offers a useful separation of concerns. But it doesn't really describe an entire system any more than a windows interface system like WPF or Winforms describes the entire application that it provides the front end for.

However, like any design pattern it is also a paradigmatic way of looking at the world, so if you wanted to see things through the filter of MVC you might say that on a larger application the View is your MVC framework application, the Controller is your business process layer and the Model is your ORM layer or whatever other data source you use. Doubtless you could see MVC within MVC within MVC to a fractal degree if that was the way you chose to look at an application.

It is not the only way of looking at the world, and not necessarily a useful one but the more different ways you can see the same system the more rounded your understanding becomes, so it is by no means a bad idea to see how you can interpret a system through different paradigms, especially during the early design stages.


答:
Keep the configuration files as simple as possible. That means: avoid PHP code and redundancy (like XML). A suitable format is the INI format, which can be read by PHP into an array.

```<code>[specificwebsite1.com]
title="title pattern"
price="price pattern"

[anotherwebsite.com]
title="title pattern"
price="price pattern"
</code>```

With that, your configuration files are human readable and easy to maintain, and you can put several configurations into one file.

The next step is to create a directory for these files, lets call it config. Store the configuration files in that directory, naming them in a way that makes it easy for you to find a specific one.

Now, you can read the configurations from PHP:

```<code>$configs = array();
foreach (glob('config/*.ini') as $filename) {
    $configs = array_merge($configs, parse_ini_file($filename, true));
}
</code>```

$configs now contains all your configurations, indexed by site name.

Title: How can I decouple configuration data from the program that uses it?

Body:
I am a beginning programmer who has written a spider application in PHP. Currently there are three parts:

```<code>1) The Spider (spider.php)
2) The Harvester (harvest.php)
3) The Configuration file (for example, craigslist_config.php)
</code>```

I use the spider to search the web for items I want to buy. An item can be found on any website, like ebay, craigslist, etc. The Harvester provides three functions to spider so it can act on the data it finds - get_title_from($markup), get_description_from($markup), and get_price_from($markup).

Each web site that I want to spider has, of course, different markup surrounding the data that I want to extract. My config file contains a configuration array that holds the regex patterns for each of the items I want to find. The structure of the file is always the same, the only thing that changes is the regex patterns. So, I would have craigslist_config.php, ebay_config.php, etc.

```<code>$conf = array(
    'title' => ' specific_site title pattern',
    'description => 'specific_site description pattern',
    'price' => 'specific_site price pattern'
);
</code>```

My problem is when I want to add a new website.  I have to edit the Spider.php file and add to an ever-growing "if, elseif" statement that detects what site is currently being read, and load the correct config file, which in turn feeds the correct REGEX data to the harvester functions.

How can I decouple my configuration from my Spider.php file? What I have designed does not feel like a flexible, scalable solution, and I don't want to have to mess with spider.php everytime I want to add or take away a new site.

Ultimately, what I am trying to achieve is the ability to simply drop in a new configuration file into my config directory and move the 'if, elseif' logic somewhere else so that the spider and harverster functions never have to worry about what files are or are not included in the config directory. It's the "somewhere else" I am having trouble figuring out. Actually, it would be even better if I could get rid of the 'if else' logic all together so that everything just 'works.'

My current design is not an OOP approach, however I am not opposed to one. I am currently reading, "PHP Objects, Patterns, and Practice" to get up to speed on OOP and related design patterns, so feel free to suggest in that direction should you feel it a solution.

EDIT: Based on Doc Brown's direction, I have come up with the following. I have individual configuration files with content like so:

```<code>$conf['specificwebsite1.com'] = array(
    'title' => 'title pattern',
    'price' => 'price pattern',

    etc...
);
</code>```

In my Harvester file I have a new function called load_config($url, $config). As suggested, I loops through all the configuration files and load them into one large $conf array. Then, the load_config function checks if the key is a sub string of the url I'm currently reading. If so, then it loads all the necessary values to continue parsing.  This is the function:

```<code>function load_config($url, $config){
  foreach($config as $key => $value){
    if(stristr($url, $key) !== FALSE){
      ## see if a key in our config file
      ## is a substring of our url.
      $conf = $config[$key];
      break;
    } else {

      $conf = FALSE;
    }
  }

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

This is working really well, so I'll accept that as the answer. But please feel free to make suggestions for improvements in the comments or as another answer.

Body:
Keep the configuration files as simple as possible. That means: avoid PHP code and redundancy (like XML). A suitable format is the INI format, which can be read by PHP into an array.

[specificwebsite1.com]
title="title pattern"
price="price pattern"

[anotherwebsite.com]
title="title pattern"
price="price pattern"

With that, your configuration files are human readable and easy to maintain, and you can put several configurations into one file.

The next step is to create a directory for these files, lets call it config. Store the configuration files in that directory, naming them in a way that makes it easy for you to find a specific one.

Now, you can read the configurations from PHP:

$configs = array();
foreach (glob('config/*.ini') as $filename) {
    $configs = array_merge($configs, parse_ini_file($filename, true));
}

$configs now contains all your configurations, indexed by site name.


问:
Exception handling class: static or object-oriented?
I am working in a windows service (using VB.Net) for internal use of my department. When ever a certain type of exception (FooException for now on) is captured, I follow the same logic:

Log the exception

Send an email to someone.

In order to reuse code and not to copy-paste the same lines in every catch FooException block I have created a FooExceptionHandler method inside my ExceptionHandling class. So I ended up with a code like this:

```<code>FooExceptionHandler(fooException)
{
   //log the exception
   try
   {
      //send email
   }
   catch SmtpException
   {
      //log  the smtp exception
   }
}
</code>```

My question is about how to organize the code. Should this be a static method? Should all the exception handling class be static? Should it be a singleton? Should it be object-oriented?

If the object-oriented case is the best way, should I create a new object in every catch block or is it better to have a class variable?

答:
The right answer depends on how big your application is and how you test it. If your application is big, modular enterprise application and you use unit-tests, then the only solution you really have is to go object-oriented way.

In that case you create an interface IExceptionHandler, and all client classes consume that interface. You may then use some object-creation pattern like a factory method to create your exception handling objects that implement that interface, or handle it with Dependency injection.

That way you make your production code decoupled from exception-handling mechanism and easily adjust or change exception-handling behaviour in future without touching the code in other classes.

If your application is small and mostly for personal use, then just implement the exception-handling logic the way you feel is most convenient to you.

OwnerDisplayName: garnav


答:
Your questions are dealing only with two main XP topics: "direct customer communication" and "not too much formal documentation". So in my point of view, this is not really an "XP" question, those are topics which are part of any other agile development process I know of.

Here are my thoughts:

Too much involvements of the customer in the process make him start expressing his wishes rather than his needs to the software.

Well, if you have a waterfall-like process, with a fully detailed specification beforehand, with lots of requirements, you may get in trouble either which of those requirements are just wishes and which are real needs. The easiest way of clarifying this is IMHO talking to the customer and showing him different alternatives - whenever you come to a point where clarification is needed. So quite the opposite is true - "agile development" will help you to deal with "needs vs wishes" better.

Many products have multiple customers which lead to conflicting needs
  and opinions leading unnecessary blockades

Yes, with a fully detailed specification beforehand those conflicts may have been resolved before the development starts (if you are lucky). The solution to this problem in an agile process is to have only few people on the customer side talking directly with the devs, and one responsible representative for the customers who can make final decisions in case of conflicts.

Many products don’t have any external customers (products organization
  made for them or to be selling in future). In this case the team
  itself is playing the user as well as the developer hence killing the
  effectiveness of the process.

No, that's not correct, if you have only internal users belonging to the same company as the devs, "customer on site" is much easier to be installed than when you have only external customers. If you have no direct users at hand, that may be a problem, but that is not an agile-specific problem - you will then have to find someone who takes the role of a potential user instead (and this person is typically not from the developers team).

Not many things exists in formal documentation, this informality leads to vague vision
  and can create problems where the customer might say that this is not what we asked etc. etc.

To my experience, if you develop following a formal specification without constant customer communication, the chance of developing something where the customers says "this is not what I asked" is >100 times higher than when you talk to the customer daily. If you still run into that problem either, there is a simple solution: after each customer session, make a short written note what you have agreed upon. If necessary, send that note to the customer and give him a chance to make corrections. That works in an agile process as well as in any other kind of project.

Title: Extreme Programming practices make an application more error prone?

Body:
I am conducting academic research on the topic of Extreme Programming and whether its practices lead to creating space for more errors and bugs in applications.

From the experiences I gathered from many, I have comments that fall on both sides.  Many support it and consider it a daily necessity, with dynamics that can facilitate changing project requirements.  Many others argue that it leads to many problems, such as:

Over-involvement with the customer in the process leads to the expression of customer wishes rather than needs

Many products have multiple customers which lead to conflicting needs
and opinions, creating unnecessary blockades

Many products don’t have any external customers, where the product
is made for internal use or to be sold in the future. In these cases,
the team itself is playing the user as well as the developer, hence
killing the effectiveness of the process.

Not many things exist in formal documentation, this informality
leads to vague vision and can create problems where the customer
might say that this is not what we asked etc. etc.

The questions is why such conflicting opinions exist regarding XP.  Is it a matter of different scenarios? Is there something else? To what extent is the claim (as written in the title) true?

I would like people who are working or have worked with XP here to contribute their learning and real world experiences.  It would be ideal if you have any facts or references to support you answer.


答:
I actually had this discussion with a colleague this very morning, in the context of our own software.

A fair amount of the ASP.NET MVC software I've seen seems to be a little confused with regards to where the business logic goes. There isn't really a set "logic layer" in the framework. The most common approach seems to be to put the business logic in the controller. This suggests that the DA/BL/UI model maps directly: DA = Model, BL = Controller, UI = View.

The problem is that you often find yourself having to handle more complex presentation logic, creating viewmodels which hide away any details of the internal implementation that the user and UI don't need to know about, and/or wrap up various bits of information that the view will need. If your controllers start handling a lot of this as well, then your presentation and business logic layers have started to merge, which is generally a bad idea. There are ways to keep the logic in the views (if you're that way inclined then using Knockout makes your views able to do more of the work themselves, for example) but in many cases the presentation logic spills out. What I tend to do when that happens is to pull the logic out and leave the controllers to focus on dealing with viewmodels and user interactions.

With the View and Controller now under the banner of "UI", you're left with data access and business logic both needing homes, and only the M of MVC left free. At this point I tend to create a new layer; a set of classes that handle the business logic. I model these classes on the controllers, in that there's one for each "area" - often one per model, but not always. For example, a simple web store might have a ProductsController handling the product listings pages, and a corresponding ProductsManager handling the business logic around Product objects. Any controller can use the ProductsManager if it needs to, but most of the stuff that will need to will probably be in the ProductsController.

How you refer to this is a matter than can be debated (and has been, quite a bit). It boils down to whether you consider these logic-layer classes to be part of your model, and that mostly depends on what you mean by "model". If you think of the word "model" as meaning "a representation of an entity (or system)", then these logic-layer classes are more likely to be something else, and you've turned MVC into MLVC or something (which is fine). If you think of "model" as meaning "a representation of an entity or system and its behaviour", then yes, these logic-layer classes are part of your model, forming the "behaviour of the system" part while your entity classes do the rest. It can be argued that the latter is more correct, or at least closer to the definition of "model" that is commonly used by people like systems analysts.


答:
I'm no lawyer, but this seems quite difficult. Let's begin at the GPL FAQ on program output. The particular question is not exactly what you ask in your question, but the answer is perfectly relevant:

Is there some way that I can GPL the output people get from use of my program? For example, if my program is used to develop hardware designs, can I require that these designs must be free?

In general this is legally impossible; copyright law does not give you any say in the use of the output people make from their data using your program. If the user uses your program to enter or convert his own data, the copyright on the output belongs to him, not you. More generally, when a program translates its input into some other form, the copyright status of the output inherits that of the input it was generated from.

So the only way you have a say in the use of the output is if substantial parts of the output are copied (more or less) from text in your program. For instance, part of the output of Bison (see above) would be covered by the GNU GPL, if we had not made an exception in this specific case.

So, it varies from domain to domain, but generally, you cannot impose conditions on the output of software using only a license on that software itself. Instead, you'll need to force the user to enter into a contract with you that imposes restrictions on future works that they create using your software, even if those works are not direct derivatives of your software. I'm not sure of the legal intricacies involves there (so talk to a lawyer), but I know that, personally, if I wanted to use software that required an agreement to allow someone else to control creative works that I'd not yet created, I'd stay far away from that software.

Note that the Affero GPL handles a small subset of this case: when no binary is transferred but the program is used over a network, each user must be given access to the source.

Title: Does any well-known license require to make modifications available when only derived *output* is published?

Body:
Is there a way to make sure that modifications to free software are released even when no binaries of modified code are conveyed? Though it may sound odd and from what I understand, e.g., GPL requires to distribute source only if binary is conveyed to other parties thus making it possible for unlimited private use as it does not fall into propagation.

I would like to make sure that if someone publishes a final finding in a scientific literature derived with the aim of a modified code, then those modifications are made available.

Does any well-known license have these provisions? If not, why would it be bad? From what I understand if I add those provisions explicitly, it would limit the freedom, e.g., rendering result non-GPLish.

From one of the answers that disappeared, I guess I can add an Additional Term but would not it be a further restriction that can be simply ignored

If the Program as you received it, or any part of it, contains a
  notice stating that it is governed by this License along with a term
  that is a further restriction, you may remove that term.

From what I understand it looks like it depends on how I state it. If I require to make private modification used to derived a result that was published to make available, then it would be okay, otherwise it would be a further restriction if I prohibit from privately using and not making modified code available.

Example

A implements a super duper algorithm but they is bad with BLAS and it takes a long time for the simulation. B claims that they refined the code using CUDA or whatever and can easily get results within second. Based on their work they found this and that from simulations and got it published. And that is it. No GPL violation and no better code for community.

Another example

Imagine if some animation studio took Blender, implemented an improved version of Cycles that allows them to render things way faster and with less problems. So now they have an advantage as they can deliver animations to clients faster. No software conveyance, no GPL violation, no improvements sharing.


问:
Should I create a separate table for every derived type?
We are having a debate and I am struggling to decide which side to come down on.  Basically we have an event type lets say like like this (simplified example).

```<code>public class Event
{
    public virtual int EventId { get; set; }
    public virtual DateTime DateTimeOccurred { get; set; }
}
</code>```

Many different events in our system can inherit from this so we could have:

```<code>public class UserUpdatedEvent : Event
{
    public virtual User User { get; set; }
}
</code>```

So basically any event can inherit from the base event and then we can track what is happening in our system which we need to do as tasks occur off these events, etc.

My question is should we have a table in the DB for every event so we can enforce foreign keys in this example to the user using table per type with each sub type referencing the base Events table.  Or should we have a single table and a column, maybe linking to an enum saying what the event is and then another column containing the primary key to that object but we won't be able to enforce referential integrity (maybe with a trigger but not ideal and not full proof).

The pros for a single table are speed, no joins, simpler to build/maintain, etc.  The cons are no referential integrity, we are using EF and table per type does work very well, on the other hand we produce a lot of reports and these are direct SQL sprocs for speed and the joins will become pretty heavy.

How do people normally come down on a situation like this?

Many thanks for any insight into existing systems with similar problems and helping us settle this decision.

答:
I would split object propeties into two tables.

One table contains the "core" attributes common to all classes and defined in Event. A classic table with one column per property.

A second table contains all properties that are different from those common ones. They are stored in a table with the columns type, id, key, value. (type is optional but can help you optimize your queries)

See EAV (entity attribute value)


答:
Too much involvements of the customer in the process make him start expressing his wishes rather than his needs to the software

Are you developing software based upon what a customer needs?  What if a customer wants?  Will you deny the customer because "hey customer, I only build software based upon need!??"

I interned at an extreme programming and agile shop.  I saw first-hand weekly customer interactions that at times drove QA and developers nuts.  But they delivered exactly what the customer wanted, when he wanted it, and it was clear during "Show and Tell" with the customer what he did, what he didn't, and what should be as he wanted.

Many products have multiple customers which lead to conflicting needs and opinions leading unnecessary blockades

Not unnecessary blockades if the extreme and agile shop make it clear the objectives of implementation and what will and will not be incorporated in the product. Different versions of the same product is also a possibility and it depends upon what is negotiated.  This need not be a point of contention that halts productivity or leads to unnecessary blockades.

Many products don’t have any external customers (products organization made for them or to be selling in future). In this case the team itself is playing the user as well as the developer hence killing the effectiveness of the process.

Not necessarily.  Even external user interface whereby one is interviewing people at random on the street to determine what an interface for a particular device would look interesting to them is a possibility.

Not many things exists in formal documentation, this informality leads to vague vision and can create problems where the customer might say that this is not what we asked etc. etc.

Then formal documentation need be employed. Formal documentation holds the customer's feet to the fire and a "this is what you told us you wanted" one-line punch-card coincides with the documentation and customer interaction so there are no excuses. As I had opportunity to see this in action as an intern at an extreme and agile shop, the customer signed off on documentation weekly.  The customer also had a chance to implement changes and had to sign off on those too.  If there is a lack of documentation, there is an invitation to disaster.

The questions is why such conflicting opinions exists regarding XP, is it the matter of different scenarios or is there something else and till what extent the claim (as written in the title) is true.

I would say that depends upon the intelligence of the shop.  XP and Agile are guidelines and not instructions.  To operate successfully with XP and Agile, it has to be incorporated into operating practices and utilized throughout whole organization.  Mileage will always vary and some will undoubtedly claim it is bad, some will say it is good. where I interned, it was undoubtedly good and much success was had.

From my experience, how rigid one is to the principles of XP and Agile appear to determine if, when incorporated into the "daily grind", how successful it makes software development.  Where I interned, customer interaction drove everything and nothing was done without a customer having first stated it should be done.  The way this shop ran its' operations provided good, measurable success using sound principles of development as part of the XP and Agile framework tightly integrated into everything they do.


答: Be careful not to apply design patterns in general liberally.  Design patterns are intended to simplify or clarify design purpose and create better organization.  Design patterns should not be considered a goto in place of proper, well-thought programming.</p>


问:
should I throw exception from constructor?
I know I can throw exception from constructor in PHP but should I do it? For example, if a parameter's value is not as I expected it.

Or should I defer throwing an exception till a method is invoked. What are advantages and disadvantages in both cases?

答:
Why would you not validate the parameter set prior to instantiating your object?  Doing so would ensure that your object would be created, thus, removing any of the side affects that may occur from its failure.

Though I know you can check things in your constructor and toss exceptions, I prefer to write my constructors in such a way that they do not fail.  I perform parameter validation before instantiating objects which I can then toss exceptions without having my constructors fail.  I also generally do not try to instantiate new objects in my constructors, choosing instead to instantiate them as I need them.

Just my opinion.  PHP offers a lot of freedom - enjoy!

OwnerDisplayName: WordsWorth


问:
Why doesn't C# have local scope in case blocks?
I was writing this code:

```<code>private static Expression<Func<Binding, bool>> ToExpression(BindingCriterion criterion)
{
    switch (criterion.ChangeAction)
    {
        case BindingType.Inherited:
            var action = (byte)ChangeAction.Inherit;
            return (x => x.Action == action);
        case BindingType.ExplicitValue:
            var action = (byte)ChangeAction.SetValue;
            return (x => x.Action == action);
        default:
            // TODO: Localize errors
            throw new InvalidOperationException("Invalid criterion.");
    }
}
</code>```

And was surprised to find a compile error:

A local variable named 'action' is already defined in this scope

It was a pretty easy issue to resolve; just getting rid of the second var did the trick.

Evidently variables declared in case blocks have the scope of the parent switch, but I'm curious as to why this is. Given that C# does not allow execution to fall through other cases (it requires break, return, throw, or goto case statements at the end of every case block), it seems quite odd that it would allow variable declarations inside one case to be used or conflict with variables in any other case. In other words variables appear to fall through case statements even though execution cannot. C# takes great pains to promote readability by prohibiting some constructs of other languages that are confusing or or easily abused. But this seems like it's just bound to cause confusion. Consider the following scenarios:

If were to change it to this:

```<code>case BindingType.Inherited:
    var action = (byte)ChangeAction.Inherit;
    return (x => x.Action == action);
case BindingType.ExplicitValue:
    return (x => x.Action == action);
</code>```

I get "Use of unassigned local variable 'action'". This is confusing because in every other construct in C# that I can think of var action = ... would initialize the variable, but here it simply declares it.

If I were to swap the cases like this:

```<code>case BindingType.ExplicitValue:
    action = (byte)ChangeAction.SetValue;
    return (x => x.Action == action);
case BindingType.Inherited:
    var action = (byte)ChangeAction.Inherit;
    return (x => x.Action == action);
</code>```

I get "Cannot use local variable 'action' before it is declared". So the order of the case blocks appears to be important here in a way that's not entirely obvious -- Normally I could write these in any order I wish, but because the var must appear in the first block where action is used, I have to tweak case blocks accordingly.

If were to change it to this:

```<code>case BindingType.Inherited:
    var action = (byte)ChangeAction.Inherit;
    return (x => x.Action == action);
case BindingType.ExplicitValue:
    action = (byte)ChangeAction.SetValue;
    goto case BindingType.Inherited;
</code>```

Then I get no error, but in a sense, it looks like the variable is being assigned a value before it's declared.
(Although I can't think of any time you'd actually want to do this -- I didn't even know goto case existed before today)

So my question is, why didn't the designers of C# give case blocks their own local scope? Are there any historical or technical reasons for this?

答:
I will quote Eric Lippert, which answer is pretty clear on the subject:

A reasonable question is "why is this not legal?" A reasonable answer
  is "well, why should it be"? You can have it one of two ways. Either
  this is legal:

```<code>switch(y)
{
    case 1:  int x = 123; ...  break;
    case 2:  int x = 456; ...  break;
}
</code>```

or this is legal:

```<code>switch(y)
{
    case 1:  int x = 123; ... break;
    case 2:  x = 456; ... break;
}
</code>```

but you can't have it both ways. The designers of C# chose
  the second way as seeming to be the more natural way to do it.

This decision was made on July 7th, 1999, just shy of ten years ago.
  The comments in the notes from that day are extremely brief, simply
  stating "A switch-case does not create its own declaration space" and
  then giving some sample code that shows what works and what does not.

To find out more about what was in the designers minds on this
  particular day, I'd have to bug a lot of people about what they were
  thinking ten years ago -- and bug them about what is ultimately a
  trivial issue; I'm not going to do that.

In short, there is no particularly compelling reason to choose one way
  or the other; both have merits. The language design team chose one way
  because they had to pick one; the one they picked seems reasonable to
  me.

So, unless you are more intime with the C# developer team of 1999 than Eric Lippert, you will never know the exact reason!

Body:
I will quote Eric Lippert, which answer is pretty clear on the subject:

A reasonable question is "why is this not legal?" A reasonable answer
  is "well, why should it be"? You can have it one of two ways. Either
  this is legal:

switch(y)
{
    case 1:  int x = 123; ...  break;
    case 2:  int x = 456; ...  break;
}

or this is legal:

switch(y)
{
    case 1:  int x = 123; ... break;
    case 2:  x = 456; ... break;
}

but you can't have it both ways. The designers of C# chose
  the second way as seeming to be the more natural way to do it.

This decision was made on July 7th, 1999, just shy of ten years ago.
  The comments in the notes from that day are extremely brief, simply
  stating "A switch-case does not create its own declaration space" and
  then giving some sample code that shows what works and what does not.

To find out more about what was in the designers minds on this
  particular day, I'd have to bug a lot of people about what they were
  thinking ten years ago -- and bug them about what is ultimately a
  trivial issue; I'm not going to do that.

In short, there is no particularly compelling reason to choose one way
  or the other; both have merits. The language design team chose one way
  because they had to pick one; the one they picked seems reasonable to
  me.

So, unless you are more intime with the C# developer team of 1999 than Eric Lippert, you will never know the exact reason!


问:
Dependency Inversion Principle: How to define "high-level policy" and "low-level detail" to other people?
I am trying to explain the dependency inversion principle to my (mostly junior) colleagues. How can we define which one is the "high-level policy" and which one is the "low-level detail" in a software? For example, if our software automates workflow of several business applications, why do we say that the workflow automation is the high-level policy and the business applications are the details?</p>

答:
Note: this has been completely rewritten from my earlier example

Think about power sockets. In any given nation, the high-level policy is that power sockets are always the same. It doesn't matter where you get you electricity from (coal, gas, nuclear), the sockets on the wall should always put out the same amount of power, through the same set of connectors.

Now you can plug any device into that socket, because they all have a common interface, the "plug". The high-level policy never has to dictate any part of that implementation detail. Just plug something in and it goes.

Now if you have a device that doesn't want AC power -- maybe it runs on a 7V DC circuit -- you can still use that high-level policy, you just need some kind of adapter between the power supply and the device. And, because everyone has the same high-level policy, the manufacturer can build that into the implementation, without any change to the high-level policy. The person connecting the implementation to the policy (you, plugging your laptop in) doesn't really need to understand either.

Further, if the manufacturer wants to sell the same device in another country, all they have to do is develop a different adapter. So the same implementation can work with multiple policies while the same policy can run multiple implementations.

This is a perfect example of dependency inversion.

But here's the interesting bit: Go back to what I first said. "It doesn't matter where you get you electricity from." This is also an implementation detail. The high-level policy is still that all power sockets are the same shape and emit the same type of power. The low-level implementation details are both where the electricity comes from and what it runs.

In programming terms, that means the high-level policy is the interface (Where a language supports it. Another form of DI is duck-typing.) that an API provides and the application consumes, and the low-level implementation details are both the application consuming it and the API itself, neither of which need to understand each other.

Adapters may be used to fit the same implementation into different policies.


问:
How to read large XML files efficiently
I have a large XML file (about 75,000 lines) where I have to build a catalogue (houses) from. Building the lists works fine, but now I have a problem.

The catalogue should have a detailed presentation page of one house. One house (<item id="123">) has about 800-1200 lines of data, based on the house type.

Which is the best way, as referring to making the script faster and saving lines of code, to read these data and present them?

Some houses, for example, have a sauna, and when these data are in the XML file, the presentation page should contain a section sauna.

I tried before to read the whole XML content with a recursive function into arrays and with a lot of foreaches (maximum depth of children is three) with SimpleXML, but it was really ugly slow, and the recursion did not work at all, because my computer could not handle so much input.

Is there any other way to build this data except to query every variable with if?

答:
XML is an inefficient method of storing large amounts of data. It uses a lot of disk space (look at an XML file and note what a large portion of it is taken up by the syntax and structure definition), and it is slow and memory-intensive to access.  The whole tree (or at least a large portion of it) must be parsed just to get a single element, and XML parsers often use several times the size of the file in memory space to do this.

If you need to do something performance sensitive (such as load information onto a web page), 75,000 lines of XML just isn't going to be fast.

If performance matters, you should really move the information into a relational database, as suggested by thorsten müller.  Then your task will become trivial.  Even if you have no choice but to receive the data in XML, have your program perform a one-time load of that XML file into the database whenever it is updated, and then use the database the rest of the way.  Besides being faster, the database will also be a lot easier to work with.

If you choose to stay with XML, you can get some help with your algorithm, but more information is needed.  I suggest posting the portion of the code that you describe on Code Review.

Title: How to read large XML files efficiently