问:
Why are interfaces useful?
I have been studying and coding in C# for some time now. But still, I can't figure the usefulness of Interfaces. They bring too little to the table. Other than providing the signatures of function, they do nothing. If I can remember the names and signature of the functions which are needed to be implemented, there is no need for them. They are there just to make sure that the said functions(in the interface) are implemented in the inheriting class.

C# is a great language, but sometimes it gives you the feeling that first Microsoft creates the problem (not allowing multiple inheritance) and then provides the solution, which is rather a tedious one.

That's my understanding which is based on limited coding experience. What's your take on interfaces? How often you make uses of them and what makes you do so?

答: As a young programmer/developer, just learning C# you might not see the usefulness of interface, because you might write your codes using your classes and the code works fine, but in real life scenario, building a scalable, robust and maintainable application involves using some architectural and patterns, which can only be made possible by using interface, example is in dependency injection.</p>

Title: Why are interfaces useful?


问:
can a team lead's work ever be done working remotely
I am currently a software developer. As a next milestone in my career I am thinking I must establish myself as team leader which leads a small development team in order to meet deliverables, deployments, etc. I've read various post in this site itself which discusses the pros and cons of working from home. But I am not sure if this aspect was quite discussed/debated. I haven't stepped into the team leading role per se, but I would like to know the responsibilities that come with the title, and if (and how) that can be managed while working from home.</p>

答: It depends on the specifics of your job and if the people you will interact with will be comfortable dealing with you remotely.  Also, on how the team functions.  Do you pair program, do the others in the team have to be in the office, etc.  There are certain development styles that will not work well with working remotely.  </p>

ContentLicense: CC BY-SA 3.0


答:
There is no substitute for personal interaction.

I'm not saying that working as a lead doesn't work (just look at many of the popular OS packages available), I'm simply saying that it doesn't work as well as working with the people on your team in person.

As a team lead, part of your job is to mentor those under you. While e-mail, messaging and phone conversations solve the remote communication problem, there's so much missed in body language when you have those conversations. A strong leader knows his or her team and understands their strengths and weaknesses. Being able to read people and see how they react in certain situations (and understanding why) is key to being an effective leader. This simply can't be done remotely.

Also, working in person with the rest of the team fosters a much better relationship with the rest of your developers, therefore making you much more approachable than just being some guy who they never see.

Title: can a team lead's work ever be done working remotely

Body: I am currently a software developer. As a next milestone in my career I am thinking I must establish myself as team leader which leads a small development team in order to meet deliverables, deployments, etc. I've read various post in this site itself which discusses the pros and cons of working from home. But I am not sure if this aspect was quite discussed/debated. I haven't stepped into the team leading role per se, but I would like to know the responsibilities that come with the title, and if (and how) that can be managed while working from home.</p>


问:
how to start with ASP.NET MVC 3 razor
I going to start a new project in my company in which are involved the use of MVC 3, now this is the first time that we are using this framework, and i have a couple of questions,

It's recommended to start with MVC3 and not from the MVC2?

Any Tutorials in specific that you can recommend?

This framework have full integration with jQuery?

答:
I highly recommended using Razor, however keep in mind that there are other view engines available too.

Definitive go to MS ASP.NET MVC site, Pluralsight and the MVC team blogs

Yes, it's totally integrated with jQuery

Title: how to start with ASP.NET MVC 3 razor


答:
We have at least two team leads who are remote. It's harder to get the job if you are remote but if you are already a team lead and want to go remote, it seems a bit easier. Our 2 had to move to other parts of the country, one was already a lead and his success allowed the other to get considered for a lead position she would have had years earlier if she had not been remote. </p>

<p>You have to be very willing to be available and prove that the remoteness will
not affect your ability to mentor, to assign work, to review work, to check progress etc. I suspect that with everything that is new to an organization you might have to be that little bit better than your non-remote teammates to get selected or to be allowed to do this. Certainly we have never been willing to let anyone except our exceptional employees go remote. </p>

Body:
We have at least two team leads who are remote. It's harder to get the job if you are remote but if you are already a team lead and want to go remote, it seems a bit easier. Our 2 had to move to other parts of the country, one was already a lead and his success allowed the other to get considered for a lead position she would have had years earlier if she had not been remote.

You have to be very willing to be available and prove that the remoteness will
not affect your ability to mentor, to assign work, to review work, to check progress etc. I suspect that with everything that is new to an organization you might have to be that little bit better than your non-remote teammates to get selected or to be allowed to do this. Certainly we have never been willing to let anyone except our exceptional employees go remote.


问:
Should you document everything or just most?
It seems a bit of a controversial subject to document everything, including the "JavaBean" syntax of getters and setters for fields: People say its needlessly long and repetitive breaking DRY (don't repeat yourself), that the naming convention should explain everything, and it clutters code/documentation. Sometimes those arguments work. But other times, you end up with this:

Above is common to open source projects that do boldly follow those principles. You're left with entirely useless documentation. That doesn't explain anything about what's going on underneath, the possible effects, or even what the expected value is (could it be null or never null? I don't know; the Javadoc doesn't tell me).

So when should I document? Do I document everything even if it occasionally clutters code? Or do I document nothing since in my eyes it's "obvious"?

答: My suggestion on documentation is that if there is anything fancy in the code, that belongs in a document that should be kept up to date.  Fancy being subject to much interpretation, in my mind is where something is done in a particular fashion that may have side effects that should be noted,e.g. something may be done to recursively to ensure that grandchild items are processed in going through a tree of nodes to perform some test on all the descendants.  Articulating why something was done a particular way can be a good way to know whether or not there was a good reason to use something.</p>

ContentLicense: CC BY-SA 2.5


问:
WinRT for 'desktop' apps
So I'm reading up about the differences between the old and the new APIs, and I can't find whether the new WinRT API will provide for desktop apps, so far it seems its only available to write Metro apps - ie the full screen, 'phone style' apps.

Does this mean that I can write a WinRT-based app and it will be invoked from a desktop but display like other Metro apps (eg IE) and I won't be able to write an old-style desktop app that runs in the old-style desktop?

What does this mean for server side apps? I imagine WinRT would be the API of choice for servers, now that Windows Server comes with an optional GUI, so I imagine that if I can write a windows-less app using WinRT for the server, I should be able to write the same for the client too? (and if that's the case, surely I could connect a non-Metro UI to it)

答:
So I'm reading up about the differences between the old and the new
  APIs, and I can't find whether the new WinRT API will provide for
  desktop apps, so far it seems its only available to write Metro apps -
  ie the full screen, 'phone style' apps.

All WinRT more or less is a modified library similar to Win32.

WinRT is the Metro version of Win32, they took a great deal of complexity away from it, and made it easy to work with.

Does this mean that I can write a WinRT-based app and it will be
  invoked from a desktop but display like other Metro apps (eg IE) and I
  won't be able to write an old-style desktop app that runs in the
  old-style desktop?

In theory you would write a desktop application and add WinRT code in order to display a Metro style UI when the user requested it.

What does this mean for server side apps? I imagine WinRT would be the
  API of choice for servers, now that Windows Server comes with an
  optional GUI, so I imagine that if I can write a windows-less app
  using WinRT for the server, I should be able to write the same for the
  client too? (and if that's the case, surely I could connect a
  non-Metro UI to it)

Don't guess...Power Shell( the name escapes me if this is wrong ) is still king in Windows Server

Title: WinRT for 'desktop' apps


问:
Application development practices in a non-software company
Let me preface this by apologizing if this has been covered elsewhere, but I was unable to find sufficient information in my searching.

Background information

I work at a non-software company developing desktop applications in a team of four members, where I have been employed for more than 18 months. Being my first job out of college, I was unsure what kind of development environment to expect. What I found was transition of applications developed in a legacy language being rewritten in an OO language.

The problem

On our development, we have no guidance, practices, etc for how we plan (if any planning is done at all), code, and deploy our applications. As you can imagine, such a policy (or lack there-of) has bred pure chaos for managing applications, with often times each developer creating his/her own way of handling situations. In other words, LOTS of repetition. I have come to understand why I am the third new hire they have had in three years (other two left after one year).

Allow me to list off other issues:

No unit testing. The only type of "testing" that is done is handing
it over to the user and asking if this is what they wanted. Half the
time the user doesn't have time to check it and just starts using it
to produce their analysis data.

No database. We have several, several thousands of records and
information that are stored over a multitude of CSV files.

No documentation. This speaks for itself. No user requirements.
No explanation for how to make the program work. Nothing.

No bug tracking at all.

No version control.

Overwhelming number of applications. We have well over 150+ small applications. Many of which no one knows what they even do or how they work. The development policy is each time the user needs a new report, create a brand new program with just one button. Push they button and magically on a network share in a folder, several reports in notepad are created.

And many more issues...

So... what are you asking us?

Ah, now we get to the real reason for all of this. I have been tasked by my manager to propose suggestions and guidelines for our development team. Mind you, I am not optimistic that anyone will actually heed them, but I have to propose them none-the-less. Most of the suggestions I have found are for software companies. I have been informed by other people at my company that such practices would be "overkill" at our work.

What are some practices that application developers at a non-IT company follow?

Thank you for your time.

答:
Going with the "choose your battles" notion...

The biggest bang for your buck/effort would be a version control system. Talk it up, get people on your side, then convince management to provide a week-long training workshop for developers on how to use the tool. If you can pull this off, you'll be able to create a modicum of order in chaos.

If you're still working there when it finally gets used, you can then tackle introducing databases and the other stuff. But honestly, I think that if you want to develop as a software engineer that environment will slow you down.

Do some good, help them out, but use it as a stepping stone into a better job.


问:
Product Owner introduces ungroomed (unfamiliar, not estimated) User Story(s) into the Sprint Planning meeting
A problem that I am facing and would like some input into is; a Product Owner introduces ungroomed (unfamiliar, not estimated) User Story(s) into the Sprint Planning meeting.

The issue that this has caused is the team rushes to understand and estimate the User Story(s), which puts significant time pressure on the commitment portion of the Sprint Planning meeting. The team also seems to be unsure of their estimate due to the rushed nature of grooming them in the Sprint Planning meeting. The end result of this is a rushed, half hearted Sprint commitment, which is usually an under-commitment due to so much uncertainty.

I have seen two distinctly different causes for the late introduction of the User Story(s):

The team is new to Scrum and has been having difficulties grooming
stories, prior to planning.

A brand new high priority User Story has
appeared just before the Sprint Planning meeting.

I have discussed these issues with the Product Owner and we have decided upon on actions, I am wondering what have you tried when the Product Owner introduces a brand new high priority User Story just before or even in the Sprint Planning meeting? What worked, what failed?

The team that is having difficulty grooming User Stories in new to Scrum; hence I suspect that some facilitation of their grooming sessions, mentoring and some time will help them.

Do you have any other suggestions for helping teams come up prompt Planning Poker estimates?

答:
Given they've just been given the story during planning (regardless of it being a new story or not having estimated it earlier) you have a few choices.

First, you can ask the product owner if the story can be left on the backlog so that it can be properly checked and estimated during this sprint, and take it next sprint.

If that's not possible, another option is to do it in a spike - a time-boxed story to investigate something or to try something out - and then you'll have a head start for doing it next sprint.

Finally, if you really must start on it this sprint, then try and find out as much as you can during planning, and take the story but make it clear of the risk that it may not be completed this sprint. If it doesn't get completed, so be it, you've done some of it and now know a lot more about it for the next sprint.

Remember, estimates are just the best estimates with the information you have at the time, and can be revised later when you have more info.

Also remember you should be spending about 5%-10% of the sprint in refining the backlog. Doing this really helps keep the planning meetings shorter, more focused and more productive.

OwnerDisplayName: Andrew Rusling


答:
Document everything that makes sense to document.

In an ideal world, yes, you would document everything.   However, on Earth, we have deadlines, feature cuts, families and friends to visit, vacations to take, only 24 hours in a day and only 365 days in a year.  There's just not enough time to document everything.  So, optimally, document everything you can (you won't get done), but get the most bang for your buck by:

Make code readable and method signatures as obvious as possible so that documenting is less likely to be needed.

Documenting the most obscure things first.  Help out others by documenting the crazy hacks you had to do to get things out the door.

Document the why before the what - Don't comment what something does, like "Iterate over the customer records where balance is less than zero and rating is less than one and add them to the exemptCustomers list".  Document why you are adding them to the list in plain english (or your team's language), like "Since these customers have a negative balance and low ratings, they are causing us to lose money, so exclude them from being able to check out.

Title: Should you document everything or just most?

Body:
It seems a bit of a controversial subject to document everything, including the "JavaBean" syntax of getters and setters for fields: People say its needlessly long and repetitive breaking DRY (don't repeat yourself), that the naming convention should explain everything, and it clutters code/documentation. Sometimes those arguments work. But other times, you end up with this:

Above is common to open source projects that do boldly follow those principles. You're left with entirely useless documentation. That doesn't explain anything about what's going on underneath, the possible effects, or even what the expected value is (could it be null or never null? I don't know; the Javadoc doesn't tell me).

So when should I document? Do I document everything even if it occasionally clutters code? Or do I document nothing since in my eyes it's "obvious"?


答:
I have been tasked by my manager to propose suggestions and guidelines
  for our development team. Mind you, I am not optimistic that anyone
  will actually heed them, but I have to propose them none-the-less.
  Most of the suggestions I have found are for software companies. I
  have been informed by other people at my company that such practices
  would be "overkill" at our work.

I'd be tempted to want to have a few different documents to handle this:

End state - What are the things you'd like to see being used that aren't right now.  This is where you get to have whatever grand dreams you'd want of the state at the end of the rainbow.

Transition plans -  Prioritize the list of which practices in terms of which are likely to yield the best changes and write out a plan to bring these in slowly over a long time period.  This is kind of like the road map to get to that end state and is a separate document because it is where you may have some fights in terms of how to get this done.

Hazards of current state - This is where you explain why the transitions should be done to get out of the current chaotic mess you have now.

Title: Application development practices in a non-software company

Body:
Let me preface this by apologizing if this has been covered elsewhere, but I was unable to find sufficient information in my searching.

Background information

I work at a non-software company developing desktop applications in a team of four members, where I have been employed for more than 18 months. Being my first job out of college, I was unsure what kind of development environment to expect. What I found was transition of applications developed in a legacy language being rewritten in an OO language.

The problem

On our development, we have no guidance, practices, etc for how we plan (if any planning is done at all), code, and deploy our applications. As you can imagine, such a policy (or lack there-of) has bred pure chaos for managing applications, with often times each developer creating his/her own way of handling situations. In other words, LOTS of repetition. I have come to understand why I am the third new hire they have had in three years (other two left after one year).

Allow me to list off other issues:

No unit testing. The only type of "testing" that is done is handing
it over to the user and asking if this is what they wanted. Half the
time the user doesn't have time to check it and just starts using it
to produce their analysis data.

No database. We have several, several thousands of records and
information that are stored over a multitude of CSV files.

No documentation. This speaks for itself. No user requirements.
No explanation for how to make the program work. Nothing.

No bug tracking at all.

No version control.

Overwhelming number of applications. We have well over 150+ small applications. Many of which no one knows what they even do or how they work. The development policy is each time the user needs a new report, create a brand new program with just one button. Push they button and magically on a network share in a folder, several reports in notepad are created.

And many more issues...

So... what are you asking us?

Ah, now we get to the real reason for all of this. I have been tasked by my manager to propose suggestions and guidelines for our development team. Mind you, I am not optimistic that anyone will actually heed them, but I have to propose them none-the-less. Most of the suggestions I have found are for software companies. I have been informed by other people at my company that such practices would be "overkill" at our work.

What are some practices that application developers at a non-IT company follow?

Thank you for your time.


答:
Estimation is not something that should be done off-the-cuff or quickly and without thought, regardless of your methodology. I wouldn't expect the team to be able to quickly come up with a reasonable estimate without having an opportunity to look at the user story and think about it for a while. At the same time, I do recognize that there are high priority new features, bug fixes, or feature modifications that need to be addressed quickly so that the customer can see added value. It's a trade-off between delivering the feature now versus maintaining schedule and/or quality that needs to be discussed.

If this is a continual thing, that suggests a problem with the process. That doesn't mean the process doesn't work, but rather (in this case) the Product Owner doesn't understand the value in the process. The Product Owner and the rest of the Scrum team need to be able to work together. Continuously sacrificing schedule or quality doesn't bode well for the project. At some point, I believe that the ScrumMaster, on behalf of the team, needs to draw a line and simply put the story in the backlog for estimation at a later time and not include it in the sprint. It will just be at the top of the backlog for the next one. This will reduce stress and allow for greater quality.

Body:
A problem that I am facing and would like some input into is; a Product Owner introduces ungroomed (unfamiliar, not estimated) User Story(s) into the Sprint Planning meeting.

The issue that this has caused is the team rushes to understand and estimate the User Story(s), which puts significant time pressure on the commitment portion of the Sprint Planning meeting. The team also seems to be unsure of their estimate due to the rushed nature of grooming them in the Sprint Planning meeting. The end result of this is a rushed, half hearted Sprint commitment, which is usually an under-commitment due to so much uncertainty.

I have seen two distinctly different causes for the late introduction of the User Story(s):

The team is new to Scrum and has been having difficulties grooming
stories, prior to planning.

A brand new high priority User Story has
appeared just before the Sprint Planning meeting.

I have discussed these issues with the Product Owner and we have decided upon on actions, I am wondering what have you tried when the Product Owner introduces a brand new high priority User Story just before or even in the Sprint Planning meeting? What worked, what failed?

The team that is having difficulty grooming User Stories in new to Scrum; hence I suspect that some facilitation of their grooming sessions, mentoring and some time will help them.

Do you have any other suggestions for helping teams come up prompt Planning Poker estimates?


问:
What makes a piece of software an Engine?
The descriptor 'Engine' gets thrown around a lot: graphics engine, RegEx engine, AI engine, etc. but what actually makes a piece of software an Engine? Design, Input/Output, Purpose, Size?</p>

答:
An engine would be something that is "under the hood", so to speak. It is not, or at least very rarely is, visible to the end user. A graphics engine, for instance, drives all the rendering calculations but passes those changes on to the actual environment to be modeled. Input: math. Output: pretty colors. An engine might also have very different working variables than a more high level interface. For instance, in the previous example, it is using raw numerical data to manipulate graphics without worrying about whether something is a shadow or a texture, all of that is abstracted into the equations and matrix operations to be performed by that engine. Think of the Engine as the "Kernel" of a given system while the rest would be more like the "Shell".

To use a real world, CS101 analogy, an engine is just like a car engine. It takes two inputs, air and gas. It then passes them into a chamber, whereupon electricity is used to generate the world's smallest use of arc welding. Stuff then explodes. This produces two outputs, exhaust and a pressure wave which drives a piston. The rest is transferred into wheel motion by the various drive shafts and such. So the Engine is the engine and the car itself is the shell. You could use a car engine for a different purpose, say driving a generator for electricity or a mill to grind grain. You could use different inputs if the Engine has the coatings and such to handle things like ethanol or biodiesel.

To sum it up, an Engine is a piece of software that is usually not found in isolation. It acts as motive force for that piece of software but typically interacts very little if at all with the outside world. Several engines may work together to produce complimentary outputs or may be pipelined together as needed. An engine does not do things related to user experience in an aesthetic sense directly but drives those experiences none the less by motivating dataflow and being responsive enough to allow for good application performance.


问:
Why is the 'final' keyword used so little in the industry?
Since I discovered the powers of the final keyword in Java a few years ago, it helped me to make my codes a lot more readable, since people can easily see what are read-only variables. It may also provide a little boost to the JIT, and while that's a very limited one, it can't harm, especially when targeting embedded devices such as android platforms.

But more than everything, it contributes to make a design more robust to changes, and guide other committers when they need to modify the code base.

However, while browsing some of the JDK source code, I never stumbled upon this keyword, either for classes, methods, or variables. The same applies for various systems I had to review.

Is there a reason? Is it a common design paradigm to let everything mutable from the inside?

答:
Recently I discovered the joy of "Save Actions" feature in Eclipse IDE. I can force it to reformat my code, insert missing @Override annotations, and do some nifty stuff like remove unnecessary parenthesis in expressions or put final keyword everywhere automatically every time I hit ctrl + S. I activated some of those triggers and, boy, it helps a lot!

It turned out that many of those triggers act like a quick sanity check for my code.

I intended to override a method but annotation didn't show up when I hit ctrl + s? - perhaps I screwed up parameter types somewhere!

Some parenthesis were removed from the code on save? - maybe that logic expression is way too difficult for a programmer to get around with quickly. Otherwise, why would I add those parenthesis in a first place?

That parameter or local variable isn't final. Does it have to change its value?

It turned out that the fewer variables change the less trouble I have at debug time. How many times you were following some variable's value only to find that it somehow changes from say 5 to 7? "How the hell it could be?!" you ask yourself and spend next couple of hours stepping in and out of countless methods to find out that you've made a mistake in your logic. And in order to fix it you have to add one more flag, a couple of conditions and carefully change some values here and there.

Oh, I hate debugging! Each time I run the debugger I feel like my time is running out and I desperately need that time to make at least some of my childhood dreams to become true! To hell with debugging! finals mean no more mysterious value changes. More finals => less flimsy parts in my code => less bugs => more time to do good stuff!

As for final classes and methods I don't really care. I love polymorphism. Polymorphism means reuse means less code means less bugs. JVM does a pretty good job with devirtualization and method inlining anyway so I don't see a value in killing possibilities for code reuse for unsound performance benefits.

Seeing all those finals in the code is somewhat distracting at first and takes time to get used too. Some of my team mates still getting very surprised to see so many final keywords. I wish there was a setting in the IDE for special syntax coloring for it. I would be happy to switch it to some shade of gray (like annotations) so they won't get too distracting when reading code. Eclipse currently has a separate color for return and all other keywords but not for final.

OwnerDisplayName: Aurélien Ribon


答:
Engine is anything that takes some input, process that input, and delivers something as output. By this definition, a CMS engine is the part which generates the HTTP response on the fly based on the input. A database engine is what takes the query, draws an execution plan, and the executes it, and returns result.

Technically speaking, even a cow is a kind of engine. It takes grass, processes it, and delivers milk.

Don't forget that the term engine is more used in Mechanical engineering, and it does exactly the same.

Another aspect of an engine, is being the core of a system. For example, database engine is the core of any RDMS. However, IDEs to connect to that engine, while taking input and returning result, is not considered an engine.

Title: What makes a piece of software an Engine?

Body: The descriptor 'Engine' gets thrown around a lot: graphics engine, RegEx engine, AI engine, etc. but what actually makes a piece of software an Engine? Design, Input/Output, Purpose, Size?</p>


问:
Developing BlackBerry app with 3rd party content
I'm working on a BlackBerry app, however I'm loading data from various 3rd party websites around the web. (E.g, posts from stackoverflow or currency rates off Yahoo.com) I'd like to know if this could cause any trouble, would it be allowed?

Thank you!

答: As a lawyer to check out the end user and/or developer and/or api licenses and agreements that each of the sites provides. If what you're doing falls within the limits of those licenses, then you're ok, if not, then you might get into trouble.</p>


问:
Java and .NET: Why different sorting algorithms are used by default?
Just wondering why Java and .NET Framework uses different sorting algorithm by default.

In Java Array.Sort() uses Merge Sort algorithm by default and as Wikipedia.com says:

In Java, the Arrays.sort() methods use merge sort or a tuned
  quicksort depending on the datatypes and for implementation efficiency
  switch to insertion sort when fewer than seven array elements are
  being sorted

In .NET Framework Array.Sort/List.Sort() uses Quick Sort as default sorting algorithm (MSDN):

List.Sort() uses Array.Sort, which uses the QuickSort algorithm. This
  implementation performs an unstable sort; that is, if two elements are
  equal, their order might not be preserved. In contrast, a stable sort
  preserves the order of elements that are equal.

By looking at the great "Comparison of algorithms" table we can see that both algorithms has pretty different behaviour from Worst Case and Memory Usage perspectives:

Both Java and .NET are great Frameworks for Enterprise Solutions development, both has platforms for embedded development. So why they are using different sorting algorithm by default, any thoughts?

答:
Different development teams in two different companies came to different conclusions regarding the usual use case for their frameworks and components and have decided to implement accordingly.

Essentially, each company did their analysis, looked at their client base and made different decisions accordingly.

You can't expect analysis by different companies and teams, using different assumptions and raw data to come to the same conclusion.


问:
Are suffix trees and suffix arrays identical? What are the differences?
I have read some information through internet...i found that somehow suffix tree is quite similar to suffix array but somehow they are no the same thing..

Given a string, a suffix tree construction algorithm looks something like this (I copied the algorithm from a website)

```<code>FOR i ← 1 to n-1
FOR j ← 1 to i+1
   find the end path for S[j…i]
   extend the path, if needed, to S[i+1]
</code>```

suffix tree able to list down all the subtring obtained from a given string

However, does the suffix array also give the same functionality to obtain the list of substrings? Or are suffix arrays just an implementation of suffix trees? Or suffix array just provide some storing function?

答:
A suffix array is a space-efficient datastructure, which, if stored together with the original string, provides the same functions as a suffix tree.

So yes, you can think of a suffix array as a storage mechanism for suffix trees. Depending on the details, there may be performance costs to using arrays over full-blown trees, which typically are outweighed by the space-use benefits. I believe the arrays are constructed almost identically to the way trees are constructed, and, in practice, suffix arrays are the preferred method for storing/representing a suffix tree.


答: IMHO, an engine is just another word for subsystem, module, or library.  I.E. - one or more components cohesively designed for a specific purpose.</p>


问:
Moving from one man project to team project in the future. What should I be doing now in preparation and what can wait?
To elaborate I am interested in knowing what people think you need to put in place while still a one man project (team source control, documentation, builds etc) and what things don't need to be done until that point when the second person comes onto the project.

Anyone with any experience of moving through this scenario, their insights will be appreciated.

答:
These points are most important in my opinion:

Read over crucial parts of your code and make sure they are easy to understand. Use comments or intuitive function and variable names.

Make it easy for the new person to submit code.

If it's not trivial, make a README file that explains all necessary step for the new developer on how to setup the development environment. Alternatively closely assist in setting up this environment.

Give the new developer very clearly defined tasks when working on this new project. In my opinion these tasks should involve new but simple functionality. Clean-up tasks don't make much sense in my opinion as the new developer first has to get used to your coding style and those habits in it, even if they are bad. Cleaning up or even refactoring are jobs that need to be done by people who know the code.

Make clear what the process is for submitting code. (E.g. submit only stuff that compiles.) But don't be too strict, this can be frustrating at the beginning.

Have a document with coding conventions ready. It can be really frustrating to guess what the other ones coding conventions are.

If the app is complex, have some documentation ready explaining the architecture. Or explain the architecture to the new person using flow charts or something similar. You don't want to have the new developer waste too much time on reverse-engineering your project.

If the new developer is supposed to do deployments himself, have an ordered checklist ready explaining all necessary steps needed to deploy.

And last but not least: get a version control system. Subversion is just fine. But be sure not to add those Eclipse files (or whatever) that are user-specific and therefore constantly chaning. They make you waste hours. Don't hesitate to ask on Stackoverflow if you have problems with it.


问:
Does anyone have a specific example of using the Flyweight Pattern?
I have been studying design patterns and came accross the fly weight pattern.  I have been trying to see opportunities to use the pattern in my applications but I am having trouble seeing how to use it.  Also, what are some signs that a fly weight pattern is being used when I read other peoples code?

According to the definition it says:

Use sharing to support large numbers of fine-grained objects efficiently.

If I read it right Dictionaries and Hashtables could be instances of fly weights is this correct?

Thanks in advance.

答:
One example is in the Java libraries. Java has primitive types (e.g. int, which is a 32-bit integer) and wrappers for them (e.g. Integer, which wraps int). There are methods to "box" an int into an Integer and unbox an Integer into an int. The wrappers are necessary because the primitive types aren't objects and hence can't e.g. be used as keys in Maps or placed in Collections.

The boxing method uses an array of flyweight objects as a kind of cache for Integers corresponding to int values between -128 and 127. Since those are the values most likely to be used as keys or placed in collections, it reduces allocation and memory use. (If there are 5000000 Integers representing the value 0 floating around, that uses 5000000 times as much memory as reusing the flyweight instance).


问:
Computer Science and other advance topics taught in javascript
I'm an 'okay' Javascript developer working on browser javascript and node.js. However because I'm self taught, starting from copying and pasting jquery, there are a lot of holes in my programming/CS education.

I don't really have the time to devote myself to fully learning all the CS concepts that might be taught in a real college program, and learning C or Scala or whatnot, I'm just too addicted to hacking and making things that work. On some level studying CS seems to be 'premature scaling' in Hacker News parlance.

But every so often I get struck by the thought that maybe there is something that I should really really learn to take my skills and productivity to the next level.

Is there a book, a list of concepts or things that I can study and learn in my 'down time' that would see reasonable returns on my productivity and ability in 1 - 2 months?

Thanks!

And yes, the only language I know and use is Javascript, though I have tinkered with Python (High School), Ruby (RoR), PHP (Drupal), and Assembly (Making Diablo 2 Mods) in the past.

Please note that a book like O'Reilly's JavaScript Web Applications is too easy for me and not really what I'm talking about.

答:
Regarding the CS stuff, these are in my opinion the most important basic concepts:

Chained lists

Hash-Tables

Binary Trees, AVL Trees

Queues

Stacks

Regarding data structures, this is stuff you should really know. It helps you to write much more efficient code afterwards.

Regarding algorithms you should at least heard of this stuff:

Complexity, O(n²), O(n), O(n log n)

Sort algorithms like Quick Sort, Merge Sort, Insert sort, ...

Tree traversing... In Order, Post order, ...

Recursive vs. iterative algorithms

That's stuff I heard in university in my minor "Algorithms and data structures". (That's how this lecture is called in germany.)

Regarding Javascript I recommend you to play around stuff like prototypes, it's quite interesting. Though you should wait to use them productively unless you did some private project with them. I find it really strange that only very few ressources (including "The Good Parts" as far as I can see) don't deal with them in thourough detail. But you rarely see large-scale Javascript apps anyway (yet).

Things that helped me in the past to increase productivity very quickly was reading blogs from people that use my favorite programming language. In particular check out reddit:

r/javascript,

/Python,

...

Watch out for blog articles covering optimization and software design! That's stuff that matters ;)

Oh, and regarding Javascript: be sure to check out JavaScript on MDN: in my opinion the best JavaScript resource on the web.

ContentLicense: CC BY-SA 4.0


答:
The definition of engine is

noun /ˈenjən/

engines, plural

A machine with moving parts that converts power into motion

A thing that is the agent or instrument of a particular process

#1 is the semantic equivelant to engine in the software sense that it causes something to happen. 3D Graphics Engine, takes inputs and converts that to motion on the screen. A regex engine takes inputs and converts them to a different output.

#2 is like using the sentence an engine of change. This semantic can be applied to software as well.

Software engines are more than frameworks and libraries that passively do things. Software engines act on their own based on inputs, they are not passive, and usually have their own run loop to do processing on their own volition.


问:
On what basis would you split donation money among your open source team members without any strife?
I am a developer of an open source project which is hosted in SourceForge.

It started out as a little app then after some releases, it got more and more popular and it started consuming more time and responsibility from me. So I have enabled the donation option in SourceForge.

I'm passionate to continue developing it for free but if (ever) any money comes in, how should I split it with my team?

Should I split the amount equally among the number of team members? (50-50 as it is two-member team now)

Number of classes, commits or any other valuable submissions by team members?

Any other idea?

What would you do in such situation? Please give your opinions.

I hope this question will be useful for others.

答:
I think the reality of it is, it'll sit in your PayPal account until there's enough to take the team out and buy a round of beers. And if you're like most OS projects, that'll be months or years from now.

If it gets to be any real amount of money, I propose that you NOT spend it on developers--who obviously are contributing their time and expertise for good reasons that are not financial. Instead spend it on tools needed to further the project (hardware, a Basecamp account, etc), or on fun cheap marketing stuff like tshirts and desk toys. Make sure everyone who's ever contributed gets a t-shirt and you have enough left over to toss out at users groups and conventions and stuff.


问:
How would you feel if your code editor formatted your code for you as you typed, without tabs/spaces?
As with most things, I'm sure this concept has been tried before - I just haven't come across editors that use what I've termed 'Virtual Formatting'. The principle is that there's a floating left-margin that simulates the effect of the padding space/tab characters that are conventionally inserted by the developer or the editor itself to format the code. The editor continously parses code (even when commented-out) as you type and calculates the required indent based on the context where each line-feed is found

I'm developing this idea working specifically with an XML editor as XML has some peculiar problems with formatting characters and it tends to be heavily nested, however I believe many of the principles still hold for conventional code.

Have you experience coding with such a tool or do you have a view on whether it would help or hinder? Would it cause problems with version-control systems? (it detects and strips out all existing padding characters)

Unless you've tried it, the behavior of such a tool is hard to describe, it looks conventional until you actually start editing. I've put up  a screencast video showing a prototype in action which demonstrates editing XML, changing it's hierarchy and doing drag/drop and copy and paste operations, and then how formatting is broken/fixed when invalid characters are typed.

Edit
All answers/comments have so far been negative - so to attempt to redress balance, some benefits of virtual-formatting to think about:

No more debates on formatting standards, just place line-feeds where that conforms to your chosen/mandated convention

Where space is at a premium (in a book/blog/documentation) you can word-wrap but still getting perfect indentation

Each code-block can have a 'mouse-handle' immediately adjacent to where it starts, not squeezed into the screen edge - click this to select the whole block or inner-block

Drag, drop and forget - becomes viable for the first time

No time spent reformatting other peoples code

No incorrectly formatted code (in the sense that there is none - just the rendering)

Using Backspace instead of Ctrl+Backspace keeps your fingers on the keyboard guide-keys

Flexible rendering - adapt rendered formatting to your environment, anyone tried reading code on a mobile phone/small-screen tablet?

Consider that there are roughly 25% fewer editable characters (in a sample XSLT), doesn't that have efficiency benefits?

Edit - Conclusions so far

Developers have established tools and working methods that efficiently overcome most of the disadvantages inherent in the use of padding characters used for indentation.

There is concern that removal of formatting characters will detrimentally affect some differencing tools.

Developers want the flexibility to 'fine-tune' formatting in such a way that automated rendering could not handle.

The removal of leading spaces/tabs means that a 'code-aware' tool capable of code-formatting is needed to review such code efficiently - a plain-text editor would show no formatting.

Those that feel there may be some hypothetical benefits (to virtual-indentation), have a view that the disadvantages outweigh those potential benefits - conclusively.

Edit - Verdict

The perception of the obstacles and few (if any) benefits is such that it would be unwise for me, as a sole-developer, to pursue this space-free editing concept for general languages. For XML/XSLT, however (because of its special treatment of whitespace), there seems to be some agreement of potential at least.

Edit - Product Shipped

In spite of the generally negative sentiment found here, I went ahead and shipped the editor. I made a free version in the hope it would bring criticism in the form of more concrete issues, based on real experience. Somewhat frustratingly, there have been no complaints so far (in fact barely any feedback considering download volume). I'd like to think this was because users adjusted to the idea so well that they see this as a 'so what?' kind of feature - but there's no way of telling...

答: There exist an even more radical approach - a pure semantic editor. An IDE deals with a code which is even represented internally as an AST, not a plain text. See JetBrains MPS for example.</p>


答:
ASCII-range Character instances in Smalltalk are flyweights.

When you evaluate something like Character space, Character class >> #value: executes:

```<code>value: anInteger
    "Answer the Character whose value is anInteger."

    anInteger > 255 ifTrue: [^self basicNew setValue: anInteger].
    ^ CharacterTable at: anInteger + 1.
</code>```

The class variable CharacterTable is initialised like this:

```<code>initialize
    "Create the table of unique Characters, and DigitsValues."
    "Character initializeClassificationTable"

    CharacterTable ifNil: [
        "Initialize only once to ensure that byte characters are unique"
        CharacterTable := Array new: 256.
        1 to: 256 do: [:i | CharacterTable
            at: i
            put: (self basicNew setValue: i - 1)]].
    self initializeDigitValues
</code>```

So when you create a String, the ASCII-range Characters will come from CharacterTable rather than being newly-created every time.

Title: Does anyone have a specific example of using the Flyweight Pattern?

Body:
I have been studying design patterns and came accross the fly weight pattern.  I have been trying to see opportunities to use the pattern in my applications but I am having trouble seeing how to use it.  Also, what are some signs that a fly weight pattern is being used when I read other peoples code?

According to the definition it says:

Use sharing to support large numbers of fine-grained objects efficiently.

If I read it right Dictionaries and Hashtables could be instances of fly weights is this correct?

Thanks in advance.

Body:
ASCII-range Character instances in Smalltalk are flyweights.

When you evaluate something like Character space, Character class >> #value: executes:

value: anInteger
    "Answer the Character whose value is anInteger."

    anInteger > 255 ifTrue: [^self basicNew setValue: anInteger].
    ^ CharacterTable at: anInteger + 1.

The class variable CharacterTable is initialised like this:

initialize
    "Create the table of unique Characters, and DigitsValues."
    "Character initializeClassificationTable"

    CharacterTable ifNil: [
        "Initialize only once to ensure that byte characters are unique"
        CharacterTable := Array new: 256.
        1 to: 256 do: [:i | CharacterTable
            at: i
            put: (self basicNew setValue: i - 1)]].
    self initializeDigitValues

So when you create a String, the ASCII-range Characters will come from CharacterTable rather than being newly-created every time.


问:
Best practice for shared files within multiple unit test fixtures
I was wondering what the general best practice is for when you have multiple unit test fixtures testing different things that use the same file paths.

My test files are in a directory tree with certain rules so currently I am reusing directory macros in each file, i.e.

```<code>#define TYPE1FILE "Type1Files/"
#define TYPE2FILE "Type2Files/"

#define GENERIC_FILE "generic/"
#define SPECIAL_FILE "special/"

#define BAR1 TYPE1FILE GENERIC_FILE "/foo.txt"
#define BAR2 TYPE1FILE SPECIAL_FILE "/foo2.txt"
#define BAR3 TYPE2FILE GENERIC_FILE "/foo.txt"
#define BAR4 TYPE2FILE SPECIAL_FILE "/foo2.txt"
</code>```

However, this is starting to cause duplication in multiple files because multiple test fixtures will use the same:

```<code>#define BAR1 TYPE1FILE GENERIC_FILE "/foo.txt"
</code>```

if they both use the same file.

Also, it seems like it may be better to give them some scope by using variables inside the fixture or at least within the file.

I like being able to see a list of what files are being used for each test, but I am also considering moving the BAR defines all into a shared header file. I am not crazy about that idea because it will end up being a giant list of files without any indication of what is used where. Also it couples the test cases together more.

Does anyone have any suggestions?

答:
Aren't you actually coupling your implementation to a directory structure on the filesystem?

That point aside, maybe the problem has gone beyond what you can satisfactorily accomplish with #define and it's time to write some code to handle it.

I'm thinking a simple string GetMyFilename(Conditions condition) so you can utilise all the power of your development language and not just the macro-preprocessor to handle what file you need for your tests.

My general point is that tests are first class citizens of the code base, don't be afraid to use all your skills and disciplines on them.


问:
Is there any value in knowing the execution time for a unit test?
Is the execution time of a unit test useful for anything? Is a unit test an appropriate place for any sort of code profiling? Why or why not?

Sample Use Case:

Every unit test lists name, pass/fail, and execution time.

Time is also calculated per unit test class and total test suite execution time.

How could I use this info?

答: It could be useful if you are testing a procedure that is known to be slow. You could write unit tests that test the procedure with different sized inputs and you could monitor the performance of just that piece of code. Of course, whether or not this is applicable in your project really depends on your project. I use when it is easy to determine which function has a bottle-neck, and we want to isolate and test just that function.


答:
I can only think of timing out tests if something like an endless loop could occur in the tested code.

If you run unit tests as a part of post-commit checks or a build, the machine handling it may have all kinds of loads that will make precisely timed tests useless. Run such performance tests (not unit tests) in a controlled environment.

It can be useful to collect stats of test execution times to predict running times of large test suites (integration servers, like TeamCity, do this).

Title: Is there any value in knowing the execution time for a unit test?

Body:
Is the execution time of a unit test useful for anything? Is a unit test an appropriate place for any sort of code profiling? Why or why not?

Sample Use Case:

Every unit test lists name, pass/fail, and execution time.

Time is also calculated per unit test class and total test suite execution time.

How could I use this info?


答:
Is the execution time of a unit test useful for anything?

Yes.

Is a unit test an appropriate place for any sort of code profiling?

Yes.

Why?

Because you need facts and measurements.  If a test suite is slow, it's important to know where so you can focus -- separately -- on why.

Code profiling is only done when code is proven to be slow.

Unit tests are an easy, cheap, obvious, painless way to locate (1) slow code and (2) sudden changes in performance.


答:
It could be valuable if you were to keep track of average performance for a given test across all test runs. A sudden increase in execution time might signal the introduction of a new bottleneck into the code.

Also in general it is desirable to keep the execution for an entire set of unit tests as fast as possible in order to encourage running the tests frequently. Knowing the execution of individual tests will help identify which tests are taking the most time across all tests.


答:
One of the most important rules about unit tests is they should run fast.

Developers should be able to run the whole suite of unit tests in seconds, and definitely not in minutes and minutes. Developers should be able to quickly run them after changing the code in anyway. If it takes too long, they won't bother running them and you lose one of the main benefits of the tests. We currently have about 800 tests that run in around 30 seconds.

So if any tests do start taking too long, you can see which test is taking too long and do something about it.

Our unit test framework tells us how long it took for each test to run. Most of them take 0- 5ms (if I remember correctly), we have one or two that take about 3 seconds.


答:
Consider the development of your non-functional requirements:

Functional Requirement: Once the filters have been selected, the report should be generated.
Non-Functional: The report should generate in 2 seconds.

If your test's execution time is > 2 seconds, you could even fail the test as a "usability" test.


问:
Space complexity of Iterative Deepening DFS
We read on Wikipedia > Iterative deepening depth-first search that

The space complexity of IDDFS is O(bd), where b is the branching factor and d is the depth of shallowest goal.

Wikipedia also gives some decent pseudocode for IDDFS; I pythonified it:

```<code>def IDDFS(root, goal):
  depth = 0
  solution = None
  while not solution:
    solution = DLS(root, goal, depth)
    depth = depth + 1
  return solution

def DLS(node, goal, depth):
  print("DLS: node=%d, goal=%d, depth=%d" % (node, goal, depth))
  if depth >= 0:
    if node == goal:
      return node

    for child in expand(node):
      s = DLS(child, goal, depth-1)
      if s:
        return s

  return None
</code>```

So my question is, how does the space complexity include the branching factor? Does that assume that expand(node) takes up O(b) space? What if expand uses a generator that only takes constant space? In that case, would the space complexity still be a function of the branching factor? Are there situations where it is even possible for expand to be a constant-space generator?

答:
You're right. Wikipedia is wrong! Does anyone have the book referenced on Wikipedia to find out what they mean (maybe they're talking about an optimization of some sort)?

Check out http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.91.288 and http://intelligence.worldofcomputing.net/ai-search/depth-first-iterative-deepening.html


答:
The closest answer so far, in my opinion, was the one that said it's a marketing thing.

Let's try to think where the use of the word in this context came from.

Certainly car or jet engines aren't called that way because they take input and produce some output. If that were it, then not just functions, but many things in life would be engines.

It's not by coincidence, in my opinion, that a cow is most definitely not an engine, not technically speaking, and not in any sane way of speaking.

Engines use various types of energy and turn it into motion (i.e. a specialized type of energy).

Why do we use it in software? I'd say the most realistic guess is because it simply sounds cool. Why do we call some programmers 'Architects'? Same reason, if you ask me.

Another reason could be that programmers generally like to use metaphors to describe parts of their software, so they could be easier to grasp (because naturally the're often not).

My hunch is that it's bad practice to abuse metaphors in such a way that your application domain is full of them. I think it could be useful when talking to clients, or people who don't know or care about programming. Other than that, simple flow charts and diagrams do the job for me.


问:
How to determine the source of a request in a distributed service system?
Map/Reduce is a great concept for sorting large quantities of data at once. What to do if you have small parts of data and you need to reduce it all the time?

Simple example - choosing a service for request.

Imagine we have 10 services. Each provides services host with sets of request headers and post/get arguments. Each service declares it has 30 unique keys - 10 per set.

```<code>service A:
name
id
...
</code>```

Now imagine we have a distributed services host. We have 200 machines with 10 services on each. Each service has 30 unique keys in there sets. but now to find to which service to map the incoming request we make our services post unique values that map to that sets. We can have up to or more than 10 000 such values sets on each machine per each service.

```<code>service A machine 1
name = Sam
id = 13245
...
service A machine 1
name = Ben
id = 33232
...
...
service A machine 100
name = Ron
id = 777888
...
</code>```

So we get 200 * 10 * 30 * 30 * 10 000 == 18 000 000 000 and we get 500 requests per second on our gateway each containing 45 items 15 of which are just noise. And our task is to find a service for request (at least a machine it is running on).

On all machines all over cluster for same services we have same rules.

We can first select  to which service came our request via rules filter 10 * 30. and we will have 200 * 30 * 10 000 == 60 000 000.

So... 60 mil is definitely a problem...
I hope to get on idea of mapping 30 * 10 000 onto some artificial neural network alike Perceptron that outputs 1 if 30 words (some hashes from words) from the request are correct or if less than Perceptron should return 0. And I’ll send each such Perceptron for each service from each machine to gateway. So I would have a map Perceptron <-> machine for each service.

Can any one tall me if my Perceptron idea is at least “sane”? Or normal people do it some other way? Or if there are better ANNs for such purposes?

答:
Why not just tack a machine ID on top of the service identifier? That way you have 31 unique keys, which specify both the service and machine, and you're left with a lookup into 10000.

Also, I don't really understand your math. From what I see, it looks more like:

You're choosing from (200 machines) * (10 services per machine) * (10000 values per service).

If you filter by service based on the 30 keys then you're looking at (200 machines) * (1 service) * (10000 values per service).  Where does the extra 30 come from in there?


问:
Designing for an algorithm that reports progress
I have an iterative algorithm and I want to print the progress. However, I may also want it not to print any information, or to print it in a different way, or do other logic. In an object oriented language, I would perform the following solutions:

Solution 1: virtual method

have the algorithm class MyAlgoClass which implements the algo. The class also implements a virtual reportIteration(iterInfo) method which is empty and can be reimplemented. Subclass the MyAlgoClass and override reportIteration so that it does what it needs to do. This solution allows you to carry additional information (for example, the file unit) in the reimplemented class.

I don't like this method because it clumps together two functionalities that may be unrelated, but in GUI apps it may be ok.

Solution 2: observer pattern

the algorithm class has a register(Observer) method, keeps a list of the registered observers and takes care of calling notify() on each of them. Observer::notify() needs a way to get the information from the Subject, so it either has two parameters, one with the Subject and the other with the data the Subject may pass, or just the Subject and the Observer is now in charge of querying it to fetch the relevant information.

Solution 3: callbacks

I tend to see the callback method as a lightweight observer. Instead of passing an object, you pass a callback, which may be a plain function, but also an instance method in those languages that allow it (for example, in python you can because passing an instance method will remain bound to the instance). C++ however does not allow it, because if you pass a pointer to an instance method, this will not be defined. Please correct me on this regard, my C++ is quite old.

The problem with callbacks is that generally you have to pass them together with the data you want the callback to be invoked with. Callbacks don't store state, so you have to pass both the callback and the state to the Subject in order to find it at callback execution, together with any additional data the Subject may provide about the event is reporting.

Question

My question is relative to the fact that I need to implement the opening problem in a language that is not object oriented, namely Fortran 95, and I am fighting with my usual reasoning which is based on python assumptions and style. I think that in Fortran the concept is similar to C, with the additional trouble that in C you can store a function pointer, while in Fortran 95 you can only pass it around.

Do you have any comments, suggestions, tips, and quirks on this regard (in C, C++, Fortran and python, but also in any other language, so to have a comparison of language features that can be exploited on this regard) on how to design for an algorithm that must report progress to some external entity, using state from both the algorithm and the external entity ?

答:
Pass the reference to your callback function as an additional parameter into your algorithm.

If you don't want to bother with the callbacks this invocation, pass a simple stub function that does nothing more than return.


答: Graphics. Typically, a raster image (which are the backbone of most consumer-level computer graphics) is CPU-cheap, but memory-expensive to work with (which is fine because memory's cheap but CPU is expensive). If that raster image is to be repeated many times in rendering a larger UI (from icons in a Windows GUI app to characters of a font in a word processor, to textures on surfaces in a 3D game), it makes a lot of sense to load the image into memory once, and simply point to it using very simple objects that are cheap to make and do not, themselves, take up a lot of memory. A sprite, which is simply a point in graphical space at which an image should be displayed, is just a 3D point and a memory pointer to the first pixel of the image to use. MAYBE it also includes the dimensions of the portion of the sprite image file to be used, either in graphical or memory terms. This information is all very inexpensive to change, say to change the image or location of the sprite, and it can be done without loading a new image each time, thus drastically increasing performance of the underlying program to manipulate and display the proper portions of the proper images to render a complete UI "scene".</p>


问:
3D Secure MPI providers - suitable for multiple third party payment systems?
Long time user of SO, and figured this is the best place to ask this question.

I am currently consolidating our eCommerce payment clearing systems into one Asp.Net MVC website that will work like PayPal but for our own websites.

The businesses within the group have different rules as to the payment provider that is to be used throughout the world and, at present, we currently integrate with Servebase, Moneris and PayPal.  Thus, I have an architecture that allows new providers to be written and plugged in through a single extensibility point.

In the future I am likely to have to support 3D Secure, which complicates matters significantly; not only because of the additional web workflow but because each provider appears to recommend it's own MPI provider/solution for the 3D Secure process.

Assuming that all three of these payment providers appear to offer the same kind of 3D Secure hooks (I'm seeing similar parameters in all of their APIs), do you think I'm going to be able to use a single MPI to manage the 3D Secure process for all or do you think I'm going to have to have a different 3D Secure implementation for each of my providers?

For example, PayPal recommend using Cardinal Centinel for 3D Secure - so let's say I get a single account for them, what are the chances I'll be able to reuse it for Moneris or Servebase, say.  Is the 3D Secure system a 'standard' (using the term loosely there!) that is simply being echoed by these other payment providers?

It doesn't matter if you know nothing of the other APIs I mention here - i.e. Moneris, Servebase - if you've had any experience similar to mine I would appreciate any thoughts.

答:
The 3D Secure protocol is standardized. So, in theory at least, you should be able to use a single MPI to manage the 3D Secure process for all of the transactions - once you've obtained an authorization, it should be valid for the given card for all payment processors.

You're one step removed from the process, though - you'll be expected to pass the information in to each of the vendors; depending on their API, that portion of the work may vary.

Watch out when you choose your MPI though; make sure they provide solid services for all cards. While payment processors won't care which MPI you used, the card issuer will - an MPI which is acceptable for VISA may not necessarily be acceptable for Mastercard.


答:
As deterministic as computers themselves are, computer engineering is not an exact science. Two people, given the same problem domain, will perform an analysis and develop two different solutions that satisfy all constraints of the problem. It may be difficult or impossible to empirically determine which of these is "better" in the general case.

My guess is that the .NET QuickSort is layered on top of something in the MFCs or Windows API, and is probably inherited from much older versions of Windows where the multi-threadable advantage of MergeSort wouldn't have even been considered for the computers of the day. (EDIT: it's not, though Microsoft developers have been QuickSort fanboys for a long time, as evidenced by this choice of sorting implementation since MS-DOS).

Java, which can't use any platform-specific implementation because Java was designed from scratch to be completely platform-independent, went a different way. Who knows why MergeSort came out on top; my wild guess is that the implementation won some sort of performance competition versus some other sorts the developers came up with, or else that an O(n)-space MergeSort just looked best on paper in terms of best-case and worst-case performance (MergeSort has no Achilles' heel relating to element selection like QuickSort, and its best-case is a near-sorted list while that's often QuickSort's worst). I doubt multithreading benefits were initially considered, but the current implementation may well be multithreaded.

Title: Java and .NET: Why different sorting algorithms are used by default?

Body:
Just wondering why Java and .NET Framework uses different sorting algorithm by default.

In Java Array.Sort() uses Merge Sort algorithm by default and as Wikipedia.com says:

In Java, the Arrays.sort() methods use merge sort or a tuned
  quicksort depending on the datatypes and for implementation efficiency
  switch to insertion sort when fewer than seven array elements are
  being sorted

In .NET Framework Array.Sort/List.Sort() uses Quick Sort as default sorting algorithm (MSDN):

List.Sort() uses Array.Sort, which uses the QuickSort algorithm. This
  implementation performs an unstable sort; that is, if two elements are
  equal, their order might not be preserved. In contrast, a stable sort
  preserves the order of elements that are equal.

By looking at the great "Comparison of algorithms" table we can see that both algorithms has pretty different behaviour from Worst Case and Memory Usage perspectives:

Both Java and .NET are great Frameworks for Enterprise Solutions development, both has platforms for embedded development. So why they are using different sorting algorithm by default, any thoughts?


问:
What is the best way to design a web site to be highly scalable?
For websites that need to be highly scalable, such as social networks like facebook, what's the best way to design the website?

Should I have a web service which the site queries to get data that it needs?

or

Should the site query databases directly? (can be done using built in language constructs to fill tables automatically etc).

I would think the web service is the better design since it gives centralized data access and things like caching and the like become much easier to control, but what do others think?

答:
For websites that need to be highly scalable such as social networks like facebook, whats the best way to design the website?

Measure.

I would think the...

Bad policy.

Actual measurement is required.


答:
There are two primary ways to scale, up and out.

Scaling up is replacing a machine with a more powerful one.  Scaling out means adding another machine to do the work that existing machines are doing.

Any high traffic web site needs the ability to scale out.  The software architecture needs to be done is such as way so that more machines can be added easily the busier the site gets.

Usually this means splitting the application into tiers so that one can plug and play more servers at each tier.

I would do option 1, have a service instead of doing it directly.  You can only scale a monolithic application so far.

Title: What is the best way to design a web site to be highly scalable?

Body:
For websites that need to be highly scalable, such as social networks like facebook, what's the best way to design the website?

Should I have a web service which the site queries to get data that it needs?

or

Should the site query databases directly? (can be done using built in language constructs to fill tables automatically etc).

I would think the web service is the better design since it gives centralized data access and things like caching and the like become much easier to control, but what do others think?


答:
Wow, this is a simple question, which a huge array of possible answers. The more explicit part of your question asks whether it is more scalable to interface with your database directly or through a web service. That answer is simple: query the database directly. Going through the web service adds a whole bunch of latency that is completely unnecessary for code operating behind a firewall (by and large). A web service for example requires some component to receive a request, deserialize it, query the DB, serialize a response and return it. So if your code is all operating behind a firewall, save yourself the trouble and just query the DB directly.

Making a web site scalable however goes way beyond the question you initially posed. So forgive me if I go off on a tangent here, but I thought it might be useful considering that you mentioned Facebook in particular.

I would recommend you read up on the work and tools built by Brad Fitzpatrick (founder of LiveJournal and now at Google). When I worked with him at Six Apart, here are some of the things I learned from him, and about LiveJournal's architecture that made it so scalable.

Use narrow database tables as opposed to wide ones. What was fascinating about this was learning what motivated this architecture, which was creating a system that was easily and quickly upgraded. If you use wide tables, or tables for which each field or property is a column in the table, when it comes time to upgrade the database schema, for example adding a new column, then the system will need to lock the table while the schema change is implemented. When operating at scale this would mean a simple change to the database schema could result in a large database outage. Which sucks obviously. A narrow table on the other hand simply stores each individual property associated with an object as a single row in the database. Therefore when you want to add a new column to the database all you need to do is INSERT records into a table, which is a non-locking operation. Ok, that is a little background, let's see how this model actually translates in working system like LiveJournal.

Let's say you want to load the last 10 journal entries on a person's blog, and let's say each journal entry has ten properties. In a classic wide table layout, each property would correlate to a column on a table. A user would then query the table once to fetch all of the data they need. The query would return 10 rows and each row would have all the data they need (e.g. SELECT * FROM entries ORDER BY date LIMIT 10). In a narrow table layout however things are bit different. In this example there are actually two tables: the first table (table A) stores simple criteria one would want to search by, e.g. the id of the entry, the id of the author, the date of the entry, etc. A second table (table B) then stores all of the properties associated with an entry. This second table has three columns: entry_id, key and value. For every row in table A, there would be 10 rows in table B (one row for each property). Therefore in order to fetch and display the last ten entries, you would need 11 queries. The first query gives you the list of entry IDs, and then the next ten queries would fetch the properties associated with each of the entries returned in the first query.

"Holy moly!" you say, "how on Earth can that be more scalable?!" Its totally counter-intuitive right? In the first scenario we just had one database query, but in the second "more scalable" solution we have 11 database queries. That makes no sense. The answer to that question relies entirely upon the next bullet.

Use memcache liberally. In case you were not aware, memcache is a distributed, stateless, low latency, network based caching system. It is used by Facebook, Google, Yahoo, and just about every popular and scalable web site on the planet. It was invented by Brad Fitzpatrick partially to help offset the database overhead inherent in a narrow table database design. Let's take a look at the same example as discussed in #1 above, but this time, let's introduce memcache.

Let's begin when a user first visits a page and nothing is in the cache. You begin by querying table A which returns the IDs of the 10 entries you want to display on the page. For each of those entries you then query the database to retrieve the properties associated with that entry, and then using those properties constitute an object that your code can interface with (e.g. an object). You then stash that object (or a serialized form of that object) in memcache.

The second time someone loads the same page, you begin the same way: by querying table A for the list of entry IDs you will display. For each entry you first go to memcache and say, "do you have entry #X in the cache?" If yes, then memcache returns the entry object to you. If not, then you need to query the database again to fetch its properties, constitute the object and stash it in memcache. Most of the time, the second time someone visits the same  page there is only one database query, all other data is then pulled straight from memcache.

In practice, what ended up happening for most of LiveJournal is that most of the system's data, especially the less volatile data, was cached in memcache and the extra queries to the database needed to support the narrow table schema were all but completely offset.

This design made solving the problem associated with assembling a list of posts associated with all of your friends into a stream, or "wall" much, much easier.

Next, consider partitioning your database. The model discussed above surfaces yet another problem, and that is your narrow tables will tend to be very large/long. And the more rows those tables have the harder other administrative tasks become. To offset this, it might make sense to manage the size of your tables by partitioning the tables in someway, so that clusters of users are served by one database, and another cluster of users are served by a separate database. This distributes load on the database and keeps queries efficient.

Finally, you need awesome indexes. The speed of your queries will depend largely upon how well indexed your database's tables are. I won't spend too much time discussing what an index is, except to say that it is a lot like a giant card catalog system to make finding needles in a haystack more efficient. If you use mysql then I recommend turning on the slow query log to monitor for queries that take a long time to fulfill. When a query pops up on your radar (e.g. because it is slow), then figure out what index you need to add to the table to speed it up.

"Thank you for all of this great background, but holy crud, that is a lot of code I will have to write."

Not necessarily. Many libraries have been written that make interfacing with memcache really easy. Still other libraries have codified the entire process described above; Data::ObjectDriver in Perl is just such a library. As for other languages, you will need to do your own research.

I hope you found this answer helpful. What I have found more often than not is that the scalability of a system often comes down less and less to code, and more and more to a sound data storage and management strategy/technical design.


答:
It's accepted wisdom that web applications should be designed with three tiers by default - web (presentation), application and database layers. This division is due to different requirements at each of the layers - typically quality disk access/storage for the database, high CPU/Memory at the app layer, and high external bandwidth/memory/geographic dispersion at the web layer. The application/database layer are often merged into one until much later in the lifecycle of the application, as database machines often tend to be massive servers which can be built to handle the early application load, too.

The specific number of layers and appropriate architecture for your application, though, doesn't have to match this or any other model.

Plan to need to measure and monitor all of the activity in your system. Start from a two or three tier design, and focus on the portions of it which, as you're building it, look like will require the most amount of resources. Let the running application guide your design, at this level. The more information you gather, and the more accurate and detailed it is, the better decisions you can make about designing the application as it grows.

Choose a framework and architecture which will, later on, allow you to pivot/make required changes as quickly and painlessly as possible. Even if your data-access/storage/processing and application processing are being performed in the same executable, if they're properly factored, it won't be as difficult to split them out into two layers later on, for example.


问:
Why are interfaces useful?
I have been studying and coding in C# for some time now. But still, I can't figure the usefulness of Interfaces. They bring too little to the table. Other than providing the signatures of function, they do nothing. If I can remember the names and signature of the functions which are needed to be implemented, there is no need for them. They are there just to make sure that the said functions(in the interface) are implemented in the inheriting class.

C# is a great language, but sometimes it gives you the feeling that first Microsoft creates the problem (not allowing multiple inheritance) and then provides the solution, which is rather a tedious one.

That's my understanding which is based on limited coding experience. What's your take on interfaces? How often you make uses of them and what makes you do so?

答:
So in this example, the PowerSocket doesn't know anything else about the other objects. The objects all depend on Power provided by the PowerSocket, so they implement IPowerPlug, and in so doing they can connect to it.

Interfaces are useful because they provide contracts that objects can use to work together without needing to know anything else about each other.

Title: Why are interfaces useful?


答:
Depending on the nature of the product, I have known more than a couple developers who have just put a sliding progress bar that just goes every second or two (few percent per tick), which is totally unrelated to the actual progress.  There is one callback at the end to make it flash to 100% (or not).  It's a total hack, but then again a progress bar is just a tool to tell the user "please wait, yes the program hasn't broken and is actually doing something."

If you're talking about a big complicated install progress notification this isn't a good idea, but for something that might take 30 seconds or less it's fine.

In other words, if it's a small operation, don't worry about being super accurate, or even representative at all, in your progress reporting.

Title: Designing for an algorithm that reports progress

Body:
I have an iterative algorithm and I want to print the progress. However, I may also want it not to print any information, or to print it in a different way, or do other logic. In an object oriented language, I would perform the following solutions:

Solution 1: virtual method

have the algorithm class MyAlgoClass which implements the algo. The class also implements a virtual reportIteration(iterInfo) method which is empty and can be reimplemented. Subclass the MyAlgoClass and override reportIteration so that it does what it needs to do. This solution allows you to carry additional information (for example, the file unit) in the reimplemented class.

I don't like this method because it clumps together two functionalities that may be unrelated, but in GUI apps it may be ok.

Solution 2: observer pattern

the algorithm class has a register(Observer) method, keeps a list of the registered observers and takes care of calling notify() on each of them. Observer::notify() needs a way to get the information from the Subject, so it either has two parameters, one with the Subject and the other with the data the Subject may pass, or just the Subject and the Observer is now in charge of querying it to fetch the relevant information.

Solution 3: callbacks

I tend to see the callback method as a lightweight observer. Instead of passing an object, you pass a callback, which may be a plain function, but also an instance method in those languages that allow it (for example, in python you can because passing an instance method will remain bound to the instance). C++ however does not allow it, because if you pass a pointer to an instance method, this will not be defined. Please correct me on this regard, my C++ is quite old.

The problem with callbacks is that generally you have to pass them together with the data you want the callback to be invoked with. Callbacks don't store state, so you have to pass both the callback and the state to the Subject in order to find it at callback execution, together with any additional data the Subject may provide about the event is reporting.

Question

My question is relative to the fact that I need to implement the opening problem in a language that is not object oriented, namely Fortran 95, and I am fighting with my usual reasoning which is based on python assumptions and style. I think that in Fortran the concept is similar to C, with the additional trouble that in C you can store a function pointer, while in Fortran 95 you can only pass it around.

Do you have any comments, suggestions, tips, and quirks on this regard (in C, C++, Fortran and python, but also in any other language, so to have a comparison of language features that can be exploited on this regard) on how to design for an algorithm that must report progress to some external entity, using state from both the algorithm and the external entity ?


问:
Why are so many HTML tables used in page markup?
I'm trying to get into web development, so I started learning HTML, CSS, and javascript. I've been coming along really well with javascript, but I still can't make anything with HTML and CSS to save my life. I was going to ask for some places or tips to improve, but I know the question has been asked countless times and would just get deleted, so I figured I would just view the source of some other people's websites. </p>

<p>What I keep seeing is tables left and right. Is there a valid reason to use tables? </p>

答: Browsers are the problem. Despite what was best practice and what everyone agreed to, it was just too hard to make websites look consistently good in browsers without tables, even though it was coded wrong. It has to do with semantics. That's the most important thing to learn with HTML, is the semantic meanings of tags. The best place to learn that is in the first five or so videos that Educator.com has on it's HTML videos, they're all done with HTML5 in mind and explaining what semantics is and why it's important.</p>


问:
SLF4J vs LOG4J Which one to prefer?
I have seen this question but does not answer me.

It have a brand new project. I have almost always used Log4J but now I am seeing SLF4J making some waves. What is it that Log4J could not do and we needed SLF4J? Is it really an upgrade. Should I use it in a project which will also have an API for other to extend upon?

答:
SLF4J is basically an abstraction layer. It is not a logging implementation. It means that if you're writing a library and you use SLF4J, you can give that library to someone else to use and they can choose which logging implementation to use with SLF4J e.g. log4j or the Java logging API. It helps prevent projects from being dependent on lots of logging APIs just because they use libraries that are dependent on them.

So, to summarise: SLF4J does not replace log4j, they work together. It removes the dependency on log4j from your library/app.

Take a look here: http://www.slf4j.org/, or even here: http://commons.apache.org/logging/ (Apache Commons Logging is similar to SLF4J) for more info on this concept.

Title: SLF4J vs LOG4J Which one to prefer?


问:
New PHP project, how to best architect it
I have already done a PHP project, and I did a number of things pretty wrong :)

I just had all pages as scripts with php mixed in with html. I also wasn't using a framework like cakephp. And I didn't really use objects, nor had any sort of test suite. :)

This time around I want to get it right.  What should I do in terms of good practice approaches?  And what am I ok not using? Any suggestions? Tips?

答:
From what you've provided, it's difficult to offer anything other than general guidelines.

SOLID principles of OO design and best practices such as those found in Kent Beck's Smalltalk Best Practice Patterns

an architecture that is appropriate for your domain and well understood by you and/or your team (MVC, for example)

mature frameworks or libraries with an eye to familiarity, community engagement, documentation, stability, etc

a coding style that favors consistency, readability and maintainability

a commitment to automated testing and/or TDD

a modern version control system (git, mercurial)

a willingness to treat PHP like a real language and not just a collection of cobbled-together HTML templates

most importantly, a process that is iteratively self-evaluating and self-improving

Edit: Finally, don't try to do too much at once. Make a change, give it time to set in, and evaluate that change. Keep what works.


答:
Honestly, just do it. Every project you undertake is going to yield a whole whack of new learnings. The definition of "getting it right" changes with every new project completed. It's honestly a never-ending evolution.

Just do it.

Try using a framework for your next project, see how they recommend doing things. If you're feeling particularly ambitious, try using a different framework (or even another language) for your subsequent project. The more you expose yourself to different methodologies, the more you learn. It's absolutely up to you whether or not you agree with those practices.

Key here is not to let past errors stand in your way or overwhelming you to the point you don't do anything because you're worried about making mistakes.

Edit: While I couldn't agree more with Rein's answer, I've found that in the process of doing things with packages and frameworks done by people smarter and more experienced than you, a lot of his points are learned without you actively having to learn them. Makes sense hopefully?

Title: New PHP project, how to best architect it

Body:
I have already done a PHP project, and I did a number of things pretty wrong :)

I just had all pages as scripts with php mixed in with html. I also wasn't using a framework like cakephp. And I didn't really use objects, nor had any sort of test suite. :)

This time around I want to get it right.  What should I do in terms of good practice approaches?  And what am I ok not using? Any suggestions? Tips?


问:
How can I find the root of confusing problems?
Several times after posting over at Stack Overflow, I have found that the problem I have is somewhat specific, and may have issues rooted elsewhere in my code. These are very difficult to solve with a question, and I often end up getting poor or irrelevant answers.

When a problem is just too localized or to complex for Stack Overflow to be any use, what should I be doing to break down the problem so it can be solved? How can I approach peers or user groups about problems like these?

答:
So to start off with, see Wikipedia:

Problems that can be solved in theory (e.g., given infinite time), but which in practice take too long for their solutions to be useful, are known as intractable problems.

Your problems may be many things, but they are probably not intractable. So let's attack the heart of the issue:

How do you break down problems to a level that others can assist with?

The obvious answer is debugging. A problem occurs when your program does something you don't expect. So, at every step, you want to make sure that reality is congruent with your expectations. When you reach a point where what's happening is not what you expect, then you've found your issue.

People often post asking for help for what they "think" is wrong. Maybe they googled the error and tried implementing the solution in the first result. Maybe their rubber ducky told them. Frankly, what you "think" is usually worthless. To quote Richard Feynman:

It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong.

So first figure out what's wrong!

From there, it's simply about isolating the inputs and outputs. People don't need to understand your whole program - they simply need to see what's coming in, and what's supposed to come out. You should always describe the source of what is coming in, because sometimes what you think is going in is not what's really going in.

At that point, the problem can usually be solved. Some SO guru will see what's up, perhaps ask you to do something, and you'll accept their answer and say "thanks a lot!" or something like that.

Remember, find the point where expectations and reality differ. Write the basic case that causes this. Implement the solution. QED.


答:
Scalablility is not a function of specific implementation strategies but rather of designing your application architecture so that the data access layer can evolve without massive refactoring and rewriting.

An important technique in building a system that scales is to understand your high-level data access requirements and build an interface contract around them. For example, you might have the requirement to get one user or to list the 50 photos posted most recently by any user.

You don't necessarily need a network channel between your application business logic and the data access logic; a method call indirection with one method per logical operation would do just fine to start.

Make these data access methods as simple as possible to start with. It's very hard to predict where the performance problems will lie until your application is serving real usage patterns and you are collecting data about where you have bottlenecks.

By having a well defined data access interface you can evolve your data access implementation without making broad changes to your entire application. You can also decide to switch to a web service architecture transparently to your business logic.

Many of the answers above give some great advice on how to proceed once you've discovered your performance bottlenecks, but if you apply these too early you can become hamstrung by the complexity of your code before you know if that complexity is even required.


问:
Using MVP, how to create a view from another view, linked with the same model object
Background

We use the Model-View-Presenter design pattern along with the abstract factory pattern and the "signal/slot" pattern in our application, to fullfill 2 main requirements

Enhance testability (very lightweight GUI, every action can be simulated in unit tests)
Make the "view" totally independant from the rest, so we can change the actual view implementation, without changing anything else
In order to do so our code is divided in 4 layers :

Core : which holds the model
Presenter : which manages interactions between the view interfaces (see bellow) and the core
View Interfaces : they define the signals and slots for a View, but not the implementation
Views : the actual implementation of the views
When the presenter creates or deals with views, it uses an abstract factory and only knows about the view interfaces.

It does the signal/slot binding between views interfaces. It doesn't care about the actual implementation. In the "views" layer, we have a concrete factory which deals with implementations.

The signal/slot mechanism is implemented using a custom framework built upon boost::function.

Really, what we have is something like that : http://martinfowler.com/eaaDev/PassiveScreen.html

Everything works fine.

The problem

However, there's a problem I don't know how to solve.

Let's take for example a very simple drag and drop example.

I have two ContainersViews (ContainerView1, ContainerView2). ContainerView1 has an ItemView1. I drag the ItemView1 from ContainerView1 to ContainerView2.

ContainerView2 must create an ItemView2, of a different type, but which "points" to the same model object as ItemView1.

So the ContainerView2 gets a callback called for the drop action with ItemView1 as a parameter. It calls ContainerPresenterB passing it ItemViewB

In this case we are only dealing with views. In MVP-PV, views aren't supposed to know anything about the presenter nor the model, right ?

How can I create the ItemView2 from the ItemView1, not knowing which model object is ItemView1 representing ?

I thought about adding an "itemId" to every view, this id being the id of the core object the view represents.

So in pseudo code, ContainerPresenter2 would do something like

itemView2=abstractWidgetFactory.createItemView2();
this.add(itemView2,itemView1.getCoreObjectId())
I don't get too much into details. That just work. The problem I have here is that those itemIds are just like pointers. And pointers can be dangling. Imagine that by mistake, I delete itemView1, and this deletes coreObject1. The itemView2 will have a coreObjectId which represents an invalid coreObject.

Isn't there a more elegant and "bulletproof" solution ?

Even though I never did ObjectiveC or macOSX programming, I couldn't help but notice that our framework is very similar to Cocoa framework. How do they deal with this kind of problem ? Couldn't find more in-depth information about that on google. If someone could shed some light on this.

I hope this question isn't too confusing ...

答:
Finally we found a solution :
- When you start dragging an object, the view sends a "drag" signal to it's controller. The controller gets the model object and puts a pointer to it in a shared "DragandDrop-board" (think of it as a clipboard)

When you drop, the view sends a "drop" signal to it's controller. The controller gets the object from the drag and drop board, and updates the model accordingly.

If you use a kind of a smart pointer that's quite easy to make sure the pointer is still valid after the drop .