问:
What is the most egregious waste of money you have seen, and what did you do about it?
Often we as programmers see large organisations wasting huge sums of money on bloated and inefficient solutions to problems. This pains me greatly because I like organisations to benefit from best of breed solutions. However, my abilities as a programmer are limited when it comes to influencing the key decision makers and often my perspective on the matter is constrained to my own little technical world.

So, my question is this. After encountering an egregious waste of money on some software and/or hardware that really got your goat, what did you do about it to get it fixed or were you doomed to bite the bullet and mutter forever under your breath? I'm interested in hearing your overall experiences and especially what lessons you learned about how to tackle this sort of thing in the future. Let's not name names, the experience of how to tackle the problem is more important than the actual offending product.

答:
Status meetings & weekly reports

An organization I worked in was all about weekly status reports - rolled up at 3 different levels. The dev leads and test leads for each of the 4-6 projects in flight report their progress in a lengthy email, which then get rolled up by the next manager up, which in turn gets arbitrarily summarized by the next one.

The following business day, all project leads gather in a 1 hour meeting to go over the report.

Effectively one day each week is spent on reporting that week's progress. Keep in mind that this is all separate from the daily standups and weekly demo / retrospective meetings.


问:
Is it plausible to use a filesystem-based configuration format rather than an INI file?
After having had endless issues with INI-style configuration files and parsing them correctly (let along getting binary data right), a crazy idea crossed my mind recently:

What if an INI file like

```<code># settings.ini
[Windows]
DirectorySeparator='\\'
PathSeparator=';'

[Linux]
DirectorySeparator='/'
PathSeparator=':'
</code>```

Would rather be stored in the file system; each group is a directory, each key/value pair is a file, so

```<code>settings/Windows/DirectorySeparator (contains just `\`)
settings/Windows/PathSeparator      (contains just ';')
... you get the idea ...
</code>```

Disadvantages:

Possibly slow due to IO work, but maybe file system caches help?

Running low of inodes on some file systems?

Group/Key names may only consists of characters which are also valid for directory/filenames, may be rather restrictive depending on the FS.

Advantages:

Arbitrarily deeply nested groups

No worries about escaping, binary data is easy. Just have each file contain the raw data.

Automatic locking, access control and whatever the filesystem provides

Easy to access with most programming languages, even by hand

I'm very tempted to try this in my next project, I wonder whether anybody else has experience with this approach already?

答:
Here are some considerations not mentioned so far, that may or may not be relevant, depending on the application you would want to use this for.

Integrity check of the configuration

With a single config file, take a cryptographic hash. Done. If it differs next time you read the file, then you know it was manually modified or got corrupted.

With a filesystem based config, you need to traverse the whole tree, reading items in a specified order before feeding them one by one to the hash. Not only the file content, but also the directory and file names. Quite some things to do.

Related to this,

Robustness

Even if you have integrity checks, then there is so much more that could go wrong within a file system. Just think about a user looking through the files, moving some files/directories to other locations, with his uncareful mouse movements and a nervous finger.

How would you handle a link to a parent directory that throws you into an infinite loop when you traverse it unchecked. With the right corruption that is not checked by your application, it may as well be rendered completely broken, requiring an uninstall, manual cleanup and a reinstall.

A backup of a corrupted config file is so much easier to restore.

Talking about backups,

Power users and administrators

What may they want to do?

Modify it in ways that are useful, but not possible with your builtin configuration dialogs.

Backup and restore of the config.

Export/import of the config.

Transfer the config to a friend or to their second machine.

Rollout a mandatory config to a large number of machines and users.

With a single, text based config file, everything you need is already built into the OS: Copy, move and rename of files, a text editor, copy and paste. The transfer is as easy as attaching a file to a mail.

With the filesystem, everything that is not provided by your application is hard to do. Manually modifying a text option? Better take care not to add an additional End Of Line. Modifying a binary option? Hmm, hex editor?

Transfer the config by mail? You need additional tools: Build a zip file and attach it. Then hope that the binary options inside won't trigger the mail virus scanner and that it gets through.

For all practical purposes, a filesystem based config is as good as a black box for a non-programmer.

EDIT: Cluster size

A separate file for each option may use quite a lot of hard disk space. When each 5 byte option eats a full 64KB cluster, and you have a lot of them... Do the math.

ContentLicense: CC BY-SA 3.0


问:
How do you unit test an encoder?
I have something like this:

```<code>public byte[] EncodeMyObject(MyObject obj)
</code>```

I've been unit testing like this:

```<code>byte[] expectedResults = new byte[3]{ 0x01, 0x02, 0xFF };
Assert.IsEqual(expectedResults, EncodeMyObject(myObject));
</code>```

EDIT: The two ways I've seen proposed are:

1) Using hardcoded expected values, like the above example.

2) Using a decoder to decode the encoded byte array and comparing the input/output objects.

The problem I see with method 1 is that it is very brittle and requires a lot of hard coded values.

The problem with method 2 is that testing the encoder depends on the decoder working correctly. If the encoder/decoder are broken equally (in the same place), then the tests could produce false positives.

These may very well be the only ways to test this type of method. If that's the case then fine. I'm asking the question to see if there are any better strategies for this type of testing. I can not reveal the internals of the particular encoder I am working on. I am asking in general how you would solve this type of problem, and I don't feel the internals are important. Assume that a given input object will always produce the same output byte array.

答:
You get to decide what is important to you.

Is it important to you that an Object survives the round trip, and the exact wire format isn't really important? Or is the exact wire format an important part of the functionality of your encoder and decoder?

If the former, than just make sure that objects survive the round trip. If the encoder and decoder are both broken in exactly complementary ways, you don't really care.

If the latter, then you need to be testing that the wire format is as you expect for the given inputs. This means either testing the format directly, or else using a reference implementation. But having tested the basics, you may get value from additional round-trip tests, which should be easier to write in volume.

Title: How do you unit test an encoder?


答:
Test to the requirements.

If the requirements is only 'encode to a byte stream that when decoded produces an equivalent object.', then just test the encoder by decoding.  If you are writing both the encoder and the decoder, then just test them together.  They can't have "matching errors".  If they work together, then the test passes.

If there are other requirements for the data stream, then you will have to test them by examining the encoded data.

If the encoded format is predefined, then either you will have to verify the encoded data against the expected result, as you did, or (better) obtain a reference decoder that can be trusted to do the verification.  Use of a reference decoder eliminates the possibility that you have misinterpreted the format specification.

Title: How do you unit test an encoder?

Body:
I have something like this:

```<code>public byte[] EncodeMyObject(MyObject obj)
</code>```

I've been unit testing like this:

```<code>byte[] expectedResults = new byte[3]{ 0x01, 0x02, 0xFF };
Assert.IsEqual(expectedResults, EncodeMyObject(myObject));
</code>```

EDIT: The two ways I've seen proposed are:

1) Using hardcoded expected values, like the above example.

2) Using a decoder to decode the encoded byte array and comparing the input/output objects.

The problem I see with method 1 is that it is very brittle and requires a lot of hard coded values.

The problem with method 2 is that testing the encoder depends on the decoder working correctly. If the encoder/decoder are broken equally (in the same place), then the tests could produce false positives.

These may very well be the only ways to test this type of method. If that's the case then fine. I'm asking the question to see if there are any better strategies for this type of testing. I can not reveal the internals of the particular encoder I am working on. I am asking in general how you would solve this type of problem, and I don't feel the internals are important. Assume that a given input object will always produce the same output byte array.


问:
Question on System Requirement
I have a scenario where a specific webservice must be consumed based on the state to setup user info. For instance if the state is NY or NJ call service A , if state is WA call service B etc. But specifying what service must be called is I guess is "HOW" to setup a user info and not a "WHAT".

So should I just have single requirement stating system must have the capbality to setup up user info based on state?

EDIT: I would also like to add that the services implemented for different states might want different info to be provided in addition to basic details such as first name and last name. Should the services , input to the services , output from the service , what the program needs to do with the output etc be part of system requirements? I guess all these deal with "HOW" more than "WHAT" - hence is it in the requirements realm?

答: As described, it sounds like a design decision, not a system requirement.  I think the requirement should sound like this: "The marklar depends on the user's state of residence.  For users in NY, NJ ... .  For users in WA, ... etc." or "The marklar depends on the user's state of residence, and is calculated according to the following table: ..."</p>

Title: Question on System Requirement


问:
Have there been disputes when software identifies genders with a boolean?
I remember that in our first programming class with Java, while explaining data types, the following occurred (kinda):

Professor: So, what data type would you choose if your program needs
  to store the user's gender?

Someone: How about a boolean? You know, true for male and false for
  female.

Professor: Sure, that may work, but some people may hesitate about that. There
  have been disputes about calling men "true" and women "false" in the
  past...

*laughter *

He ended up recommending us to consider chars (like 'm' and 'f') although booleans should be fine.

I tried searching a bit if there has been any kind of historical entry regarding a major dispute based on this programming practice with no luck.

I'm not asking what data type to use for gender nor if it is fine or not to use booleans. I'm asking if, historically, there has been a dispute regarding the usage of booleans to determine the gender in programming because of the apparently "wrong" (I'm not saying it is wrong - I don't care about that) behavior of calling a woman "false" as the professor seemed to suggest.

Searching around related questions, the results happen to be only about software efficiency.

答:
This is more a cultural issue then technical, and represents a society's perspective on gender.

If you studied in an Islamic country or China, then I can understand why this would be an issue.

So the answer is no, I've never heard of such a silly issue in programming.


问:
Functional Requirements / Specification and relationship to DEV and QA
Functional Requirements state "WHAT" needs to be done from a user's perspective
Functional Specification state "HOW" it needs to be done from technical standpoint.

Do QA teams focus and Functional Requirements whereas development team focus primarily on functional specification though they need to have an understanding of functional requirements?

I assume QA would sign off the product as long as the functional requirements are met irrespective of whether the functional specification is followed or not. Is my assumption correct?

答:
Q: Do QA teams focus and Functional Requirements whereas development team focus primarily on functional specification though they need to have an understanding of functional requirements?

The two are closely related. For small efforts, the requirements can be developed to a level of detail where they can also be used as the specification. In general, Functional Requirements are the customer-centric statement of what a system should do. Functional Specifications are a more detailed description of the Functional Requirements. For example:

Rqmt: The customer shall be able to change the upper alarm limit temperature

Spec: Selecting the parameter::alarms::temperature menu item shall bring up a pop-up form that allows the user to enter a value between 50 and 100 for the alarm limit.

Functional specifications are still in the realm of describing what a system does, not how it does it. Because of this, QA teams should be interested in the Functional Specifications for several reasons, among them are:

The QA team needs to ensure that proper reviews have been covered to determine that the Functional Specifications are consistent with the requirements, and that they are complete.

Each of the Functional Specifications are testable, so the QA team needs to make sure the tests are defined and the software product passes those tests.

Q: I assume QA would sign off the product as long as the functional requirements are met irrespective of whether the functional specification is followed or not. Is my assumption correct?

Whether or not QA signs off on a product that deviated from the specification depends in part on what process was followed when decisions were made to make changes. If the development team followed the change processes, QA would not have any problems. (Note -- these processes should insure that proper communications about these changes occur). However if a development team goes off and decides on their own there are a lot of risks that even though their efforts satisfy the requirements, their efforts may be rejected by QA, other business units in their organization, and even the end users.

That is not to say that a development team is bound by the specifications. It is OK for a development team to deliver something that deviates from the original specifications. However making decisions to implements something different that what is in the specifications should be made unilaterally. Mature organizations will have some defined change control processes to accommodate changes. QA will be making sure such processes are followed.


答: Never use a boolean just because there are only two values.  Unless the meaning of true and false will be obvious, use an enumeration if available, and string constants or symbols or whatever if not.  And all too often, there are initially two values and then later there are three.  Or four.</p>

Title: Have there been disputes when software identifies genders with a boolean?

Body:
I remember that in our first programming class with Java, while explaining data types, the following occurred (kinda):

Professor: So, what data type would you choose if your program needs
  to store the user's gender?

Someone: How about a boolean? You know, true for male and false for
  female.

Professor: Sure, that may work, but some people may hesitate about that. There
  have been disputes about calling men "true" and women "false" in the
  past...

*laughter *

He ended up recommending us to consider chars (like 'm' and 'f') although booleans should be fine.

I tried searching a bit if there has been any kind of historical entry regarding a major dispute based on this programming practice with no luck.

I'm not asking what data type to use for gender nor if it is fine or not to use booleans. I'm asking if, historically, there has been a dispute regarding the usage of booleans to determine the gender in programming because of the apparently "wrong" (I'm not saying it is wrong - I don't care about that) behavior of calling a woman "false" as the professor seemed to suggest.

Searching around related questions, the results happen to be only about software efficiency.


答:
Besides the reason given by Kevinn, using boolean in this case forces using variable and property names like isMale (or isFemale), instead of gender.

To me, a neutral name like "gender" is much better than "isblabla" as a property name.


问:
More comfortable working on the backend, often referred for role on the front end
I have applied and have been approached for different roles in web development recently. The one thing that keeps coming up is that I am more suited to front end development than back end development.  This makes sense given my background starting out as a designer.

The problem is that while I have worked on a large app using ExtJS, I found designing and developing in ExtJS really frustrating. Oddly enough, it was working on this project that landed me in web development because my coding skills were recognized by some senior developers. I highlight that in my CV but I wonder if this might be a mistake.

I also use JavaScript quite a bit outside of web development. Specifically in Photoscript and InDesign to create batch operations.  It's often assumed that I have expert knowledge in JavaScript, whereas I'm only just getting to grips with the OOP style of JavaScript.  I use a lot of procedural code or I just use libraries like jQuery and Google Maps. I have created some experimental apps in Node and Knockout which I fortunately have enjoyed although Node is back end. I used to avoid JavaScript and jQuery in certain web projects since I was focusing on SEO and would only use Javascript if I really needed it.

During the interviews, I'm asked questions about JavaScript and front-end development.  But I really wanted to talk about PHP and the server side development, so I guess my background shows. How do I address this when I am either contacted for a role or I applying directly without selling my short?

答:
Well, let's look at it from the point of view of those doing the hiring.

Sometimes they are looking for a specialist, often to fill in a skill they are lacking.  If they are talking to you because they think you are a specialist with deep knowledge of a particular front end technology, don't take the job and expect it will be easy to switch to back end development.  Also, if you talk about how you really want to do back end development you may talk yourself out of an offer, which is fine if that's not what you want to do anyway.

If they are looking for a specialist in some back end technology then you probably won't get that either, because you aren't one, yet.

However, there is value in versatility as well.  So you could probably most easily get a job doing some back end programming by selling yourself as a versatile full stack developer with more experience in the front end but more interest in the back end.  In practice, companies usually prefer you not learn on their dime, so make clear that you do have real, immediately applicable, skill in doing back end development - it's just not as extensive on your resume.  It would be easier to transition to more back end development from such a position, if you continue to think back end development is where you want to focus.

You could also try to get a job as a pure back end developer, but it will be somewhat harder without a resume to back it up, and you might come in at a lower salary.  But there isn't much harm in trying as long as you don't burn bridges by marketing yourself so hard as a back end developer that you remove yourself from consideration for other positions (which would be unlikely to happen if you are honest).

Smaller companies tend to have more generalists than larger ones, out of necessity.  Agile teams also tend to prefer less specialization, for certain values of Agile.

OwnerDisplayName: anon


答:
I don't see that anyone has mentioned this one yet.

Building your own solution when you could buy it.

Variations of this pattern:

not even considering the buy-vs.-build tradeoff

significant scope creep of the in-house solution

limited scope, but also limited utility of the in-house solution

ContentLicense: CC BY-SA 2.5

Title: What is the most egregious waste of money you have seen, and what did you do about it?

Body:
Often we as programmers see large organisations wasting huge sums of money on bloated and inefficient solutions to problems. This pains me greatly because I like organisations to benefit from best of breed solutions. However, my abilities as a programmer are limited when it comes to influencing the key decision makers and often my perspective on the matter is constrained to my own little technical world.

So, my question is this. After encountering an egregious waste of money on some software and/or hardware that really got your goat, what did you do about it to get it fixed or were you doomed to bite the bullet and mutter forever under your breath? I'm interested in hearing your overall experiences and especially what lessons you learned about how to tackle this sort of thing in the future. Let's not name names, the experience of how to tackle the problem is more important than the actual offending product.


问:
A question regarding linked list vs vector insert/remove results comparison
I was reading this blog post: http://kjellkod.wordpress.com/2012/02/25/why-you-should-never-ever-ever-use-linked-list-in-your-code-again/

and I found there a code to run: http://ideone.com/62Emz

I've compiled it using gcc 4.7.2 with g++ -std=c++11 on my old laptop with T5450 cpu with two cores with 32 Kbytes L1 cache each and 2 Megabytes of (common?) L2 cache and I have got this results:

```<code>********** Times in microseconds**********
Elements ADD (List, Vector)     ERASE(List, Vector)
100,     , 0, 0,                0, 0
200,     , 0, 0,                15625, 0
500,     , 0, 0,                0, 0
1000,    , 15625, 0,            0, 15625
4000,    , 109375, 140625,              46875, 31250
10000,   , 750000, 875000,              312500, 187500
20000,   , 2968750, 3468750,            1296875, 781250
40000,   , 12000000, 13843750,          5359375, 3156250
Exiting test,. the whole measuring took 45375 milliseconds (45seconds or 0 minut
es)
</code>```

Which actually says an opposite, at least for ADD operation comparing to what author of that blog post says. List is faster for ADD than Vector. What conclusions should I make from my results? Does it proof anything? What should I think or understand?

答:
First a suggestion: You probably want to wait more than a couple of minutes before accepting an answer: You'll probably get more answers that way.

Second: when benchmarking, you should always compile with optimizations turned on. Something like g++ -std=c++11 -O3 -march=native should get you good results.

std::list really is a poor data structure, and I have not yet found a situation in which it is the best. For instance, consider the case where you want to maintain a sorted data structure. You may think that std::list, with O(1) insertion and deletion time would be ideal, but in fact, it is sub-optimal!

For these tests, my contained type is a trivial class that contains an array of 4-byte integers. I test a std::array of size 1, 10, and 100 (giving me an element size of 4, 40, and 400). I chose a std::array because a move and a copy are the same thing. The initial element of the array is initialized to some random number between 0 and std::numeric_limits<uint32_t>::max(). I create a std::vector of some number of these (the x-axis), then I start the timer. I test iterating over that std::vector for each element and inserting it in order (as sorted by that first element in the array, using operator<=). To help avoid any clever compiler optimizations of removing any work, I then output the first element of the sorted container (which cannot be determined until the end) to some file and stop the timer.

These are my results for various sizes of elements:

We see that for 4 and 40 byte elements, std::vector is better even at this inserting into the middle than std::list, and for any element size you're better off using a std::vector<unique_ptr> than std::list.

In general, I cannot come up with a reason to use std::list over a class that wraps std::vector<std::unique_ptr> to make it appear as though it has value semantics, other than the ability to copy (which I hope to fix by either submitting a value_ptr class to Boost if one isn't added soon, although there is discussion about that).

As an additional note, if this were real code, not a comparison / benchmark game, I would have written the std::vector version much more differently. I would have copied the entire original container directly, then used std::sort. I intend to write a more complete analysis on data structures, with a focus on the importance of data locality, and I will include timing on the "correct" way to do it. (the correct version blows all other methods out of the water, completing in much less than a second for 400,000 elements of size 400, which is 10 times more elements than I tested in my graphs).

I hope I explained everything well; these are some tests I ran several months ago and haven't yet finished my notes on the subject.

Tests were done on an Intel i5 machine with 4 GiB of RAM. I believe I was using Fedora 17 x64.


答:
You're in a bit of an obnoxious situation there. If you had a static format you were encoding into, your first method would be the way to go. If it were just your own format, and nobody else had to decode than the second method would be the way to go. But you don't really fit into either of those categories.

What I'd do is try to break things down by the level of abstraction.

So I'd start with something at the bit level, that I'd test something like

```<code>bitWriter = new BitWriter();
bitWriter.writeInt(42, bits = 7);
assertEqual( bitWriter.data(), {0x42} )
</code>```

So the idea is that the bitwriter knows how to write out the most primitive types of fields, like ints.

More complex types would be implemented using and tested something like:

```<code>bitWriter = new BitWriter();
writeDate(bitWriter, new Datetime(2001, 10, 4));

bitWriter2 = new BitWriter();
bitWriter2.writeInt(2001, 12)
bitWriter2.writeInt(10, 4)
bitWriter2.writeInt(4, 6)

assertEquals(bitWriter.data(), bitWriter2.data() )
</code>```

Notice that this avoids any knowledge of how the actual bits get packed. That's tested by the previous test, and for this test we'll pretty much just assume that it works.

Then at the next level of abstraction we'd have

```<code>bitWriter = new BitWriter();
encodeObject(bitWriter, myObject);

bitWriter2 = new BitWriter();
bitWriter2.writeInt(42, 32)
writeDate(bitWriter2, new Datetime(2001, 10, 4));
writeVarString(bitWriter2, "alphanumeric");

assertEquals(bitWriter.data(), bitWriter2.data() )
</code>```

so, again, we don't try to include the knowledge of how varstrings or dates or numbers are actually encoded. In this test, we are only interested in the encoding produces by encodeObject.

The end result is that if the format for dates is changed, you'll have to fix the tests that actually involve dates, but all other code and tests aren't concerned with how dates are actually encoded and once you update the code to make that work, all those tests will pass just fine.

Title: How do you unit test an encoder?

Body:
You're in a bit of an obnoxious situation there. If you had a static format you were encoding into, your first method would be the way to go. If it were just your own format, and nobody else had to decode than the second method would be the way to go. But you don't really fit into either of those categories.

What I'd do is try to break things down by the level of abstraction.

So I'd start with something at the bit level, that I'd test something like

bitWriter = new BitWriter();
bitWriter.writeInt(42, bits = 7);
assertEqual( bitWriter.data(), {0x42} )

So the idea is that the bitwriter knows how to write out the most primitive types of fields, like ints.

More complex types would be implemented using and tested something like:

bitWriter = new BitWriter();
writeDate(bitWriter, new Datetime(2001, 10, 4));

bitWriter2 = new BitWriter();
bitWriter2.writeInt(2001, 12)
bitWriter2.writeInt(10, 4)
bitWriter2.writeInt(4, 6)

assertEquals(bitWriter.data(), bitWriter2.data() )

Notice that this avoids any knowledge of how the actual bits get packed. That's tested by the previous test, and for this test we'll pretty much just assume that it works.

Then at the next level of abstraction we'd have

bitWriter = new BitWriter();
encodeObject(bitWriter, myObject);

bitWriter2 = new BitWriter();
bitWriter2.writeInt(42, 32)
writeDate(bitWriter2, new Datetime(2001, 10, 4));
writeVarString(bitWriter2, "alphanumeric");

assertEquals(bitWriter.data(), bitWriter2.data() )

so, again, we don't try to include the knowledge of how varstrings or dates or numbers are actually encoded. In this test, we are only interested in the encoding produces by encodeObject.

The end result is that if the format for dates is changed, you'll have to fix the tests that actually involve dates, but all other code and tests aren't concerned with how dates are actually encoded and once you update the code to make that work, all those tests will pass just fine.


问:
Style for creating IEnumerable unions
There isn't any cool LINQ sugar for creating unions. The Enumerable.Union() method is usually called like this:

```<code>var bigList = list1.Union(list2);
</code>```

The alternative is to call Enumerable.Union() which can be more readable:

```<code>var bigList = Enumerable.Union(list1, list2);
</code>```

However neither of these methods are very stylish (more importantly, readable) when scaling out The following is probably the best method:

```<code>var reallyBigList = list1.Union(list2).Union(list3);
</code>```

Which can result in some messy method chaining. Alternatives need incidental variables:

```<code>var list1and2 = list1.Union(list2);
var reallyBigList = list1and2.Union(list3);
</code>```

or

```<code>var list1and2 = Enumerable.Union(list1, list2);
var reallyBigList = Enumerable.Union(list1and2, list2);
</code>```

Is there a clean way of setting up these more complex unions? Would an extension like Enumerable.Union(params IEnumerable<T> collections) (used like var reallyBigList = Enumerable.Union(list1, list2, list3)) be better?

答:
The method chaining style is pretty readable, in my opinion. Certainly you could write your own extension method to take many IEnumerable<T>.

Here's an example using iteration. Recursion works too, but I didn't like it as much.

```<code>public static IEnumerable<T> MyUnion<T>(
    this IEnumerable<T> original, params IEnumerable<T>[] toUnion)
{
    var enumerable = original;
    foreach (var other in toUnion)
    {
        enumerable = enumerable.Union(other);
    }

    return enumerable;
}
</code>```

Title: Style for creating IEnumerable unions

Body:
The method chaining style is pretty readable, in my opinion. Certainly you could write your own extension method to take many IEnumerable<T>.

Here's an example using iteration. Recursion works too, but I didn't like it as much.

public static IEnumerable<T> MyUnion<T>(
    this IEnumerable<T> original, params IEnumerable<T>[] toUnion)
{
    var enumerable = original;
    foreach (var other in toUnion)
    {
        enumerable = enumerable.Union(other);
    }

    return enumerable;
}


问:
What "version naming convention" do you use?
Are different version naming conventions suited to different projects? What do you use and why?

Personally, I prefer a build number in hexadecimal (e.g 11BCF), this should be incremented very regularly. And then for customers a simple 3 digit version number, i.e. 1.1.3.

```<code>1.2.3 (11BCF) <- Build number, should correspond with a revision in source control
^ ^ ^
| | |
| | +--- Minor bugs, spelling mistakes, etc.
| +----- Minor features, major bug fixes, etc.
+------- Major version, UX changes, file format changes, etc.
</code>```

答:
```
              1.0.0
                |
              1.0.1
                |
(public 1.0)  1.0.2-----
                |       \
              2.0.0    1.1.0
                |        |
              2.0.1    1.1.1 (public 1.1)
                |
(public 2.0)  2.0.2-----
                |       \
              3.0.0    2.1.0
                         |
                       2.1.1 (public 2.1)
                         |
                       2.2.0
                         |
                       2.2.1
```

X.Y.Z is our internal version number. X.Y is the public version number, the one that has a meaning to our clients. When a X.Y.Z version becomes public, there will never be a X.Y.(Z+1) version : the public version is always the last of the serie.

X is incremented when a major version is released.

Y is used for the maintenance branches of those major releases, only for bug fixes.

Z is used internally, and has no fixed meaning. Until now, I create a new Z version when I think that the application has a set of features that are interesting to show to non developers, and is relatively stable. This way, I can show a demo of the "last known good version" of the application when someone ask one. In a near future, I plan to use the Z number versions for naming a "target" of features, in our bugtracker.

As a side note, we use maven (with the release command) to increment the version number. So, there are X.Y.Z-SNAPSHOT versions, too (which indicates any version between X.Y.(Z-1) and X.Y.Z).

Body:
              1.0.0
                |
              1.0.1
                |
(public 1.0)  1.0.2-----
                |       \
              2.0.0    1.1.0
                |        |
              2.0.1    1.1.1 (public 1.1)
                |
(public 2.0)  2.0.2-----
                |       \
              3.0.0    2.1.0
                         |
                       2.1.1 (public 2.1)
                         |
                       2.2.0
                         |
                       2.2.1

X.Y.Z is our internal version number. X.Y is the public version number, the one that has a meaning to our clients. When a X.Y.Z version becomes public, there will never be a X.Y.(Z+1) version : the public version is always the last of the serie.

X is incremented when a major version is released.

Y is used for the maintenance branches of those major releases, only for bug fixes.

Z is used internally, and has no fixed meaning. Until now, I create a new Z version when I think that the application has a set of features that are interesting to show to non developers, and is relatively stable. This way, I can show a demo of the "last known good version" of the application when someone ask one. In a near future, I plan to use the Z number versions for naming a "target" of features, in our bugtracker.

As a side note, we use maven (with the release command) to increment the version number. So, there are X.Y.Z-SNAPSHOT versions, too (which indicates any version between X.Y.(Z-1) and X.Y.Z).


答:
You should be able to word this as a single requirement as described by the other posters.

The problem with defining this as two or more requirements is this pushes your developers towards designing the "NY/NJ" interface and the "WA" interface. When a generic interface with options/rules driven by location would probably be a superior implementation.

Title: Question on System Requirement

Body:
I have a scenario where a specific webservice must be consumed based on the state to setup user info. For instance if the state is NY or NJ call service A , if state is WA call service B etc. But specifying what service must be called is I guess is "HOW" to setup a user info and not a "WHAT".

So should I just have single requirement stating system must have the capbality to setup up user info based on state?

EDIT: I would also like to add that the services implemented for different states might want different info to be provided in addition to basic details such as first name and last name. Should the services , input to the services , output from the service , what the program needs to do with the output etc be part of system requirements? I guess all these deal with "HOW" more than "WHAT" - hence is it in the requirements realm?


答:
I don't consider the method chaining option messy at all. Sure, something like

```<code> var reallyBigList = (from ... where ... select ...).Union(from ... where ... select)...
</code>```

can easily get unreadable, but, on the other hand,

```<code> var mp3s = from ... where ... select ...
 var videos = from ... where ... select ...
 var alreadyProcessed = from ... where ... select ...

 var toDo = mp3s.Union(videos).Except(alreadyProcessed)
</code>```

reads quite naturally. So, when using well-named intermediate variables, the method chaining approach is extremely readable.

Title: Style for creating IEnumerable unions

Body:
There isn't any cool LINQ sugar for creating unions. The Enumerable.Union() method is usually called like this:

```<code>var bigList = list1.Union(list2);
</code>```

The alternative is to call Enumerable.Union() which can be more readable:

```<code>var bigList = Enumerable.Union(list1, list2);
</code>```

However neither of these methods are very stylish (more importantly, readable) when scaling out The following is probably the best method:

```<code>var reallyBigList = list1.Union(list2).Union(list3);
</code>```

Which can result in some messy method chaining. Alternatives need incidental variables:

```<code>var list1and2 = list1.Union(list2);
var reallyBigList = list1and2.Union(list3);
</code>```

or

```<code>var list1and2 = Enumerable.Union(list1, list2);
var reallyBigList = Enumerable.Union(list1and2, list2);
</code>```

Is there a clean way of setting up these more complex unions? Would an extension like Enumerable.Union(params IEnumerable<T> collections) (used like var reallyBigList = Enumerable.Union(list1, list2, list3)) be better?

Body:
I don't consider the method chaining option messy at all. Sure, something like

 var reallyBigList = (from ... where ... select ...).Union(from ... where ... select)...

can easily get unreadable, but, on the other hand,

 var mp3s = from ... where ... select ...
 var videos = from ... where ... select ...
 var alreadyProcessed = from ... where ... select ...

 var toDo = mp3s.Union(videos).Except(alreadyProcessed)

reads quite naturally. So, when using well-named intermediate variables, the method chaining approach is extremely readable.


问:
Features that encourage use of project management software
I'm a student and I work as an undergrad in an IT department. They are very effective programmers, but as far as documenting what they do and tracking what time is spent on specific tasks - not so much.

I was put in charge of setting up project management software. So far, I've created a satisfactory solution with Trac. But still, programmers use it because "the boss says so" and do so grudgingly.

No one likes having to go to a separate webpage, logging what they did, checking boxes, and what not - so my question is this:

As a programmer, what incentives or features in project management software would encourage you (or least make it more bearable) to use it?

答:
We're not using Trac, but Bugzilla in Combination with ScrumWorks. We've introduced it, after a few incidents where features/bugs have been forgotten to fix or under-priorized, and led to a hurry in the end - and therefore resulting in bugs, stress, etc.
Now we adopted the new working style of

turning on the computer

check new bugs/requested features

fix/implement it

check in with bugzilla, mark it as solved

repeat

I, and this is an absolute personal opinion, find this very comforting, because at the end of the day, you see what you've achieved and what you have to do tomorrow. It took a while to get used to the extra steps, but it was worth it.

Regarding to you question, what features would it make more encouraging, i would say integration with the IDE. No additional programs/webpages, just a few more clicks every day.


问:
Database connection - should they be passed as parameter?
We have a system whereby the database connection is get once using a common method, and being pass throughout the relevant class to be used. There are doubts that passing the database connection as a parameter to different classes would cause problem, so i'm checking here to see whether this is actually viable, and are there any better patterns to do it?

I know there are some ORM tools to do the persistence but we can't go into that, yet..

Any feedback is welcomed, thanks.

答:
It sounds like you're after Dependency Injection. That is, the pooled connection gets created once and injected whereever it's needed. Certainly passing in the connection via a method parameter is one way to dependency inject, but an IoC container such as Guice, PicoContainer or Spring is another (safer) way you can do this.

Using DI means you can neatly wrap up the logic around the creation, opening, usage and closing of the connection - away from your core business logic.

Spring JDBC et al are otehr examples of performing this sort of behaviour for you


问:
Should I return from a function early or use an if statement?
I've often written this sort of function in both formats, and I was wondering if one format is preferred over another, and why.

public void SomeFunction(bool someCondition)
{
    if (someCondition)
    {
        // Do Something
    }
}
```

<p>or</p>

<pre class="lang-java prettyprint-override"><code>public void SomeFunction(bool someCondition)
{
    if (!someCondition)
        return;

    // Do Something
}
</code>```

I usually code with the first one since that is the way my brain works while coding, although I think I prefer the 2nd one since it takes care of any error handling right away and I find it easier to read

答:
I use both.

If DoSomething is 3-5 lines of code then the code just look beautiful using the first formatting method.

But if it has many more lines than that, then I prefer the second format.  I don't like when the opening and closing brackets are not on the same screen.


答:
It's unclear in your question what exactly you're using the software for, and I think that's the central point, not features.

You need to formulate clear and concrete goals: what do you want to achieve with this software? And how specifically does this software help achieving those goals?

If the goals are shared by the programmers, making them understand these points should help convincing them to use the software (or prompt them to point out specific problems with the software that can be fixed).

If the goals are not shared by the programmers (such as micro-managing their "productivity" as measured by some artificial metric), then the only way to make them comply is to outright order them, and punish those who don't comply. I hope I don't need to point out that this is a very bad idea.

Title: Features that encourage use of project management software

Body:
I'm a student and I work as an undergrad in an IT department. They are very effective programmers, but as far as documenting what they do and tracking what time is spent on specific tasks - not so much.

I was put in charge of setting up project management software. So far, I've created a satisfactory solution with Trac. But still, programmers use it because "the boss says so" and do so grudgingly.

No one likes having to go to a separate webpage, logging what they did, checking boxes, and what not - so my question is this:

As a programmer, what incentives or features in project management software would encourage you (or least make it more bearable) to use it?


问:
Is this a test smell or is it even worse?
I have recently been looking at some testscripts which looks a bit like

```<code>...
try {
  receiveSomething();
  // something was received even though it shouldn't
  failTest();
} catch (TimeoutException e) {
  // nothing should be received
  succedTest();
}
</code>```

The problem I have with these types of tests is

They are not deterministic. You don't know if nothing was sent on purpose or if everything has crashed.

Its very hard to simultaneously test something else, which might actually, in this case, send something.

My thoughts are one the one side, how can this types of tests be designed better, and secondly can this be an indication of a bigger design smell of the software that is being tested?

EDIT

To clairify, these test scripts are used for black box-testing of event-based, complex software, not unit testing, and my feeling is that the 'doing nothing' event is a very ambiguous one. :)

答:
Very hard to answer this without some context.

Were the test case to be for the function point ("System Will not respond to any request after 'Quiesce' command issued") then yes this would be a perfectly valid test case.

Although I would have specified that a valid response was received immediately before the shutdown request - but these type of "operational" tests are very difficult to automate as its unlikely you would be able to issue such system commands from an API.


答:
"The boss says so" is actually a good reason, which is why it works. As you've noticed though, it isn't great for morale. If you want to encourage programmers to use it, look for ways it can make their lives easier. The main ways it can do that are:

It can stop you forgetting things. Anybody who has had a last-minute panic before (or after!) release would rather not repeat the experience. As long as all the tasks/bugs go in there, it should be impossible to forget about things. Having everyone use the same system means things don't get forgotten even when people leave the company/go on holiday/are off sick/etc.

It makes for better scheduling. Nobody likes crunch periods. If you've got a deadline looming and you aren't going to get things done in time, then the sooner you (and your boss) know about this the better. You can cut features, assign more resources, or just spread out the extra hours over a longer period (8 45-hour weeks are probably better than 1 80-hour week).

The little things. A lot of PM tools can do various little things to make your developers' lives easier: If it integrates with your wiki, that can make it easier to keep it up to date. If it integrates with your source control system, it makes it easier to quickly find the commit where you fixed that niggly UI bug which has suddenly appeared in a different part of the application.


答:
They are not deterministic. You don't know if nothing was sent on purpose or if everything has crashed.

It doesn't matter, in the context of this test. All that matters is that, under the preconditions defined, nothing is received. Other tests can simulate other preconditions and check their results are as-expected.

Its very hard to simultaneously test something else, which might actually, in this case, send something.

Good! You shouldn't try to plug other tests into this test. This test is doing exactly what it should be doing. One set of preconditions => One result.

This is the very definition of determinism.

Title: Is this a test smell or is it even worse?

Body:
I have recently been looking at some testscripts which looks a bit like

```<code>...
try {
  receiveSomething();
  // something was received even though it shouldn't
  failTest();
} catch (TimeoutException e) {
  // nothing should be received
  succedTest();
}
</code>```

The problem I have with these types of tests is

They are not deterministic. You don't know if nothing was sent on purpose or if everything has crashed.

Its very hard to simultaneously test something else, which might actually, in this case, send something.

My thoughts are one the one side, how can this types of tests be designed better, and secondly can this be an indication of a bigger design smell of the software that is being tested?

EDIT

To clairify, these test scripts are used for black box-testing of event-based, complex software, not unit testing, and my feeling is that the 'doing nothing' event is a very ambiguous one. :)


答:
They are not deterministic. You don't know if nothing was sent on purpose or if everything has crashed.

A deterministic algorithm as defined in Wikipedia is as such:

is an algorithm which, given a particular input, will always produce the same output

But the algorithms to build the test hardly matter, pretty much because it is supposed to test the proper state after invoking a specific behavior.  In other words, the only thing that needs to be deterministic is the code module that you are testing.  The test itself is actually definining what the determinination should be.

Its very hard to simultaneously test something else, which might actually, in this case, send something.

As others pointed out, that is a GOOD THING.  If I have a test for module A, that has dependencies on Modules B and C, then if TestModuleA fails, how do I know it is ACTUALLY an issue in module A?

Your test is supposed to just verify that Module A provides the correct state for invoking an operation of A.  You should have seperate tests for the dependencies to verify their correct behavior too.


问:
One Service or Two Services for Parsing and Sending Emails
I have a bulk emailing service within the system which allows for a template to be used. When the mail is sent the template is parsed or run through a parser to replace tags with actual information. i.e "[Foo] has logged in" is parsed to "Bar has logged in"

Is it better to have 1 service for sending and parsing emails (concurrently), or to have 2 services, one which only checks for pending emails and parses them, and another which only sends emails who have already been parsed?

答:
Unless I am missing some information I see no reason why you would need 2 services to do what you could do with 1. In fact, it may be easier to do it with one in case there are any resources they both share, could mean less code and/or memory used.

You could add more info to the question such as: What do you currently think would be the advantage to having 2 services instead of 1?

Then we can look at that and explain why 1 can handle that better, or we will see your reasoning and agree, yes you can't do that with only 1 service, etc.


答:
From what you wrote, I guess your situation is more like this

```<code>startAsynchronousComplexOperation();
try {
  receiveSomething(); // listen for events from that async operation above

  // something was received even though it shouldn't
  failTest();
} catch (TimeoutException e) {
  // nothing should be received
  succedTest();
}
</code>```

and the object under test is not "receiveSomething", but the asynchronous operation. Thus the outcome of that test depends on the speed of the test machine, if a lot of other processes run on the test machine etc., and this is non-deterministic.

IMHO, this is indeed a design problem, and not just a "test smell". If your "complex operation" crashes, or just "takes too long", not only your test will give a "false negative". Think about how to deal with that situation in production. Is there no requirement to determine for sure that your "complex operation" is still alive and did not crash? So you may think about adding some kind of "watchdog" or "keep-alive-packet-sending" mechanic to your program to get control over that. And once you have such a mechanic available, you can use it in your tests like this:

```<code>startAsynchronousComplexOperation();
while(isComplexOperationStillAlive())
{
    try {
       receiveSomething(); // listen for events from that async operation above
       // something was received even though it shouldn't
       failTest();
   } catch (TimeoutException e) {
   }
}
if(didComplexOperationCrash())
    failTest();
else
    succedTest();
</code>```

Body:
From what you wrote, I guess your situation is more like this

startAsynchronousComplexOperation();
try {
  receiveSomething(); // listen for events from that async operation above

  // something was received even though it shouldn't
  failTest();
} catch (TimeoutException e) {
  // nothing should be received
  succedTest();
}

and the object under test is not "receiveSomething", but the asynchronous operation. Thus the outcome of that test depends on the speed of the test machine, if a lot of other processes run on the test machine etc., and this is non-deterministic.

IMHO, this is indeed a design problem, and not just a "test smell". If your "complex operation" crashes, or just "takes too long", not only your test will give a "false negative". Think about how to deal with that situation in production. Is there no requirement to determine for sure that your "complex operation" is still alive and did not crash? So you may think about adding some kind of "watchdog" or "keep-alive-packet-sending" mechanic to your program to get control over that. And once you have such a mechanic available, you can use it in your tests like this:

startAsynchronousComplexOperation();
while(isComplexOperationStillAlive())
{
    try {
       receiveSomething(); // listen for events from that async operation above
       // something was received even though it shouldn't
       failTest();
   } catch (TimeoutException e) {
   }
}
if(didComplexOperationCrash())
    failTest();
else
    succedTest();


问:
Spurious wakeups explanation sounds like a bug that just isn't worth fixing, is that right?
According to the Wikipedia article on Spurious Wakeups

"a thread might be awoken from its waiting state even though no thread signaled the condition variable".

While I've know about this 'feature' I never knew what actually caused it until, in the same article

"Spurious wakeups may sound strange, but on some multiprocessor systems, making condition wakeup completely predictable might substantially slow all condition variable operations."

Sounds like a bug that just isn't worth fixing, is that right?

答:
TL;DR Assumption ("contract") of spurious wakeups is a sensible architectural decision made to allow for realistically robust implementations of thread sheduler.

"Performance considerations" are irrelevant here, these are just misunderstanding that became widespread because of having stated in a published authoritative reference. (authoritative references might have errors, y'know - just ask Galileo Galilei) Wikipedia article keeps the reference to the note you quoted just because it perfectly matches their formal guidelines of citing the published reference.

Much more compelling reason for introducing concept of spurious wakeups is provided in this answer at SO that is based on additional details provided in an (older version) of that very article:

The Wikipedia article on spurious wakeups has this tidbit:

The pthread_cond_wait() function in Linux is implemented using the futex system call. Each blocking system call on Linux returns abruptly with EINTR when the process receives a signal. ... pthread_cond_wait() can't restart the waiting because it may miss a real wakeup in the little time it was outside the futex system call...

Just think of it... like any code, thread scheduler may experience temporary blackout due to something abnormal happening in underlying hardware / software. Of course, care should be taken for this to happen as rare as possible, but since there's no such thing as 100% robust software it is reasonable to assume this can happen and take care on the graceful recovery in case if scheduler detects this (eg by observing missing heartbeats).

Now, how could scheduler recover, taking into account that during blackout it could miss some signals intended to notify waiting threads? If scheduler does nothing, mentioned "unlucky" threads will just hang, waiting forever - to avoid this, scheduler would simply send a signal to all the waiting threads.

This makes it necessary to establish a "contract" that waiting thread can be notified without a reason. To be precise, there would be a reason - scheduler blackout - but since thread is designed (for a good reason) to be oblivious to scheduler internal implementation details, this reason is likely better to present as "spurious".

From thread perspective, this somewhat resembles a Postel's law (aka robustness principle),

be conservative in what you do, be liberal in what you accept from others

Assumption of spurious wakeups forces thread to be conservative in what it does: set condition when notifying other threads, and liberal in what it accepts: check the condition upon any return from wait and repeat wait if it's not there yet.


答:
Having programmers to 1st line phone support.

Having programmers do testing.


答:
There are two jobs to be done

Preparing personalized mails based on a template and data-set

Sending a mail to a recipient.

This asks for two services.

Because it is very common to do both jobs one immediately after the other, I would further arrange things such that the end-user only has to use one service:

either a third (convenience) service that encapsulates the two above

or the first service automatically invokes the second service

Title: One Service or Two Services for Parsing and Sending Emails

Body:
I have a bulk emailing service within the system which allows for a template to be used. When the mail is sent the template is parsed or run through a parser to replace tags with actual information. i.e "[Foo] has logged in" is parsed to "Bar has logged in"

Is it better to have 1 service for sending and parsing emails (concurrently), or to have 2 services, one which only checks for pending emails and parses them, and another which only sends emails who have already been parsed?


问:
Unit test and Code Coverage of Ant build scripts
In our development environment We have more and more build scripts for ant to perform the build tasks for several different build jobs.

These build scripts sometimes become large and do a lot of things and basically is source code in and of itself.

So in a "TDD-world" we should have unit tests and coverage reports for the source code.

I found AntUnit and BuildFileTest.java for doing unit tests. But it would also be interesting to know the code coverage of those unit tests.

I have been searching google, but have not found anything. Does anyone know of a code coverage tool for Ant build scripts?

答:
To answer my own question :-)

I have ended up using AntUnit to do unit testing of Ant Build scripts.
To to coverage I did not find a tool/library that works out of the box.
What I did find that might be usable with some adjustments is Antro (https://sourceforge.net/projects/antro/) with its code at https://github.com/jkff/antro.

Antro is "Ant profiler: a line-level profiler for Ant build scripts"

It uses a listener to collect data about the build script run.

The challenge is to try to use Antunit and Antro together or make a listner for Antunit that can do the same as Antro to collect data.

But then again that might my an interesting challenge to do :-)


问:
Why use skins if we can handle server controls with CSS in ASP.Net?
The main difference between themes/skins and CSS are the HTML & Server Side Controls (SSC) only. We use skin files to format & cosmetics SSC which are not in HTML like <asp:Label>, <asp:TextBox> & </asp:RadioButtonList> etc...

But what if we use the rendered HTML SSC in CSS instead skin files to handle <asp:Label> & <asp:TextBox> etc...

ie: when SSC <asp:TextBox> rendered on browsers it shows <input type="text"> so if we write following style in my CSS file instead Skin, it also works...

```<code>input[type=text], textarea, select {
    outline: none;
    padding: 3px 0px 3px 3px;
    margin: 5px 1px 3px 0px;
    border: 1px solid #cccccc;
    background: #ffffff;
    width: 150px;
    color: #484848;
    font-size:
    12px;
}
</code>```

So my question is, if we still can handle SSC with CSS do we really need skins? Or if I am missing anything please correct me, thanks.

答:
It's a question of choice; I have never needed to use skins, I have always been able to do all the styling I wanted to with pure css.

Looking at your example it is purely the look and feel of controls you want to control - so I see no reason for you to use skins. Html for mark-up, JavaScript for client-side functionality and css for how things look. Keeping your mark-up free from script or style has its advantages and css is the best tool for styling.

I wouldn't mix and match css and skins for the simple reason that I would then find it more difficult to know whether I needed to look in a skin file or a css file to make a style change.

If you had need of setting properties on your SSC then a skin file would be something to consider, but as we've both said you're only looking for styling then I wouldn't complicate things by introducing skin files. StackOverflow has an answer to skin vs css that might be interesting to read.


问:
Should programmers talk with customers / users according to MSF / agile methods?
I've just read two statements that seem to be very different:

Des Weiteren ist mangelnde Kommunikation zwischen Programmierern und Nutzern eine nicht zu vernachlässigende Quelle von unzureichenden Produkten.

Translated:

A lack of communication between programmers and users is a source of poor [software] products.

Source: de.wikipedia.org

I think I have read something similar in CHAOS report of Standish Group.

And

Insbesondere bei der Rolle Development ist Kontakt zum Kunden oder zu den Benutzern nach Meinung des MSF geradezu zu unterbinden.

Translated:

According to MSF, especially the role "Development" should not have contact to the customer or to the user.

Source: msdn.microsoft.com

This also makes sense, because as a programmer I want to have happy end users. So the user likes to have a new feature, I'll try to implement it. This could lead to feature creep.

If I understand it correctly, MSF (Microsoft Solution Framework) tries to avoid this problem by a role that has contact to the customer (this is the product manager, the user experience role and maybe the testing role, isn't it?) and only one role that has contact to the development role (the program manager).

Question 1: How do agile methods deal with the problem of feature creep? I read that the developers should have very strong contact with customers in agile methods and that one of the main problems in using scrum is to persuade the customer to get involved in the process.

Does in SCRUM only the Product Owner have contact with the user / customer? Isn't this a problem, as the programmer might see different problems than the Product Owner?

Question 2: Who does the requirements engineering in agile methods and MSF?

Question 3: Do you validate in MSF / agile methods if your product does what the customer wants and the user needs before shipping it? How do you do it?

答:
The customer is part of the team that produces the product, so they can indeed pile wish upon wish. But they are also part of the planning process for which story is included in which sprint, so they get immediate feedback on which wish will take how long to implement, and what other wish will be postponed because of it. This creates a natural counterweight. (Whereas in old-fashioned, strictly segregated requirements engineering and development, such feedback virtually never happens, and the customer has no idea which features would take how much time; sometimes the customer reps even actively lie about it.)

Differs from method to method, but in general, the focus is on getting requirements from people closer to the actual users of the finished product, rather than from their managers, their procurement department, or even their sales reps.

Pretty much part of the same arrangement as in (1): the customer should be present when the feature is planned, implemented, tried out and tested. If during all those steps, they don't notice that they don't actually want it, well... some people truly can't be pleased, and even agile methods are helpless against that.


答:
How do agile methods deal with the problem of feature creep

The main way is that Agile methods are typically based on the idea of a backlog and release planning. Anything that a user wants can go in the backlog. At regular intervals, the users are expected to prioritize the backlog, to set the features that the team works on.

Users are not, however, allowed to request new features for the current iteration. The definition of "feature" is somewhat blurry, though: at the start of the iteration, the programmer knows just enough about the requirements to give a rough estimate of time. During the iteration, the developer will work with the user to refine the requirements. As this happens, the agreed-upon feature may turn out to be more complex than originally expected. In a perfect world, user and developer settle on "good enough," and defer enhancements to the backlog.

In the real world, sprints occasionally fail (don't produce anything) because user and developer can't agree on reasonable scope. After a few failures, the team (which includes the users) have to face their disfunction, and figure out a way around it.

Who does the requirements engineering in agile methods

The team, which includes developers and users. The idea behind Agile methods is that users rarely know what they want in the level of detail needed to implement a production-quality piece of software. There are always corner cases, and those should come out as the developer analyzes the feature.

The big issue is how those requirements are captured. There's no reason that you couldn't produce a formal requirements document as part of an Agile methodology, but most teams see that as anti-Agile. Some teams use testcases as the requirements, and a well-written suite of integration tests is one of the best formal requirements documents you can get. A wiki page that captures the discussion between users and developers is also reasonable.

Unfortunately, many teams see Agile as "we don't need no stinkin' documentation," and end up in arguments six months down the road. Which occasionally spells the end of "Agile" at that particular company.

Do you validate in MSF / agile methods if your product does what the customer wants and the user needs before shipping it? How do you do it?

The user says "this meets my needs." This happens at the end of every iteration, and when enough needs are met the project releases. Then the enhancements start.

Title: Should programmers talk with customers / users according to MSF / agile methods?

Body:
I've just read two statements that seem to be very different:

Des Weiteren ist mangelnde Kommunikation zwischen Programmierern und Nutzern eine nicht zu vernachlässigende Quelle von unzureichenden Produkten.

Translated:

A lack of communication between programmers and users is a source of poor [software] products.

Source: de.wikipedia.org

I think I have read something similar in CHAOS report of Standish Group.

And

Insbesondere bei der Rolle Development ist Kontakt zum Kunden oder zu den Benutzern nach Meinung des MSF geradezu zu unterbinden.

Translated:

According to MSF, especially the role "Development" should not have contact to the customer or to the user.

Source: msdn.microsoft.com

This also makes sense, because as a programmer I want to have happy end users. So the user likes to have a new feature, I'll try to implement it. This could lead to feature creep.

If I understand it correctly, MSF (Microsoft Solution Framework) tries to avoid this problem by a role that has contact to the customer (this is the product manager, the user experience role and maybe the testing role, isn't it?) and only one role that has contact to the development role (the program manager).

Question 1: How do agile methods deal with the problem of feature creep? I read that the developers should have very strong contact with customers in agile methods and that one of the main problems in using scrum is to persuade the customer to get involved in the process.

Does in SCRUM only the Product Owner have contact with the user / customer? Isn't this a problem, as the programmer might see different problems than the Product Owner?

Question 2: Who does the requirements engineering in agile methods and MSF?

Question 3: Do you validate in MSF / agile methods if your product does what the customer wants and the user needs before shipping it? How do you do it?


问:
What are some good assignments for an introductory course, to reach today's students?
I periodically teach an introduction to programming course using Java. I want to give my students some exciting assignments that they can relate to or find interesting. At the very least, I want assignments that make sense and have an internal cohesion (for example, some very bad programming exercises seem contrived just so that you have to use the most recent programming construct covered).

To give you an idea of scope, here's what's being covered:

The assignments must be in Java, using some external library can be done, but it would need to be a simple API and not a full framework

Variables, Primitives and Strings

Console input and output

if, for, while

Arithmetic and logical operators

Simple graphics with line and shape drawing

Static methods

One-dimensional arrays

The students will not go into advanced topics (e.g., no recursion, no emphasis on inheritance). Thus, I'm not looking for complex projects: "Have them write a C compiler. I did it when I was 5 and it was the only way I learned!"

If you have ideas that are more advanced than the scope, please post them on the "Challenging" question linked below, and not this one.

Instead, what I'm looking for are interesting ideas that can be accomplished in a short program. For example:

Students can write a console version of the "Star Wars Name" Generator. This is effectively reading Strings and using substring, but I think it's fun. A similar variation would be a "Mad Libs" program. I used this one five years ago, and perhaps it's already "stale."

Using some trig supplied to them, students can draw regular polygons, and create interesting spiral shapes.

It's also not out of the question to use some simple animation class with most of the code supplied to them. And if you know a Twitter or Facebook service that can be accessed with a simple API, I would be very interested to know.

Please note that this question is different from the "Challenging Java questions for beginners" Question. I'm not looking for challenging per se, just interesting. I think if students work on something where they can easily answer "why would anyone ever want to program that?" then they will learn better.

Even simple problems like computing Miles per Gallon can be good. Although, if there's a way to update it to be more relevant, all the better. If you have an exercise from somewhere else (a book or a website), please cite the source.

To help you think about the typical freshman today, check out the Beloit Mindset list, for many examples that will surely make you feel old.

答: Sounds like a text adventure game could be a terrific assignment somewhere mid semester. I had a class that made us play Colossal Cave Adventure and then make our own game. It taught me a lot of the basic constructs early on, and it was so much fun to do everyone got into it.</p>


问:
Avoiding ubiquitous language clashes
I have been reading DDD Quickly and wondered about how to avoid naming clashes with technical terms and domain terms. For example, if I commonly used the repository pattern (with classes such as AddressRepository), but a customer also has something called a repository in their domain. How would I best avoid confusing the two?</p>

答:
I would say that in the spirit of DDD's Ubiquitous Language, the Domain definition of the clashing term should take precedence over the programming infrastructure term.

In your case, if there is already a Domain definition for "Repository", then you should choose another name for your AddressRepository that doesn't clash, even though the class is implementing the DDD repository mechanism. After all, you don't have to append "Repository" to the end of the class name, it's just a convenience to you, the programmer. I would probably just call it "Addresses".

Title: Avoiding ubiquitous language clashes


问:
Basic memcached question
I have been reading up on distributed hashing. I learnt that consistent hashing is used for distributing the keys among cache machines. I also learnt that, a key is duplicated on mutiple caches to handle failure of cache hosts. But what I have come across on memcached doesn't seem to be in alignment with all this. I read that all cache nodes are independent of each other and that if a cache goes down, requests go to DB. Theres no mention of cache miss on a host resulting in the host directing the request to another host which could either be holding the key or is nearer to the key.

Can you please tell me how these two fit together? Is memcached a very preliminary form of distributed hashing which doesnt have much sophistication?

答:
There are two parts to this story: memcached servers and libmemcached clients.

Memcached servers are indeed independent of each other. Memcached server is just an efficient key-value store implemented as in-memory hash table. What makes memcached distributed is the client, which in most implementations can connect to a pool of servers. Typical implementations use consistent hashing, which means that when you add or remove server to/from a pool of N servers, you only have to remap 1/N keys. Typically keys are not duplicated on various hosts, as memcached is not meant to be persistent store and gives no guarantees that your value will persist (for example when running out of assigned memory, memcached server drops least recently used (LRU) elements). Thus it's assumed that your application should handle missing keys.

if a cache goes down, requests go to DB

If you happen to use memcached to cache results of DB queries. Which is one of many possible uses. Memcached is just generic key-value store, can be used for other purposes.

Title: Basic memcached question


问:
At what stage of a project should documentation be done?
I was assigned to fix some issues in a PHP project. Later on, I was asked to implement some new features, which I did. The project neither uses any framework nor does it use OOP. It  lacks a proper structure, except for the use of folders  (no sub-folders) to organize files. There was no documentation created by the original programmer. Now, the project's almost complete with some simple testing to be done and the client is asking for documentation.

I have a few questions actually.

I have always considered documentation as a process which is starts before starting the coding part of project and which continues till the end. So, am I wrong about this? Is the documentation done at the end of the project?

What should be my way to go on documenting this project. I've kept a basic "log like" documentation on the changes I've made for my  personal use. But since there was no initial documentation produced, I didn't bother to create one either.

Is there a good documentation template that I could use to reduce my effort?

I am talking about or code documentation (not  user documentation). Although I'm assuming this since they haven't specified anything.

答:
You are right, documentation should begin before you start coding. However, it is a continuous process as the documentation should change with the project.

Documenting your changes alone may not be the sufficient. It's good to start with the changes you have made, but even better we if you take time to document the entire code base. I know this can be a pain, but good things hardly come easy.

Wikis are a good documentation template . Try DokuWiki https://www.dokuwiki.org/template. Developing in languages like Java makes documentation easier as there are tools that generate documentation from comments in your code. But I am not sure you have the same luxury with PHP.


答:
Yes it is safe to pass around a connection. You handle the connection in an outer controlling block. There is nothing unsafe about it.

What is unsafe is writing code that does not guarantee the connection is properly disposed in a timely manner. Forgetting to clean up a resource is unrelated to passing it around. You could just as easily write code that leaves a hanging connection without passing it anywhere.

In C++, you are protected by RAII if you allocate on the stack or use smart pointers. In C# make a hard rule that all disposable objects (such as connections) be declared in a "using" block. In Java clean up with try-finally logic. Have code reviews on all data layer code to ensure this.

The most common use-case is when you have several operations that can be combined in many permutations. And each of these permutations need to be an atomic-transaction (all succeed or rollback). then you must pass the transaction (and therefore the corresponding connection) around to all the methods.

Suppose we have many foobar() actions that can be combined in various ways as atomic-transactions.

```<code>//example in C#
//outer controlling block handles clean up via scoping with "using" blocks.
using (IDbConnection conn = getConn())
{
    conn.Open();
    using (IDbTransaction tran = conn.BeginTransaction())
    {
        try
        {//inner foobar actions just do their thing. They don't need to clean up.
            foobar1(tran);
            foobar2(tran);
            foobar3(tran);
            tran.Commit();
        }
        catch (Exception ex)
        { tran.Rollback(); }
    }
}//connection is returned to the pool automatically
</code>```

BTW you will want to open connections as late as possible, dispose them as soon as possible. Your teammates could be right if you are treating connections as object members, introducing them as unnecessary state, and leaving connections open much longer than necessary. But the act of passing a connection or transaction as a parameter is not inherently wrong.

BTW. Depending on your language's support for first class functions you may take in a list of foobar() actions. So one function could handle all permutations of the actions. Eliminating duplication of the outer controlling block for each permutation.

Title: Database connection - should they be passed as parameter?

Body:
We have a system whereby the database connection is get once using a common method, and being pass throughout the relevant class to be used. There are doubts that passing the database connection as a parameter to different classes would cause problem, so i'm checking here to see whether this is actually viable, and are there any better patterns to do it?

I know there are some ORM tools to do the persistence but we can't go into that, yet..

Any feedback is welcomed, thanks.

Body:
Yes it is safe to pass around a connection. You handle the connection in an outer controlling block. There is nothing unsafe about it.

What is unsafe is writing code that does not guarantee the connection is properly disposed in a timely manner. Forgetting to clean up a resource is unrelated to passing it around. You could just as easily write code that leaves a hanging connection without passing it anywhere.

In C++, you are protected by RAII if you allocate on the stack or use smart pointers. In C# make a hard rule that all disposable objects (such as connections) be declared in a "using" block. In Java clean up with try-finally logic. Have code reviews on all data layer code to ensure this.

The most common use-case is when you have several operations that can be combined in many permutations. And each of these permutations need to be an atomic-transaction (all succeed or rollback). then you must pass the transaction (and therefore the corresponding connection) around to all the methods.

Suppose we have many foobar() actions that can be combined in various ways as atomic-transactions.

//example in C#
//outer controlling block handles clean up via scoping with "using" blocks.
using (IDbConnection conn = getConn())
{
    conn.Open();
    using (IDbTransaction tran = conn.BeginTransaction())
    {
        try
        {//inner foobar actions just do their thing. They don't need to clean up.
            foobar1(tran);
            foobar2(tran);
            foobar3(tran);
            tran.Commit();
        }
        catch (Exception ex)
        { tran.Rollback(); }
    }
}//connection is returned to the pool automatically

BTW you will want to open connections as late as possible, dispose them as soon as possible. Your teammates could be right if you are treating connections as object members, introducing them as unnecessary state, and leaving connections open much longer than necessary. But the act of passing a connection or transaction as a parameter is not inherently wrong.

BTW. Depending on your language's support for first class functions you may take in a list of foobar() actions. So one function could handle all permutations of the actions. Eliminating duplication of the outer controlling block for each permutation.


答:
These two philosophies seem to contradict each other, but they can coexist. Just because the developers don't have direct contact to the users, doesn't mean that there is no communication between them.

The communication happens through the customer relations people who ask the customers for their requirements, translate them into the language of the development team, and give the developers clear instructions they can follow. Then they present the prototypes to the customers, aggregate their feedback and report it back to the development team. That way the developers can concentrate fully on the development.

The Microsoft philosophy is based on the assumption that programmers and customer relations are two entirely different specializations with entirely different training. The programmers are trained for talking to machines while the CR people are trained for talking to humans. Each one should do the job they are trained for.

The agile philosophy, on the other hand, assumes an eloquent software developers who can both write elegant code and have the social abilities to deal with the users. Excelling both at social and at technical skills is an ideal we should all strafe for, but let's be honest: most people are more enclined for either one or the other. Those who can do both are a rare (and extremely valuable!) elite.


答:
Welcome to the drop-dead boring side of software engineering, reverse engineering products you already have!

This is largely a job to make it look like you know what you're doing and the company isn't incompetent enough to skip a crucial step of the process, even though they are. In theory, any sort of documentation will help other programmers step into the project later, so if you find anything that was confusing to you, write out an explanation about it after you figured it out.

1) You're absolutely right. Most documentation (should) come before the product is made. Things like requirements, design, testing. Others however, are expected to be made afterwards; users manuals, traceability, QA checks.

2) If the customer is asking for it, you should take the dive and produce the documentation they want.

3) There are a lot of different templates out there. Google around for SRS, software requirements specification, SDD, software design description, or even DO-178 templates, if you want to be buried in paperwork till the end of your days.

But by and far, do enough paperwork to make the customer happy, as you've already missed the boat for it's usefulness. Unless, of course, this project has a long life ahead of it. In which case, you should really get on top of it before it grows to something unmanageable.

Title: At what stage of a project should documentation be done?

Body:
I was assigned to fix some issues in a PHP project. Later on, I was asked to implement some new features, which I did. The project neither uses any framework nor does it use OOP. It  lacks a proper structure, except for the use of folders  (no sub-folders) to organize files. There was no documentation created by the original programmer. Now, the project's almost complete with some simple testing to be done and the client is asking for documentation.

I have a few questions actually.

I have always considered documentation as a process which is starts before starting the coding part of project and which continues till the end. So, am I wrong about this? Is the documentation done at the end of the project?

What should be my way to go on documenting this project. I've kept a basic "log like" documentation on the changes I've made for my  personal use. But since there was no initial documentation produced, I didn't bother to create one either.

Is there a good documentation template that I could use to reduce my effort?

I am talking about or code documentation (not  user documentation). Although I'm assuming this since they haven't specified anything.


答:
Passing around database things rather than data things can lead to problems.  To that extent, whenever it practical, don't pass a database thing unless one can guarantee proper database hygiene.

The problem with passing around database things is that it can be sloppy.  I have seen more than one bug in code with someone passing around a database connection, that someone then grabs a result set to and stashes in a local object (the result set, still connected to the database) and then ties up a cursor in the database for a significant time.  Another instance someone passed a result set to someone else (which was then stashed) and then method that passed the result set closed it (and the statement) leading to errors when other methods tried to work with the result set that wasn't anymore.

All of this stems from not respecting the database, the connection, the statement, the result set, and their lifecycles.

To avoid this, there are existing patterns and structures that play more nicely with databases and don't have database things need to get out of the classes they are confined in.  Data goes in, data goes out, the database stays put.


问:
How is the Trial Period for Softwares generally implemented?
I was always under the assumption that trial softwares write a registry value of the installation date and then the software makes a comparison of the current date of the system with the remaining days + installation date to verify whether the trial period is still valid or not. If this assumption is to be true, winding your system clock further up ahead than the remaining days should end the trial period of the software (or keep winding the clock backwards to use the trial software as long as you wanted to). From experience, This was the case for some softwares, not so for others.

For the latter case, I noticed that the trial period expires only after the days have passed in real time. I confirmed that the software wasn't connecting to the outer network for validation mechanism, so it is definitely something being done locally on the computer. How does the trial period validation work in this case?

答:
I don't know how it's correctly done, there are problably others that can tell you.

However off the top of my head, to prevent someone from rolling back time you could store and update the current time, but only update it if the time is larger than the old stored time. That way you wouldn't have a future time unless someone had rolled their clock back.


答:
A calculator would be a good project for learning arithmetic operations. You can make it a simple menu-based console app, or a GUI. Four functions (+,-,*,/) to start, with extra points for more complex things like square root, etc.

For string handling, I'd suggest some validation problems. Email addresses come to mind - leaving them unchecked can lead to SQL injection holes, or other things, and they're relatively straightforward to validate. For appending, maybe have a program that can take first, middle, and last names and string them together with spaces, and also take a full name and parse it into each component. The first is used more than the second in real life, but I haven't been able to think of another parsing scenario that would be simple enough.

To demonstrate static methods, you could assign an email-lookup problem. Have a file of names and emails, and the static constructor could put the names/emails into a list, with a static method to find the email for a given name.

Hope these give you some ideas! Good luck with your class.

Title: What are some good assignments for an introductory course, to reach today's students?

Body:
I periodically teach an introduction to programming course using Java. I want to give my students some exciting assignments that they can relate to or find interesting. At the very least, I want assignments that make sense and have an internal cohesion (for example, some very bad programming exercises seem contrived just so that you have to use the most recent programming construct covered).

To give you an idea of scope, here's what's being covered:

The assignments must be in Java, using some external library can be done, but it would need to be a simple API and not a full framework

Variables, Primitives and Strings

Console input and output

if, for, while

Arithmetic and logical operators

Simple graphics with line and shape drawing

Static methods

One-dimensional arrays

The students will not go into advanced topics (e.g., no recursion, no emphasis on inheritance). Thus, I'm not looking for complex projects: "Have them write a C compiler. I did it when I was 5 and it was the only way I learned!"

If you have ideas that are more advanced than the scope, please post them on the "Challenging" question linked below, and not this one.

Instead, what I'm looking for are interesting ideas that can be accomplished in a short program. For example:

Students can write a console version of the "Star Wars Name" Generator. This is effectively reading Strings and using substring, but I think it's fun. A similar variation would be a "Mad Libs" program. I used this one five years ago, and perhaps it's already "stale."

Using some trig supplied to them, students can draw regular polygons, and create interesting spiral shapes.

It's also not out of the question to use some simple animation class with most of the code supplied to them. And if you know a Twitter or Facebook service that can be accessed with a simple API, I would be very interested to know.

Please note that this question is different from the "Challenging Java questions for beginners" Question. I'm not looking for challenging per se, just interesting. I think if students work on something where they can easily answer "why would anyone ever want to program that?" then they will learn better.

Even simple problems like computing Miles per Gallon can be good. Although, if there's a way to update it to be more relevant, all the better. If you have an exercise from somewhere else (a book or a website), please cite the source.

To help you think about the typical freshman today, check out the Beloit Mindset list, for many examples that will surely make you feel old.


问:
What's the effect of this assignment (whatever the language)?
I think my book (Programming Languages: Principles and Paradigms) is wrong. a is a vector, assume a C-like language:

b = 0;
a[f(3)] = a[f(3)] + 1;

int f(int n) {
    if(b == 0) {
        b = 1;

        return 1;
    }
    else return 2;
}
```

It has the effect of assigning the value of a[1] + 1 to a[2] whenever
  the evaluation of the left-hand component of the assignment precedes
  the evaluation of the right-hand one.

For me, it has the opposite effect, that is assigning the value of a[2] + 1 to a[1].

Because the left-hand component is evaluated first, so f returns 1 (because b is 0), thus a[1]. Then the right-hand component is evaluated, f returns 2 (because now b is 1), thus a[2] + 1.

Am I right and the book is wrong? Unfortunatly there is no errata...

答:
You are correct. The first call to f() will evaluate to 1, subsequent ones evaluate to 2. If the left-hand operand of the assignment is indeed fully evaluated first (which is kind of debatable), then the assignment evaluates to:

```<code>a[1] = a[2] + 1;
</code>```

This, however, also assumes that the value of b is never modified before this line is called, and also that f() is never called before this line.

Also note that the n parameter to f() is never used, so I assume it's quite likely that there is some typo of sorts there.

It would be interesting to know what point the author is trying to make here.

Body:
You are correct. The first call to f() will evaluate to 1, subsequent ones evaluate to 2. If the left-hand operand of the assignment is indeed fully evaluated first (which is kind of debatable), then the assignment evaluates to:

a[1] = a[2] + 1;

This, however, also assumes that the value of b is never modified before this line is called, and also that f() is never called before this line.

Also note that the n parameter to f() is never used, so I assume it's quite likely that there is some typo of sorts there.

It would be interesting to know what point the author is trying to make here.


答:
Passing Connection instances around is not usually a problem, even though in most situation only the DAO implementations should have anything to do with them. Now, with your problem being about connections not being closed after used, it is actually easy to fix: the Connection object needs to be closed at the same level it is opened, i.e. in the same method. I personally use the following code pattern :

```<code>final Connection cnx = dataSource.getConnection();
try {
    // Operations using the instance
} finally {
    cnx.close();
}
</code>```

That way I ensure all connections are always closed, even if an exception is thrown within the block. I actually go as long as using the exact same pattern for Statement and ResultSet instances, and everything has been smooth sailing so far.

Edit 2018-03-29: As indicated by user1156544 in the comments below, starting with Java 7 the use of the try-with-resources construct should be favoured. Using it, the code pattern I provided in my initial answer can be simplified like so:

```<code>try (final Connection cnx = dataSource.getConnection()) {
    // Operations using the instance
}
</code>```

Body:
Passing Connection instances around is not usually a problem, even though in most situation only the DAO implementations should have anything to do with them. Now, with your problem being about connections not being closed after used, it is actually easy to fix: the Connection object needs to be closed at the same level it is opened, i.e. in the same method. I personally use the following code pattern :

final Connection cnx = dataSource.getConnection();
try {
    // Operations using the instance
} finally {
    cnx.close();
}

That way I ensure all connections are always closed, even if an exception is thrown within the block. I actually go as long as using the exact same pattern for Statement and ResultSet instances, and everything has been smooth sailing so far.

Edit 2018-03-29: As indicated by user1156544 in the comments below, starting with Java 7 the use of the try-with-resources construct should be favoured. Using it, the code pattern I provided in my initial answer can be simplified like so:

try (final Connection cnx = dataSource.getConnection()) {
    // Operations using the instance
}


答:
I think your interpretation is correct. If the book's author has an e-mail address, you might want to contact him.

I have not read the book, but I guess the next paragraph will state that it is dangerous to rely on such side-effects, since they make code hard to read and lead to difficult bugs. Quod erat demonstrandum, I would say. :-)

Title: What's the effect of this assignment (whatever the language)?

Body:
I think my book (Programming Languages: Principles and Paradigms) is wrong. a is a vector, assume a C-like language:

b = 0;
a[f(3)] = a[f(3)] + 1;

int f(int n) {
    if(b == 0) {
        b = 1;

        return 1;
    }
    else return 2;
}
```

It has the effect of assigning the value of a[1] + 1 to a[2] whenever
  the evaluation of the left-hand component of the assignment precedes
  the evaluation of the right-hand one.

For me, it has the opposite effect, that is assigning the value of a[2] + 1 to a[1].

Because the left-hand component is evaluated first, so f returns 1 (because b is 0), thus a[1]. Then the right-hand component is evaluated, f returns 2 (because now b is 1), thus a[2] + 1.

Am I right and the book is wrong? Unfortunatly there is no errata...