答:
Adding to Eric's answer, you can also install the Android SDK and Eclipse IDE (needed for Android development) alongside your .NET development environment.

Make sure you have the recommended version of Java installed for Android development, which is not necessarily the latest version.

Title: Can I run both .Net and Java framework in one computer?

Body:
I have .NET 2.5 installed in my computer unit, as a beginner I used VB.net
As I went along I've also installed new .dlls that would help me with my VB.net programming
Now, I want to study java as well, leading to Android programming.

If I install the JDK 7.0, can I still be able to program in VB.net? Will installing JDK overwrite .dlls essential to the .NET framework? Should I get a new developing PC that focuses with JAVA/Android development?


问:
Chain-of-Command/Responsibility pattern for my simple API. A good idea?
I need to write a simple API for a project I'm working on. This API will be used internally to perform some server side actions that were triggered by AJAX calls.

To make things easier for me, I thought of using the Chain-of-Command/Responsibility pattern.

Here are more details about what I want to do:

I am creating a "dashboard" where admins of my app will be able to update information (meta data) about items stored in a database.

To make things easy and simple for the admins, I chose to use AJAX. So, if an admin wants to delete an item, (s)he clicks on the "delete button". A POST request is then sent to edit.php page with all the information needed to perform the deletion (action : delete, element : link, id : xx ...). Where action, element and of course id can change.

This is why I opted for a mini-API that will, depending on the action and the element data, call a different function from a different class.

Now, to implement the API, I decided to use Chain-of-Responsibility design pattern. Why? Because I can easily add, delete or update classes without having to modify the API itself.

Is the Chain of Responsibility design pattern good fit for my case?

答:
I do not recommend using a single method to handle all the request operations. Ideally you should use DELETE to handle delete operations, thus helping you to have an API which exactly tells what the API is all about and POST, GET and so on.

I would suggest you to start defining your API's first as a starting point.

Deleting an item in the dashboard -> http://domain.com/dashboard/itemId - Delete Operation

Refreshing an item -> http://domain.com/dashboard/itemId - Get operation

See, the API is self explanatory, and would give a clear picture of what you want to do as well as help in maintenance.

In your controller you can detect the operation and then call respective services for each operation to take an action. COR may not be a good option, as it does not fit in here.

ContentLicense: CC BY-SA 3.0


问:
Database is performing slow, even all the tables are having normalization
An interviewer asked me this question:

Tables are created with appropriate normalization rules, However the database is performing slow. [Ie.: The select, insert statements are taking time to do his operation.] What are areas we need to look to improve the database performance.

Obviously this is a vague question. What sorts of things might be wrong with a database that is running slowly, even when normalized?

答:
Sounds to me that your interviewer was not looking for a data scientist answer but was simply looking to make sure you understand that "normalization" != "performance".  So I'll keep this answer at the level that I'm guessing he wanted.

Normalization means minimizing redundancy in stored data.  Instead you setup relationships (often with foreign constraints) between multiple tables.  However, while normalization might lead to a smaller amount of stored data, often it creates performance problems because now many queries end up joining multiple tables.  Same thing with adding data where you might now have to update multiple tables at once.

Often, speed gains could be achieved by de-normalizing the data.  You are storing more and there might be duplicates, but when it comes to running most frequently used queries, all your data would now be in one table.  Getting results from one table is usually much easier on the hardware than having to join multiple tables


问:
Overcoming slow problem solving due to increased knowledge of what might go wrong
This has been troubling me for some time, and I'd really appreciate the input of other professionals.

Short background: I started programming when my parents bought me my first computer in 1988 (at age 14, I'm 39 now). I followed a couple of other career paths before finally becoming a professional programmer in 1997. Late bloomer, perhaps, but that's how it was. I'm still happy with my choice, I love programming, and I consider myself good at what I do.

Lately, I've been noticing that the more experience I gain, the longer it takes me to complete projects, or certain tasks in a project. I'm not going senile yet. It's just that I've seen so many different ways in which things can go wrong. And the potential pitfalls and gotchas that I know about and remember are just getting more and more.

Trivial example: it used to be just "okay, write a file here". Now I'm worrying about permissions, locking, concurrency, atomic operations, indirection/frameworks, different file systems, number of files in a directory, predictable temp file names, the quality of randomness in my PRNG, power shortages in the middle of any operation, an understandable API for what I'm doing, proper documentation, etc etc etc.

In short, the problems have long since moved from "how do I do this" to "what's the best/safest way of doing it".

The upshot is that it takes me longer to finish a project than a novice. My version may be rock solid, and as impenetrable as I know how to make it, but it takes longer.

The "create file" example above was just that, an example. Real tasks are obviously more complex, but less suited for a generic question like this one. I hope you understand where I'm going with this. I have no problem coming up with efficient algorithms, I love math, I enjoy complex subjects, I have no difficulties with concentration. I think I do have a problem with experience, and consequently with a fear of errors (intrinsic or extrinsic).

I spend almost two hours a day reading up on new developments, new techniques, languages, platforms, security vulnerabilities, and so on. The conundrum is that the more knowledge I gain, the slower I am in completing projects.

How do you deal with this?

答:
@Zilk, I am not great programmer and I am been programming since 1998.  Even I am facing this issue now.  But what I realized is ultimately quality matters.  If I die today, somebody should be able to pickup what I am doing now from where I have left.  Such should be the standards of programming (Universal).

I have moved myself from developer to architect now.  Moving to Management is changing the line.  If you want to continue with your passion you can move to become Architect.

Initially as Technical Architect-->Solution Architect-->Enterprise Architect-->Chief Architect and so on.

As an Architect you will be guiding people to success.  People like you who have been programming for decades those years of experience you can utilize to guide others.

Like a bird higher it flies more land it can see so is your experience.

Let me also tell you programming correct implementation is important than programming a wrong implementation faster.  Recently one of my juniors programmed something wrong and it cost a bank lots of money.  Ofcourse we had delivered on time earlier but that was no use!  Was I given the role to guide even though the same junior would have coded that problem would not have happened.  I am giving this example to stress that giving good guidance is also important.  Some call this job as Consultancy.


问:
good replacement for commenting out code?
It bothers me to see commented out code and it seems often-times a symptom of developers not being proficient in advanced source control features, but on the other hand source control does not really go all the way in solving the problem that commenting out is intended for.  I've occasionally seen requirements flip-flop in such a way that code that would have been deleted suddenly is needed again.  If the code is really deleted, there doesn't seem to be a clear procedure for locating exactly where to find that deleted code in source control.

Is there a better way out there to save developer work that may be needed again besides the tried-and-true "commenting out"?

答:
I would like to extend the idea of the now deleted answer of @MathewFoscarini.

Create an abstraction (an interface in the simplest case) around the code in question. Make the code in question an implementation of it and create a new implementation that matches with the new requirements. If needed move the old implementation to it's own module, so that it won't be packaged for production.

To summarize: you are running into a violation of the open closed principle, because you are often changing the implementation details of a class. To solve this you need to introduce a common interface and configure your application to use the correct implementation.


答:
Some good answers here already, but I would like to add one point.

I've occasionally seen requirements flip-flop in such a way that code that would have been deleted suddenly is needed again.

That is the point you should work on: better identifying the requirements and the reason why they change. If they change because the first analysis was not proper, or your client needed some prototypes until he understands what he really wants, the chances are high that they will stabilize after some iterations. Then it gets obvious that you can delete the older code and keep only the code which does what it should.

If, however, your requirements change because its part of the business (for example, requirement A is for January, March and June, and B for February, April and July), then it might be probably wrong to support only just one implementation - in such case, your application may better support those different requirements at once, either by supporting them in a generalized way, or by allowing some customizing or configuration. In such a case, all your code stays in the application, but not in an outcommented part, but in an active part.

The latter can also be a solution if you have to deal with changing requirements in a prototyping phase as long as the knowledge about the real requirements has not stabilized. In this case, a feature toggle may be a feasible solution.

Title: good replacement for commenting out code?

Body:
It bothers me to see commented out code and it seems often-times a symptom of developers not being proficient in advanced source control features, but on the other hand source control does not really go all the way in solving the problem that commenting out is intended for.  I've occasionally seen requirements flip-flop in such a way that code that would have been deleted suddenly is needed again.  If the code is really deleted, there doesn't seem to be a clear procedure for locating exactly where to find that deleted code in source control.

Is there a better way out there to save developer work that may be needed again besides the tried-and-true "commenting out"?


问:
Control flow of PHP scripts
I have a PHP script example1.php.

How would the control flow be when I call another PHP script example2.php from example1.php?

For e.g.:

```<code> example1.php looks like this
 ....
 ...
 ....
 example2.php (calling example2.php from example1.php)
 .....
 .....
</code>```

How does the control flow work for this? Does example1.php wait until example2.php completes execution and then continuous with rest of code logic or does it just continues allowing example2.php to run independently?

答:
The parsing is done line by line in php.

So it will go sequentially.

Yes example1.php will wait until it completes execution of example2.php

Title: Control flow of PHP scripts


答:
Only thing I can see is: "You are becoming more and more valuable".

As and when you get more experience you learn about things you should avoid, and this is what make you better than others.

One thing you would have noticed that your code would be safer and more maintainable now.

Only thing you need to do is to explain your client why it took time and how it would be useful for them.

You need to show them depth of your knowledge.

You need to tell them why you did, what you did and how it would matter them and their business.

My suggestion would be to concentrate on this part.

Title: Overcoming slow problem solving due to increased knowledge of what might go wrong

Body:
This has been troubling me for some time, and I'd really appreciate the input of other professionals.

Short background: I started programming when my parents bought me my first computer in 1988 (at age 14, I'm 39 now). I followed a couple of other career paths before finally becoming a professional programmer in 1997. Late bloomer, perhaps, but that's how it was. I'm still happy with my choice, I love programming, and I consider myself good at what I do.

Lately, I've been noticing that the more experience I gain, the longer it takes me to complete projects, or certain tasks in a project. I'm not going senile yet. It's just that I've seen so many different ways in which things can go wrong. And the potential pitfalls and gotchas that I know about and remember are just getting more and more.

Trivial example: it used to be just "okay, write a file here". Now I'm worrying about permissions, locking, concurrency, atomic operations, indirection/frameworks, different file systems, number of files in a directory, predictable temp file names, the quality of randomness in my PRNG, power shortages in the middle of any operation, an understandable API for what I'm doing, proper documentation, etc etc etc.

In short, the problems have long since moved from "how do I do this" to "what's the best/safest way of doing it".

The upshot is that it takes me longer to finish a project than a novice. My version may be rock solid, and as impenetrable as I know how to make it, but it takes longer.

The "create file" example above was just that, an example. Real tasks are obviously more complex, but less suited for a generic question like this one. I hope you understand where I'm going with this. I have no problem coming up with efficient algorithms, I love math, I enjoy complex subjects, I have no difficulties with concentration. I think I do have a problem with experience, and consequently with a fear of errors (intrinsic or extrinsic).

I spend almost two hours a day reading up on new developments, new techniques, languages, platforms, security vulnerabilities, and so on. The conundrum is that the more knowledge I gain, the slower I am in completing projects.

How do you deal with this?


答:
I think you need to learn to decide how much needs to be done for which project. Some project may be trivial and you really don't need to spend all that time in perfecting it. Not everything is gonna need rock-solid encryption nor everything will be scaling to million users.

Writing a program which can scale well for more than a million users will take time and experience which you have now, but if you know that your application is not going to be used by more than 1000 users max, there is no point in spending all that time perfecting it.

I think this is an important stage in your programming career and now you need to goto next level, which has to do with maturity and not programming. You need to be able to correctly decide on how much time and code should be enough for any particular project.

And like everything else, you can attain this as well with practice. So try to decide this before you start a project, sometime even while you are already working on it, with experience you will get past that as well. There may be a few hits and misses in the beginning but with time you will perfect it (decision-making, not code).


问:
Good or bad code? Or "a secret reason"?
I think this code:

```<code>if(file_exists("amodule.inc.php"))
    require_once("amodule.inc.php");
</code>```

is misleading because of the use of the require_once.

I think that - to keep the logic and "wording" in line - "include_once" would be appropriate (where there are a number of arguments to not use the "if" at all but I would like to concentrate on "require vs include").

As far as my understanding goes, the ONLY difference between "require" and "include" is that "require has a consequence (halt) if the file does not exist vs. include proceeds with just a warning.

But, in the example, if the file does not exists, the require_once code will not executed anyways. Therefore the "require" misleads from my point of view.

From a superficial view, one could argue that the above code using require_once and

```<code>if(file_exists("amodule.inc.php"))
    include_once("amodule.inc.php");
</code>```

are "identical" what, from my view, is not. Because:

A "rough" analysis (like an automated check of a project) would throw a message, that "amodule.inc.php" is a vital project file, which the code shows, is not.

Also, in a (not very likely) cas that between the execution of the "if" statement and the require_once statement, the file could be deleted. Then, even worse, the code would NOT execute like expected (to run without load) but give up.

So, how would you guys out there argue?

答:
The fact that a file exists does not mean it is readable, that's a first issue:

http://php.net/manual/en/function.is-readable.php

With includes some other strange things happen, see for example:

http://php.net/file_exists

Warning
  This function returns FALSE for files inaccessible due to safe mode restrictions. However these files still can be included if they are located in safe_mode_include_dir.

So the check of file_exists is the most well-known, but maybe not the most right method, to see if you can include a file.

This seems to be the right solution:

http://php.net/stream_resolve_include_path

Resolve filename against the include path according to the same rules as fopen()/ include.

That really checks whether you can include the file.

Then: Should you require it: Yes in most cases. If a class file exists and is includable than the loading should succeed. If php files cannot be loaded correct there is an, in my opinion, big issue with server / software which we would call fatal. So, for us it is valid to use require_once.

OwnerDisplayName: jwka

Title: Good or bad code? Or "a secret reason"?


问:
why javascript is widely used scripting language in web applications?
I cant find plugins written in other languages other than javascript. There is browser support only for Javascript. Why didn't they use other scripting languages other than Javascript when people started using scripts on their web pages? Why javascript was given priority?</p>

答:
it was the first client side scripting language introduced by netscape in the browser war in '95 during the time it was dominant, submitted for standardization in '96 (and standardized a year later).

Microsoft just couldn't get a properly competing scripting language out fast/popular enough so they just adopted it as JScript.

Any other browser just didn't have the market to introduce their own scripting language and were forced to adopt javascript to stay competitive.


问:
Two or one git repo for Android app + web service backend?
I'm wondering what the best practice is for my git repository structure where I have an Android application + a web service backend. In general, I'd like to release these two independently (e.g., perf fixes to the backend with no changes to the front-end), but I am worried that if I keep these in two separate repos I'll lose the ability to see combined app + web service changes (e.g., there's a change in the APIs on the server).

What are other Android developers doing to manage their source code + release process? Should I use one git repo with separate folders or two git repos?

I should note that I'd like to set this up as push-to-deploy -- any changes to my master branch will trigger a tag and deploy.

答:
2 different repos.

Any changes to API you make will have to be backwards compatible anyway, so you really don't want to be changing the API if you can avoid it.

Two repos reflects reality much better; that any old version of the app could be accessing the current version of the API, and you have to make sure that all works.

If you only use 1 repo you might start thinking "THIS version of the app is accessing THIS version of the API" and that's not what really happens.


问:
How can I test an application that depends on specific hardware
I have written an Android application. The application requires a Bluetooth connection to a very specific piece of hardware, currently a prototype. The only way I can test the app at present is to drive hundreds of miles to see if it works. If I end up with an ANR it can be very embarrassing in front of my client. The connection type and the mechanics of this app are very tightly tailored to this hardware. I am not permitted to bring a laptop, root my phone, or connect to any other device on the premises.

Is there any way to test this application at home, or step through the activities to ensure I don't have another ANR on the second activity while the client looks at me horrified?

I should point out I am not an experienced programmer and this is a new idea that I am helping someone with, as hiring staff is not on the cards at present, so I am undoubtedly missing some of the very basic concepts of developing this.

答:
As @MrSmith42 suggested, you should do the best you can to mock the interface.  You don't need an actual bluetooth connection in order to mock it.  Call an interface.  That interface, in debug, sends what you'd expect for a given input.  When not in debug, it performs the actual work of sending and receiving the data.  You be sure that what you send to the interface and how you handle its output works.  Sprinkle a generous bit of error handling, managing errors you can manage and logging the rest, and you have yourself something you can test directly at the client.

The only possibility for error at that point lies in the difference between how you expect the device to handle and how it actually behaves, and in my experience, hardware is never entirely reliable.  So do be sure to have a blanket error handling mechanism for something you don't expect that would at least dump some sort of log.


答:
I had the (likely) same problem many years ago, it lasted for a few years and I overcame it. So maybe it would be of some interest to you to know how I achieved that, even if I'm not sure my way will also apply to you.

You should also have a look here : The Seven Stages of Expertise in Software Engineering It shows that productivity is in great part a side effect of skill level. It may be that you are still at some point between stage 3 and stage 4 on the technology you're currently using (skill proficiency depends on technology, you can be master of some technologies while still learning others).

Now I start with biographic testimony.

A bit of context. I'm 47. I started programming at 12 in the 80's. While in high school I also worked as a part time professional game programmer. Basically it didn't got me that much money, just enough to buy hardware, but I enjoyed it and learned much. At 18 I started a formal learning of Computer Science.

As a result when I turned 20, whenever starting any programming task I knew many ways to solve the given problems and was very conscious of the many parameters and pitfalls at hands, and drawbacks and limits of any method.

At some points (say about 26 years old) it became really difficult for me to write any program at all. There was so many possibilities opened that I was not able any more to choose between them. For a few years (make it 6) I even stopped programming and became a technical news-writer.

I never totally stopped trying to program nevertheless. And at some point it came back. The key for me was extreme Programming, more specifically the Simplicity principle: "Write The Simplest Thing That Could Possibly Work".

Basically I forced myself not to care about code efficiency (that was my main roadblock, avoid inefficient designs), but just go the easiest way. I also forced me to care less about errors, and delay error handling to a later time, after writing tests raising the error (really that's TDD).

That's something I learned when I was writing. When I do not know what to write, or I knew what I was writing was bad. Just go on. Actually write the bad stuff. I will eventually correct it later. Or if it's really that bad erase it and rewrite it, but it's faster to write things two times that write anything perfect the first time.

Really it is very likely that a code you believe is good at first writing will need as much improvement as a really bad one.

If you follow the Simplicity path you also get an added bonus. You easily accept to remove/change initial design/coding. You get a more flexible mind.

I also got in the habit to put a temporary comment in code, explaining what I was not doing now and intended to do later when the code would be functional in the normal use case.

I also attended an XP Dojo an practiced code katas with other programmers to internalize the XP practices. It helped.  It made the above formal methods instinctive. Pair programming also helped. Working with young programmers gives some momentum, but with experience you also see what they do not. For instance in my case I often see them engage in overly complicated designs and I know the design nightmare that may lead to. Gone that way. Did that. Had problems.

The paramount point for me is keeping the flow. Being fast is really succeeding in keeping the flow.

Now I'm back as a professional programmer and I believe I'm both better and faster with a deeper understanding of what I'm doing. Practicing TDD I may still be slightly slower than when I was a young bull (and tested nothing), but I also have no fear refactoring and certainly spend much less time debugging (nearly no time at all, make it less than 10% of time).

Summarily: I overcame my codeblock using agile methods (XP), going for simplicity then refactoring, and practicing to make that instinctive. It worked for me. Not sure it can be generalized to anyone else.

In terms of skill acquisition level, I mostly learned to accept that every time I change technology (learn new language, new framework, etc.) I will go through a stage when I'm slowing down. This is normal and will eventually overcome that. I also can compensate for that with good methodology and general purpose programming skills and It won't be as much a problem.


答:
I think this is a project management issue, not so much a software development issue.

You should have a week or two with access to the device (on your client's premises or at your office) to evaluate the basic behavior of the device and to see how it interacts with your software.
Afterwards you can mock the interface and develop the functionality at home and again a week of integration tests.

When you interact with hardware the need and the value of integration tests cannot be underestimated. If integration tests are not possible, your project will fail. Programming blindly towards a hardware you cannot test does not work.

You have to discuss this issue with your client.

Title: How can I test an application that depends on specific hardware

Body:
I have written an Android application. The application requires a Bluetooth connection to a very specific piece of hardware, currently a prototype. The only way I can test the app at present is to drive hundreds of miles to see if it works. If I end up with an ANR it can be very embarrassing in front of my client. The connection type and the mechanics of this app are very tightly tailored to this hardware. I am not permitted to bring a laptop, root my phone, or connect to any other device on the premises.

Is there any way to test this application at home, or step through the activities to ensure I don't have another ANR on the second activity while the client looks at me horrified?

I should point out I am not an experienced programmer and this is a new idea that I am helping someone with, as hiring staff is not on the cards at present, so I am undoubtedly missing some of the very basic concepts of developing this.


答:
I think there were two major factors among many:

Danish programmer Lars Bak changed the landscape of the internet when his team at Google engineered Chrome's V8 runtime.

Steve Jobs.

There were a bunch of other scripting languages; Oracle created wild applets so you could use Java.

I remember ActionScript being way ahead of JavaScript back when it first dropped, and I think later JavaScript basically copied ActionScript 2.

Remember when all you had to do was click here and wait while Flash was installed? Back then it was the only way cool things could happen, Flash's ActionScript became the true mark of high quality browser development with cutting edge UI/UX.

JavaScript was terrible then, ECMAScript 3 was a major improvement, but Microsoft was way too big to care about anyone's ideas on standards.

Suddenly the execution environment sped up dramatically with V8 and the browser wars had begun. As FireFox, Chrome, Safari, and many others fought it out, the execution time was under constant re-optimization.

One day, Steve Jobs woke up and decided to kill Flash.

So yea, probably Steve Jobs and Lars Bak were the two major reasons I would say.

Title: why javascript is widely used scripting language in web applications?

Body: I cant find plugins written in other languages other than javascript. There is browser support only for Javascript. Why didn't they use other scripting languages other than Javascript when people started using scripts on their web pages? Why javascript was given priority?</p>


答: Knowing all the possible criteria's while developing app is the most important thing in developing a quality product. You are doing good in this. One thing you can do is, You can categorize the requirement into quality level then map the level with the given deadline. In this way to you can meet the project deadline easliy.</p>


问:
What exactly does it mean that storing “large blobs in the database reduces performance”?
To someone who knows database internals this may be an easy question, but can someone explain in a clear way why storing large blobs (say 400 MB movies) in the database is supposed to decrease performance and what exactly does that mean? This is a claim often found throughout the internet, but I've never seen it really explained.

To be specific, I'm referring to SharePoint/MSSQL performance, i.e. file-upload performance, site browsing, displaying lists, document opening etc. - operations that are said to become slower once a database gets too big. Blob externalization to filesystem (which in SharePoint is called Remote Blob Storage, aka moving files out of the database, leaving only a reference) is supposed to solve this to an extent, but what exactly - at the bottom level - is the difference? It's obvious that backups would take longer with giant files stored in the database ... but what operations exactly are impacted and what's the underlying mechanism of it (i.e. in what way are files stored on filesystem outside of the database accessed or stored differently)?

Suppose a simple table containing columns ID(guid, PK), FileName(string), Data(varbinary(max)) - would large Data column really slow down operations such as displaying a list of files on a website (which I assume internally means running SELECT FileName FROM table), or inserting a new row? It's not like the actual binary content columns are indexed.

I know there have been some questions like this asked already, but I've not found an adequate explanation.

答:
This really depends on the DB system, but one major thing you have to consider with BLOBs is transaction processing. By externalization to the filesystem, one takes changes to the binary data out of the transactions. That will typically result in faster write operations, opposed to the situation where the DB assures you ACID compliance with full rollback mechanisms etc.

Slower read operations hypothetically can also occur, when you retrieve data from your db from a BLOB table without actually selecting the BLOB data, since the DB may store the remaining rows more localized on disk, which will allow faster read access (but I guess most modern DB sytems are clever enough to store the actual binary data in a separated disk area or table space, so without testing this with a real world scenario one should not make any general assumptions here).


问:
Properly licensing FOSS: text location
I have a few FOSS projects that I want to release under the BSD license, since I only wish to maintain the intellectual property of the code, that others will know I've been writing it.

I suppose all I need to do is:

Have a file called LICENSE only in the main folder of the software.

Write at the top of each source file the following text:

This software is released under the BSD License.

  Full text of the
  license can be found in the file named 'LICENSE' at the main folder of
  the software.

  Copyright (c) 2013, Dor.

Am I correct? Is there anything to add/remove/change?

Should I include my full name,

or may I just use the first name and first letter of last name (e.g. Dor K.)?

The BSD license text that's contained in the file LICENSE is:

```------------------------------------------------------------------------
This software is released under the BSD License
------------------------------------------------------------------------
Copyright (c) 2013, Dor K.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
------------------------------------------------------------------------```

答: Adding a license file and the license header to each source file should be okay though I would add my full name to the Copyright notice.</p>

Title: Properly licensing FOSS: text location


答:
An important part of programming is managing and controlling complexity and for me personally, it is one of the top issues. If ignored then either the frequency of deficiencies surges or, as in your case, the ETA of finished software increases dramatically.

Software complexity can be controlled and managed from many different levels and ways but a good rule of thumb to get some perspective is this: "Top priority of any software project is customer satisfaction which is a function of their expectations."

In other words, software complexity depends a great deal on how skilled you are at controlling the expectations of your customer.

When one adopts that view, then two important points become obvious:

customer expectations must be made explicit (in whatever form);

customer expectations can always be modified and that is done through the art of negotiation.

Your example is a very good one, "simply write it" vs "myriad of other considerations". Think about it - if someone were to write down exhaustive requirements for both variants, can there be an equivalence in described features or not?

Building an F16 is different from building a Cessna although both of them can fly.


答:
It sounds like your skills would be very useful for very high quality mission critical systems development, like finance/trading related applications, broadcasting, aerospace, defense...

Errors in these sort of applications are very costly and they employ people who think like you as you can cover all the cases.


问:
How to code review without offending other developers
I work on a team that does frequent code reviews. But it seems like more of a formality than anything.

No one really points out problems in the code for fear of offending other developers. The few times I've tried to ask for changes were met with very defensive and reluctant attitudes.

This is of course not good. Not only are we spending the time to code review, but we're getting literally zero value from it. Is this an issue that needs to be addressed by individual developers, or are there techniques for suggesting changes without stepping on other people's toes?

答:
This seems to be a pretty common prevailing attitude among some developers. Everyone seems to feel that a code review is some challenge to their work, and that makes no sense to me. A code review is a quality assurance mechanism that has the added bonus of education to go along with it. We implement code reviews extensively where I work, and I've fostered within my own team the attitude that the code reviews are a collaboration mechanism more than a quality process.

The only way to begin coding as a team is to see each other's work and to question it. That's how best practices are formed. Dialog is the key. I've sent code back to developers for silly reasons such as formatting, agreed upon best practices, and spelling. I code review with a very fine edge, and I expect my own code to withstand the same scrutiny. I've used tactics such as checking in code that would not pass my own code review for the sole purpose of getting a junior developer to challenge me. Tell me it doesn't meet the requirements. Stand up and have an opinion.

Some guidelines I think everyone should really have when engaged in a code reviewing environment:

Get over yourself. You're not perfect, and just because a junior developer managed to see you do something silly in a for loop isn't the end of the world. If you're so great, invite criticism and prove it. Expect code reviews to reveal new ideas on how to do things, new habits, and, most of all, constructive dialog.

Keep an open mind when reviewing someone else's code. Be receptive to their ideas, and make sure that changes you suggest are suggested for a reason. It is assumed that the checked-in code builds, but that doesn't mean that it follows best practices. Make sure that anything you bring up can be backed up with a cited reference. If you say it doesn't follow best practices then cite the standards document and section. If you say it isn't a "performant" method, then have a link to a document that shows why and possibly provides metrics.

Make suggestions useful, and explain why you're suggesting something. You will occasionally find a problem with code that is self explanatory, but most of the time this person has coded something based on habits. Explain why this habit should be altered and the value of altering it (unless you're explaining it for the 5th time, in which you have a personnel problem).

When your code is reviewed, consider everything the reviewer is suggesting objectively. If you're pushing back, ask yourself honestly if you're just being defensive or if you really believe you have a case. If you have a case, continue the dialog. Don't get argumentative, bring ammunition such as facts and metrics.

Whether you're a reviewer or a reviewee, use code reviews as an opportunity to educate. Whether it's educating yourself or the person you're reviewing, if there is a discrepancy then there is a chance to learn somewhere. Make good use of it.

Ask questions and be ready to have your questions answered truthfully. I recently made a statement that was "pseudo-true". It wasn't wrong, but it definitely wasn't right. A junior developer challenged me on it, and I disagreed. My response was "I haven't seen that behavior, but if you can find me a document on it I would love to read it.". I spent about an hour that afternoon reading the document he sent me, and now I have a much better response (re: educated) when confronted with a similar situation.

Given the education slant that I put code reviews in, I often make my responses about questions. If I find something blatantly wonky, I will instruct the developer to correct it. Otherwise I will ask the questions. "Why did you use method A to achieve goal B?" "What gain does declaring a variable have in the instance of its usage in method Z?" "I see you have copied/pasted some code, did you consider refactoring? You didn't refactor, what was the reasoning behind that choice?" The code doesn't progress until the reviewer approves it, and the reviewer won't approve it until the questions are answered. When framed in an inquisitive way that indicates you don't understand the developers reasoning it becomes less confrontational and takes on more of an instructional vibe.

OwnerDisplayName: Joel Etherton


答:
In order to help people take a code review less personally, try to target criticisms and suggestions to the code itself and not its author.
For example,

The function "getFoo" should do xyz because....

is better than

You should do xyz in function "getFoo" because...

On the other hand, if you want to express a positive opinion you can praise the author. For example,

I like how you used abc algorithm because ...

Title: How to code review without offending other developers

Body:
I work on a team that does frequent code reviews. But it seems like more of a formality than anything.

No one really points out problems in the code for fear of offending other developers. The few times I've tried to ask for changes were met with very defensive and reluctant attitudes.

This is of course not good. Not only are we spending the time to code review, but we're getting literally zero value from it. Is this an issue that needs to be addressed by individual developers, or are there techniques for suggesting changes without stepping on other people's toes?

OwnerDisplayName: DHall


答:
First, the team should agree on the goal of the code review (you should not take such agreement for granted, as code review can be used for a variety of things, and different organisations do it for different reasons. For this answer, I will assume that the goal is to improve a developer's future work.

To improve, the developer needs to know

what was good? -> I should continue doing that

what was bad? -> I should change that

and why? -> So I can assess the severity of the issue, as well as recognize similar issues in the future

What action is this about?

Feedback should be about a behavior, not a person. Therefore, it should identify (preferably with examples) the behavior the feedback is about. So don't say:

You're lazy.

because that isn't about behaviour, but (perceived) personality. Also, it's too vague: Is this about the long lunch break? Slow typing? Not answering email in a timely manner? Too few implemented features? Instead, you should say:

Last week, you were assigned bug #1059. You have marked this defect resolved, but the bug still exists, and can be reproduced with the instructions provided in the trouble ticket.

And Why?

Next, you should describe why that action or code was good or bad. The reason given should be objective, and of sufficient importance to justify a change. For instance:

AClass.amethod() is invoked by concurrent threads, but not thread safe. Data corruption is likely.

or

This method name is misleading. It sounds as if we're merely calulating the pay grade, but actually also pays the employee. Somebody not familiar with the implementation is likely to misuse the method, causing accidental salary payments ...

Of course, sometimes you don't have an ironclad argument why the code is bad (or good). This means you don't know you're right. Therefore it is prudent to be inquisitive, rather than presumptious:

This looks awfully complex. Why don't you just invoke AClass.amethod() instead?

or

This appears to be nearly identical to AClass.amethod(). Why can we not reuse that method?

You can also give positive feedback, of course:

That's a great approach to coordinate the worker threads! I usually do X instead, and I always wondered how I could make it more robust. Now I know :-)

Please don't do this

To illustrate the importance of the above, let's consider a real world counter example taken verbatim from a formal code review:

The code leaves a poorly thought out impression.

And that's all the reviewer wrote about that finding.

That is terrible feedback on so many levels:

The feedback implies something unflattering about the author (namely, that he thinks poorly)

The feedback is about an impression. That is not objective.

The feedback does not identify any part of the code that needs to be changed,

nor give any reason why it should be.

The recipient of such "feedback" can not know what he needs to change, nor why, and thus not learn anything. In fact, he can not even know that the feedback is fair, and, given the unflattering unsubstantiated conclusion, is far more likely to see this as a personal attack by somebody who wants to elevate himself by pushing his peers down. Shouting is likely to ensue.

OwnerDisplayName: meriton


答:
A code review shouldn't just be a fail/pass with only fail returning detailed feedback. This tends to focus on the negative and turns people off from exposing themselves to the criticism.

Reviewers might also give a more negative response to get more limelight in the eyes of the manager in terms of "look I'm doing my job and criticizing everything that comes across my desk"

If you add a comment field that is returned to the submitter fail or pass then he'll feel more positive about his work and the reviewer can still give feedback about what could still be better even if the code passes the review. This also would allow the reviewer to do a limited refactor of the code (formatting, renaming variables) without needing to bother the developer for the trivial changes besides notifying him of them.

OwnerDisplayName: ratchet freak


问:
Using algorithms in papers in my code
For example a new research paper comes out and it describes some algorithm, say this is a cutting edge algorithm and came out last week, is there anything stopping me from using that algorithm in my codebase that is for commercial purposes?

What if the paper includes some code and I use that code? There does not seem to be any information about code licensing on the paper so I am not sure how this is meant to work.

答:
You are free to use it unless it is a patented or copyrighted work. Usually such work won't be published, or not all necessary reproducing the work details will be available in the publication.
However, one must follow the code of ethics and cite the publication in their work to avoid plagiarism. I wouldn't encourage using of the same code myself.
However, there could be legal issues associated with this, and this is not a right place to ask this question.</p>

Title: Using algorithms in papers in my code


答:
In difference to the given and accepted answer, just to prevent some poor souls who run over this question to not do wrong decisions too easily:

No, you can not use that code until you have clarified the licensing terms. So for you, as you  don't have any licensing terms, all you can assume is that there are "all rights reserved".

You solve that by trying to find out who the copyright holder of that code-fragment actually and then ask for the licensing. The person who wrote the paper probably is able to tell you. I can not say to you if she is willing though to tell you.

What if the paper includes some code and I use that code?

You have copied that code. You also have changed the work, you translated it from a printed work into a digital file on your computer most likely.

There does not seem to be any information about code licensing on the paper so I am not sure how this is meant to work.

Well, the reproduction of a work  (for example translation) is only allowed if the copyright holder granted you a (at least limited) right to do so.

As you write that there is no information about the code-licensing given, then you perhaps (I don't know so I can only assume) have no right at all to "use" that code (if using means to type it from the paper into your project).

For the code licensing itself, it's perhaps only used in that publication to give an example, a quote from an existing copyrighted work, used under fair-use. No information must be given about it, and you can safely assume that there is all rights reserved on anything you see there unless you know otherwise. Which is not the case, so clarify with author(s) and copyright-holder(s) first.

Title: Using algorithms in papers in my code

Body:
For example a new research paper comes out and it describes some algorithm, say this is a cutting edge algorithm and came out last week, is there anything stopping me from using that algorithm in my codebase that is for commercial purposes?

What if the paper includes some code and I use that code? There does not seem to be any information about code licensing on the paper so I am not sure how this is meant to work.

OwnerDisplayName: hakre


问:
Optional semicolons
Most often, in a general-purpose imperative language - semicolons as statement-delimiters are either required, or completely disallowed (e.g. C and Python).

However, some languages, like JavaScript, allow you to opt out of delimiting your statements with semicolons, in favor of other delimiters (such as a newline).

What are the design decisions behind this? I understand that semicolons are essential when writing multiple statements on the same line, but is there another reason to make them mandatory (except following C)?

答: All design and construction aside, I believe that a lot of programmers come from different backgrounds and some learned to use the semi-colon and some didn't. A lot of newer languages that are emerging aren't requiring a semi-colon but still allow it to exist. I think it might just be a way of getting more programmers to learn how to code in these new languages without having to give up their habits from when they began.</p>

Title: Optional semicolons


问:
Staging environment vs Production environment
I work for a company where we build enterprise applications, and we maintain three environments: development (or dev), staging (or stage) and production (or prod).

The meaning of dev is intuitive: it's the environment used during development of the application.

What's the difference between staging and production environments?

答:
My experience with US Government/Department of Defense IT is that:

DEVELOPMENT/TEST is for developers and application developers. They build and test applications away from enterprise systems.

When the applications are completed, they will be deployed to the IT environment into the PACKAGING server(s).

From that point on, the applications are supported by IT, migrated into STAGING (which enables IT to develop/check implementation guidelines, configurations, etc.). User testing may happen at this stage to some degree to verify that the application will function as expected in a system that replicates PRODUCTION.

If all of the test plans are completed and they are good to go the application will be moved to PRODUCTION and go into the sustainment (i.e., operations and maintenance, or O&M) mode.

New updates from the developers will also be deployed into PACKAGING and the process continues.


问:
Deploying Qt-based app on Mac OS X
I wrote a cross platform application and I distributed it successfully for Windows and Linux (and tested them on "clean" virtual systems, with no other additional software and/or libraries installed).

I have already compiled my application on my Mac computer, but I don't know if my application will work on other Mac computers.

I don't have two Macs, also, (as far as I know) it's difficult to install Mac OS X on a virtual machine with "legal" discs or without having to perform a process similar to building a Hackintosh, this means that I cannot test my app on a virtual machine (in VirtualBox).

So may question would be: Is there any way of making sure that my application will actually run on other Mac systems (without the need of using another computer)?

As an additional note, I wrote my application by using the Qt framework.

答:
You can use a VM running on your Mac. I've done this with Parallels: http://kb.parallels.com/en/112121

If I understand correctly, you have a Mac on which you have compiled the app but you want a clean environment to test deployment. So if you set up a VM on that same Mac that you compiled on, you will have a clean OSX to test with.

Title: Deploying Qt-based app on Mac OS X


答:
Netscape made it to give their web browser an advantage, and then it stuck.

From this video by Douglas Crockford: Why the syntax is the way it is, why it's called JavaScript, why Microsoft copied it, and why some of the bad parts are there. (I didn't copy in the parts about ECMAScript, but that's an interesting story as well):

One of the things [Netscape] wanted to do was to put interactivity
  back into the browser, because we had lost interactivity when we went
  to the browser model. [...]

In order to do that, they hired this guy, Brendan Eich, who had been
  at Silicon Graphics. Brilliant guy. In his interview he said he wanted
  to write a Scheme interpreter, and they said ‘that’s great, that’s
  just what we want’. After they hired him they found out what Scheme
  was, and they said ‘no, no, no, you can’t do that. People won’t like
  that. Do something that looks more like Visual BASIC, or Java,
  something people like.’  [...]

Brendan took elements of all three of these languages [Java, Scheme,
  and Self], and a little bit of his own, and put them together into a
  new language that was called LiveScript. [...]

LiveScript was going to become one of the key technologies for
  Netscape going forward. It was going to be in Netscape Navigator 2, so
  you could have LiveScript applications running on the client-side, and
  on the server-side; Netscape’s LiveWire server had server-side
  JavaScript in it. This was back in ’95, so JavaScript was there from
  the very beginning. It was very clear at the time that there was a lot
  of excitement about Java and the Netscape browser, and Sun and
  Netscape decided they needed to work together against Microsoft
  because if they didn’t join forces Microsoft would play them off
  against each other and they’d both lose.

The biggest point of contention in that arrangement was what to do
  with LiveScript. Sun’s position was: "Well, we’ll put Java into the
  Netscape browser, we’ll kill LiveScript, and that’ll be that." And
  Netscape said no, that they really believed in the HyperCard-like
  functionality, and they wanted a simpler programming model in order to
  capture a much larger group of programmers. So there was an impasse,
  and the relationship almost broke up, when I think Marc Andreessen -
  and I have been able to document this, but people have told me - Marc
  Andreessen, maybe as a joke, suggested: ‘let’s change the name to
  JavaScript.’

[laughter]

And it worked, except that Sun claimed ownership of the trademark.
  Even though they had nothing to do with the language and they tried to
  kill the language, they said ‘we own the trademark, but we’ll give you
  a license to use the trademark’. Netscape said ‘great, an exclusive
  license only, we can call it JavaScript, that’s fine’.

At Microsoft they’d been watching this with some alarm, particularly
  when folks at Netscape were saying that Netscape Navigator was going
  destroy Microsoft. Microsoft said ‘oh, we don’t want to be destroyed’.
  It turned out Netscape Navigator didn’t destroy Microsoft. In fact,
  the software that is going to destroy Microsoft is Windows Mobile.

[laughter]

But I’m getting ahead of the story again. What Microsoft did was they
  decided they needed to copy the Netscape model in order to be
  competitive. They reverse engineered the JavaScript engine and called
  it JScript. They couldn’t call it JavaScript because Sun owned the
  trademark, and they weren’t getting along very well with Sun at that
  time, so they called it JScript. [...]

Most languages take years to develop – for example, Smalltalk was
  eight years from Alan Kay’s first prototype to Smalltalk 80, when it
  was first made available to the public. That’s a good timeframe for a
  programming language, because you want to go through it and test it,
  make sure that it works, and refine it in order to make sure that it’s
  meeting its goals. JavaScript was prepared in about as many days. It’s
  amazing that he could get it done and designed and working in such an
  incredibly short time; in about two weeks. I challenge any language
  designer – it’s sort of like a quickfire challenge. That turns out not
  to be a good way to make software, but that’s how it was done, and
  we’re now living with the consequences of that. Had Netscape been a
  better managed company, they might have taken a lot more time, maybe a
  couple of extra weeks, to clean it up, and we wouldn’t be dealing with
  the bad parts that we have now. But we have.

The good news is that, for the most part, the bad parts can be
  avoided. And if you avoid the bad parts, and if you work just with
  what’s left over, the good parts, there’s actually a brilliant
  language there.

http://www.youtube.com/watch?v=RO1Wnu-xKoY


问:
Algorithm to update priority ordered list where changing priority of an item is expensive
I am working against an API where I can add and update items in a list that is ordered by a priority field I can set (which need not be contiguous but must be a positive integer).

Updating a list member is an expensive operation, so I want to minimise the number of updates.

What I want is an algorithm that will minimise the number of updates required when the list is reordered.

i.e. The worst case scenario for this is to use contiguous priorities starting at 1. e.g.

```<code>Priority  | item
----------------
1           A
2           B
3           C
4           D
5           E
</code>```

In this case to move item E to the top of the list, every single item would need to have its priority updated.

The naive solution I have though of is to implement large numbers for priorities, and put in priorities that are half way through the interval between two items when an item needs to be placed there.

e.g.

```<code>Priority  | item
----------------
1000000     A
2000000     B
3000000     C
4000000     D
5000000     E
</code>```

In this case to move E to the top only one update is required (and I can set its priority to 500000)

The issue with this is that it will act like a binary search, and even in the case of using starting numbers of the order of 10^9 it will only take 30 operations until the priorities could become contiguous.

Is there a better algorithm than this? Is there an existing one I can use (but have not found via google), or can someone suggest an improvement to this?

答:
If you're stuck with your list data structure

If you strive purely to minimize number of priority updates, there's not too much you can do beyond the binary-search approach.

Otherwise, one possible smallish improvement I can think of is, whenever you insert an item C between B and D, check the item A to the B and E to the right of D and redistribute their priorities.

So for something like:

```<code>        C
        ↓
A B           D E
0 1           7 8
</code>```

You'd end up with:

```<code>A   B   C   D   E
0   2   4   6   8
</code>```

Note that A and E don't change, their priorities are just required in the calculation.

Or you can leave B where it is and only update D, or vice versa, depending on whether A and D or B and E are further apart.

If you can pick another data structure

It would be better to pick one where the priority is structurally defined, not explicitly for each element.

A modified balanced binary search tree could work here, where you store the number of ancestors for each node.

The comparison for insertion would then be defined purely on where you want to insert it compared with the counts of each node.

Note that something like a red-black tree doesn't require any comparisons to keep itself balanced.

Update, insert and delete operations will all take O(log n).

Depending on your requirements, you can have a secondary data structure that allows you to look up some node for a given element.

Body:
If you're stuck with your list data structure

If you strive purely to minimize number of priority updates, there's not too much you can do beyond the binary-search approach.

Otherwise, one possible smallish improvement I can think of is, whenever you insert an item C between B and D, check the item A to the B and E to the right of D and redistribute their priorities.

So for something like:

        C
        ↓
A B           D E
0 1           7 8

You'd end up with:

A   B   C   D   E
0   2   4   6   8

Note that A and E don't change, their priorities are just required in the calculation.

Or you can leave B where it is and only update D, or vice versa, depending on whether A and D or B and E are further apart.

If you can pick another data structure

It would be better to pick one where the priority is structurally defined, not explicitly for each element.

A modified balanced binary search tree could work here, where you store the number of ancestors for each node.

The comparison for insertion would then be defined purely on where you want to insert it compared with the counts of each node.

Note that something like a red-black tree doesn't require any comparisons to keep itself balanced.

Update, insert and delete operations will all take O(log n).

Depending on your requirements, you can have a secondary data structure that allows you to look up some node for a given element.


答:
I would talk about how there are many things which can be done to improve performance. The first thing is always to investigate if the correct indexes are in place. Of particular concern in a normalized database is making sure FKs are indexed. Likely this would fix many performance issues.

Other things to look at would be rewriting the SQL code to use more performant techniques such as getting rid of cursors and correlated subqueries and making where clauses sargable. You would want to review the worst performing queries individually. You would also want to review queries that are frequently run (especially if multiple users run them simultaneously) as a small change in those could multiply through the system. If your worst queries are coming from an ORM, they might need to be rewritten as stored procs so they can be performance tuned.

You might also want to make sure you have a performance problem. What you might have is really a blocking problem where performant code is being blocked by other processes and has to wait.

Then you would look at hardware, if you have underpowered hardware and network connections, likely no other change is going to fix that.

In a large enterprise system, you might consider data partitioning.

Denormalization is a technique to improve performance but it is the last thing you would want to consider. First, you have the risk to the data of changing the structure that drastically. Converting the data to the new structure is something that can go very badly wrong if a mistake is made and it is more time-consuming to make this type of structural change than any of the other possible performance improvements. It would also be irresponsible to denormalize without creating triggers to make sure the data stays in synch as it is changed in the denormalized tables. This may mean selects are imporved but action queries are slower, so performance may not be imporved as much as you think. It is also a concern that in denormalizing, you may be making the tables significantly wider and that can affect performance negatively if you have wide tables.

Title: Database is performing slow, even all the tables are having normalization

Body:
An interviewer asked me this question:

Tables are created with appropriate normalization rules, However the database is performing slow. [Ie.: The select, insert statements are taking time to do his operation.] What are areas we need to look to improve the database performance.

Obviously this is a vague question. What sorts of things might be wrong with a database that is running slowly, even when normalized?


答:
You may want to look into SQL Server FileTables. The idea is to provide the best of both worlds: file system level access and performance, along with database access and integrated security and services. The database does have a performance over-head in some cases. Just compare a hard-coded HTML file on a webserver to one that has to fetch the contents from a database.

Imagine an application that didn't find storing blobs in the database was a significant limit to performance, but then the app grew to the point where it was. There's less of a coding change using FileTables. Also, you can manage the transaction at the database level and file level without a lot of coding. The file and meta data are available with SQL.

On the Windows Server, a share drive is created to access the files without using the database transaction over-head.

It's a common problem that Microsoft has attempted to handle "out of the box" with SQL Server 2012. Not a bad feature to justify an upgrade.

Title: What exactly does it mean that storing “large blobs in the database reduces performance”?

Body:
To someone who knows database internals this may be an easy question, but can someone explain in a clear way why storing large blobs (say 400 MB movies) in the database is supposed to decrease performance and what exactly does that mean? This is a claim often found throughout the internet, but I've never seen it really explained.

To be specific, I'm referring to SharePoint/MSSQL performance, i.e. file-upload performance, site browsing, displaying lists, document opening etc. - operations that are said to become slower once a database gets too big. Blob externalization to filesystem (which in SharePoint is called Remote Blob Storage, aka moving files out of the database, leaving only a reference) is supposed to solve this to an extent, but what exactly - at the bottom level - is the difference? It's obvious that backups would take longer with giant files stored in the database ... but what operations exactly are impacted and what's the underlying mechanism of it (i.e. in what way are files stored on filesystem outside of the database accessed or stored differently)?

Suppose a simple table containing columns ID(guid, PK), FileName(string), Data(varbinary(max)) - would large Data column really slow down operations such as displaying a list of files on a website (which I assume internally means running SELECT FileName FROM table), or inserting a new row? It's not like the actual binary content columns are indexed.

I know there have been some questions like this asked already, but I've not found an adequate explanation.


问:
content fingerprint algorithm
I have a database containing just simple URLs. It is as simple as it sounds for now and URL can link to a website or a document(i.e. anything parseable to text).

Now I have a simple code which inserts records to database. The problem is that website/document may be actually the same, just:

Hosted somewhere else

Not available so linked from Google Cache

Not available so linked from archive.org

Page can be republished from another source

etc...

I would like to get some kind of fingerprint of a website/document and think of a way to do so.

What I have thought of:

I can rely on title

because even if content is published somewhere else or cached on some server it will probably will have the same title. That's ok, as title is usually short and doesn't consume a lot of space. Downside - this works only on website. Maybe a filename is appropriate for documents but these can be renamed too.

I can rely on keyword count

But the problem is that I just enter URL and no keywords nor do I want to do so. It is a simple URL storage engine.

I can get a some kind of checksum of all the content

But this method would be a total guess.

SO MY QUESTION IS:
How can I fingerprint a content so later on I could identify possible duplicates ?

EDIT

I don't want to fingerprint just the title. I want to fingerprint the whole content. Content stays the same but can be hosted anywhere and it's structure(title too) can change. For documents - filename can change too. I want to fingerprint content. All the text so I can later on identify possible duplicates.

答:
The answer depends on how accurate you wish to be.

Title

Title would work with 99% of web sites, and you'd have the added advantage that it lies in the head, meaning if the server supports it, you could even only request the page header.  The disadvantage would be that, of course, you may have the same title but different pages.

Keyword

Check by keyword would have the disadvantage that two similar pages may share the same keyword count (take into consideration the possibility of finding keywords in the menu and none in the main body -- all pages with that menu are "the same" for what concerns keyword count).  Also consider that you'd have to download the entire page to do this sort of check, so it has no edge over the title method.

Checksum

Checksum would be the most accurate by far.  Perhaps even too accurate than what you're expecting.  All it takes is a server-generated timestamp and you've got a page different than the previous.  However, if this is acceptable, then you can't beat a md5 checksum.  It would also be relatively easy extract the checksum and to save in the database.

It has the added advantage that you can perform checksums on not just the html source but the images and stylesheets as well.  You could perform a "deep" check on all page resources once a week and a shallow check once a day on the html source (you'll need separate columns in your database for each check type).

Conclusion

Of all these solutions, I would probably implement the Checksum.  My database would house two new columns, one for a MD5 checksum and the other for date of page expiration.   The checksum tells me how to know if the page has changed while the page expiration would tell me with what frequency I should check it, assuming it is available.

I hope that helps, and good luck!

Title: content fingerprint algorithm


问:
C++ control reaches end of non-void function
I have a function, which looks something like this:

```<code>ClassA& ClassB::Check(int x){
    //some calculations in a loop, if a condition is met, a reference to a ClassA object is given
}
</code>```

I'm getting the "control reaches end of non-void function" error. As far as I understand, this is because, the function wont always return something, am I right?

The function is getting the mouse coordinates and checking witch objects was selected. If something was selected, then do stuff, otherwise do nothing.

What would be the best option to solve this? Should I create and return an object with some kind of flag to check against?

答: You should not be returning a reference in this case but rather a pointer.  In case there is no object to return, you should return 0.  Another option to consider is to stick with returning a reference, but if you cannot for some reason, throw an exception.</p>


问:
Overcoming slow problem solving due to increased knowledge of what might go wrong
This has been troubling me for some time, and I'd really appreciate the input of other professionals.

Short background: I started programming when my parents bought me my first computer in 1988 (at age 14, I'm 39 now). I followed a couple of other career paths before finally becoming a professional programmer in 1997. Late bloomer, perhaps, but that's how it was. I'm still happy with my choice, I love programming, and I consider myself good at what I do.

Lately, I've been noticing that the more experience I gain, the longer it takes me to complete projects, or certain tasks in a project. I'm not going senile yet. It's just that I've seen so many different ways in which things can go wrong. And the potential pitfalls and gotchas that I know about and remember are just getting more and more.

Trivial example: it used to be just "okay, write a file here". Now I'm worrying about permissions, locking, concurrency, atomic operations, indirection/frameworks, different file systems, number of files in a directory, predictable temp file names, the quality of randomness in my PRNG, power shortages in the middle of any operation, an understandable API for what I'm doing, proper documentation, etc etc etc.

In short, the problems have long since moved from "how do I do this" to "what's the best/safest way of doing it".

The upshot is that it takes me longer to finish a project than a novice. My version may be rock solid, and as impenetrable as I know how to make it, but it takes longer.

The "create file" example above was just that, an example. Real tasks are obviously more complex, but less suited for a generic question like this one. I hope you understand where I'm going with this. I have no problem coming up with efficient algorithms, I love math, I enjoy complex subjects, I have no difficulties with concentration. I think I do have a problem with experience, and consequently with a fear of errors (intrinsic or extrinsic).

I spend almost two hours a day reading up on new developments, new techniques, languages, platforms, security vulnerabilities, and so on. The conundrum is that the more knowledge I gain, the slower I am in completing projects.

How do you deal with this?

答:
I think you should stick to your coding standards, but make sure you are up-front with your clients. Many clients do not know what it takes/costs to build good software. It's part of the professional developer's job to educate them.

Whether you're agile or waterfall, you get some sort of agreement from the client about what they expect the application to do. Too many developers (OK maybe more salespeople) are guilty of sandbagging. "They didn't say they wanted a highly secured website." In most cases, it's because they weren't asked. "Do you mind if your ecommerce site gets hacked?" Of course they care and why would you build it to let anyone penetrate the security? You have to educate them.

Just make sure you're doing only what the client is paying you to do. Part of your service is your experience. Clients expect you to know the pitfalls without them having to ask. It's up to them to include the requirement. You may want to pass on clients that want something cheap.


答:
They say that "code should be owned by the team", so a review is not so much a personal attack on an individual coder, but a quality step to ensure things are understandable by the rest of the team. I'd say most places I've worked, this is understood and accepted.

However, I did interview a guy who was so against the idea of reviewing other people's code that we rejected him as a candidate! (that is - not of having his code reviewed, but doing reviews... it was weird).

That made me think a little about the problem with how some people approach reviews. I'd say a better way of doing code reviews then is to remember the first line on my answer, and build on that. To do this is quite simple: a code review stops being the perceived "someone checks out my code and tells me what I've done wrong" and becomes an opportunity for the coder to tell someone else what their code does, how it does it, and what changes they've made.

In this case, the coder will end up reviewing their own code, with an extra pair of eyes to help spot the bit they might miss. It's also a chance for the coder to explain why he did things a certain way. Such an approach is just as effective (if not more so as you have a good exchange of communication) and considerably more friendly. I don't think it would take more time considering the review should be quicker as the original coder can explain the changes directly. People should also understand the need for such a review as they are "handing over" the knowledge to another team member who might be required to maintain the code in the future.

You can enhance this by having the 'reviewer' come prepared with the original requirements for the change, and tick them off as their solution is detailed, so it would feel even less like a code review.


答:
Following the SOLID principles (especially the "S" = single responsibility), you may consider to split this up into 2 functions:

one doing the check and returning a bool to signal if the check was successful or not, and

one to request the reference to the classA object.

Currently, yur current function has two responsibilities (checking and returning a related object).

Or you rename your function to GetSelectedObject, in which case the first part of @JoelFan's answer is fine. This changes the point of view about the responsibility of that function.

Title: C++ control reaches end of non-void function

Body:
I have a function, which looks something like this:

```<code>ClassA& ClassB::Check(int x){
    //some calculations in a loop, if a condition is met, a reference to a ClassA object is given
}
</code>```

I'm getting the "control reaches end of non-void function" error. As far as I understand, this is because, the function wont always return something, am I right?

The function is getting the mouse coordinates and checking witch objects was selected. If something was selected, then do stuff, otherwise do nothing.

What would be the best option to solve this? Should I create and return an object with some kind of flag to check against?


问:
Pro/con of using Angular directives for complex form validation/ GUI manipulation
I am building a new SPA front end to replace an existing enterprise's legacy hodgepodge of systems that are outdated and in need of updating. I am new to angular, and wanted to see if the community could give me some perspective. I'll state my problem, and then ask my question.

I have to generate several series of check boxes based on data from a .js include, with data like this:

```<code>$scope.fieldMappings.investmentObjectiveMap = [
  {'id':"CAPITAL PRESERVATION", 'name':"Capital Preservation"},
  {'id':"STABLE", 'name':"Moderate"},
  {'id':"BALANCED", 'name':"Moderate Growth"},
   // etc
  {'id':"NONE", 'name':"None"}
];
</code>```

The checkboxes are created using an ng-repeat, like this:

```<code><div ng-repeat="investmentObjective in fieldMappings.investmentObjectiveMap">
 ...
</div>
</code>```

However, I needed the values represented by the checkboxes to map to a different model (not just 2-way-bound to the fieldmappings object).

To accomplish this, I created a directive, which accepts a destination array destarray which is eventually mapped to the model. I also know I need to handle some very specific gui controls, such as unchecking "None" if anything else gets checked, or checking "None" if everything else gets unchecked.

Also, "None" won't be an option in every group of checkboxes, so the directive needs to be generic enough to accept a validation function that can fiddle with the checked state of the checkbox group's inputs based on what's already clicked, but smart enough not to break if there is no option called "NONE".

I started to do that by adding an ng-click which invoked a function in the controller, but in looking around Stack Overflow, I read people saying that its bad to put DOM manipulation code inside your controller - it should go in directives. So do I need another directive?

So far: (html):

```<code><input my-checkbox-group
          type="checkbox"
          fieldobj="investmentObjective"
          ng-click="validationfunc()"
          validationfunc="clearOnNone()"
          destarray="investor.investmentObjective" />
</code>```

Directive code:

```<code>.directive("myCheckboxGroup", function () {
  return {
    restrict: "A",
    scope: {
      destarray:      "=",  // the source of all the checkbox values
      fieldobj:       "=",  // the array the values came from
      validationfunc: "&"   // the function to be called for validation (optional)
    },
    link: function (scope, elem, attrs) {
      if (scope.destarray.indexOf(scope.fieldobj.id) !== -1) {
        elem[0].checked = true;
      }
      elem.bind('click', function () {
        var index = scope.destarray.indexOf(scope.fieldobj.id);
        if (elem[0].checked) {
          if (index === -1) {
            scope.destarray.push(scope.fieldobj.id);
          }
        }
        else {
          if (index !== -1) {
            scope.destarray.splice(index, 1);
          }
        }
      });
    }
  };
})
</code>```

.js controller snippet:

```<code>.controller( 'SuitabilityCtrl', ['$scope', function ( $scope ) {
  $scope.clearOnNone = function() {
    // naughty jQuery DOM manipulation code that
    // looks at checkboxes and checks/unchecks as needed
  };
</code>```

The above code is done and works fine, except the naughty jquery code in clearOnNone(), which is why I wrote this question.

And here is my question: after ALL this, I think to myself - I could be done already if I just manually handled all this GUI logic and validation junk with jQuery written in my controller. At what point does it become foolish to write these complicated directives that future developers will have to puzzle over more than if I had just written jQuery code that 99% of us would understand with a glance? How do other developers draw the line?

I see this all over Stack Overflow. For example, this question seems like it could be answered with a dozen lines of straightforward jQuery, yet he has opted to do it the angular way, with a directive and a partial... it seems like a lot of work for a simple problem.

Specifically, I suppose I would like to know: how SHOULD I be writing the code that checks whether "None" has been selected (if it exists as an option in this group of checkboxes), and then check/uncheck the other boxes accordingly? A more complex directive? I can't believe I'm the only developer that is having to implement code that is more complex than needed just to satisfy an opinionated framework.

答:
In my opinion, the quick way to do this in an angular-like way is to introduce a property of your javascript data that tracks the state of the checkbox. Your clearOnNone() function would modify a (dynamically-introduced) checked property of your investmentObjectiveMap. These updates would be immediately be reflected in the DOM. That way there's no jQuery hacking in the midst of your controller.

I forked an old fiddle of mine to give you an example.

html:

```<code><div ng-app="myApp">
    <div ng-controller="MyCtrl">
        <event class="event" ng-repeat="event in events" event="{{ event.name }}"
               ng-class="{'selected' : event.selected}"
               ng-click="selectEvent($index)">
            {{ event.name }}
        </event>
        <br>
        <input ng-repeat="event in events" type="checkbox" ng-model="event.selected">
        <br>
        <button ng-click="selectNone()">select none</button>

    </div>
</div>
</code>```

js:

```<code>var myApp = angular.module('myApp', []).controller('MyCtrl', function ($scope, $http) {
$scope.events = [
    {name: 'event1', selected: false},
    {name: 'event2', selected: false},
    {name: 'event3', selected: false}
];
$scope.selectEvent = function ($index) {
    var e = $scope.events[$index];
    e.selected = !e.selected;
};
$scope.selectNone = function () {
    $scope.events.forEach(function (event) {
        event.selected = false;
    });
};
</code>```

});

With the ng-true-value and corresponding directives, you can get any value you like into your model when boxes are checked.

EDIT

I just realized that I misunderstood your clearOnNone() behavior, but hopefully you can see how modifying the data and letting angular do the rest is an answer to your question.

Body:
In my opinion, the quick way to do this in an angular-like way is to introduce a property of your javascript data that tracks the state of the checkbox. Your clearOnNone() function would modify a (dynamically-introduced) checked property of your investmentObjectiveMap. These updates would be immediately be reflected in the DOM. That way there's no jQuery hacking in the midst of your controller.

I forked an old fiddle of mine to give you an example.

html:

<div ng-app="myApp">
    <div ng-controller="MyCtrl">
        <event class="event" ng-repeat="event in events" event="{{ event.name }}"
               ng-class="{'selected' : event.selected}"
               ng-click="selectEvent($index)">
            {{ event.name }}
        </event>
        <br>
        <input ng-repeat="event in events" type="checkbox" ng-model="event.selected">
        <br>
        <button ng-click="selectNone()">select none</button>

    </div>
</div>

js:

var myApp = angular.module('myApp', []).controller('MyCtrl', function ($scope, $http) {
$scope.events = [
    {name: 'event1', selected: false},
    {name: 'event2', selected: false},
    {name: 'event3', selected: false}
];
$scope.selectEvent = function ($index) {
    var e = $scope.events[$index];
    e.selected = !e.selected;
};
$scope.selectNone = function () {
    $scope.events.forEach(function (event) {
        event.selected = false;
    });
};

});

With the ng-true-value and corresponding directives, you can get any value you like into your model when boxes are checked.

EDIT

I just realized that I misunderstood your clearOnNone() behavior, but hopefully you can see how modifying the data and letting angular do the rest is an answer to your question.


答: It's usually an issue with bandwidth.  If you're serving up hundreds of videos an hour, then you're tying up the bandwidth in and out of the database, mostly copying buffers.  It's also an issue if you have naive queries (possibly auto-generated by an ORM tool) that simply select all columns from a table.  You're also subject to file fragmentation like a filesystem (except in this case it's record fragmentation), but (usually) without any tools to de-fragment.  If you're also modifying the BLOB (e.g., you're supporting some kind of video editing), then the database will copy the entire BLOB to the rollback or redo segment, then write the updated BLOB to the database.  So now you're copying those several hundred megabytes around, and tying up the redo segment until the transaction ends (not to mention the problems you can run into if the redo segment size is fixed).</p>


问:
Database is performing slow, even all the tables are having normalization
An interviewer asked me this question:

Tables are created with appropriate normalization rules, However the database is performing slow. [Ie.: The select, insert statements are taking time to do his operation.] What are areas we need to look to improve the database performance.

Obviously this is a vague question. What sorts of things might be wrong with a database that is running slowly, even when normalized?

答:
Specifically, in the Query Execution Plan look for actions that are table scans instead of index seeks. It is a hint that you might want to add an index to say a column that represents foreign key (they don't get created automatically)

Other options would be to put your data files on different physical disks. Using RAID for your partitions might work as well. At the very least, you want to separate log files from that data files...so that writing to the log does not impact the write time to the data file.

More advanced scenarios include clustering and sharding to allow the load for searches to be spread across multiple nodes.


答:
when in doubt default to badly quoting Knuth...

"Premature optimization is the root of all evil."

Here is what I would suggest, as it seems like you have a problem that I have from time to time...

What really works for me...

Write the unit tests, as if all the code was done.

document the interface.

implement the interface.

what you have really done:

work through the model layers requirements

really set up the division of work, which objects are responsible for what

get to work in an environment when you can actually step through the working code, which makes things so much faster an more accurate...

also rely on asserts in early development... then figure out which remedies need to be implemented and you wont write code that is unreachable, or hard to test.


答:
To know why this is ugly, you have to know how a database is saved on the hard drive (specifically rows).  The physical contents of a row saved on the disk is divided into its static and dynamic counterparts.  Fields such as int, byte, char(n) which have a fixed length are listed first.  What follows is a number of fixed length which refers to the number of variable length fields to follow.  All variable fields (regardless of the order of the columns presented to you, the programmer) are added at the end, each with a number of fixed length which determines how much space the variable length field occupies.

To give you a concrete example.  Suppose my table is the following:

```<code>char(3) A
varchar(4) B
int C
</code>```

Now suppose I do INSERT INTO mytable (A, B, C) VALUES ('AAA', 'B', 256).
On the database, that row would probably be stored as the following:

Field A gets saved as you'd expect.  Had I inserted 'A', it would have provided a special character to mark the premature end of the string after the first character, but it would occupy the same space.

Field C gets saved as the binary equivalent of 256.  Why C and not B?  C is the next static field with fixed length, and as such, it gets grouped together with all other static data in the database row.

Field D is meta information for the database which indicates that in the following variable length fields section, there will be precisely 1 field.

Field E is again meta information for the database which indicates that for this particular field, it is at most 1 character in length.  This information is essential because otherwise the database would not know where field B ends and another variable length field begins.

All of this to demonstrate how databases handle saving variable-length fields.  BLOB is very much a variable-length field to this effect.  The database structure allows one row to contain both small and large values in the BLOB, however, there are other factors at play here.  Databases normally deal with chunks of information since disks don't care about the contents but rather if it fits in a single chunk.

The database will try to fit as many rows into a single chunk without having to separate a row into two pieces, because otherwise the effect is the same as having a fragmented file on your hard drive.  Once one chunk is loaded, if the row overflows that particular chunk, the hard drive must then search for the rest of it in another chunk.  Worse still, there is no way a database can know that a row occupies more than a chunk without fully reading its contents since it is variable-length, so you cannot optimize by fetching both chunks at once.

Following this line of logic, if you could make a static-length BLOB, you wouldn't have this optimization problem, since the database could simply guarantee that the chunk size is larger than the minimum row size thereby ensuring that most rows won't have to be divided across multiple chunks.  Of course, databases don't do this because it would mean dedicating precious space when you probably won't need it.

BLOBS are fine when you're dealing with relatively small amounts, but for large files like videos and the like, a common workaround is simply to save the file path in the database and let the software deal with loading the file which is almost always more efficient.

Hope that explains it.  :)

Body:
To know why this is ugly, you have to know how a database is saved on the hard drive (specifically rows).  The physical contents of a row saved on the disk is divided into its static and dynamic counterparts.  Fields such as int, byte, char(n) which have a fixed length are listed first.  What follows is a number of fixed length which refers to the number of variable length fields to follow.  All variable fields (regardless of the order of the columns presented to you, the programmer) are added at the end, each with a number of fixed length which determines how much space the variable length field occupies.

To give you a concrete example.  Suppose my table is the following:

char(3) A
varchar(4) B
int C

Now suppose I do INSERT INTO mytable (A, B, C) VALUES ('AAA', 'B', 256).
On the database, that row would probably be stored as the following:

Field A gets saved as you'd expect.  Had I inserted 'A', it would have provided a special character to mark the premature end of the string after the first character, but it would occupy the same space.

Field C gets saved as the binary equivalent of 256.  Why C and not B?  C is the next static field with fixed length, and as such, it gets grouped together with all other static data in the database row.

Field D is meta information for the database which indicates that in the following variable length fields section, there will be precisely 1 field.

Field E is again meta information for the database which indicates that for this particular field, it is at most 1 character in length.  This information is essential because otherwise the database would not know where field B ends and another variable length field begins.

All of this to demonstrate how databases handle saving variable-length fields.  BLOB is very much a variable-length field to this effect.  The database structure allows one row to contain both small and large values in the BLOB, however, there are other factors at play here.  Databases normally deal with chunks of information since disks don't care about the contents but rather if it fits in a single chunk.

The database will try to fit as many rows into a single chunk without having to separate a row into two pieces, because otherwise the effect is the same as having a fragmented file on your hard drive.  Once one chunk is loaded, if the row overflows that particular chunk, the hard drive must then search for the rest of it in another chunk.  Worse still, there is no way a database can know that a row occupies more than a chunk without fully reading its contents since it is variable-length, so you cannot optimize by fetching both chunks at once.

Following this line of logic, if you could make a static-length BLOB, you wouldn't have this optimization problem, since the database could simply guarantee that the chunk size is larger than the minimum row size thereby ensuring that most rows won't have to be divided across multiple chunks.  Of course, databases don't do this because it would mean dedicating precious space when you probably won't need it.

BLOBS are fine when you're dealing with relatively small amounts, but for large files like videos and the like, a common workaround is simply to save the file path in the database and let the software deal with loading the file which is almost always more efficient.

Hope that explains it.  :)


答:
The solution is to create a collection of libraries with commonly used functions which you can re-use across projects. E.g. I have a StringFunctions.dll .NET library which does stuff like encoding, encryption, decryption, regular expression evaluation, etc. This way, I don't have to continually rewrite things that don't change.

Having a wrapper for file creation tasks also makes lots of sense. Your library could expose a method called GetFile() which does all the checks for you and returns either null or a file (or whatever you deem useful).


问:
SQLite with two python processes accessing it: one reading, one writing
I'm developing a small system with two components: one polls data from an internet resource and translates it into sql data to persist it locally; the second one reads that sql data from the local instance and serves it via json and a restful api.

I was originally planning to persist the data with postgresql, but because the application will have a very low-volume of data to store and traffic to serve, I thought that was overkill. Is SQLite up to the job? I love the idea of the small footprint and no need to maintain yet another sql server for this one task, but am concerned about concurrency.

It seems that with write ahead logging enabled, concurrently reading and writing a SQLite database can happen without locking either process out of the database.

Can a single SQLite instance sustain two concurrent processes accessing it, if only one reads and the other writes? I started writing the code but was wondering if this is a misapplication of SQLite.

答:
You are looking for the File Locking And Concurrency documentation.

SQLite processes use a series of locks to handle concurrency; to read, several processes can obtain a SHARED lock.

A process that writes, will need to obtain a RESERVED lock, and only when actually having to flush changes to disk does it move to the PENDING state. Any reading process will then have to unlock the file, after which the writing process is able to move to EXCLUSIVE for writing to the actual database file.

Because the writer process only needs to lock the database file for actual writes (memory flushes, commits), a setup with just one reader and just one writer will perform quite well. I'd expect it to perform just as well, if not better, as a setup with just one process doing all the reading and writing.

SQLite is less suited when you have multiple processes frequently writing to the same database, as writing requires obtaining the exclusive PENDING lock to serialize changes.