问:
Are there any programming languages that follow a minimalist development approach?
I find it that when languages are considered the same as commercial software, there is always a constant need to add new features to justify new releases.

Can there be or are there languages where version 1.0 is the final version? Of course bug fixes are exempt from this, but the feature set always remains the same?

This way every feature in the language fits together nicely and not seem like they are bolted on after the fact with obsolete features still around because of backwards compatibility.

I assume some academic languages are like this? But are there commercially successful languages that follows this idea? The accompanying library is also free to get new features too, but the language always remains constant.

One example I can give is, one of my favourite languages C#, which I use pretty often, more and more features are being added every release. To take advantages of these, I have to drop actual tasks in hand, and spend considerable time learning these instead of being able to pick up trivial concepts and combine those myself to solve more complex problems with ease.

So I guess I am looking for a minimalist approach where everything is consistent, makes sense, and is as orthogonal as possible.

答: I think "minimalist" design approach would be exemplified by Tcl. As an example, you can use the {catch} command with {return -code n} values to implement any control structure your imagination can conceive, Perl had to wait a decade to (officially) get {switch} -- (not that Perl ever needed it). Also you will see that Dodekalogue http://wiki.tcl.tk/10259 covers the grammar of Tcl completely --  the rest, whatever programming paradigm you want to work with, can be expressed in this grammar.</p>


问:
Why isn't Java being used for web multimedia applications?
I've been focusing on multimedia applications and I've noticed that most applications don't use Java in the back end. Instead, I see solutions with PHP/ROR/Python and Flash/Silverlight without any trace of Java in the application stack.

What part of Java is either limiting or constrained - or perceived as limiting or constrained - when building multimedia applications?

Let me clarify, when I said Java, I meant Java EE. Of course on the client side people will use flash or html5 for audio/video. But what I am trying is that, why is most multimedia platforms backend is created with PHP/ROR/Python(Django)? There are many audio distribution platforms out there. soundcloud.com (uses go lang), tunrtable.fm (uses Python) youtube.com (uses PHP), is Java EE not capable of doing those?

答:
If by multi-media you mean things like video sites, Java is a reasonably common, albeit not the most common choice; it is certainly capable of doing it though. One of the nicest multi-media servers I have come across in common use is Wowza which is written in Java and is very performant and easily extensible.

Youtube is actually Python, not PHP. The google stack is C++ / Java / Python. Very nearly everything they do is in one of those 3 primarily.

ContentLicense: CC BY-SA 3.0


问:
If I unite many web apps into one, will I save resources?
I'm comparing 2 different approaches of J2EE application architecture:

Divide independent parts as modules of one big web app.

Divide independent parts as different web apps running on same server.

Between these approaches I prefer the second one because it allows me to make changes without redeploying the whole application. Imagine that you want to change something in app2. In the case of one big app you have to redeploy the whole application. In the second case you have to only redeploy app2.

But I have some doubts about resources consumption such as memory usage and CPU usage. I personally think that each J2EE application, no matter how large it is, has some minimal limit of memory it will necessary use. This minimal limit is what is needed for context loading, web framework config files, jar libs and other things. My hypothesis sounds like "Having many web apps running on same server leads to multiplication of wasting of default minimal resources." Is the picture below correct? Does one big application save me from default resorces overhead multiplication shown on the picture?

答:
You might save a bit of disk space with the god application approach, which btw is an anti-pattern.

But you're gaining a maintenance nightmare, deployment nightmares, support nightmares, etc. etc. etc. I think you get the message.

Rather than deploying just one part that changed and leaving the rest you now need to every time some tiny thing changes in a subsystem deploy everything from scratch, not a good idea.


问:
Most efficient way to store this collection of moduli and remainders?
I have a huge collection of different moduli and associated with each modulus a fairly large list of remainders. I want to store these values so that I can efficiently determine whether an integer is equivalent to any one of the remainders with respect to any of the moduli (it doesn't matter which, I just want a true/false return).

I thought about storing these values as a linked-list of balanced binary trees, but I was wondering if there is a better way?

EDIT

Perhaps a little more detail would be helpful. As for the size of this structure, it will be holding about 10s of thousands of (prime-1) moduli and associated to each modulus will be a variable amount of remainders. Most moduli will only have one or two remainders associated to it, but a very rare few will have a couple hundred associated to it.

This is part of a larger program which handles numbers with a couple thousand (decimal) digits. This program will benefit more from this table being as large as possible and being able to be searched quickly.

Here's a small part of the dataset where the moduli are in parentheses and the remainders are comma separated:

```<code>(46) k = 20
(58) k = 15, 44
(70) k = 57
(102) k = 36, 87
(106) k = 66
(156) k = 20, 59, 98, 137
(190) k = 11, 30, 68, 87, 125, 144, 182
(430) k = 234
(520) k = 152, 282
(576) k = 2, 11, 20, 29, 38, 47, 56, 65, 74, ...(add 9 each time), 569
</code>```

I had said that the moduli were prime, but I was wrong they are each one below a prime.

答:
Since you don't mind which reminder the integer is related to and I see 87 is duplicated (and you will count only once), you could use a unique structure to store theese values.
A boolean array gives you O(1) complexity to find if an element exists or not (if you know the maximum dimention and you can have it all in RAM).
If also theese values can have thousands of digits and RAM is not enought you should use structures as tree (eg. B-trees) to have O(log n) complexity (the height of the tree).</p>


答:
Well apart from the fact that Java EE started as an interface to CORBA and as such will always be tainted with the dreaded "curse of CORBA".

There are a number of anomilies with java EE servers. Yes they should be faster and yes they should be easier to use and yes they should be more reliable -- but for the most part they are not. If you are stuck waiting for minutes at a time for an e-Commerce server to respond (Air France and BBVA come to mind) then the chances are that the back end was built with Java EE.

There are reasons for this.

The early Java EE APIs were awful (EJBs in particular) ate up enormous amounts of programmers time and created thousands of lines of technical code that had nothing to do with the business application in hand. Programmers were so pleased to get anything appearing on the browser they did care how clunky it was.

Then there is the Java culture of over engineering -- why define Pi as a constant when you can have an abstract constant factory which reads an XML configuration file telling it where to find the XML file with the value of Pi.

Proprietary hardware, most enterprises had standardized on Solaris or AIX when Java EE came along and so built the new web based infrastructure on what they were familiar with. Nice though these machines are they cost at least twice as much as commodity x86 so most of these sites cannot take advantage of high perfomance, cheap x86 linux systems without significant re-engineering.

PHP works! Like COBOL or VB before it, it is intensely disliked by language purists but tens of thousands of successful business applications have been built using these "ugly" languages.

The surprise is not why more sites use Java but why more sites do not use Python. Even though there is a proliferation of web servers and frameworks they are pretty much all excellent. YouTube, Google etc are pretty impressive as reference customers. Not to mention it has a serious kool factor.

Title: Why isn't Java being used for web multimedia applications?

Body:
I've been focusing on multimedia applications and I've noticed that most applications don't use Java in the back end. Instead, I see solutions with PHP/ROR/Python and Flash/Silverlight without any trace of Java in the application stack.

What part of Java is either limiting or constrained - or perceived as limiting or constrained - when building multimedia applications?

Let me clarify, when I said Java, I meant Java EE. Of course on the client side people will use flash or html5 for audio/video. But what I am trying is that, why is most multimedia platforms backend is created with PHP/ROR/Python(Django)? There are many audio distribution platforms out there. soundcloud.com (uses go lang), tunrtable.fm (uses Python) youtube.com (uses PHP), is Java EE not capable of doing those?


问:
Confused about layered application development
So, for the first big project that I'm getting paid for, I decided I'd do things right.

To that end, I've created several projects in my Solution. Some of these projects are generic and handle common elements that can be used in any project (stuff like logging and such), but that's not where the focus of my question is.

The projects I'm concerning myself with in this case are mostly to do with the database and the actual web application that will use it.

So I have an Ortund.Objects Class Library Project and an Ortund.DBContext Class Library Project.

Ortund.Objects

This project defines the classes, setting properties and default values that makes up the database.

For example, the Users class defines properties such as Username, EmailAddress, Password

Ortund.DBContext

This project uses Entity Framework and the Objects project to build the database. DbSets of the classes in the Objects project and various other components explicitly define the structure of the database.

Obviously, given this structure, I would have the DBContext project referencing the Objects project. This means that, while somewhere in my head, it makes sense to put all my methods and functions that the web app will use into the Objects project, I wouldn't actually be able to do anything because I won't have the DbContext. To reference the DBContext project would create a circular reference.

So, in order for my application to interact with the database now, the only option I can see is to create a 4th project to act as the data layer, effectively becoming a "middle-man" between the web application and the other 2 projects.

When I suggested this on IRC, someone answered with the following:

No, the application layer uses the database layer to provide persistence to the domain layer.

I don't understand what this means. Can someone help me to understand how best to structure this application?

答:
A typical full-tier application can be composed in many different ways. One of those ways is very similar to what you have come up with so far. The layers go somewhat like this (feel free to tweak and extend as per your use case):

A Models layer, which contains your database objects. These classes map directly to your database.

A Data Access layer, which contains your entity framework code and uses (1) to provide CRUD operations, etc.

A Business Logic or Domain layer, which uses (2) and adds business logic, domain-specific rules and functions to the mix

Sometimes, this business tier defines its own Domain Objects. These are also model objects, but they are in the context of the end-user rather than that of the database. A single business-tier object may be a logical composition/transformation of multiple database model objects.

Finally, an Application layer, which exposes a specific UI for a specific consumer. It uses the underlying business tier functions to do the actual work.

It is not necessary for these layers to be in separate packages. It all depends on the complexity and size of your application. The loose coupling helps to separate out the layers into independent modules as and when the need arises.


问:
Why encapsulate container variables?
I wasn't sure how to formulate the title, but there's something that's made little sense to me in OOP

I understand the idea of encapsulation, which is to create a layer of protection, and abstraction, so that there's no direct access to a given variable.

However, there are cases where encapsulation doesn't make any sense to me, allow me to demonstrate using a simple C# class:

```<code>public class Person
{
    public int Age { get; private set; } // makes sense, the setter is isolated
    public string Name { get; private set; } // same story
    public string ZipCode { get; set; } // doesn't make sense
    public string Country { get; set; } // doesn't make sense either

    public string City // also makes sense
    {
        get { /*some logic that gets the city based on zip code and country */ }
        private set;
    }
}
</code>```

My issue here is that, according to good OOP practices, variables without any special rules, must also have separate accessors and mutators, but why should they if all you do anyway is set and get data to and from them?

In the case of Age and Name it makes sense to encapsulate it, because the setter is private, you can't mutate it outside the scope of the class, likewise with the City field, that has a special getter, that gets the data based on special rules.

What difference does it make if I wrap ZipCode and Country in a property or not? I can fully access and mutate the variables in either case.

Could someone clarify this?

答:
It often happens that variables with no special rules, end up with special rules. If that happens you don't want to rewrite the code another time to use setters and getters, you can do it at the beginning, and if any rules changed you can simply change the implementation of the setter/getter.

That said, I know several cases were getters/setters are not usually needed, namely vector3/4 classes in game development, they often don't have setters/getters, they don't have any rules nor they will ever have as far as they represent the mathematical vector.

Regarding any perfomance hit, it's negligible in most of the applications anyway, unless you are doing a very performance critical application such as a game or a simulation. That said; languages like C++ tend to trivially inline these functions, eliminating any function call overhead.

Title: Why encapsulate container variables?


问:
Why does VBA / VBScript require "Set" prefix
When I set a variable to an object in VBA / VBScript, I always need to prefix the statement with "Set" such as: Set Dict = CreateObject("Scripting.Dictionary")

If I set a variable to a primitive or array value, I have to omit the "Set" such as: Num = 123

This is different from languages such as JavaScript, PHP, Python etc. Is there some technical reason why they have this requirement in VBA / VBScript?

答:
VB and VBA had something called "default properties".  If your code mentioned the object without any property/ method specified, then you got the default property, not the object!

Dim vValue as Variant

vValue = textBox1

? TypeName( vValue )

String

Should this work?  Well, yes, because VB interprets this as ...

vValue = textBox1.Text

... because Text is the textbox's default property.

The Set statement overrides all that, forcing VB to use the object itself.

Set vValue = textBox1

? TypeName( vValue )

TextBox


答:
You're right - there's no point in using properties when fields will do (F# uses similar techniques with Records, and you could similarly use Structs in C#). If you're worried about runtime performance though, it will never be an issue (the performance impact is negligible compared to other bottlenecks in any system).

As others have said though, encapsulation is about future flexibility and maintenance of your software. The more dependencies you have on your class, the more likely it is that it will need to change at some point. When it does change, it could break a lot of the dependencies which use it.

In general though, the reason for encapsulation is to hide your implementation. For example, you might discover after writing your initial code that it makes more sense to store your data in a dictionary (I know you wouldn't normally expect that, but a similar thing happens in WPF and other frameworks, so it just might). If you have provided Properties instead of fields, it is easy to change that implementation without breaking any of your dependencies. In face, you are then free to change anything in your class without having to worry about the dependencies on it.

If you're using Visual Studio, it usually only makes sense to use properties anyway, as the Code Snippets "prop" and "propg" do all the typing for you.

Title: Why encapsulate container variables?

Body:
I wasn't sure how to formulate the title, but there's something that's made little sense to me in OOP

I understand the idea of encapsulation, which is to create a layer of protection, and abstraction, so that there's no direct access to a given variable.

However, there are cases where encapsulation doesn't make any sense to me, allow me to demonstrate using a simple C# class:

```<code>public class Person
{
    public int Age { get; private set; } // makes sense, the setter is isolated
    public string Name { get; private set; } // same story
    public string ZipCode { get; set; } // doesn't make sense
    public string Country { get; set; } // doesn't make sense either

    public string City // also makes sense
    {
        get { /*some logic that gets the city based on zip code and country */ }
        private set;
    }
}
</code>```

My issue here is that, according to good OOP practices, variables without any special rules, must also have separate accessors and mutators, but why should they if all you do anyway is set and get data to and from them?

In the case of Age and Name it makes sense to encapsulate it, because the setter is private, you can't mutate it outside the scope of the class, likewise with the City field, that has a special getter, that gets the data based on special rules.

What difference does it make if I wrap ZipCode and Country in a property or not? I can fully access and mutate the variables in either case.

Could someone clarify this?


答:
(Code examples are in Java since I'm not familiar with C#).

Another point I'd like to mention, is that sometimes the implementation of how you store the data changes. And when that happens, you don't want the client code to break.

For example, you might store data in an array. With no encapsulation, the code that needs this data would access the array directly. For example, class.array[8].

But what if you want to change the way the data is stored, to an ArrayList for example? All of the client code gets broken.

With encapsulation, you'd create a getter in the first place:

```<code>public int getValue(int i){
    return array[i];
}
</code>```

Any classes that need to get data would do this: class.getValue(8).

And when the data structure changes from an array to an ArrayList, you only have change the getter, without breaking all of the client code:

```<code>public int getValue(int i){
    return list.get(i);
}
</code>```

All of the client code stays the same: class.getValue(8).

This is an example of how encapsulation gives you flexibility to change the internal implementation of a class without worrying it'll ripple and break other things.

Title: Why encapsulate container variables?

Body:
(Code examples are in Java since I'm not familiar with C#).

Another point I'd like to mention, is that sometimes the implementation of how you store the data changes. And when that happens, you don't want the client code to break.

For example, you might store data in an array. With no encapsulation, the code that needs this data would access the array directly. For example, class.array[8].

But what if you want to change the way the data is stored, to an ArrayList for example? All of the client code gets broken.

With encapsulation, you'd create a getter in the first place:

public int getValue(int i){
    return array[i];
}

Any classes that need to get data would do this: class.getValue(8).

And when the data structure changes from an array to an ArrayList, you only have change the getter, without breaking all of the client code:

public int getValue(int i){
    return list.get(i);
}

All of the client code stays the same: class.getValue(8).

This is an example of how encapsulation gives you flexibility to change the internal implementation of a class without worrying it'll ripple and break other things.


答:
Note: when I say "Visual Basic" here I mean Visual Basic 6 and before - not Visual Basic.NET.

Visual Basic is designed to be easy to use. Pointers are hard - and to some extent, so are reference types, like the ones used for objects in most modern languages.

the Set syntax is a way to hide this:

```<code>Set foo = Bar() 'Treat foo as reference variable
foo = Bar() 'Treat foo as a value variable
</code>```

So, people that don't understand references can use objects like value variables, which are easier to grasp. Given that Visual Basic is usually used with the Visual Studio form designer to create GUI applications, you can do quite a lot without using references.

VBA and VBS usually require more reference usage(in VBA you usually need to use Office objects, and in VBS you usually need to use COM objects to interact with Windows), but their syntax still needs to follow the Visual Basic one so they also require the Set syntax.

Title: Why does VBA / VBScript require "Set" prefix

Body:
When I set a variable to an object in VBA / VBScript, I always need to prefix the statement with "Set" such as: Set Dict = CreateObject("Scripting.Dictionary")

If I set a variable to a primitive or array value, I have to omit the "Set" such as: Num = 123

This is different from languages such as JavaScript, PHP, Python etc. Is there some technical reason why they have this requirement in VBA / VBScript?

Body:
Note: when I say "Visual Basic" here I mean Visual Basic 6 and before - not Visual Basic.NET.

Visual Basic is designed to be easy to use. Pointers are hard - and to some extent, so are reference types, like the ones used for objects in most modern languages.

the Set syntax is a way to hide this:

Set foo = Bar() 'Treat foo as reference variable
foo = Bar() 'Treat foo as a value variable

So, people that don't understand references can use objects like value variables, which are easier to grasp. Given that Visual Basic is usually used with the Visual Studio form designer to create GUI applications, you can do quite a lot without using references.

VBA and VBS usually require more reference usage(in VBA you usually need to use Office objects, and in VBS you usually need to use COM objects to interact with Windows), but their syntax still needs to follow the Visual Basic one so they also require the Set syntax.


问:
Is it possible for business logic not to creep into the view?
I've developed for several web application projects for the last 3 years, both personal and at work, and I can't seem to figure out whether it's possible for at least some business logic not ending up in the view layer of the application.

In most cases there will be problems like "If the user has selected option x then the application must enable him to supply info for y, if not then s/he should supply info z".
Or do some AJAX operation which should apply some changes to the model but NOT commit them until the user has explicitly requested so. These are some of the simplest problems I've encountered and I can't figure out how it's possible to avoid complex logic in the view.

Most of the books I've read describing MVC usually showcase some very trivial examples, like CRUD operations that just update data on the server and display them, but CRUD is not the case on most rich applications.

Is it possible to achieve having a view with no business logic at all?

答:
If the user has selected option x then the application must enable him
  to supply info for y, if not then s/he should supply info z".

There are inputs that have conditionally based required values. In most GUI environments, there are a lot of choices on how to handle inputs especially the timing. The selected option (in this case x) needs to be processed, so send it to the controller. Send it when the users leaves the input field. Wait until they click on another object or hit save. Doesn't matter to the business logic. One way or another, the controller will make a decision and needs to tell the view, "y is required".

How the view interprets or implements this doesn't really matter from a business logic standpoint. Make y a required field. Have a pop-up or shoot off a cannon and tell the user to enter y or just be stubborn and don't let the poor user do anything until she figures this out.

And just think, all of this may have occurred because the controller tried to save and didn't put in a value for a required field in the database and was purely responding to a database error. It doesn't matter as far as the view is concerned.

Something like a required or limited value for an input can be handled in many places. If you "only" address it in the view, many developers would see this as a problem when there can be multiple user interfaces. This is why business logic can be created and tested without much of a user interface or even a database. You don't even have to have a website.


问:
tracking time for personal projects
Does everyone track time spent on personal projects? I feel that it is valuable to know how much time (and potential money) has been put into a hobby.

I am currently using harvest to track time.

答:
Yes

I treat my personally hobby in the same manor as my commercial work. I manage it as if I was at work.

I use Issues logs, bug tracking & source control.

I find this helps me keep focus on my hobby projects and see it through to completion.

ContentLicense: CC BY-SA 2.5

Title: tracking time for personal projects


答:
The main advantage of being able to change implementation without affecting dependents was already pointed in the other answers, but I would like to point out another advantage - uniformity. There is value in having all your "data slots" being accessed the same way.

While it looks like public fields and properties in C# are accessed the same way(objectReference.FieldOrProperty), it's different in reflection. To get the public fields you use Type.GetFields() and to get the properties you use Type.GetProperties().

Using both types as public data slots will force reflection code to deal with both of them, which will make it more complex. Therefore many libraries that need to map data slots(e.g. ORMs, serializers) choose to only deal with properties - which are more flexible.

Title: Why encapsulate container variables?


问:
How important is XML for a developer?
Is it must that every developer should know XML? How important is XML for a developer? Any suggestion...</p>

答: A good developer should know when it is appropriate to use XML and when it is not. This requires understanding the concepts behind it and the reasons for its use. That way you can make an informed decision on whether you need to utilise it in a specific scenario. But there's no reason to start learning the mechanics of XPATH, XSLT etc. and the finer points of syntax until you actually need to.

Title: How important is XML for a developer?


问:
How faster could a dedicated chip do sequential squaring modulo operations to break a time crypto capsule?
I'm facing a very real problem and, sadly, I cannot find the answer on my own: I'm hitting my limit as a programmer because my hardware knowledge is not sufficiently advanced at all.

It's an issue I'm encountering during the development of the system (I need to find safe parameters value which depends on the answer to the question I'm asking here) (maybe crypto.SE would be better but crypto.SE seems more about theoretical questions about math/cryptography).

For a project I'm working on I need to use a "time capsule cryptosystem". More specifically the following one, invented by Rivest (the 'R' in 'RSA'):

Rivest's time capsule crypto puzzle

It is an inherently sequential operation: parallelization here is of no use. An attacker could use a botnet of million computers: it wouldn't help. One million ASIC chips wouldn't help either. The solution can only be found sequentially, by design.

Now I need to approximate (one or two orders of magnitude off is ok: an approximation 10x faster/slower or even 100x faster/slower ain't really an issue) how much faster could a dedicated chip solve that puzzle compared to a fast CPU (a fast core, really).

Basically solving that puzzle consists in doing a lot of squarings modulo n. Let's consider there's no "attack" on that puzzle: I've chosen bit enough RSA integers etc.

Would a GPU help compared to a CPU here? A FPGA? Would a dedicated chip like some ASIC chip help?

If any of these would help, how can I determine how much faster it could be than  a fast CPU?

I'd need some back-of-the-envelope calculation and, as I stated previously, an approximation about 10x or 100x the actual answer ain't that a big problem.

I guess my question boils down to something like (but feel free to rewrite if I'm mistaken): knowing that the problem is inherently sequential and consists in repeated squaring modulo n operation, can you build/program some hardware to only do sequential squaring modulo operations and how much faster would it be than an actual CPU?

答:
I don't have a definitive answer because it would take a bit of analysis. It also depends on the CPU, how many bits etc. But for a ballpark number.

FPGA's have been built that will do a divide operation in 1 clock cycle. A general purpose CPU/ALU can take 20 to 100 clock cycles. DSP processors will likely take less clock cycles. So we can say a FPGA is 100x faster.

That's just the divide operation (which gives you the modulo also).

You would still have to account for memory transfers. Would a dedicated FPGA need to do this? A general purpose CPU would definitely need to do it. This makes the FPGA all the more faster if it could eliminate most of its memory transfers.  At this point 1000x improvement seems realistic.

However, I didn't read the algorithm but I'm guessing there's some table updates/lookups, this would probably require memory transfers to/from the FPGA, which slows down the FPGA processing to be more equivalent to the CPU processing. Even if the FPGA has dedicated extra fast memory, this would still cause that 1000x guesstimate to drop somewhat.

Anyways, my estimate would be a 100x-1000x speed improvement with an FPGA in the best of circumstances.

UPDATE

I came across this article http://research.microsoft.com/apps/pubs/default.aspx?id=70636 (a little dated 2008) but probably still applicable. It is called "Where's the Beef? Why FPGAs are so fast" They did various timing comparisons including 128-bit AES encryption. For the AES encryption they were able to get about a 4000 times increase from the simplistic software implementation to a highly optimized FPGA. Although, I think there were also software optimizations that could be made that reduced that 4000 times number.

I don't know if the state-of-the-art FPGA/Custom Hardware has improved dramatically compared to state-of-the-art CPUs of today, so I don't know how applicable this info is any longer. But, I still think the 100x to 1000x is a good estimate as I would assume that the software would be optimized.


问:
Store history in SQL database column
We have a database that stores the process/history of particular machines. What I mean by that is that we have "started", "updating", "shutdown", and so on for all the computers in the company.

The requirement is to have the history available in the right order. Simple enough. My approach was to have a simple database table like this:

```
counter  | machine | time | process | ...
 1       | PC1     | ...  | started
 2       | PC2     | ...  | updating
 1       | PC1     | ...  | updating
```

The query will be very simple because I can simply sort by the counter.

My colleagues suggested a very different approach. My gut feeling says it is wrong but I don't have enough arguments against it. Can you help define what is wrong with it?

Instead of using multiple rows for the process of a machine, they want to write them sequentlly into the same field. Same goes for the time field. Their main arguement is that we save storage

```
counter  | machine | time        | process | ...
 1       | PC1     | ... ... ... | started updating shutdown
 2       | PC2     | ... ... ... | started updating
```

答:
That's a fine idea, assuming that your database vendor has no maximum text field size. I could also see there being concurrency issues on the rows when two threads are trying to append to the same row near simultaneously.

Ask your team why space is that much of an issue that you would try to make bad optimisations that will save very little space compared to the problems they might cause.

Title: Store history in SQL database column


答:
The second approach produces ambiguous results: what is the value in the time column? the time of started? or shutdown? or something else?

The second approach also makes it harder to query for machines that have a started and updating process and no shutdown process, which could be useful for creating reports and live monitoring. Sure you could use like... but that tends to be slower than when you can match a value exactly, and even slower if you index the column, or just store an ID that references a process name.

The second approach might save some storage, but how much data do you plan on having? Millions of new records every few days? More? Unless storage is a serious problem even at this design stage, I would not use the second approach.

Title: Store history in SQL database column

Body:
We have a database that stores the process/history of particular machines. What I mean by that is that we have "started", "updating", "shutdown", and so on for all the computers in the company.

The requirement is to have the history available in the right order. Simple enough. My approach was to have a simple database table like this:

```
counter  | machine | time | process | ...
 1       | PC1     | ...  | started
 2       | PC2     | ...  | updating
 1       | PC1     | ...  | updating
```

The query will be very simple because I can simply sort by the counter.

My colleagues suggested a very different approach. My gut feeling says it is wrong but I don't have enough arguments against it. Can you help define what is wrong with it?

Instead of using multiple rows for the process of a machine, they want to write them sequentlly into the same field. Same goes for the time field. Their main arguement is that we save storage

```
counter  | machine | time        | process | ...
 1       | PC1     | ... ... ... | started updating shutdown
 2       | PC2     | ... ... ... | started updating
```


答:
Actually, the answer is simple.  If you use Java to compare, the answer will be the development cost.

Let me make it easier, PHP/Python are used as server side scripting when Flash/Silverlight are used as client side scripting.  If you want to utilize java for this purpose, you will need learn how to handle java servlet, inside an application server, and also properly design an applet or JavaFX component. By using Java, it already gives you additional burden to learn more about programming.

On the other hand, PHP/Python allows you to start everything with instant result, without necessity to learn everything properly.  But please don't get me wrong.  I'm not saying Java is more powerful.  PHP can be used to create something big.

At this moment Java is used when there is a need to protect some commercial code that can be done without any cost (Open Source).  If you want to use PHP to achieve the same result as Java, you can have a look at Zend framework as an example.


答:
The first method is better.  The second method will need a column with no limit of length.  Unless both of you have known the maximum characters that will be put in.  </p>

<p>The second method will not allow you to monitor the process, step by step like the first one.  </p>

Title: Store history in SQL database column

Body:
The first method is better.  The second method will need a column with no limit of length.  Unless both of you have known the maximum characters that will be put in.

The second method will not allow you to monitor the process, step by step like the first one.


问:
Why not embed styles/scripts in HTML instead of linking?
We concatenate CSS and JavaScript files to reduce the number of HTTP requests, which improves performance. The result is HTML like this:

```<code><link rel="stylesheet" href="all-my-css-0fn392nf.min.css">
<!-- later... -->
<script src="all-my-js-0fn392nf.min.js"></script>
</code>```

If we've got server-side/build logic to do all this for us, why not take it one step further and embed those concatenated styles and scripts in the HTML?

```<code><style>.all{width:100%;}.my{display:none;}.css{color:white;}</style>
<!-- later... -->
<script>var all, my, js;</script>
</code>```

That's two fewer HTTP requests, yet I've not seen this technique in practice. Why not?

答:
In addition to the caching and retrieving concerns that the other answers raise, I'd like to highlight another, more obscure problem: parsing.

JavaScript appearing in HTML can run into parsing issues, like in this example:

```<code><html>
<head>
<script>
function myfunc() {
    if ("</style> isn't a problem")
        return "but </script> is"
}
</script>
<style>
body::after {
  content: '</script> is okay, but not </style>'
}
</style>
</head>
<body>
<script>document.write(myfunc())</script>
</body>
</html>
</code>```

...which means you'd have to transform your script to escape some characters that trigger in HTML.  This issue goes away when you supply CSS and JavaScript as external resources, because they no longer have to take the 'parent' parsing context into account.

If you serve your content as XML, you have a part way around this by using CDATA sections.  CDATA, however, comes with a similar issue:

```<code><?xml version="1.0" encoding="utf-8"?>
<html>
<head>
<script>
// <![CDATA[
function myfunc() {
    if ("</script> is no longer a problem")
        return "but ]]> is"
}
// ]]>
</script>
<style>
<![CDATA[
body::after {
  content: 'same ]]> issue here'
}
]]>
</style>
</head>
<body>
<script>document.write(myfunc())</script>
</body>
</html>
</code>```

Inliners beware.

Body:
In addition to the caching and retrieving concerns that the other answers raise, I'd like to highlight another, more obscure problem: parsing.

JavaScript appearing in HTML can run into parsing issues, like in this example:

<html>
<head>
<script>
function myfunc() {
    if ("</style> isn't a problem")
        return "but </script> is"
}
</script>
<style>
body::after {
  content: '</script> is okay, but not </style>'
}
</style>
</head>
<body>
<script>document.write(myfunc())</script>
</body>
</html>

...which means you'd have to transform your script to escape some characters that trigger in HTML.  This issue goes away when you supply CSS and JavaScript as external resources, because they no longer have to take the 'parent' parsing context into account.

If you serve your content as XML, you have a part way around this by using CDATA sections.  CDATA, however, comes with a similar issue:

<?xml version="1.0" encoding="utf-8"?>
<html>
<head>
<script>
// <![CDATA[
function myfunc() {
    if ("</script> is no longer a problem")
        return "but ]]> is"
}
// ]]>
</script>
<style>
<![CDATA[
body::after {
  content: 'same ]]> issue here'
}
]]>
</style>
</head>
<body>
<script>document.write(myfunc())</script>
</body>
</html>

Inliners beware.


问:
Why doesn't Ruby have implicit conversion of Fixnum into String?
Ruby normally makes things easy. However, it doesn't allow implicitely converting a number to a string:

```<code>2.0.0p247 :010 > "a"+1
TypeError: no implicit conversion of Fixnum into String
</code>```

Why is this when nearly every other language allows it, including Perl, PHP, Java and VBA?

答:
First, to clear up some misconceptions...

perl and php

Perl and php do not have implicit conversion from number to string.

Consider the code (written as perl to get the warnings, but you can run it as php also and get the same (sans warnings) results):

```<code>#!/usr/bin/perl

use strict;
use warnings;

print "a" + "b"; print "\n";
print "a" + 1; print "\n";
print "a" . 1; print "\n";
print 1 + 2; print "\n";
print 1 . 2; print "\n";
</code>```

https://ideone.com/yZFiBn

When run this prints out:

```
Argument "b" isn't numeric in addition (+) at foo.pl line 6.
Argument "a" isn't numeric in addition (+) at foo.pl line 6.
0
Argument "a" isn't numeric in addition (+) at foo.pl line 7.
1
a1
3
12
```

Specifically note that the + operator is not doing any conversion of one type to another - it is a math operator, not a string operator (thats .).  Each operation you explicitly identify what type of operation you want to do on the operands.

Java

```<code>public static String foo(String a, String b) {
    return a.concat(b);
}
public static void main (String[] args) {
    System.out.println(foo("a",1));
}
</code>```

Thats a compile error.  Changing it to an Integer object rather than an int primitive doesn't help:

```<code>public static String foo(String a, String b) {
    return a.concat(b);
}
public static void main (String[] args) {
    System.out.println(foo("a",Integer.valueOf(1)));
}
</code>```

Thats a compile error too.  There is no implicit conversion from Integer or int (or any number format) to a String.

There is however, a shortcut for creating Strings based on the + operator.  Apparently, it was decided that the String + ? and ? + String operator should be overloaded to do a string concatenation of the two objects as this was done frequently enough.  However this is an example of operator overloading in a single special case rather than implicit conversion from some type to a String.  It is also never ambiguous if you are using the + operator on Strings or Numbers.  If its all numbers, its a number; if there is a String, its a string (you can't add Objects or Streams).

Additionally, in many places toString() is called on an object to do something with it (debug messages and the like) - but this isn't an implicit change.

VB.NET

```<code>Imports System

Public Class Test
    Public Shared Sub Main()
        Console.writeLine(1 + 2)
        Console.writeLine("a" + "b")
        Console.writeLine(1 + "a")
    End Sub
End Class
</code>```

https://ideone.com/YZTwhX

This produces the output:

```
3
ab
```

And has a runtime error:

```
Unhandled Exception: System.InvalidCastException:
  Conversion from string "a" to type 'Double' is not valid.
  ---> System.FormatException: Unknown char: a
```

Note that changing the order of 1 + "a" to "a" + 1 gives the same error.

Conclusion

No, other languages don't have implicit conversion from one type to another.  Some languages have explicit operands for doing math or string operations, but these are not implicit.

Some languages catch these errors at compile time, others at run time - but in the sample given, they are not valid.

In languages with dynamic typing, it can be rather difficult to understand if you want the String concatenation operator or the math addition operator.  Since Ruby uses + for both of these operations, when there is an ambiguous situation, its an error.

The alternative would be to make the operands different (as Perl and Basic do), but Ruby tends to have a culture of overloading operators and thus the design decision that leads to this error.  Similar issues will arise with << being used in different contexts (the << operator appends to an array, writes to IO, left shifts a bit, appends to a message digest, or whatever else it has been overloaded to do).  This was a design decision to allow this level of mailability in the language - something other languages tend to avoid.  It came with the cost of errors when it possible to interpret something two ways (something other languages tend to avoid).

Into the perl magic

There seems to be some confusion with perl's scalar as a string and an number.

From perldata:

Scalars aren't necessarily one thing or another. There's no place to declare a scalar variable to be of type "string", type "number", type "reference", or anything else. Because of the automatic conversion of scalars, operations that return scalars don't need to care (and in fact, cannot care) whether their caller is looking for a string, a number, or a reference. Perl is a contextually polymorphic language whose scalars can be strings, numbers, or references (which includes objects). Although strings and numbers are considered pretty much the same thing for nearly all purposes, references are strongly-typed, uncastable pointers with builtin reference-counting and destructor invocation.

There isn't a conversion of a String to an Integer being done - its just a scalar that is there.  Depending on how you want to look at it (which operator you are using), the appropriate part of the scalar is pulled out and used.

A scalar value in perl is a String or a number until its used in a numeric context or a string context.  Once it is used in that context, the data structure that is behind the Scalar populates the other value too.

The easiest way to show this is to create a scalar that has radically different values in its parts.

```<code>#!/usr/bin/perl

use Scalar::Util qw (dualvar);

$foo = dualvar 42, "Hello";
print $foo + 2,"\n";
print $foo . " world\n";
</code>```

The output of this is:

```
44
Hello world
```

There is no implicit conversion being done here.

You can see the other value being populated when looking at the isdual function:

```<code>#!/usr/bin/perl

use Scalar::Util qw (isdual);

$foo = "10";    # A string
$dual = isdual($foo);
print ($dual?"true":"false"); print "\n";
$bar = $foo + 0; # $foo used in numeric context
$dual = isdual($foo);
print ($dual?"true":"false"); print "\n";
print "\n";

$qux = 10;      # A number
$dual = isdual($qux);
print ($dual?"true":"false"); print "\n";
$bar = $qux . "foo";    # $qux used in a string context
$dual = isdual($qux);
print ($dual?"true":"false"); print "\n";
</code>```

The output of this is:

```
Argument "b" isn't numeric in addition (+) at foo.pl line 6.
Argument "a" isn't numeric in addition (+) at foo.pl line 6.
0
Argument "a" isn't numeric in addition (+) at foo.pl line 7.
1
a1
3
12
```0

The value starts out as one thing, and as it is used in the given context the other half of the scalar is populated.  However, each of these is an explicit conversion when it is used in that context.

It should be noted that my VBA example above is incomplete.  The & operator is the String concatenation in many Basic dialects.  Larry Wall started out as a Basic programmer and was undoubtedly influenced by its approach to handling string and math operators as separate things rather than trying to mix them together.

Body:
First, to clear up some misconceptions...

perl and php

Perl and php do not have implicit conversion from number to string.

Consider the code (written as perl to get the warnings, but you can run it as php also and get the same (sans warnings) results):

#!/usr/bin/perl

use strict;
use warnings;

print "a" + "b"; print "\n";
print "a" + 1; print "\n";
print "a" . 1; print "\n";
print 1 + 2; print "\n";
print 1 . 2; print "\n";

https://ideone.com/yZFiBn

When run this prints out:

Argument "b" isn't numeric in addition (+) at foo.pl line 6.
Argument "a" isn't numeric in addition (+) at foo.pl line 6.
0
Argument "a" isn't numeric in addition (+) at foo.pl line 7.
1
a1
3
12

Specifically note that the + operator is not doing any conversion of one type to another - it is a math operator, not a string operator (thats .).  Each operation you explicitly identify what type of operation you want to do on the operands.

Java

public static String foo(String a, String b) {
    return a.concat(b);
}
public static void main (String[] args) {
    System.out.println(foo("a",1));
}

Thats a compile error.  Changing it to an Integer object rather than an int primitive doesn't help:

public static String foo(String a, String b) {
    return a.concat(b);
}
public static void main (String[] args) {
    System.out.println(foo("a",Integer.valueOf(1)));
}

Thats a compile error too.  There is no implicit conversion from Integer or int (or any number format) to a String.

There is however, a shortcut for creating Strings based on the + operator.  Apparently, it was decided that the String + ? and ? + String operator should be overloaded to do a string concatenation of the two objects as this was done frequently enough.  However this is an example of operator overloading in a single special case rather than implicit conversion from some type to a String.  It is also never ambiguous if you are using the + operator on Strings or Numbers.  If its all numbers, its a number; if there is a String, its a string (you can't add Objects or Streams).

Additionally, in many places toString() is called on an object to do something with it (debug messages and the like) - but this isn't an implicit change.

VB.NET

Imports System

Public Class Test
    Public Shared Sub Main()
        Console.writeLine(1 + 2)
        Console.writeLine("a" + "b")
        Console.writeLine(1 + "a")
    End Sub
End Class

https://ideone.com/YZTwhX

This produces the output:

3
ab

And has a runtime error:

Unhandled Exception: System.InvalidCastException:
  Conversion from string "a" to type 'Double' is not valid.
  ---> System.FormatException: Unknown char: a

Note that changing the order of 1 + "a" to "a" + 1 gives the same error.

Conclusion

No, other languages don't have implicit conversion from one type to another.  Some languages have explicit operands for doing math or string operations, but these are not implicit.

Some languages catch these errors at compile time, others at run time - but in the sample given, they are not valid.

In languages with dynamic typing, it can be rather difficult to understand if you want the String concatenation operator or the math addition operator.  Since Ruby uses + for both of these operations, when there is an ambiguous situation, its an error.

The alternative would be to make the operands different (as Perl and Basic do), but Ruby tends to have a culture of overloading operators and thus the design decision that leads to this error.  Similar issues will arise with << being used in different contexts (the << operator appends to an array, writes to IO, left shifts a bit, appends to a message digest, or whatever else it has been overloaded to do).  This was a design decision to allow this level of mailability in the language - something other languages tend to avoid.  It came with the cost of errors when it possible to interpret something two ways (something other languages tend to avoid).

Into the perl magic

There seems to be some confusion with perl's scalar as a string and an number.

From perldata:

Scalars aren't necessarily one thing or another. There's no place to declare a scalar variable to be of type "string", type "number", type "reference", or anything else. Because of the automatic conversion of scalars, operations that return scalars don't need to care (and in fact, cannot care) whether their caller is looking for a string, a number, or a reference. Perl is a contextually polymorphic language whose scalars can be strings, numbers, or references (which includes objects). Although strings and numbers are considered pretty much the same thing for nearly all purposes, references are strongly-typed, uncastable pointers with builtin reference-counting and destructor invocation.

There isn't a conversion of a String to an Integer being done - its just a scalar that is there.  Depending on how you want to look at it (which operator you are using), the appropriate part of the scalar is pulled out and used.

A scalar value in perl is a String or a number until its used in a numeric context or a string context.  Once it is used in that context, the data structure that is behind the Scalar populates the other value too.

The easiest way to show this is to create a scalar that has radically different values in its parts.

#!/usr/bin/perl

use Scalar::Util qw (dualvar);

$foo = dualvar 42, "Hello";
print $foo + 2,"\n";
print $foo . " world\n";

The output of this is:

44
Hello world

There is no implicit conversion being done here.

You can see the other value being populated when looking at the isdual function:

#!/usr/bin/perl

use Scalar::Util qw (isdual);

$foo = "10";    # A string
$dual = isdual($foo);
print ($dual?"true":"false"); print "\n";
$bar = $foo + 0; # $foo used in numeric context
$dual = isdual($foo);
print ($dual?"true":"false"); print "\n";
print "\n";

$qux = 10;      # A number
$dual = isdual($qux);
print ($dual?"true":"false"); print "\n";
$bar = $qux . "foo";    # $qux used in a string context
$dual = isdual($qux);
print ($dual?"true":"false"); print "\n";

The output of this is:

false
true

false
true

The value starts out as one thing, and as it is used in the given context the other half of the scalar is populated.  However, each of these is an explicit conversion when it is used in that context.

It should be noted that my VBA example above is incomplete.  The & operator is the String concatenation in many Basic dialects.  Larry Wall started out as a Basic programmer and was undoubtedly influenced by its approach to handling string and math operators as separate things rather than trying to mix them together.


问:
For web apps (vs web pages) why not put scripts and css inline?
I understand that for normal websites you'd want to share scripts and css and therefore liking to them means that each page shares them from the cache. But this question is not about "normal multipage websites", it's about WebApps. Pages where only one page is ever served. Examples include gmail, new google maps, facebook, etc...

We use less etc to compile CSS, possibly from multiple files, and we use various JavaScript minifiers to concat/compress our JavaScript into one file, why don't we just take it one step further and compile put everything directly into the HTML file?

At least for single page web apps (think gmail, google maps, facebook), that seems like it would be the best. A single request gets you everything.

What am I missing? It seems like whatever arguments applied for concatenating the CSS and the JavaScript should apply equally to the entire bundle CSS+JavaScript+HTML

答:
Even in a context of a single page application, the idea of caching holds.

Consider that you may be using JavaScript libraries that are publicly available (jQuery, for instance).  It's likely other people's webapps (single page or multiple) also use it.  If you use the same public URI, we may be able to cache that.

But wait, maybe you have the jQuery version stashed on your own servers, to off-load others, or because you can serve a better connection, or whatever.  Now you can inline, right?  Yes, provided that you have no other webapps that also use that library, or that it is highly unlikely for users to use more than one of those webapps.

Things that you write specifically for that single page of yours, that have no relevance to other pages or applications, yes, you can safely inline.  For the rest, I'd say: think broader than the page.

There are trade-offs to the approach you suggest.  It's not inherently bad, and there are clear upsides (fewer HTTP requests), and there are downsides (caching issues).  I'd call it optimisation; if you have good, verifiable evidence that inlining will resolve an issue, then that's a good reason to do it.

You may find the comments on the accepted answer for "Why not embed styles/scripts in HTML instead of linking?" relevant, especially:

If you are doing a single page app style site then where the vast majority of the code was specific to one page then it might make some sense still?

If the page is visited only once, yes. If visited multiple times, all the embedded stuff gets transmitted multiple times instead of just once and cached.

Do not forget the effects of a CDN or local caching at the isp. My request for the javascript for google likely never reaches google even if I have a cache miss locally because another person who uses the same isp will have already caused the isp to cache the data.

There is also the potential issue of parsing, though that's more a caveat than a breaking issue.


问:
Dealing with not knowing parameter names of a function when you're calling it
Here's a programming/language problem I'd like to hear your thoughts on.

We have developed conventions that most programmers (should) follow that aren't a part of the languages syntax but serve to make code more readable. These are of course always a matter of debate but there's at least some core concepts that most programmers find agreeable. Naming your variables appropriately, naming in general, making your lines not outrageously long, avoiding long functions, encapsulations, those things.

However, there's a problem that I have yet to find anyone commenting on and that just might be the biggest one of the bunch. It's the problem of arguments being anonymous when you call a function.

Functions stem from mathematics where f(x) has a clear meaning because a function has a much more rigorous definition that it usually does in programming. Pure functions in mathematics can do a lot less than they can in programming and they are a much more elegant tool, they usually only take one argument (which is usually a number) and they always return one value (also usually a number). If a function takes multiple arguments, they are almost always just extra dimensions of the function's domain. In other words, one argument isn't more important than the others. They are explicitly ordered, sure, but other than that, they have no semantic ordering.

In programming however, we have more freedom defining functions, and in this case I'd argue it isn't a good thing. A common situation, you have a function defined like this

```<code>func DrawRectangleClipped (rectToDraw, fillColor, clippingRect) {}
</code>```

Looking at the definition, if the function is written correctly, its perfectly clear what's what. When calling the function, you might even have some intellisense/code completion magic going on in your IDE/editor that will tell you what the next argument should be. But wait. If I need that when I'm actually writing the call, isn't there something we're missing here? The person reading the code doesn't have the benefit of an IDE and unless they jump to the definition, they have no idea which of the two rectangles passed as arguments is used for what.

The problem goes even further than that. If our arguments come from some local variable, there might be situations where we don't even know what the second argument is since we only see the variable name. Take for example this line of code

```<code>DrawRectangleClipped(deserializedArray[0], deserializedArray[1], deserializedArray[2])
</code>```

This is alleviated to various extents in different languages but even in strictly typed languages and even if you name your variables sensibly, you don't even mention the type the variable is when you're passing it to the function.

As it usually is with programming, there are a lot of potential solutions to this problem. Many are already implemented in popular languages. Named parameters in C# for example. However, all that I know have significant drawbacks. Naming every parameter on every function call can't possibly lead to readable code. It almost feels like maybe we're outgrowing possibilities that plain text programming gives us. We've moved from JUST text in almost every area, yet we still code the same. More information is needed to be displayed in the code? Add more text.
Anyways, this is getting a bit tangential so I'll stop here.

One reply I got to the second code snippet is that you would probably first unpack the array to some named variables and then use those but the variable's name can mean many things and the way it's called doesn't necessarily tell you the way it's supposed to be interpreted in the context of the called function.
In the local scope, you might have two rectangles named leftRectangle and rightRectangle because that's what they semantically represent, but it doesn't need to extend to what they represent when given to a function.

In fact, if your variables are named in the context of the called function than you're introducing less information than you potentially could with that function call and on some level if does lead to code worse code. If you have a procedure that results in a rectangle you store in rectForClipping and then another procedure that provides rectForDrawing, then the actual call to DrawRectangleClipped is just ceremony. A line that means nothing new and is there just so the computer knows what exactly you want even though you've explained it already with your naming. This isn't a good thing.

I'd really love to hear fresh perspectives on this. I'm sure I'm not the first one to consider this a problem, so how is it solved?

答:
In practice, it's solved by better design. It is exceptionally uncommon for well-written functions to take more than 2 inputs, and when it does occur, it's uncommon for those many inputs to not be able to be aggregated into some cohesive bundle. This makes it pretty easy to break up functions or aggregate parameters so you're not making a function do too much. One it has two inputs, it becomes easy to name and much clearer about which input is which.

My toy language had the concept of phrases to deal with this, and other more natural language focused programming languages have had other approaches to deal with it, but they all tend to have other downsides. Plus, even phrases are little more than a nice syntax around making functions have better names. It's always going to be hard to make a good function name when it takes a bunch of inputs.


问:
Website Internationalisation approaches
I'm currently working on a project that the client wants to deliver to different countries.

The application is a website.

We started out using resource files to change the text the user sees when visiting pages based on their locations. But this isn't enough.

In the English version of the site we have a page that states "page x of y".
In the Chinese version of the site, it's not enough to do " x  y"

The syntax for the language doesn't match the placeholders that were created for the English language.

The only other option I can think of is to have a completely separate site, serving different content.

Are there any other options to solve this issue?

答:
It depends on your programming tools, as some tools enable project culture that allow you to change design accordingly.  If you are using ASP.NET you can have a look this information http://msdn.microsoft.com/en-us/library/ff647353.aspx

If you are using PHP/Phyton, you need to prepare all the words, then detect the language of the user.  Finally, if there is a request, your application need to compose the page properly.

Title: Website Internationalisation approaches


答:
Mostly it's solved by good naming of functions, parameters, and arguments.  You already explored that and found it had deficiencies, however.  Most of those deficiencies are mitigated by keeping functions small, with a small number of parameters, both in the calling context and the called context.  Your particular example is problematic because the function you are calling is trying to do several things at once:  specify a base rectangle, specify a clipping region, draw it, and fill it with a specific color.

This is kind of like trying to write a sentence using only the adjectives.  Put  more verbs (function calls) in there, create a subject (object) for your sentence, and it's easier to read:

```<code>rect.clip(clipRect).fill(color)
</code>```

Even if clipRect and color have terrible names (and they shouldn't), you can still discern their types from the context.

Your deserialized example is problematic because the calling context is trying to do too much at once:  deserializing and drawing something.  You need to assign names that make sense and clearly separate the two responsibilities.  At a minimum, something like this:

```<code>(rect, clipRect, color) = deserializeClippedRect()
rect.clip(clipRect).fill(color)
</code>```

A lot of readability problems are caused by trying to be too concise, skipping intermediate stages that humans require to discern context and semantics.

Title: Dealing with not knowing parameter names of a function when you're calling it

Body:
Here's a programming/language problem I'd like to hear your thoughts on.

We have developed conventions that most programmers (should) follow that aren't a part of the languages syntax but serve to make code more readable. These are of course always a matter of debate but there's at least some core concepts that most programmers find agreeable. Naming your variables appropriately, naming in general, making your lines not outrageously long, avoiding long functions, encapsulations, those things.

However, there's a problem that I have yet to find anyone commenting on and that just might be the biggest one of the bunch. It's the problem of arguments being anonymous when you call a function.

Functions stem from mathematics where f(x) has a clear meaning because a function has a much more rigorous definition that it usually does in programming. Pure functions in mathematics can do a lot less than they can in programming and they are a much more elegant tool, they usually only take one argument (which is usually a number) and they always return one value (also usually a number). If a function takes multiple arguments, they are almost always just extra dimensions of the function's domain. In other words, one argument isn't more important than the others. They are explicitly ordered, sure, but other than that, they have no semantic ordering.

In programming however, we have more freedom defining functions, and in this case I'd argue it isn't a good thing. A common situation, you have a function defined like this

```<code>func DrawRectangleClipped (rectToDraw, fillColor, clippingRect) {}
</code>```

Looking at the definition, if the function is written correctly, its perfectly clear what's what. When calling the function, you might even have some intellisense/code completion magic going on in your IDE/editor that will tell you what the next argument should be. But wait. If I need that when I'm actually writing the call, isn't there something we're missing here? The person reading the code doesn't have the benefit of an IDE and unless they jump to the definition, they have no idea which of the two rectangles passed as arguments is used for what.

The problem goes even further than that. If our arguments come from some local variable, there might be situations where we don't even know what the second argument is since we only see the variable name. Take for example this line of code

```<code>DrawRectangleClipped(deserializedArray[0], deserializedArray[1], deserializedArray[2])
</code>```

This is alleviated to various extents in different languages but even in strictly typed languages and even if you name your variables sensibly, you don't even mention the type the variable is when you're passing it to the function.

As it usually is with programming, there are a lot of potential solutions to this problem. Many are already implemented in popular languages. Named parameters in C# for example. However, all that I know have significant drawbacks. Naming every parameter on every function call can't possibly lead to readable code. It almost feels like maybe we're outgrowing possibilities that plain text programming gives us. We've moved from JUST text in almost every area, yet we still code the same. More information is needed to be displayed in the code? Add more text.
Anyways, this is getting a bit tangential so I'll stop here.

One reply I got to the second code snippet is that you would probably first unpack the array to some named variables and then use those but the variable's name can mean many things and the way it's called doesn't necessarily tell you the way it's supposed to be interpreted in the context of the called function.
In the local scope, you might have two rectangles named leftRectangle and rightRectangle because that's what they semantically represent, but it doesn't need to extend to what they represent when given to a function.

In fact, if your variables are named in the context of the called function than you're introducing less information than you potentially could with that function call and on some level if does lead to code worse code. If you have a procedure that results in a rectangle you store in rectForClipping and then another procedure that provides rectForDrawing, then the actual call to DrawRectangleClipped is just ceremony. A line that means nothing new and is there just so the computer knows what exactly you want even though you've explained it already with your naming. This isn't a good thing.

I'd really love to hear fresh perspectives on this. I'm sure I'm not the first one to consider this a problem, so how is it solved?

Body:
Mostly it's solved by good naming of functions, parameters, and arguments.  You already explored that and found it had deficiencies, however.  Most of those deficiencies are mitigated by keeping functions small, with a small number of parameters, both in the calling context and the called context.  Your particular example is problematic because the function you are calling is trying to do several things at once:  specify a base rectangle, specify a clipping region, draw it, and fill it with a specific color.

This is kind of like trying to write a sentence using only the adjectives.  Put  more verbs (function calls) in there, create a subject (object) for your sentence, and it's easier to read:

rect.clip(clipRect).fill(color)

Even if clipRect and color have terrible names (and they shouldn't), you can still discern their types from the context.

Your deserialized example is problematic because the calling context is trying to do too much at once:  deserializing and drawing something.  You need to assign names that make sense and clearly separate the two responsibilities.  At a minimum, something like this:

(rect, clipRect, color) = deserializeClippedRect()
rect.clip(clipRect).fill(color)

A lot of readability problems are caused by trying to be too concise, skipping intermediate stages that humans require to discern context and semantics.


答:
I agree that the way functions are often used can be a confusing part of writing code, and especially reading code.

The answer to this problem partly depends on the language. As you mentioned, C# has named parameters. Objective-C's solution to this problem involves more descriptive method names. For example, stringByReplacingOccurrencesOfString:withString: is a method with clear parameters.

In Groovy, some functions take maps, allowing for a syntax like the following:

```<code>restClient.post(path: 'path/to/somewhere',
            body: requestBody,
            requestContentType: 'application/json')
</code>```

In general, you can solve this issue by limiting the number of parameters you pass to a function. I think 2-3 is a good limit. If it appears that a function needs more parameters, it causes me to re-think the design. But, this can be harder to answer generally. Sometimes you are trying to do too much in a function. Sometimes it makes sense to consider a class for storing your parameters. Also, in practice, I often find that functions which take large numbers of parameters normally have many of them as optional.

Even in a language like Objective-C it makes sense to limit the number of parameters. One reason is that many parameters are optional. For an example, see rangeOfString: and its variations in NSString.

A pattern I often use in Java is to use a fluent-style class as a parameter. For example:

```<code>something.draw(new Box().withHeight(5).withWidth(20))
</code>```

This uses a class as a parameter, and with a fluent-style class, makes for easily readable code.

The above Java snippet also helps where the ordering of parameters may not be so obvious. We normally assume with coordinates that X comes before Y. And I normally see height before width as a convention, but that is still not very clear (something.draw(5, 20)).

I've also seen some functions like drawWithHeightAndWidth(5, 20) but even these can't take too many parameters, or you'd start to lose readability.

Body:
I agree that the way functions are often used can be a confusing part of writing code, and especially reading code.

The answer to this problem partly depends on the language. As you mentioned, C# has named parameters. Objective-C's solution to this problem involves more descriptive method names. For example, stringByReplacingOccurrencesOfString:withString: is a method with clear parameters.

In Groovy, some functions take maps, allowing for a syntax like the following:

restClient.post(path: 'path/to/somewhere',
            body: requestBody,
            requestContentType: 'application/json')

In general, you can solve this issue by limiting the number of parameters you pass to a function. I think 2-3 is a good limit. If it appears that a function needs more parameters, it causes me to re-think the design. But, this can be harder to answer generally. Sometimes you are trying to do too much in a function. Sometimes it makes sense to consider a class for storing your parameters. Also, in practice, I often find that functions which take large numbers of parameters normally have many of them as optional.

Even in a language like Objective-C it makes sense to limit the number of parameters. One reason is that many parameters are optional. For an example, see rangeOfString: and its variations in NSString.

A pattern I often use in Java is to use a fluent-style class as a parameter. For example:

something.draw(new Box().withHeight(5).withWidth(20))

This uses a class as a parameter, and with a fluent-style class, makes for easily readable code.

The above Java snippet also helps where the ordering of parameters may not be so obvious. We normally assume with coordinates that X comes before Y. And I normally see height before width as a convention, but that is still not very clear (something.draw(5, 20)).

I've also seen some functions like drawWithHeightAndWidth(5, 20) but even these can't take too many parameters, or you'd start to lose readability.


问:
Designing unit tests for a stateful system

Background

Test Driven Development was popularized after I already finished school and in the industry. I am trying to learn it, but some major things still escape me. TDD proponents say lots of things like (hereafter referred to as the "single assertion principle" or SAP):

For some time I've been thinking about how TDD tests can be as simple, as expressive, and as elegant as possible. This article explores a bit about what it's like to make tests as simple and decomposed as possible: aiming for a single assertion in each test.

Source: http://www.artima.com/weblogs/viewpost.jsp?thread=35578

They also say things like this (hereafter referred to as the "private method principle" or PMP):

You generally don't unit test private methods directly. Since they are private, consider them an implementation detail. Nobody is ever going to call one of them and expect it to work a particular way.

You should instead test your public interface. If the methods that call your private methods are working as you expect, you then assume by extension that your private methods are working correctly.

Source: How do you unit test private methods?

Situation

I am trying to test a stateful data processing system. The system can do different things for the exact same piece of data given what it's state was prior to receiving that data. Consider a straightforward test that builds up state in the system, then tests the behavior that the given method is intended to test.

SAP suggests that I should not be testing the "state build up procedure", I should assume that the state is what I expect from the build up code and then test the one state change I'm trying to test

PMP suggests that I can't skip this "state build up" step and just test the methods that govern that functionality independently.

The result in my actual code has been tests that are bloated, complicated, long, and difficult to write. And if the state transitions change, the tests have to be changed... which would be fine with small, efficient tests but extremely time consuming and confusing with these long bloated tests. How is this normally done?

答:
Perspective:

So let's take a step back and ask what TDD is trying to help us with.  TDD is trying to help us determine if our code is correct or not.  And by correct, I mean "does the code meet the business requirements?"  The selling point is that we know changes will be required in the future, and we want to make sure that our code remains correct after we make those changes.

I bring that perspective up because I think it's easy to get lost in the details and lose sight of what we're trying to achieve.

Principles - SAP:

While I'm not an expert in TDD, I think you're missing part of what Single Assertion Principle (SAP) is trying to teach.  SAP can be restated as "test one thing at a time."  But TOTAT doesn't roll off the tongue as easily as SAP does.

Testing one thing at a time means that you focus on one case; one path; one boundary condition; one error case; one whatever per test.  And the driving idea behind that is you need to know what broke when the test case fails, so you can resolve the issue more quickly.  If you test multiple conditions (ie. more than one thing) within a test and the test fails, then you have a lot more work on your hands.  You first have to identify which of the multiple cases failed and then figure out why that case failed.

If you test one thing at a time, your search scope is a lot smaller and the defect is identified more quickly.  Keep in mind that "test one thing at a time" doesn't necessarily exclude you from looking at more than one process output at a time.  For example, when testing a "known good path", I may expect to see a specific, resulting value in foo as well as another value in bar and I may verify that foo != bar as part of my test.  The key is to logically group the output checks based upon the case being tested.

Principles - PMP:

Likewise, I think you're missing a bit about what Private Method Principle (PMP) has to teach us.  PMP encourages us to treat the system like a black box.  For a given input, you should get a given output.  You don't care how the black box generates the output.  You only care that your outputs align with your inputs.

PMP is really good perspective for looking at the API aspects of your code.  It can also help you scope what you have to test.  Identify your interface points and verify that they meet the terms of their contracts.  You don't need to care about how the behind-the-interface (aka private) methods do their job.  You just need to verify they did what they were supposed to do.

Applied TDD (for you)

So your situation presents a bit of a wrinkle beyond an ordinary application.  Your app's methods are stateful, so their output is contingent upon not only the input but also what's been previously done.  I'm sure I should <insert some lecture> here about state being horrible and blah blah blah, but that really doesn't help solve your problem.

I'm going to assume you have some sort of state diagram table that shows the various potential states and what needs to be done in order to trigger a transition.  If you don't, you're going to need it as it will help express the business requirements for this system.

The Tests: First, you're going to end up with a set of tests that enact state change.  Ideally, you'll have tests that exercise the full range of state changes that can occur but I can see a few scenarios where you may not need to go that full extent.

Next, you need to build tests to validate the data processing.  Some of those state tests will be reused when you create the data processing tests.  For example, suppose you have a method Foo() that has different outputs based upon an Init and State1 states.  You'll want to use your ChangeFooToState1 test as a setup step in order to test the output when "Foo() is in State1".

There's some implications behind that approach that I want to mention.  Spoiler, this is where I'll infuriate the purists

First off, you have to accept that you using something as a test in one situation and a setup in another situation.  On the one hand, this seems to be a direct violation of SAP.  But if you logically frame ChangeFooToState1 as having two purposes then you're still meeting the spirit of what SAP is teaching us.  When you need to make sure Foo() changes states, then you use ChangeFooToState1 as a test.  And when need to validate "Foo()'s output when in State1" then you're using ChangeFooToState1 as a setup.

The second item is that from a practical point of view, you're not going to want fully randomized unit testing for your system.  You ought to run all of the state change tests prior to running the output validation tests.  SAP is kind of the guiding principle behind that ordering.  To state what should be obvious - you can't use something as setup if it fails as a test.

Putting it together:

Using your state diagram, you'll generate tests to cover the transitions.  Again, using your diagram, you generate tests to cover all of the input / output data processing cases driven by state.

If you follow that approach, the bloated, complicated, long, and difficult to write tests should get a little bit easier to manage.  In general, they should end up smaller and they should be more concise (ie less complicated).  You should notice that the tests are more decoupled or modular as well.

Now, I'm not saying the process will be completely pain free because writing good tests does take some effort.  And some of them will still be difficult because you're mapping a second parameter (state) over quite a few of your cases.  And as an  aside, it should be a little more apparent why a stateless system is a easier to build tests for.  But if you adapt this approach for your application, you should find that you are able to prove your application is working correctly.


问:
How to handle asynchronous calls in an offline application
I'm using an offline application's javascript API and I'd like to know if I can use deferred objects to handle the callbacks. The API calls do not use HTTP, the calls are to and from the applications local database.

The only way I've been able to display information is by using 'setTimeout' on subsequent calls, which I know, is terrible! So I have a long list of callbacks and timeouts.

```<code>var jsObj = {};
var anotherObj = {};

//first async call
methodName("arg1", jsObj, callback);

function callback(result){

  jsObj["data"] = result;

}

//second async call
setTimeout(function(){methodName("arg1", anotherObj, callback2);}, 200);

function callback2(result){

  jsObj["data"] = result;

}

//wait
setTimeout(function(){

  $("#content").html(JSON.stringify(jsObj));

},300);
</code>```

Is there anyway to refactor this? Any advice is appreciated. I've looked at the following post, but I'm not sure it would work. I'm aware I can create custom deferred objects (jQuery), but altering the API to use this or any other method of promise objects seems unrealistic.

答:
Deferred objects are indeed the way to go here.  You don't have to change the API, just use a promise library like Q that can wrap it.  It would look something like this:

promiseMethodName = Q.denodeify(methodName);

promiseMethodName("arg1", jsObj).then(function(result) {
  jsObj["data"] = result;
  return result;
}).then(function(result) {
  return promiseMethodName("arg1", anotherObj);
}).then(function(result) {
  jsObj["data"] = result;
  return result;
}).then(function(result) {
  $("#content").html(JSON.stringify(jsObj));
});
```

If your API doesn't use node-style callbacks, you might need to implement your own version of denodeify, which can be tricky, but the rest will be the same.

Body:
Deferred objects are indeed the way to go here.  You don't have to change the API, just use a promise library like Q that can wrap it.  It would look something like this:

promiseMethodName = Q.denodeify(methodName);

promiseMethodName("arg1", jsObj).then(function(result) {
  jsObj["data"] = result;
  return result;
}).then(function(result) {
  return promiseMethodName("arg1", anotherObj);
}).then(function(result) {
  jsObj["data"] = result;
  return result;
}).then(function(result) {
  $("#content").html(JSON.stringify(jsObj));
});

If your API doesn't use node-style callbacks, you might need to implement your own version of denodeify, which can be tricky, but the rest will be the same.


问:
How to store the file names, start offset and length while avoiding the issue of self imposed limits (lookup table) or having to scan the entire file?
I am attempting to learn more about C and it's descendants(C++ mainly).  I have decided that I would like to create a "file system" of sorts.  Not a particularly advanced one mind you but something to play with.  I have no intentions of making it mountable, securable or even recoverable.

At the moment I am stuck in concept land with trying to decide how to implement the MFT/FAT.

At first I thought that I would just use the first X number of bytes to store a lookup table, when I realized that there would then be a limitation to the number of files I could store I thought maybe use some type of metadata with each file but then I would have to scan the entire filesystem to locate a file.

I have read through this and this although the z80 link seems like it is more up my alley.

From a high level I want to be able to issue a command like:

```<code>./myfs funnycat.jpg mystorage.mfs
</code>```

Essentially appending binary data to the end of mystorage.mfs

How can I store the information that would contain the file names, start offset and length while avoiding the issue of self imposed limits (lookup table length) or having to scan the entire file (metadata with binary data)?

Concise Explanation
I am looking for a way to label binary data stored in a single contiguous file so that I can pull data from a given offset range or by string.

```<code>./myfs mystorage.mfs funnycat.jpg
</code>```

Likely in order to accomplish this I will add some logic to myfs to check the first argument for signs that it is a blob containing other files or not.

答:
Below are some possible solutions to some of the problems have mentioned.

How do I store a number of arbitrary size?

A simple approach is to use a Variable Length Quantity.  Basically, for each octet used to represent your number, 7/8 of its bits are used to represent the number and the extra bit is set to 1 if there are any more octets.  You can also just pick some "unreachable" size and write code to support this fixed size.

How can I tell when I've reached the end of a file?

You can use some form of file allocation table (you can represent it as a linked list to allow it to grow to arbitrary size).

I want to support disk fragmentation.  I.e., I should be able to grow a file, even if the file is about to overlap another file, without either file.

Have each file be made up of clusters of some fixed size.  Each cluster will have a header with information like:

How much of the cluster is allocated? (clusters are fixed size, a cluster's content may not be fully populated with data).

Where is the next cluster? (A file may need more than one cluster.  You could store this information in an allocation table instead, of course).

Is this cluster allocated? (If this information is in the cluster rather than in an allocation table, you'll be forced to "format" your entire "hard drive" using a "slow format." Using a file allocation table allows you to perform a "quick format." Mind you, you could cheat by leveraging your existing file system (i.e., you can assume byte is initialized to 0).


问:
Most efficient way to store this collection of moduli and remainders?
I have a huge collection of different moduli and associated with each modulus a fairly large list of remainders. I want to store these values so that I can efficiently determine whether an integer is equivalent to any one of the remainders with respect to any of the moduli (it doesn't matter which, I just want a true/false return).

I thought about storing these values as a linked-list of balanced binary trees, but I was wondering if there is a better way?

EDIT

Perhaps a little more detail would be helpful. As for the size of this structure, it will be holding about 10s of thousands of (prime-1) moduli and associated to each modulus will be a variable amount of remainders. Most moduli will only have one or two remainders associated to it, but a very rare few will have a couple hundred associated to it.

This is part of a larger program which handles numbers with a couple thousand (decimal) digits. This program will benefit more from this table being as large as possible and being able to be searched quickly.

Here's a small part of the dataset where the moduli are in parentheses and the remainders are comma separated:

```<code>(46) k = 20
(58) k = 15, 44
(70) k = 57
(102) k = 36, 87
(106) k = 66
(156) k = 20, 59, 98, 137
(190) k = 11, 30, 68, 87, 125, 144, 182
(430) k = 234
(520) k = 152, 282
(576) k = 2, 11, 20, 29, 38, 47, 56, 65, 74, ...(add 9 each time), 569
</code>```

I had said that the moduli were prime, but I was wrong they are each one below a prime.

答:
Your best bet is going to be to parse the data as you read it in, and throw away what you don't need.  Since you don't care which modulus a given remainder corresponds to, or how many times a given remainder shows up, you can just keep a list of remainders.

In pseudocode, building this might look something like:

```<code>while (data = ReadNextModulus())
   foreach (rem in data.Remainders)
      if !allRemainders.HasRemainder(rem) // We do this check because presumably lookups are faster
         allRemainders[rem] = true        // than inserts, so we want to skip the insert if possible.
</code>```

Then when you're done and actually using it, you just check allRemainders.HasRemainder(rem).

In terms of the best data structure to store this, I don't have any specific recommendations myself, but I can point you in the right direction.  You're going to have a list of int values which you need fast lookup for.  The naive implementation would be to store it as an unsorted array of ints.  This would have O(n) time.  If you sorted it before using it, that'd reduce to O(log n), presuming you use a binary search.

There's other options out there, though.  This question talks about it.  The answers suggest trying a Van Emde Boas tree, a bitmap, or just the previously discussed sorted array.  You'll need to take a look at them, consider the sparseness of the data you have, and what your language of choice has as built in data structures, and pick the one you think will be best.

But the key is that you're just storing whether a given remainder has been seen, not the full data structure.

Body:
Your best bet is going to be to parse the data as you read it in, and throw away what you don't need.  Since you don't care which modulus a given remainder corresponds to, or how many times a given remainder shows up, you can just keep a list of remainders.

In pseudocode, building this might look something like:

while (data = ReadNextModulus())
   foreach (rem in data.Remainders)
      if !allRemainders.HasRemainder(rem) // We do this check because presumably lookups are faster
         allRemainders[rem] = true        // than inserts, so we want to skip the insert if possible.

Then when you're done and actually using it, you just check allRemainders.HasRemainder(rem).

In terms of the best data structure to store this, I don't have any specific recommendations myself, but I can point you in the right direction.  You're going to have a list of int values which you need fast lookup for.  The naive implementation would be to store it as an unsorted array of ints.  This would have O(n) time.  If you sorted it before using it, that'd reduce to O(log n), presuming you use a binary search.

There's other options out there, though.  This question talks about it.  The answers suggest trying a Van Emde Boas tree, a bitmap, or just the previously discussed sorted array.  You'll need to take a look at them, consider the sparseness of the data you have, and what your language of choice has as built in data structures, and pick the one you think will be best.

But the key is that you're just storing whether a given remainder has been seen, not the full data structure.


问:
Hashing + security as it pertains to a theoretical file sharing site
These days it's possible to hash a file client-side, send the hash to the server, and have the server check whether or not that file is already uploaded. If it is, we can skip the file upload and make it appear to upload almost instantly to the user. This can also save have oodles of bandwidth for a large site.

This has some important security implications, however.

If the user is clever, he can use this information to determine what files are already on the server, including illegal or sensitive documents

He can produce millions of fake hashes, send them to the server, and hope for a collision to gain access to random documents

There is a legitimate but unintentional collision where the hashes are the same, but the file is actually different

My question is: is it worth the trade-off? Can we mitigate some or all of these problems? How?

答:
Sure, if you suspect that a certain illicit file is on the server you can upload another copy and deduce that it is there. But deducing isn't proof in a court of law; you'd still have to get the repo owner subpoenaed  to disclose which documents are or aren't on their systems, otherwise you may or may not convince a court that they really do have that file. And you yourself deliberately uploading the same file might weaken your own case - if you willingly put it onto an internet storage service, how secret or valuable can it be?

Since the hashes are used only internally by the storage provider, I don't see how a user sending hash values could compromise or, indeed, achieve anything.

The popular hash functions are good enough that this isn't a concern. Many of them are broken with respect to an intelligent, capable attacker who actively tries to generate hash collisions, e.g. to conduct a birthday attack. But two files that different people generate and upload are still extremely unlikely to clash - unlikely as in "you should worry about a meteor hitting the data center instead".


答:
This is a bad approach.  And I believe its how Dropbox got busted a while back.

The approach would work if:

The files are unencrypted on the server and you simply copy the file over without an upload (meaning anyone can see their contents)

You use a shared encryption key for all files and you simply copy the files over without an upload (meaning you can see the contents)

You store encryption keys on the server to allow you to decrypt and re-encrypt the files when doing such a copy (meaning you can see the contents).

Each of those options, IMO, are a serious privacy issue.  If you want it to be secure and private, you'll want a way to derive a per user key that you do not store and/or do the encryption on the client side.

Title: Hashing + security as it pertains to a theoretical file sharing site

Body:
These days it's possible to hash a file client-side, send the hash to the server, and have the server check whether or not that file is already uploaded. If it is, we can skip the file upload and make it appear to upload almost instantly to the user. This can also save have oodles of bandwidth for a large site.

This has some important security implications, however.

If the user is clever, he can use this information to determine what files are already on the server, including illegal or sensitive documents

He can produce millions of fake hashes, send them to the server, and hope for a collision to gain access to random documents

There is a legitimate but unintentional collision where the hashes are the same, but the file is actually different

My question is: is it worth the trade-off? Can we mitigate some or all of these problems? How?