问:
Do licenses matter if there's nobody around to enforce them?
Suppose that the original creators can't (or won't) enforce a license on their software/code, but that work is still popular.

I guess if you want to visualize it, I'll throw out a convoluted hypothetical:

Imagine a very small group of developers that released a code project under an open-source license. The repository was hosted on their servers. However, everybody on the immediate development team passed away in a tragic accident or something. Their servers shut down after this happened. The project had a fairly large user base, and so others began to host the last revision on their own servers for others to download.

(Yes, I have an active imagination)

Does abiding by the license simply become a matter of morality by its users, or can there still exist a legal penalty when there is no one user or group to enforce it? Could anything be done if an unscrupulous user decided to branch off the project and use it under a different license?

I am not looking for legal advice -- I am simply curious about how software licenses work. I tend to think of strange situations and wonder what would happen in those scenarios.

答:
Do laws matter if there is no police around to enforce them?

I think copyright is a complex matter and varies from country to country. You can pretend it's not there, but like any other offense it eventually might haunt you.


问:
How can I organise a better deployment process?
I work in a company of about 10 developers. When we start a project, each of the developers involved in the project have their dev environment. Once they complete a task, it is then pushed to phase two which is called "Staging", where QA takes place. Once bugs are cleared, it is then set to "Ready for UAT" and UAT (User Acceptance Testing) is done on the same phase (Staging). If the client is happy with the work it is pushed to the last stage which is called "Live".

Detailed development overflow

Specification

Development (Once specification is approved by client)

Staging QA

Staging UAT

Go Live Deployment

Stage of Support (1 month of free support, this is to give the client a change so point out things we/they didn't notice on the QA and UAT stage)

Once the stage of support ends and the client wants to change something to the software, they need to rise a CR (Change Request), and it all start's from stage one again (Big change requests, things such as new functionality to the current system)

If the change request is small then no specification is needed. It will start from development stage to go Live deployment. If the client spots something wrong after the go live for this CR, then he will have to rise a support case and it starts all over again from the development stage.

My question is, could there be a better way of doing this?

答:
I'd suggest continuous integration of any useful incremental work from any developer in a single integration branch, with automated and manual QA done as often as possible on the integration branch. Everybody is on the same page, no room for "wandering off" or wasting effort and resources polishing changes (in individual dev environments) which may be completely invalidated when integrating with other similarly polished code from other individual dev environments.

In such environment you can apply in a much more effective manner other useful software development strategies like Agile, TDD, etc.

For example, applying Agile:

steps 1-4 all happen on the same/main branch, simultaneously. The specifications are broken down in many small pieces which can be developed, QA'd, UAT'd, and delivered incrementally (in max a few days). The client can see the incremental results early, incrementally adjust the small specs and re-prioritize them as needed. By working closer with the client in steps 1-4 QA and UAT can really become one, which is ideal IMHO. TDD can also be thrown in the mix here.

Steps 5-6 are done on short-lived (1-4 weeks, the shorter the better) versions (ideally just selected CI/CD labels/tags on the main branch), mostly producing new incremental specs for subsequent such versions (instead of producing support requests for longer life versions each in their own branches), until the final client-satisfying product is achieved (much faster than through the repeated longer iterations you have now).

Steps 5-6 for one version are done in parallel with steps 1-4 for the next version.

The team's focus and resources are mostly directed towards reaching the final product version the customer desires instead of "wandering off" supporting non-final versions, which can easily lead to exploding costs.

ContentLicense: CC BY-SA 3.0


问:
Differences between XML and JSON for data exchange
I'm wondering about the differences between XML and JSON.

Without any regard to existing processing tools and backend systems and merely as a means of representing data is there anything JSON does better than XML or vice versa? Or can they both be used to the same extent ?

I.e. is there anything that could be represented by JSON  that XML could not represent or vice versa?

答:
XML and JSON are both capable of transmitting the same data, but which is better depends mostly on what you want to do with it.

This does touch on existing tooling, but you're not likely to be hand rolling parsers for either, so it is relevant.

XML

Has better tooling for verifying schema.

Has built in support for namespaces.

Can be more easily restructured into HTML.

Can be queried with XPath, which is really exceptional query language - think SQL for XML. I haven't really seen an equivalent for JSON - but I'd love to hear about it if I'm wrong on this one.

JSON

Has significantly lower ceremony (and, at least in my experience, better pretty printers), so when a human has to look at it, it can be easier to read.

Is, unsurprisingly, the best format when one end of the transfer is written in JavaScript.

Has less legacy, so the chance of being forced to accept malformed JSON is lower. There is also no cultural expectation that you should have to accept malformed input.

Because one is capable of storing anything that can be stored in the other, usability and tooling is about the only criteria that is useful for comparing what it's like to use them.

If all that were wiped away, I'd probably use neither, and transmit the data as chunks of Lisp.

It's lower ceremony than JSON (barely),  much easier to transform than XML, and easier to write a parser for than either (which I'd have to do if all the tooling were gone).


答:
It's a bit of apples and Oranges. They are both used for the same task yes, but they are distinctively different.

XML is Extensible Markup Language. The main benefit of it is that it can cary meta data. In short:

```<code><tag attribute="something">data</tag>
</code>```

So having attributes on your elements allows you to be more explicit.

More on that you can have predefined schema that enforces your defined rules of the structure. For e.g. certain elements might use certain attributes, but if others try to use it, schema validation would give you an error.

This gives a lot of power. But with great power comes great responsibility. Often these XML's represent structures and relations more than the data it self. Thus it becomes hard to read for a human because of all of the additional information it carries. So for some operations it causes more of a bloat since you want to save 5 lines of data while having 20 or more lines of xml because you need to satisfy the schema.

Wikipedia has quite a good summary of it: https://en.wikipedia.org/wiki/XML

JSON on the other hand is only meant to represent data and nothing more.
You get main types of data:

Integers

Strings

Booleans

Arrays

Dictionaries

Thats it. You can nest them in any manner you want but thats your limitation. You only use those data types. More here: http://www.json.org/

Sure you can move attributes to their own array within the dictionary or so and you could achieve most of the same, and thats why there is this comparison often is mentioned.
But at the core of it these are 2 tools for different uses. Just when you get a hammer everything becomes a nail.

XML is great when you have complex structures, relations between objects, maybe even overrides of one file by another (handled by another answer mentioned XPath).

JSON is great when you need to communicate with small objects that could have their own validation done in code. Also it's human readable and dead simple.

Title: Differences between XML and JSON for data exchange

Body:
I'm wondering about the differences between XML and JSON.

Without any regard to existing processing tools and backend systems and merely as a means of representing data is there anything JSON does better than XML or vice versa? Or can they both be used to the same extent ?

I.e. is there anything that could be represented by JSON  that XML could not represent or vice versa?

Body:
It's a bit of apples and Oranges. They are both used for the same task yes, but they are distinctively different.

XML is Extensible Markup Language. The main benefit of it is that it can cary meta data. In short:

<tag attribute="something">data</tag>

So having attributes on your elements allows you to be more explicit.

More on that you can have predefined schema that enforces your defined rules of the structure. For e.g. certain elements might use certain attributes, but if others try to use it, schema validation would give you an error.

This gives a lot of power. But with great power comes great responsibility. Often these XML's represent structures and relations more than the data it self. Thus it becomes hard to read for a human because of all of the additional information it carries. So for some operations it causes more of a bloat since you want to save 5 lines of data while having 20 or more lines of xml because you need to satisfy the schema.

Wikipedia has quite a good summary of it: https://en.wikipedia.org/wiki/XML

JSON on the other hand is only meant to represent data and nothing more.
You get main types of data:

Integers

Strings

Booleans

Arrays

Dictionaries

Thats it. You can nest them in any manner you want but thats your limitation. You only use those data types. More here: http://www.json.org/

Sure you can move attributes to their own array within the dictionary or so and you could achieve most of the same, and thats why there is this comparison often is mentioned.
But at the core of it these are 2 tools for different uses. Just when you get a hammer everything becomes a nail.

XML is great when you have complex structures, relations between objects, maybe even overrides of one file by another (handled by another answer mentioned XPath).

JSON is great when you need to communicate with small objects that could have their own validation done in code. Also it's human readable and dead simple.


问:
Two structs with the same members but different naming, is it a good idea?
I am writing a program that involves working with both polar and Cartesian coordinates.

Does it make sense to create two different structs for each kind of points, one with X and Y members and one is with R and Theta members.

Or is it too much and it is better to have just one struct with first and second as members.

What I am writing is simple and it won't change much. But I am curious what is better from a design point of view.

I am thinking the first option is better. It seems more readable and I will get the benefit of type checking.

答:
I believe that having the same member names is a bad idea in this case, because it makes the code more error prone.

Imagine the scenario: you have a couple of cartesian points: pntA and pntB. Then you decide, for some reason, that they should better be represented in polar coordinates, and change the declaration and constructor.

Now, if all your operations were just method calls like:

```<code>double distance = pntA.distanceFrom(pntB);
</code>```

then you're fine. But what if you used the members explicitly? Compare

```<code>double leftMargin = abs(pntA.x - pntB.x);
double leftMargin = abs(pntA.first - pntB.first);
</code>```

In the first case, the code will not compile. You'll see the error immediately and will be able to fix it. But if you have the same member names, the error will be only on the logical level, much harder to detect.

If you write in a non-object-oriented language, then it's even easier to pass wrong struct to the function. What's to stop you from writing the following code?

```<code>double distance = calculate_distance_polar(cartesianPointA, polarPointB);
</code>```

Different data types, on the other hand, would allow you to find the error during compilation.

Body:
I believe that having the same member names is a bad idea in this case, because it makes the code more error prone.

Imagine the scenario: you have a couple of cartesian points: pntA and pntB. Then you decide, for some reason, that they should better be represented in polar coordinates, and change the declaration and constructor.

Now, if all your operations were just method calls like:

double distance = pntA.distanceFrom(pntB);

then you're fine. But what if you used the members explicitly? Compare

double leftMargin = abs(pntA.x - pntB.x);
double leftMargin = abs(pntA.first - pntB.first);

In the first case, the code will not compile. You'll see the error immediately and will be able to fix it. But if you have the same member names, the error will be only on the logical level, much harder to detect.

If you write in a non-object-oriented language, then it's even easier to pass wrong struct to the function. What's to stop you from writing the following code?

double distance = calculate_distance_polar(cartesianPointA, polarPointB);

Different data types, on the other hand, would allow you to find the error during compilation.


问:
How do I approach a complicated merge
Here is the deal, I have joined a new company and have been asked to finish off the work on a branch which hasn't been touched for almost a year. In the meanwhile, the master branch has been growing with a steady pace. Ideally I would like to merge all of the changes from the master branch into the feature branch and continue the work from there, but I'm not too sure how to approach this.

How do I perform this merge safely while preserving important changes on both sides of the branch?

答:
Trying to merge the mainline head onto a year stale branch can be an exercise in frustrations and deepening the dent on the desk with your forehead.

The mainline didn't get to where it is in one go over the course of months. It too had development and releases.  Trying to bring it all up to date in one monolithic merge can be overwhelming.

Instead, start out by merging from the first feature merge back into mainline after the stale branch split. Get that merge working.  Then the next feature merge. And so on.  Many of those feature merges will merge in without conflict. It is still important to make sure that the current functionality of stale branch remains compatible with the direction that the mainline has gone.

You may wish to branch from the head of the stale branch for the role of merging in other changes. This is more about making sure that the commits and the history when someone looks back at it is clear and communicates what the role and policy of each branch is.  The stale branch was a feature branch. The one you are working from is an accumulation and reconciliation branch.

Much of this will be easier if the old feature or release branches still exist out there and are easily accessible (some places have a policy of cleaning up the names of branches that are older than some date so that the list of branches isn't overwhelming).

The important thing in all of this is to make sure that you test and fix after successfully merging each part of the mainline history into place. Even though something may merge without conflicts, that just means the code didn't conflict. If the way that the stale feature was accessed was deprecated or removed, there may need to be fixes after the successful merge.

As an aside, this works for other version control systems too.  I've occasionally had need to merge a specific group of svn commits into a branch (cherry picking) for one feature, fix the branch to work with that feature, and then merge the next group of svn commits rather than just doing a wholesale svn merge.

While one can do a git cherry-pick here, and it does allow bringing in specific commits, this has some disadvantages that may complicate the process.  The cherry pick will not show information about the commit you picked from (you can append it to the commit message). This makes actually tracking the commits in the history harder.

Furthermore, it means that you aren't going to effectively replaying the master onto the stale branch - you are going to be picking possibly incomplete features - and those features may be played out of order.

The key reason that one should merge from historical commits to master onto the stale branch is to be able to keep the, lets call it "future history" of the stale branch in a state that you can reason about. You can clearly see the merges from history onto the stale branch and the fixes to reintegrate the functionality.  The features are being added in the same order as they were to master. And when you are done, and finally do the merge from the head of master onto the stale branch, you know that everything has been merged and you aren't missing any commits.

Title: How do I approach a complicated merge


答:
XML is an eXtensible Markup Language.  That's important to understand:  It's designed to be essentially like HTML--a document markup language--but a bit more formalized.  Problem is, document markup isn't what anyone uses it for.  People abuse it for data storage and data exchange, which is a really bad idea because data storage isn't anything like document markup.

A typical HTML document has a pretty high content-to-markup ratio.  It's quite possible to write entire pages without needing to insert a single tag beyond the occasional
, for example.  But in data storage, every data element needs to be defined in some formal way.  With XML, that turns everything into a mess of tag soup very quickly.  For example:

Some people, when presented with a problem, think “I know, I’ll use
  XML.”

<Problem:Worsening> <Problem:TimeDescription>Now</Problem:TimeDescription> <Problem:Posessive>they have</Problem:Posessive> <Problem:Quantity>many, many</Problem:Quantity> <Problem:WorseningDescription>more problems</Problem:WorseningDescription> </Problem:Worsening>

We see that the content can very easily end up completely drowned out by all of the tags. (This is often true even when the XML has been pretty-printed.)

Also, XML requires that every <tag> must be closed by a corresponding </tag>.  It also requires that tags be closed in strict LIFO (ie. Stack) order.  This is redundant: if the only tag that's valid to close is the last one that hasn't been closed yet, every closing tag could theoretically be a simple </> with no loss of semantic information. (It would be harder to follow if you didn't pretty-print it, but who wants to try to read non-pretty-printed XML anyway?)  This makes XML files larger and bulkier than they need to be.

The XML DOM model is also incredibly complex.  Just for starters, you have two distinct ways to represent sub-data on a node: nodes between the opening tag and its closing tag, and Attributes inside the opening tag.  Again, this is an idea that makes a lot of sense in document markup () but a lot less for data storage.  Then you've got all sorts of additional complexities like namespaces, schemas and validation, XSLT, and so on...

The principal difference between XML and JSON is that JSON is just a data storage format.  It's optimized for expressing serialized data, not for document markup, and so it does a much better job of expressing serialized data.  Douglas Crockford calls it href="http://www.json.org/fatfree.html" rel="noreferrer">"the fat-free alternative to XML."

The really interesting thing is, it manages to do this without actually losing any power.  You want schemas and validation?  Use JSON Schema.  Queries? There's JSONPath for that.  (And if, on the other hand, you don't care about those things, you don't need any code for it bloating up your program.)  You want comments and namespaces, or even attributes? {"namespace": "MyNamespace", "attributes": {"attrib1": "value1", "attrib2": "value2"}, "data": "Insert content here", "comment": "It's that simple"}

To answer the original question, there's no data that can be represented in either system that the other can't also do.  It's just a lot smaller and cleaner in JSON, almost every time, because JSON was specifically designed for data storage and XML wasn't.  It's a simple matter of "use the right tool for the job."

Body:
XML is an eXtensible Markup Language.  That's important to understand:  It's designed to be essentially like HTML--a document markup language--but a bit more formalized.  Problem is, document markup isn't what anyone uses it for.  People abuse it for data storage and data exchange, which is a really bad idea because data storage isn't anything like document markup.

A typical HTML document has a pretty high content-to-markup ratio.  It's quite possible to write entire pages without needing to insert a single tag beyond the occasional <p>, for example.  But in data storage, every data element needs to be defined in some formal way.  With XML, that turns everything into a mess of tag soup very quickly.  For example:

Some people, when presented with a problem, think “I know, I’ll use
  XML.”

<Problem:Worsening> <Problem:TimeDescription>Now</Problem:TimeDescription> <Problem:Posessive>they have</Problem:Posessive> <Problem:Quantity>many, many</Problem:Quantity> <Problem:WorseningDescription>more problems</Problem:WorseningDescription> </Problem:Worsening>

We see that the content can very easily end up completely drowned out by all of the tags. (This is often true even when the XML has been pretty-printed.)

Also, XML requires that every <tag> must be closed by a corresponding </tag>.  It also requires that tags be closed in strict LIFO (ie. Stack) order.  This is redundant: if the only tag that's valid to close is the last one that hasn't been closed yet, every closing tag could theoretically be a simple </> with no loss of semantic information. (It would be harder to follow if you didn't pretty-print it, but who wants to try to read non-pretty-printed XML anyway?)  This makes XML files larger and bulkier than they need to be.

The XML DOM model is also incredibly complex.  Just for starters, you have two distinct ways to represent sub-data on a node: nodes between the opening tag and its closing tag, and Attributes inside the opening tag.  Again, this is an idea that makes a lot of sense in document markup (<a href="http://example.com">) but a lot less for data storage.  Then you've got all sorts of additional complexities like namespaces, schemas and validation, XSLT, and so on...

The principal difference between XML and JSON is that JSON is just a data storage format.  It's optimized for expressing serialized data, not for document markup, and so it does a much better job of expressing serialized data.  Douglas Crockford calls it "the fat-free alternative to XML."

The really interesting thing is, it manages to do this without actually losing any power.  You want schemas and validation?  Use JSON Schema.  Queries? There's JSONPath for that.  (And if, on the other hand, you don't care about those things, you don't need any code for it bloating up your program.)  You want comments and namespaces, or even attributes? {"namespace": "MyNamespace", "attributes": {"attrib1": "value1", "attrib2": "value2"}, "data": "Insert content here", "comment": "It's that simple"}

To answer the original question, there's no data that can be represented in either system that the other can't also do.  It's just a lot smaller and cleaner in JSON, almost every time, because JSON was specifically designed for data storage and XML wasn't.  It's a simple matter of "use the right tool for the job."


问:
Is accessing data using a representative value a viable strategy?
Over the years i have seen multiple cases where data is accessed and/or manipulated using a representative value which is internally resolved to the right object/data-field/algorithm.

Some examples:

```<code>// 1. Provide same logger globally using it's name
auto myLogger = LoggingService.getLogger("myLogger")

// 2. Get person object using a unique id
auto person = PersonCatalog.getPerson("12345")

// 3. Get the left child of a B-Tree node
auto lnode = BTreeNode.getNode("left")

// 4. Make player character do something based off a command
playerCharacter.performAction("move_north")
</code>```

I understand, why one would use this approach for the logger, as you don't have to pass around concrete objects to a Logger instance throughout the whole program. I can also see the merit in the second example, as it could provide an interface to a dictionary or a database lookup.

But the third and fourth example in contrast smell of design flaws.

Does this kind of design have a name and is it a viable strategy for more than just a couple of special scenarios?

答:
I agree, the latter two appear to be code smells, and the first one also. But I believe the contrast posed in this question has more to do with choosing the appropriate data structures than with specific patterns. I'll go over each one, starting with the one that seems fine.

The #2 above looks okay (assuming the magic string is just for brevity) and uses the concept of an identifier. It's less a pattern and more of a basic concept used long before computers existed. In this case, the chosen identifier as a number string.

The #1 is a smell because it's unclear what should be passed in to get the right logger. Is "myLogger" an internal name, or a class to instantiate, or a connection string? I can't tell from this call, and I've seen all of those options used before. If I again assume the magic string is for brevity, it could be alright if the variable name and/or a comment clarifies it.

The #3 is a smell because it's using a very fluid and easy-to-get-wrong structure (a string identifier) to represent a very concrete concept. In fact, there are 2 or so correct strings you can pass in and virtually infinite incorrect strings. This is concrete enough that it should ideally be represented by a design-time validated structure like a class. Then when you get a member name wrong, the code won't even compile or will give a syntax error. The structure itself is important enough that the compiler/interpreter shouldn't even run it if incorrect.

The #4 is a smell in a similar way to #3 (no validated structure) and #1 (lack of clarity), but less specific than B-tree code in #3. (Assuming you have reasons for not representing actions using methods/functions, f.ex. client/server.) You could represent this with a pattern or two. You could use messaging or even the GOF command pattern, depending on the specific application. Having messages as concrete classes would both give your inputs a validated structure and give your callers a well-defined means to communicate with you.

In summary, most of these examples highlight the need to choose the appropriate data structure. Using strings to activate code branches is not nearly specific enough for well-known algorithms like b-trees. But it might be okay for a database connection. Choose wisely.


问:
Are there any significant disadvantages to depending upon abstractions?
I was reading this wiki on the Stable Abstractions Principle (SAP).

The SAP states that the more stable a package the more abstract it should be. This implies that if a package is less stable (more likely to change) then it should be more concrete. What I don't really understand is why this should be the case. Surely in all cases regardless of stability we should be depending upon abstractions and hiding the concrete implementation?

答:
I think you're perhaps confused by the word stable chosen by Robert Martin. Here's where I think the confusion starts:

This implies that if a package is less stable (more likely to change) then it should be more concrete.

If you read through to the original article, you'll see (emphasis mine):

The classic definition of the word stability is:”Not easily moved.”
   This is the definition
  that we will be using in this article. That is, stability is not a measure of the likelihood
  that a module will change; rather it is a measure of the difficulty in changing a module.

Clearly, modules that are more difficult to change, are going to be less volatile. The
  harder the module is to change, i.e. the more stable it is, the less volatile it will be.

I have always struggled with the author's choice of the word stable, as I (like you) tend to think of the "likelihood" aspect of stability, i.e., unlikely to change. Difficulty implies that changing that module will break a lot of other modules, and it's going to be a lot of work to fix the code.

Martin also uses the words independent and responsible, which to me convey much more meaning. In his training seminar, he used a metaphor about parents of children growing up, and how they should be "responsible," because their children depend on them. Divorce, unemployment, incarceration, etc. are great real-world examples of the negative impact that change in parents will have on children. Therefore, parents should be "stable" for the benefit of their kids. By the way, this metaphor of children/parents is not necessarily related to inheritance in OOP!

So, following the spirit of "responsible" I came up with alternative meanings for difficult to change (or should not change):

Obligated - meaning other classes depend on this class so it shouldn't change.

Beholden - ibid.

Constrained - the obligations of this class limit its facility in changing.

So, plugging these definitions into the statement

the more stable a package the more abstract it should be

the more obligated a package the more abstract it should be

the more beholden a package the more abstract it should be

the more constrained a package the more abstract it should be

Let's cite the Stable Abstractions Principle (SAP), emphasizing the confusing words stable/unstable:

Packages that are maximally stable should be maximally abstract. Unstable packages should be concrete. The abstractness of a package should be in proportion to its stability.

Clarifying it without these confusing words:

Packages that are maximally beholden to other parts of the system should be maximally abstract. Packages that can change without difficulty should be concrete. The abstractness of a package should be in proportion to how difficult it will be to modify.

TL;DR

The title of your question asks:

Are there any significant disadvantages to depending upon abstractions?

I think if you create the abstractions properly (e.g., they exist because a lot of code depends on them), then there aren't any significant disadvantages.


问:
In general, is it worth using virtual functions to avoid branching?
There seems to be rough equivalents of instructions to equate to the cost of a branch miss virtual functions have a similar tradeoff:

instruction vs. data cache miss

optimization barrier

If you look at something like:

```<code>if (x==1) {
   p->do1();
}
else if (x==2) {
   p->do2();
}
else if (x==3) {
   p->do3();
}
...
</code>```

You could have a member function array, or if many functions depend on the same categorization, or more complex categorization exists, use virtual functions:

```<code>p->do()
</code>```

But, in general, how expensive are virtual functions vs branching
It is hard to test on enough platforms to generalize, so I was wondering if any one had a rough rule of thumb (lovely if it were as simple as 4 ifs is the breakpoint)

In general virtual functions are clearer and I would lean towards them.  But, I have several highly critical sections where I can change code from virtual functions to branches. I would prefer to have thoughts on this before I undertake this. (it's not a trivial change, or easy to test across multiple platforms)

答:
May I just explain why I think this is an XY-problem?
(You're not alone in asking them.)

I assume your real goal is to save time overall, not just to understand a point about cache-misses and virtual functions.

Here's an example of real performance tuning, in real software.

In real software, things get done that, no matter how experienced the programmer is, could be done better.
One doesn't know what they are until the program is written and performance tuning can be done.
There are nearly always more than one way to speed up the program.
After all, to say a program is optimal, you are saying that in the pantheon of possible programs to solve your problem, none of them take less time.
Really?

In the example I linked to, it originally took 2700 microseconds per "job".
A series of six problems were fixed, going counter-clockwise around the pizza.
The first speedup removed 33% of the time.
The second one removed 11%.
But notice, the second one wasn't 11% at time it was found, it was 16%, because the first problem was gone.
Similarly, the third problem was magnified from 7.4% to 13% (almost double) because the first two problems were gone.

At the end, this magnification process allowed all but 3.7 microseconds to be eliminated. That's 0.14% of the original time, or a speedup of 730x.

Removing the initially large problems gives a moderate amount of speedup,
but they pave the way for removing later problems.
These later problems could have initially been insignificant parts of the total, but after early problems are removed, these small ones become large and can produce big speedups.
(It's important to understand that, to get this result, none can be missed, and this post shows how easily they can be.)

Was the final program optimal? Probably not.
None of the speedups had anything to do with cache misses.
Would cache misses matter now? Maybe.

EDIT: I'm getting downvotes from people homing in on the "highly critical sections" of the OP's question. You don't know something is "highly critical" until you know what fraction of time it accounts for. If the average cost of those methods being called is 10 cycles or more, over time, the method of dispatching to them is probably not "critical", compared to what they are actually doing.
I see this over and over, where people treat "needing every nanosecond" as a reason to be penny-wise and pound-foolish.


问:
Why isn't there a python compiler to native machine code?
As I understand, the cause of the speed difference between compiled languages and python is, that the first compiles code all way to the native machine's code, whereas python compiles to python bytecode, to be interpreted by the PVM. I see that this way python codes can be used on multiple operation system (at least in most cases), however I do not understand, why is not there an additional (and optional) compiler for python, which compiles the same way as traditional compilers. This would leave to the programmer to chose, which is more important to them; multiplatform executability or performance on native machine.
In general; why are not there any languages which could be behave both as compiled and interpreted?</p>

答: I think it has more to do with the Python specifics itself, the same reason you can't compile C# to machine code. Language specifics would actually render your programs buggy even if it were possible due to the nature of the language. Why not just learn the C language? Its much easier than C++ and slightly advanced than Python but still approachable.</p>


答:
When anyone asks any question of the form "Is there a better way to x," the answer is always yes. ^_^

In general, though, if your team is meeting targets and generally satisfied with the current process, the cost of rocking that boat is often not worthwhile.

Title: How can I organise a better deployment process?

Body:
I work in a company of about 10 developers. When we start a project, each of the developers involved in the project have their dev environment. Once they complete a task, it is then pushed to phase two which is called "Staging", where QA takes place. Once bugs are cleared, it is then set to "Ready for UAT" and UAT (User Acceptance Testing) is done on the same phase (Staging). If the client is happy with the work it is pushed to the last stage which is called "Live".

Detailed development overflow

Specification

Development (Once specification is approved by client)

Staging QA

Staging UAT

Go Live Deployment

Stage of Support (1 month of free support, this is to give the client a change so point out things we/they didn't notice on the QA and UAT stage)

Once the stage of support ends and the client wants to change something to the software, they need to rise a CR (Change Request), and it all start's from stage one again (Big change requests, things such as new functionality to the current system)

If the change request is small then no specification is needed. It will start from development stage to go Live deployment. If the client spots something wrong after the go live for this CR, then he will have to rise a support case and it starts all over again from the development stage.

My question is, could there be a better way of doing this?


问:
Memory/cache performance in working with arrays in C
I've been toying with some array examples in C. I want to understand more about memory concepts, alignment, and cache. Especially on large arrays on heap. Sometimes I work on large images (extremely large) so that's why it's important to me, but it doesn't matter for what I'm asking now.

I wrote a following example:

```<code>#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <GLFW/glfw3.h>

#define ARRAY_NUM 1000000 * 1000 // GIG
int main(int argc, char *argv[]) {

    if(!glfwInit()) {
        exit(EXIT_FAILURE);
    }

    uint64_t *array = malloc(sizeof(uint64_t) * ARRAY_NUM);

    double time_start = glfwGetTime();
    for(uint64_t i = 0; i < ARRAY_NUM; ++i) {
        array[i] = 0xff;
    }
    double time_end = glfwGetTime();

    free(array);
    glfwTerminate();

    double performance = ((ARRAY_NUM * sizeof(uint64_t))/1000000) / (time_end - time_start);
    printf("Done in %f\n", (time_end - time_start));
    printf("Performance: %f MB/s\n", performance);

    exit(EXIT_SUCCESS);
}
</code>```

With 1 gig allocated I get:

```<code>Done in 36.126213
Performance: 221.445854 MB/s
</code>```

300 meg I get:

```<code>Done in 7.564931
Performance: 317.253391 MB/s
</code>```

200 meg I get:

```<code>Done in 1.279391
Performance: 1250.594728 MB/s
</code>```

100 meg I get:

```<code>Done in 0.355763
Performance: 2248.685313 MB/s
</code>```

10 meg I get:

```<code>Done in 0.036575
Performance: 2187.315761 MB/s
</code>```

and 1 meg I get:

```<code>Done in 0.004050
Performance: 1975.160383 MB/s
</code>```

Of course, timings vary ~5-10% between runs.

Now, what I'm wondering here is what can and should be done in order to have consistent maximum throughput one could get in order to traverse arrays on the heap - especially large ones. Something tells me that even highest numbers of 2+ GB/s aren't top numbers here, and what interests me is 1 meg result vs 10 and 100 - it seems cache gets hot only after certain time.

I am using C99 (gcc5) and my understanding is that with malloc and uint64_t I ought to get aligned memory already. If not, is there maybe a sure way to that? If that's the culprit altogether, of course. Maybe I'm hitting cache lines wrong, or hitting OS's VM pages that it doesn't like? Where would one even start in optimizing linear access within a large array?

Details:

I've done this on a 2011 MacbookAir4,2 (I5-2557M) with 4GB RAM and SSD (maybe that's the problem - low amount of RAM and malloc couldn't get the block in one go? I have to test this on my workstation when I get to it)

gcc 5.2.0 (homebrew) with flags: -pedantic -std=c99 -Wall -Werror -Wextra -Wno-unused -O9 (yeah, niner... I use 3 otherwise, but you never know) with additional include and library flags as well as framework flags in order to use glfw timer which I tend to use. I could've done it without, it doesn't matter.

EDIT:

After some thoughts Erik implanted me with, I made another test. Showing some of the stuff related to pre-fetching and, hopefully, cache as well.

```<code>#include <stdio.h>
#include <stdlib.h>
#include <string.h> // memcpy
#include <inttypes.h>
#include <GLFW/glfw3.h>

#define ARRAY_NUM 1000000 * 125 // GIG
int main(int argc, char *argv[]) {

    double performance = 0.0;

    if(!glfwInit()) {
        exit(EXIT_FAILURE);
    }

    uint64_t *array = malloc(sizeof(uint64_t) * ARRAY_NUM);
    uint64_t *array_copy = malloc(sizeof(uint64_t) * ARRAY_NUM);

    double time_start = glfwGetTime();
    for(uint64_t i = 0; i < ARRAY_NUM; ++i) {
        array[i] = 0xff;
    }
    double time_end = glfwGetTime();

    performance = ((ARRAY_NUM * sizeof(uint64_t))/1000000) / (time_end - time_start);
    printf("Init done in %f - size of array: %lu MBs (x2)\n", (time_end - time_start), (ARRAY_NUM*sizeof(uint64_t)/1000000));
    printf("Performance: %f MB/s\n\n", performance);

    performance = 0;
    time_start = glfwGetTime();
    for(uint64_t i = 0; i < ARRAY_NUM; ++i) {
        array_copy[i] = array[i];
    }
    time_end = glfwGetTime();

    performance = ((ARRAY_NUM * sizeof(uint64_t))/1000000) / (time_end - time_start);
    printf("Copying (linear) done in %f\n", (time_end - time_start));
    printf("Performance: %f MB/s\n\n", performance);

    performance = 0;
    time_start = glfwGetTime();
    for(uint64_t i = 0; i < ARRAY_NUM; i=i+8) {
        array_copy[i] = array[i];
        if(i < (ARRAY_NUM-8)) {
            array_copy[i+1] = array[i+1];
            array_copy[i+2] = array[i+2];
            array_copy[i+3] = array[i+3];
            array_copy[i+4] = array[i+4];
            array_copy[i+5] = array[i+5];
            array_copy[i+6] = array[i+6];
            array_copy[i+7] = array[i+7];
        }
    }
    time_end = glfwGetTime();

    performance = ((ARRAY_NUM * sizeof(uint64_t))/1000000) / (time_end - time_start);
    printf("Copying (stride 8) done in %f\n", (time_end - time_start));
    printf("Performance: %f MB/s\n\n", performance);

    const int imax = 100;
    double performance_average = 0.0;
    for(int j = 0; j < imax; ++j) {
        uint64_t tmp = 0;
        performance = 0;
        time_start = glfwGetTime();
        for(uint64_t i = 0; i < ARRAY_NUM; i=i+8) {
            tmp = array[i];
            if(i < (ARRAY_NUM-8)) {
                tmp = array[i+1];
                tmp = array[i+2];
                tmp = array[i+3];
                tmp = array[i+4];
                tmp = array[i+5];
                tmp = array[i+6];
                tmp = array[i+7];
            }
        }
        time_end = glfwGetTime();

        performance = ((ARRAY_NUM * sizeof(uint64_t))/1000000) / (time_end - time_start);
        performance_average += performance;
        printf("[%d/%d] Performance stride 8: %f MB/s\r", j+1, imax, performance);
        fflush(stdout);
    }
    performance_average = performance_average / imax;
    printf("\nAverage: %f MB/s\n\n", performance_average);

    performance_average = 0.0;
    for(int j = 0; j < imax; ++j) {
        uint64_t tmp = 0;
        performance = 0;
        time_start = glfwGetTime();
        for(uint64_t i = 0; i < ARRAY_NUM; ++i) {
            tmp = array[i];
        }
        time_end = glfwGetTime();

        performance = ((ARRAY_NUM * sizeof(uint64_t))/1000000) / (time_end - time_start);
        performance_average += performance;
        printf("[%d/%d] Performance dumb: %f MB/s\r", j+1, imax, performance);
        fflush(stdout);
    }
    performance_average = performance_average / imax;
    printf("\nAverage: %f MB/s\n\n", performance_average);

    performance = 0;
    time_start = glfwGetTime();
    memcpy(array_copy, array, ARRAY_NUM*sizeof(uint64_t));
    time_end = glfwGetTime();

    performance = ((ARRAY_NUM * sizeof(uint64_t))/1000000) / (time_end - time_start);
    printf("Copying (memcpy) done in %f\n", (time_end - time_start));
    printf("Performance: %f MB/s\n", performance);

    free(array);
    free(array_copy);
    glfwTerminate();

    exit(EXIT_SUCCESS);
}
</code>```

If you don't want to use glfw's timer, just replace it with yours. I compile it with -pedantic -std=c99 -Wall -Werror -Wextra -Wno-unused -O0 and -fprefetch-loop-arrays doesn't seem to have effects, so I left it out.

Now I can see effects of pre-fetching in play. Here's a sample output:

```<code>Init done in 0.784799 - size of array: 1000 MBs (x2)
Performance: 1274.211087 MB/s

Copying (linear) done in 2.086404
Performance: 479.293545 MB/s

Copying (stride 8) done in 0.313592
Performance: 3188.856625 MB/s

[100/100] Performance stride 8: 6458.897164 MB/s
Average: 6393.163000 MB/s

[100/100] Performance dumb: 2597.816831 MB/s
Average: 2530.225830 MB/s

Copying (memcpy) done in 0.202581
Performance: 4936.303056 MB/s
</code>```

You can see how reading 8 values per loop (arbitrary value for now) yields more than twice the bandwidth! What surprised me was that copying was x6+ increase. I have included memcpy as a reference, which is even more faster. Interesting play. I would like to hear more about what to look for and how to approach optimal read/write on arrays and possibly non-trivial payloads (I chose uint64_t in this case).

edit2:

with a stride of 40 (no more, no less - 320bytes) I got reads of ~7800 MB/s. Which is closer to maximum of 10600 (1333MhZ DDR3).

答:
Your tests are basically looping straight thru memory, so the cache is working for you roughly as follows:

You touch some memory not in any cache, which triggers a cache line load from the closest cache, L1, which cascades thru the L2 and L3 caches, until main memory is reached.  An L3-cache-line-sized chunk of data from main memory is fetched to fill the L3.  Then an L2-cache-line-sized chunk of data from L3 is fetched to fill the L2, and so with L1 filling from L2.

Your program can now resume processing of the memory location that triggered the miss.  The next few iterations of your loop will proceed with data from the L1 cache, because a cache line size is larger than the 8 bytes you are touching in each iteration.  After some small number of iterations, the cache line end is reached, and this triggers another cache miss.  It may or may not go all the way out to main memory again, depending on if the L2 or L3 cache line size is the same or larger than the L1 cache line size.

Your loop is generally only benefiting from the cache by the cache line size as more data then one iteration is asking for is fetched by the hardware.

Some processors (Itanium) have a mechanism for the software to inform the hardware to try to perform cache line loads in advance.  This can cover some of the cost of the main memory loads.

Since your loop never revisits any of the memory it touches, you don't get any further benefit from the cache.  If you wanted to experiment further, you could consider:

Writing an outer loop that steps by some fixed stepping amount and some inner loops that use data in the stepping range repeatedly:  So, a triply nested loop with the outer iterating by the fixed stepping amount and the first inner providing simple repetition (e.g. 100 iterations) and the inner most loop accessing the range of the array as per the stepping amount.  Essentially we're going to access each memory location in the array 100 times, but change the order and grouping of how we access those locations, by varying the stepping size in different runs.  This should show substantially more variation (orders of magnitude) between when the caches are working well and they are not.

With small stepping size the caches should work well and the 100 iterations over the each small range will benefit from greatly from cache reuse, and with large stepping sizes, performance should fall off dramatically, being closer to what you're observing with your straight run thru memory.

(All this assumes, of course, that the compiler doesn't mess with your code too much, sometimes overly simple benchmarks get optimized away...)


问:
Is the Perl default variable like an FP monad?
I have been getting my head around monads in functional programming and seem to see some commonality between Perl's default variable $_ and FP monads.

Is this true?  Are there similarities, if not why not - perhaps this'll allow me to grok monads once and for all by understanding why the use of $_ is not monad like!

My (at this stage vague) understanding of a monad is that one aspect is that it can be used to chain commands together so that the output of one is magically the input of the next.

So for example this perl:

```<code>while (<STDIN>) {
   chomp;
   s/Hello/Goodbye/;
   print;
}
</code>```

Takes from the stdin, removes white space (chomp) replaces "Hello" with "Goodbye" and prints out the result.  Each of those commands uses the value in the implied variable $_ and outputs the result into it.

The same code but showing the implied variable use:

```<code>while ($_ = <STDIN>){
    chomp $_;
    $_ =~ s/Hello/Goodbye/;
    print $_;
}
</code>```

So correct me if I'm wrong but that feels very much like one aspect of monads.

答:
Well, I don't know Perl, but that code reminds me of the State monad.  So here's my attempt at mimicking it in Haskell:

```<code>{-# LANGUAGE OverloadedStrings #-}

import Control.Monad
import Control.Monad.Trans
import Control.Monad.Trans.State
import Data.Text.Lazy (Text)
import qualified Data.Text.Lazy as T
import qualified Data.Text.Lazy.IO as TextIO

main :: IO ()
main = flip evalStateT "" $ do
  stdin <- lift TextIO.getContents
  forLinesM_ stdin $ do
    modify T.strip                        -- chomp
    modify (T.replace "Hello" "Goodbye")  -- s/Hello/Goodbye/
    get >>= (lift . TextIO.putStrLn)      -- print

-- | Auxiliary function to loop over the lines of a 'Text', putting
-- each line into the implicit state of the 'StateT' monad transformer.
forLinesM_ :: Monad m => Text -> StateT Text m b -> StateT Text m ()
forLinesM_ text body =
    forM_ (T.lines text) $ \line -> do
      put line
      body
</code>```

The State monad does require that the type of the state be uniform throughout the computation, so I'm guessing it's rather more restrictive than the Perl implicit variable.

I find the above code cringeworthy.  What Perl does with implicit variables, Haskell does with function or monadic composition.  So a better Haskell program for this would be:

```<code>{-# LANGUAGE OverloadedStrings #-}

import Data.Text.Lazy (Text)
import qualified Data.Text.Lazy as T
import qualified Data.Text.Lazy.IO as TextIO

main :: IO ()
main = TextIO.interact go
  -- The '.' operator is right-to-left function composition, so
  -- this reads from bottom to top:
  where go =
       -- Join the lines back together
         T.unlines

       -- Replace strings in each line
       . map (T.replace "Hello" "Goodbye")

       -- trim whitespace from each line
       . map T.strip

       -- Split input into lines
       . T.lines
</code>```

Function composition resembles Unix pipes more than Perl implicit variables.

Body:
Well, I don't know Perl, but that code reminds me of the State monad.  So here's my attempt at mimicking it in Haskell:

{-# LANGUAGE OverloadedStrings #-}

import Control.Monad
import Control.Monad.Trans
import Control.Monad.Trans.State
import Data.Text.Lazy (Text)
import qualified Data.Text.Lazy as T
import qualified Data.Text.Lazy.IO as TextIO

main :: IO ()
main = flip evalStateT "" $ do
  stdin <- lift TextIO.getContents
  forLinesM_ stdin $ do
    modify T.strip                        -- chomp
    modify (T.replace "Hello" "Goodbye")  -- s/Hello/Goodbye/
    get >>= (lift . TextIO.putStrLn)      -- print

-- | Auxiliary function to loop over the lines of a 'Text', putting
-- each line into the implicit state of the 'StateT' monad transformer.
forLinesM_ :: Monad m => Text -> StateT Text m b -> StateT Text m ()
forLinesM_ text body =
    forM_ (T.lines text) $ \line -> do
      put line
      body

The State monad does require that the type of the state be uniform throughout the computation, so I'm guessing it's rather more restrictive than the Perl implicit variable.

I find the above code cringeworthy.  What Perl does with implicit variables, Haskell does with function or monadic composition.  So a better Haskell program for this would be:

{-# LANGUAGE OverloadedStrings #-}

import Data.Text.Lazy (Text)
import qualified Data.Text.Lazy as T
import qualified Data.Text.Lazy.IO as TextIO

main :: IO ()
main = TextIO.interact go
  -- The '.' operator is right-to-left function composition, so
  -- this reads from bottom to top:
  where go =
       -- Join the lines back together
         T.unlines

       -- Replace strings in each line
       . map (T.replace "Hello" "Goodbye")

       -- trim whitespace from each line
       . map T.strip

       -- Split input into lines
       . T.lines

Function composition resembles Unix pipes more than Perl implicit variables.


问:
The granularity level to repeat code: should downloading, unzipping etc. be handled by separate classes?
I make extensive use of DI, but I wonder, where is the 'granurality' limit, when some set of functionality should be separated to a class - lets take an example:

```<code>public class DownloadManager : IDownloadManager
{
    public Uri ResourceIdentifier { get; }
    public DownloadManager(Uri uniqueResourceIdentifier)
    {
        if (!uniqueResourceIdentifier.IsWellFormedOriginalString()) throw new ArgumentException(nameof(uniqueResourceIdentifier));
        ResourceIdentifier = uniqueResourceIdentifier;
    }
    public DownloadManager(string uniqueResourceIdentifier) : this(new Uri(uniqueResourceIdentifier)) { }

    public async Task<byte[]> GetBytesAsync(CancellationToken token = new CancellationToken())
    {
        using (var client = new HttpClient())
        using (var response = await client.GetAsync(ResourceIdentifier, token).ConfigureAwait(false))
        {
            var data = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
            return data;
        }
    }
}
</code>```

It looks for me that this class brings almost no additional benefit, and even hinders some operations:

what if we need a stream (ReadAsStreamAsync())?

it's not a great idea
to litter the code with async, as it spreads like GPL (also see Task
etiqutte).

may lead (but in this case it rather promotes composition) to yo-yo problem

and could be replaced with a static method like this (or even the extension to Uri class):

```<code>public static async Task<byte[]> GetBytesAsync(/*this */Uri uniqueResourceIdentifier, CancellationToken token = new CancellationToken())
{
    if (!uniqueResourceIdentifier.IsWellFormedOriginalString()) throw new ArgumentException(nameof(uniqueResourceIdentifier));
    using (var client = new HttpClient())
    using (var response = await client.GetAsync(uniqueResourceIdentifier, token).ConfigureAwait(false))
    {
        var data = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
        return data;
    }
}
</code>```

But one cannot simply DI a static method, so this will be wrapped in thin class implementing an interface.

Option 3: The third option would be, the code could be written just where it's needed (the API is great, almost no redundant configuration), risking, that if needed, the code will need to be refactored (or copied) in the future. Also, it could be tempting than to make a god-class like FileManager, which will download, unzip CRUD & all, but sacrificing freedom and 'S' from SOLID.

Which one is preferred in this specific case (almost the same goes
for file unzipping, using ZipArchive and couple more of great MS
APIs)?

Based on what, should I make a decision which one of this
three approaches to take?

答:
May I remind you to assess conformance with the SOLID principles.

Also, bear in mind the idea that "Good enough really does mean good enough". If extra abstractions aren't necessary, then don't do it. It's unnecessary complication.

But downloading, unzipping etc should be in separate classes if they may be used independently, or if alternative methods of downloading and/or unzipping might be desirable.

One major pro for separate classes is testability; a huge benefit of the single responsibility principle.

It's all a matter of balance. If you're finding the code is becoming unmanagable, then do something about it by being stricter on good OOP heuristics such as SOLID.

Lastly, remember to code to an interface. Design the interface first. Write a snippet of code that would use the module in question, as if it already exists, in the most natural way. Then write that code according to that interface. Then finally decide on the implementation, and keep that separate from the interface.

OwnerDisplayName: Piotr Falkowski


问:
Does syntax really matter in a programming language?
One of my professors says "the syntax is the UI of a programming language", languages like Ruby have great readability and it's growing, but we see a lot of programmers productive with C\C++, so as programmers does it really matter that the syntax should be acceptable?

I would love to know your opinion on that.

Disclaimer: I'm not trying to start an argument. I thought this is a good topic of discussion.

Update: This turns out to be a good topic. I'm glad you are all participating in it.

答:
Yep, sure.

If you wanna initiate a big flame, ask the folks, where they put the opening bracet in C-like languages. I mean

```<code>void foo() {
  // blah
}
</code>```

VS

```<code>void foo()
{
  // blah
}
</code>```

or even VS

```<code>void foo()
{ // blah
}
</code>```

And this is just the same language! Also, ask them about spaces, where they place them (function name and bracet, operators etc.).

1000 answers are guaranteed!

Body:
Yep, sure.

If you wanna initiate a big flame, ask the folks, where they put the opening bracet in C-like languages. I mean

void foo() {
  // blah
}

VS

void foo()
{
  // blah
}

or even VS

void foo()
{ // blah
}

And this is just the same language! Also, ask them about spaces, where they place them (function name and bracet, operators etc.).

1000 answers are guaranteed!

ContentLicense: CC BY-SA 2.5


问:
Do 'OR' conditional statement continue if already true?
I have a question in regards to Java conditional if statements. If I have a statement such as this:

```<code>if(true || false){
//output
}
</code>```

Will Java go inside of the the if statement as soon as it sees the true statement or will it still evaluate the false statement? I ask because I will have a  condition statement which will have a few conditional tests, each which will require a call to a database:

```<code>if(isDatabase1() || isDatabase2() || isDatabase3()){
}
</code>```

The first test will be a condition which will most likely return true, so I was wondering if it will stop after this and go straight into the if statement, or if it will still test the remaining statements (which then will require useless database calls)?

答: No, they short circuit. More specifically if isDatabase1 returns true then neither isDatabase2 or isDatabase3 will be called. https://en.wikipedia.org/wiki/Short-circuit_evaluation</p>


问:
What i and n stand for in python?
I've been writing a program to pick random words from my list. However, to do that I had to imitate some solutions on the internet, and I succeeded.
Unfortunately, there is something that I can't understand in my work.

```<code>def repeat(pic_word, n):
    for i in range(n):
        pic_word()
</code>```

My question is what is the meaning of i in "for i in range"?

答:
i is just the name of a variable, in this case an integer. Most programmers will use i for the outermost for loop when iterating through a list of numbers.

What this loop is doing is creating a range from 0 to n (some number). Then it's looking at all the numbers in there, one at a time. In this case the loop is running pic_word() n times.

Title: What i and n stand for in python?


问:
How big should be an input array to unit test a method with a loop?
This probably sounds like a dumb question, forgive me if I'm asking this in the wrong place, I'd be happy to delete it if so. I'm sort of new to unit testing and I'd like to learn best practices regarding this question, it's been bugging me for a while.

Assume that there is a method which takes an array as the input and performs some algorithm to its elements. There may be a situation where the method is called repeatedly with input containing elements that have already been processed by the object (and remembered somewhere). I'd like to test that the method doesn't process the same input elements twice, i.e. that it does nothing the second time it encounters those items.

The object under test is actually a wrapper around another object that is provided as a depencency to it, so I can mock that object and verify that the necessary processing methods are called.

So my question is:

How many input elements would you consider to be "enough" to in order to test that the method works properly (for both old and new elements)? One? Three? Ten?

答:
What you describe is something of the form:

```<code>for elt in list loop
  if unprocessed(elt) then process(elt) end loop
end loop
</code>```

What you REALLY have is something of the form

```<code>for elt in list loop
   maybe_process(elt)
end loop
</code>```

where

```<code>proc maybe_process(elt) is
   if unprocessed(elt) then process(elt) end if
end proc
</code>```

LISPers will immediately recognize the second form as MAPC, which takes a function and applies it to each element of a list.

At that point, your original problem is decomposed into two much simpler problems, that of calling an ARBITRARY function on each element of a list, and that of writing a function that can take an element, determine whether it has already been processed, and, if not, process it.  The first function takes as few elements in the list as you need, I'd probably use three.  The second function takes as many individual test cases as you need to check out the two pieces of the pie.

Even if you decompose it in the first form, you can use very simple test functions to check out the loop, and then a very simple process() function to check out the unprocessed() predicate.

The key is that you can use MUCH SIMPLER functions to do your testing phases.

Body:
What you describe is something of the form:

for elt in list loop
  if unprocessed(elt) then process(elt) end loop
end loop

What you REALLY have is something of the form

for elt in list loop
   maybe_process(elt)
end loop

where

proc maybe_process(elt) is
   if unprocessed(elt) then process(elt) end if
end proc

LISPers will immediately recognize the second form as MAPC, which takes a function and applies it to each element of a list.

At that point, your original problem is decomposed into two much simpler problems, that of calling an ARBITRARY function on each element of a list, and that of writing a function that can take an element, determine whether it has already been processed, and, if not, process it.  The first function takes as few elements in the list as you need, I'd probably use three.  The second function takes as many individual test cases as you need to check out the two pieces of the pie.

Even if you decompose it in the first form, you can use very simple test functions to check out the loop, and then a very simple process() function to check out the unprocessed() predicate.

The key is that you can use MUCH SIMPLER functions to do your testing phases.


答: If someone finds out you have misappropriated the open source license, he might search for the legal successor of the original copyright owners and persuade them to sue you. Or buy the copyright from them and sue you himself. You are never completely safe.</p>

Title: Do licenses matter if there's nobody around to enforce them?

Body:
Suppose that the original creators can't (or won't) enforce a license on their software/code, but that work is still popular.

I guess if you want to visualize it, I'll throw out a convoluted hypothetical:

Imagine a very small group of developers that released a code project under an open-source license. The repository was hosted on their servers. However, everybody on the immediate development team passed away in a tragic accident or something. Their servers shut down after this happened. The project had a fairly large user base, and so others began to host the last revision on their own servers for others to download.

(Yes, I have an active imagination)

Does abiding by the license simply become a matter of morality by its users, or can there still exist a legal penalty when there is no one user or group to enforce it? Could anything be done if an unscrupulous user decided to branch off the project and use it under a different license?

I am not looking for legal advice -- I am simply curious about how software licenses work. I tend to think of strange situations and wonder what would happen in those scenarios.


问:
Do primitive data types have different bit patterns?
And if so, why do they have different bit patterns?

I read a book that said data types have different bit patterns and I was like "why do they have different bit patterns?"

答:
Signed Integers can be represented using four different well-know bit patterns.  They can vary in their bit patterns by the number of bytes used to represent them, and the way those bytes are ordered (endianness).  Unsigned integers have their own bit patterns that are different from signed integers.

Floating point numbers use an entirely different bit pattern representation.  Single-precision floating point looks like this:

And double-precision floating point looks like this:


问:
Difference between immutable and const
I've often seen the terms immutable and const used interchangeably. However, from my (little) experience, the two differ a lot in the 'contract' they make in code:

Immutable makes the contract that this object will not change, whatsoever (e.g. Python tuples, Java strings).

Const makes the contract that in the scope of this variable it will not be modified (no promise whatsoever about what other threads might do to the object pointed to during this period, e.g. the C/C++ keyword).

Obviously, the two are not equivalent, unless the language is single-threaded (PHP), or has either linear or uniquness typing system (Clean, Mercury, ATS).

First, is my understanding of these two concepts correct?

Second, if there is a difference, why are they almost exclusively used interchangeably?

答:
Speaking of JavaScript, the key words const and Object.freeze

const applies to bindings variables. It creates an immutable binding, you cannot assign a new value to it.

Object.freeze works on object values. It makes an object immutable. Namely, you cannot change its properties.

Title: Difference between immutable and const


问:
Unifying two implementation through an interface
I have a simple object like this

```
  public class Book
    {
        public List Chapters { get; private set; }
        public TableOfContent BookTOC { get; set; }
        public string Identifier { get; private set; }
        public string Title { get; private set; }
        public string Publisher { get; set; }
        public string Rights { get; internal set; }
        public DateTime PublishingDate { get; internal set; }

        private IBookRepository _repository = null;

        public Book(IBookRepository bookRepository)
        {
            _repository = bookRepository;
        }

        public Book Load(string bookid)
        {
            //Load book from repository
        }

       public Chapter GetNextChapter(string chapterId)
        {
            //Load Chapter from repository
        }

    }
```

In this domain (actually anywhere in the world!), book has chapters and book has ToC. In our application, there would be two possible implementations of repository i.e. the book object may either be loaded from Xml or be loaded from database. These books are pretty large in size i.e. two-four hundred thousand chapters (This is all about book metadata, content is separate but out of question for now)

My problem is that when the book is loaded from database, I want to leverage database capabilities of querying and indexes etc. For example, in the GetNextChapter, when I am using database repository, I can locate just the specific chapter and find next of it. However in case of Xml, since I don't want to traverse xml over and over again, I would like to keep it loaded at one time initially and the cache it upstream so that I can find the next chapter just from the object itself. As you can see, these two implementations by nature are different, but at any given time we will be using only one of those. I am not able come up with a uniform interface of operations that both these implementations can follow. Let's say if I decide to have operation GetNextChapter implemented in both, then for database repository, I just need the chapter ID, whereas in the Xml repository, I need the whole book object or at least list of chapters (and even that has nothing to do with xml actually because that's just in memory filter).

Can anyone help in deciding how do I uniform these two through a single interface having two implementation? Or are they not meant to be uniformed via a single interface and deserve a separate way of implementation?

EDIT
To clarify further, if I add GetNextChapter method in the interface, Database implementation needs only the chapterID as parameter, whereas Xml implementation would need the book object. Similarly the LoadBook method will return full object (with chatpers and toc) for XMLImplementation (so that I don't have to traverse xml over and over) whereas it will only return partial object (without chapters and toc) in case of DatabaseImplementation (for obvious performance reasons).

So how will these two go against a single interface?

答:
Simple. Make Book abstract class and implement it's loading in concrete classes.

First, I would not mix up "repository-like" methods in Book, but keep it only in repository.

```<code>public abstract class Book
{
    public List<Chapter> Chapters { get; private set; }
    public TableOfContent BookTOC { get; set; }
    public string Identifier { get; private set; }
    public string Title { get; private set; }
    public string Publisher { get; set; }
    public string Rights { get; internal set; }
    public DateTime PublishingDate { get; internal set; }

    public abstract Chapter GetNextChapter(string chapterId);
}

public class DBBookDepository
{
    public Book LoadBook(string identifier)
    {
        return new DBBook(this);
    }
}

public class DBBook : Book
{
    private readonly DBBookDepository _bookRepo;

    public DBBook(DBBookDepository bookRepo)
    {
        _bookRepo = bookRepo;

        // load stuff from DB
    }

    public override Chapter GetNextChapter(string chapterId)
    {
        // either do SQL command or call repository
    }
}

public class XmlBookRepository
{
    public Book LoadBook(string path)
    {
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load(path);
        return new XmlBook(path, xmlDocument);
    }
}

public class XmlBook : Book
{
    private readonly string _path;
    private readonly XmlDocument _xml;

    public XmlBook(string path, XmlDocument xml)
    {
        _path = path;
        _xml = xml;

        // load some other stuff
    }

    public override Chapter GetNextChapter(string chapterId)
    {
        // load chapter from xml
    }
}
</code>```

This is not exact solution. For example you can load stuff in repository and then pass it into concrete objects instead of doing that in constructor. Also, it seems that you cannot have generic IBookRepository, because different repos will load books based on different parameters (eg. DB ID vs. path to xml).

Also, if you want to have "chapter browsing" logic, then extracting that into it's own object might be good idea. Similar to what iterators do.

Body:
Simple. Make Book abstract class and implement it's loading in concrete classes.

First, I would not mix up "repository-like" methods in Book, but keep it only in repository.

public abstract class Book
{
    public List<Chapter> Chapters { get; private set; }
    public TableOfContent BookTOC { get; set; }
    public string Identifier { get; private set; }
    public string Title { get; private set; }
    public string Publisher { get; set; }
    public string Rights { get; internal set; }
    public DateTime PublishingDate { get; internal set; }

    public abstract Chapter GetNextChapter(string chapterId);
}

public class DBBookDepository
{
    public Book LoadBook(string identifier)
    {
        return new DBBook(this);
    }
}

public class DBBook : Book
{
    private readonly DBBookDepository _bookRepo;

    public DBBook(DBBookDepository bookRepo)
    {
        _bookRepo = bookRepo;

        // load stuff from DB
    }

    public override Chapter GetNextChapter(string chapterId)
    {
        // either do SQL command or call repository
    }
}

public class XmlBookRepository
{
    public Book LoadBook(string path)
    {
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load(path);
        return new XmlBook(path, xmlDocument);
    }
}

public class XmlBook : Book
{
    private readonly string _path;
    private readonly XmlDocument _xml;

    public XmlBook(string path, XmlDocument xml)
    {
        _path = path;
        _xml = xml;

        // load some other stuff
    }

    public override Chapter GetNextChapter(string chapterId)
    {
        // load chapter from xml
    }
}

This is not exact solution. For example you can load stuff in repository and then pass it into concrete objects instead of doing that in constructor. Also, it seems that you cannot have generic IBookRepository, because different repos will load books based on different parameters (eg. DB ID vs. path to xml).

Also, if you want to have "chapter browsing" logic, then extracting that into it's own object might be good idea. Similar to what iterators do.


问:
How can I organise a better deployment process?
I work in a company of about 10 developers. When we start a project, each of the developers involved in the project have their dev environment. Once they complete a task, it is then pushed to phase two which is called "Staging", where QA takes place. Once bugs are cleared, it is then set to "Ready for UAT" and UAT (User Acceptance Testing) is done on the same phase (Staging). If the client is happy with the work it is pushed to the last stage which is called "Live".

Detailed development overflow

Specification

Development (Once specification is approved by client)

Staging QA

Staging UAT

Go Live Deployment

Stage of Support (1 month of free support, this is to give the client a change so point out things we/they didn't notice on the QA and UAT stage)

Once the stage of support ends and the client wants to change something to the software, they need to rise a CR (Change Request), and it all start's from stage one again (Big change requests, things such as new functionality to the current system)

If the change request is small then no specification is needed. It will start from development stage to go Live deployment. If the client spots something wrong after the go live for this CR, then he will have to rise a support case and it starts all over again from the development stage.

My question is, could there be a better way of doing this?

答:
There are lots of questions that need to be answered here:

Are you using a vcs?

What is your branching strategy?

Can you deploy using a single button?

Do you have automated tests?

When do you run the tests?

Do you have any continuous integration?

What happens with your team while the job is on the Staging area? Are you working on something else? Or just waiting?

What happens with the QA team while the UAT part is going on?

How long it takes you to deploy?

Does deployment mean that your user cannot work on the system?

Do you have a database? Can you undo a deployment on your database?

The basics of what you are doing is fine, mostly because of that luxurious trait of having a fully closed specification (never happened to me). But the basics don't make your life easier, nor provide the best value for your company.

I am supposing you have a vcs. Which branching strategy are you using? Those strategies will change based on the vcs. I would choose a different branching strategy if I am using Subversion/TFS Source Control or if I am using distributed ones like git or hg.

Have tests(unit, integration, requirement), and make sure that you can run them automatically. Tests should be run on a CI environment that goes between the dev and the staging environments. Any bug encountered by the QA team should become an automated test where possible (depending on your UI technology that could not be always possible). These actions will: reduce the time spend on "Staging", reduce the number of bugs that appear, reduce the number of regression errors that will appear.

The CI environment should automatically compile and run all the tests (it could be well possible that you want to run unit tests all the time, but integration tests only at one specific point of the day). This action will provide your team with assurance that either you are not breaking anything or that you will know pretty much immediatly. Tools for CI? Jenkins, Team City, TFS, ...

At some point I was working on a team on which a deployment of our application meant usually a two hour manual process that needed all users to be logged out. We managed to improve it, though still manual, to less than 5 minutes with only a restart for the users if there was no database change. If there was a database change, it still required half hour of the users being out. But the time I left the company we were looking at using tools (Octopus on this case) to make it even easier and a green/blue strategy on our database to eliminate completely downtime (except for the restart of the client terminals).

I would completely separate the staging environment for internal QA from the staging environment for UAT. My previous experience is that is very messy to have them together.

I would have a prelive environment just to test the deployment on the exact same conditions as there are in live. I had suprises before because our system was depending on some library or OS patch that was present on all stages but Live.

Which then will mean that there is another question, can your IT team replicate the setup of live? If they cannot they should be working on it. The provisioning of a machine/environment should be completely automated once you have the hardware.

You can always improve your process. At the beginning you usually can take some big strides, later one, is more fine tuning. You should ask yourself (and your team) the question that you have posted every month.


问:
Converting data in setters in a DTO
I wrote a simple DTO to transfer data from one point to another, or so was the initial thought.

Now I just caught myself in the act of doing various things in the DTO, for simplicity's sake, like converting data in a DTO setter to make sure it fits the format, or even validating it and throwing an exception, to reduce method calls / simplify the point where you are actually hydrating the DTO with information.

As DTO's – by the definition I know – should not have any behaviour except storage and retrieval of data, I'm now looking for a better structure.

The DTO's used here are mainly for transferring data from one source class (controller) into other classes (e.g. payment gateways) to have the same naming of data (e.g. "price" for the transaction price) for all gateways, which then are processed accordingly in the anticorruption layer in front of the payment gateway's SDK provided by their vendors.

```<code>User code --> DTO -> Gateway ACL (-> SDK -> …)
</code>```

As my current setup does not really line up with a DTO definition, I'm now looking for an architecture that is clean and matches a design pattern.

An example of my now non-DTO-compliant DTO:

```<code>class GenericDataTransferObject {
    protected $price;

    public function setPrice($price) {
        $this->price = $this->convertAmount($price);
    }
}

abstract class AbstractDataTransferObject {
    protected convertAmount($amount) {
        // …
    }
}
</code>```

How would this look like in a perfect world?

答:
You can add a new layer (that I usually call adapters or glue) to handle these conversions between user objects and DTOs.

```<code>User code --> adapters/glue --> DTO
</code>```

The adapter code would look something like:

```<code>class MyAdapter {

    public toDTO(BusinessObject $bo) {
        DTO $dto = new DTO();
        $dto.setPrice(convertAmount($bo.getPrice());
        return $dto;
    }

    private convertAmount($amount) {
        //Convert according to some rule
    }

    public fromDTO(DTO $dto) {
        //If the reverse operation is needed
    }
}
</code>```

Title: Converting data in setters in a DTO

Body:
You can add a new layer (that I usually call adapters or glue) to handle these conversions between user objects and DTOs.

User code --> adapters/glue --> DTO

The adapter code would look something like:

class MyAdapter {

    public toDTO(BusinessObject $bo) {
        DTO $dto = new DTO();
        $dto.setPrice(convertAmount($bo.getPrice());
        return $dto;
    }

    private convertAmount($amount) {
        //Convert according to some rule
    }

    public fromDTO(DTO $dto) {
        //If the reverse operation is needed
    }
}


答:
In C, C++ and related languages, there is also a difference between an object being const and your reference or pointer to the object being a constant reference.

If you try to modify a constant object you get undefined behaviour. (You can try to modify a constant object for example by taking its address, casting the address to a non-const pointer, and then using that non-const pointer to modify the object).

The constant pointer or reference on the other hand just tells the compiler that you cannot use this pointer or reference to modify the object. You can cast the pointer or reference and try to modify the object. If the object itself was constant, bad things will happen. If the object wasn't actually constant, it will change. This may of course confuse users of your code and quite possibly cause bugs.

In C, if you use a string literal like "Hello", the five chars and the trailing zero bytes are actually constant, but you get a non-const pointer. Very bad idea to use that non-const pointer to change the object.

In C, you can have a "const restrict" pointer. That means the object pointed to is temporarily constant. If the object is modified by any means while the "const restrict" pointer is in scope, you get undefined behaviour. This is stronger than a const pointer which only prevents you from changing an object through this pointer.

Title: Difference between immutable and const

Body:
I've often seen the terms immutable and const used interchangeably. However, from my (little) experience, the two differ a lot in the 'contract' they make in code:

Immutable makes the contract that this object will not change, whatsoever (e.g. Python tuples, Java strings).

Const makes the contract that in the scope of this variable it will not be modified (no promise whatsoever about what other threads might do to the object pointed to during this period, e.g. the C/C++ keyword).

Obviously, the two are not equivalent, unless the language is single-threaded (PHP), or has either linear or uniquness typing system (Clean, Mercury, ATS).

First, is my understanding of these two concepts correct?

Second, if there is a difference, why are they almost exclusively used interchangeably?


问:
Requirements and Documentation in Agile
My company has recently started to take on smaller software development projects. Typically small web applications to be used on a client's intranet. They are usually 3-5 months work in total.

Typically, the client doesn't know what they want exactly so they pay us for a smaller projects (3-4 weeks) to gather requirements and write a functional design. Once that is approved, we both know what is wanted and can quote for the remainder of the project and carry on. This is because they like to have fixed-price contracts rather than paying time and materials.

This development lends itself quite nicely to a waterfall model, we gather requirements, design, implement, test, deliver. However, despite this, we sometimes get to delivery and the client says "that's not what I meant".

My instinct is to make sure we have a more detailed design up front but I'm being told we should be "more agile" and iteratively development the system with the client so they can see it develop and we can catch problems with expectations earlier.

I'm struggling to see how to fit that into a fixed-price model. How can I estimate a project when the client doesn't even know what they want? I can see that Agile development would be beneficial in this scenario, but not with a fixed-price.

Am I missing something?

答: If customer is fine paying for 3-4 week long project, they should be fine paying for 2 week project. And then next 2 week project. And then next one. And next one and so on. And at end of each of these projects, you give them some semi-finished application and ask them what is missing or what is wrong. Also ask what is most important from that which is missing or wrong, so it can be included in next project.</p>

Title: Requirements and Documentation in Agile


答:
The important thing is to agree what will be delivered and then time-box it into a series of 2 week sprints - mini-waterfall developments if you will.

If the client is a little unclear on how something will work, that is fine, but it shouldn't be committed to the development sprint as it can't realistically have any accurate development effort assigned to it. Throw some wireframes together and thrash the issues out first.

It can be tempting to think you know what the client will want as it is cash in the bank but if at the end of the sprint(s), the client still isn't happy, then they have you over a barrel. In their mind, they have paid for the feature and development should progress until it is done. This nightmare scenario is to be avoided at all costs. I worked on a medium sized development where the account manager agreed to what the client called "some simple reports" without looking into the detail. These "simple reports" burned through a 1/3 of our development budget despite only having a 1/10 initially budgeted. Make it clear to the client that you're willing to throw designs around until they're happy but only commit to development once you have a firm idea of what is required.

If the design process looks like it might take some time, then by all means have a series of parallel sprints for this too. The client may decide at the end of the sprint, that they really aren't clear what is required and so might just throw the idea away, or better - they'll have been able to narrow down what exactly it was they wanted which will feed into the next sprint.

Title: Requirements and Documentation in Agile

Body:
My company has recently started to take on smaller software development projects. Typically small web applications to be used on a client's intranet. They are usually 3-5 months work in total.

Typically, the client doesn't know what they want exactly so they pay us for a smaller projects (3-4 weeks) to gather requirements and write a functional design. Once that is approved, we both know what is wanted and can quote for the remainder of the project and carry on. This is because they like to have fixed-price contracts rather than paying time and materials.

This development lends itself quite nicely to a waterfall model, we gather requirements, design, implement, test, deliver. However, despite this, we sometimes get to delivery and the client says "that's not what I meant".

My instinct is to make sure we have a more detailed design up front but I'm being told we should be "more agile" and iteratively development the system with the client so they can see it develop and we can catch problems with expectations earlier.

I'm struggling to see how to fit that into a fixed-price model. How can I estimate a project when the client doesn't even know what they want? I can see that Agile development would be beneficial in this scenario, but not with a fixed-price.

Am I missing something?


答:
The simple fact is that you already are failing to cater for a fixed price model: you do upfont design, code and deliver ans then the client says "oh no, we want it to be different", and what do you do then? Charge them extra for the new work, or complete the changes for free?

Agile simply changes the development model to cater for this, your pricing is either by time spent or by fixed delivery date. In the former, you keep going until the client is happy, in the latter you keep going until the cash runs out. (this is one reason why you must always have something deliverable at the end of each timebox, there may not be another one)

Hopefully agile means you get to complete more work as the requirements capture is cheaper (as its iterative and requires less full documentation and signoff) so the client gets what they wanted quicker. That's the idea.. but in my experience, the client is never satisfied so they end up paying more (in a time-spent billing system) without noticing.. which always pleases management :-)

Title: Requirements and Documentation in Agile


问:
What does `Resolving` mean in programing?
I see in many places where the word Resolve is used, in Dependency Injection (resolve an implementation to an interface),
Package Managers (ex: Resolve packages dependencies), Web (ex: Resolve a hostname).

So what does make the logic of the code so special that makes someone choose the word Resolve over a simple Convert or Transform or even a Get?

答:
The difference is slight, but consistent. Transforming a representation into another one involves taking the same data and expressing it in another format - for instance, a hexadecimal into a decimal number, or  mixed-case strings to an all-lower string. Typically, you need nothing but a fixed set of rules to carry out such a transformation.

Resolving a name typically involves querying some repository that associates names with information that cannot be predicted without the repository. google.com may resolve to a specific I.P. address (several, actually), but that is an accident of the I.P. assignment history - it might just as well have been a totally different number, and there is no way of making this translation that doesn't somehow involve storing the entire registry, or querying it online. Similarly, resolving packages dependencies usually requires installing the missig packages - it isn't enough to know the names of the missing components, you have to actually acquire their contents, and the contents are not predictable from the name alone ("QMail" sounds like a mail program, but you can't predict its exact properties just from the name without installing it, since the name transports far less information than the compiled program).

Therefore you shouldn't use the term "resolving" for something that is a mere context-free data transformation. Reserve it for situations where a substantial amount of information is symbolized by each name, so much that the only reasonable arrangement is to have a central registry for it.


问:
Do licenses matter if there's nobody around to enforce them?
Suppose that the original creators can't (or won't) enforce a license on their software/code, but that work is still popular.

I guess if you want to visualize it, I'll throw out a convoluted hypothetical:

Imagine a very small group of developers that released a code project under an open-source license. The repository was hosted on their servers. However, everybody on the immediate development team passed away in a tragic accident or something. Their servers shut down after this happened. The project had a fairly large user base, and so others began to host the last revision on their own servers for others to download.

(Yes, I have an active imagination)

Does abiding by the license simply become a matter of morality by its users, or can there still exist a legal penalty when there is no one user or group to enforce it? Could anything be done if an unscrupulous user decided to branch off the project and use it under a different license?

I am not looking for legal advice -- I am simply curious about how software licenses work. I tend to think of strange situations and wonder what would happen in those scenarios.

答: You want to bet your company on some licence not being enforced?</p>


问:
Did labelled loops pre-date for loops?
Was the first implementation of looping control flow effectively the goto  (or something like a labelled JMP in assembler) or was there another syntactic construction?

for (für) was introduced in Superplan in 1949 IIRC.

答: Assembler has several low-level jump operations including a non-conditional jump to a label and several conditional jumps to a label.  The conditional jumps execute only if a specific control flag condition is true, otherwise program flow drops through to the next instruction.  There is also a loop instruction that affects the value in the ecx register and jumps to a label when non-zero.</p>

Title: Did labelled loops pre-date for loops?


问:
Is the word 'statement' in 'statement block' redundant?
Is the word 'statement' in 'statement block' redundant?

In other words is a block in a program composed, by definition, of statements?

答: No.  A program's text can contain "comment" or "documentation" blocks as well, and certain languages might have "data structure" blocks, like baked-in XML or JSON.</p>


问:
Making internal search engine return results that adhere to local permissions on an internal web application
I have an ElasticSearch search engine and an internal web application.  Prior to applying the security model, search results were very fast.  The security model restricts some users from accessing certain pages.  We have 7 "types" of pages.

Our process is currently this:

Search engine queries all records

For each type of result, I get all recordIds of that type from the database that the user is allowed to access. This ensures I only call the database a max of 7 times (once per type)

I do an intersect between the records the user can access and the records search is returning

The above approach isn't fast enough. I have an autosuggest search and each query takes up to 2 seconds now.

What is a typical way to make a search engine return results that adhere to security?  Another developer has recommended I store the security permissions directly in ElasticSearch itself(essentially mirroring what our sql server has).

Edit: Each record has a page. We can assume 200-500 users and 25000+ records.

答:
A practice I use for implementing security in search scenario's is like this. You would define groups of users and use these groups to define access to records. When building the index for search, include the access for each group in the index (basically this is the ACL for your record).

You should then have a repository that stores what users are members of what groups. You can then easily retrieve groupmemberships for a user, for example at login.

At query time you would only include the group memberships in your query so that it needs to have at least one of the groups the user is a member of.

In practice the repository holding the group memberships tends to be more complex, because of recursive group memberships.

To summarize:

Work with groups

Have a security info repository

Index your ACL's

Query with users group memberships

HTH.


问:
Are there any compilers that attempt to fix syntax errors on their own?
I heard a while back that there used to be a compiler that attempted to fix syntax errors by analyzing context and inferring what was intended.

Does such a compiler really exist? Obviously it has little practical value, but would be very interesting to play with and learn from.

答:
In some sense, the act of compiling is inferring what certain syntax is meant to do, and hence a syntax error is when the compiler isn't able to figure it out. You can add more "guessing" to have the compiler infer further things and be more flexible with the syntax, but it must do this inferring by a specific set of rules. And those rules then become a part of the language, and is no longer errors.

So, no, there are no such compilers, really, because the question doesn't make sense. Guessing what syntax errors are meant to do according to some set of rules just becomes a part of the syntax.

In that sense, there is a good example of a compiler that does this: Any C compiler. They will often just print out a warning of something that isn't like it should be, and then assume you meant X, and go on. This is in fact "guessing" of unclear code (although it's mostly not syntax per se), something that just as well could have stopped compilation with an error, and therefore qualify as an error.


问:
shopping system cart problem with stock
I'm trying to implement a little shopping system. I have a single product which has a limited stock.

For example, my product can be bought 5 times.

My problem is, if a user adds this product to his cart, fill the form and pay.

Decrement the stock when he adds the product to his cart (problem: if he quits the site before the payment? The product stock is decremented, but not bought)

Decrement the stock when the payment is confirmed (problem, if 2 or more user pay at the same time)

Have you got any advice on the best way to solve this?

Thanks

答:
I would look at it as if it was a physical shop. Customer A goes to a shelve, takes the products needed from the shop into his or her cart and may or may not proceed to buy the products in the cart.
Customer B can go to the shelve, however he or she can not pick up the same stock as Customer A as that stock has moved into Customers A cart.

The point is to not handle stock as a number, but as actual objects that move through your system, until they are checked out.

You should then have a time out mechanism, so that if someone leaves a cart with stock in your shop, stock is put back on the shelve at some point. Think of it as a shop clerk going through the store and cleaning up after the customers after the shop has closed for the night.

Title: shopping system cart problem with stock