问:
How often is software speed evident in the eyes of customers?
In theory, customers should be able to feel the software performance improvements from first-hand experience.

In practice, sometimes the improvements are not noticible enough, such that in order to monetize from the improvements, it is necessary to use quotable performance figures in marketing in order to attract customers.

We already know the difference between perceived performance (GUI latency, etc) and server-side performance (machines, networks, infrastructure, etc).

How often is it that programmers need to go the extra length to "write up" performance analyses for which the audience is not fellow programmers, but managers and customers?

答:
I just wanted to jump in here and offer an unusual case where....

*THE CUSTOMERS FREAKING CARE ABOUT PERFORMANCE AND NOTICE EVERY LITTLE CHANGE!.

It's in my field where we cover production rendering which tends to be analyzed to death in terms of performance by the customers themselves. A 2% slowdown in performance over a minor version can equate to the slowdowns being reported in the form of "bug reports" en masse.

Forum threads are often started with customers benchmarking their scenes against various versions of the software, where the customers are actually benchmarking more than the developers themselves. "This scene took 1 hour and 40 minutes to render in version X. It now takes 32 minutes in version Y."

"This scene took 18 minutes to load in version X, now it takes 4 minutes to load in version Y."

They're extremely appreciative when optimizations are applied, and that alone may suffice to warrant a purchase of a new, very expensive upgrade of the software, and sometimes with only modest improvements like a 10% reduction in times.

In some larger contexts, it may also save the customer enormous amounts of money when the product is sped up, since some bigger studios use render farms where they have to pay for hundreds of machines rendering all day long, and any improvement in times here can speed up their whole production process (and possibly even yield better results when artists are more productive creating art rather than waiting for it to render).

So there exist fields like this where the customers really, really, really notice -- sometimes even more than the developers themselves, and this is outside of UI interaction concepts which are more about latency than throughput.

How often is it that programmers need to go the extra length to "write
  up" performance analyses for which the audience is not fellow
  programmers, but managers and customers?

In our case, all the time, with just about every minor release. Speed is one of the top selling points, and even the most technical benchmarks and performance analyses are actually appreciated and understood by the customers and managers. The perception of customers is often like rabid wolves, hungry for more optimizations, and trying to make suggestions to the developers about how to potentially make things go faster. In this case, it actually takes discipline to resist some of the customer urges to optimize further and focus on other metrics like maintainability and feature enhancements.


问:
Should defects be raised during sprints or just noted in the stories and assigned to developers?
I have heard that when testing stories developed in the current sprint, no issues are raised they are not completed (i.e. definition of done was not fulfilled) and that you should only raise issues when you find another, unrelated issue etc. While it does make sense not to raise issues on something that developers still can work on till the end of the sprint, I fail to find some "best practices". Is that a correct approach?</p>

答:
The Scrum Guide provides some guidance on this

The Increment is the sum of all the Product Backlog items completed
  during a Sprint and the value of the increments of all previous
  Sprints. At the end of a Sprint, the new Increment must be “Done,”
  which means it must be in useable condition and meet the Scrum Team’s
  definition of “Done.”

Therefore, a team practicing Scrum should not knowingly allow the product to suffer unexpected regression of functionality. They should also disallow known defects in new functionality unless the Increment is in a "useable condition and meets the Scrum Team's definition of "Done."

Also

As Scrum Teams mature, it is expected that their definitions of “Done”
  will expand to include more stringent criteria for higher quality. Any
  one product or system should have a definition of “Done” that is a
  standard for any work done on it.

Scrum allows leeway for Scrum Teams to create their own processes for managing work in progress and bugs in production code. This is because there is no "best practice" in a complex domain like most software development. Successful practices emerge as the result of performing experiments and choosing the ones that tend to succeed. More on complexity here.

Enough beating around the bush...HERE'S YOUR ANSWER

Pick a process e.g. log and fix all bugs regardless of when and where they are found and start measuring and retrospecting on the effect of that new process. The team will see pros and cons and will eventually settle on a proven method that works for them within the Scrum framework.

Most importantly, the Scrum Team will have made its decision empirically and in a self-organization manner.

ContentLicense: CC BY-SA 3.0


问:
How to safely and cheaply delete a directory's contents that are constantly being deleted/overwritten
I'm coding something that will check a specific static location's directory, and if it exists, and a certain process has not run in n minutes, it deletes that directory's files and all sub-directory files.

The catch is that this directory is constantly being written to. As soon as this service detects a build process trigger, that directory is being copied to and the deletion must not interfere with the files being copied into it.

Now, I came up with a design that I think is mostly safe, but it's expensive, and I have a feeling there is a cleverer way to do it. I'm looking to make this process cheaper and faster, if possible, and safer, if I'm neglecting any important caveats that aren't essentially self-evident. This isn't the full code logic, just the deletion logic for the files.

My main goal is making this cheaper without compromising robustness, and covering any obvious use cases that could compromise it not covered in the diagram below.

Note: Although I appreciate software suggestions, I'm looking to make this on my own.

Current suggestions:

Use an alternative (currently unspecified) to the file system for queuing -kevin cline

Rename build directory so that it won't be interacted with when a build event triggers -avnr

答: You could rename the directory to a new name and create a new empty directory by that name, both steps under the same lock (e.g., by locking the parent directory, but that depends on your specific file system). Then release the lock and empty the renamed directory. IMHO using this method you should be able to skip the renaming of each individual file.</p>


问:
Using a WrapBootstrap theme in an open source project
I'm developing a desktop analytics application wrapped in Electron (that integrates with an external REST API), and am looking to use one of the WrapBootstrap themes to spruce it up.

I'm deciding between the MIT or Apache license for the project itself, which will be hosted on GitHub.

I'll be fronting the cost of the theme myself, and including it in the project's repository.

There are 3 different license "styles" listed: http://support.wrapbootstrap.com/knowledge_base/topics/usage-licenses

Both the Single and Multiple Application licenses indicate:

The item may not be redistributed or resold.

Does this restriction require me to purchase an Extended license for the above use?

答:
It depends on the components that make up the item. For example, if you want a theme that contains GPL components, then their copyleft is viral. Which is one reason why copyleft is banned in many open source projects (such as Android). From a different perspective, the theme might have a component with some innocent-looking license that is actually legal-engineered to inflict some limitation of which you're not aware.

So I suggest to get the approval of the seller of the specific theme, and not just rely on the fact that the Extended license allows redistribution. There is some extra fine print on that license. I'm familiar with people who fell into IP-licensing honeypots and it ain't fun at all.

Either way, the non-Extended licenses are definitely not applicable in your case.


问:
Rationale for modern languages implement changeable global variables
Common sense seems to be that usage of global variables is bad, in particular if they are used as real variables (i.e. changing state; e.g. Why is Global State so Evil?).

How does it come then, that virtually any language I've used so far implements them nonetheless, especially the modern fourth generation (4GL) languages like python or perl?

Do there exist procedural languages without global variables. If not, would it be possible to create one completely without the concept of global rewritable state?

答: Newspeak has no global variables. And in fact no global and/or static state at all. It also doesn't have a global namespace (the designer considers a global namespace to be equivalent to global and/or static state).


问:
Will Web Components in HTML Add Too Much Complexity?
The way I've been accustomed to User Interfaces is to see everything as a div. Where a div is essentially a box and they can be stylized in CSS.

```<code><div class='container'>
   <div class='header'></div>
   <div class='mainbody'>
       <div class='someusercontrol'>
       </div>
   </div>
   </div class='footer'></div>
</div>
</code>```

But it seems like nowadays with React and Angular Directives, and now with Web Components people are slowly shifting to:

```<code><container>
     <header></header>
     <mainbody>
             <someusercontrol someattribute='123'>
             </someusercontrol>
     </mainbody>
     <footer></footer>
</container>
</code>```

The problem that I see with the second example is that now all elements are heterogeneous as opposed to homogenous with divs. Each element can have different attributes / properties and can work in different ways. This can lead to complexity when viewing someone else's code for the very first time as each one of these "things" works differently.

But some argue that now elements will be more modular and easier to re-use. But I'm wondering if it's worth it. Are web components really the way to go? It sure seems that the future indicates they are.

答:
Actually, I think it will reduce complexity.

The point isn't to substitute a div tag for some other random tag. In your example, it really doesn't help anything. But that's because you've left most of the tags in your example empty. In reality they would be filled with markup, more like:

```<code><div class='container'>
   <div class='header'>
       <div class='logo'/>
       <div class='menu'>
           <div class='menuitem'>
              ...
           </div>
        </div>
   </div>
   <div class='mainbody'>
       <div class='someusercontrol'>
            <label>All the money</label>
            <input type="checkbox">
            <input type="text"/>
            <img res="data:..."/>
       </div>
   </div>
   </div class='footer'>
       <div class="copyright">Lorem Ipsum...</div>
       <a href="terms">Terms</a>
   </div>
</div>
</code>```

There are some problems with this:

All that markup makes it hard to get an idea of what's going on.

I can't easily reuse the same markup in multiple places/pages.

One way of solving this is to use a template language, so you could write something like:

```<code><div class='container'>
    {{include:header.html}}
    <div class="mainbody">
         {{include:someusercontrol label="All the money"}}
    </div>
    {{include:footer.html}}
</div>
</code>```

But this has two problems:

We've got a mix of syntaxes between the template and the html.

Its easy to include the markup for something without including the appropriate css or js.

The components address both of these. They extend html syntax, so that the code to include a template look like an html tag, and including the template also automatically includes the approriate js and css.

Basically, unless you are willing to duplicate markup all over the place, you need to something pretty much equivalent to these components. Components happen to be a neat and clean way of doing it.

Body:
Actually, I think it will reduce complexity.

The point isn't to substitute a div tag for some other random tag. In your example, it really doesn't help anything. But that's because you've left most of the tags in your example empty. In reality they would be filled with markup, more like:

<div class='container'>
   <div class='header'>
       <div class='logo'/>
       <div class='menu'>
           <div class='menuitem'>
              ...
           </div>
        </div>
   </div>
   <div class='mainbody'>
       <div class='someusercontrol'>
            <label>All the money</label>
            <input type="checkbox">
            <input type="text"/>
            <img res="data:..."/>
       </div>
   </div>
   </div class='footer'>
       <div class="copyright">Lorem Ipsum...</div>
       <a href="terms">Terms</a>
   </div>
</div>

There are some problems with this:

All that markup makes it hard to get an idea of what's going on.

I can't easily reuse the same markup in multiple places/pages.

One way of solving this is to use a template language, so you could write something like:

<div class='container'>
    {{include:header.html}}
    <div class="mainbody">
         {{include:someusercontrol label="All the money"}}
    </div>
    {{include:footer.html}}
</div>

But this has two problems:

We've got a mix of syntaxes between the template and the html.

Its easy to include the markup for something without including the appropriate css or js.

The components address both of these. They extend html syntax, so that the code to include a template look like an html tag, and including the template also automatically includes the approriate js and css.

Basically, unless you are willing to duplicate markup all over the place, you need to something pretty much equivalent to these components. Components happen to be a neat and clean way of doing it.


问:
Dependency Inversion Principle and Hollywood analogy
Often DIP is known as the Hollywood principle: "Don't call us, we'll call you.".

But doesn't always higher level module calls lower level module, whether there is dependency inversion or not?

Then why is DIP called the Hollywood principle?

Am I missing something? Thanks!

答:
A. High-level modules should not depend on low-level modules. Both
should depend on abstractions.

B. Abstractions should not depend on details. Details should depend on abstractions.

A long time ago, in a galaxy far, far away, there existed two droids: a supervisor droid (model S-100) who was programmed to routinely issue requests to a cleaner droid (model C-100) to clean a room. The cleaner droid was programmed to grab a broom, use its wheels to go to the appropriate location, and begin cleaning the dirty room.

This was all fine and dandy, until the invention of a new cleaning device for certain situations known as a mop.

Since the C-100 was not programmed to use a mop, a new model of cleaning droid had to be invented that could utilize both a broom and a mop, known as C-101.

However, since the S-100 was not programmed to communicate with anything but a C-100, a new model of the supervisor, S-101, also had to be created to account for C-101.

... and this lead to order in the universe...

... until the invention of a new cleaning device known to the mystics as the dust mop.

At this point, the designers were getting fed up. To create a new C-102 model here to handle this new and alien cleaning device would likewise require a new S-102 model.

To solve this problem, the elders convened and decided that all of these cleaning devices should be standardized with an abstract cleaning interface.

This abstract cleaning interface allowed the new C-102 to work with any compatible cleaning device through this common interface. Unfortunately this still required a new S-102 to be designed to interface with C-102, but the designers were confident that no new cleaning devices introduced hereafter would require a new supervisor model.

... and order was once again restored in the galaxy...

... until the invention of a new transportation device known as legs which allowed droids to work in more complex environments.

At this point, the designers of the S and C series of droids threw themselves off a cliff.

The elders convened and brought in a new design team to solve the problem. The new design team recognized that in order to avoid yet further and further modifications to the S model, the C cleaning droid model also needed an abstract interface through with the supervisor model would communicate. In addition, they realized a similar treatment was required for an abstract transportation interface.

... and cleanliness spread through the universe like a spring breeze (except, of course, that spring time did not exist in this galaxy)...

... until the invention of the hoverboard which made legs and wheels obsolete.

This caused the new design team to commit ritualistic light saber seppuku. The elders then brought yet another design team to replace the former one.

At this point, design team C realized that even though the supervisor droid did not have to change, the cleaning droid still needed programming to be aware of all the various types of cleaning and transport devices available for it to use (shown in red in above).

They also realized that the supervisor droid, who was merely programmed to routinely issue requests to clean rooms, was actually much easier to change than the cleaner droid, and had far less general applicability than a cleaner droid.

And thus, the designers decided to make cleaner droids accept requests to use any compatible cleaner device and any compatible transport device from a supervisor droid.

They also decided to use blue for abstract interfaces in their schematics, after realizing that they like blue.

... and thus was born the basic idea and primitive form of both dependency inversion and dependency injection, which would get further and further embellished in the future.

A curious thing happened with this new design. Instead of the cleaner robot being programmed to use these low-level cleaning and transportation devices directly, it was now being told what type of cleaning and transportation devices to use. Using its very limited A.I., the new prototype model of cleaning droid attempted to ask the designers what cleaning devices it should use now that it is no longer programmed to use specific ones. "Don't worry about it, your model will be told what to use when needed, but rest assured that a compatible cleaning and transportation device conforming to your abstract requirements will be supplied," said the designers.

A random passer-by then looked at this schematic and scribbled something obscure about efferent and afferent couplings yielding an instability metric while mumbling something cryptic about the ease at which packages can be modified.

The galaxy rejoiced with their new and flexible design until the designers realized that the supervisor droid was doing too much, and the elders realized that the designers and the factories they worked in should be included in the schematic.

The End


问:
Why is the most common integer number 32 bits, but the most common floating point number 64 bits?
Coming from a Java and C# background, I've learned to use int (32 bits) whenever I need a whole number, and double (64 bits) when dealing with fractional values. Most methods from their respective frameworks (JVM and .NET) usually expect these two types.

My question is, why don't we use both long and double for consistency? I know that having 64 bits of precision in integers is not needed most of the time, but then again, we don't usually need 64 bits of precision in floating point numbers, or do we?

What is the reasoning behind this, if any?

答:
short, int, single and double are the same size in java as they are in most common C compilers for 32-bit and 64-bit platforms and C like java clearly considers int to be the main integer type and double to be the main floating point type. I think it's reasonable to say that Java inherited this convention from common C compilers at the time.

Integers are usually used for counting or indexing stuff.It's pretty rare (though not unheard of) to need to count or index more than 2 billion of something. Indeed prior to C99 you had to use vendor specific types if you wanted a 64-bit integer.

Floating point numbers are usuaully used as an approximation of the real numbers. Single precision is good enough much of the time but it's not hard to find problems where it causes an unacceptable amount of rounding error. I expect demand from scientific computing is what drove double precision floating point support to be ubiquitous long before 64-bit integer support was.

What I do find curious is that C seems to encourage use of double while fortran seems to encourage the use of single precision.


问:
Is it a good idea to define one big private function in a class to maintain valid state, that is, to update the object's data members?
Although in the code below a simple single item purchase in an e-commerce site is used, my general question is about updating all data members to keep an object's data in valid state at all times.

I found "consistency" and "state is evil" as relevant phrases, discussed here:
https://en.wikibooks.org/wiki/Object_Oriented_Programming#.22State.22_is_Evil.21

```<code><?php

class CartItem {
  private $price = 0;
  private $shipping = 5; // default
  private $tax = 0;
  private $taxPC = 5; // fixed
  private $totalCost = 0;

  /* private function to update all relevant data members */
  private function updateAllDataMembers() {
    $this->tax =  $this->taxPC * 0.01 * $this->price;
    $this->totalCost = $this->price + $this->shipping + $this->tax;
  }

  public function setPrice($price) {
      $this->price = $price;
      $this->updateAllDataMembers(); /* data is now in valid state */
  }

  public function setShipping($shipping) {
    $this->shipping = $shipping;
    $this->updateAllDataMembers(); /* call this in every setter */
  }

  public function getPrice() {
    return $this->price;
  }
  public function getTaxAmt() {
    return $this->tax;
  }
  public function getShipping() {
    return $this->shipping;
  }
  public function getTotalCost() {
    return $this->totalCost;
  }
}
$i = new CartItem();
$i->setPrice(100);
$i->setShipping(20);
echo "Price = ".$i->getPrice().
  "<br>Shipping = ".$i->getShipping().
  "<br>Tax = ".$i->getTaxAmt().
  "<br>Total Cost = ".$i->getTotalCost();
</code>```

Any disadvantages, or maybe better ways to do this?

This is a recurring issue in real-world applications backed by a relational database, and if you do not use stored procedures extensively to push all the validation into the database. I think that the data store should just store data, while the code should do all the run-time state maintaining work.

EDIT: this is a related question but does not have a best-practice recommendation regarding a single big function to maintain valid state:
https://stackoverflow.com/questions/1122346/c-sharp-object-oriented-design-maintaining-valid-object-state

EDIT2: Although @eignesheep's answer is the best, this answer - https://softwareengineering.stackexchange.com/a/148109/208591 - is what fills the lines between @eigensheep's answer and what I wanted to know - code should only process, and global state should be substituted by DI-enabled passing of state between objects.

答:
All else being equal, you should express your invariants in code. In this case you have the invariant

```<code>$this->tax =  $this->taxPC * 0.01 * $this->price;
</code>```

To express this in your code, remove the tax member variable and replace getTaxAmt() with

```<code>public function getTaxAmt() {
  return $this->taxPC * 0.01 * $this->price;
}
</code>```

You should do something similar to get rid of the total cost member variable.

Expressing your invariants in your code can help avoid bugs. In the original code, the total cost is incorrect if checked before setPrice or setShipping is called.

Body:
All else being equal, you should express your invariants in code. In this case you have the invariant

$this->tax =  $this->taxPC * 0.01 * $this->price;

To express this in your code, remove the tax member variable and replace getTaxAmt() with

public function getTaxAmt() {
  return $this->taxPC * 0.01 * $this->price;
}

You should do something similar to get rid of the total cost member variable.

Expressing your invariants in your code can help avoid bugs. In the original code, the total cost is incorrect if checked before setPrice or setShipping is called.


问:
Do you apply a code of conduct on accepting jobs, projects or requirements?
When writing software for yourself, your company or third parties, do you always consider certain principles, values, standards, or rules of behavior that guide the decisions, procedures and systems in a way that contributes to the welfare of its key stakeholders, and respects the rights of all constituents affected by its operations?

And can this code of conduct sometimes be overruled by business requirements, lack of technical skills or other friction during the development process?

Some random examples in order of severity. (yes that is controversial) :

Do you accept known bugs as a risk for the end-user?

When writing applications, do you always give the end user the chance for a complete de-install?

Do you always secure and encrypt private data delivered end-users in your web application?

Do you alwask ask the end-user before submitting his entered data to the server?

Did you ever wrote an application that sends unwanted e-mails?

Did you ever work on harvesting or scraping projects only for the benefit of the business?

Did you ever write software that is legal but moraly controversial, like  for weapons industry.

Did you ever wrote software that ( can intentionally) or is be used for criminal activities

It would be nice if you can get a good case with explanation on your moral and ethical decisions.

note: Since ethics and law are quite local and cultural dependent, it would be interesting if you add the location of the "crime scene" with it.

答:
Everyone does all the time in everything they do. Your actions disclose your values, your morality and your ethics, regardless of what you say or think or think you believe. If there's a discrepancy between them and it arises from a considered choice, not some sort of reflex or "passion of the moment" or out of the possession of misinformation or ignorance you could not remedy, then that choice makes clear what your true moral and ethical values are. If compensation can change your choice when faced with a particular decision, then that simply shows that money or other forms of material value are part of your moral and ethical code.

I'll also add that your list discloses things about your own moral sentiments and does not stem from some absolute moral or ethical source. I am not passing judgment or commenting on it in any way, just pointing out that presumably these are things you care about and, perhaps, things you are sometimes conflicted about.

ContentLicense: CC BY-SA 2.5


问:
How to get around non-initialized objects in the `finally` block?
I like using final variables whenever possible. Often those variables have to be closed afterwards. Note that I'm currently working on Java 6 so there is not closeable interface, but the same applies to later Java versions as well. For example:

```<code>final InputStream in;

try{
    in=new InputStream(...);
    //...
}catch(...) {}
finally{
    in.close(); //whoops!
}
</code>```

At that point the IDE will point out that in might be uninitialized. Wrapping the close statement in an if like so

```<code>if(in!=null)
    in.close();
</code>```

doesn't work either, despite null being the default value for uninitialized objects. My solution so far is to forget about declaring them final in the first place, and initializing them as null at declaration.

Another solution is to place the declaration and close statement in the try block:

```<code>try{
    final InputStream in=new InputStream(...);
    //...
    in.close();
}catch(InputStreamRelatedException) {};
</code>```

which would leave in open (?) in case of non-InputStream related exception.

What is the preferred method?

答:
You are running into difficulties because you are trying to handle two different cases with the same try-catch:

constructing the InputStream could fail, in which case you also can't close() anything.

using the InputStream could fail, in which case you need a close().

The solution is to separate these cases, and not instantiate the object in the same scope where it might be used:

final InputStream in;
try { in = new InputStream(); }
catch (...) { ... /* must throw or return, or assign to `in` */ }

try { doSomethingWith(in); }
catch (...) { ... }
finally { in.close(); }
```


问:
Microservices and isolated data storage models
I have been looking at Microservices for a while now. The concept is not new but it's communicated in a lightweight manner. So, I am very excited about this.

However, there is a question that I am not sure what the answer is: is each microservice supposed to have its own isolated data storage model completely? Consider the below:

Note: This is probably not a good example as the seperation here is done based on read/writes whereas the good approach would be to separate the business concerns.

products-write-service: responsible for dealing with creation and edits of the products. Knows MongoDB and writes data to there.

products-lookup-service: responsible for dealing with retrieving products by their identifiers, providing basic listing of products based on categories, etc. Knows MongoDB and reads data from there.

products-search-service: responsible for dealing with search requests on products. Knows Elasticsearch and reads data from there.

Here, we have two separate data storage techs between three services. However:

products-write-service and products-lookup-service works on the same data storage model. There is a tried relationship between two services.

at the end of the day, there should be a process to do ETL from MongoDB to Elasticsearch for products-search-service to function. In some sense, three microservices have their own data storage model but there is a middle process which knows about these two models.

What do you say about this model? Is the separation completely wrong here?

答:
For answer:  each service should have own storage mechanism.

Underline informations:

Maybe your micro-service (bounded context) boundaries are wrong.Product write and lookup service might be belong to same boundary but searching is not domain concept so searching might be another micro service boundary. Product lookup service is basic rest service in Product bounded context so it doesn't need to another database.

https://www.thoughtworks.com/insights/blog/domain-driven-design-services-architecture


答:
Any disadvantages[?]

Sure. This method relies on everyone always remembering to do something. Any method relying on everyone&always is bound to fail sometimes.

maybe better ways to do this?

One way to avoid the burden of remembering ceremony is calculating properties of the object that depend on other properties as needed, as @eigensheep suggested.

Another is making the cart item immutable and calculating it in the constructor/the factory method. You normally would go with "calculate as needed" method, even if you made the object immutable. But if the calculation is too time consuming and would be read many, many times; you may choose "calculate during creation" option.

```<code>$i = new CartItem();
$i->setPrice(100);
$i->setShipping(20);
</code>```

You should ask yourself; Does a cart item without a price makes sense? Can the price of an item change? After it is created? After its tax calculated? etc Maybe you should make CartItem immutable and assig price and shipping in the constructor:

```<code>$i = new CartItem(100, 20);
</code>```

Does a cart item make sense without the cart it belongs to?

If not, I'd expect $cart->addItem(100, 20) instead.

Title: Is it a good idea to define one big private function in a class to maintain valid state, that is, to update the object's data members?

Body:
Although in the code below a simple single item purchase in an e-commerce site is used, my general question is about updating all data members to keep an object's data in valid state at all times.

I found "consistency" and "state is evil" as relevant phrases, discussed here:
https://en.wikibooks.org/wiki/Object_Oriented_Programming#.22State.22_is_Evil.21

```<code><?php

class CartItem {
  private $price = 0;
  private $shipping = 5; // default
  private $tax = 0;
  private $taxPC = 5; // fixed
  private $totalCost = 0;

  /* private function to update all relevant data members */
  private function updateAllDataMembers() {
    $this->tax =  $this->taxPC * 0.01 * $this->price;
    $this->totalCost = $this->price + $this->shipping + $this->tax;
  }

  public function setPrice($price) {
      $this->price = $price;
      $this->updateAllDataMembers(); /* data is now in valid state */
  }

  public function setShipping($shipping) {
    $this->shipping = $shipping;
    $this->updateAllDataMembers(); /* call this in every setter */
  }

  public function getPrice() {
    return $this->price;
  }
  public function getTaxAmt() {
    return $this->tax;
  }
  public function getShipping() {
    return $this->shipping;
  }
  public function getTotalCost() {
    return $this->totalCost;
  }
}
$i = new CartItem();
$i->setPrice(100);
$i->setShipping(20);
echo "Price = ".$i->getPrice().
  "<br>Shipping = ".$i->getShipping().
  "<br>Tax = ".$i->getTaxAmt().
  "<br>Total Cost = ".$i->getTotalCost();
</code>```

Any disadvantages, or maybe better ways to do this?

This is a recurring issue in real-world applications backed by a relational database, and if you do not use stored procedures extensively to push all the validation into the database. I think that the data store should just store data, while the code should do all the run-time state maintaining work.

EDIT: this is a related question but does not have a best-practice recommendation regarding a single big function to maintain valid state:
https://stackoverflow.com/questions/1122346/c-sharp-object-oriented-design-maintaining-valid-object-state

EDIT2: Although @eignesheep's answer is the best, this answer - https://softwareengineering.stackexchange.com/a/148109/208591 - is what fills the lines between @eigensheep's answer and what I wanted to know - code should only process, and global state should be substituted by DI-enabled passing of state between objects.

Body:
Any disadvantages[?]

Sure. This method relies on everyone always remembering to do something. Any method relying on everyone&always is bound to fail sometimes.

maybe better ways to do this?

One way to avoid the burden of remembering ceremony is calculating properties of the object that depend on other properties as needed, as @eigensheep suggested.

Another is making the cart item immutable and calculating it in the constructor/the factory method. You normally would go with "calculate as needed" method, even if you made the object immutable. But if the calculation is too time consuming and would be read many, many times; you may choose "calculate during creation" option.

$i = new CartItem();
$i->setPrice(100);
$i->setShipping(20);

You should ask yourself; Does a cart item without a price makes sense? Can the price of an item change? After it is created? After its tax calculated? etc Maybe you should make CartItem immutable and assig price and shipping in the constructor:

$i = new CartItem(100, 20);

Does a cart item make sense without the cart it belongs to?

If not, I'd expect $cart->addItem(100, 20) instead.


问:
How to make one test depend on the results of another test?
Let's say there is a utility class that provides some common static methods used everywhere in your code by many other classes.

How would you design your unit tests for the consumers of the utility so that their tests fail if any one of the utility tests do not pass? Can you do it or do you have to check it yourself whether the utility class tests are all green?

For example I have a message-splitter utility that is used (or rather its output) by a message-parser. I'd like to be sure that the message-splitter works correctly before the message-parser gets tested.

I have written tests for both of them but is there a way to link them and make one test depend on the result of some other test?

I couldn't find a suitable tag for this but I'm using Visual Studio's unit test engine.

答:
I have the habit of always trying to differentiate application level code from framework level code, so I have encountered the problem that you are describing quite often: you usually want all the framework level code to be tested before any application level code begins to be tested.  Also, even within framework level code, there tend to be some fundamental framework modules that are used by all other framework modules, and if something fails in the fundamentals, there is really no point in testing anything else.

Unfortunately, purveyors of testing frameworks tend to have somewhat rigid ideas about how their creations are meant to be used, and are rather protective of those ideas, while the people who use their frameworks tend to accept the intended usage without questioning.  This is problematic, because it stifles experimentation and innovation.  I don't know about everyone else, but I would prefer to have the freedom to try to do something in an odd way, and see for myself whether the results are better or worse than the established way, rather than not having the freedom to do things my way in the first place.

So, in my opinion, test dependencies would be an awesome thing to have, and in lieu of that, the ability to specify the order in which tests will be executed would be the next best thing.

The only way I have found to address the issue of ordering tests is by careful naming, so as to exploit the tendency of testing frameworks to execute tests in alphabetical order.

I do not know how this works in Visual Studio, because I have yet to do anything involving extensive testing with C#, but in the Java side of the world it works as follows: Under the source folder of a project we usually have two subfolders, one called "main", containing the production code, and one called "test", containing the testing code.  Under "main" we have a folder hierarchy which exactly corresponds to the package hierarchy of our source code.  Java packages roughly correspond to C# namespaces. C# does not require you to match the folder hierarchy to the namespace hierarchy, but it is advisable to do so.

Now, what people usually do in the Java world is that under the "test" folder they mirror the folder hierarchy found under the "main" folder, so that each test resides in the exact same package as the class that it tests. The rationale behind this is that quite often the testing class needs to access package-private members of the class under test, so the testing class needs to be in the same package as the class under test.  In the C# side of the world there is no such thing as namespace-local visibility, so there is no reason to mirror the folder hierarchies, but I think that C# programmers do more or less follow the same discipline in structuring their folders.

In any case, I find this whole idea of allowing the testing classes to have access to package-local members of the classes under test misguided, because I tend to test interfaces, not implementations.  So, the folder hierarchy of my tests does not have to mirror the folder hierarchy of my production code.

So, what I do is that I name the folders (meaning, the packages) of my tests as follows:

```<code>t001_SomeSubsystem
t002_SomeOtherSubsystem
t003_AndYetAnotherSubsystem
...
</code>```

This guarantees that all the tests for "SomeSubsystem" will be executed before all tests for "SomeOtherSubsystem", which in turn will all be executed before all tests for "AndYetAnotherSubsystem", and so on, and so forth.

Within a folder, individual test files are named as follows:

```<code>T001_ThisTest.java
T002_ThatTest.java
T003_TheOtherTest.java
</code>```

Of course, it greatly helps that modern IDEs have powerful refactoring capabilities which allow you to rename entire packages (and all sub-packages, and all code that references them) with just a couple of clicks and keystrokes.

Body:
I have the habit of always trying to differentiate application level code from framework level code, so I have encountered the problem that you are describing quite often: you usually want all the framework level code to be tested before any application level code begins to be tested.  Also, even within framework level code, there tend to be some fundamental framework modules that are used by all other framework modules, and if something fails in the fundamentals, there is really no point in testing anything else.

Unfortunately, purveyors of testing frameworks tend to have somewhat rigid ideas about how their creations are meant to be used, and are rather protective of those ideas, while the people who use their frameworks tend to accept the intended usage without questioning.  This is problematic, because it stifles experimentation and innovation.  I don't know about everyone else, but I would prefer to have the freedom to try to do something in an odd way, and see for myself whether the results are better or worse than the established way, rather than not having the freedom to do things my way in the first place.

So, in my opinion, test dependencies would be an awesome thing to have, and in lieu of that, the ability to specify the order in which tests will be executed would be the next best thing.

The only way I have found to address the issue of ordering tests is by careful naming, so as to exploit the tendency of testing frameworks to execute tests in alphabetical order.

I do not know how this works in Visual Studio, because I have yet to do anything involving extensive testing with C#, but in the Java side of the world it works as follows: Under the source folder of a project we usually have two subfolders, one called "main", containing the production code, and one called "test", containing the testing code.  Under "main" we have a folder hierarchy which exactly corresponds to the package hierarchy of our source code.  Java packages roughly correspond to C# namespaces. C# does not require you to match the folder hierarchy to the namespace hierarchy, but it is advisable to do so.

Now, what people usually do in the Java world is that under the "test" folder they mirror the folder hierarchy found under the "main" folder, so that each test resides in the exact same package as the class that it tests. The rationale behind this is that quite often the testing class needs to access package-private members of the class under test, so the testing class needs to be in the same package as the class under test.  In the C# side of the world there is no such thing as namespace-local visibility, so there is no reason to mirror the folder hierarchies, but I think that C# programmers do more or less follow the same discipline in structuring their folders.

In any case, I find this whole idea of allowing the testing classes to have access to package-local members of the classes under test misguided, because I tend to test interfaces, not implementations.  So, the folder hierarchy of my tests does not have to mirror the folder hierarchy of my production code.

So, what I do is that I name the folders (meaning, the packages) of my tests as follows:

t001_SomeSubsystem
t002_SomeOtherSubsystem
t003_AndYetAnotherSubsystem
...

This guarantees that all the tests for "SomeSubsystem" will be executed before all tests for "SomeOtherSubsystem", which in turn will all be executed before all tests for "AndYetAnotherSubsystem", and so on, and so forth.

Within a folder, individual test files are named as follows:

T001_ThisTest.java
T002_ThatTest.java
T003_TheOtherTest.java

Of course, it greatly helps that modern IDEs have powerful refactoring capabilities which allow you to rename entire packages (and all sub-packages, and all code that references them) with just a couple of clicks and keystrokes.


问:
Change database based on request origin
I am developing a web application which will be run by multiple companies.

Each company will have its own instance of that web application and its own database.  The web applications and databases will be running on the same server.

But each of these instances will be talking to one single Web API application to retrieve data from their database for that specific company.

My question is that I am wondering whether it is a good practice to change the database connection string in the Web API application based on the request origin? Or am I overlooking something?

答:
Now I am wondering whether it is a good practice to change the database connection string in the Web API application based on the request origin?

It depends upon what you mean by "based upon the request origin."  If you're referring to something that the client controls, such as an IP address, then what you're considering is likely a bad idea.  Things controlled by the client are easily spoofed and are effectively an invitation for mischief.

On the other hand, if you're referring to something that isn't easily spoofed, such as a client authentication token, then what you're considering is fine.

I can't tell which architecture you're planning on using.

Option A

```
          +->Application
          |
 Web API+->
          |
          +->DB
```

Option B:

```Application+->Web API+->DB```

In either case, the general concept that you're grappling with right now is termed "authorization."

Authentication is a related concept, but refers to the client proving they are who they say they are.  Typically authentication is done with passwords, certificates, tokens, etc....

Authorization is related to providing access to functionality based upon who the client authenticated as.  Determining database connection strings based upon client authorization is a fairly typical case for authorization.

If you happen to be using Option A, then the Web API will determine DB and application access based upon the client's authorization privileges.  And if you happen to be using Option B, then you'll need to make sure that the application passes along the authentication token into the Web API so it can determine the correct DB for access.

Title: Change database based on request origin

Body:
Now I am wondering whether it is a good practice to change the database connection string in the Web API application based on the request origin?

It depends upon what you mean by "based upon the request origin."  If you're referring to something that the client controls, such as an IP address, then what you're considering is likely a bad idea.  Things controlled by the client are easily spoofed and are effectively an invitation for mischief.

On the other hand, if you're referring to something that isn't easily spoofed, such as a client authentication token, then what you're considering is fine.

I can't tell which architecture you're planning on using.

Option A

          +->Application
          |
 Web API+->
          |
          +->DB

Option B:

Application+->Web API+->DB

In either case, the general concept that you're grappling with right now is termed "authorization."

Authentication is a related concept, but refers to the client proving they are who they say they are.  Typically authentication is done with passwords, certificates, tokens, etc....

Authorization is related to providing access to functionality based upon who the client authenticated as.  Determining database connection strings based upon client authorization is a fairly typical case for authorization.

If you happen to be using Option A, then the Web API will determine DB and application access based upon the client's authorization privileges.  And if you happen to be using Option B, then you'll need to make sure that the application passes along the authentication token into the Web API so it can determine the correct DB for access.


问:
Organizing Git repositories with common nested sub-modules
I'm a big fan of Git sub-modules.  I like to be able to track a dependency along with its version, so that you can roll-back to a previous version of your project and have the corresponding version of the dependency to build safely and cleanly.  Moreover, it's easier to release our libraries as open source projects as the history for libraries is separate from that of the applications that depend on them (and which are not going to be open sourced).

I'm setting up workflow for multiple projects at work, and I was wondering how it would be if we took this approach a bit of an extreme instead of having a single monolithic project.  I quickly realized there is a potential can of worms in really using sub-modules.

Supposing a pair of applications: studio and player, and dependent libraries core, graph and network, where dependencies are as follows:

core is standalone

graph depends on core (sub-module at ./libs/core)

network depdends on core (sub-module at ./libs/core)

studio depends on graph and network (sub-modules at ./libs/graph and ./libs/network)

player depends on graph and network (sub-modules at ./libs/graph and ./libs/network)

Suppose that we're using CMake and that each of these projects has unit tests and all the works.  Each project (including studio and player) must be able to be compiled standalone to perform code metrics, unit testing, etc.

The thing is, a recursive git submodule fetch, then you get the following directory structure:

```<code>studio/
studio/libs/                    (sub-module depth: 1)
studio/libs/graph/
studio/libs/graph/libs/         (sub-module depth: 2)
studio/libs/graph/libs/core/
studio/libs/network/
studio/libs/network/libs/       (sub-module depth: 2)
studio/libs/network/libs/core/
</code>```

Notice that core is cloned twice in the studio project.  Aside from this wasting disk space, I have a build system problem because I'm building core twice and I potentially get two different versions of core.

Question

How do I organize sub-modules so that I get the versioned dependency and standalone build without getting multiple copies of common nested sub-modules?

Possible solution

If the the library dependency is somewhat of a suggestion (i.e. in a "known to work with version X" or "only version X is officially supported" fashion) and potential dependent applications or libraries are responsible for building with whatever version they like, then I could imagine the following scenario:

Have the build system for graph and network tell them where to find core (e.g. via a compiler include path).  Define two build targets, "standalone" and "dependency", where "standalone" is based on "dependency" and adds the include path to point to the local core sub-module.

Introduce an extra dependency: studio on core.  Then, studio builds core, sets the include path to its own copy of the core sub-module, then builds graph and network in "dependency" mode.

The resulting folder structure looks like:

```<code>studio/
studio/libs/                    (sub-module depth: 1)
studio/libs/core/
studio/libs/graph/
studio/libs/graph/libs/         (empty folder, sub-modules not fetched)
studio/libs/network/
studio/libs/network/libs/       (empty folder, sub-modules not fetched)
</code>```

However, this requires some build system magic (I'm pretty confident this can be done with CMake) and a bit of manual work on the part of version updates (updating graph might also require updating core and network to get a compatible version of core in all projects).

Any thoughts on this?

答:
Facing exactly the same problem here. One of solutions could be to have some repo libs that would hold core, network, graph as submodules and just CMakeLists that would tell each of the libs where to find its dependencies.
Each application now would have libs as submodule and use only necessary libs.

Testing of each lib could be setup in 2 ways:

Have core_testing, graph_testing, network_testing as separate applications

Deploy tested libs to test-servers and find them while running tests using cmake


问:
Job Scheduling, Stored Procedures
I am working on a polyglot web app project that has functionalities that can be separated, but that rely on each other. I am unfamiliar with the following (what key words to even search for, what concepts are in play):

I have a few different sources of disparate data that are separately created/ingested into a data store. the data that is needed to back the actual web app requires clean-up and curation of the different types of data.

As a concrete example, perhaps you have a large book/article datastore and a user datastore. You want to curate and push data from both to a graph store for recommender purposes, and a paired down version of the book store to actually serve the web app and search functionality.

How do you usually go about this? It's not fast changing data, so I wouldn't think a full event-based reactive system is quite necessary. Is the concept to somehow schedule jobs at a specified period?

答:
A key concept/search term may be ETL (Extract, transform, load). That's a formalization of the process you describe: grab data from all over the place, clean it up, and then deliver it. You're in luck, there are both a number of existing tools you may be able to use and a lot written on the topic.

I'm most familiar with Pentaho (Kettle, actually). Don't take this as a recommendation, but more as an example of what can be done...

With Kettle, I pull in loads of data from many sources: files, databases, and even web services. If your data source isn't supported out of the box, you can likely find a plugin that works. Then, I transform the data into an appropriate shape, clean it up, and drop it somewhere else. Again, if your destination isn't supported out of the box, you can likely find a plugin to help. (Actually, writing your own plugin isn't insurmountable.) Best of all, I can schedule all of these processes in any idiosyncratic fashion I see fit.

Be sure to evaluate a bunch of tools to decide what works best for your team.

With regards to best practices, there's much written, but most is technology-specific. The big vendor databases have their own approach. Open source tools have their approach. You don't mention the tech you're using, but you'll likely find more detail by searching for ETL as related to that tech. Generically, best practice recommendations tend to be sort of obvious: know your requirements, logging, that sort of thing. Maybe post secondary questions as you discover details.

Title: Job Scheduling, Stored Procedures


答:
Your assumption that global variables are used only for state is incorrect. In (probably) most interpreted languages, for example, namespaces are used to communicate interfaces. Since in many of these languages interfaces (enumerations, functions, classes) are just variables, then without global variables there will be no global interfaces.

In addition, some of the programmers out there work in constrained environment such as embedded systems and real real-time applications. In those environments where efficiency is above all, one tends to avoid deep stacks and heavy heaps. In those environments, communicating over global variables is often the most efficient option.

Note that from the theoretical standpoint there cannot be a program without some sort of global variables. After all, any program has to generate some output, and the output device is a global handle. Same goes for all other system interfaces such as the time, environment variables, etc. Having access to these variables only through getters and setters doesn't make them less global, only less visible.

A programming language is not supposed to inflict algorithmic censorship. It is a tool, and it is up to the programmer to use this tool in a right way.

Title: Rationale for modern languages implement changeable global variables

Body:
Common sense seems to be that usage of global variables is bad, in particular if they are used as real variables (i.e. changing state; e.g. Why is Global State so Evil?).

How does it come then, that virtually any language I've used so far implements them nonetheless, especially the modern fourth generation (4GL) languages like python or perl?

Do there exist procedural languages without global variables. If not, would it be possible to create one completely without the concept of global rewritable state?


问:
How to find the kth minimum element in an array
I am looking for an efficient algorithm to find the k'th minimum element in an unsorted array of n elements, where 1 <= k <= n. The obvious thing is to sort the array first, then pick the k'th element, which will result in a running time of O(n * log(n))  But I guess it can be done in a more efficient way, since sorting the array seems to do "too much". For example, for k=1 the task is to find the minimum of the array, which can be done in O(n).

Does anyone know  a better algorithm for k>=2?

答:
Is the Array sorted?
If yes then google for the "Median of Medians" or a "Binarysearch" or an "Interpolation-search" there are way more than these, if it is not sorted,you need to sort it first.  </p>


问:
Scala, checking for type and then cast to it
I have sometimes the usecase that I need to check if a variable is of a specific type and if so cast to it to access a property/function of it.

Example:

```<code>    if (state.isInstanceOf[StateA] && state.asInstanceOf[StateA].isFlagBlaBla)
</code>```

How can this be improved ? I could use match/case but is there a better option (shorter) ?

答:
The shorter way is to architect your application so your variable already has .isFlagBlaBla, either by making state already an instance of StateA, or by making .isFlagBlaBla a method of the base class you're using.  This is basic SOLID OOP design principles.  If you post more context in another question, we could help you refactor.

Aside from that, there's really no way to shorten this, other than extracting it into a function.  Functional languages let you simplify almost anything by extracting it into a function.  You can always write an abomination like the following:

  class BaseState() {
    import scala.reflect.{ClassTag, classTag}

    def castAndTest[A: ClassTag](f: A => Boolean): Boolean =
      if (classTag[A].runtimeClass.isInstance(this)) f(this.asInstanceOf[A])
      else false
  }

  class StateA() extends BaseState {
    def isFlagBlaBla = true
  }

  def main(args: Array[String]) {
    val baseState: BaseState = new BaseState()
    val stateA:    BaseState = new StateA()

    baseState castAndTest[StateA] {_.isFlagBlaBla} // returns false
    stateA    castAndTest[StateA] {_.isFlagBlaBla} // returns true
  }
```

Type casts are extremely rare in languages with type systems as powerful as Scala's.  I've literally never had to do one in Scala, and didn't even remember the syntax.  Even in languages with medium-strength type systems, they should be heavily scrutinized.

Body:
The shorter way is to architect your application so your variable already has .isFlagBlaBla, either by making state already an instance of StateA, or by making .isFlagBlaBla a method of the base class you're using.  This is basic SOLID OOP design principles.  If you post more context in another question, we could help you refactor.

Aside from that, there's really no way to shorten this, other than extracting it into a function.  Functional languages let you simplify almost anything by extracting it into a function.  You can always write an abomination like the following:

  class BaseState() {
    import scala.reflect.{ClassTag, classTag}

    def castAndTest[A: ClassTag](f: A => Boolean): Boolean =
      if (classTag[A].runtimeClass.isInstance(this)) f(this.asInstanceOf[A])
      else false
  }

  class StateA() extends BaseState {
    def isFlagBlaBla = true
  }

  def main(args: Array[String]) {
    val baseState: BaseState = new BaseState()
    val stateA:    BaseState = new StateA()

    baseState castAndTest[StateA] {_.isFlagBlaBla} // returns false
    stateA    castAndTest[StateA] {_.isFlagBlaBla} // returns true
  }

Type casts are extremely rare in languages with type systems as powerful as Scala's.  I've literally never had to do one in Scala, and didn't even remember the syntax.  Even in languages with medium-strength type systems, they should be heavily scrutinized.


问:
Efficient way to sort large set of numbers
I have to sort a set of 100000 integers as a part of a programming Q. The time limit is pretty restrictive, so I have to use the most time-efficient approach possible.

My current code -

```<code>#include<cstdio>
#include<algorithm>
using namespace std;

int main() {
    int n,d[100000],i;
    for(i=0;i<n;++i) {
                     scanf("%d",&d[i]);
    }
    sort(d,d+n);
    ....
}
</code>```

Would this approach be more efiicient?

```<code>int main() {
    int n,d[100000],i;
    for(i=0;i<n;++i) {
                     scanf("%d",&d[i]);
                     sort(d,d+i+1);
    }
    ....
}
</code>```

What is the most efficient way to sort a large dataset?

Note - Not homework...

答:
If you want to sort time-efficient and it is guaranteed that your elements are numbers, then go for Radix-Sort. It is sorting your Dataset in O(a*n), a is the length of the longest/biggest number.
10 would get you a=2, 200 would get you a=3, 2*10^(100) would get you a=1000 and so on.

Quicksort can sort in O(log(n)*n) what is good, but can suck and be as bad as Bubblesort and sort your Dataset in Theta(n²).

Can you tell us how much time you have to sort it, or how much time you have in general?


问:
Best strategy for reporting progress to the UI - how should the callback happen?
Sometimes the user starts an extended technical operation that takes a while to execute. In these cases, it's usually nice to display some kind of progress bar, along with information about which task is being executed right now.

In order to avoid close-coupling the UI and the logic layers, it's usually best to have the communication occur via some kind of proxy. That is, the back-end should not be manipulating its own UI elements, or even interacting with the intermediary layer directly.

Obviously, there has to be some call-back somewhere to make this work. I've generally implemented it in one of two ways:

Pass a mutable object to the back-end, and have the back-end make changes to it on progress. The object notifies the front-end when a change occurs.

Pass a call-back function of the form void f(ProgressObject) or ProgressObject -> unit that the back-end invokes. In this case, the back-end constructs the ProgressObject and it's completely passive. It must construct a new object every time it wants to report progress, I assume.

What are the drawbacks and advantages of these methods? Is there an agreed-upon best method to use? Are there different circumstances for their use?

Are there completely different techniques of reporting progress that I've overlooked?

答:
Pass a mutable object to the back-end, and have the back-end make
  changes to it on progress. The object notifies the front-end when a
  change occurs.

It's difficult to balance efficiency if the backend notifies in this respect. Without care you might find that incrementing your progress ends up doubling or tripling the time it takes to complete the operation if you're aiming for a very smooth progress update.

Pass a call-back function of the form void f(ProgressObject) or
  ProgressObject -> unit that the back-end invokes. In this case, the
  back-end constructs the ProgressObject and it's completely passive. It
  must construct a new object every time it wants to report progress, I
  assume.

I don't get the difference here so much.

Are there completely different techniques of reporting progress that
  I've overlooked?

Poll from the front-end in a separate thread with atomic increments in the backend. Polling makes sense here since it's for an operation that finishes in a finite period and the likelihood of the frontend picking up state changes is high, especially if you're aiming for a silky smooth progress bar. You could consider condition variables if you don't like the idea of polling from the frontend thread, but in that case you might want to avoid notifying on every single granular progress bar increment.


问:
Domain driven design - Entity is updateable in certain circumstances
Here is my business rule:

Anyone (anonymous users) can make an Application (as in to apply for something). Applications can be updated until they are reviewed and approved by an officer. Approved applications cannot be updated.

Therefore I created an abstract class

```<code>abstract class Application {
    //various fields of application form
    //fields do not have setters, but they have getters

    public enum State {APPROVED, UNAPPROVED};

    public abstract State getState();
}
</code>```

And since an Application can be in two states, I modeled them like:

```<code>public class ApprovedApplication extends Application{
    //an approved application can only be created from an unapproved application
    public ApprovedApplication(UnapprovedApplication ua) {
        //create it
    }

    public State getState() {
        State.APPROVED;
    }
}
</code>```

and

```<code>public class UnapprovedApplication extends Application{
    //only unapproved applications can be updated
    public void update(ApplicationDTO applicationDTO) {
        //do the updating
    }

    public State getState() {
        State.UNAPPROVED;
    }
}
</code>```

Am I on the right track here or is this complete garbage? And what about Officer approving the application? Getting a bit lost here.

```<code>UnapprovedApplication.approve(Officer o);
</code>```

or

```<code>Officer.approve(UnapprovedApplication ua); ?
</code>```

答:
Am I on the right track here or is this complete garbage?

Upon approval of an application, does the nature or the content of the application change?

If the nature of the application changes, (meaning, you can do vastly different things with it once it has been approved,) then you might be on the right track, though it will still be difficult to implement, because you will still need to create a new application object, (since it will now have to be of a different class,) copy the contents of the old (not-yet-approved) object to the new one, and update any references that already exist throughout your system, which were referring to the old object, to now refer to the new object.  That's an awful lot of work to do, so you better have some good reasons to do it.

If the nature of the application does not change upon approval, then modelling simply the state of an object by introducing a new subclass is, I am afraid, to use your own words, complete garbage.

Since you have already come up with an enum describing the state of the application, I would think that you should keep just one class and simply change its state from not-yet-approved to approved, and an approved application object will be preventing its content from being altered.  So, the getState() getter would be returning the value of some private State state; field.

And what about Officer approving the application?

(Well, what about them?)


问:
Should I be using both AngularJS and ASP.NET MVC?
I started learning AngularJS and ASP.NET MVC, but am not sure why to use them both together in the same project?

Aren't they both MVC frameworks? Should I be using them both in the same application? Isn't it one or the other?

答:
ASP.NET MVC is a server-side framework; it doesn't care what JavaScript libraries do you use. AngularJS is a client-side library, which doesn't care what server-side technology powers the website—it can be Python, ASP.NET MVC, or even the plain old bunch of static HTML files stored directly on disk.

ASP.NET MVC and AngularJS are both compatible, and there are plenty of projects out there which use them together.

Do you need to use both? Not really. It depends entirely on the needs of your project.

If your website is dynamic, you have to use some server-side scripting. You could use ASP.NET, and the fact that you are using AngularJS won't make your server-side code more MVC. If you want to be able to structure your server-side application as MVC, you'll rather pick ASP.NET MVC.

Similarly, the fact that you are using ASP.NET MVC doesn't imply anything about the structure of your client-side code. You can put all your JavaScript in a single file without any thought about the structure and be happy with that (until the project starts to grow). Here again, AngularJS presents itself as a way to structure your application on client-side.

Side note: you tagged your question ASP.NET MVC 3. Unless this is a legacy project you have to maintain or you have specific constraints when hosting the application on legacy servers which weren't updated for the last four years, you could use ASP.NET MVC 4 or 5 instead.


问:
Does immutability hurt performance in JavaScript?
There seems to be a recent trend in JavaScript towards treating data structures as immutable. For example, if you need to change a single property of an object, better to just create a whole new object with the new property, and just copy over all the other properties from the old object, and let the old object be garbage collected. (That's my understanding anyway.)

My initial reaction is, that sounds like it would be bad for performance.

But then libraries like Immutable.js and Redux.js are written by smarter people than me, and seem to have a strong concern for performance, so it makes me wonder if my understanding of garbage (and its performance impact) is wrong.

Are there performance benefits to immutability I'm missing, and do they outweigh the downsides of creating so much garbage?

答:
Late-comer to this Q&A with already great answers, but I wanted to intrude as a foreigner used to looking at things from the lower-level standpoint of bits and bytes in memory.

I'm very excited by immutable designs, even coming from a C perspective, and from the perspective of finding new ways to effectively program this beastly hardware we have these days.

Slower/Faster

As to the question of whether it makes things slower, a robotic answer would be yes. At this kind of very technical conceptual level, immutability could only makes things slower. Hardware does best when it's not sporadically allocating memory and can just modify existing memory instead (why we have concepts like temporal locality).

Yet a practical answer is maybe. Performance is still largely a productivity metric in any non-trivial codebase. We typically don't find horrific-to-maintain codebases tripping over race conditions as being the most efficient, even if we disregard the bugs. Efficiency is often a function of elegance and simplicity. The peak of micro-optimizations can somewhat conflict, but those are usually reserved for the smallest and most critical sections of code.

Transforming Immutable Bits and Bytes

Coming from the low-level standpoint, if we x-ray concepts like objects and strings and so forth, at the heart of it is just bits and bytes in various forms of memory with different speed/size characteristics (speed and size of memory hardware typically being mutually exclusive).

The memory hierarchy of the computer likes it when we repeatedly access the same chunk of memory, such as in the above diagram, since it'll keep that frequently-accessed chunk of memory in the fastest form of memory (L1 cache, e.g., which is almost as fast as a register). We might repeatedly access the exact same memory (reusing it multiple times) or repeatedly access different sections of the chunk (ex: looping through the elements in a contiguous chunk which repeatedly accesses various sections of that chunk of memory).

We end up throwing a wrench in that process if modifying this memory ends up wanting to create a whole new memory block on the side, like so:

... in this case, accessing the new memory block could require compulsory page faults and cache misses to move it back into the fastest forms of memory (all the way into a register). That can be a real performance killer.

There are ways to mitigate this, however, using a reserve pool of preallocated memory, already touched.

Big Aggregates

Another conceptual issue that arises from a slightly higher-level view is simply doing unnecessary copies of really big aggregates in bulk.

To avoid an overly complex diagram, let's imagine this simple memory block was somehow expensive (maybe UTF-32 characters on an unbelievably limited hardware).

In this case, if we wanted to replace "HELP" with "KILL" and this memory block was immutable, we would have to create a whole new block in its entirety to make a unique new object, even though only portions of it have changed:

Stretching our imagination quite a bit, this kind of deep copy of everything else just to make one little part unique might be quite expensive (in real-world cases, this memory block would be much, much bigger to pose a problem).

However, in spite of such an expense, this kind of design will tend to be far less prone to human error. Anyone who has worked in a functional language with pure functions can probably appreciate this, and especially in multithreaded cases where we can multithread such code without a care in the world. In general, human programmers tend to trip over state changes, especially ones that cause external side effects to states outside of a current function's scope. Even recovering from an external error (exception) in such a case can be incredibly difficult with mutable external state changes in the mix.

One way to mitigate this redundant copying work is to make these memory blocks into a collection of pointers (or references) to characters, like so:

Apologies, I failed to realize we don't need to make L unique while making the diagram.

Blue indicates shallow copied data.

... unfortunately, this would get incredibly expensive to pay a pointer/reference cost per character. Furthermore, we might scatter the contents of the characters all over the address space and end up paying for it in the form of a boatload of page faults and cache misses, easily making this solution even worse than copying the entire thing in its entirety.

Even if we were careful to allocate these characters contiguously, say the machine can load 8 characters and 8 pointers to a character into a cache line. We end up loading memory like this to traverse the new string:

In this case, we end up requiring 7 different cache lines worth of contiguous memory to be loaded to traverse this string, when ideally we only need 3.

Chunk Up The Data

To mitigate the issue above, we can apply the same basic strategy but at a coarser level of 8 characters, e.g.

The result requires 4 cache lines worth of data (1 for the 3 pointers, and 3 for the characters) to be loaded to traverse this string which is only 1 short of the theoretical optimum.

So that's not bad at all. There's some memory waste but memory is plentiful and using up more doesn't slow things down if the extra memory is just going to be cold data not frequently accessed. It's only for the hot, contiguous data where reduced memory use and speed often go hand-in-hand where we want to fit more memory into a single page or cache line and access it all prior to eviction. This representation is pretty cache-friendly.

Speed

So utilizing a representation like the above can give quite a decent balance of performance. Probably the most performance-critical uses of immutable data structures will take on this nature of modifying chunky pieces of data and making them unique in the process, while shallow copying unmodified pieces. It does also imply some overhead of atomic operations to reference the shallow copied pieces safely in a multithreaded context (possibly with some atomic reference-counting going on).

Yet as long as these chunky pieces of data are represented at a coarse enough level, a lot of this overhead diminishes and is possibly even trivialized, while still giving us the safety and ease of coding and multithreading more functions in a pure form without external side effects.

Keeping New and Old Data

Where I see immutability as potentially most helpful from a performance standpoint (in a practical sense) is when we can be tempted to make whole copies of large data in order to make it unique in a mutable context where the goal is to produce something new from something that already exists in a way where we want to keep both new and old, when we could just be making little bits and pieces of it unique with a careful immutable design.

Example: Undo System

An example of this is an undo system. We might change a small portion of a data structure and want to keep both the original form we can undo towards, and the new form. With this kind of immutable design that only makes small, modified sections of the data structure unique, we can simply store a copy of the old data in an undo entry while only paying the memory cost of the added unique portions data. This provides a very effective balance of productivity (making the implementation of an undo system a piece of cake) and performance.

High-Level Interfaces

Yet something awkward arises with the above case. In a local kind of function context, mutable data is often the easiest and most straightforward to modify. After all, the easiest way to modify an array is often to just loop through it and modify one element at a time. We can end up increasing the intellectual overhead if we had a large number of high-level algorithms to choose from to transform an array and had to pick the appropriate one to ensure that all these chunky shallow copies are made while the parts that are modified are made unique.

Probably the easiest way in those cases is to use mutable buffers locally inside the context of a function (where they typically don't trip us up) which commit changes atomically to the data structure to get a new immutable copy (I believe some languages call these "transients")...

... or we might simply model higher and higher-level transform functions over the data so that we can hide the process of modifying a mutable buffer and committing it to the structure without mutable logic involved. In any case, this is not a widely-explored territory yet, and we have our work cut out if we embrace immutable designs more to come up with meaningful interfaces for how to transform these data structures.

Data Structures

Another thing that arises here is that immutability used in a performance-critical context will probably want data structures to break down to chunky data where the chunks aren't too small in size but also not too big.

Linked lists might want to change quite a bit to accommodate this and turn into unrolled lists. Large, contiguous arrays might turn into an array of pointers into contiguous chunks with modulo indexing for random access.

It potentially changes the way we look at data structures in an interesting way, while pushing the modifying functions of these data structures to resemble a bulkier nature to hide the extra complexity in shallow copying some bits here and making other bits unique there.

Performance

Anyway, this is my little lower-level view on the topic. Theoretically, immutability can have a cost ranging from very large to smaller. But a very theoretical approach doesn't always make applications go fast. It might make them scalable, but real-world speed often requires embracing the more practical mindset.

From a practical perspective, qualities like performance, maintainability and safety tend to turn into one big blur, especially for a very large codebase. While performance in some absolute sense is degraded with immutability, it's hard to argue the benefits it has on productivity and safety (including thread-safety). With an increase to these can often come an increase to practical performance, if only because the developers have more time to tune and optimize their code without being swarmed by bugs.

So I think from this practical sense, immutable data structures might actually aid performance in a lot of cases, as odd as it sounds. An ideal world might seek a mixture of these two: immutable data structures and mutable ones, with the mutable ones typically being very safe to use in a very local scope (ex: local to a function), while the immutable ones can avoid external side effects outright and turn all changes to a data structure into an atomic operation producing a new version with no risk of race conditions.


问:
Can profiling be used to verify if optimization was successful?
I know that profiling is useful to identify bottlenecks and determining what parts of the code require how much time to execute. The latter isn't always very easy to track in the midst of other paths being executed, so once I decide what I want to optimize it might be problematic to see the improvement in numbers. This is especially true in desktop apps which run constantly and it is difficult to: execute the same path and execute it the same number of times to have reliable comparison.

It won't help me if before optimization the function ran X times and took 500 milliseconds, and after optimization it run Y times and took 400 milliseconds.

In such cases, can I somehow use a profiler to determine improvement or do I have to resolve to other options?

答:
In such cases, can I somehow use a profiler to determine improvement [...]?

Of course. The timings you get not only help to determine improvement but you can see how hotspots move around in the process, getting very informed feedback of how your changes are affecting critical execution paths as well as things like cache misses.


答:
If you're building a single page application (SPA), then you probably don't need the "MVC" in ASP.NET MVC. Views, especially dynamic views, are likely delivered/manipulated client-side. Angular handles that just fine.

But maybe you don't want a 100% SPA. Then what? Imagine instead 10 pages, but 10 pages that are very dynamic. After a user logs on, there's a little user badge up in the right-hand corner. That's not dynamic. It just shows a few nifty things like the user's "score" and their latest selfie. You cache the nifty things so they can be easily retrieved. Now, you can go two ways with this. If you're a client-side MVC purist, you just fetch the badge data after the initial HTML payload is delivered, just like all the other data. But maybe you're not a purist. Maybe you're the opposite of a purist. Maybe you're an impurist. So, instead of delivering the initial HTML, delivering some JavaScript that will post back to your server, post via JavaScript to grab badge data, and then ultimately merge that data into a view via client-side MVC, you simply decide to merge the data already in your cache into a view on the server and then deliver that as your initial HTML. After your initial HTML is delivered, you proceed with your typical client-side MVC antics.

So... MVC on the server and on the client is just a convenient way to organize code that used to be a mess in 2001. You don't have to choose one or the other. You can choose both. Granted, the more you do after that initial HTML is delivered, the less you need server-side MVC. Still, it's there for you if you need it. For example, I worked on a ASP.NET MVC/Angular application where external Angular templates might actually be .NET MVC ActionResult. That means your server controller could merge data into its view, deliver it to Angular as a template, and Angular's controller could then merge its data into the view. I'm not saying this is a good idea, but it just shows that one form of MVC doesn't make the other obsolete.

Besides, no matter how you deploy Angular, you're going to need a way to deliver that initial HTML, the templates, and most importantly the data. Why not use a platform that makes it easy? There are many, but .NET MVC is no slouch. Like I said, you can make the initial HTML and external Angular templates the result of an MVC action, but better yet, you can use .NET'S Web API to deliver the data. Web API is as delicious as apricot compote.

Summarized: MVC is just a pattern. You may want to use that pattern on any number of physical layers. It can't be used up. Use it freely if it makes sense. Besides, Angular may not be MVC anyway (so says people who care about these things), so feel free to use it with a tool that has "MVC" in the name. Hell, even if it is MVC, mix and match as desired.

Title: Should I be using both AngularJS and ASP.NET MVC?

Body:
I started learning AngularJS and ASP.NET MVC, but am not sure why to use them both together in the same project?

Aren't they both MVC frameworks? Should I be using them both in the same application? Isn't it one or the other?


问:
How narrow should my unit tests be?
Here's an example:

I have a chat module in my app, and there's a ChatService class that is responsible for networking, and there's a ChatNotificationService helper class that is responsible for sending out and receiving broadcasts of messages, and there's also a ChatNotificiationDelegate protocol that has methods on it that get called when a notification arrives.

In order to test message sending, in theory I could just call ChatService.sendMessage() and check if the ChatNotificationDelegate.didSentMessage() got called.

However, sending a message calls several different methods before it gets to the delegate. It sends the message to the server, the server then sends a response that triggers sending of the notification, and the notification service then receives the notification and calls the delegate method.

This whole chain can be covered by one unit test, or I could test each piece of functionality separately.

What I'm asking is which approach should I take on this, test every bit we know exactly what went wrong, or to make my tests as general as possible (just the endpoints) to allow more freedom of refactoring the code.

答:
One approach I sometimes use is to write tests against your interface ie.

```<code>SendMessageTest(IChatService service)
{
    service.SendMessage(message);
    Assert.IsTrue(message.Sent);
}
</code>```

Then use data driven testing to call the test with multiple configurations of service. First one with all mocks, this is your unit test, only testing the service itself, then an In process service, so using real messages but all instantiated in process for debugging. Then multiple integration versions using a client version of the service which connects to test, uat and live environments.

each test case should be labeled up as "Integration" or whatever so you can choose which to run under what scenario.

So for CI builds you might only run the unit tests, for debugging you can run the in process ones, after deploying to test you can run the test integration ones and if you have a weird problem in live you can run the live integration ones.

The benefit of this is you only have to write one set of tests but are able to run them in a variety of ways.

PS. you can/should also write further tests for specific logic etc, but I find this 'top level' testing of the most practical use. After all most of the time you know it 'should work' you are trying to find out why it doesn't under circumstance X

Title: How narrow should my unit tests be?

Body:
One approach I sometimes use is to write tests against your interface ie.

SendMessageTest(IChatService service)
{
    service.SendMessage(message);
    Assert.IsTrue(message.Sent);
}

Then use data driven testing to call the test with multiple configurations of service. First one with all mocks, this is your unit test, only testing the service itself, then an In process service, so using real messages but all instantiated in process for debugging. Then multiple integration versions using a client version of the service which connects to test, uat and live environments.

each test case should be labeled up as "Integration" or whatever so you can choose which to run under what scenario.

So for CI builds you might only run the unit tests, for debugging you can run the in process ones, after deploying to test you can run the test integration ones and if you have a weird problem in live you can run the live integration ones.

The benefit of this is you only have to write one set of tests but are able to run them in a variety of ways.

PS. you can/should also write further tests for specific logic etc, but I find this 'top level' testing of the most practical use. After all most of the time you know it 'should work' you are trying to find out why it doesn't under circumstance X


答:
If you are using visual studio there is a new 'single page app' MVC website template which includes angular and MVC Web Api controllers.

This works well because your MVC server-side code provides json endpoints for the angular client side code to call.

Additionally you can use the MVC controllers to serve the basic html view or views for your single page app. this gives you the power of the server side generation, authorization, redirects, error handling etc

Often you will want a small amount of server generated html/javascript even on a single page app, things like endpoint urls for different environments, different languages, or even the odd authentication page which may just be easier to do server side.


答:
The question you should actually be asking yourself is this: is my code testable?  If you find yourself writing elaborate tests or using elaborate mocks in your tests, it is the fault of the code under test, not the tests themselves.

Unit tests test a unit of code.  So it comes down to "what is a unit?"  A unit, in general terms, is one small bit of clearly-defined functionality that is readily testable.  Usually, it is a single method. If you write code that is testable, your proper test "width" should naturally arise from that.

In any case, what you are describing in your question is not a unit test; it is an integration test.  The relative merits of unit tests vs integration tests are discussed at length elsewhere, but suffice it to say that you write unit tests to make sure your code works, but you write integration tests to make sure your code works together.

Title: How narrow should my unit tests be?

Body:
Here's an example:

I have a chat module in my app, and there's a ChatService class that is responsible for networking, and there's a ChatNotificationService helper class that is responsible for sending out and receiving broadcasts of messages, and there's also a ChatNotificiationDelegate protocol that has methods on it that get called when a notification arrives.

In order to test message sending, in theory I could just call ChatService.sendMessage() and check if the ChatNotificationDelegate.didSentMessage() got called.

However, sending a message calls several different methods before it gets to the delegate. It sends the message to the server, the server then sends a response that triggers sending of the notification, and the notification service then receives the notification and calls the delegate method.

This whole chain can be covered by one unit test, or I could test each piece of functionality separately.

What I'm asking is which approach should I take on this, test every bit we know exactly what went wrong, or to make my tests as general as possible (just the endpoints) to allow more freedom of refactoring the code.


问:
Design pattern for "operation on object allowed, only if object is in certain state"
For example:

Only job applications that are not yet in review or approved, can be updated. In other words, a person can update his job appliance form until HR starts reviewing it, or it's already acepted.

So a job application can be in 4 states:

APPLIED(initial state), IN_REVIEW, APPROVED, DECLINED

How do I achieve such behavior?

Surely I can write an update() method in Application class, check
  Application state, and do nothing or throw exception if Application is
  not in required state

But this kind of code does not make it obvious such a rule exists, it allows anyone to call update() method, and only after failing a client knows such operation was not permitted. Therefore client needs to be aware that such an attempt might fail, therefore take caution. Client being aware of such things also means that logic is leaking outside.

I tried creating different classes for each state (ApprovedApplication etc.) and put allowed operations on only allowed classes, but this kind of approach feels wrong too.

Is there an official design pattern, or a simple piece of code, to implement such behavior?

答:
In general, what you are describing is a workflow.  More specifically, business functions that are embodied by states such as REVIEWED APPROVED or DECLINED fall under the heading of "business rules" or "business logic."

But to be clear, business rules should not be encoded into exceptions.  To do so would be to use exceptions for program flow control, and there are many good reasons why you should not do that.  Exceptions should be used for exceptional conditions, and the INVALID state of an application is entirely unexceptional from a business standpoint.

Use exceptions in cases where the program cannot recover from an error condition without user intervention ("file not found," for example).

There is no specific pattern for writing business logic, other than the usual techniques for arranging business data processing systems and writing code to implement your processes.  If the business rules and workflow are elaborate, consider using some sort of workflow server or business rules engine.

In any case, the states REVIEW, APPROVED, DECLINED, etc. can be represented by an enum type private variable in your class.  If you use getter/setter methods, you can control whether or not the setters will allow changes by first examining the value of the enum variable. If someone tries to write to a setter when the enum value is in the wrong state, then you can throw an exception.


问:
Should I be using XML to store configuration values in a database?
There are a couple of answers on SE comparing  single-row table vs. name-value-pair table used for config settings, but I haven't seen anyone arguing for using  XML (or JSON) as a name-value pair model.

Option A

```<code>UserID     AppID     ParamID    Value
1          1         10         5.40
1          1         11         John Smith
2          1         10         4.40
2          1         11         Jane Smith
</code>```

Option B

```<code>UserID     AppID     Settings
1          1         <params><param><ID>10</ID><value>5.40</value></param><param><ID>11</ID><value>John Smith</value></param></params>
2          1         <params><param><ID>10</ID><value>4.40</value></param><param><ID>11</ID><value>Jane Smith</value></param></params>
</code>```

Option A is plain key-value pair solution, and Option B uses only one serialized XML settings per user/app.

Is there any value in using XML instead of a plain key-value pair?

答:
I'll refer to option A as the "structured" approach, i.e. the one where the structure of the data is known to and understood by the database, and option B as the "blob" approach, where the data is just an opaque blob that the database knows nothing about.

The main things to consider are:

Storing this data in a structured way allows you to perform queries on it directly. Do you expect to have a use for queries like SELECT UserId WHERE ParamID = 10 AND Value > 5 at any point in the future?

The "structured" approach forces your data to fit into this particular relational database table. If this data was in "blob" form, then it would be entirely up to you what shape the data takes. In the case of XML blobs, you're imposing a tree-like shape on your data, which is significantly less restrictive than rows in a single relational table.

If you store the data in blob form, then the structure of your data blob is not dictated by the database schema. This should mean it has a separate schema (in your case, an .xsd file somewhere). That means you can change the blob's structure without migrating the whole database to a new schema, but it does mean more schemas/versions to keep track of.

So which one you use depends on how much you'll want to write queries that can "see inside" this data, how likely it is that you'll need the flexibility of the more freeform structure, and whether having two schemas to worry about is a net benefit. Where I work, some of our data is structured, and some of it is "blobbed". It depends.