问:
What are the proper terms for desk/user testing?
Is there a common and accepted term for the manual tests done by programmers or users on software in lieu of or in addition to automated testing?  For example, in a progression as follows:

"Desk Test" for the non-automated testing a programmer does while writing code.

"Unit Test" for an automated test for a particular class/method/function.

"Integration Test" for an automated test of multiple components working together.

"System Test" or "Acceptance Test" for application-wide tests that would be meaningful in conversation with the end user.

"User Test", for when we hand it to the user and ask them to find bugs.

Are there better terms for #1 ("Desk Test") and #5 ("User Test")?

(And I know that TDD would say write #2 before #1... but making that point is exactly why I want to know the right word for #1.)

答:
Verification of functionality during coding is a category of White Box Testing.  Note the Wikipedia description:

White-box testing (also known as clear box testing, glass box testing,
  transparent box testing and structural testing) tests internal
  structures or workings of a program, as opposed to the functionality
  exposed to the end-user. In white-box testing an internal perspective
  of the system, as well as programming skills, are used to design test
  cases. The tester chooses inputs to exercise paths through the code
  and determine the appropriate outputs. This is analogous to testing
  nodes in a circuit, e.g. in-circuit testing (ICT).

Most unit testing, if done properly, is actually black box testing; the unit test should not need knowledge of the internals of the method in order to complete the testing.

See also Development Testing, the process of applying best practices to the development process in order to reduce defects.  Development Testing might include static code analysis, data flow analysis metrics analysis, peer code reviews, unit testing, code coverage analysis, traceability, and other software verification practices.


问:
How has an increase in the complexity of systems affected successive generations of programmers?
As a "new" programmer (I first wrote a line of code in 2009), I've noticed it's relatively easy to create a program that exhibits quite complex elements today with things like .NET framework for example. Creating a visual interface, or sorting a list can be done with very few commands now.

When I was learning to program, I was also learning computing theory in parallel. Things like sorting algorithms, principles of how hardware operates together, boolean algebra, and finite-state machines. But I noticed if I ever wanted to test out some very basic principle I'd learned in theory, it was always a lot more difficult to get started because so much technology is obscured by things like libraries, frameworks, and the OS.

Making a memory-efficient program was required 40/50 years ago because there wasn't enough memory and it was expensive, so most programmers paid close attention to data types and how the instructions would be handled by the processor. Nowadays, some might argue that due to increased processing power and available memory, those concerns aren't a priority.

My question is if older programmers see innovations like these as a godsend or an additional layer to abstract through, and why might they think so? And do younger programmers benefit more learning low-level programming BEFORE exploring the realms of expansive libraries? If so then why?

答:
My question though is how do people feel about this "hiding" of lower-level elements. Do you older programmers see it as a godsend or an unnecessary layer to get through?

Neither, really.

Layering is necessary because without it, you reach a point where your system becomes unmaintainable spaghetti.  It's also one of the tenets of reusability:  if the developer of a library did a good job, the people using it shouldn't have to care about the details of the implementation.  The amount of canned code we use in our systems has grown by orders of mangitude from what it was when I wrote my first program 35 years ago.  That growth means we're able to do more powerful things as time goes on.  This is good.

The place where it's been a problem for me is entirely cultural.  My pragmatic half understands that it's no longer possible to wrap my mind around every last detail and be able to finish the things I want to get done.  (Getting older doesn't help, either.)  My cantankerous graybeard half had a difficult time letting go of many years of having such a fine-grained understanding of everything I worked on.

Do you think younger programmers would benefit more learning low-level programming BEFORE exploring the realms of expansive libraries?

As has been pointed out in other answers, there's a balance to be struck between attracting and maintaining the attention of neophytes and giving them an ideal, from-the-bottom-up education.  If you can't do the former, the latter can't happen.

I see things in our industry that parallel the rest of society.  It used to be that almost everybody grew their own food and spent a lot of time doing that.  Since then, we've sprouted specialists called farmers who do that job, freeing up others to do other things that contribute to society.  I buy my food at a grocery store and would be thoroughly unable to produce most of it on my own if I had to.  We have a similar thing going on, albeit on a much more compressed time scale.  Programmers are specializing in some set of layers and not others.  The average guy writing GUIs may know that there is such a thing as swap space but probably doesn't know or care much about how the operating system is managing it.

The upshot of all this is that it's no longer about just development.  Continued specialization means developers will need to continue improving their communication and integration skills.

ContentLicense: CC BY-SA 3.0


问:
Microsoft Reciprocal License: include source even for unmodified binary?
The Microsoft Reciprocal License (here taken from the WIX toolset I use) has the following sentence:

For any file you distribute that contains code from the software (in source code or binary format), you must provide recipients the source code to that file along with a copy of this license

Does that mean I have to provide the source code even if I only use the unmodified (original) DLL in binary format? Note that this unmodified DLL contains code from the software, although it was not compiled by me.

What does provide mean? Shall I provide 11 MB of source code as part of my application (install the source on hard disk) or can I provide it as an extra download as well?

答:
Does that mean I have to provide the source code even if I only use the unmodified (original) DLL in binary format?

Not if you just link to the DLL from your software.  Read the license again:

For any file you distribute that contains code from the software (in source code or binary format), you must provide recipients the source code to that file along with a copy of this license

(the software being the DLL, or its original source)

Provide means to supply the source code to the person to whom you're distributing the software (the "user").


答:
I believe the term you're looking for is "Ad-Hoc Testing".  Those are tests you might write as you go but are not intended to be kept or used as part of any structured testing which might be kept and reused.

It's usefulness is dubious and much of the current school of thought (or fashion) is that, like a proof of concept, if you needed to write a test in the first place then it's worth keeping even if only to show how much work you've done?

Title: What are the proper terms for desk/user testing?

Body:
Is there a common and accepted term for the manual tests done by programmers or users on software in lieu of or in addition to automated testing?  For example, in a progression as follows:

"Desk Test" for the non-automated testing a programmer does while writing code.

"Unit Test" for an automated test for a particular class/method/function.

"Integration Test" for an automated test of multiple components working together.

"System Test" or "Acceptance Test" for application-wide tests that would be meaningful in conversation with the end user.

"User Test", for when we hand it to the user and ask them to find bugs.

Are there better terms for #1 ("Desk Test") and #5 ("User Test")?

(And I know that TDD would say write #2 before #1... but making that point is exactly why I want to know the right word for #1.)


问:
Does bootstrapping limit the achievable speed of the new compiler?
I have a good grasp on how the C compiler was bootstrapped from itself and how that must have been very efficient, since the first pre-bootstrapping version was written in assembler, which is as low level as you can get.

BUT today we have a very different situation, with languages first written on other relatively high-level languages (e.g. Clojure).

Question 1:
I have a hard time imagining that the bootstrapped compiler could be faster in anything than its non-bootstrapped counterpart. Is this possible?

Question 2:
Especially once you take the step in which you write then new version of your compiler in your new language:
Can any inefficiencies that existed in the non-bootstrapped code be fixed or is the code generation of the new compiler haunted by those forever? That matters even more since you can’t easily go back and fix those afterwards!

答:
Modern compilers (especially JITs) have optimization algorithms that exist outside the domain of the source languages. This invalidates your original premise, which is that a bootstrapped compiler can't be faster than a "native" compiler because it is built on top of a subset of the source language.

Programmers are notoriously bad at predicting where code will perform suboptimally.  The only good way to know is to measure performance with a profiler.

Nowadays, a good optimizing compiler can often produce object or binary code that is faster than hand-coded assembly, provided the source code is written in a way that doesn't defeat the compiler's optimizations.

ContentLicense: CC BY-SA 4.0


答: One reason why they (the bootstrapped compilers) may be faster because in low-level language, you make things work, but you probably won't bother to implement complex behaviour. In high-level language it's much easier. IOW, optimizations (at algorithm and data structure level) is often easier to do in higher language than in assembler.</p>

Title: Does bootstrapping limit the achievable speed of the new compiler?

Body:
I have a good grasp on how the C compiler was bootstrapped from itself and how that must have been very efficient, since the first pre-bootstrapping version was written in assembler, which is as low level as you can get.

BUT today we have a very different situation, with languages first written on other relatively high-level languages (e.g. Clojure).

Question 1:
I have a hard time imagining that the bootstrapped compiler could be faster in anything than its non-bootstrapped counterpart. Is this possible?

Question 2:
Especially once you take the step in which you write then new version of your compiler in your new language:
Can any inefficiencies that existed in the non-bootstrapped code be fixed or is the code generation of the new compiler haunted by those forever? That matters even more since you can’t easily go back and fix those afterwards!


答:
Design language A.

Write first A-compiler in language B.

Compile first A-compiler using any B-compiler.

Write second A-compiler in language A.

Compile second A-compiler using the executable obtained in step 3.

Compile second A-compiler using the executable obtained in step 5.

Assume that all compilers involved (the B-compiler and both A-compilers) are correct.
Then the executables obtained in step 5 and 6 both match the source code written in step 3 - in particular, they parse A code, optimize it, and generate code for it exactly as specified during step 4, regardless of what happened during step 2.

In addition, the executable from step 6 contains exactly the code the second compiler ought to emit, regardless of what kind of code the first compiler emitted during step 5 (again, as long as it was correct given the source code).

Repeat this process as necessary. Any compiler, no matter how complicated its ancestry and bootstrapping process are, can be made to both

run as fast as it possible can with the best A- or B-compiler.

emits code that is as efficient as the most efficient code any A-compiler.

Note that these are two quite different metrics, I included both because it wasn't clear which one you were talking about.


答:
I think the easiest way to fix this problem, mentally, is just to realize that it all ends up back as machine code anyway. How it ended up as machine code is the story, and the story certainly has meaning to us as people, but computers don't care what the history of the machine code is. They just do what the lowest-level instruction set tells them to do.

Mentally, we might think like this:

Someone wrote the appropriate machine code to allow, say, C.

C was used to write C-Plus-Classes

C++ gets used to create an implementation of, say, Java.

Therefore, we justify a belief that anything in Java must be slower than anything in, say, Assembler. But...that's not how it really works!

How It Really Works Nowadays

Computer runs machine code.

Or, for those who want bytecode considered:

Machine code (JIT) turns bytecode into machine code. (slower startup time caused, maybe)

Computer runs machine code.

All the code ends up the same place, so the long path it took to get there doesn't necessarily effect how it runs.

Sometimes there is an extra layer, like with JavaScript that never gets compiled to machine code before being run, but then machine code turns that stuff into machine code too. It all ends up the same place, in the CPU and the RAM. Some machine code isn't as efficient as other machine code, but how that code came to be written doesn't really matter: sometimes machine code writes the best machine code, and sometimes a human hand-writing it would have done it differently and that way would have been better. Now adays, thanks to some very clever humans, that's a rare feat indeed, and the differences are usually too small to care about.

So, whether or not the compiler was bootstrapped or not, and whether or not compilation happens late, or early, or just in time, or only via mail service with a SASE to Bangladesh, the result is not necessarily limited by the path it took to the same destination.


问:
Is embedded programming closer to electrical engineering or software development?
I am being approached with a job for writing embedded C on micro controllers. At first I would have thought that embedding programming is too low on the software stack for me, but maybe I am thinking about it wrong.

Normally I would have shrugged off an opportunity to write embedded code, as I don't consider myself an electrical engineer. Is this a bad assumption? Am I able to write interesting and useful software for embedded systems, or will I kick myself for dropping too low on the software stack?

I went to school for computer science and really enjoyed writing a compiler, thinking about concurrent algorithms, designing data structures, and developing frameworks. However, I am currently employed as a web developer, which doesn't scream the interesting things I just described. (I currently deal with issues like: "this check box needs to be 4 pixels to the left" and "this date is formatted wrong".)

I appreciate everyone's input. I know I have to make the decision for myself, I just would like some clarification on what it means to be a embedded programmer, and if it fits what I find to be interesting.

答:
If you want to be good at working on embedded systems, then yes, you need to think like a EE some of the time.  That is generally when you are writing code to interface with the various peripherals (serial busses like UART, SPI, I2C or USB), 8 and 16-bit timers, clock generators, and ADCs and DACs.  "Datasheets" for microcontrollers often run into the hundreds of pages as they describe every bit of every register.  It helps to be able to read a schematic so you can probe a board with an oscilloscope or logic analyzer.

At other times, it is just writing software.  But under tight constraints: often you won't have a formal OS or other framework, and you might have only a few KB of RAM, and maybe 64 KB of program memory.  (These limits are assuming you are programming on smaller 8 or 16-bit micros; if you are working with embedded Linux on a 32-bit processor, you won't have the same memory constraints but you will still have to deal with any custom peripheral hardware that your Linux distro doesn't provide drivers for.)

I have a background in both EE and CS so I enjoy both sides of the coin.  I also do some web programming (mostly PHP), and desktop apps (C# and Delphi), but I've always enjoyed working on embedded projects the most.

ContentLicense: CC BY-SA 2.5


问:
Get subset of associative array. Which of these is more readable? Is there another reason to use one over the other (or a different method)
If I have an array that has more info than I need and I want to pull out a subset of its name value pairs I have come up with 2 methods.

Both rely on this:

```<code>  $detailkeys=array(title,summary,subtitle,url,imgsrc,description);
</code>```

The method I came up with on my own:

```<code>  foreach ($detailkeys as $keyname) {
    $details[$keyname]=$data[$keyname];
  }
</code>```

The method I found online:

```<code>  // get just the desired parts of $data and put them in $details.
  $details=array_intersect_key($data, array_flip($detailkeys));
</code>```

The second method, with a decent comment, is what I chose, but wanted to see what anyone else had to say about it.

答:
I think the second method is preferable. And this would seem perfectly readable, providing you are familiar with the array_intersect_key() function (although that's a quick lookup if you're not, and if you are it's almost self documenting). To be honest, I think your first method would also require a similar comment.

The second method also avoids an additional $keyname variable. And with it being a built-in function you would expect it to be optimised.

Have you tried benchmarking the results? (Although TBH with such a small set of "required" keys I can't imagine this being significant - I micro optimisation at best.)

You could perhaps avoid the need for your $detailkeys if you initialised your $details array with the required keys to begin with (if that is possible)...

```<code>$details = array (
    'title' => null,
    'summary' => null,
    // etc.
);
</code>```

And then you also wouldn't need to call array_flip()...

```<code>$details = array_intersect_key($data, $details);
</code>```

However, I would also query whether you needed to create a subset array to begin with. If you are only reading the data, passing it around, and not changing it, then it's not necessarily an optimisation and could even be the opposite.

Body:
I think the second method is preferable. And this would seem perfectly readable, providing you are familiar with the array_intersect_key() function (although that's a quick lookup if you're not, and if you are it's almost self documenting). To be honest, I think your first method would also require a similar comment.

The second method also avoids an additional $keyname variable. And with it being a built-in function you would expect it to be optimised.

Have you tried benchmarking the results? (Although TBH with such a small set of "required" keys I can't imagine this being significant - I micro optimisation at best.)

You could perhaps avoid the need for your $detailkeys if you initialised your $details array with the required keys to begin with (if that is possible)...

$details = array (
    'title' => null,
    'summary' => null,
    // etc.
);

And then you also wouldn't need to call array_flip()...

$details = array_intersect_key($data, $details);

However, I would also query whether you needed to create a subset array to begin with. If you are only reading the data, passing it around, and not changing it, then it's not necessarily an optimisation and could even be the opposite.


问:
Does cherry-pick just pull changes into the current working tree as uncommitted changes?
My team uses Mercurial for version control. Our development / version control routine is:

all been committing to the same branch

pulling for changes and rebasing our commits locally before pushing back to the hosted repository

This keeps everyone in sync. And the latest code (even unfinished code) is than grabbed from the tip of this branch and put into our test environment.

I've been tasked with creating a management process where we can more cleanly extract "completed features" into the test environment.

I've advocated that we begin to branch->pull-request->merge->close branch process, but I've been told that branching is not an option, and that the project director would prefer too be able to manually select commits representing completed features/bug fixes and only include those commits in a deployment to testing.

I am not sure what terminology besides cherry-pick in GIT / HG that would represent this process, and if it would even be advisable as the commits would no-longer have a proper revision history as it's my understanding that cherry-pick just pulls changes into the current working tree as uncommitted changes correct?

答:
I'm not sure about mercurial, but git cherry-pick will create a new commit to contain the changes unless you tell it otherwise.  The crucial point is that it detaches the commit from its history, including any commits it depends on for proper operation.  That also makes it a pain when you have to merge testing changes back into the development branch, and makes it difficult or impossible to use built-in version control tools to do just about any useful comparison you'd want to do between testing and develop, including simple questions like "Did that bug fix make it into testing?"

I would find out the reasons behind branching not being an option.  Perhaps they are basing it on a previous bad merging experience in a subpar version control system (pretty much all of them were bad at merging before hg and git came along, and a lot still are bad).

I would sell feature branches as being able to select commits containing completed features together with all required dependencies, while always maintaining the ability to track and compare between development and testing.  cherry-pick is useful to correct the occasional exceptional circumstance that your branching model didn't account for, but using it as the total basis for your configuration management is asking for trouble.


问:
Which one of the SOLID principles had the largest effect on your code?
Which if the SOLID principles introduced by "Uncle Bob" Martin had the largest effect on your day to day coding? Please give details!</p>

答: I will say ISP and a to a slightly lesser extent SRP. I must clarify that it was much later after I started following these principles that I heard about SOLID. The reason SOLID is so popularis because, like Design patterns, it is summarization of good programming practices that programmers follows through intuition or experience and not an exercise in coining jargons (which I have come to believe is the major side effect of programming)</p>


问:
How do I find which line connected two squares?
In the Permissive Field of View algorithm, a destination square is visible from a source square if any unobstructed line can be drawn from the source square to the destination square. The algorithm works by defining a series of view fields (essentially, triangles) and checking if the destination square exists inside of a view field.

Unfortunately, the algorithm does not make available the actual line that it found between the source square and the destination square; it only tells you whether such a line is guaranteed to exist.

How can I find the line, presuming it is guaranteed to exist?

I've tried plotting lines between the edges of the source square to the edges of the destination square, but this apparently is just an approximation (it can fail to find the line).

答:
Find all squares that are between the source and destination. (E.g. all squares that intersect a bounding box.)

Do a 2d rotation of all the squares (including src & dest) so that a line that connects the source and destination is perpendicular to the x axis.

Perform an orthographic projection of the squares into 1d space. I.e. squish them into intervals.

Take the source interval, and subtract the intervals of all the other squares from it. (Perhaps using something like an interval tree.)

The intersection of the subtracted source interval and the destination will give an interval of lines which connect the two squares. (Due to floating point imprecision after all these calculations, you may want to filter by a small epsilon.)

Project the interval back into 2d space and rotate back into the original basis, and you have a set of lines which connect the two squares.

This should be able to calculate both if the destination is visible and the set of all lines that connect them.


问:
Do code style guides increase productivity?
Our team of 12 is currently considering implementing a style guide. The question came up if long term productivity benefits outweigh the short term productivity loss as everyone adapts.

What has been your experience with style guides?

I should also note our code base is rather large and we'll probably have to run our code through uncrustify or something similar to bring the entire code base into conformance with the guide.

答:
Independent of whether style guides are worth the pain you should consider how you're going to enforce the policy as this will be a larger drag on productivity than learning the guide.

As a C# developer, I'm a proponent of adopting StyleCop as both a compliance tool and using it's rules as a guide. It's a static analysis tool I can run from the IDE which will analyze the source code at a document / project level and list all the violations as warnings. This doesn't prevent me from building, and I don't have to learn the rules beforehand.


答:
My experience has been positive, if the style document is well designed.

I expect to NOT have to think about any white space or indentation rules when I write code, and be able to just use a standard autoformating template. I appreciate the consistency when I read code.

More important than the white space consistency are the usual patterns/designs/name spaces styles. Knowing where you can look for something, and have a good chance to find it - or be reasonably sure it doesn't exist if it's not where you expect it to be is a huge time saver when you're exploring a large code base.

Title: Do code style guides increase productivity?

Body:
Our team of 12 is currently considering implementing a style guide. The question came up if long term productivity benefits outweigh the short term productivity loss as everyone adapts.

What has been your experience with style guides?

I should also note our code base is rather large and we'll probably have to run our code through uncrustify or something similar to bring the entire code base into conformance with the guide.


答:
Yes and no.. it depends on the standards. Note that I feel style guides are useless, who really gives a damn how many spaces you have to put between an operator and variable, or how to indent your members.

However, once you assume your coders know their stuff, are professional enough to maintain consistency within a code file (ie will not just reformat everything because of a personal preference) and that any guide you come up with will never be perfect, you can start to crate something genuinely useful.

Standards contribute most when they are about communication. They are at their worst when they are about restrictions.

So a guide that says how to lay your project out, where to find the main project file (.sln or makefile or whatnot), where the entry point is, and how to name the files so that someone coming along can pick up a never-before-seen project and know exactly where to look for the things they need to find... that is worth its weight in gold.

I used to have a standards doc that said you pretty much laid out a project in a certain way - the .sln was included in the directory (ie there was none of this project file and a subdirectory of code), that there was always a release directory containing everything you needed to deliver the component, that every release directory contained a readme.txt that contained a summary of the component, what special stuff you needed to do to build it, and what you needed to do to deploy it. When everyone knew this, you never needed to tell them - I could simply direct a systems engineer at the source repo and they'd be able to deploy it all themselves, no handholding required, and all because of the consistency withi each project. I could direct another dev at the project and they'd be up and running very quickly because they knew how to fit the component into the system and build it, and where the bits of code they needed to look at were (because of the reasonably generic naming convention)

I'm sure you've picked up an open source project and been unable to know where to start, with code files scattered all over the shop, a readme that tells you nothing useful, and no way to find where the output is built to, or how to use it once built.

But at no time did I talk down to my fellow devs and tell them how to write their code, or what their code should or should not look like. Productivity was high and we were all happy with the situation.

If you really want a style guide and you have to run your existing code through a beautifier anyway ... well, you might as well simply run the code through the beautifier before each checkin and let everyone code whatever style they like.


问:
How do you program a way to create mathematics formulas?
For one of my project ideas, I want to create a dice rolling app.  It would allow people to setup somewhat complex combinations of dice roll cascades in a visual way.

I have a pretty good idea on how I would program the logic for the dice, namely as if you're creating a math formula;

Class:

```<code>Formula
</code>```

Properties:

```<code>Left formula
Right formula
Function
</code>```

Thus, adding X = A + B would consist of Formula X, with left Formula A, right Formula B and function Add. Formula A and B would consist of left a or b, right null and function Constant.

This approach seems fine to me, but I like to try and find approaches others have come up with. However, because 'programming math' tends to lead to completely different questions (of the 'Do I need to learn math to program' or 'How do I program this formula' variety), I'm having difficulties finding other approaches.

答:
If I was implementing a formula-based dice calculator, my tendency would be to start by implementing a standard calculator (i.e., supporting PEMDAS).  This would normally involve tokenizing and parsing an input string (into a tree), evaluating the root node of the tree (which cascades down the tree).  Most popular programming languages usually have a wide variety of tutorials describing how to do this.

E.g., 5+2*6 would become

```<code>    +
   / \
  5   *
     / \
    2   6
</code>```

then

```<code>    +
   / \
  5   12
</code>```

then

```<code> 17
</code>```

After implementing a standard calculator, I would add one additional operator, the "d" operator, defined as follows:

XdY = Summation of X randomly generated integers between (inclusive) 1 and Y.

For example, 2D6+1D3 means "roll 2 6-sided dice and 1 3-sided die." Many audiences interested in dice rollers (particularly role-players) are already familiar with this notation.

See also Wikipedia: Dice Notation

Body:
If I was implementing a formula-based dice calculator, my tendency would be to start by implementing a standard calculator (i.e., supporting PEMDAS).  This would normally involve tokenizing and parsing an input string (into a tree), evaluating the root node of the tree (which cascades down the tree).  Most popular programming languages usually have a wide variety of tutorials describing how to do this.

E.g., 5+2*6 would become

    +
   / \
  5   *
     / \
    2   6

then

    +
   / \
  5   12

then

 17

After implementing a standard calculator, I would add one additional operator, the "d" operator, defined as follows:

XdY = Summation of X randomly generated integers between (inclusive) 1 and Y.

For example, 2D6+1D3 means "roll 2 6-sided dice and 1 3-sided die." Many audiences interested in dice rollers (particularly role-players) are already familiar with this notation.

See also Wikipedia: Dice Notation


答:
One way to do this is to convert your math / dice expression to Reverse Polish Notation (postfix).  Once converted, the expression is fairly easy to evaluate.

To do the conversion, you can use the Shunting Yard Algorithm.  This can handle not only basic operators, but also nested expressions and function calls.

The only area you would have to extend is handling the nD dice notation.  So instead of 5 + 5, you'd be interpreting 5d6 + 5d6.  That should be easy enough to handle once you have the expression reduced to tokens.

An expression evaluator like this is a very good learning project.

Title: How do you program a way to create mathematics formulas?

Body:
For one of my project ideas, I want to create a dice rolling app.  It would allow people to setup somewhat complex combinations of dice roll cascades in a visual way.

I have a pretty good idea on how I would program the logic for the dice, namely as if you're creating a math formula;

Class:

```<code>Formula
</code>```

Properties:

```<code>Left formula
Right formula
Function
</code>```

Thus, adding X = A + B would consist of Formula X, with left Formula A, right Formula B and function Add. Formula A and B would consist of left a or b, right null and function Constant.

This approach seems fine to me, but I like to try and find approaches others have come up with. However, because 'programming math' tends to lead to completely different questions (of the 'Do I need to learn math to program' or 'How do I program this formula' variety), I'm having difficulties finding other approaches.


答:
I rely upon the SWEBOK Testing chapter when I'm looking for generally accepted definitions of testing levels.

That having been said, they only recognize three levels:

unit

integration

system

But they acknowledge multiple objectives of testing:

acceptance / qualification

installation

alpha & beta

conformance / functional / correctness

reliability achievement and evaluation

regression

performance

stress

back-to-back

recovery

configuration

usability

Your first item appears to be a version of unit testing, perhaps with an objective of acceptance or conformance but most organizations place those tests in the system testing phase.

Your fifth item could fall into some combination of the objectives: acceptance, alpha, beta, conformance, and / or usability.


答:
Do younger programmers benefit more learning low-level programming BEFORE exploring the realms of expansive libraries? If so then why?

I don't think so. There are still plenty of situations in where it's beneficial to be aware of low the 'layer below' works, e.g.

When debugging a problem on layer n, it's can often be explained by considering what happens on layer n-1 (i.e. the layer below). I guess layer 0 would be "transistors" but if you want to explain a problem with transistors you would probably talk about physics (e.g. heat), so maybe physics is really level 0.

When optimizing code it (unfortunately) does help at times to lower the level of abstraction, i.e. implementing an algorithm in terms of a lower-level layer. However, compilers became really good at doing this for you if they actually see all the code involved. This reason became more popular recently with the boom of mobile and embedded devices, which tend to have weaker processors and where "performance per Watt" is much more relevant than on, say, desktop systems.

In general however, it became a lot easier to make computers do stuff (even if in slightly inefficient ways) which means that there are a lot more programmers than there used to be. This in turn made the "human" factor much more important: Robert Harvey's answer already mentioned that "humans can only hold so much knowledge in their heads at any given moment", and I think that's a very relevant aspect nowadays.

A major motivation in programming language and library (i.e. API) design is to make things easier on the human brain. To this day, everything still gets compiled down to machine code. However, this is not only error-prone, it's also notoriously hard to understand. So it's very desireable to

Have the computer help you in finding logical errors in the programs you write. Things like static type systems or source code analyzers (I hear Eric Lippert works on a fairly popular one these days) help with that.

Have a language which can be processed efficiently by a compiler and which communicates the intent of the programmer to other programmers to make working on the program easier. As an absurd extreme, imagine writing programs in plain english was possible. Fellow programmers might have an easier time to imagine what's going on but still, the description would be very hard to compiler into machine instructors, and it's notoriously ambiguous. So you need a language which a compiler can understan but which is also comprehensible.

Given that a lot of (most?) compilers are still very much general-purpose, they feature a very generic instruction set. There's no "draw a button" instruction or "play this movie". Hence, moving down the abstraction hierarchy makes you end up with programs which are very hard to comprehend and maintain (though trivial to compile). The only alternative is to move up the hierarchy, leading to more and more abstract languages and libraries.

Title: How has an increase in the complexity of systems affected successive generations of programmers?

Body:
As a "new" programmer (I first wrote a line of code in 2009), I've noticed it's relatively easy to create a program that exhibits quite complex elements today with things like .NET framework for example. Creating a visual interface, or sorting a list can be done with very few commands now.

When I was learning to program, I was also learning computing theory in parallel. Things like sorting algorithms, principles of how hardware operates together, boolean algebra, and finite-state machines. But I noticed if I ever wanted to test out some very basic principle I'd learned in theory, it was always a lot more difficult to get started because so much technology is obscured by things like libraries, frameworks, and the OS.

Making a memory-efficient program was required 40/50 years ago because there wasn't enough memory and it was expensive, so most programmers paid close attention to data types and how the instructions would be handled by the processor. Nowadays, some might argue that due to increased processing power and available memory, those concerns aren't a priority.

My question is if older programmers see innovations like these as a godsend or an additional layer to abstract through, and why might they think so? And do younger programmers benefit more learning low-level programming BEFORE exploring the realms of expansive libraries? If so then why?


问:
Best approach for propagating errors in service access layer of a multiplatform mobile app
We've started working on a mobile app using Xamarin. There's a core library that is to be shared among Android and iOS apps. In the core library there will be several functions for communicating with a web service. I've had a discussion with my brother on what the best approach would be, we both have a different opinion, so we'd like to hear your views on the matter. And perhaps there's an even better approach we both didn't think of yet.

The web service we talk to has some predefined HTTP status codes for certain errors. For example when performing verification of an account and information is incorrect, the web service could return a status code 400 and some other errors might result in a different status code.

My approach was the following:

```<code>public async Task<string> VerifyAccountAsync (string email, string password) {
        // prevent null values
        email = email ?? "";
        password = password ?? "";

        var queryString = String.Format ("/VerifyAccount?email={0}&password={1}", email, password);
        var request = WebRequest.Create (_accountUrlString + queryString);
        string result = null;

        try {
                using (var response = await request.GetResponseAsync ()) {
                        using (var stream = response.GetResponseStream ()) {
                                using (var reader = new StreamReader (stream)) {
                                        result = reader.ReadToEnd ();
                                }
                        }
                }
        } catch (WebException ex) {
                // a protocol exception means we got a response, but the status code implies some error occured.
                if (ex.Status == WebExceptionStatus.ProtocolError) {
                        var httpResponse = (HttpWebResponse)ex.Response;

                        switch (httpResponse.StatusCode) {
                        case HttpStatusCode.BadRequest: // 400
                                throw new DepotServiceException (httpResponse, "User could not be found.");
                        case HttpStatusCode.InternalServerError: // 500
                                throw new DepotServiceException (httpResponse, "Some error");
                        default: // TODO: use the response stream to set the error message.
                                throw new DepotServiceException (httpResponse, "Undefined error ...");
                        }
                } else {
                        // some other exception occured, we'll just rethrow it (should not be ignored)
                        throw ex;
                }
        }

        return result;
}
</code>```

My reasoning was the following:

Since the web service uses some predefined status codes for certain errors, we should use a typed exception (DepotServiceException) to make it more clear for the consumer of the core library what's the origination of the error.

Handling exceptions this way in the core library, will prevent having to use if/else constructions in the iPhone / Android visual layers. In the visual layer we won't have to check what the status code was, to show the appropriate error message.

My brother's approach is the following:

```<code>public async Task<DepotServiceResult> RetrieveUrl(string url) {
        DepotServiceResult serviceResult = new DepotServiceResult();
        WebRequest request = WebRequest.Create(url);

        try {
                using (var response = await request.GetResponseAsync()) {
                        var webresponse = (HttpWebResponse)response;
                        using (var stream = response.GetResponseStream()) {
                                serviceResult.statuscode = webresponse.StatusCode;
                                using (var streamReader = new StreamReader(stream)) {
                                        serviceResult.response = streamReader.ReadToEnd();
                                }
                        }
                }
        } catch (WebException ex) {
                serviceResult.errorDescription = ex.ToString();
        }

        return serviceResult;
}

public async Task<DepotServiceResult> VerifyAccountAsync(string email, string password) {
        var url = _urlBase + "api/account/VerifyAccount?email={0}&password={1}";
        email = WebUtility.HtmlEncode (email);
        password = WebUtility.HtmlEncode (password);

        url = String.Format (url, email, password);
        return await RetrieveUrl(url);
}

public class DepotServiceResult {
     public string errorDescription;
     public HttpStatusCode statuscode;
     public string response;
}
</code>```

My brother prefers his approach because he doesn't want the core layer of the app to handle the error. He prefers to handle the error in the UI layer of the app. So for his approach he choose to create a DepotServiceResult object that contains the whole result of the operation, including errors if any occurred.

Personally, I believe my approach is the best, though I have difficulty in explaining why it's the better approach. So if anyone would agree with me, could I get some solid reasons on why it's the better approach? Of course, my brother prefers his approach and if his approach is better, I certainly would like to hear good reasoning on why it's the better one.

If there's an even better approach compared to our preferences, we'd like to hear it as well.

答:
Yours is better in the fact that you treat your corelib as an API. Throwing meaningful exceptions upwards to the application.

His is better for being precautious by encoding the email and password entries.

I really don´t see the need of the DepotServiceResult if not to avoid costly exception handling (which in this case should include a boolean property HasError).


答:
The main difference seems to be that your code converts error responses into exceptions while your brother simply returns them. I believe that exceptions should be used to indicate programming bugs or emergency situations that threaten the integrity of a piece of the system. From this perspective, the library should probably not throw an exception because there is no bug in the code and no emergency as far as it knows. Rather, an external service is down or a user has entered bad input, both of which are basically "normal" situations.

I agree with your brother.

Title: Best approach for propagating errors in service access layer of a multiplatform mobile app

Body:
We've started working on a mobile app using Xamarin. There's a core library that is to be shared among Android and iOS apps. In the core library there will be several functions for communicating with a web service. I've had a discussion with my brother on what the best approach would be, we both have a different opinion, so we'd like to hear your views on the matter. And perhaps there's an even better approach we both didn't think of yet.

The web service we talk to has some predefined HTTP status codes for certain errors. For example when performing verification of an account and information is incorrect, the web service could return a status code 400 and some other errors might result in a different status code.

My approach was the following:

```<code>public async Task<string> VerifyAccountAsync (string email, string password) {
        // prevent null values
        email = email ?? "";
        password = password ?? "";

        var queryString = String.Format ("/VerifyAccount?email={0}&password={1}", email, password);
        var request = WebRequest.Create (_accountUrlString + queryString);
        string result = null;

        try {
                using (var response = await request.GetResponseAsync ()) {
                        using (var stream = response.GetResponseStream ()) {
                                using (var reader = new StreamReader (stream)) {
                                        result = reader.ReadToEnd ();
                                }
                        }
                }
        } catch (WebException ex) {
                // a protocol exception means we got a response, but the status code implies some error occured.
                if (ex.Status == WebExceptionStatus.ProtocolError) {
                        var httpResponse = (HttpWebResponse)ex.Response;

                        switch (httpResponse.StatusCode) {
                        case HttpStatusCode.BadRequest: // 400
                                throw new DepotServiceException (httpResponse, "User could not be found.");
                        case HttpStatusCode.InternalServerError: // 500
                                throw new DepotServiceException (httpResponse, "Some error");
                        default: // TODO: use the response stream to set the error message.
                                throw new DepotServiceException (httpResponse, "Undefined error ...");
                        }
                } else {
                        // some other exception occured, we'll just rethrow it (should not be ignored)
                        throw ex;
                }
        }

        return result;
}
</code>```

My reasoning was the following:

Since the web service uses some predefined status codes for certain errors, we should use a typed exception (DepotServiceException) to make it more clear for the consumer of the core library what's the origination of the error.

Handling exceptions this way in the core library, will prevent having to use if/else constructions in the iPhone / Android visual layers. In the visual layer we won't have to check what the status code was, to show the appropriate error message.

My brother's approach is the following:

```<code>public async Task<DepotServiceResult> RetrieveUrl(string url) {
        DepotServiceResult serviceResult = new DepotServiceResult();
        WebRequest request = WebRequest.Create(url);

        try {
                using (var response = await request.GetResponseAsync()) {
                        var webresponse = (HttpWebResponse)response;
                        using (var stream = response.GetResponseStream()) {
                                serviceResult.statuscode = webresponse.StatusCode;
                                using (var streamReader = new StreamReader(stream)) {
                                        serviceResult.response = streamReader.ReadToEnd();
                                }
                        }
                }
        } catch (WebException ex) {
                serviceResult.errorDescription = ex.ToString();
        }

        return serviceResult;
}

public async Task<DepotServiceResult> VerifyAccountAsync(string email, string password) {
        var url = _urlBase + "api/account/VerifyAccount?email={0}&password={1}";
        email = WebUtility.HtmlEncode (email);
        password = WebUtility.HtmlEncode (password);

        url = String.Format (url, email, password);
        return await RetrieveUrl(url);
}

public class DepotServiceResult {
     public string errorDescription;
     public HttpStatusCode statuscode;
     public string response;
}
</code>```

My brother prefers his approach because he doesn't want the core layer of the app to handle the error. He prefers to handle the error in the UI layer of the app. So for his approach he choose to create a DepotServiceResult object that contains the whole result of the operation, including errors if any occurred.

Personally, I believe my approach is the best, though I have difficulty in explaining why it's the better approach. So if anyone would agree with me, could I get some solid reasons on why it's the better approach? Of course, my brother prefers his approach and if his approach is better, I certainly would like to hear good reasoning on why it's the better one.

If there's an even better approach compared to our preferences, we'd like to hear it as well.


问:
N-Tier MVC4 Web Application with console application
I've built an MVC web application and I need to run several tasks at different time intervals. So I've also made a console application to schedule these tasks through task manager in windows. For example, the application needs to send out an email every week summarizing the program's data. I've chosen to use the Nuget RazorEngine package to format the emails because I already have to show the same data on a webpage too, so by using the RazorEngine package, I can re-use my viewmodel for both the email and the UI of the web project (just use it in a template formatted for email instead of web). But therein lies the problem, the ViewModel is in the UI project. I don't think the right thing to do here is for my console application to take a dependency on my UI layer. The logical decision to me at first was "just make another layer, App.ViewModelBuilders (maybe also help me with a better name?), and take a dependency in both App.UI and App.ConTasks" But I've never seen that done before, so I'm not sure if that is a wise choice or not...

Here's my current layers (projects) for the solution:

App.DAL (No Dependencies - Unit of Work and Repository pattern)

App.Service (Depends on DAL - uses DAL for business operations, such as getDataWithCondition() which returns the relevant objects (it's actually an IQueryable)

App.UI (Depends on Service - gets the data back from service and runs it thru a ViewModelBuilder to map the business objects to the appropriate view model, and then renders a view using that view model)

App.ConTasks (SHOULD depend only on service, but still needs ViewModels - uses data returned from service to render view formatted for email and send the email)

What Layer should my View Models and View Model Builders go in?

答:
View Models (and their builders) can go in App.Service or App.UI.

Your controllers will have to take a dependency on the Service Layer anyway; the controller methods will obtain application data from the Service Layer, marry it with the View Models it obtains from the Service Layer or from App.UI, and return a suitable view.


问:
Is JavaScript "write once run everywhere"?
I have a business requirement for a javascript "write once run everywhere" App. I did research and discovered that there are a few options between: FireFox OS, ChromeOS, NodeOS, Tizen, and Dart.

What I do not know - is if 1) this software is truly capable of allowing a single website to be written once and exported to Hosted or Packaged Native Apps.

2) Can a single JavaScript Website really traverse device-platform as a Native App via this breed of JavaScript Operating System or not?

3) Is it simple, or is it painful? Has anyone seen a full implementation first hand in at least a test environment or production?

Also - are the upsides expected or unexpected - and what are the downsides?

Overall: is JavaScript write once run everywhere - or not?

To the point it isn't 'Native' - I found these.

Apple iOS7 JS first-class citizenship. Last month.
http://fluentconf.com/fluent2014/public/schedule/detail/32624
It is called JavaScriptCore framework.

I installed a FireFox OS Open WebApp with emulator.
https://developer.mozilla.org/en-US/Apps/Quickstart/Build/Your_first_app

I don't know what these are, but it was easy and it looks cool. Like a website packaged into a Native App.

Has any one reviewed this class of JavaScript Architecture?

答: No more than any other language.  There are plenty of inconsistencies between JavaScript engines - in fact, that's why most JavaScript frameworks, including jQuery, get started - trying to smooth those differences over.  And that's before you get into platform-specific aspects.  For example, Palm OS phones used JavaScript as their primary app language.  But the components your code interacted with were highly-Palm OS-specific, so they wouldn't be able to move to Android or iOS.</p>


问:
Should angularjs directive directly interact with services or is it considered an anti-pattern?
Which one is considered better:

having a directive that interacts with services directly

or

having a directive that exposes certain hooks to which controller may bind behaviour (involving services)?

答:
Allow me to disagree with Michal Charemza answer.

Although his answer is theoretically correct, it is not very practical for the real world.

I am saying that because I used to think like that and tried to enforce it on a large real world app that myself and my team are building and it just became too troublesome.

The analogy to the HTML language is not good, because you should not strive to build general purpose, extremely reusable directives, because you're not building a generic application like a web browser.

Instead, you should use the directives to build a Domain Specific Language (DSL) for your app, that lives on its own domain.

That does not mean that all directives should not be generic. Some might be, if it's in their nature. If you're building a custom date picker, by all means make it generic and reusable across apps.

But if you're building something like a login box that binds to your back-end, just do it.

The only rule of thumb should be: do not ever duplicate code (abstract little pieces to factories and services) and make it testable through dependency injection. Fortunately, with Angular, those are a piece of cake.

Keep it simple. :)


问:
How do I architect zip code & distance calculation functionality?
I am building an app using MVC 4/Razor and EF 4/ .NET 4 in which it is possible to search items by zip code and thus retrieving items only a certain distance from your current zip code. I have a class that calculates distance using census gazetteer files (zip, lat, and lon) and the Haversine formula.

My specific problem deals with the SQL portion of my code. I have a view that uses a pager (20 results per page) and has hyper links that order the columns in asc and desc order. I also have 3 drop down lists that can further filter the results down: Manufacturer, Category, Distance.

When I am filtering by say 50 miles my class retrieves an IEnumerable of zipcodes that are within the requested distance. At times this can be in the 10,000 - 30,000 area. Each Item object contains an int ZipCode

The problem EF generates when I try to grab item objects from the database using a list of acceptable zip codes is One of your queries is too deeply nested. Try breaking them up. I am assuming that it is because you can not have a where clause with thousands of conditions. This makes sense. I had found a link a few days ago that basically dealt with creating a temporary table to use as the list in the where condition but can not find the link anymore. It also applied to raw ADO.NET as opposed to EF.

My question is basically, how can I implement the functionality that I want without having make huge changes?

```<code> using (var context = new JDMExchangeEntities())
        {

            var results = context.Items.Include(P => P.Manufacturer).Include(P => P.Category)
                .Include(P => P.VehicleMake).Include(P => P.VehicleModel).Include(P => P.VehicleYear);

           //Category logic
            if (!string.IsNullOrEmpty(Categories))
            {
                results = results.Where(P => P.Category.Name == Categories);
            }

            //Manufacturers logic
            if (!string.IsNullOrEmpty(Manufacturers))
            {
                results = results.Where(P => P.Manufacturer.Name == Manufacturers);
            }

            //Vehicle logic
            if (Vehicle > 0)
            {
                results = results.Where(P => P.YearId == Vehicle);
            }

            if (acceptableCodes.Count > 0)
            {
                List<int> codes = acceptableCodes.Keys.ToList();
                results = results.Where(P => codes.Contains((int)P.ZipCode));

                    //return _UoW.tblcoursebookingRepo.All
   //.Where(cb => AttendanceIDs.Contains(cb.Attended))
   //.ToList();
            }
            ////Take out items that are not in the ManualQueryResults Dictionary
            //if (!String.IsNullOrEmpty(ManualQuery))
            //{
            //    var all = listB.Where(b => listA.Any(a => a.code == b.code));
            //    results = results.Where(r => ManualQueryResults.Any(a => a == r.id));
            //}

            ////Take out zips not in the closeCodes variable
            //if (!string.IsNullOrEmpty(DistanceLimit))
            //{
            //    results = results.Where(P => closeCodes.Any(z => z.ZipCode.Code == P.ZipCode));
            //}

            switch (sortOrder)
            {
                case "Price_desc":
                    results = results.OrderByDescending(P => P.Price);
                    break;
                case "Price":
                    results = results.OrderBy(P => P.Price);
                    break;
                case "Date_asc":
                    results = results.OrderBy(P => P.PostDate);
                    break;
                default:
                    results = results.OrderByDescending(P => P.PostDate);
                    break;

            }
            int pageSize = 20;
            int pageNumber = (page ?? 1);

            IPagedList<Item> cc = results.ToPagedList(pageNumber, pageSize);
</code>```

I could call .ToList() somewhere before I hit the .ToPagedList() but then I will be returning back thousands of entries and thus lagging the response time by a few seconds.

After that I could easily make a paged list from the IEnumerable list. I want to try and minimize response time and so I think the area I need to fix is in the SQL code so as to not pull unnecessary data and then filter it out.

答:
Doing any geospatial work in SQL Server without using the geospatial types is, frankly, insane.

Store the postal code if you want for reference, but query based on latitude and longitude using the geography type. It even has an STDistance function that can use a geospatial index in order to return results in constant time.

Yes, that involves what you'd probably call "huge" changes. Do it. I've seen search systems implemented using the approach you're trying to use and they are, quite frankly, horrible. It's so simple to do a true radius search these days, you're doing yourself and your users a major disservice to hack together a half-assed version like this.

Also, zip codes as numeric? Obviously you're only intending this to be used in the USA, but even so... great way to ensure extreme pain if you ever need or want to globalize.


答:
A huge part of a JaveScript program has to do with the DOM (the objects that JavaScript works on, e.g. typically all of the stuff that shows up on your screen), and that is not actually part of JavaScript but ties a given JavaScript code to it's DOM.

So for example, the JavaScript on FireFox and IE might be similar or even identical, but differences in the DOM mean that the two implementations don't work quite the same when actually run.

It's sort of the idea of the program and the data.  They are intertwined.  JavaScript is code.  DOM is data (in simple terms).

JScript is useful in that it provides an abstraction layer so that things work the same everywhere.

Title: Is JavaScript "write once run everywhere"?

Body:
I have a business requirement for a javascript "write once run everywhere" App. I did research and discovered that there are a few options between: FireFox OS, ChromeOS, NodeOS, Tizen, and Dart.

What I do not know - is if 1) this software is truly capable of allowing a single website to be written once and exported to Hosted or Packaged Native Apps.

2) Can a single JavaScript Website really traverse device-platform as a Native App via this breed of JavaScript Operating System or not?

3) Is it simple, or is it painful? Has anyone seen a full implementation first hand in at least a test environment or production?

Also - are the upsides expected or unexpected - and what are the downsides?

Overall: is JavaScript write once run everywhere - or not?

To the point it isn't 'Native' - I found these.

Apple iOS7 JS first-class citizenship. Last month.
http://fluentconf.com/fluent2014/public/schedule/detail/32624
It is called JavaScriptCore framework.

I installed a FireFox OS Open WebApp with emulator.
https://developer.mozilla.org/en-US/Apps/Quickstart/Build/Your_first_app

I don't know what these are, but it was easy and it looks cool. Like a website packaged into a Native App.

Has any one reviewed this class of JavaScript Architecture?


问:
Does Exception Handling Violates "Program to Abstraction"?
I am talking based on experience with Java and C#. I do not know if other language have different exception handling implementation.

In order to achieve loose coupling, we need our code being programmed to use abstraction rather than implementation. However the exception handling case is the opposite. The best practice is you need to handle specific exception type (SqlException, StackOverflowException, etc).

This thing may be better (or not) in java thanks for it's Checked Exception, there is a kind of "contract" between the interface and the consumer. But in C# or for the Unchecked Exception there is no contract about what exception can be thrown by the interface.

For example, say that we use Repository Pattern to decouple the DAL with BLL. The simple catch exception usually be used like:

```<code>public void Consume()
{
    try{
        productRepository.Get(k=>k.Id == "0001");
    }
    catch(Exception e){
        // handle
    }
}
</code>```

In more specific case we usually use SqlException. However it means that we must know that the ProductRepository is a repository to database server. What if the implementation changed to use file repository instead? Now you need to catch FileNotFoundException or something like that.

Why does it violates the "code to abstraction" principle? And what can we do to prevent it?

答:
You should handle a specific case where it's appropriate to handle that specific case.

In your example, the consumer is not the appropriate abstraction level to handle a SqlException, as you've surmised.

The repository itself, which knows it is retrieving from a database, should be what handles the SqlException, perhaps repackaging it into a more application-specific and custom DataAccessException that can then be handled appropriately by the consumer.

If you change the repository implementation to pull data from, say, a web service, that repository can handle an HttpException and repackage it into the DataAccessException. Or an XML repository can handle XML-specific exceptions and repackage them. And so forth.


问:
Is it sufficient to use acceptance and integration tests instead of unit test?
Short introduction to this question. I have used now TDD and lately BDD for over one year now.  I use techniques like mocking to make writing my tests more efficiently. Lately I have started a personal project to write a little money management program for myself. Since I had no legacy code it was the perfect project to start with TDD. Unfortunate I did not experience the joy of TDD so much. It even spoiled my fun so much that I have given up on the project.

What was the problem? Well, I have used the TDD like approach to let the tests / requirements evolve the design of the program. The problem was that over one half of the development time as for writing / refactor tests. So in the end I did not want to implement any more features because I would need to refactor and write to many test.

At work I have a lot of legacy code. Here I write more and more integration and acceptance tests and less unit tests. This does not seem to be a bad approach since bugs are mostly detected by the acceptance and integration tests.

My idea was, that I could in the end write more integration and acceptance tests than unit tests. Like I said for detecting bugs the unit tests are not better than integration / acceptance tests. Unit test are also good for the design. Since I used to write a lot of them my classes are always designed to be good testable. Additionally, the approach to let the tests / requirements guide the design leads in most cases to a better design. The last advantage of unit tests is that they are faster. I have written enough integration tests to know, that they can be nearly as fast as the unit tests.

After I was looking through the web I found out that there are very similar ideas to mine mentioned here and there. What do you think of this idea?

Edit

Responding to the questions one example where the design was good,but I needed a huge refactoring for the next requirement:

At first there were some requirements to execute certain commands. I wrote an extendable command parser - which parsed commands from some kind of command prompt and called the correct one on the model. The result were represented in a view model class:

There was nothing wrong here. All classes were independent from each other and the I could easily add new commands, show new data.

The next requirement was, that every command should have its own view representation - some kind of preview of the result of the command. I redesigned the program to achieve a better design for the new requirement:

This was also good because now every command has its own view model and therefore its own preview.

The thing is, that the command parser was changed to use a token based parsing of the commands and was stripped from its ability to execute the commands. Every command got its own view model and the data view model only knows the current command view model which than knows the data which has to be shown.

All I wanted to know at this point is, if the new design did not break any existing requirement. I did not have to change ANY of my acceptance test. I had to refactor or delete nearly EVERY unit tests, which was a huge pile of work.

What I wanted to show here is a common situation which happened often during the development. There were no problem with the old or the new designs, they just changed naturally with the requirements - how I understood it, this is one advantage of TDD, that the design evolves.

Conclusion

Thanks for all the answers and discussions. In summary of this discussion I have thought of an approach which I will test with my next project.

First of all I write all tests before implementing anything like I always did.

For requirements I write at first some acceptance tests which tests the whole program. Then I write some integration tests for the components where I need to implement the requirement. If there is a component which work closely together with another component to implement this requirement I would also write some integration tests where both components are tested together. Last but not least if I have to write an algorithm or any other class with a high permutation - e.g. a serializer - I would write unit tests for this particular classes. All other classes are not tested but any unit tests.

For bugs the process can be simplified. Normally a bug is caused by one or two components. In this case I would write one integration test for the components which tests the bug. If it related to a algorithm I would only write a unit test. If it is not easy to detect the component where the bug occurs I would write an acceptance test to locate the bug - this should be an exception.

答:
The last advantage of unit tests is that they are faster. I have written enough integration tests to know, that they can be nearly as fast as the unit tests.

This is the key point, and not only "the last advantage". When the project gets bigger and bigger, your integration acceptance tests are becoming slower and slower. And here, I mean so slow that you are going to stop executing them.

Of course, unit tests are becoming slower as well, but they are still more than order of magnitude faster. For example, in my previous project (c++, some 600 kLOC, 4000 unit tests and 200 integration tests), it took about one minute to execute all and more then 15 to execute integration tests. To build and execute unit tests for the part being changed, would take less then 30 seconds on average. When you can do it so fast, you'll want to do it all the time.

Just to make it clear: I do not say not to add integration and acceptance tests, but it looks like you did TDD/BDD in a wrong way.

Unit test are also good for the design.

Yes, designing with testability in mind will make the design better.

The problem was that over one half of the development time as for writing / refactor tests. So in the end I did not want to implement any more features because I would need to refactor and write to many test.

Well, when requirements change, you do have to change the code. I would tell you didn't finish your work if you didn't write unit tests. But this doesn't mean you should have 100% coverage with unit tests - that is not the goal. Some things (like GUI, or accessing a file, ...) are not even meant to be unit tested.

The result of this is better code quality, and another layer of testing. I would say it is worth it.

We also had several 1000s acceptance tests, and it would take whole week to execute all.


答:
It depends heavily what kind of exception you are talking about. Exceptions are meant to be exceptional, but some aren't.

For example, lets say interface method IProductRepo.Get has implied condition, that it always returns a product. But for some reason, the ProductRepo implementation of it can throw an exception if the product doesn't exist. In this case you can handle the exception in Consume and act appropriately in a case when product is not found. But like you assume, this is violation of abstraction, more correctly a violation of LSP as discussed here. This usually implies your abstraction is wrong and you should remodel it to correctly capture the additional case. In this example, you could say the Get method might return null if product is not found. This kind of exception can also happen if you have some assertions in your code and they fail. But this usually means bug in your code somewhere.

On the other side, there are truly exceptional exceptions or exceptions your application cannot predict. For example, loosing a connection to the DB is exceptional. The thing with those kind of exceptions is that they cannot be handled properly within the logic of the application. For example, if you did handle it like in your example, you would equate the case of not finding a product and loosing a connection and saying it is a same case. Which is obviously wrong. Those kind of exceptions can only be handled in generic way on program's boundary. In case of UI application, all you can possibly do is to display "The operation cannot be finished because: (reason here). Try again later." and hope the user doesn't fill it as a bug report. There is no violation here, because it doesn't extend possible cases the caller has to handle. So it doesn't extend the contract.

Title: Does Exception Handling Violates "Program to Abstraction"?

Body:
I am talking based on experience with Java and C#. I do not know if other language have different exception handling implementation.

In order to achieve loose coupling, we need our code being programmed to use abstraction rather than implementation. However the exception handling case is the opposite. The best practice is you need to handle specific exception type (SqlException, StackOverflowException, etc).

This thing may be better (or not) in java thanks for it's Checked Exception, there is a kind of "contract" between the interface and the consumer. But in C# or for the Unchecked Exception there is no contract about what exception can be thrown by the interface.

For example, say that we use Repository Pattern to decouple the DAL with BLL. The simple catch exception usually be used like:

```<code>public void Consume()
{
    try{
        productRepository.Get(k=>k.Id == "0001");
    }
    catch(Exception e){
        // handle
    }
}
</code>```

In more specific case we usually use SqlException. However it means that we must know that the ProductRepository is a repository to database server. What if the implementation changed to use file repository instead? Now you need to catch FileNotFoundException or something like that.

Why does it violates the "code to abstraction" principle? And what can we do to prevent it?


答:
But exceptions are part of the contract!

You need to define which exceptions are to be handled in the interface.

Just as rental lease might specify "you must insure against fire damage" then an interface can specify that you have to handle SqlException.

In java at least the compiler gives an error if an implementation throws an exception not explicitly mentioned in the interface.

That is not to say that exceptions should be blindly passed through to the parent. As far as possible exceptions should be handled inside any implementation but sometimes this is just not possible. An implementation cannot hide the fact that it cannot connect to its data source be it a database, web service or whatever.


问:
How to approach design to minimize conflicts when distributing individual locations to merchants
One of my potential customers is operating an open market in their town.

From what I'm told, they make a decent profit by renting out the individual locations on that market to small merchants who come there every day to sell produce and other goods you normally find in such places.

Each merchant needs to pay some fixed amount up front to receive a seasonal contract that grants them the right to sell their goods on the market during given season.

The contract does not, however, specify at which micro-location the merchant will be stationed, or indeed, on which days they will be present. The only thing the contract grants them, is the right to sell certain types of goods and during which season.

For each day that they are actually present on the market, a cashier comes to their spot and collects the daily rent for that spot.

Now, certain micro-locations have more traffic than others, so there is bound to be some competition. Which is good, normally. Established merchants wish to retain their spot so their loyal customers can find them.

The problem is that the existing "mechanism" of reservation is basically first-come, first-(re)served. In reality, this means at around 02:00 AM, merchants start appearing and placing all their stuff on their sweet spots in order to prevent others from doing the same. At times, competition can be quite literal. Things escalated into a physical altercation a couple of times in the past, so an overseer is now required to be present during the night before busiest days of the season.

The customer has now asked me to come up with a software-based solution for this problem. An idea for a web and/or mobile app has been tossed around, but whatever the form, it would have to support the following features:

merchant authentication,

selection of a preferred micro-location for the next day,

advance payment of the daily rent via some online method.

While I was reading through the requirements, one thing immediately jumped out at me: I cannot possibly do this in an app form! Here's what I think it will happen, if I do anyway:

In the first days and weeks, merchants that know about and use the app, will have an unfair advantage over merchants that don't. It would be easy to assume this problem will self-correct quickly enough, but it would be a wrong assumption, IMHO. Many of the small merchants are in reality older, computer-illiterate people.

Imagine a kind, old lady selling flowers, for example. She will never use the app, because it's space technology to her. But she will continue to show up at 02:00 AM and happily place her flowers on the same spot as the day before. Only to learn 4 hours later that someone else had already reserved and payed for that spot yesterday, from their home, using a computer!

How is she to compete with that?

But things don't end here... Let's assume that, somehow, she does learn to use the app (or gets someone else do do it for her, which is more likely). Now, everyone is using the app to reserve their spot. Fast forward a couple of weeks, and this is what we get:

Joe S. Merchant logs into the app at 05:10 PM, 10 minutes after the reservations for the next day have opened. To his surprise, almost all the spots have already been taken, including his usual one. Now he can either hope someone will not show up so he can take their spot at the last minute, or he can accept he's been too late and he lost a whole business day because of this confounded computer thing!

Naturally, I've explained my views to the customer... And they agree it's a problem. They wouldn't want to make a bad situation worse just to have a more convenient way of collecting daily rent.

We both believe the present system could stand improvement. The basis of present system is competition and that is probably here to stay. The problem is that currently the competition is directly based on physical effort (i.e. how early a person is willing to wake up and travel to the market place to get the spot). This effort is in turn a natural limiting factor on the (un)fairness of the whole competition.

By introducing ANY software substitute, this key factor is effectively gone! It reduces the problem to who is faster in the first minutes when reservations start. There's no real effort involved anymore which automatically appears unfair!

So we feel like the rules have to be adjusted somehow. We've tossed around some ideas that could make the online competition more fair. Ideas like:

1. Building a reputation based system

This one brings in a lot of extra complexity, not to mention responsibility of tracking and assessing the various factors that contribute to reputation score. Most of these cannot be automated which means they would incur extra cost to the customer.

2. Randomizing the start of each day's reservation window

This one has the potential of lessening the competition pressure in the first minutes of the reservation window, giving it an appearance of being more fair. Yet, I fear it would only annoy the merchants.

What both me and my customer agree about, is that we absolutely CANNOT implement a reservation mechanism based on bidding. That would be in violation of financial laws the customer's company must abide by which call for a fixed rent in this situation.

I refuse to believe that the best solution is the one that presently exists, even if merchants are used to coming in at 02:00 AM and waiting 4 hours for the market to actually open.

I turn to this community to ask: How can I earnestly better the present system with a software component? What options am I missing?

答:
Some kind of lottery system for reservations? People register interest in a space and maybe list preferences. Then run a random lottery? You have new removed the first come first served aspect. You could even give people some kind of preference but not by money - even if that was allowed in the terms it would probably piss traders off. Maybe more established traders get a weighting over newer ones?

One thing you haven't mentioned here is what customers expect. The markets I go to, I'm looking for one or two of my favourite stalls and they tend to be in the same place. If they moved regularly that could get annoying. Some kind of traders books a space permanently system, for the good of the customer? Altho if you do do that then the problem becomes who gets to book what space permanently, and the stakes are much higher, so you've just made yourself a different problem :-)


答:
I agree that random lottery is a good solution.  One system would be to have merchants register interest for a block of time (maybe a week or a month).  Then roughly equal numbers of days could be allocated to each merchant.

Another would be to have merchants pay a fixed price for a fixed number of points.  They could then bid these points for specific spots/days in an auction.  A fixed amount of cash is changing hands but the market decides if a spot up the front on a busy day is worth more than a several days at a less ideal location

Title: How to approach design to minimize conflicts when distributing individual locations to merchants

Body:
One of my potential customers is operating an open market in their town.

From what I'm told, they make a decent profit by renting out the individual locations on that market to small merchants who come there every day to sell produce and other goods you normally find in such places.

Each merchant needs to pay some fixed amount up front to receive a seasonal contract that grants them the right to sell their goods on the market during given season.

The contract does not, however, specify at which micro-location the merchant will be stationed, or indeed, on which days they will be present. The only thing the contract grants them, is the right to sell certain types of goods and during which season.

For each day that they are actually present on the market, a cashier comes to their spot and collects the daily rent for that spot.

Now, certain micro-locations have more traffic than others, so there is bound to be some competition. Which is good, normally. Established merchants wish to retain their spot so their loyal customers can find them.

The problem is that the existing "mechanism" of reservation is basically first-come, first-(re)served. In reality, this means at around 02:00 AM, merchants start appearing and placing all their stuff on their sweet spots in order to prevent others from doing the same. At times, competition can be quite literal. Things escalated into a physical altercation a couple of times in the past, so an overseer is now required to be present during the night before busiest days of the season.

The customer has now asked me to come up with a software-based solution for this problem. An idea for a web and/or mobile app has been tossed around, but whatever the form, it would have to support the following features:

merchant authentication,

selection of a preferred micro-location for the next day,

advance payment of the daily rent via some online method.

While I was reading through the requirements, one thing immediately jumped out at me: I cannot possibly do this in an app form! Here's what I think it will happen, if I do anyway:

In the first days and weeks, merchants that know about and use the app, will have an unfair advantage over merchants that don't. It would be easy to assume this problem will self-correct quickly enough, but it would be a wrong assumption, IMHO. Many of the small merchants are in reality older, computer-illiterate people.

Imagine a kind, old lady selling flowers, for example. She will never use the app, because it's space technology to her. But she will continue to show up at 02:00 AM and happily place her flowers on the same spot as the day before. Only to learn 4 hours later that someone else had already reserved and payed for that spot yesterday, from their home, using a computer!

How is she to compete with that?

But things don't end here... Let's assume that, somehow, she does learn to use the app (or gets someone else do do it for her, which is more likely). Now, everyone is using the app to reserve their spot. Fast forward a couple of weeks, and this is what we get:

Joe S. Merchant logs into the app at 05:10 PM, 10 minutes after the reservations for the next day have opened. To his surprise, almost all the spots have already been taken, including his usual one. Now he can either hope someone will not show up so he can take their spot at the last minute, or he can accept he's been too late and he lost a whole business day because of this confounded computer thing!

Naturally, I've explained my views to the customer... And they agree it's a problem. They wouldn't want to make a bad situation worse just to have a more convenient way of collecting daily rent.

We both believe the present system could stand improvement. The basis of present system is competition and that is probably here to stay. The problem is that currently the competition is directly based on physical effort (i.e. how early a person is willing to wake up and travel to the market place to get the spot). This effort is in turn a natural limiting factor on the (un)fairness of the whole competition.

By introducing ANY software substitute, this key factor is effectively gone! It reduces the problem to who is faster in the first minutes when reservations start. There's no real effort involved anymore which automatically appears unfair!

So we feel like the rules have to be adjusted somehow. We've tossed around some ideas that could make the online competition more fair. Ideas like:

1. Building a reputation based system

This one brings in a lot of extra complexity, not to mention responsibility of tracking and assessing the various factors that contribute to reputation score. Most of these cannot be automated which means they would incur extra cost to the customer.

2. Randomizing the start of each day's reservation window

This one has the potential of lessening the competition pressure in the first minutes of the reservation window, giving it an appearance of being more fair. Yet, I fear it would only annoy the merchants.

What both me and my customer agree about, is that we absolutely CANNOT implement a reservation mechanism based on bidding. That would be in violation of financial laws the customer's company must abide by which call for a fixed rent in this situation.

I refuse to believe that the best solution is the one that presently exists, even if merchants are used to coming in at 02:00 AM and waiting 4 hours for the market to actually open.

I turn to this community to ask: How can I earnestly better the present system with a software component? What options am I missing?


问:
In TDD, is it bad practice to pass a test with code that would pass more than one test?
When using TDD, is it bad practice to pass a newly written test with code that could also pass another test? For example, take the following test (in PHP)

```<code>public function WhenSomethingIsNull_ThrowsException()
{
    $input = null;
    $this->module->someMethod($input);
}
</code>```

This could be made to pass with the following code

```<code>public function someMethod($input)
{
    if(!$input)
        throw new Exception()
}
</code>```

This would also pass the test WhenSomethingIsZero_ThrowsException. In these cases, should I fix what I know is wrong with this test in the refactoring step? Or only write code which passes this and only this test?

答:
No, it isn't bad practice.

The point of TDD is to make sure that all your code is covered by tests. The rule that every test should test one thing only is to promote readability of tests, and is in general reasonable.

But the idea that every possible defect in your code base should trip exactly one test is overkill, and in real-life projects you'll almost never be able to manage such a precise fine-tuning. That's no problem as long as every possible defect raises at least one error in your test suite. Since the point of having a test suite is to make sure it passes all tests at all times, this guarantees the necessary coverage well enough.


问:
How to use mock objects [C++] without passing them as arguments to functions
I'm in the process of integrating a Unit Testing Framework for an existing code base in C++. I've zeroed down on CxxTest, but as it turns out we can use other Mocking Frameworks (like googlemock) in conjunction with CxxTest too.

After reading tutorials on CxxTest (Mocking) and googlemock (the infamous turtle example), I have the general idea that you have to define a mock class (using macros etc), declare an object of the mock class and then pass it to the function you are unit testing. Now, there are many occurences in the existing code base where it isn't possible to do that.

Here's an pseudo example to clarify:

```<code>class CCandidateForTest
{
    public:
    bool foo(int a)
    {
        CAnotherClass obj;
        int b = obj.bar(a+2);
        if (a == b) {
            return true;
        } else {
            return false;
        }
    }
}
</code>```

(This is over-simplified, there are also exceptions and the primitive types may also be other objects etc.; but you get the general idea) (also, creation of objects is not always direct and may sometimes use a factory)

I want to write a test for the function CCandidateForTest::foo. This method internally creates an object of CAnotherClass, which I need to mock so that CAnotherClass::bar returns different values so that different code paths in foo are traversed in unit testing.

In a nutshell, the problem is that the function being tested internally creates an object of the class that needs to be mocked - hence passing an instance of the mocked object to the function is not possible.

How do I use mocking in such a case? Is there a specific mocking framework that makes this possible?

答:
If a class internally creates objects to do its job, then that object isn't a "collaborator" at all. For testing purposes, this is exactly the same situation as if it had a lot of inline code doing the same thing. In other words, you can't mock this object because from the perspective that the test has to assume, it doesn't exist!

(This may be an indicator of your class doing too much, and it can be an argument when weighing whether to refactor the class so that it does have a true collaborator, but it is only one factor - there may be other good reasons to leave the class unchanged and test its behaviour as well as possible without mocking.)


答:
You could use a factory to create instances of CAnotherClass.

```<code>class CCandidateForTest
{
    public:
    bool foo(int a)
    {
        std::shared_ptr< CAnotherClassInterface > obj = CAnotherClassFactory::Create();
        int b = obj->bar(a+2);
        if (a == b) {
            return true;
        } else {
            return false;
        }
    }
}
</code>```

CAnotherClassFactory can look like :

```<code>class CAnotherClassFactory
{
public:
    typedef std::shared_ptr< CAnotherClassInterface > (*fn)();
    static std::shared_ptr< CAnotherClassInterface > Create()
    {
        return createFn();
    }

    static fn createFn;
};
</code>```

Random thoughts about your approach :

If that is over-simplified, then you need to think how to refactor it.

there are better unit test frameworks then cxxunit. gtest and cute-test.com

Title: How to use mock objects [C++] without passing them as arguments to functions

Body:
I'm in the process of integrating a Unit Testing Framework for an existing code base in C++. I've zeroed down on CxxTest, but as it turns out we can use other Mocking Frameworks (like googlemock) in conjunction with CxxTest too.

After reading tutorials on CxxTest (Mocking) and googlemock (the infamous turtle example), I have the general idea that you have to define a mock class (using macros etc), declare an object of the mock class and then pass it to the function you are unit testing. Now, there are many occurences in the existing code base where it isn't possible to do that.

Here's an pseudo example to clarify:

```<code>class CCandidateForTest
{
    public:
    bool foo(int a)
    {
        CAnotherClass obj;
        int b = obj.bar(a+2);
        if (a == b) {
            return true;
        } else {
            return false;
        }
    }
}
</code>```

(This is over-simplified, there are also exceptions and the primitive types may also be other objects etc.; but you get the general idea) (also, creation of objects is not always direct and may sometimes use a factory)

I want to write a test for the function CCandidateForTest::foo. This method internally creates an object of CAnotherClass, which I need to mock so that CAnotherClass::bar returns different values so that different code paths in foo are traversed in unit testing.

In a nutshell, the problem is that the function being tested internally creates an object of the class that needs to be mocked - hence passing an instance of the mocked object to the function is not possible.

How do I use mocking in such a case? Is there a specific mocking framework that makes this possible?

Body:
You could use a factory to create instances of CAnotherClass.

class CCandidateForTest
{
    public:
    bool foo(int a)
    {
        std::shared_ptr< CAnotherClassInterface > obj = CAnotherClassFactory::Create();
        int b = obj->bar(a+2);
        if (a == b) {
            return true;
        } else {
            return false;
        }
    }
}

CAnotherClassFactory can look like :

class CAnotherClassFactory
{
public:
    typedef std::shared_ptr< CAnotherClassInterface > (*fn)();
    static std::shared_ptr< CAnotherClassInterface > Create()
    {
        return createFn();
    }

    static fn createFn;
};

Random thoughts about your approach :

If that is over-simplified, then you need to think how to refactor it.

there are better unit test frameworks then cxxunit. gtest and cute-test.com


问:
Implementation of algorithm to generate chess positions
I am interested in developing a program that generates an endgame table-base for the chess.

I have gone through the technical papers of the greats like Shannon and Ken Thompson. I have found that retrograde analysis is used for the generation.

How can I generate all positions in a KBBK chess endgame which are mates in 0?

答:
Let's look at the problem in a more general way. You probably want to have tables for all combinations of pieces. Also, you probably have a chess engine that can generate possible moves, execute moves, detect check and checkmate situations, evaluate positions, etc. I have, and I use the following algorithm:

In 4 nested loops, generate all possible positions. Skip illegal positions. This nest is executed many times. In the first pass, detect all checkmate positions (= black has no moves and is in check).
In following passes, execute all possible moves and see if they lead to a position already covered. Add passes until no more solutions are found. Table values must be consistent with the move evaluating system of your chess engine (probably with a conversion formula). I use one byte per table entry.

Table size for a 4-piece endgame is:

no pawns: 2*10*64*64*64 (using horizontal, vertical and diagonal mirroring)

with pawns: 2*32*64*64*64 (using horizontal mirroring only)

In a general algorithm, you can not use shortcuts, but have to use the brute force method. I generate my 4-piece tables in app. 1 minute each on a high end PC.