问:
Brittle unit tests due to need for excessive mocking
I've been struggling with an increasingly annoying problem regarding our unit tests that we are implementing in my team. We are attempting to add unit tests into legacy code that wasn't well designed and while we haven't had any difficulty with the actual addition of the tests we are starting to struggle with how the tests are turning out.

As an example of the problem let's say you have a method that calls 5 other methods as part of its execution. A test for this method might be to confirm that a behavior occurs as a result of one of these 5 other methods being called. So, because a unit test should fail for one reason and one reason only, you want to eliminate potential issues caused by calling these other 4 methods and mock them out. Great! The unit test executes, the mocked methods are ignored (and their behavior can be confirmed as part of other unit tests), and the verification works.

But there's a new problem - the unit test has intimate knowledge of how you confirmed that behavior and any signature changes to any of those other 4 methods in the future, or any new methods that need to be added to the 'parent method', will result in having to change the unit test to avoid possible failures.

Naturally the problem could be mitigated somewhat by simply having more methods accomplish less behaviors but I was hoping there was perhaps a more elegant solution available.

Here's an example unit test that captures the problem.

As a quick note 'MergeTests' is a unit testing class that inherits from the class we are testing and overrides behavior as needed. This is a 'pattern' we employ in our tests to allow us to override calls to external classes / dependencies.

```<code>[TestMethod]
public void VerifyMergeStopsSpinner()
{
    var mockViewModel = new Mock<MergeTests> { CallBase = true };
    var mockMergeInfo = new MergeInfo(Mock.Of<IClaim>(), Mock.Of<IClaim>(), It.IsAny<bool>());

    mockViewModel.Setup(m => m.ClaimView).Returns(Mock.Of<IClaimView>);
    mockViewModel.Setup(
        m =>
        m.TryMergeClaims(It.IsAny<Func<bool>>(), It.IsAny<IClaim>(), It.IsAny<IClaim>(), It.IsAny<bool>(),
                         It.IsAny<bool>()));
    mockViewModel.Setup(m => m.GetSourceClaimAndTargetClaimByMergeState(It.IsAny<MergeState>())).Returns(mockMergeInfo);
    mockViewModel.Setup(m => m.SwitchToOverviewTab());
    mockViewModel.Setup(m => m.IncrementSaveRequiredNotification());
    mockViewModel.Setup(m => m.OnValidateAndSaveAll(It.IsAny<object>()));
    mockViewModel.Setup(m => m.ProcessPendingActions(It.IsAny<string>()));

    mockViewModel.Object.OnMerge(It.IsAny<MergeState>());

    mockViewModel.Verify(mvm => mvm.StopSpinner(), Times.Once());
}
</code>```

How have the rest of you dealt with this or is there no great 'simple' way of handling it?

Update - I appreciate everyone's feedback. Unfortunately, and it's no surprise really, there doesn't seem to be a great solution, pattern, or practice one can follow in unit testing if the code being tested is poor. I marked the answer that best captured this simple truth.

答:
Fix the code to be better designed. If your tests have these issues, then your code will have worse issues when you try to change things.

If you can't, then perhaps you need to be less ideal. Test against the pre and post-conditions of the method. Who cares if you're using the other 5 methods? They presumably have their own unit tests making it clear(er) what caused the failure when the tests fail.

"unit tests should have only one reason to fail" is a good guideline, but in my experience, impractical. Hard to write tests don't get written. Fragile tests don't get believed.


问:
Extreme Programming Daily Commits
Isn't daily commits (committing all changes before going home) an XP practice?

What are the benefits of following this?

What are the risks of not following this?

答:
Among many XP activities one of them is RESPECT that states:

Programmers should never commit changes that break compilation, that make existing unit-tests fail, or that otherwise delay the work of their peers

```<code>What are the benefits of following this
</code>```

You know how much work is done & how much is left to be done.

Other ppl have faster access to your code(less waiting time).

Easier to spot if a programmer is doing the task correctly.

Easier and faster code Integration.

Run Continous Integration + unit testing.

ContentLicense: CC BY-SA 3.0

Title: Extreme Programming Daily Commits

Body:
Among many XP activities one of them is RESPECT that states:

Programmers should never commit changes that break compilation, that make existing unit-tests fail, or that otherwise delay the work of their peers

What are the benefits of following this

You know how much work is done & how much is left to be done.

Other ppl have faster access to your code(less waiting time).

Easier to spot if a programmer is doing the task correctly.

Easier and faster code Integration.

Run Continous Integration + unit testing.


答:
The concept of making daily (if not more often) commits comes out of the idea of Continuous Integration.  The basic idea of CI is that developers should be integrating their changes several times a day, thus eliminating the historical norm of every one developing on their own for several weeks, then spending several months trying to integrate all of their different pieces.  Most CI tools automate the process of building code changes and running suites of unit tests, so there is little burden on the developers.

With that as background, here are the direct answers to your questions.

What are the benefits of [committing all changes before going home]?

Allows for Continuous Integration, which is a Good Thing.

Less chance of loosing your work (if using repositories not on your machine)

Having an easy way of rolling back something if you made a mistake

You make changes available to other developers.

What are the risks of not [committing all changes before going home]?

You loose something because of a hard disk crash.

You loose something because you accidentally deleted or modified the wrong thing.

You don't know you have integration issues until it is really hard to solve them.

Your code doesn't get tested in the context of other pieces of the code.

Title: Extreme Programming Daily Commits

Body:
Isn't daily commits (committing all changes before going home) an XP practice?

What are the benefits of following this?

What are the risks of not following this?


答:
Daily??  If you're following that strategy, you should commit far more often than that.

The idea is that you commit early.  Commit often.  Sync often.  Make commits small and easy to review.  Conflicts happen because 2 people are independently working on the same thing - when you work in small fast units the window to conflict becomes much smaller.  And the remaining conflicts are tiny, and easy to resolve because everyone still has mental state.  (As opposed to the industry standard of trying to untangle large conflicts 6 months down the line.)

The disadvantage is that you can be broken by someone else working far from you in the company.  Also it makes it hard to work on branches that may or may not want to go into production.  A lot of people also think that it is not a scaleable strategy - large organizations need more sophisticated practices.

I disagree with that criticism.  For a reference point, at Google all changes must be peer reviewed before being committed.  The average commit is less than 20 lines.  Commits happen straight on the main branch.  There are extensive unit tests and integration tests that automatically run before commits can finish.  (With an infrastructure to make this happen acceptably fast.)  The received wisdom at Google is that these practices successfully scale to Google.  However if you take out any of those pieces, it wouldn't.

Title: Extreme Programming Daily Commits


答:
I think daily commits (or more) are a good software engineering practice in general, regardless of the methodology used. Some benefits (in no particular order)

Tracking time your commit log becomes a great source of "just what did I do on Tuesday" when filling your timesheet just before the deadline the next Monday.

Verification/Sanity check if you're using continuous integration, a check-in will prompt a build which gives you verification that your code (at the very least) doesn't break the build. (Add in TDD and automated smoke tests, and you get even more validity).

Smaller batches working in smaller daily batches, reduces cognitive load. Instead of thinking about all you have to do over this current sprint, you can focus on what you are doing today.

Minimizing the impact of Murphy's law I've had hard drives die for no apparent reason. Think of your check-in as a backup. How much work are you willing to lose when (not if) your local hardware craps out on you?

Some detriments of less frequent check ins

Merging the longer you hold a file and the more files you hold, the more likely that your changes will conflict with another developer's changes requiring a merge

Going dark without check-ins, your team mates have no visibility into what you're doing.

The opposite all the benefits you lose all the benefits of committing daily or more frequently.

Title: Extreme Programming Daily Commits


答:
Breaking up large methods into more focused small methods is definitely a best practice.  You see it as pain in verifying unit test behavior, but you're experiencing the pain in other ways as well.

That said, it is a heresy but I am personally a fan of creating realistic temporary test environments.  That is, rather than mocking out everything that is hidden inside of those other methods, make sure there is an easy to set up temporary environment (complete with private databases and schemas - SQLite may help here) which lets you run all of that stuff.  The responsibility for knowing how to build/tear down that test environment lives with the code that requires that, so that when it changes, you don't have to change all of the unit test code that depended on its existence.

But I do note that this is a heresy on my part.  People who are heavily into unit testing advocate "pure" unit tests and call what I described "integration tests".  I don't personally worry about that distinction.

Title: Brittle unit tests due to need for excessive mocking

Body:
I've been struggling with an increasingly annoying problem regarding our unit tests that we are implementing in my team. We are attempting to add unit tests into legacy code that wasn't well designed and while we haven't had any difficulty with the actual addition of the tests we are starting to struggle with how the tests are turning out.

As an example of the problem let's say you have a method that calls 5 other methods as part of its execution. A test for this method might be to confirm that a behavior occurs as a result of one of these 5 other methods being called. So, because a unit test should fail for one reason and one reason only, you want to eliminate potential issues caused by calling these other 4 methods and mock them out. Great! The unit test executes, the mocked methods are ignored (and their behavior can be confirmed as part of other unit tests), and the verification works.

But there's a new problem - the unit test has intimate knowledge of how you confirmed that behavior and any signature changes to any of those other 4 methods in the future, or any new methods that need to be added to the 'parent method', will result in having to change the unit test to avoid possible failures.

Naturally the problem could be mitigated somewhat by simply having more methods accomplish less behaviors but I was hoping there was perhaps a more elegant solution available.

Here's an example unit test that captures the problem.

As a quick note 'MergeTests' is a unit testing class that inherits from the class we are testing and overrides behavior as needed. This is a 'pattern' we employ in our tests to allow us to override calls to external classes / dependencies.

```<code>[TestMethod]
public void VerifyMergeStopsSpinner()
{
    var mockViewModel = new Mock<MergeTests> { CallBase = true };
    var mockMergeInfo = new MergeInfo(Mock.Of<IClaim>(), Mock.Of<IClaim>(), It.IsAny<bool>());

    mockViewModel.Setup(m => m.ClaimView).Returns(Mock.Of<IClaimView>);
    mockViewModel.Setup(
        m =>
        m.TryMergeClaims(It.IsAny<Func<bool>>(), It.IsAny<IClaim>(), It.IsAny<IClaim>(), It.IsAny<bool>(),
                         It.IsAny<bool>()));
    mockViewModel.Setup(m => m.GetSourceClaimAndTargetClaimByMergeState(It.IsAny<MergeState>())).Returns(mockMergeInfo);
    mockViewModel.Setup(m => m.SwitchToOverviewTab());
    mockViewModel.Setup(m => m.IncrementSaveRequiredNotification());
    mockViewModel.Setup(m => m.OnValidateAndSaveAll(It.IsAny<object>()));
    mockViewModel.Setup(m => m.ProcessPendingActions(It.IsAny<string>()));

    mockViewModel.Object.OnMerge(It.IsAny<MergeState>());

    mockViewModel.Verify(mvm => mvm.StopSpinner(), Times.Once());
}
</code>```

How have the rest of you dealt with this or is there no great 'simple' way of handling it?

Update - I appreciate everyone's feedback. Unfortunately, and it's no surprise really, there doesn't seem to be a great solution, pattern, or practice one can follow in unit testing if the code being tested is poor. I marked the answer that best captured this simple truth.


问:
Do I have to include full gpl v2 license information in my commercial project if I use another author's work?
I would like to use another author's work in my commercial project.

That author's work is licensed under GPL v2.

I'm not using full code. I'm using only part of the code. Also I modified it as per my project needs.

Now i'm confused about what kind of license information I should include.

This is the full license text.

I'm using multiple author's work in functions.php file. Some works are licensed under MIT.

1) Do I have to include full license text in my php file as a comment?

2) Do I have to create separate php file for each author's work.?

答:
Do I have to include full license text in my php file as a comment?

What you need to do is spelled out in the "How to apply these terms to your programs" text of the license text you linked.  It says:

If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these
terms.

To do so, attach the following notices to the program.  It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.

```<code><one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year>  <name of author>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
</code>```

Also add information on how to contact you by electronic and paper
mail.

Do I have to create separate php file for each author's work?

The GPL doesn't require this.

Body:
Do I have to include full license text in my php file as a comment?

What you need to do is spelled out in the "How to apply these terms to your programs" text of the license text you linked.  It says:

If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these
terms.

To do so, attach the following notices to the program.  It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.

<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year>  <name of author>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

Also add information on how to contact you by electronic and paper
mail.

Do I have to create separate php file for each author's work?

The GPL doesn't require this.


问:
Freelancers: How Do You Go About Gathering Requirements?
As a freelance programmer:

What is your process for gathering requirements from a client?

How much time does the requirement gathering process take you? I know this is not fixed, and there are variables such as how prompt the client is in responding and such. In general, accounting for the delay in responses and such, how long does it take to get to the final requirement?

Which communication channel (email, phone, instant messenger, other) do you use to gather these requirements?

Do you charge for the time spent in requirements gathering?

Are there any deliverables in your requirements gathering process? If so what are they?

答:
1.What is your process for gathering requirements from a client?

interview, whiteboard, conference call, shop tour, worker observation, staff interviews, meetings, etc. - whatever is appropriate, whatever it takes to understand the real problem, balanced with whatever they're amenable to and will make time for

2.How much time does the requirement gathering process take you? I know this is not fixed, and there are variables such as how prompt the client is in responding and such. In general, accounting for the delay in responses and such, how long does it take to get to the final requirement?

obviously this depends on the size of the project. it's not unusual to spend 20 hours in requirements and modeling for a very small project (< 100 hours), because you have to understand the business context well enough to peel back the layers of the problems the customer is presenting in order to get to the real problem that you'll have to solve to make them happy

whether that 20 hours is two calendar days or six weeks depends on customer responsiveness and availability, and how much thinking you have to do in between session (for hard problems)

3.Which communication channel (email, phone, instant messenger, other) do you use to gather these requirements?

all of them

4.Do you charge for the time spent in requirements gathering?

Hell, yes!

You have to understand the client's business, comprehend and document their problems, and propose solutions which they could then take to someone else to implement. This is the consulting part of the process, and consultants don't work for free.

5.Are there any deliverables in your requirements gathering process? If so what are they?

Typically, a draft list of features, user stories, test-case descriptions, an abbreviated work-breakdown structure (with project estimates), a highlighted list of unclear/unknown areas/items for further discussion/investigation, and a list of things (info, resources, tools, access, etc.) that you will require from the client with target dates. All of this is packaged as a proposal with some background info on the business, the methods used to identify the problems, constraints and caveats about the solution, notes about the expected timelines and ROI, and a request for follow-up by a specific date.

ContentLicense: CC BY-SA 2.5


问:
What is Java's primary focus? Why does it take so long to get new features?
I have been exploring the new features in the JDK8, like the lambda expressions, the extension methods, and the new stream API.

Evidently none of these features are new in the programming world and that made wonder why are getting all these things in Java until now.

We had lambda expressions in Lisp (1958), SML (1973), Haskell (1990), Python (1991), JavaScript (1994), Ruby (1995), Scala (2003), C# (2007) and 55 years after Lisp and practically everyone else, in Java (2013).

And I had read about streams in SIC (1996).

I have been wondering why now? The evidence suggests that competing with other languages is not the motivation.

It would seem that all the cool new features in this release of Java are just a by-product of implementing parallelism. We have lambdas because they make writing parallel algorithms simpler, and we have extension methods because we needed them to give support to the changes that lambda expressions required, etc., etc.

So, my question is: can we safely affirm that the main topic in this upcoming release of Java is actually parallelism? Or can we justify other reasons for the appearance of the oldest tricks in the book until now in Java?

答:
Evidently none of these features are new in the programming world and
  that made wonder why are getting all these things in Java until now.

Because Java has to go through an approval process that involves several high-visibility stakeholders in a process akin to "design by committee", and that process takes time, that's all.

Contrast that with other languages and you'll find either a benevolent dictator, or a small committee of language designers who work closely together and are not tied to corporate interests.

Combine that with an established code base of millions of lines of Java code that has to stay backwards compatible, and you have all the ingredients for change at a glacial pace.


问:
System evolution approach: gradual or cataclysmic?
When working on established systems, I've often found ways to improve a user interface to maximize user efficiency (example: legacy app maintenance screen doesn't allow multi-line operations where doing so could save the user hours per week over the "edit one line at a time" existing method).

The challenge I encounter is that a GUI improvement is something that I can't apply universally to a system without a more sizable programming effort (an effort that management is unlikely to approve).

I then struggle with the decision to either try and add the improvement in one screen, or give up on it until the mythical project is someday approved.

Not doing it means the system is not enabling users to be most efficient, and this bothers me, as if I have purposely allowed a speed throttle on them that they don't deserve.

But doing it means that now there is some inconsistency in the system GUI. Not all the screens will work the same way, which can create potential confusion.

Given your knowledge and experience, what is the best practice here? Consistency or efficiency?

Update

Thank you everyone for your answers. I have upvoted all your thoughtful responses. I am familiar with the Spolsky article Robert Harvey links to. The change I refer to is less risky than the whole rewrite of the Netscape example it refers to, but the article is pertinent and always a good read.

This question was spurred by a time when I found a way to make it possible for users to process items for invoicing faster, so it could be argued that it improved revenue efficiency, however, as many of you ascertained, it was not critical.  The stakeholders succumbed to the decision-consequence gap, and as they were not the ones having to do the data entry were more than happy to let the users languish with the inefficient GUI. However, the craftsman in me wants continuous improvement, and as Justin Cave says, if you do nothing, your system is not evolving, it is stagnating.

In some other cases I've done what is suggested in the responses and tried a grassroots approach, having users push for change through their stakeholder representatives. I found that approval for incremental changes that broke consistency have depended on accessibility to end-users, a willing stakeholder in the change request meetings and strangely enough, IT leaders that either cared very much or not at all about the application in question. Those that cared empathized with the end-user's pain and those that didn't were comfortable leaving the decision to subordinates that did. The moderately concerned would glom onto the consistency principle which is arguably laudable but to which rigid adherence without a plan for change means stagnation.

答:
Your goals may differ from those of the stakeholders.

The stakeholders may already be familiar with the interface and its quirks.  Changing it, no matter the improvement, will initially add friction, and require a training effort.

The software is already making money for the organization.  Any change to the software eventually needs to translate to potential improvements in future revenue.

Most system evolution is gradual.  For a cautionary tale about cataclysmic change, see here:

http://www.joelonsoftware.com/articles/fog0000000069.html


答:
Ask the application owners.

More than likely, if you can save each user multiple hours every week and the trade-off is that they need to train the users to understand that different screens work slightly differently, they'll jump all over the request.  At a cost of, say, half an hour of training per user, they'd gain many times that investment in productivity in the first week.  That's a pretty easy decision for management to make.  Most likely, they'll then push to make the same change on a number of other screens.  Perhaps they'll want to make the change to half a dozen screens before rolling the change and the training out to the users.

In the vast majority of established applications, you'll see traces of different approaches to development and to designing the user interface.  It makes little sense to remain consistent with the first screen that was built when new techniques, new architectures, and new information about how the users actually interact with the system have given you better ways of building things.  On the other hand, it often makes little sense to invest the time to rewrite years of work just because you've found an incrementally better approach.  Waiting for the "big bang" project that will likely never come means, in practice, that nothing ever gets better.

Title: System evolution approach: gradual or cataclysmic?

Body:
When working on established systems, I've often found ways to improve a user interface to maximize user efficiency (example: legacy app maintenance screen doesn't allow multi-line operations where doing so could save the user hours per week over the "edit one line at a time" existing method).

The challenge I encounter is that a GUI improvement is something that I can't apply universally to a system without a more sizable programming effort (an effort that management is unlikely to approve).

I then struggle with the decision to either try and add the improvement in one screen, or give up on it until the mythical project is someday approved.

Not doing it means the system is not enabling users to be most efficient, and this bothers me, as if I have purposely allowed a speed throttle on them that they don't deserve.

But doing it means that now there is some inconsistency in the system GUI. Not all the screens will work the same way, which can create potential confusion.

Given your knowledge and experience, what is the best practice here? Consistency or efficiency?

Update

Thank you everyone for your answers. I have upvoted all your thoughtful responses. I am familiar with the Spolsky article Robert Harvey links to. The change I refer to is less risky than the whole rewrite of the Netscape example it refers to, but the article is pertinent and always a good read.

This question was spurred by a time when I found a way to make it possible for users to process items for invoicing faster, so it could be argued that it improved revenue efficiency, however, as many of you ascertained, it was not critical.  The stakeholders succumbed to the decision-consequence gap, and as they were not the ones having to do the data entry were more than happy to let the users languish with the inefficient GUI. However, the craftsman in me wants continuous improvement, and as Justin Cave says, if you do nothing, your system is not evolving, it is stagnating.

In some other cases I've done what is suggested in the responses and tried a grassroots approach, having users push for change through their stakeholder representatives. I found that approval for incremental changes that broke consistency have depended on accessibility to end-users, a willing stakeholder in the change request meetings and strangely enough, IT leaders that either cared very much or not at all about the application in question. Those that cared empathized with the end-user's pain and those that didn't were comfortable leaving the decision to subordinates that did. The moderately concerned would glom onto the consistency principle which is arguably laudable but to which rigid adherence without a plan for change means stagnation.


问:
Storing in-text metadata in a discrete data structure
I am developing an application which will need to store inline, intext metadata. What I mean by that is the following: let's say we have a long text, and we want to store some metadata connected with a specific word, or sentence of the text.

What would be the best way to store this information?

My first thought was to include in the text some kind of Markdown syntax that would then be parsed on retrieving. Something looking like this:

```<code>Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diam __nonummy nibh__[@note this sounds really funny latin]
euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
</code>```

This would introduce two problems I can think of:

A relatively small one, is that if said syntax happen to be fortuitously on the said text, it can mess with the parsing.

The most important one is that this doesn't maintain this metadata separate from the text itself.

I would like to have a discrete data structure to hold this data, such a different DB Table in which these metadatas are stored, so that I could use them in discrete ways: querying, statistics, sorting, and so on.

EDIT: Since the answerer deleted his answer, I think it might be good to add his suggestion here, since it was a workable suggestion that expanded on this first concept. The poster suggested to use a similar syntax, but to link the metadata to the PRIMARY KEY of the metadata database table.

Something that would look like this:

```<code>Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diam __nonummy nibh__[15432]
euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
</code>```

Where 15432 would be the ID of a table row containing the necessary, queriable information, as per example below.

My second thought was to store information of this kind in a DB Table looking like this:

```<code>TABLE: metadata

ID    TEXT_ID    TYPE    OFFSET_START    OFFSET_END    CONTENT
1     lipsum     note    68              79            this sounds really funny latin
</code>```

In this way the metadata would have a unique id, a text_id as a foreign key connected to table storing the texts and it would connect the data with the text itself by using a simple character offset range.

This would do the trick of keeping the data separated from the metadata, but a problem that I can immediately see with this approach is that the text would be fundamentally not editable. Or, if I wanted to implement the editing of the text after the assignation of metadata, I would basically have to calculate characters additions, or removal compared to the previous version, and check whether each of this modifications adds or remove characters before or after each of the associated metadata.

Which, to me, sounds like a really unelegant approach.

Do you have any pointers or suggestions for how I could approach the problem?

Edit 2: some XML problems

Adding another case which would make quite necessary for this separation of data and metadata to happen.

Let's say I want to make it possible for different users to have different metadata sets of the same text, with or without the possibility of each user actually displaying the other user metadata.

Any solution of the markdown kind (or HTML, or XML) would be difficult to implement at this point. The only solution in this case that I could think about would be to have yet another DB Table which would contain the single user version of the original text, connecting to the original text table by the use of a FOREIGN KEY.

Not sure if this is very elegant either.

XML has a hierarchical data model: any element which happens to be within the borders of another element is considered as its child, which is most often not the case in the data model I'm looking for; in XML any children element must be closed before the parent tag can be closed, allowing for no overlapping of elements.

Example:

<note content="the beginning of the famous placeholder"> Lorem ipsum
  dolor sit <comment content="I like the sound of amet/elit"> amet </note>,
  consectetuer adipiscing elit </comment> , <note content="adversative?"> sed
  diam <note content="funny latin"> nonummy </note> nibh </note> euismod
  tincidunt ut laoreet dolore magna aliquam erat volutpat.

Here we have two different problems:

Different elements overlapping: The first comment starts within the first note, but ends after the end of the first note, i.e. it's not its child.

Same elements overlapping: The last note and the boldfaced note overlap; however, since they are the same kind of element, the parser would close the lastly opened element at the first closure, and the first opened element at the last closure, which, in this circumstance, is not what is intended.

答:
The Solution Use Case

I disagree with some of the other answers, simply because, while great solutions, they are probably not your solution. Yes XML has the word markup in it's acronym, but it is probably not ideal for your situation. It is way too complex, it offers little assistance in keeping the meta data separate from the original text. Essentially it will turn everything into a form of metadata, creating one overweight data-set.

Since there is likely no absolutely correct solution or approach, the best solution answers the question:

How will the data be used by the system?

Also, if you try and ask, how a solution design could inherently add to the value of the system, in the way that it will be used, then you are closer to finding your elegant answer.

Understanding the problem

Ok enough commentary, let's dig into the problem.
This is the problem as I understand it is (obviously adding to this will be beneficial):

There is an original text

Assumptions about this original text:

This text, may or may not be made up of several independent documents

This text, may or may not be edited by one or more users

This text, contains related information. By that I am assuming (correct me if I am wrong) that the metadata is  related and not descriptive. So it stores information related to the original text, and not information that describes the text. So it will store notes about the original text, and not by example describe that the text is a heading that is bold and is a link to a website, etc.

The text should be easily filtered distinct from the metadata

The text should be protected from being corrupted by, and corrupting the metadata

There should be a means of storing information related to the original text (metadata)

This metadata also needs it's own (meta)metadata, that would hold information such as which user's (or groups?) the meta data is relevant for, such as a description of the metadata, say weather it is a note, or comment, or description etc.

This metadata (and it's (meta)metadata) need to withstand alterations in the original text, alterations of the metadata and alterations of the (meta)meta data

The metadata (+ Meta-Metadata) needs to be structured well and easily queried, and indexed or even joined in a relational way to other datasets. The relational nature of the metadata should not just be limited to Queries, but also facilitate updates or write back and alteration of the metadata as a result of the relational data activities.

The value of the metadata (+ Meta-Metadata) is in it's very related nature. It becomes immediately counter productive the moment it looses it's relation to the original text. Thus the integrity of it's relation to the original text is a mandatory design imperative.

Other assumptions about the nature of the problem and how it will be used are:

Concurrent heterogeneous system access. That is to say that the user's may wish to view the text and edit metadata, at the same time as the administrator (or another process) is performing relational data queries on the structured metadata.

The system will have several users

The system is modern. That is to say that it is not constrained by storage space, or processing speed, or real-time imperatives. The integrity and purpose focused functionality is a higher priority than physical computing resource limitations.

There is a (albeit low) chance that the uses and functionality of the system may evolve or change somewhat, as the system is used.

Building the solution design

Understanding the problem as I have outlined it above, I will now start to suggest possible solutions and approaches that aims to solve the above problem.

Components

So I would see that there would need to be a custom built user access system. It would filter relevant and irrelevant metadata from the original text. It would facilitate the editing and viewing of metadata into the text. It would ensure the integrity of the relationship between the metadata and it's original text. It would structure the metadata and offer a data source to a relational data system. It will most likely provide a host of other purpose driven functions.

Structure

So since it is important to keep the metadata's integrity to the original text, the best way of ensuring this, is to keep the metadata inline with the original text. This will offer the benefit that the original data can be confidently edited without breaking this integrity.

The concerns with this approach are the corruption of the metadata by the original data and vice versa. The adequate indexing and structuring of the metadata and it's (meta)metadata in a way that allows for queries and updates and efficient access. The easy filtering of metadata from the original text.

With this in mind, I would suggest that a portion of the solution be based on the approach of using ESCAPE CHARACTERS within the original text. This is not the same as designing your own Mark-up Language or using an existing Markup Language such as XML or HTML. It is easy to design an ESCAPE CHARACTER that has a zero, or near zero chance of existing in the original text.

My advice to you in this regard would be to carefully consider the
  original data, and try and determine the nature of the code-page that
  it is stored in and then look for an ideal CHARACTER or
  CHARACTER SEQUENCE that is unlikely or impossible to occur. For example in ASCII there are literally built-in control characters with
  byte values that are never used in standard user interfaces. The same
  can be said for a font based or relational data based information
  system. Just be careful with binary data codecs. Depending on the nature of the
  original data it may be valuable to build a parser that confirms the discovery of a
  control sequence, perhaps by looking at the data that is escaped and verifying it's
  integrity, either with a simple inspection of the structure of the escaped data, or
  even by including a control character that is calculated for each escaped data
  sequence.

Example Data With Escape Sequences

This is a story of a man. >>>>(#)Why is this story about a man not a woman?(#)()userid::77367()Manager's Comment()DataID::234234234>>>> A man who went to mow a meadow, went to mow a meadow. The man went with his dog >>>>(#)Ask the client if the story would be better with a cat instead(#)>>>> to mow the meadow. So now this is the story of a man and his dog who went to mow a meadow.

One man and his dog, went to mow a meadow, went to mow a meadow, a meadow reached over the mountain. >>>>(#)This sounds alot better with a forest(**)Suggestion Note(#)>>>>

The man and his dog and his mission, to mow a meadow, a meadow reached over the mountain is only reached when crossing the river.

Example Data Without Escape Sequences

This is a story of a man. A man who went to mow a meadow, went to mow a meadow. The man went with his dog to mow the meadow. So now this is the story of a man and his dog who went to mow a meadow.

One man and his dog, went to mow a meadow, went to mow a meadow, a meadow reached over the mountain.

The man and his dog and his mission, to mow a meadow, a meadow reached over the mountain is only reached when crossing the river.

Obviously this is easily parsed, not complex as an entire Mark-up language and easily adaptable to your purpose.

Solved Yet?
Well, I would say no. Our solution still has some holes. The indexing and structured access of this data is poor. Also, it would not be reasonable to query this file (or several files) at the same time as editing it.

How could we solve that problem?

I would suggest a DATA ALLOCATION TABLE as a document header. I would also suggest implementing a TRANSACTIONAL TABLE UPDATE QUEUE. Let me explain. The designers of a file system, particularly a rotational disk file system, faced similar design challenges to the ones you have described above. They needed to embed information about the files on the disk with, along with the data. A great solution to the relationship integrity of this data, was to DUPLICATE it in a File Allocation Table (FAT).

This means that for each individual Metadata Item, there is a corresponding entry in the Data Allocation Table. So it is fast, structured and relational, and independent of the original data. If queries or joins or updates need to be performed on the metadata, then it is easily done by simply accessing the Data Allocation Table.

Obviously care must be given to ensure that the original in-line metadata is a true reflection of the Data Allocation Table data. That is where a Transactional Table Update Queue comes in. Every change, addition or removal of metadata, is made not on the data it'self, but rather on the queue. the queue will then ensure that either all the changes are made to both the in-line and table data, or no change is made at all. It also allows for asynchronous updates to be performed, for example, all the metadata of a certain user can be deleted by running a delete command on the queue. If the inline metadata was locked and in use, the queue would not perform any changes until it could do it to both the Table data and the inline data.


答:
In my (limited) experience, if you have to apply a cataclysmic change, you're dealing with a design oversight of some kind, which makes it more dangerous than most people would already portray it as.  I am reading your case as a 'would be nice' feature, instead of a 'must have immediately!' feature, so lucky you, this one shouldn't be too painful...of course, your manager might not agree, because of that.

Because of this, I would go with the gradual approach.  Try making the GUI control a multiline, but before you commit, get some stockholder input, and see if they want this everywhere, and an unofficial estimate of just how much time they expect to save with a multiline control, or if it even will save them any time.  If they like it, they'll probably out-and-out tell you a few more places that this would save them effort instead of just 'everywhere' (though, they could say that too.)  Either way, you're going to be expending effort to make this happen, and a prototypical upgrade will save you the trouble of figuring out the gotchas of making this GUI change.


答:
Java has changed focus with time. At first it was designed as a simple powerful language, as a reaction to "powerful complex" C++. Some features that were in C++ were intentionally left out, like operator overloading, templates, enums, that were deemed too complicated or relics of the C era, and OOP being at the peak of its popularity, everything was made an Object in a single-paradigm worldview. Lambdas at this time were considered simply "not needed" since introduction of anonymous/inner classes in Java 1.1. The fact that the syntax was far more verbose was almost considered a feature.

Java having found its public, there were no incentive to change until Microsoft introduction of C#, which learned from the lessons of Java design mistakes, and launched a series of new language features. They were not constrained by backwards compatiblity. I think that Java conceptors realized the danger of C# competition and release Java 5 with generics, enums, etc.

Inclusion of lambdas in Java is discussed since that time and it was only exacerbated by the current trend for functional programming. But things like this are slow to make right, and it has to be right the first time. In my opinion Java botched generics with type erasure because backwards compatibility was considered a reason to implement it as no more than syntactic sugar. Closures has been thought up more thoroughly, it appears, and it will be more than just syntactic sugar.

In conclusion, what is Java 8 main topic? I don't think a language version has a topic. As C++11, the Java 8's raison d'etre is to keep up with the competition by introducing in the language the things that more and more programmers are taking for granted now. Lisp may have lambda since 1958, its popularity has plateaued for decades now and only recently functional programming has been considered seriously for "mainstream" programming (for lack of a better word).

Title: What is Java's primary focus? Why does it take so long to get new features?

Body:
I have been exploring the new features in the JDK8, like the lambda expressions, the extension methods, and the new stream API.

Evidently none of these features are new in the programming world and that made wonder why are getting all these things in Java until now.

We had lambda expressions in Lisp (1958), SML (1973), Haskell (1990), Python (1991), JavaScript (1994), Ruby (1995), Scala (2003), C# (2007) and 55 years after Lisp and practically everyone else, in Java (2013).

And I had read about streams in SIC (1996).

I have been wondering why now? The evidence suggests that competing with other languages is not the motivation.

It would seem that all the cool new features in this release of Java are just a by-product of implementing parallelism. We have lambdas because they make writing parallel algorithms simpler, and we have extension methods because we needed them to give support to the changes that lambda expressions required, etc., etc.

So, my question is: can we safely affirm that the main topic in this upcoming release of Java is actually parallelism? Or can we justify other reasons for the appearance of the oldest tricks in the book until now in Java?


问:
Pros and cons of sending data from android to php in string and/or json
I have seen some tutorials and some stackoverflow discussions on how to send data from android to php page. most of the cases i have seen people sending data in string format but returning in json format. My question is that is it better to send data from android to php in json format. or in what cases i should use json over string.

Regards

答:
Why do most people send just a string to a php page, instead of a JSON object?  Probably because they just need to send one item of data; usually it's an ID, like a Customer ID.

JSON is a structured data format.  It works seamlessly with  Javascript, and benefits from the easy availability of well-written parsers.  It is used to return an object containing fields of data.

To do the same things as JSON does using strings, you would have to

Come up with a data format,

Write a parser for it, and

Map the resulting data fields to Javascript variables.

All that work is already done for you, if you choose JSON.  That's why it is used to return, for example, a customer record from a database.


答:
When Java was first designed it was considered appropriate to leave out anonymous functions. I can think of two reasons (but they might be different from the official ones):

Java was designed as an object-oriented language without functions, so it was not very natural to have anonymous functions in a language without functions. Or at least, this would have influenced the design of the language a lot.

Anonymous functions were not popular in the programmer communities that Java was meant to attract (C, C++, Pascal?). Even now, many Java programmers seem to consider these features quite exotic (but this will probably change very quickly with Java 8).

In the following years, as Robert Harvey has explained, the policy of Sun was always to keep Java backward compatible and very stable.

On the other hand, other competing languages have emerged (the most important being C#, which was born as a Java clone and then took its own development direction).

Competing languages have put Java under pressure for two reasons:

Expressive power

New features can make certain programming idioms easier to write, making the language more attractive for programmers. Normally the set of features provided by a language is a compromise between expressive power, language complexity, design coherence: adding more features makes a language more expressive but also more complex and difficult to master.

Anyway, in the last few years Java's competitors added lots of new features that Java did not have, and this can be considered an advantage.

Hype

Yes, unfortunately this is a factor in technology choice, at least from what I can see in my daily experience as a programmer: a tool must have a certain feature, even if most members of the team don't know how to use it and those who would be able to use it don't need it most of the times.

Hype can be even more important for non technical people like managers, who may be the ones who decide the platform for a certain project. Managers sometimes only remember some keywords like lambda, parallelism, multicore, functional programming, cloud computing, ... If our technology of choice has a green mark
on each item of the list, then we are up to date.

So IMO for some time Java has been caught between

the original policy of language stability and design simplicity, a huge code base and developer community on one hand, and

the pressure of competing languages that could attract Java programmers, C# at first, and then Scala, Clojure, F# (I name the ones I am aware of, there may be others).

Eventually Oracle decided to upgrade Java to make it more competitive.
In my opinion, the new features address especially Java programmers that might be tempted to switch to C# and who see other languages like Scala and Clojure as too different from Java.
On the other hand, developers who have some experience with functional programming and still want to use the JVM have probably already switched to
Scala, Clojure, or another language.

So the new Java 8 features will make Java more powerful as a language and the declared focus is concurrent and parallel programming, but the upgrade seems to address also the marketing aspects (Mark Reinhold, chief architect for Java at Oracle, said: "Some would say adding Lambda expressions is just to keep up with the cool kids, and there’s some truth in that, but the real reason is multicore processors; the best way to handle them is with Lambda", see this article).

So, yes, many (all) Java 8 features were well known already, but why and when a feature is added to a language depends on many factors: target audience,
existing community, existing code base, competitors, marketing, etc.

EDIT

A short note regarding "... I had read about streams in SIC (1996).": do you mean that you need Java 8 lambdas to implement streams? Actually you can implement them using anonymous inner classes.


问:
NoSQL Document Database as a Message Queue
I am considering using a NoSQL Document database as a messaging queue.

Here is why:

I want a client to post a message (some serialized object) to the server and not have to wait for a synchronous response.

I want to be able to pull messages off of the "queue" based on some criteria, which may be more sophisticated than just a priority level (I am working on a hosted web app, so I want to give all of my customers a fair amount of "computing time", and not let one customer hog all of the processing).

I want the queue to be durable - if the server goes down I want any remaining messages to be handled when it comes back up.

So, I am considering using MongoDB or RavenDB as a message queue.  The client can post the message object to a web service which writes it to the database.  Then - the service doing the work can pull the various message types based on any criteria that may arise.  I can create indexes around the scenarios to make it faster.

So - I am looking for someone to shoot a hole in this.  Has anybody successfully done this?  Has anybody tried this and failed in some way?

答:
There are no visible holes in it as your requirements list is pretty short :-). Basically the longer the requirements list the bigger the chances to find holes in writing your own.

In my opinion, using a NoSQL database for this scenario would fit:

if the requirements are not for a full featured queue

if the app will not have to move from the pull model to a push model (queue v pub/sub)

the structure of the messages is pretty variable and changes over time

the app needs to pull messages based on different criteria

reusing the NoSQL database would reduce the number of systems the app would depend on

As a side note, I'd (biasedly) encourage you to also take a look at RethinkDB.


答:
I feel obliged to point out that "committing all changes before going home" is not necessarily something that you should do. When your mind has already called it a day, you risk breaking the build by committing something and then quickly leaving the office. Consider these questions first:

Did you compile the code?

Did you run all the unit tests of the project to make sure everything works?

Were you working on your very own branch instead of the master?

Are you using a tool that shows if you forgot to include something in your commit?

Having broken the build many enough times and having seen others do it many enough times as well, I do not commit my changes before going home if I answer "no" to any of those questions. All this being said, I'm all in favor of micro commits and committing at least hourly. Just don't commit if you've already put your coat on.

Title: Extreme Programming Daily Commits


问:
Testing Java as Groovy
We're looking into improving our testing process for some of our applications, and the idea that's been put forward it to write unit tests in Groovy and automatically run them with Maven.

The first application to get this treatment is written in Java, but I've been told that some of the classes being tested need to be renamed as ".groovy" files * and they will then be compiled as Groovy classes, not as Java classes. This renaming can all be done using Maven and Ant, but I'm concerned that if we compile our classes as Groovy classes, we're not actually testing them as Java classes, which is how they will be deployed.

During the demo, it was also shown that Groovy does not compile to the same bytecode that Java does. There was at least one Groovy-specific member that was added, called "metaClass" (there were others, I didn't get a good enough look at the screen). Apparently this is necessary for the testing framework they want to use, which is named Spock.

Am I right to be concerned about this? Does it matter?

  * I've been told this is for technical reasons that have to do with the way the application is built. I've been told that this does not apply to all classes, but to some.

答:
The information that they gave to you is wrong.

Groovy can interact with Java classes without problems. In fact, most the the Spock core classes are Java (.java) not Groovy classes.

The problems about testing Java classes with Groovy are caused by Maven (well not Maven directly, but by the plugins in charge of compilation and execution of tests).

For some reason the interaction between the Groovy plugin and the Maven test plugin (surefire) sucks. (See the UPDATE it doesn't sucks in recent versions of GMaven).

I ran into this problem a long time ago. Which probably is caused by how the classloader for the test execution is setup.
I can't give you an answer on how to fix that Maven Groovy/Surefire compatibility problem, because at that time instead of trying to fix it I took the fastest path for me: I switched from Maven to Gradle.

And about the differences on compilation... don't do the .java to .groovy rename.
Groovy is a wonderful dynamic language, and is easy to migrate from Java to Groovy. But is not exactly the same language, there are some little differences to take into account: http://groovy.codehaus.org/Differences+from+Java

So if you rename the files automatically for the testing, there is a good chance that some things doesn't compile like array literals, or they compile with a different semantic like ==.

So my advice is: Spock is a wonderful framework for testing, and Groovy can save you a lot of repetitive code. You can migrate all to Groovy, but that decision requires more evaluation of the technical aspects of your project. If you want to use Groovy for tests and Java for main classes, look for a solution in the Maven community or try switching your build to Gradle.

Additional note: The Groovy compiler is a superset of the Java compiler. When a .java file is given to the Groovy compiler it uses the Java compiler. It means that you can mix .java and .groovy in the same directory and use the Groovy compiler to compile all. Maybe that setup helps you with the Maven test plugin.

UPDATE: Recently I've to work with a project using Spock and Maven 3. Making both to coexist is easier than before :)

First, configure your GMaven plugin, you can see an example here: https://gist.github.com/xlson/1253342

Then you have to configure Surefire so it sees your test classes:

```<code>        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.14</version>
            <configuration>
                <includes>
                    <include>%regex[.*Spec.*]</include>
                </includes>
            </configuration>
        </plugin>
</code>```

The trick is in the regex expression since according to Surefire docs it applies to classes (if you don't use regex it applies to source files).

Title: Testing Java as Groovy

Body:
The information that they gave to you is wrong.

Groovy can interact with Java classes without problems. In fact, most the the Spock core classes are Java (.java) not Groovy classes.

The problems about testing Java classes with Groovy are caused by Maven (well not Maven directly, but by the plugins in charge of compilation and execution of tests).

For some reason the interaction between the Groovy plugin and the Maven test plugin (surefire) sucks. (See the UPDATE it doesn't sucks in recent versions of GMaven).

I ran into this problem a long time ago. Which probably is caused by how the classloader for the test execution is setup.
I can't give you an answer on how to fix that Maven Groovy/Surefire compatibility problem, because at that time instead of trying to fix it I took the fastest path for me: I switched from Maven to Gradle.

And about the differences on compilation... don't do the .java to .groovy rename.
Groovy is a wonderful dynamic language, and is easy to migrate from Java to Groovy. But is not exactly the same language, there are some little differences to take into account: http://groovy.codehaus.org/Differences+from+Java

So if you rename the files automatically for the testing, there is a good chance that some things doesn't compile like array literals, or they compile with a different semantic like ==.

So my advice is: Spock is a wonderful framework for testing, and Groovy can save you a lot of repetitive code. You can migrate all to Groovy, but that decision requires more evaluation of the technical aspects of your project. If you want to use Groovy for tests and Java for main classes, look for a solution in the Maven community or try switching your build to Gradle.

Additional note: The Groovy compiler is a superset of the Java compiler. When a .java file is given to the Groovy compiler it uses the Java compiler. It means that you can mix .java and .groovy in the same directory and use the Groovy compiler to compile all. Maybe that setup helps you with the Maven test plugin.

UPDATE: Recently I've to work with a project using Spock and Maven 3. Making both to coexist is easier than before :)

First, configure your GMaven plugin, you can see an example here: https://gist.github.com/xlson/1253342

Then you have to configure Surefire so it sees your test classes:

        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.14</version>
            <configuration>
                <includes>
                    <include>%regex[.*Spec.*]</include>
                </includes>
            </configuration>
        </plugin>

The trick is in the regex expression since according to Surefire docs it applies to classes (if you don't use regex it applies to source files).


答:
I would consider easing on the mocks and just formulate tests that might include the methods it calls out to.

Don't test the how, test the what. It's the result that matters, include the sub-methods if need be.

From another angle you could formulate a test, make it pass with one big method, refactor and end up with a tree of methods after refactoring. You don't need to test each and every one of them in isolation. It's the end result that counts.

If the sub methods make it hard to test some aspects consider breaking them out to separate classes so you can mock them there more cleanly without your class under test being heavily instrumented/seamed. It's kind of hard to tell if you are actually testing any concrete implementation in your example test.


问:
How to deal with necessary workarounds in software?
Even with detailed specifications for exchanging data between computer programs, chances are that data generated by older versions of programs do not conform 100% to the specification, or that they use old obscure features which few people know how to implement them. As a result, companies have to keep a library of "odd specimens" for testing their software.

In good software design, these quirks can be confined into a small layer of libraries called "Abstraction Layer". However, most abstraction layers cut off too many functionalities (in order to prevent the higher level software from touching the unstable parts of the lower level software).

Sometimes it is not possible to completely hide away the unstable parts. What are the strategies for coping with such necessary workarounds?

((Please pardon my poor language skills.)

答: Unit/integration tests can be used to identify those 'features' (if they can't be removed). Likewise, the same kinds of tests can test the code that consumes the libraries in question to ensure that they satisfy the contract of mis-behaviour.


问:
What non-theoretical, practical programming language has no reserved keywords?
I have been searching for a practical programming language that has no reserved keywords but I haven't had any luck finding one.

I am working on a programming language for my own edification and entertainment and I haven't needed to include any keywords yet, that is what led me to my search and the question:

I don't see convenience to the compiler writer as being important to the end user of the language. Computers are powerful enough now days to be able to infer meanings from context. No more than a writer needs to label nouns, verbs and such when writing a novel, why should a programmer have to label functions and variables with function x() {} or set x = 1 or var x = 1 etc; when I can infer from the context of the statements that it is a function declaration or an invocation or that a label is an assignment to a value or a reference to that labels value?

Here is a concrete example of what my current parser is doing, no need for reserved keywords to support common things that would normally have a bunch of noise like func or function or dec or what not.

Function Declaration

```<code>sum3(a,b,c) -> a + b + c.
</code>```

Function Invocation

```<code>x = sum3(1,2,3).
</code>```

Anonymous Function Named x

```<code>x = (a,b,c) -> a + b + c.
y = x(1,2,3).
</code>```

I would like to know why are keywords that important to a successful programming language?

答:
You seem to be laboring under a false assumption, that keywords are there to make things easier for the compiler.  While they do make things easier for the compiler, they have a much more important benefit: they make things easier for the person reading the code.

Yes, you could look at a bunch of context and figure out that you're looking at a function declaration or a variable declaration, but depending on the context and on the complexity of the code involved, that could take a long time to figure out.  Or, you could have a handy keyword there like function or var, and you know immediately what you're looking at.

Yes, they make the code more complicated to write, but considering that programs spend a lot more time in maintenance than in production, and that their code is read, debugged and maintained a lot more than it is created, trying to design your language to make it easy to write instead of easy to read is a classic case of harmful premature optimization.

Also, don't disdain concepts that make the compiler's job easier.  The easier it is to parse, the faster your code will compile, which means edit-build-debug cycles get faster, which means your productivity goes up.

When you have a large, complex codebase, that can amount to a non-trivial difference in productivity.  For example, where I work we have a program at work that's about 4 million lines of Delphi.  We have another module that's about 300,000 lines of C++.  They both take about the same length of time to compile. (About 3 minutes.)  If we had 4 million lines of C++, it could take an hour to build!


问:
How to implement two different web services running the same business logic?
We are developing a ReSTful HTTP API with Servicestack(.NET).

Some of the API clients, which have a subset of the actions, demand a service which could be easily added with all the entities, like with the "Add Service Reference" with Visual Studio.

We are thinking about creating a simple asmx web service, but our thoughts on how to do it divide to two:

The asmx WS will call the ServiceStack service with an HTTP client.

The asmx WS will reference the same BL that the main service references and will call it directly.

Which is the preffered way?

Is there any other way you would recommend?

答: If your API has a layer of user facing view/edit models you could put them in a separate assembly and provide your client a simple .NET based driver/bridge/wrapper that uses the same models and invokes your API for them. This way you basically give them what asmx binding code generates for you.</p>


问:
I/O interface for portable library
I'm writing a new .NET portable library that can read a file format, and file I/O is not available for portable libraries. The article "How to Make Portable Class Libraries Work for You" states that I'll have to write platform-specific libraries to solve this for each platform. That's not a problem.

So, I'll have the file format reading code in the portable library, and then some platform-specific libraries to help with platform-specific I/O: one platform-specific library will be written using traditional traditional .NET System.IO.File, whereas the Windows Store apps-library will use Windows.Storage and the Windows Phone one will use System.IO.IsolatedStorage. I know WinRT is all asynchronous, but I only have experience with the traditional .NET File and synchronous I/O.

Each of these platform-specific libraries will reference the portable library and provide the file format reader with a platform-specific implementation of a data source object:

```<code>ISomeDataSource dataSource = new WinRTDataSource(@"filename.bin");
var reader = new FileFormatReader(dataSource);
</code>```

How do I design the interface (e.g. the ISomeDataSource above) such that I can read and seek within the data? But then platform-agnostic and maybe even asynchronous?

I have access to all features of Visual Studio 2012 and C# 5 (async and await), but since I'm not on Windows 8 I cannot try WinRT.

答:
I don't see anything hard about this, you can just create the interface (using asynchronous method for Read()) and then implement it three times:

```<code>interface ISomeDataSource
{
    Task<int> ReadAsync(byte[] buffer, int offset, int count);
    long Seek(long offset, SeekOrigin origin);
}
</code>```

Both signatures are copied from Stream. Which is another option: use Stream instead of your interface. It has ReadAsync(), Seek() and more.

You can do this since System.IO and System.IO.IsolatedStorage directly use Stream (FileStream in System.IO and IsolatedStorageFileStream in System.IO.IsolatedStorage). WinRT has its own stream types, but you can convert those to Stream using extension methods from WindowsRuntimeStreamExtensions.

ContentLicense: CC BY-SA 4.0

Title: I/O interface for portable library

Body:
I don't see anything hard about this, you can just create the interface (using asynchronous method for Read()) and then implement it three times:

interface ISomeDataSource
{
    Task<int> ReadAsync(byte[] buffer, int offset, int count);
    long Seek(long offset, SeekOrigin origin);
}

Both signatures are copied from Stream. Which is another option: use Stream instead of your interface. It has ReadAsync(), Seek() and more.

You can do this since System.IO and System.IO.IsolatedStorage directly use Stream (FileStream in System.IO and IsolatedStorageFileStream in System.IO.IsolatedStorage). WinRT has its own stream types, but you can convert those to Stream using extension methods from WindowsRuntimeStreamExtensions.


问:
How to achieve N-Tier type in Codeigniter MVC
I know that N-Tier intended to separate layers on different network

but I would like to have the same code separation in codeigniter

I got this idea to have
Model : for database CRUD - > Data layer
REST API : for business logic—> Business layer   that calls CRUD method from models

Controller & View are going as before

and flow will be

View<—>Controller <—> API(with Business classes) <—>Model

I wanna know what kind of drawbacks it can be such architecture

答:
N-Tier is not necessarily on different networks. Each tier is in a separate process, it could all be on one machine.

3 tier is somewhat out of fashion due to the out of process marshaling it requires.

Any extra tier, read: process boundary or worst communication across a network transport, is expensive.

In a 3-tier system the second tier is usually the business logic. A two tier system is most popular these days, since business logic is really difficult to divorce from user data.

A typical web application is two tier; user data and business logic are one tier and a DBMS is the other tier.

The biggest drawback is having to marshal/serialize everything multiple times as you bounce across tiers. The less tiers you have the less bouncing you need to do. All work you can do directly without jumping through hoops is massively more efficient. The difference between passing your data by (in-process memory) reference or through a transport is huge.

API code or a business logic in the form of application code is easily replicated and thus efficient to run in-process. A business logic tier offers little scaling opportunities since it depends on transactions against the data tier. You don't need tiers for reuse, you can reuse layers just fine.

I'd stick to layering for as long as possible. For what you call 'API' in your 'flow': look into application service layer pattern (which is a layer, not a tier) as an alternative.


问:
Can I start with a passing unit test?
Uncle Bob's rules for TDD are specified here.

You are not allowed to write any production code unless it is to
make a failing unit test pass.

You are not allowed to write any more
of a unit test than is sufficient to fail; and compilation failures
are failures.

You are not allowed to write any more production code
than is sufficient to pass the one failing unit test.

But, is it ok to write a bunch of tests that pass as soon as the test compiles?  For example, a test that asserts null and the default impl of a method I'm testing returns null.  Am I doing something wrong by doing this?  Should I skip to the first test that will fail or is it ok to write tests that automatically pass first?

答:
No, because it is possible to write a test that inadvertently passes when it should actually fail.

That's why you must make it fail first, so that you can demonstrate transitioning from a failed state to a passed state where you're testing the actual functionality that you want, rather than having a bogus test that passes and makes you think that your code actually works, when in fact it doesn't.

Title: Can I start with a passing unit test?


问:
Is garbage collection needed for implementing safe closures?
I recently attended an online course on programming languages in which, among other concepts, closures were presented. I write down two examples inspired by this course to give some context before asking my question.

The first example is an SML function that produces a list of the numbers from 1 to x, where x is the parameter of the function:

```<code>fun countup_from1 (x: int) =
    let
        fun count (from: int) =
            if from = x
            then from :: []
            else from :: count (from + 1)
    in
        count 1
    end
</code>```

In the SML REPL:

```<code>val countup_from1 = fn : int -> int list
- countup_from1 5;
val it = [1,2,3,4,5] : int list
</code>```

The countup_from1 function uses the helper closure count that captures and uses the variable x from its context.

In the second example, when I invoke a function create_multiplier t, I get back a function (actually, a closure) that multiplies its argument by t:

```<code>fun create_multiplier t = fn x => x * t
</code>```

In the SML REPL:

```<code>- fun create_multiplier t = fn x => x * t;
val create_multiplier = fn : int -> int -> int
- val m = create_multiplier 10;
val m = fn : int -> int
- m 4;
val it = 40 : int
- m 2;
val it = 20 : int
</code>```

So variable m is bound to the closure returned by the function call and now I can use it at will.

Now, for the closure to work properly throughout its lifetime, we need to extend the lifetime of the captured variable t (in the example it is an integer but it could be a value of any type). As far as I know, in SML this is made possible by garbage collection: the closure keeps a reference to the captured value which is later disposed of by the garbage collector when
the closure is destroyed.

My question: in general, is garbage collection the only possible mechanism to
ensure that closures are safe (callable during their whole lifetime)?

Or what are other mechanisms that could ensure the validity of closures without garbage collection: Copy the captured values and store it inside the closure? Restrict the lifetime of the closure itself so that it cannot be invoked after its captured variables have expired?

What are the most popular approaches?

EDIT

I do not think the example above can be explained / implemented by copying the captured variable(s) into the closure. In general, the captured variables can
be of any type, e.g. they can be bound to a very large (immutable) list.
So, in the implementation it would be very inefficient to copy these
values.

For the sake of completeness, here is another example using references
(and side effects):

```<code>(* Returns a closure containing a counter that is initialized
   to 0 and is incremented by 1 each time the closure is invoked. *)
fun create_counter () =
    let
        (* Create a reference to an integer: allocate the integer
           and let the variable c point to it. *)
        val c = ref 0
    in
        fn () => (c := !c + 1; !c)
    end

(* Create a closure that contains c and increments the value
   referenced by it it each time it is called. *)
val m = create_counter ();
</code>```

In the SML REPL:

```<code>val create_counter = fn : unit -> unit -> int
val m = fn : unit -> int
- m ();
val it = 1 : int
- m ();
val it = 2 : int
- m ();
val it = 3 : int
</code>```

So, variables can also be captured by reference and are still alive after
the function call that created them (create_counter ()) has completed.

答:
Garbage collection is not necessary in order to implement closures.  In 2008, the Delphi language, which is not garbage collected, added an implementation of closures.  It works like this:

The compiler creates a functor object under the hood that implements an Interface representing a closure.  All closed-over local variables get changed from locals for the enclosing procedure to fields on the functor object.  This ensures that the state is preserved for as long as the functor is.

The limitation to this system is that any parameter passed by reference to the enclosing function, as well as the function's result value, cannot be captured by the functor because they are not locals whose scope is limited to that of the enclosing function.

The functor is referred to by the closure reference, using syntactic sugar to make it look to the developer like a function pointer instead of an Interface.  It uses Delphi's reference-counting system for interfaces to ensure that the functor object (and all the state it holds) remains "alive" as long as it needs to, and then it gets freed when the refcount drops to 0.


问:
How many different configurations are used for software tests?
This article shows that for Android apps, software developers use between 30 and 50 different devices for compatibility tests.

I tried to find information about tests of applications on "classic" computers, for example PC games or graphics-oriented business applications: how many different hardware configurations are typically tested in this area?

答: This is where automated testing is the only manageable approach. It has a lot of overhead but in the end it is worth it to get the coverage you are looking for. For the sake of brevity, you develop the automated tests for maximum code coverage. And then set up a schedule for testing which alternates the different architectures you want to test. You can choose the schedule depending on the number of architecture options you want to cover. I normally just do amd64, ARM, and x86. I also include languages(en-us, ja-jp, etc) for international audiences. But I am working on a Desktop app. Hope that helps!</p>


问:
Is memory management in programming becoming an irrelevant concern?
Background

I revisited an old (but great) site I had not been to for ages - the Alioth Language Shootout (http://benchmarksgame.alioth.debian.org/).

I started out programming in C/C++ several years ago, but have since then been working almost exclusively in Java due to language constraints in the projects I have been involved in. Not remembering the figures, I wanted to see, approximately, how well Java fared against C/C++ in terms of resource usage.

The execution times were still relatively good, with Java at worst performing 4x slower than C/C++, but on average around (or below) 2x. Due to the nature of the implementation of Java itself, this was no surprise, and it's performance time was actually lower than what I expected.

The real brick was the memory allocation - at worst, Java allocated:

a whopping 52x more memory than C

and 25x more than C++.

52x the memory ... Absolutely nasty, right? ... or is it?  Memory is comparatively cheap now.

Question:

If we do not speak in terms of target platforms with strict limits on working memory (i.e. embedded systems and the like), should memory usage be a concern when picking a general purpose language today?

I am asking in part because I am considering migrating to Scala as my primary language. I very much like the functional aspects of it, but from what I can see it is even more expensive in terms of memory than Java. However, since memory seems to be getting faster, cheaper and more plentiful by the year (it seems to be increasingly hard to find a consumer laptop without at least 4GB of DDR3 RAM), could it not be argued that resource management is becoming increasingly more irrelevant as compared to (possibly implementation-wise expensive) high-level language features which allow for faster construction of more readable solutions?

答:
For people dealing with "big data" memory management is still a huge issue. Programs in astronomy, physics, bioinformatics, machine learning, etc., all have to deal with multi-gigabyte datasets, and the programs run a lot faster if the relevant portions can be kept in memory. Even running on a machine with 128GB of RAM doesn't solve the problem.

There is also the matter of taking advantage of GPU, though perhaps you'd classify that as an embedded system. Most of the hard thinking in using CUDA or OpenCL boils down to memory management issues in transferring data from main memory to GPU memory.


问:
Read K&R before learning low latency C++?
I have never really done a large amount of C programming but am in the middle of teaching myself low latency C++. Would it do more harm than good to read the K&R C programming book? I am a bit worried I will be reading about C conventions which may no longer hold in C++ (especially with C's fondness of global variables).</p>

答:
I'm a c# programmer, and I'm planning to study K&R.  I'm hoping it won't corrupt me so that I'm no longer able to write c# code.

Seriously, just think of C as a low-level programming language that follows the same fundamental principles as any other programming language, which bears some similarity to C++ and share some of its features.

In other words, it's useful to study C as a language in its own right, and I believe it will be a useful primer for learning C++, if you want to go that route.


答: C isn't the same as C++, particularly today. They have wildly diverged in terms of good practices and idiomatics. K&R may be a classic but that's just what it is, a classic. There are better modern C books out there and certainly better resources on low-latency C++ that focus on C++ as C++ rather than C with gribblies.</p>

Title: Read K&R before learning low latency C++?

Body: I have never really done a large amount of C programming but am in the middle of teaching myself low latency C++. Would it do more harm than good to read the K&R C programming book? I am a bit worried I will be reading about C conventions which may no longer hold in C++ (especially with C's fondness of global variables).</p>


答:
To be fair, a lot of Java out there indulges in some truly and pointlessly class-explosive patterns that just murder performance and hog memory but I do wonder how much of that memory is just the JVM which in theory (heh) let's you run the same app in multiple environments without having to completely rewrite new ones. Hence the design tradeoff question it all boils down to: "How much of your users memory is such a development advantage worth to you?"

This is, IMO a perfectly worthwhile and reasonable tradeoff to consider. What pisses me off though is the notion that because modern PCs are so powerful and memory is so cheap, we can completely ignore such concerns and bloat features and bloat code and be lazy about choices to the point where it seems like a lot of the stuff I do on a windows PC now, takes just as long as it did in Window '95. Seriously though, Word? How much new crap that 80% of their user-base actually needs could they have possibly added in 18 years? Pretty sure we had spellcheck pre-windows right? But we were talking memory which is not necessarily speed if you have plenty of it so I digress.

But of course if you can get the app done in 2 weeks at the cost of maybe a few extra megabytes rather than 2 years to get the needs-only-a-few-K version, it's worth considering how a few megs compares to (I'm guessing) 4-12 gigs on the average users machine before scoffing at the idea of being so sloppy.

But what does this have to do with Scala beyond the tradeoff question? Just because it's garbage collection, doesn't mean you shouldn't always be trying to think about the flow of data in terms of what's in scopes and closures and whether it should be left sitting around or used in such a way that it will be deallocated by GC when it's no longer needed. That's something even us JavaScript UI web devs have had to think about and hopefully will continue to as we spread into other problem domains like the perf-savvy cancer (that you all should have killed with Flash or Applets or something when you had the chance) that we are.

Title: Is memory management in programming becoming an irrelevant concern?

Body:
Background

I revisited an old (but great) site I had not been to for ages - the Alioth Language Shootout (http://benchmarksgame.alioth.debian.org/).

I started out programming in C/C++ several years ago, but have since then been working almost exclusively in Java due to language constraints in the projects I have been involved in. Not remembering the figures, I wanted to see, approximately, how well Java fared against C/C++ in terms of resource usage.

The execution times were still relatively good, with Java at worst performing 4x slower than C/C++, but on average around (or below) 2x. Due to the nature of the implementation of Java itself, this was no surprise, and it's performance time was actually lower than what I expected.

The real brick was the memory allocation - at worst, Java allocated:

a whopping 52x more memory than C

and 25x more than C++.

52x the memory ... Absolutely nasty, right? ... or is it?  Memory is comparatively cheap now.

Question:

If we do not speak in terms of target platforms with strict limits on working memory (i.e. embedded systems and the like), should memory usage be a concern when picking a general purpose language today?

I am asking in part because I am considering migrating to Scala as my primary language. I very much like the functional aspects of it, but from what I can see it is even more expensive in terms of memory than Java. However, since memory seems to be getting faster, cheaper and more plentiful by the year (it seems to be increasingly hard to find a consumer laptop without at least 4GB of DDR3 RAM), could it not be argued that resource management is becoming increasingly more irrelevant as compared to (possibly implementation-wise expensive) high-level language features which allow for faster construction of more readable solutions?


问:
Testing a REST client against a REST Server. How to do fixtures?
When writing unit tests, it's common to use fixtures: little testable data, so we can say:
1. Get all clients should include Willy Wonka.
2. Delete client 3, and now get clients should not include Willy Wonka anymore.

That's fine for unit tests.  Use setup/teardown to re-load the fixtures or rollback the transaction.  So testing creates, updates, and deletes are done inside a transaction.  The new temporary data lasts just for the length of that test, then is reset.

But what about when we've separated the REST server from the REST client?

We want to make sure our REST client is not just reading correctly, but creating, updating, and deleting correctly.

I haven't been able to find any examples or suggestions for how to do this against a remote test REST server.

Assuming I've got a test REST server serving only fixtures.  The whole stateless nature of HTTP means it'd be hard to send a "BEGIN TRANSACTION" and "ROLLBACK TRANSACTION" or "RELOAD FIXTURES" type of message, right?

I can't be the first to want to do this, so I have a feeling I need a different way of thinking about this.

Any suggestions?

答:
I think faking the REST server responses is the best way to test the client.

For Ruby, there's FakeWeb gem which you can use to emit fake responses - https://github.com/chrisk/fakeweb.

Also, in JavaScript you can use something like Sinon.JS, which gives you a fake server - http://sinonjs.org/docs/#fakeServer.


答:
Software systems ideally have well-defined system boundaries and interfaces between them. REST services are good examples of this.

To that end, I would recommend against doing what you're trying to do.

Specifically:

We want to make sure our REST client is not just reading correctly, but creating, updating, and deleting correctly.

What I would suggest instead is:

Building tests for your REST client, to ensure that it behaves correctly, given specific input and output. Account for good (expected) and bad (unexpected) values.

Building tests for your REST service (if you control it, that is), to behave according to its intended function

Keep tests close to their problem domain, so they can help guide the design and development of what is important in that context

Title: Testing a REST client against a REST Server. How to do fixtures?

Body:
When writing unit tests, it's common to use fixtures: little testable data, so we can say:
1. Get all clients should include Willy Wonka.
2. Delete client 3, and now get clients should not include Willy Wonka anymore.

That's fine for unit tests.  Use setup/teardown to re-load the fixtures or rollback the transaction.  So testing creates, updates, and deletes are done inside a transaction.  The new temporary data lasts just for the length of that test, then is reset.

But what about when we've separated the REST server from the REST client?

We want to make sure our REST client is not just reading correctly, but creating, updating, and deleting correctly.

I haven't been able to find any examples or suggestions for how to do this against a remote test REST server.

Assuming I've got a test REST server serving only fixtures.  The whole stateless nature of HTTP means it'd be hard to send a "BEGIN TRANSACTION" and "ROLLBACK TRANSACTION" or "RELOAD FIXTURES" type of message, right?

I can't be the first to want to do this, so I have a feeling I need a different way of thinking about this.

Any suggestions?


答:
Two angles to keep in mind here:

Are you testing your code or the plumbing? Presuming you are using a well know service and client stack you can probably safely presume their testers and thousands of users will generally ensure there isn't a fundamental bug in the underpinnings.

Why aren't your tests idempotent? Make a way to write non-production data or write to a different endpoint. Pick some predictable naming pattern. Pre-load the rest server DB before the tests. And there are probably a few more ways to make this happen -- method is really tactical and should depend on the nature of the app.


问:
Choosing DOCTYPE for an online eBook
I'm about to begin working on a project which involves creating an eBook-like website which will contain lots of large documents with text and images.

The site should have a rather minimalistic design, but it will include a UI with some scripting and animations to make browsing the large documents easier.

I've been thinking on this matter for a while now and I can't decide what markup to use for such a project.

Should it be HTML 4.0 strict? XHTML 1.0 strict? Perhaps HTML5?

It is a likely that people who are not especially internet-savvy will visit the site due to its content, and as such I think it's very plausible for some of the visitors to have relatively old browsers. Can XHTML or HTML5 hurt me in this regard?

Reading about various eBook formats (specifically EPUB), I learned that many of them use XHTML internally; does this mean that if I use XHTML (correctly) the site might also be packaged and delivered as an EPUB in the future? The strictness and cleanliness of XHTML/XML also appeals to me greatly.

Overall XHTML appeals to me the most, but I'm scared about the implication of its use. It has supposedly been discontinued by W3C in favor of HTML 5. However, I don't really see any advantages to using HTML5 over HTML 4 strict, as I won't be using any of it's features. I'm also worried about browser compatiablity of HTML5.

Should I just stick with good ol' HTML 4 strict?

答:
```<code><!doctype html>
</code>```

And your good to go... If you dont use HTML5 features then you don't and you have nothing to be afraid about.

Title: Choosing DOCTYPE for an online eBook

Body:
<!doctype html>

And your good to go... If you dont use HTML5 features then you don't and you have nothing to be afraid about.


问:
better way to define an exception thrown by a method in Java?
I know how to define exceptions and all, but I'm not sure if the way I'm doing it is the most intuitive and readable by another users.

I have a very simple method I've written that I want to throw an exception, because it's easier then manually checking return values; the method is private and there is no way need to define a public Exception type.  None of the existing Java exceptions fit the error exactly, so I'm trying to figure out what type of exception I want to throw here?

I don't want to just throw a RuntimeException, I only want to catch my specific exception I throw, all other Exceptions should propagate up. I figured I would define a little inner class for my exception.  However, i don't really need to overwrite anything in the RuntimeException class, I just want to catch and and print it's message.  So now I have a line that says something like

```<code>private class MissingPropertyException extends RuntimeException{};
</code>```

This does nothing but allow me to catch only the exception I'm intentionally throwing while ignoring the rest.  Is this good syntax?  it feels odd to define a inner class without implementing anything within it.  Is there a way to do this elegantly without creating an empty inner class?

答:
Exceptions are not for errors that can be reasonably checked.  I'm also not sure why your defining a private exception type since you won't be able to catch that exception as it propagates up.  I'm also not sure why you're creating an inner class.

Extending the RuntimeException, Exception or Throwable is not about overriding behavior.  It's about creating an exception hierarchy appropriate for your application.  For example, if you're driving a car, a low level exception might be GPSReadError which is a type of NavigationError, which is a type of AutoDriveError which is a type of VehicleException. It's also about identifying the source of the exception.  For example, a GPSReadError would not be used when checking tire pressure.

IMHO Exceptions are one area where Java could be cleaned up, with frameworks like spring throwing subclasses of RuntimeException almost exclusively.  In fact, you could look at Spring's intelligent use of RuntimeExceptions to better classify errors when using JDIBC.  http://static.springsource.org/spring/docs/3.0.x/reference/jdbc.html