问:
Is test coverage an adequate measure of code quality?
If I have some code that has 80% test coverage (all tests pass), is it fair to say that it's of higher quality than code with no test coverage?

Or is it fair to say it's more maintainable?

答:
In a strict sense, it is not fair to make any claims until the quality of the test suite is established. Passing 100% of the tests isn't meaningful if most of the tests are trivial or repetitive with each other.

The question is: In the history of the project, did any of those tests uncover bugs? The goal of a test is to find bugs. And if they didn't, they failed as tests. Instead of improving code quality, they might only be giving you a false sense of security.

To improve you test designs, you can use (1) whitebox techniques, (2) blackbox techniques, and (3) mutation testing.

(1) Here are some good whitebox techniques to apply to your test designs. A whitebox test is constructed with specific source code in mind. One important aspect of whitebox testing is code coverage:

Is every function called? [Functional coverage]

Is every statement executed? [Statement coverage-- Both functional coverage and statement coverage are very basic, but better than nothing]

For every decision (like if or while), do you have a test that forces it to be true, and other that forces it to be false? [Decision coverage]

For every condition that is a conjunction (uses &&) or disjunction (uses ||), does each subexpression have a test where it is true/false? [Condition coverage]

Loop coverage: Do you have a test that forces 0 iterations, 1 iteration, 2 iterations?

Is each break from a loop covered?

(2) Blackbox techniques are used when the requirements are available, but the code itself is not. These can lead to high-quality tests:

Do your blackbox tests cover multiple testing goals? You'll want your tests to be "fat": Not only do they test feature X, but they also test Y and Z. The interaction of different features is a great way to find bugs.

The only case you don't want "fat" tests is when you are testing an error condition. For example, testing for invalid user input. If you tried to achieve multiple invalid input testing goals (for example, an invalid zip code and an invalid street address) it's likely that one case is masking the other.

Consider the input types and form an "equivalence class" for the types of inputs. For example, if your code tests to see if a triangle is equilateral, the test that uses a triangle with sides (1, 1, 1) will probably find the same kinds of errors that the test data (2, 2, 2) and (3, 3, 3) will find. It's better to spend your time thinking of other classes of input. For example, if your program handles taxes, you'll want a test for each tax bracket. [This is called equivalence partitioning.]

Special cases are often associated with defects. Your test data should also have boundary values, such as those on, above, or below the edges of an equivalence task. For example, in testing a sorting algorithm, you'll want to test with an empty array, a single element array, an array with two elements, and then a very large array. You should consider boundary cases not just for input, but for output as well. [This is call boundary-value analysis.]

Another technique is "Error guessing." Do you have the feeling if you try some special combination that you can get your program to break? Then just try it! Remember: Your goal is to find bugs, not to confirm that the program is valid. Some people have the knack for error guessing.

(3) Finally, suppose you already have lots of nice tests for whitebox coverage, and applied blackbox techniques. What else can you do? It's time to Test your Tests. One technique you can use is Mutation Testing.

Under mutation testing, you make a modification to (a copy of) your program, in the hopes of creating a bug. A mutation might be:

Change a reference of one variable to another variable;
  Insert the abs() function;
  Change less-than to greater-than;
  Delete a statement;
  Replace a variable with a constant;
  Delete an overriding method;
  Delete a reference to a super method;
  Change argument order

Create several dozen mutants, in various places in your program [the program will still need to compile in order to test]. If your tests do not find these bugs, then you now need to write a test that can find the bug in the mutated version of your program. Once a test finds the bug, you have killed the mutant and can try another.

Addendum: I forgot to mention this effect: Bugs tend to cluster. What that means is that the more bugs you find in one module, the higher the probability that you'll find more bugs. So, if you have a test that fails (which is to say, the test is successful, since the goal is to find bugs), not only should you fix the bug, but you should also write more tests for the module, using the techniques above.

So long as you are finding bugs at a steady rate, testing efforts must continue. Only when there is a decline in the rate of new bugs found should you have confidence that you've made good testing efforts for that phase of development.


问:
Merits of a "Message Passing" system vs. an "Event Based" system
My question is coming from an somewhat uneducated perspective.

What are the relative merits of a "message passing" system vs an "event based" system.

Why would one choose one over the other? What are their strengths and weaknesses?

I would like to know not just "in theory", but also "in practice".

EDIT:

Specific problem:

I want to build system of plug-able components which behave as small scale services (each performing some small task and or providing some piece of information).

The services can:

be layered so that the output of one service can act as one of the inputs to another

have containment hierarchies so that one service can be contained by another without a specific input-output relationship as mentioned in the previous sentence

The goal of the system is to translate a single flow of low level events in another system into higher level information and functionality, and in addition provide a channel back to the other system providing a single series of events.

I can provide more detail if this isn't enough.

After looking around some more. This and this probably describes my situation better.

This looks like it may be a good fit for my situation: http://akka.io/

答:
This is apples and oranges:

An Event Driven system can react to events passed as messages ( messages in this context are implied immutable non-shared data ) as the events are raised. This is a purely architectural design.

A Message Passing system can be driven by events that create and pass the messages. This is a purely implementation design.

The two are not mutually exclusive.

Example: You can implement an event driven design in any language which may also be a message passing environment as in Erlang.

ContentLicense: CC BY-SA 3.0

ContentLicense: CC BY-SA 4.0


问:
Handling SMS/email convergence: how does a good business app do it?
I'm writing a school administration software package, but it strikes me that many developers will face this same issue: when communicating with users, should you use email or SMS or both, and should you treat them as fundamentally equivalent channels such that any message can get sent using any media, (with long and short forms of the message template obviously) or should different business functions be specifically tailored to each of the 3?

This question got kicked off "StackOverflow" for being overly general, so I'm hoping it's not too general for this site - the answers will no doubt be subjective but "you don't need to write a whole book to answer the question".

I'm particularly interested in people who have direct experience of having written comparable business applications.

Sub-questions:

Do I treat SMS as "moderately secure" and email as less secure? (I'm thinking about booking tokens for parent/teacher nights, permission slips for excursions, absence explanation notes - so high security is not a requirement for us, although medium security is)

Is it annoying for users to receive the same message on multiple channels?

Should we have a unified framework that reports on delivery or lack thereof of emails and SMS's?

答:
A common subsystem of applications with sufficient scope and maturity is what I refer to as Alerts and Notifications.

The basic gist is that the system has certain events that users and other systems may be interested in (That's the alert). Users can subscribe to alerts and inform the system how they want the alerts to be delivered. Some kinds of alerts might be high priority so they want them blasted across every channel immediately. Others might be something that doesn't require immediate action (or any) so the user might want it delivered in a digest with other low priority alerts.

You should consider designing your system to enable users to configure what alerts they want to receive and how they want to receive them. To answer your specific questions:

The security of each channel depends on the measures put in place to ensure security. For example if a user opts to receive SMS messages, what happens if someone gets their phone? If you require users to pin-lock their phones, you can assume some modicum of security. If not, well it's about as secure as water in a sieve. The same considerations apply to email. What measures are in place to protect against unauthorized users accessing the email account?

Being able to configure how they receive messages addresses this issue.

Yes I feel that you'll want something in place that you can leverage and extend without the client code having to be impacted.


问:
What are some things you have read that inspired and guided you as a programmer?
For example, being a beginner, I find a lot of inspiration and direction from reading this post by Bryan Woods.</p>

答: The most useful single book ever - not about programming, but about how to work effectively in this field: "Peopleware" by DeMarco and Lister. Should be on every programmers book shelf. And if you move up into management, doubly so.</p>

ContentLicense: CC BY-SA 2.5


问:
Should I perform encryption in the front end or within the database?
I want to store an encrypted string (specifically, email addresses) in a database. I'm currently using Python and MySQL. I was initially going to use MySQL's AES_ENCRYPT/DECRYPT to handle it, but then got to wondering if one was was better than another.

It seems that if I tie it to a database function, that could potentially lock me into the database and at this point that selection may change down the road. Otherwise I would look to handling it in the Python code, but part of me thinks that having it all handled on the back end would free you to use whatever front end logic and code you wanted to.

Any thoughts or ideas?

答:
I think it would depend on any future plans you have. Do you see yourself using something other than MySQL? Or perhaps see yourself using some other encrypting algorithm?

If you can answer yes (or maybe) to any of those two, then it would be a good time investment to delegate that task to the "middle ware", which ideally should come between the front end and the DB.


答:
There are a lot of depends on this, but I would tend to put it in the application layer rather than the database.

if someone downloads your database, they potentially have the encryption code.  That means in this regard your attempt to secure data is useless.  Remember you always have to assume a worse case scenario, so if they have the code, then you have to assume they can use it.

Encryption isn't what a database is used for.  In scaling out systems, you can always easily add another app server.  It's difficult to add a database (syncing the two etc.).  I try and keep as little business logic code as possible on the database server, because if you start having processes eating up cpu and memory on the database, you have to start migrating off of that server.  It's easier to just not have to do this in the first place.

Title: Should I perform encryption in the front end or within the database?

Body:
I want to store an encrypted string (specifically, email addresses) in a database. I'm currently using Python and MySQL. I was initially going to use MySQL's AES_ENCRYPT/DECRYPT to handle it, but then got to wondering if one was was better than another.

It seems that if I tie it to a database function, that could potentially lock me into the database and at this point that selection may change down the road. Otherwise I would look to handling it in the Python code, but part of me thinks that having it all handled on the back end would free you to use whatever front end logic and code you wanted to.

Any thoughts or ideas?


答: Among many inspirations the most recent one was this book on: Apprenticeship Patterns

Title: What are some things you have read that inspired and guided you as a programmer?

Body: For example, being a beginner, I find a lot of inspiration and direction from reading this post by Bryan Woods.</p>


答:
Since AES_ENCRYPT/DECRYPT are just functions that you can apply to your data as part of the query, I don't see how that would cause lock in. If you write your code to use it, then change your mind and decide its better to have it handled by your front-end, you'll just have to:

Change your queries/view so they don't use those functions anymore;

Change the code calling those queries and have them encrypt/decrypt the data using the same algorithm.

(Those two changes must come into effect simultaneously, of course; the converse operation is also possible)

The main advantage I see in doing it in the front end is that you can choose other values for the key size and padding (MySQL uses 128bits and 16 respectively), if needed. You can also change the algorithm more easily, if eventually you decide AES is not the best choice.

I left aside security implications of your setup, since it's hard to tell from which kinds of risks you're trying to protect your data. However, I agree with @Kevin that you get a little more security (by obscurity) if you don't explicitly show the algorithm in the database, assuming your code is kept secret.

Edit: seems I misunderstood Kevin's argument; while I agree that hiding the algorithm is mostly pointless, hacking your server is not the only way for an attacker to access your database - the processes you use to make regular backups of it, including offline ones, creates more opportunities for data leaks that does not necessarily applies to the code (which might have its own process vulnerabilities as well). But in the end what matters most is how strong and well-protected are your encryption key(s).


答: In my experience, the biggest difference between the two is that messages in a message-passing system are first-class objects, whereas events in event-driven systems are much simpler.  Messages tend to carry information, and that information may be transformed, stored, retrieved and re-sent.  Events tend to carry smaller, more focused bits of information that are immediately consumed and then discarded.  They tend to be sent from an event source directly to one or more event sinks, whereas messages are more often routed among several receivers, and may be converted/translated/wrapped or otherwise processed at any point along the route.  They use similar technologies (buses, queues, filters, etc) but they're really disparate beasts.</p>

Title: Merits of a "Message Passing" system vs. an "Event Based" system

Body:
My question is coming from an somewhat uneducated perspective.

What are the relative merits of a "message passing" system vs an "event based" system.

Why would one choose one over the other? What are their strengths and weaknesses?

I would like to know not just "in theory", but also "in practice".

EDIT:

Specific problem:

I want to build system of plug-able components which behave as small scale services (each performing some small task and or providing some piece of information).

The services can:

be layered so that the output of one service can act as one of the inputs to another

have containment hierarchies so that one service can be contained by another without a specific input-output relationship as mentioned in the previous sentence

The goal of the system is to translate a single flow of low level events in another system into higher level information and functionality, and in addition provide a channel back to the other system providing a single series of events.

I can provide more detail if this isn't enough.

After looking around some more. This and this probably describes my situation better.

This looks like it may be a good fit for my situation: http://akka.io/


答:
In SOA you have the concept of Command Messages and Event Messages.  There is a need for both.

However, command messages have a higher behavioural coupling to the recipient endpoint since it is explicitly asking the endpoint to perform some function.  It need not be tied to a particular endpoint (this can be configured or determined at runtime).

Event messages, on the other hand, have no behavioural coupling between the sender/recipient since the sender has no idea what a recipient is going to do with the message.  It doesn't even know if anyone is subscribed to the event.

For some more background you are welcome to peruse my service bus site here: http://www.servicebus.co.za


问:
Displaying the number of registered users to a website
I am building an online game much like Mafia Wars. I was thinking of displaying the number of registered users on the home page, but then I thought:

"What if a new user sees that the total number of registered users
  is 5?!" :D

That would be too embarrassing to my still growing game. Then another thought came by:

"Why don't I display 500 instead?!" ;)

Nobody would know if there is actually 500 users registered to my website unless of course they start actually playing and interacting with other players.

However, I just have a feeling that this isn't ethical. What if this generates a negative publicity to my website?!!

What's your opinion should I fake the number? or should I honestly display it? or should I wait till I have 1000 real account then start displaying the number?

And most importantly is this legal?! :D

答:
You shouldn't be afraid to actually say you only have 5 users online. Everyone starts small. You should run advertising campaigns on big sites that are mmo-based.

However, if you still want to lie and be dishonest, you could always 'manipulate' the number.


问:
How does agile estimate the time required for a "research phase"?
According to canonical agile development, what is the appropriate amount of learning that an engineer should engage in before implementing a solution to a problem?

If an engineer knows she is too ignorant of a given topic (say, compression) to start implementing, she goes and learns what she thinks she needs to know, and then implements a solution.

Clearly a deeper understanding of said topic could result in a better implementation, but at the cost of agility. On the flip side if an engineer learns too little about a topic, there is a danger she will implement a significantly insufficient / broken implementation that negatively affects customers in the immediate or near future. In some cases, not a lot of research is required, and in some cases developers are faced with a topic even the senior-most developer knows little to nothing about, so the answer to this question includes an element of scalability to meet both situations.

Towards which end of this scale, if any, do guiding principles of agile lead an engineer? Or does agile leave this as a sliding scale, where the answer may be different in any given situation?

In other words, I'm looking to understand not when/how inside agile development research happens, but moreover how to estimate a "good" amount of time to spend learning a topic in the research phase before moving on to implementing a solution.

答:
"Agile" doesn't say anything about this specifically, but it does specifically say that things like this are best decided by the team. If it did dictate things like this it wouldn't be called "Agile".

The Agile that can be specified isn't Agile.


问:
Is loose coupling w/o use cases an anti-pattern?
Loose coupling is, to some developers, the holy grail of well-engineered software.  It's certainly a good thing when it makes code more flexible in the face of changes that are likely to occur in the foreseeable future, or avoids code duplication.

On the other hand, efforts to loosely couple components increase the amount of indirection in a program, thus increasing its complexity, often making it more difficult to understand and often making it less efficient.

Do you consider a focus on loose coupling without any use cases for the loose coupling (such as avoiding code duplication or planning for changes that are likely to occur in the foreseeable future) to be an anti-pattern?  Can loose coupling fall under the umbrella of YAGNI?

答:
I think what you're getting at here is the concept of cohesion. Does this code have a good purpose? Can I internalize that purpose and understand the "big picture" of what's going on?

It could lead to hard-to-follow code, not just because there are many more source files (assuming these are separate classes), but because no single class seems to have a purpose.

From an agile perspective, I might suggest that such loose coupling would be an anti-pattern. Without the cohesion, or even use cases for it, you cannot write sensible unit tests, and cannot verify the code's purpose. Now, agile code can lead to loose coupling, for example when test-driven development is used. But if the right tests were created, in the right order, then it's likely there is both good cohesion and loose coupling. And you're talking about only the cases for when clearly no cohesion is there.

Again from the agile perspective, you don't want this artificial level of indirection because it is wasted effort on something that probably won't be needed anyway. It's much easier to refactor when the need is real.

Overall, you want high-coupling within your modules, and loose coupling between them. Without the high-coupling, you probably do not have cohesion.


答:
From a practical point of view, messages are typically implemented as a block of memory that is copied from the sender's address space to the receiver's address space (or is otherwise enforced to be an immutable object) so you do gain thread-safety immediately.

In some cases of message passing the sender must specify the receiver, but in some other cases you can just post a message to a mailbox, and anyone can listen for messages in that mailbox, so there is some flexibility in how tightly coupled they are.  Of course, you can have a mailbox where the contract is "I will post a message to this mailbox when this event happens."

In the case of events you're registering a delegate or callback with the owner of the event.  In object oriented programming this means the event owner keeps a reference to the object that registered to receive the event.  This can sometimes lead to bookkeeping nightmares, trying to figure out what object forgot to unregister their event handler.  It means the target can't be garbage collected until the event owner is garbage collected, even if the target is no longer doing anything useful.

Personally I would choose message passing over events, except in cases where events are forced on you, such as Windows GUI programming, etc.


问:
Writing commit messages as a solo developer?
On my projects where the repository is shared between me and other programmers, I always write commit messages even if I'm the primary developer.

But on those projects where I'm the solo developer working on a project, and the repository is hosted on my personal laptop, and is not even hosted by the client, hence no one except myself would see the commits, should I still write commit messages?

So far I have been writing them, but I've found that I've never gone back and viewed my commit messages. I take time off development to write down the messages, but then they're never seen again even by me.

Are there any good reasons for writing commit messages as a solo developer, or should you just skip them in favor of staying focused on development?

答: Definitely, or else you make features like branching and merging a lot harder to use. And you will want to use them, even if solo developer.


答: I try to always.  How many times do you look back and think, "Man what was I doing when I made this change."  I do all the time.  30 seconds of writing a message can save you 20 minutes worth of work trying to remember. </p>

Title: Writing commit messages as a solo developer?

Body:
On my projects where the repository is shared between me and other programmers, I always write commit messages even if I'm the primary developer.

But on those projects where I'm the solo developer working on a project, and the repository is hosted on my personal laptop, and is not even hosted by the client, hence no one except myself would see the commits, should I still write commit messages?

So far I have been writing them, but I've found that I've never gone back and viewed my commit messages. I take time off development to write down the messages, but then they're never seen again even by me.

Are there any good reasons for writing commit messages as a solo developer, or should you just skip them in favor of staying focused on development?


答:
While you have not come across the need to view your commit messages yet, you may be very grateful for them in the future.  You should keep writing them even for yourself.  There are many reasons they could be useful later (You forgot why you were adding a feature, locate missing files, etc.)

Here is a related question regarding the purpose of commit messages: Why Should I Write A Commit Message


答:
I always commit with a meaningful message about changes, and I do so frequently with incremental changes.

Is this always the most useful thing? No, there is no problem in doing so. If you need to revert to a previous stage in progress your messages will let you know where you are and what you've done. It also can be used to keep track of progress on a project. As for it being a viewed as some waste of time, is the 30 seconds it takes to jot down a sentence really that consequential?


问:
Rigorous Definition of Syntactic Sugar?
It seems like in language holy wars, people constantly denigrate any feature they don't find particularly useful as being "just syntactic sugar".  The line between "real features" and "syntactic sugar" tends to get blurred in these debates.  What do you believe is a reasonable and unambiguous definition of syntactic sugar that avoids it being defined as any feature the speaker/writer doesn't find useful?</p>

答:
Here is a very rigorous definition for a related concept: expressiveness, by

Matthias Felleisen:

On the Expressive Power of Programming Languages [Postscript was the only free form I could find.]

Also see this entry on the Java language and closures.

Effectively, something is syntactic sugar if it can be changed to a form without the syntax by only making local changes. If, for example, without the syntactic form, you would need to change several different code locations, or move fragments to other locations, then it is not sugar.

That said, syntactic sugar is fine when used appropriately. I think any Scheme programmer would rather there be a let special form than to have to create a new anonymous function and then apply it, which would do the same thing. The purpose is to make code clearer.

Title: Rigorous Definition of Syntactic Sugar?


答:
Well here is one reason: If you suddenly realize something has been broken for the last few hundred commits (possible if you commit at every minor edit, less feasible if you, like me, commit only "stable" snapshots), you can more easily find where you've inserted the bug if you've written clear commit messages, rather than "bugfixes." (A colleague's favorite string, I believe). Sure, you can go with svn log or whatever SCM you are using, but it should be easier the other way around.

Also commit messages force you to think what exactly you have done as a change and, I believe, summing up in your mind how it would be best to continue improving the project.


答:
Do you honestly believe that the overhead of typing around 40 to 80 characters in plain english is significant overhead for a commit, or are you looking for an excuse to be lazy?

Pehaps you problem is expressing in plain english why you made the change, in which case, you may need to the review the purpose of the change, even to the point of asking yourself do you really need to do it.

My advise is don't think that because you are flying solo you can break the rules. Remain professional at all times, and add meaningful commit messages. As noted by other reponders, one day you will be thankful of it.


问:
<<access>> and <<use>> in UML Component diagrams
How to decide if have to take the <<access>> or <<use>> notation for "-->" arrows in Component diagrams written in UML?

答:
The keyword «access» is used for private package imports. The UML 2.4.1 spec says "A package import is shown using a dashed arrow with an open arrowhead from the importing namespace to the imported package. A keyword is shown near the dashed arrow to identify which kind of package import is intended. The predefined keywords are «import» for a public package import, and «access» for a private package import."[1]

The keyword «use» is used to depict the use of an interface. The UML 2.4.1 spec says "For displaying the full signature of an interface of a component, the interfaces can also be displayed as typical classifier rectangles that can be expanded to show details of operations and events." [2] and "A usage dependency is shown as a dependency with a «use» keyword attached to it." [3]

[1] UML Superstructure Specification, v2.4.1; page 112; http://www.omg.org/spec/UML/2.4.1/Superstructure/PDF

[2] UML Superstructure Specification, v2.4.1; page 153; http://www.omg.org/spec/UML/2.4.1/Superstructure/PDF

[3] UML Superstructure Specification, v2.4.1; page 139; http://www.omg.org/spec/UML/2.4.1/Superstructure/PDF


答:
The speed of Jr. developers vs Sr. Developers is taken into account for the overall velocity of the team. So if a single member is lacking in knowledge, this is no different and should reflect itself very quickly as the velocity adjusts. With senior level developers on the team, and knowing who is most likely to work on a particular task, then the estimation should account for the ramp up time, and not have any noticeable effect on velocity at all.

If the whole team needs to learn the topic, then senior level developers should consistently predict the amount of ramp up time based on the complexity of the topic. This is due to prior experience of having to learn things on the job. In other words, its a learned ability.

In the end, worst case scenario is that your velocity will fall off unexpectedly and your team will need to readjust and lower the expectations from sprint to sprint (if you have those) so that expected velocity is agreed upon by all parties. Once this learning curve has been overcome, then velocity will increase and adjustments in commitments can move upwards again.

Title: How does agile estimate the time required for a "research phase"?

Body:
According to canonical agile development, what is the appropriate amount of learning that an engineer should engage in before implementing a solution to a problem?

If an engineer knows she is too ignorant of a given topic (say, compression) to start implementing, she goes and learns what she thinks she needs to know, and then implements a solution.

Clearly a deeper understanding of said topic could result in a better implementation, but at the cost of agility. On the flip side if an engineer learns too little about a topic, there is a danger she will implement a significantly insufficient / broken implementation that negatively affects customers in the immediate or near future. In some cases, not a lot of research is required, and in some cases developers are faced with a topic even the senior-most developer knows little to nothing about, so the answer to this question includes an element of scalability to meet both situations.

Towards which end of this scale, if any, do guiding principles of agile lead an engineer? Or does agile leave this as a sliding scale, where the answer may be different in any given situation?

In other words, I'm looking to understand not when/how inside agile development research happens, but moreover how to estimate a "good" amount of time to spend learning a topic in the research phase before moving on to implementing a solution.


答:
Much of the confusion between "message passing" and "event based" has to do with architectural vs. implementation details.   I have seen (and written) event driven systems that actually use OS provided messages for their implementation.  I am guessing you are really referring to architectural ideas.

As many people have already pointed out "message passing" and "event based" aren't really good enough terms to avoid ambiguity.

What are the relative merits of a "message passing" system vs an "event based" system.

Message Passing

I'm going to start out by guessing that when you say a "message passing" system, you are talking about a system which one object spends a message to a specific other object.  When I think of a system based based on this paradigm, I more generally think of a system where an object that detects something knows who needs to be told about something.  (I'm not specifying how it knows, just that it knows.)

This type of architecture is very good for systems where the producers and consumers are well known.  Either the producer of a message knows who must receive it, or the consumer must know from who to get the message from.

If you are writing a banking application, one would expect you really want to know who you are sending your transactions to and who they are coming from.

Event Based

The other system I believe you are thinking about when you say an "event based" system is one where an object raises an "event" without knowing who (if anyone) will respond to it.

This type of event driven architecture is very good for systems where the producer does not care about who consumes the event or where the consumer doesn't really care about who produced the event.

In general, these systems are great where you do no know the relationship between consumers and producers, and where you expect the relationship to be dynamic.

One system I have used this in was a system where the application was actually composed of dynamically configured modules (plug-ins) that were loaded at run time.  When a module was loaded, it would register for the events it cared about.  The result was a system in which it was very easy to extend the functionality.

For instance, let's say condition A raised Event EA which normally caused response RA.  The object that caused response RA simply registered to receive event EA and acted on it when it arrived.   Now, let's say we want to add a new response to EA, called RA_1.  To do this, we simply add a new object that looks for EA and generates response RA_1.

Here are a couple of examples (using your terminology):

"message passing":  Your boss tells you to fill out your time sheet.

"event driven": The department secretary sends out an email to
everyone reminding them that their time sheets are due today.


答:
If you store plain text, you decouple securty from database and business logic, however if the database is able to be accessed, the email addresses are in plain text. Theres been more than one instance of attackers getting hold of backups and/or old drives. Employee that have access to the DB have access to the plain text data, but not nessicerily encyprted data (Depends if they hace access to the key) ... What are you really after - a claim you are secure, if so from whom, or in depth security where you are protected from as many attackers as possible. </p>

<p>Personally If I wanted security, I would encypt in the database and store the key on a seperate machine (if at all). It's a bit more painful, but a lot more secure. If all I wanted was to claim security, I would store the data in plain text. </p>

Body:
If you store plain text, you decouple securty from database and business logic, however if the database is able to be accessed, the email addresses are in plain text. Theres been more than one instance of attackers getting hold of backups and/or old drives. Employee that have access to the DB have access to the plain text data, but not nessicerily encyprted data (Depends if they hace access to the key) ... What are you really after - a claim you are secure, if so from whom, or in depth security where you are protected from as many attackers as possible.

Personally If I wanted security, I would encypt in the database and store the key on a seperate machine (if at all). It's a bit more painful, but a lot more secure. If all I wanted was to claim security, I would store the data in plain text.


问:
Biggest mistake you've ever made
Similar to the question I read on Server Fault, what is the biggest mistake you've ever made in an IT related position. Some examples from friends:

I needed to do some work on a production site so I decided to copy over the live database to the beta site. Pretty standard, but when I went to the beta site it was still pulling out-of-date info. OOPS! I had copied the beta database over to the live site! Thank god for backups.

And for me, I created a form for an event that was to be held during a specific time range. Participants would fill out the form for a chance to win, and we would send the event organizers a CSV from the database. I went into the database, and found ONLY 1 ENTRY, MINE. Upon investigating, it appears as though I forgot an auto increment key, and because of the server setup there was no way to recover the lost data.

I am aware this question is similar to ones on Stack Overflow but the ones I found seemed to receive generic answers instead of actual stories :)

What is the biggest coding error/mistake ever…

答: Thinking that programming is mostly about building cool new stuff from scratch.</p>

Title: Biggest mistake you've ever made


问:
Where can I learn about how programs handle file systems like NTFS?
I'm writing a program to handle the xbox 360's STFS files. I'm encountering all sorts of problems about how to keep track of used vs unused blocks, reading non-sequential files, etc. These aren't so much problems with the structure of STFS - it's just another file structure - but I don't know how I should do certain things.

I thought I would find a plethora of information on how file systems like NTFS and Fat32 are read by programs, but I'm only finding resources on how to work with the file systems using pre-made software and not so much how to make such software.

Here is a reference for STFS: http://free60.org/STFS

答:
You're talking about a filesystem driver.  I'm not sure how much information you'll find on how to write them, because very few people need to do it, and the ones that do mostly know what they're doing.

One resource would be to look at the linux kernel code.


答:
NTFS is propritary and patent encumbered, you won't find anything very useful offically  about it from Microsoft publicly available. Microsoft charges a lot of money to hand out the details about it and the ability to commercially use it.

FAT32 is also patent encumbered, but has been worked around by the Linux/Open Source community. Microsoft has yet to file any lawsuits against any Linux vendors but Microsoft sued TomTom over their use.

Your best bet to read up on the open source file systems that are available, code speaks volumes. Study the structure of database files as well, those are closer in theory to STFS than actual file systems in many cases.

Title: Where can I learn about how programs handle file systems like NTFS?

Body:
I'm writing a program to handle the xbox 360's STFS files. I'm encountering all sorts of problems about how to keep track of used vs unused blocks, reading non-sequential files, etc. These aren't so much problems with the structure of STFS - it's just another file structure - but I don't know how I should do certain things.

I thought I would find a plethora of information on how file systems like NTFS and Fat32 are read by programs, but I'm only finding resources on how to work with the file systems using pre-made software and not so much how to make such software.

Here is a reference for STFS: http://free60.org/STFS


答:
You won't always be a solo developer.

You will eventually break your codebase and attempt to restore it. Good commit messages will save you hours.

You can't remember what you were working on three weeks ago, right?

Commit messages should be very clear. If they are not, then you have either didn't finish working on a task, or a single task has spread over two or more tasks.


问:
How can I keep my visualstudio database project in sync with my database?
I'd like to have my database schema synchronized with my Visual Studio .dbproj database project.

Right now, I'm using SSMS for most of my database development work, and I've to manually use the schema compare tool of visual studio when I need the db schema and the .dbproj to be synchronized. Why would I want this? because :

It's the only way to checkin changes

It's the only way to apply changes

Failure to do so will cause some merge issues when getting files (basically, if a "get latest" contains some db changes on db objects I've been updating whithout checking them out, a merge won't be triggered, and it's easy to keep track of which version is which)

It's nice to be able to use the search features of VisualStudio on an up-to-date version of the schema

It's nice to be able to modify stored procedures from VS (typically when you have renamed a file) using the search results.

Apart from automating the "schema compare" tool to have it run every 5 minutes (which hardly sounds like a "clean" solution), are there ways to achieve this ?

I'd like to keep using SSMS if possible, but I'm also interested in ways to automatically propagate changes made using the "Server Explorer/Database connections" of visual studio to the .dbproj.

答:
there are two ways that I know of with VS2010 ultimate features to do this.

Use SSMS or other tool to edit DB schema then run schema compare periodically (end of the day) and then import any schema changes to the DB project.

Edit the files in your DB project through VS and then create a deployment package that either runs directly on your DB or generates a script for you to run to update your schema in the DB.


答:
I've been the sole committer on the TXR project, and have kept a detailed ChangeLog from quite early in the project. This is close to 11,000 lines long and growing:
http://www.kylheku.com/cgit/txr/tree/ChangeLog

(The commit messages in the repo are just a copy of what goes in the ChangeLog.)

[2016 edit: as of the mid-2015, I no longer maintain a ChangeLog file; however, the commit messages are written in a format which conforms to Git and ChangeLog conventions at the same time. The same level of detail is there, in a way that doesn't cause merge problems. A ChangeLog file could be mechanically reconstructed from these comments.]

Yes, more than once I've gone back to an old commit message associated with a change which broke something (uncovered with the help of git bisect). The message helped me make sense of what I was doing.

In the ChangeLog you can tell when a function, type, macro or global variable was first introduced and when it was subsequently touched by changes.

But the main reason for writing detailed commit messages like these when working by yourself is this: you find bugs when doing this.

To write a detailed commit message has similar benefits to a code review of your commit by someone else. The value in a commit review is not so much that someone is checking your code, but that you have to explain your changes to another developer.

When you try to explain things, you sometimes find that they don't make sense.

Another reason: you can catch yourself making a useless change.  By writing a detailed commit comment, you capture a high level view of what you are doing, and then sometimes you are confronted by the fact that it's not a good change.

I've sometimes made changes, when in the middle of writing the ChangeLog entry I realized that this was going to be a git reset --hard (throw away these useless changes) rather than git commit -a.


答: I don't see any difference regarding commit messages whether you're working in a team or not. You remember what you did here or there only for a limited time, and after that it is the same as if someone else wrote it. So you should write messages as good as you would for the other people.</p>


答:
One possible reason to: it forces you to think more abstractly about the change you've just made, and to structure changes.

If it feels pointless, perhaps just comment the major changes, or the ones that change existing functionality, in case you are ever looking for the source of a weird behaviour.


问:
Is the Mercury Programming Language used in the field?
I've strong interests in Functional and Logic programming and Mercury seems like a good blend of the two. Is it used in industry at all? I'm one to learn a language just for the fun of it but it would nice to have practical, employable avenues for it. I could not find much in the way of examples of use in industry on the language website.</p>

答:
Mercury is a souped up Prolog, but logic languages are very niche in the industry, and moreso than functional languages. Dynamic scripting languages like Python, Ruby, and PHP, and languages like Java and C# will dominate most of what employers are looking for. The landscape seems different in Europe and Australia, but only slightly.

That said, there are companies that use Mercury, though I only know of two: YesLogic, which makes PrinceXML. They are located in Australia. The other is Mission Critical IT, which is based in Brussels and Australia.

Searches on major job sites like indeed.com, glassdoor.com, monster.com, and also on careers.stackoverflow.com did not turn up Mercury-specific jobs.

However, having skills with Mercury, you can probably parlay them into working at places where Prolog has been traditionally used, like in AI.

But I think the language will mostly be niche, like Haskell. There will be a few companies here and there using it, but the interest will remain strongly in academia and research.


问:
How would you transfer data between your data structures and databases?
I'm learning programming in school and I have this question that's bugging me about data structures and transferring the information stored inside them to databases. We're doing just small systems - registration for an event,  point of sale for a local restaurant, sales and inventory, etc

Let's say someone registers himself, should it be passed into a data structure and then into the database, or should it just be transferred directly to the database?

答:
The aim is to store data into the database. If the data structure isn't needed for your use case, then don't use it.

In most apps I have worked on, data structure would be used only because I am working with code that expects (or needs) a data structure to be passed around between different business logic methods before the data can be saved to the database.

OwnerDisplayName: Daryll Santos


答:
At its most basic, what you need to develop to transfer data between data structures in memory and persistent data stores (files, DB, whatever) is a Data Access Layer or DAL. This is one of your "application tiers" in a properly-constructed N-tier application. Objects in this tier know how to create data structures from DB data, and conversely to convert a properly-populated data structure into DB data, so that objects generally lying outside this tier don't have to have this knowledge.

There are several models for building a DAL tier. The most common in my experience is a centralized "front door" that can accept any request to read or write data and produce any needed data structure. This pattern is called the "Repository". You would make a call to this object along the lines of:

```<code>int myObjectId = 1243;
MyObject myObjectInstance = myRepository.RetrieveById<MyObject>(myObjectId);
</code>```

... and then myObjectInstance would be populated with the data from the table representing MyObject records, with the identifying key that we specified. How that happens exactly, nobody outside the Repository really has to know; you could dynamically construct a SQL statement, use a Data Access Object that knows specifically how to retrieve MyObject instances, or you could do as Oded states and use a third-party library called an Object-Relational Mapper or ORM which abstracts away all the details of how the retrieval happens.

Title: How would you transfer data between your data structures and databases?

Body:
I'm learning programming in school and I have this question that's bugging me about data structures and transferring the information stored inside them to databases. We're doing just small systems - registration for an event,  point of sale for a local restaurant, sales and inventory, etc

Let's say someone registers himself, should it be passed into a data structure and then into the database, or should it just be transferred directly to the database?

Body:
At its most basic, what you need to develop to transfer data between data structures in memory and persistent data stores (files, DB, whatever) is a Data Access Layer or DAL. This is one of your "application tiers" in a properly-constructed N-tier application. Objects in this tier know how to create data structures from DB data, and conversely to convert a properly-populated data structure into DB data, so that objects generally lying outside this tier don't have to have this knowledge.

There are several models for building a DAL tier. The most common in my experience is a centralized "front door" that can accept any request to read or write data and produce any needed data structure. This pattern is called the "Repository". You would make a call to this object along the lines of:

int myObjectId = 1243;
MyObject myObjectInstance = myRepository.RetrieveById<MyObject>(myObjectId);

... and then myObjectInstance would be populated with the data from the table representing MyObject records, with the identifying key that we specified. How that happens exactly, nobody outside the Repository really has to know; you could dynamically construct a SQL statement, use a Data Access Object that knows specifically how to retrieve MyObject instances, or you could do as Oded states and use a third-party library called an Object-Relational Mapper or ORM which abstracts away all the details of how the retrieval happens.

OwnerDisplayName: KeithS


答:
The most inspiring talk I've ever heard was Richard Gabriel talking about that writing software is similar to writing poems.  Poets often use a lot of effort to get few words just right - think Haikus - and we should strive towards the same kind of excellence and elegance when writing software.

Elegance and conciseness is as important in programming as in poetry and math, and it requires a lot of effort to do so fluently.

See some of the points on http://www.dreamsongs.com/PoetryOfProgramming.html


问:
Standardized Data Structure Interface
I want to work with a variety of data structures (arrays, singly/doubly linked lists, sorted structures, etc.) on a plug-and-play basis. For example, I want to be able to easily swap in and out the sorted list and the array (which is re-sorted after every insertion/deletion), to test which one has better performance.

In every language I know of, the public interface to such data structures is very inconsistent. So changing the implementation requires a lot of work.

Is there a language, or a language-agnostic design pattern, that makes it easy to define a generic public interface that works with any data structure?

Of course, I understand how different performance would be (e.g., binary search in a sorted array vs linear search in a regular array). All I ask is that the performance of the data structure doesn't degrade asymptotically or by a large constant factor due to the use of the standard interface.

Example problem: iterate through the container of numbers, find the two numbers that are nearest  each other, and remove the pair of them. (If several pairs are equal distance, pick an arbitrary pair).

I'd like the code to solve the problem to be unchanged as I switch in and out different containers.

答:
Java does this somewhat.

See the List, Set, or Map interfaces. Each interface has a number of implementations, implementing the various operations in different ways.

Python also does this somewhat by virtue of its duck typing. The interface of different objects is similar, so you use the same code on different data structures.

Title: Standardized Data Structure Interface


问:
foreach($items as &$item) considered harmful?
Is it considered bad practice to pass items in a PHP array by reference instead of by value?

Relevant documentation: http://php.net/manual/en/control-structures.foreach.php

答:
It's not harmful if it's appropriate for the use case.

If you are concerned:

```<code>foreach($foo as &$bar) {
    $bar['addthis'] = 'x';
}
</code>```

Can also be written as:

```<code>foreach($foo as $i => $bar) {
    $foo[$i]['addthis'] = 'x';
}
</code>```

Note that as indicated by animuson in a comment, and the big fat warning on the docs page, if you leave a reference lying around and reuse the variable name - you're going to update what you are referencing.

That doesn't mean references are harmful; it just means learn how to use a tool - and then use it correctly.

Body:
It's not harmful if it's appropriate for the use case.

If you are concerned:

foreach($foo as &$bar) {
    $bar['addthis'] = 'x';
}

Can also be written as:

foreach($foo as $i => $bar) {
    $foo[$i]['addthis'] = 'x';
}

Note that as indicated by animuson in a comment, and the big fat warning on the docs page, if you leave a reference lying around and reuse the variable name - you're going to update what you are referencing.

That doesn't mean references are harmful; it just means learn how to use a tool - and then use it correctly.


问:
Is test coverage an adequate measure of code quality?
If I have some code that has 80% test coverage (all tests pass), is it fair to say that it's of higher quality than code with no test coverage?

Or is it fair to say it's more maintainable?

答:
Code with absolutely no tests can be extremely high quality, readable, beautiful and efficient (or total junk), so no, it's not fair to say that code with 80 % test coverage is of higher quality than code with no test coverage.

It could be fair to say that code 80 % covered with good tests is probably of acceptable quality, and probably relatively maintainable. But it guarantees little, really.


问:
Building software frontend for hardware that does not yet exist
Our task is to build a software frontend to control a hardware device that does not yet exist. We should start development about half a year before the first prototype is available, the project is timeboxed to one year. It is yet unclear what the hardware and the embedded software will be capable of. There're no specified interfaces yet. This will have an impact on the design of the client software.

There're only few things we really can build as long as the interfaces aren't specified in detail.

Sure, you'll probably just say: Write a mock. But this isn't an easy task and a whole mock could turn out to be a whole project by itself.

I'm thinking of writing an adapter layer in which I can plug in any device later on, as long as it has the promised capabilities.

But I'd like to hear advice from you, on how to tackle such projects and avoid possible pitfalls. To what things should we pay special attention? How should we advance on such a project?

Additional information:
We're creating the software project SCRUM-like, but not traditional. We'll make a good deal of upfront design to establish a clear vision, in order to hit the desired targets and minimize the number of iterations.

Also, we have a clear vision on what the hardware/embedded software is supposed to deliver, we just don't know how they're gonna go about it yet and if they can implement the functional requirements at all. We've started a document to collect the interfaces we'd like to have for the client software (because whoever specifies first, wins, g). We also have a rather clear vision of how the client should look like to deliver the best business value, but it is yet unclear if the hardware can support our concepts and it will probably take some time into the project before we know for sure. Yet, we should start development asap.

答:
This sounds like a perfect opportunity to me. I would love a project where it's not only feasible but desirable to the customer to go through many stages of prototype before actually developing.

Everyone at the initial design meeting will have a slightly different vision of the software. You can each help everyone else visualise the product very easily with a paper prototype.

You can discuss the differences, show them to your bosses, your customers, somebody you just dragged in off the street and get a very good idea of what works and what doesn't, until you all come down to a single vision.

Then you can create a physical prototype, in HTML, or whatever other front-end design language you're using. It shouldn't do anything. It should just be a front end, that people can click on, and type in, and move around their three screens. Whatever. Just let people play, and watch them, and learn what works and what doesn't, where the inconsistencies are in your design, where people feel a need to click, etc.

Then and only then will you really know what the simplest possible interface to your hardware should look like. And then you can write your adapter, leaving only the complications of the real interface, when it arrives, to code against.


问:
Why do popular websites store very complicated session-related data in cookies -- and what does it all mean?
As web developers, we all learn that sessions help overcome the problems related to the stateless nature of HTTP. We create a unique session id, and send it to the browser -- and when the browser sends the same id back to us, we identify the user easily.

All this sounds pretty straightforward, and NOT so complicated to implement in any language.

NOW

Look at the following screenshots I took. These show the kinds of cookies popular websites store. It looks like they are storing multiple session IDs, or they are trying the hide the actual id by setting so many cookies, or, it is something very specialized security measures they're taking to prevent session hijacking and other related problems. Or, whatever.

Gmail (before login)

Gmail (after login)

Facebook

StackExchange

(Don't worry, you can't steal my session -- it's stale and incomplete :))

So, my question is -- what purpose does this complicatedness serve? Please explain what these different cookies mean (in general), and for what purposes these are set. Lastly, hint on how I can do it (and whether I should) in my own apps.

One more question: In a lot of instances, the values in the cookies look like they are URL-encoded -- why so?

答:
Sometimes it's not practical to store certain data in a session table on the database.  If certain data gets updated a lot then it could bog down the database a lot as well.  If that's the case and the data's not too important, it might be better stored in a separate cookie.

When (1) happens you have to deal with the 4kb-per-cookie limit, so storing all of the in-cookie session data in one cookie is a bad idea.

It'd be nice to be able to store everything in the minimum number of cookies required, but project complexity concerns make this difficult.  Some applications are developed by several teams.  Sometimes what you see as one web server is actually a proxy that talks to several different clusters of web servers, routing different sections of the site to different clusters, each with their own set of cookies.

Cookies are cheap.  You're the exception to the rule - nobody looks in their cookie store to see how many cookies they have.  And there is little performance benefit to trying to combine all of the client-side session data into as few cookies as possible.


问:
Call stack starts at bottom or top?
A stack is something that piles bottom-up.

Hence a call stack adds new items on the stack when functions are called with items being removed from the stack as each function ends until the stack is empty and then the program ends.

If the above is correct, why do people refer to control moving "up" the call stack?  Surely control moves down the call stack until it reaches the bottom.

答:
There are two possible reasons for this usage:

In the context of exceptions, the control moves to the calling function/method, and this call hierarchy is typically visualized with the main method on top and method calls forming a hierarchy downwards, with a decreasing level of abstractions. In this hierarchy, an exception moves upwards.

The actual program stack in a normal x86 application is inverted, i.e. it grows downwards. The PUSH / PUSHW / PUSHD machine code instructions decrease the stack pointer. Other architectures may share this model.

Title: Call stack starts at bottom or top?


问:
Isolated environment for software stacks
I was sure that I would find this question, but I couldn't. How to create an isolated development environment? It other words, a sandbox, where I can install different combinations of web servers, databases, other software packages and play with them, without cluttering my system packages and without manually downloading packages from official websites. Something for software stacks as virtualenv + pip is for Python.

Before that I tried to install OSes inside QEMU/KVM, but that is an overkill and a bit complicated (couldn't set up bridging, for example). Is it possible to create isolated virtual environments without running virtual machines with full-blown operating systems?

I use Debian GNU/Linux.

Edit (01.08.12): A similar question on Unix.SE with more elaborated answers - https://unix.stackexchange.com/q/31136/11397

答:
To some extent you can achieve your goals under Linux/Unix in chroot'ed environments and jails.

I'd stongly advise to use virtual machines, though.

You might find this table on wikipedia helpful. It seems like OpenVZ might be worth a look. For Windows, sanboxie might also be worth an evaluation.


答: I can recommend "Practical File System Design with the Be File System" (free download from the author's website). It's a great introduction (that goes into quite a lot of detail) to the concepts relevant in filesystems.</p>


问:
What are some things you have read that inspired and guided you as a programmer?
For example, being a beginner, I find a lot of inspiration and direction from reading this post by Bryan Woods.</p>

答:
Not something I read. In 1984 I was talking to my neighbor and his friend. I was 16. The friend told me that essentially the world is split between computer users and computer programmers. He said only the smartest people in the world can become programmers. </p>

<p>Yeeeeee! What a challenge! </p>

Body:
Not something I read. In 1984 I was talking to my neighbor and his friend. I was 16. The friend told me that essentially the world is split between computer users and computer programmers. He said only the smartest people in the world can become programmers.

Yeeeeee! What a challenge!


答:
To call a function such as foo(6, x+1)...

Evaluate the actual parameter expressions, such as the x+1, in the caller's context.

Allocate memory for foo()'s locals by pushing a suitable "local block" of memory onto a runtime "call stack" dedicated to this
purpose. For parameters but not local variables, store the values from
step (1) into the appropriate slot in foo()'s local block.

Store the caller's current address of execution (its "return address") and switch execution to foo().

foo() executes with its local block conveniently available at the end of the call stack.

When foo() is finished, it exits by popping its locals off the stack and "returns" to the caller using the previously stored return
address. Now the caller's locals are on the end of the stack and it
can resume executing.

Reference:

http://cslibrary.stanford.edu/102/PointersAndMemory.pdf (p15)

Title: Call stack starts at bottom or top?

Body:
A stack is something that piles bottom-up.

Hence a call stack adds new items on the stack when functions are called with items being removed from the stack as each function ends until the stack is empty and then the program ends.

If the above is correct, why do people refer to control moving "up" the call stack?  Surely control moves down the call stack until it reaches the bottom.


答:
Scrum is a great choice in a project with such level of uncertainties.

You must work with business & hardware team closely and iterate till you meet the goal.

This means that as new information and precision arrive, your product owner will reorder and put more details in its user stories.

Let's say you planned for v0.1 but as the hardware team work, new specifications are defined. You define a new estimated target.

New requirements & specifications arrive again. New target is defined. You pivot to the new direction, but you don't have to rewrite everything completely.

Requirements & specifications can change very often, but as we advance in the project both business & hardware have clearer view of what they need to do. So do you.

Of course, this project will cost a lot more than if you had more clear requirements at the beginning, but between you and me... it's a pretty rare situation.

Information should come from business because they have the big picture. And information from hardware team is crucial for the technical implementation and interfaces.

Clearly, a great Scrum Product Owner is the key is such unclear and risky project.

Illustrations comes from a really great article called Rockets, Cars & Gardens: Visualizing waterfall, agile and stage gate.

Title: Building software frontend for hardware that does not yet exist

Body:
Our task is to build a software frontend to control a hardware device that does not yet exist. We should start development about half a year before the first prototype is available, the project is timeboxed to one year. It is yet unclear what the hardware and the embedded software will be capable of. There're no specified interfaces yet. This will have an impact on the design of the client software.

There're only few things we really can build as long as the interfaces aren't specified in detail.

Sure, you'll probably just say: Write a mock. But this isn't an easy task and a whole mock could turn out to be a whole project by itself.

I'm thinking of writing an adapter layer in which I can plug in any device later on, as long as it has the promised capabilities.

But I'd like to hear advice from you, on how to tackle such projects and avoid possible pitfalls. To what things should we pay special attention? How should we advance on such a project?

Additional information:
We're creating the software project SCRUM-like, but not traditional. We'll make a good deal of upfront design to establish a clear vision, in order to hit the desired targets and minimize the number of iterations.

Also, we have a clear vision on what the hardware/embedded software is supposed to deliver, we just don't know how they're gonna go about it yet and if they can implement the functional requirements at all. We've started a document to collect the interfaces we'd like to have for the client software (because whoever specifies first, wins, g). We also have a rather clear vision of how the client should look like to deliver the best business value, but it is yet unclear if the hardware can support our concepts and it will probably take some time into the project before we know for sure. Yet, we should start development asap.


答:
It all depends on the definition of words; what exactly do you mean with the words "top" and "bottom" in this context, and also on the implementation of the operating system or computer architecture.

I remember the following from long ago, when I was programming on the Commodore 64. The memory between address $0800 (2048) and $9FFF (40959) was reserved for BASIC programs. The code of your BASIC program was stored starting at the lower address ($0800, growing upwards from there). The stack, for storing variables and return addresses of subroutines, started at the top ($9FFF) of that range and grew towards lower addresses. So in this context it was logical to see the stack as growing downward, and when you return from a subroutine the stack frame of the subroutine was discarded by incrementing the stack pointer, so that you could say you were "moving up the stack" when returning from a subroutine.

I don't know how it works on modern versions of for example Windows or Intel x86 processors. Maybe the stack works the other way around, i.e. it grows from lower to higher addresses. If that would be the case then you'd probably use the words "top", "bottom" and "up", "down" exactly the other way around.

Title: Call stack starts at bottom or top?


答:
References are a way to have multiple variables referencing the same variable container using different names -- so whatever name you're using an operation on that variable will always have an effect on the others.So generally we  avoid References.For details see a link
Do not use PHP references

Title: foreach($items as &$item) considered harmful?

Body:
Is it considered bad practice to pass items in a PHP array by reference instead of by value?

Relevant documentation: http://php.net/manual/en/control-structures.foreach.php


问:
How do I approach this PHP/MYSQL query?
I have a reservation system that I have coded and my final task is to allow the user to enter a set of dates and then show them all available options.

The reservation table is set up with unit_id, check_in and check_out. There are twelve different unit numbers.

So I have to somehow iterate through them and find the units that have no taken dates during the date range the user has put in. Just having some trouble conceptualizing what needs to happen.

Pseudo code

```<code>$units = array (all the units);

foreach($units as $key = $value){

    Run a query searching through all reservations for that unit,
    if the unit is available, add to our $available_units array for the view later

}
</code>```

Checking availability has me stumped too though. What is the most efficient way to use the customers available date range and compare to the date ranges in the db?

答:
I come across this kind of requirement all the time. Let's say you have a date range you want to check (ie, find free units for) which is defined with a start and an end date. In the database, for the sake of making it clear what we're talking about, lets say existing reservations have a first and a last date (of the reservation).

So there's six possible cases for any given reservation (|xxxx|) when compared to the date range you're checking ([----]):

[--|xxxx|--] It can be entirely contained within the check range. This means the date range is not available for this unit.

|xx[xx|----] It can straddle the start of the check range. Again, this unit is not available.

[----|xx]xx| It can straddle the end of the check range. Not available.

|xx[----]xx| It can completely contain the check range. Again, not available.

|xxxx|[----] It can preceed the check range. This unit may be available!

[----]|xxxx| It can entirely follow the check range. This unit may be available!

You'll notice that in all the cases where the unit is not available, the first date is always before* the end date, AND the last date is always after* the start date. This is not true for the two cases where the ranges don't overlap. So! We have a distinguishing condition.

(* or equal to)

In this situation, units that are in use will have at least one existing reservation where first <= 'end' AND last >= 'start'. MySQL has a bunch of really nice grouping and aggregation functions we can use here to actually get MySQL to do all the heavy lifting for us. For instance, we could do something like this:

```<code>SELECT Units.*, SUM(IF(ReservationID IS NULL, 0, First <= 'end' AND Last >= 'start')) AS ConflictingReservations
FROM Units LEFT JOIN Reservations USING (UnitID)
GROUP BY UnitID
HAVING ConflictingReservations = 0
</code>```

In the above query (which I admit I haven't actually tested cough, but the idea is sound, honest!) we left join the Units table to the Reservations table so that if a unit has never been reserved it'll still show in the results. From there, we group the rows by the UnitID and create a derived column called "ConflictingReservations" which contains the sum of a logical comparison. As mentioned previously, if that logical comparison is TRUE (which in MySQL has the value '1') it means the specific reservation we're checking overlaps the check range. Summing all the "hits" will give us the number of reservations for a particular unit that overlap with the check range.

Note: we guard the logical comparison against the case where a unit has NEVER been booked before and therefore the left join returns a row with a NULL values in the First and Last columns. If the ReservationID is NULL (and therefore the First and Last dates would be null, forcing the entire ConflictingReservations derived column to NULL rather than 0) we count that as a non-match/false, ie: 0.

The final step says "throw away all rows (ie, units) that have a nonzero number of conflicting reservations." This should leave you with a list of rows from the Units table that have no reservations within the check range (ie, that are available to book in that range.)

If I've managed to get edge cases wrong (as I did with the zero reservations case) or syntax wrong etc, let me know and I'll edit the answer!

Title: How do I approach this PHP/MYSQL query?

Body:
I come across this kind of requirement all the time. Let's say you have a date range you want to check (ie, find free units for) which is defined with a start and an end date. In the database, for the sake of making it clear what we're talking about, lets say existing reservations have a first and a last date (of the reservation).

So there's six possible cases for any given reservation (|xxxx|) when compared to the date range you're checking ([----]):

[--|xxxx|--] It can be entirely contained within the check range. This means the date range is not available for this unit.

|xx[xx|----] It can straddle the start of the check range. Again, this unit is not available.

[----|xx]xx| It can straddle the end of the check range. Not available.

|xx[----]xx| It can completely contain the check range. Again, not available.

|xxxx|[----] It can preceed the check range. This unit may be available!

[----]|xxxx| It can entirely follow the check range. This unit may be available!

You'll notice that in all the cases where the unit is not available, the first date is always before* the end date, AND the last date is always after* the start date. This is not true for the two cases where the ranges don't overlap. So! We have a distinguishing condition.

(* or equal to)

In this situation, units that are in use will have at least one existing reservation where first <= 'end' AND last >= 'start'. MySQL has a bunch of really nice grouping and aggregation functions we can use here to actually get MySQL to do all the heavy lifting for us. For instance, we could do something like this:

SELECT Units.*, SUM(IF(ReservationID IS NULL, 0, First <= 'end' AND Last >= 'start')) AS ConflictingReservations
FROM Units LEFT JOIN Reservations USING (UnitID)
GROUP BY UnitID
HAVING ConflictingReservations = 0

In the above query (which I admit I haven't actually tested cough, but the idea is sound, honest!) we left join the Units table to the Reservations table so that if a unit has never been reserved it'll still show in the results. From there, we group the rows by the UnitID and create a derived column called "ConflictingReservations" which contains the sum of a logical comparison. As mentioned previously, if that logical comparison is TRUE (which in MySQL has the value '1') it means the specific reservation we're checking overlaps the check range. Summing all the "hits" will give us the number of reservations for a particular unit that overlap with the check range.

Note: we guard the logical comparison against the case where a unit has NEVER been booked before and therefore the left join returns a row with a NULL values in the First and Last columns. If the ReservationID is NULL (and therefore the First and Last dates would be null, forcing the entire ConflictingReservations derived column to NULL rather than 0) we count that as a non-match/false, ie: 0.

The final step says "throw away all rows (ie, units) that have a nonzero number of conflicting reservations." This should leave you with a list of rows from the Units table that have no reservations within the check range (ie, that are available to book in that range.)

If I've managed to get edge cases wrong (as I did with the zero reservations case) or syntax wrong etc, let me know and I'll edit the answer!


问:
What is an effective way to obtain use case information from preoccupied professionals?
When designing a novel system in a hospital or other clinical setting, I think it would be important to gather information from doctors, nurses, pharmacists, technicians, et al on use cases for the system.

Aside from the fact that all of these are also busy professionals, what is a good way to get them to share information pertinent to their (possibly chaotic and time sensitive) workflow that would be usable in a software engineering design?

答:
The only way to reverse-engineer what medical professionals are doing is to observe first hand, while they are working. Find and follow willing, chatty professionals like white on rice, and ask them to narrate the work and thought process. If they need prodding, ask questions that drive towards rules, like: What are you thinking? Why did you choose this over that? What are you looking for? What does that rule out?

As you learn, share your mental model, and ask questions that shape & refine the rules. Be the journal of their work.

Anything short of active, hands-on, in-person, respectful observation and feedback is guessing .. not engineering.

OwnerDisplayName: jonsca

OwnerDisplayName: David P. Hochman


答:
Invest in domain specific languages. From a software development point of view, clinical software is tricky, because you can develop software in the right way, but you'll have a hard time developing the right software. Even the brightest business analyst or software developer has a hard time communicating with clinicians. </p>

<p>In health IT domain specific languages (mostly) correspond to electronic healthcare record related standards. Check out http://www.openehr.org If the clinicians can express their requirements using a computable language that describes the domain, you'll find it much easier to turn their requirements into software. To see an example of the approach I'm talking about, you can take a look at our open source project: http://opereffa.chime.ucl.ac.uk </p>

Title: What is an effective way to obtain use case information from preoccupied professionals?

Body:
When designing a novel system in a hospital or other clinical setting, I think it would be important to gather information from doctors, nurses, pharmacists, technicians, et al on use cases for the system.

Aside from the fact that all of these are also busy professionals, what is a good way to get them to share information pertinent to their (possibly chaotic and time sensitive) workflow that would be usable in a software engineering design?

Body:
Invest in domain specific languages. From a software development point of view, clinical software is tricky, because you can develop software in the right way, but you'll have a hard time developing the right software. Even the brightest business analyst or software developer has a hard time communicating with clinicians.

In health IT domain specific languages (mostly) correspond to electronic healthcare record related standards. Check out http://www.openehr.org If the clinicians can express their requirements using a computable language that describes the domain, you'll find it much easier to turn their requirements into software. To see an example of the approach I'm talking about, you can take a look at our open source project: http://opereffa.chime.ucl.ac.uk

OwnerDisplayName: sarikan


答:
Another openEHR-related answer, from a slightly different perspective...

The openEHR approach has been having considerable success in standardising clinical data requirements - ie clinician-defined EHR content specifications; clinicians and stakeholder expert collaboration to ensure the models are valid, safe and correct; and governing/distributing them for use in clinical EHR systems. This standardised approach certainly helps to simplify capturing the workflow process, including documentation of orders and corresponding activities, thus enabling the tracking of workflow. The openEHR Clinical Knowledge Manager is acting as the hub for this activity - www.openEHR.org/Knowledge.

Some jurisdictions are starting to use this approach as well for creation of national eHealth standards - http://www.nehta.gov.au/connecting-australia/terminology-and-information/detailed-clinical-models and http://dcm.nehta.org.au/ckm/

Some professional clinical colleges are also starting to think along these lines too, to ensure that the software being built by vendors is safe and fit for clinical use - I'm aware of early activity happening in Australia, UK and US.

OwnerDisplayName: Heather Leslie


答:
Jumping off of David's post: Are you looking to develop a general EHR product or do you have a specific client site installation? We are developing an in-house Oncology EHR system and I don't even know where this project would be without being in the office everyday. If you really want to learn a practice, you really need lots of face-time and time where they point on your screen the way it should be different.

The further away from your customers you are, the slower the communication and the more you are programming to what you think they want, and not what they want.

And remember: most features will affect multiple people, nurses, admins, etc. so it is key to have input from everyone and make informed decisions.

OwnerDisplayName: Nick


问:
Database architecture decisions
I do not know too many things about databases, so a lot of questions concerning architecture came up lately. Two of these things are:

If I have a table with a lot of entries(millions probably), how can I make the select queries faster? I thought about sorting the table alphabetically and then splitting it in two, but that doesn't seem to make things easier for me. Do you have any suggestions?

I have a table user and one message. In message I should have sender_id and receiver_id. From what I know, I can't make them both foreign keys for user, so I have to pick one of them. Doesn't this, however, lead to data inconsistency(which, as far as I know, is bad)? What is the right approach here?

I do not think that it matters, but I use MySQL 5.5.

答:
If I have a table with a lot of entries(millions probably), how can I make the select queries faster?

You need to add indexes to the table - how this is done is different from database to database, so you need to consult you MySQL 5.5 documentation. As for how to choose a good index - books were written on this subject... Here is one introduction to the topic.

I have a table user and one message. In message I should have sender_id and receiver_id. From what I know, I can't make them both foreign keys for user

You know wrong. You certainly can make each of them a foreign key to user, independently.

Title: Database architecture decisions


答:
Add indexes on the columns used in the WHERE clauses. In an extreme case, use sharding, but that's if you're talking about billions of rows (at least), not millions. Additionally, for complex queries with multiple joins and subqueries, the structure of the query can make a big difference. The query optimizer of the DB should take care of that, but sometimes it messes up or needs more information (e.g. updated DB statistics). You really need an experienced DBA at that point. And of course the structure of the DB also has a big effect. Sometimes you have to denormalize, or just fix a fundamentally broken schema.

You most definitely can have foreign key constraints on multiple columns of a table that all refer to the same column in a different table. So do that.

Title: Database architecture decisions

Body:
I do not know too many things about databases, so a lot of questions concerning architecture came up lately. Two of these things are:

If I have a table with a lot of entries(millions probably), how can I make the select queries faster? I thought about sorting the table alphabetically and then splitting it in two, but that doesn't seem to make things easier for me. Do you have any suggestions?

I have a table user and one message. In message I should have sender_id and receiver_id. From what I know, I can't make them both foreign keys for user, so I have to pick one of them. Doesn't this, however, lead to data inconsistency(which, as far as I know, is bad)? What is the right approach here?

I do not think that it matters, but I use MySQL 5.5.


答:
If I have a table with a lot of entries(millions probably), how can I make the select queries faster? I thought about sorting the table alphabetically and then splitting it in two, but that doesn't seem to make things easier for me. Do you have any suggestions?

That's a HUGE open-ended question. First, remember this:

You should never optimize unless you are actually seeing performance problems. Are you seeing performance problems?

If you are, then you should further investigate to determine what those performance problems are. What's your query look like? Does it utilize any indexes? If you use EXPLAIN it'll tell you what sort of indexes are being used, and help explain what's slow/poorly optimized about the query.

I have a table user and one message. In message I should have sender_id and receiver_id. From what I know, I can't make them both foreign keys for user, so I have to pick one of them.

Why can't you? MySQL WILL let you do this- the RIGHT approach is to make them both foreign keys.

Title: Database architecture decisions


答:
Sure, you'll probably just say: Write a mock. But this isn't an easy task and a
  whole mock could turn out to be a whole project by itself.

You should do it either - develop some kind of software simulator which replaces the real hardware. You don't have to build this upfront - in each iteration, develop your simulator just to the degree you need it to test the functions you have in your frontend so far. Heck, I can't think of an effective way of iterative development, prototyping / showing functions to the user if you don't have some kind of "backend" behind it - your users have to see / work with your software if you want to get feedback, and if you don't have the hardware and no simulator too, how should they?

A simulator will also be a useful tool for testing you frontend even when you have the hardware available, because you can provide special testing functionality in it your real hardware does not provide. I don't know you hardware, but when it is just beeing constructed now, I suspect it is not so cheap that every of your team member gets an individual piece of it? Then a simulator will also pay for itself.


答:
You may find Environment Modules worth looking at.

We use it extensively to give users access to a wide variety of applications, without those apps being on their command line path unless they need them to be.

We also use it to give people the option of which version of software they want to use. For instance, we have various versions of python available:

```<code>$ python -V
Python 2.4.3
$ module avail python

----------------------- /apps/Modules/modulefiles -----------------------
python/2.6                   python/2.7.2-64
python/2.6.2                 python/3.2
python/2.6.6(default)        python/3.2.0
python/2.7                   python/3.2.2
python/2.7.1                 python/2.7.2
$ module load python
$ python -V
Python 2.6.6
$ module load python/2.7.1
$ python -V
Python 2.7.1
$
</code>```

It only really modifies environment variables, but it may be just the lightweight context switch that you need.

Title: Isolated environment for software stacks

Body:
I was sure that I would find this question, but I couldn't. How to create an isolated development environment? It other words, a sandbox, where I can install different combinations of web servers, databases, other software packages and play with them, without cluttering my system packages and without manually downloading packages from official websites. Something for software stacks as virtualenv + pip is for Python.

Before that I tried to install OSes inside QEMU/KVM, but that is an overkill and a bit complicated (couldn't set up bridging, for example). Is it possible to create isolated virtual environments without running virtual machines with full-blown operating systems?

I use Debian GNU/Linux.

Edit (01.08.12): A similar question on Unix.SE with more elaborated answers - https://unix.stackexchange.com/q/31136/11397

Body:
You may find Environment Modules worth looking at.

We use it extensively to give users access to a wide variety of applications, without those apps being on their command line path unless they need them to be.

We also use it to give people the option of which version of software they want to use. For instance, we have various versions of python available:

$ python -V
Python 2.4.3
$ module avail python

----------------------- /apps/Modules/modulefiles -----------------------
python/2.6                   python/2.7.2-64
python/2.6.2                 python/3.2
python/2.6.6(default)        python/3.2.0
python/2.7                   python/3.2.2
python/2.7.1                 python/2.7.2
$ module load python
$ python -V
Python 2.6.6
$ module load python/2.7.1
$ python -V
Python 2.7.1
$

It only really modifies environment variables, but it may be just the lightweight context switch that you need.


问:
Event driven design and separation of core/UI logic
I am new to event driven development, and I feel lost when I try to implement events that should pass the core/UI boundary.

In my program I have the following (example in c#):

```<code>UI.RuleForm   Core.RuleList     UI.ResultForm
Cell 1        Rule 1
Cell 2        Rule 2
Cell 3        Rule 3
</code>```

What I want is:
when a RuleForm cell changes, it will update the corresponding rule in RuleList. And when the RuleList changes, the resultFrom will be recalculated from the rules.

My current thought is that, in order to keep core logic separated from UI logic (i.e. core should know nothing about UI), core should then only generate events, but not processing events generated by others.

So I have to create some kind of UI.RuleListWrapper which can process RuleForm change events, updating Core.RuleList. RuleList in term should fire OnChange events that UI.ResultForm can use.

So in summary, my questions are:

I want to know if my reasoning and purposed implementation is okay or not, which probably means: should a core module be able to process events generated by outside UI
Is my separation some kind of "mysophobia", or has it been done before.
Are there other better approaches?

答: By core do you mean business layer, presentation layer, etc? By presentation I don't mean UI. Depending on the pattern, the UI might raise events to the presentation layer who would then interface with the business layer. Typically your middle tier will respond to UI events and communicate back with the view via a viewmodel or through an interface (once again depending on the pattern). If it is truly a core model in the utility sense, then I would say have a middle layer handle the events being raised and then call into the core model and back into the view.</p>


答: You're probably going to have to read in raw blocks and parse them yourself.  The docs you linked to give the header layouts, you need to read and parse those and then use that information to get the content.  I'd recommend a language like C that lets you use pointers and map data structures directly over byte buffers, but there's no reason you couldn't read a byte stream and build up your metadata from that, if your language of choice doesn't give you control over data structure layout.  I'd also recommend dumping blocks out in hex and hand-parsing them to start -- it's often useful to know what the actual bytes are so you can quickly spot endian or type boundary issues.</p>


问:
Displaying the number of registered users to a website
I am building an online game much like Mafia Wars. I was thinking of displaying the number of registered users on the home page, but then I thought:

"What if a new user sees that the total number of registered users
  is 5?!" :D

That would be too embarrassing to my still growing game. Then another thought came by:

"Why don't I display 500 instead?!" ;)

Nobody would know if there is actually 500 users registered to my website unless of course they start actually playing and interacting with other players.

However, I just have a feeling that this isn't ethical. What if this generates a negative publicity to my website?!!

What's your opinion should I fake the number? or should I honestly display it? or should I wait till I have 1000 real account then start displaying the number?

And most importantly is this legal?! :D

答: I think numbers will not affect your game site if its worth playing it in my sense so my opinion is to show the real numbers</p>


答:
The problem with the data structures you mentioned is, that they have a different semantic. I.e. they work in a different way and cannot have a common interface. E.g. a list could have an Add method that would add an element to the list. An array, however, cannot have such a method, since the length of the array fixed. An array allows you access elements randomly through an indexer (a[i] = x; print(a[k]);), but you cannot do that with a linked list. In a linked list you can only access the next or previous element starting at the list header or tail. An unsorted collection would probably have a Sort method, a inherently sorted collection (e.g. a binary tree sorted by a given key) of course not, as the elements get sorted at insertion.

One single interface won't work. You need a set of interfaces, each of them exposing specific aspects of collections.

If you want to compare different collection types, I suggest you to create wrappers using a common interface. You would then perform the tests on the wrappers.

Such a wrapper interface could look like this

public interface ICollectionTestWrapper<T>
{
    void FillWithTestData(IEnumerable<T> data);

    bool IsSortable { get; }
    void Sort();

    void FindElement(T element); // No return value needed for speed tests.

    // TODO: Add functionality for the tests you have in mind
    ...
}
```

Title: Standardized Data Structure Interface

Body:
I want to work with a variety of data structures (arrays, singly/doubly linked lists, sorted structures, etc.) on a plug-and-play basis. For example, I want to be able to easily swap in and out the sorted list and the array (which is re-sorted after every insertion/deletion), to test which one has better performance.

In every language I know of, the public interface to such data structures is very inconsistent. So changing the implementation requires a lot of work.

Is there a language, or a language-agnostic design pattern, that makes it easy to define a generic public interface that works with any data structure?

Of course, I understand how different performance would be (e.g., binary search in a sorted array vs linear search in a regular array). All I ask is that the performance of the data structure doesn't degrade asymptotically or by a large constant factor due to the use of the standard interface.

Example problem: iterate through the container of numbers, find the two numbers that are nearest  each other, and remove the pair of them. (If several pairs are equal distance, pick an arbitrary pair).

I'd like the code to solve the problem to be unchanged as I switch in and out different containers.


答: It sounds like you're looking for the strategy pattern.  What you'll do is define your own interface (like find-nearest-neighbors(NumericCollection) or find-closest-value(NumericCollection, Integer)) and provide several implementations of it.  Details of calling the various implementations are necessarily language-specific, but as long as each supports the defined interface, you should have a good basis for comparison.

Title: Standardized Data Structure Interface


答: Write the software first, then drive the hardware guys to design around what you have delivered. This is how Google changed the smartphone market 180 degrees. They delivered an OS and forced the hardware vendors to design to it. Instead of the way they were doing it before; creating lots of hardware that caused lots of different incompatible one off software to have to be written.</p>