问:
Better to use error monad with validation in your monadic functions, or implement your own monad with validation directly in your bind?
I'm wondering what's better design wise for usability/maintainability, and what's better as far as fitting with the community.

Given the data model:

```<code>type Name = String

data Amount = Out | Some | Enough | Plenty deriving (Show, Eq)
data Container = Container Name deriving (Show, Eq)
data Category = Category Name deriving (Show, Eq)
data Store = Store Name [Category] deriving (Show, Eq)
data Item = Item Name Container Category Amount Store deriving Show
instance Eq (Item) where
  (==) i1 i2 = (getItemName i1) == (getItemName i2)

data User = User Name [Container] [Category] [Store] [Item] deriving Show
instance Eq (User) where
  (==) u1 u2 = (getName u1) == (getName u2)
</code>```

I can implement monadic functions to transform the User for instance by adding items or stores etc, but I may end up with an invalid user so those monadic functions would need to validate the user they get and or create.

So, should I just:

wrap it in an error monad and make the monadic functions execute the validation

wrap it in an error monad and make the consumer bind a monadic validation function in the sequence that throws the appropriate error response (so they can choose not to validate and carry around an invalid user object)

actually build it into a bind instance on User effectively creating my own kind of error monad that executes validation with every bind automatically

I can see positives and negatives to each of the 3 approaches but want to know what is more commonly done for this scenario by the community.

So in code terms something like, option 1:

```<code>addStore s (User n1 c1 c2 s1 i1) = validate $ User n1 c1 c2 (s:s1) i1
updateUsersTable $ someUser >>= addStore $ Store "yay" ["category that doesnt exist, invalid argh"]
</code>```

option 2:

```<code>addStore s (User n1 c1 c2 s1 i1) = Right $ User n1 c1 c2 (s:s1) i1
updateUsersTable $ Right someUser >>= addStore $ Store "yay" ["category that doesnt exist, invalid argh"] >>= validate
-- in this choice, the validation could be pushed off to last possible moment (like inside updateUsersTable before db gets updated)
</code>```

option 3:

```<code>data ValidUser u = ValidUser u | InvalidUser u
instance Monad ValidUser where
    (>>=) (ValidUser u) f = case return u of (ValidUser x) -> return f x; (InvalidUser y) -> return y
    (>>=) (InvalidUser u) f = InvalidUser u
    return u = validate u

addStore (Store s, User u, ValidUser vu) => s -> u -> vu
addStore s (User n1 c1 c2 s1 i1) = return $ User n1 c1 c2 (s:s1) i1
updateUsersTable $ someValidUser >>= addStore $ Store "yay" ["category that doesnt exist, invalid argh"]
</code>```

答:
Fist I'd ask myself: Is having an invalid User a code bug or a situation that can normally occur (for example someone entering a wrong input to your application). If it's a bug, I'd try to ensure that it can never happen (like using smart constructors or creating more sophisticated types).

If it's a valid scenario then some error processing during runtime is appropriate. Then I'd ask: What does it really mean for me that a User is invalid?

Does it mean that an invalid User can make some code fail? Do parts of your code rely on the fact that a User is always valid?

Or does it just mean that its an inconsistency that needs to be fixed later, but doesn't break anything during the computation?

If it is 1., I'd definitely go for some kind of error monad (either standard or your own), otherwise you'll lose guarantees that your code is functioning properly.

Creating your own monad or using a stack of monad transformers is another issue, maybe this will be helpful: Has anyone ever encountered a Monad Transformer in the wild?.

Update: Looking at your expanded options:

Looks as the best way to go. Perhaps, to be really safe, I'd rather hide the constructor of User and instead export only a few functions that don't allow to create an invalid instance.  This way you'll be sure that any time it happens it gets handled properly. For example, a generic function for creating a User could be something like

```<code>user :: ... -> Either YourErrorType User
-- more generic:
user :: (MonadError YourErrorType m) ... -> m User
-- Or if you actually don't need to differentiate errors:
user :: ... -> Maybe User
-- or more generic:
user :: (MonadPlus m) ... -> m User
-- etc.
</code>```

Many libraries take a similar appropach, for example Map, Set or Seq hide the underlying implementation so that it's not possible to create a structure that doesn't obey their invariants.

If you postpone validation to the very end, and use Right ... everywhere, you don't need a monad any more. You can just do pure computations and resolve any possible errors at the end. IMHO this approach is very risky, as an invalid user value can lead to having invalid data elsewhere, because you didn't stop the computation soon enough. And, if it happens that some other method updates the user so that it's valid again, you'll end up with having invalid data somewhere and not even knowing about it.

There are several problems here.

The most important is that a monad must accept any type parameter, not just User. So your validate would have to have type u -> ValidUser u without any restriction on u. So it's not possible to write such a monad that validates inputs of return, because return must be fully polymorhpic.

Next, what I don't understand is that you match on case return u of in the definition of >>=. The main point of ValidUser should be to distinguish valid and invalid values, and so the monad must ensure that this is always true. So it could be simply

```<code>(>>=) (ValidUser u) f = f u
(>>=) (InvalidUser u) f = InvalidUser u
</code>```

And this already looks very much like Either.

Generally, I'd use a custom monad only if

There are no existing monads that provide you with the functionality you need. Existing monads usually have many support functions, and more importantly, they have monad transformers so you can compose them into monad stacks.

Or if you need a monad that is too complex to describe as a monad stack.

Body:
Fist I'd ask myself: Is having an invalid User a code bug or a situation that can normally occur (for example someone entering a wrong input to your application). If it's a bug, I'd try to ensure that it can never happen (like using smart constructors or creating more sophisticated types).

If it's a valid scenario then some error processing during runtime is appropriate. Then I'd ask: What does it really mean for me that a User is invalid?

Does it mean that an invalid User can make some code fail? Do parts of your code rely on the fact that a User is always valid?

Or does it just mean that its an inconsistency that needs to be fixed later, but doesn't break anything during the computation?

If it is 1., I'd definitely go for some kind of error monad (either standard or your own), otherwise you'll lose guarantees that your code is functioning properly.

Creating your own monad or using a stack of monad transformers is another issue, maybe this will be helpful: Has anyone ever encountered a Monad Transformer in the wild?.

Update: Looking at your expanded options:

Looks as the best way to go. Perhaps, to be really safe, I'd rather hide the constructor of User and instead export only a few functions that don't allow to create an invalid instance.  This way you'll be sure that any time it happens it gets handled properly. For example, a generic function for creating a User could be something like

user :: ... -> Either YourErrorType User
-- more generic:
user :: (MonadError YourErrorType m) ... -> m User
-- Or if you actually don't need to differentiate errors:
user :: ... -> Maybe User
-- or more generic:
user :: (MonadPlus m) ... -> m User
-- etc.

Many libraries take a similar appropach, for example Map, Set or Seq hide the underlying implementation so that it's not possible to create a structure that doesn't obey their invariants.

If you postpone validation to the very end, and use Right ... everywhere, you don't need a monad any more. You can just do pure computations and resolve any possible errors at the end. IMHO this approach is very risky, as an invalid user value can lead to having invalid data elsewhere, because you didn't stop the computation soon enough. And, if it happens that some other method updates the user so that it's valid again, you'll end up with having invalid data somewhere and not even knowing about it.

There are several problems here.

The most important is that a monad must accept any type parameter, not just User. So your validate would have to have type u -> ValidUser u without any restriction on u. So it's not possible to write such a monad that validates inputs of return, because return must be fully polymorhpic.

Next, what I don't understand is that you match on case return u of in the definition of >>=. The main point of ValidUser should be to distinguish valid and invalid values, and so the monad must ensure that this is always true. So it could be simply

(>>=) (ValidUser u) f = f u
(>>=) (InvalidUser u) f = InvalidUser u

And this already looks very much like Either.

Generally, I'd use a custom monad only if

There are no existing monads that provide you with the functionality you need. Existing monads usually have many support functions, and more importantly, they have monad transformers so you can compose them into monad stacks.

Or if you need a monad that is too complex to describe as a monad stack.


问:
Service layer design
I am developing an MVC website in PHP, and for the first time, I would like to implement a service layer. I have some design considerations I would like to get some advice on. The backend will by no means be a large enterprise system, so I am looking to keep things relatively simple while still leveraging the benefits of a service layer.

The idea is that my controllers will interact with services, which in turn will interact with DAOs. My main question is how granular my services should be. I am familiar with the concept of service granularity in SOA, but have never implemented this in practice. For instance, to handle user registration, should I have a RegistrationService class, or should I have a UserService which contains a register method along with other user-related services? The former approach means that I will have a service layer full of small or fine-grained services, but I guess that is a good thing. The latter seems to hinder service reuse and result in large classes with many methods.

Instead, perhaps I should combine the two approaches to ensure that my services can be reused? For instance, have a RegistrationService which interacts with a DAO class. Then, I could have a UserService class with a register method. This method interacts with the fine-grained RegistrationService. This way, the RegistrationService can easily be reused to compose more coarse-grained services. Surely there are examples where the reuse would be more likely; for instance, placing an order would need to use/orchestrate many different services. I guess you could call this a "layered" approach, which I have seen discussed before. Could I structure this in a way such that my service layer will not become a mess?

I would love to hear your thoughts on this, whether you have other suggestions, recommendations and experiences or comments on the ideas I presented above. Thank you in advance.

答:
Well, I think RegistrationService is more intent revealing.

One can easily speculate on what function can be found there such as registerUser(), ConfirmRegisteration(), ResetPassword(),...etc. To me most of them seem to fall logically in a RegisterionService class than a general UserService class.

A service layer is supposed to contain functions that operate on more than one Entity, so a UserService may not logically be the best place for such operations.

ContentLicense: CC BY-SA 3.0

Title: Service layer design


问:
Should heavy binary files not be stored in database?
I was asked an interesting question: Should a database contains all data? Or heavy binary files should be stored in file system?

Example of heavy binary files : videos or heavy pdf files (+200 MB)

With an old aspx web app (1.1) I tried to open a 200MB pdf file stored as a blob in an Oracle 11g database, and it just run out of memory.

However, same asp.net web application had no problem to open same pdf file stored in file system of a server.  It could be that maybe there is some proper way to open heavy blobs fields with asp.net.

For integrity reasons, I say that all data should be stored in database, but my described case showed me that maybe it's not the way.

I read once that if your web application goes to cloud, then it would be very difficult to keep references of files stored in file system (file paths like: ../MyFolder001/MyFile.mpg), since you don't know where those files will be distributed.

Should heavy binary files not be stored in database?

答:
In every company I've worked at the general consensus has always been to store file locations within the database and the files elsewhere.

Firstly, you do not know what size of files people are uploading or how many times. There are ways to measure the size of a file before it goes in the database and there will be programmatic ways to stop people uploading multiple versions but the point I'm trying to make is that you don't want your database's performance being hindered by someone uploading a 2GB file, or 20 people trying to download that 2GB file at the same time. The chances are your database is also having to deal with lots of other smaller queries at the same time. Help it to help you!

This leads onto my second point. Separation of concerns. A file server can look after the files, a database can store the locations of those files. Problems with storage of files, detecting and preventing viruses in files can be handled separately.

I've worked mainly with websites and applications so the reasons I'm giving are influenced by that.


问:
How to refactor "nested" view classes to avoid deep method calls?
Lets say I'm displaying a bunch of data (model) using a View class for rendering. However, a lot of the data has sub-data (models) complicated enough to require separate rendering classes.

In my design, a View class has a model which it is rendering, and has many children Views which display the sub-data. In some cases, A View, while containing a model, may not have anything to render and serves more as a wrapper for it children.

However, if you have very complex data and your sub-views have subviews,this design results in deeply nested method calls. Some methods are simply passing information to view classes who may do nothing with it except pass it to their children. This seems inefficient, so I figured there might to be a pattern or something that solves this more elegantly.

答:
On the one hand we teach ourselves to use design patterns and common sense design techniques, from which emerges the ideal of designing close to conceptual models, and, on the other hand, we're concerned when our resulting models become deep. And we shouldn't be.

If, conceptually, your views are aggregations of other views, then I see no problem with nested structures and the resulting delegation model. I see no problem with views delegating as part of fulfilling their role, as long as their interface is consistent with their purpose and speaks at a consistent level of abstraction.

Let me restate this. If you were to explain your view to colleagues, and the explanation best describes the view in terms of other views, then that is what your code should be like.

In fact, views that delegate may be simply working to realize the Law of Demeter (not something to go fanatic about, but definitely something nice to recognize in your designs).

I should say that I apply the above principles in my user interface code with good results. Event handling can become verbose, but you have EventBus to the rescue.


问:
Why is a Boolean value stored as a byte inside of a computer when it only requires one bit
I recently started learning to write code, and in my book I came across this question. "Why is a Boolean value stored as a byte inside of a computer when it only requires one bit?" can someone shed more light on this question?</p>

答:
It has to do with what the CPU can easily address.  For example on an x86 processor there is an eax (32 bits), ax (16 bits) and a ah (8 bits) but no single bit register.  So in order for it to use a single bit the CPU will have to do a read/modify/write to change the value.  If it is stored as a byte a single read or write can be used to inspect/change the value.

Additionally one might wonder if it would be better to use a single bit vs a full byte, after all a byte will be wasting 7 bits.  Unless space is a constraint the one should go for the byte because, at least the x86 and I think others, there is usually an instructions to quickly set/clear a bool which is much quicker than the read/modify/write of a single bit.  From personal measurements I have seen the read/mod/write method be 5x slower than the single instruction method.


问:
Why Java does not allow function definitions to be present outside of the class?
Unlike C++, in Java, we cannot have just function declarations in the class and definitions outside of the class. Why is it so?

Is it to emphasize that a single file in Java should contain only one class and nothing else?

答:
The real question is what would be the merit of continuing to do things the C++ way and what was the original purpose of the header file? The short answer is that the header file style allowed for quicker compile times on large projects in which many classes could potentially reference the same type. This is not necessary in JAVA and .NET due to the nature of the compilers.

See this answer here: Are header files actually good?


答:
I believe the answer is, per Wikipedia, that Java was designed to be simple and object oriented. Functions are meant to operate on the classes they are defined in. With that line of thinking, having functions outside of a class doesn't make sense. I am going to leap to the conclusion that Java doesn't allow it because it didn't fit with pure OOP.

A quick Google search for me didn't yield much on Java language design motivations.

Title: Why Java does not allow function definitions to be present outside of the class?

Body:
Unlike C++, in Java, we cannot have just function declarations in the class and definitions outside of the class. Why is it so?

Is it to emphasize that a single file in Java should contain only one class and nothing else?


问:
Are "conditional" caching policies ever actually useful?
I'm designing an abstraction over ASP.Net's built in caching to make it not so horrible to use. One design decision I'm having to look at is if I should restrict people to using just one cache policy on each item.

For example, say you're caching the HTML generated by your blog post or some such. Would you ever want to cache it sometimes for 30 minutes and sometimes for 2 hours? Note: this isn't cache invalidation.

These are the policies available:

Priority

Absolute expiration

Sliding expiration (where each access bumps the expiration by this much)

Are there any use cases where someone would want to add an item to the cache with one set of policies and in another portion of the web application use a different set of policies when it gets added(but of course, if it already exists keep with the old policy)? Are there any real world uses to allowing this?

答:
This one time, at band camp...

I've build a similar cache mechanism to what you're describing to counter-act (predictable) traffic spikes. It was a hack, and the only reason we went for it was that (at the time) we couldn't afford to upgrade our infrastructure to cope with the intense spikes and because it was quite easy to predict exactly what resources would be in demand during those spikes and when the spikes would happen (niche market + tons of existing traffic data).

The inspiration from the hack was a similar hack I've seen used in a (then) popular CMS platform to counter-act the Slashdot effect. The system would react to tons of visitors in a short time by automatically increasing TTLs (If I remember correctly, the increase was logarithmic).

In any case, conditional/adaptive caching is not uncommon, it's common enough that we have quite a few awesome algorithms for it. The question is, do you really need it? If not, well, I'm sure you know what they say about premature optimization...


答:
I think it's an artifact of the class loading mechanism.
Each class file is a container for a loadable object.
There is no place "outside" of class files.</p>


问:
How to fix bad fundamentals?
I am a native PHP developer, and have been for about a year or so. I love PHP and it was very easy for me to learn, but I have developed some bad habits along the way due to never having a formal education in programming. It's pretty easy to get the job done in PHP without writing the best code.

I am looking to work more with another language; not because I don't like PHP but because I want a clean slate to learn programming fundamentals. I am looking to stay with web development, so Ruby or Python seem to be where I'm leaning, though I am open to other suggestions. The more important question is, how do I approach learning this new language to really become a good programmer rather than a guy that can "get the job done"?

答:
You can learn to program in a cleaner way with Php.

Start by using a framework if you're not doing that already; read some code from big-scaled and well-reputed Php applications. You should also study design patterns and try to apply them.

In any case, having a clean code doesn't come automatically from using some language or tool; it's something you have to work on.

Title: How to fix bad fundamentals?


问:
Does javascript support numerically indexed arrays with a more optimized algorithm than an associative array?
I know that Python, Perl, Java, Lua and obviously C (as it's the only array that's in ANSI standard afaik) support faster looking of numerically indexed arrays than doing a hash lookup or anything like that.  Does Javascript also?

As an example in code of what I mean, in the case of Perl:

```<code>for(i=0;i<10;i++)
{
  # Do something
}
</code>```

is slower than:

```<code>for (1..9)
{
  # do something else
}
</code>```

or:

```<code>@var = (1,2,3)
foreach(@var)
{
  print $_; # look I'm fancy
}
</code>```

and faster than:

```<code>foreach my $key (keys %hash)
{
  print $_; # Look I'm fancy
}
</code>```

and in python, given the following:

```<code>class thisClass:
  def methodOne(i):
    return i+1

thisDict = {
  'number': 1
}
</code>```

These two operations are similar in speed because both involve a hash lookup into a same-widthed hash:

```<code>thisObject = new thisClass
i  = thisObject.methodOne(1)
</code>```

and:

```<code>i = 1+thisDict['1'];
</code>```

Both of these kinds of lookups are slower than this:

```<code>thisTuple = (1,)
i = thisTuple[0]+1
</code>```

While this:

```<code>thisArray = [1,2]

for i in thisArray:
 print i
</code>```

is iterated faster than:

```<code>thisTuple = (1,2)

for i in thisTuple:
  print i
</code>```

by which I mean lists iterate faster than tuples.

Does Javascript support numerically indexed arrays differently than associatinve arrays?  I don't believe Javascript supports more than arrays and associative arrays, but if it does how do they compare to the two speed wise?

答:
Those tests seem to support the idea that there is some optimization in favor of arrays on most browsers :

Javascript Array Performance Oddities


问:
MVC exposes database primary keys?
I'm going through a MVC tutorial, and I notice that convention seems to be to expose a tables primary key on detail pages/urls (ie. /Movies/Details/5 as an example from the tutorial).

It's obviously not a problem for things like a movie record or a SO post, but it might be a bit different for an invoice or transaction with confidential information on it if the key was sequential or guessable.

So, is it just this tutorial or do MVC apps typically expose a tables primary key?  Is there a common library or pattern for hiding the key if you don't want to expose it?

答:
And that's why I think those tutorials simplify things to a degree that confuses the newcomer.

Try to do a little "tabula rasa" and think the process from scratch, and decoupling the concepts.

First of all, MVC is a presentation layer. It does not (or should not) even know what is backing it up. It could be a database, an xml file, a text file, an in-memory collection, whatever. It should be abstracted away.

What does MVC understand? Receives an HTTP request, parses it using the route engine, resolves to the corresponding controller/action method and does whatever you write in the action method. End of the story.

Do you ask for an id which, behind the scenes, corresponds to a database surrogate key? MVC doesn't know that. For it, it's just an int parameter of a method.

It's entirely up to you how you decouple your logic and layers, how you retrieve data and check for security.

Assume you get asked for Products/Detail/1. Is your action method under authentication (using the authorize attribute)? If so, are your products only visible to certain users?

You'll pass to a business logic method the requested id along with the username, and that method will tell you "here's your product" or "sorry, you cannot view this" based on the logic inside. And heck, if you designed it well even the business logic method won't know if behind the scenes it used a database or anything else to retrieve the product.

I hope this little explanation clarifies things a little. MVC is just about handling HTTP requests and returning a view or some json data or whatever you want to return to the users. Anything that backs it should be abstracted away and implemented as you see fit.

Title: MVC exposes database primary keys?


答:
The best introduction to programming professionalism that I know of is a book called The Pragmatic Programmer. It discusses various aspects of software development, and is useful to learn what you don't know. It's also small and easy to digest compared to a more complete (hur hur hur) book like Code Complete.

For web development, your next step is to learn about the Model-View-Controller pattern. Building a small project using a framework like Yii, or CodeIgniter should allow you to get the hang of it.

The most important idea behind MVC is separation of concerns. If you separate your code well, then changes to one part of a program won't affect other parts negatively.

Your code will spend about 80% of it's lifetime in maintenance mode. That's when you have to go back to fix bugs or add new features. After a year, you won't remember what you coded any more. (I don't remember what I coded last month.) The more readable and well-structured your code is, the better for future you. Code readability is very important.

Documenting your project is important too! You don't want to try to remember what the database password used to be. Keep login and hosting details down in a secure location.

Finally, learning new languages (plural!) will help you become a better programmer. I like Python, so I suggest you start with that.

Also, if you are so inclined, the Tao of the Hacker may prove enlightening.

UPDATE 2018: Since this question was asked, several options for self-taught developers to get a more formal education have opened up. I still think the books above are a great foundation for programming craft, but the study plans mentioned below can help you get really solid technical skills.

Old Google Guide to Technical Development

OSSU Computer Science Study Plan

Title: How to fix bad fundamentals?

Body:
I am a native PHP developer, and have been for about a year or so. I love PHP and it was very easy for me to learn, but I have developed some bad habits along the way due to never having a formal education in programming. It's pretty easy to get the job done in PHP without writing the best code.

I am looking to work more with another language; not because I don't like PHP but because I want a clean slate to learn programming fundamentals. I am looking to stay with web development, so Ruby or Python seem to be where I'm leaning, though I am open to other suggestions. The more important question is, how do I approach learning this new language to really become a good programmer rather than a guy that can "get the job done"?

ContentLicense: CC BY-SA 4.0


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

答:
The Little LISPer blew my mind.

I took a High School pascal programming class. I already knew C64 basic pretty well and asked the teacher if all programming languages were like Basic and Pascal. He lent me his copy of the Little LISPer. After spending the weekend reading it, I didn't believe such an odd language could exist or would work. When I saw it running on dial up mini he has access to, I was floored. It did exist.

After my second university level programming class; Algorithms and Data Structures, I switched majors to get access to the better CS Computer lab. They had Unix machines with lisp, scheme, prolog, SmallTalk80 and a ton of other tools.

ContentLicense: CC BY-SA 2.5


问:
How can I plan optimization tasks in Scrum?
Recently my Scrum team has received several requests for optimizing certain parts of our code. Looking at the software with a profiler found some spots where the code could be improved.

We agreed to plan mostly based on user stories to ensure that everything we do has actual benefit for an end user, but how would this story be phrased? I can't come up with a good wording for them.

How would we best plan such effort? is there a good way of talking about performance optimizations in terms of user stories?

答:
Usually you would create user stories along these lines:

As a user, I want the transition from X to Y to happen in less than Z seconds.

As a user, I want X to be responsive.

As a server administrator, I want our server to be able to handle X simultaneous requests.

Note that the 'user' in user story doesn't always have to be a paying customer. As long as there's a clear benefit to the business, the 'user' can be one of your own staff/departments. If you can't define a clear benefit to the business - i.e. money brought in, or time/money saved - drop it and move on to something else.

Some notes on timings:

Where possible, it's better to be exact about timings since that removes any potential arguments over whether the changes meet the requirements.

Timings might be specified relative to previous versions. e.g. "half the time it takes in v1.0".

Bear in mind the hardware your product runs on will also make a difference! When it isn't implicit, it's worth specifying the target environment for acceptance testing.

You may want to back up a nebulous requirement with a concrete one for acceptance purposes. e.g. "as fast as possible (but not longer than a minute)".

When you really can't give an exact value I personally don't have a problem with being inexact ("about a minute", "about half the time it takes in v1.0") or really non-specific ("faster", "as fast as possible").


答:
JavaScript does not have any explicit way to specify that you want a normal array rather than an object--everything is an object by default. However, modern JavaScript engines have aggressive optimizations for objects that act like arrays. (Here is a good overview of what V8 does, for example.)

Modern JavaScript engines use "just-in-time compilation" (JIT). This means they optimize code based on how it's used at runtime. If the engine sees that an object is being accessed with consecutive numeric indices, it can optimize these into an array-like structure. However, this may take some time to take effect, which you should keep in mind if you want to write your own benchmarks.

Unfortunately, these are all optimizations that are not part of the language's semantics. This means that you cannot rely on any of them across engines. So if you're writing code that would benefit from array-like semantics, treat your objects just like normal arrays and be sure to profile on your target interpreter(s).

Additionally, for very specific use-cases, JavaScript has typed arrays. If you have raw binary data--like an array of ints or floats--you can take advantage of these typed arrays for storing, reading and writing them much more efficiently.

Unfortunately, you may have some issues with browser support of typed arrays. They have only been recently added to the proprietary browsers, so if you need to support IE <10 or Safari <5.1, you can't use them.

Title: Does javascript support numerically indexed arrays with a more optimized algorithm than an associative array?

Body:
I know that Python, Perl, Java, Lua and obviously C (as it's the only array that's in ANSI standard afaik) support faster looking of numerically indexed arrays than doing a hash lookup or anything like that.  Does Javascript also?

As an example in code of what I mean, in the case of Perl:

```<code>for(i=0;i<10;i++)
{
  # Do something
}
</code>```

is slower than:

```<code>for (1..9)
{
  # do something else
}
</code>```

or:

```<code>@var = (1,2,3)
foreach(@var)
{
  print $_; # look I'm fancy
}
</code>```

and faster than:

```<code>foreach my $key (keys %hash)
{
  print $_; # Look I'm fancy
}
</code>```

and in python, given the following:

```<code>class thisClass:
  def methodOne(i):
    return i+1

thisDict = {
  'number': 1
}
</code>```

These two operations are similar in speed because both involve a hash lookup into a same-widthed hash:

```<code>thisObject = new thisClass
i  = thisObject.methodOne(1)
</code>```

and:

```<code>i = 1+thisDict['1'];
</code>```

Both of these kinds of lookups are slower than this:

```<code>thisTuple = (1,)
i = thisTuple[0]+1
</code>```

While this:

```<code>thisArray = [1,2]

for i in thisArray:
 print i
</code>```

is iterated faster than:

```<code>thisTuple = (1,2)

for i in thisTuple:
  print i
</code>```

by which I mean lists iterate faster than tuples.

Does Javascript support numerically indexed arrays differently than associatinve arrays?  I don't believe Javascript supports more than arrays and associative arrays, but if it does how do they compare to the two speed wise?


问:
Best practices for unit testing methods that use cache heavily?
I have a number of business logic methods that store and retrieve (with filtering) objects and lists of objects from cache.

Consider

```<code>IList<TObject> AllFromCache() { ... }

TObject FetchById(guid id) { ... }

IList<TObject> FilterByPropertry(int property) { ... }
</code>```

Fetch.. and Filter.. would call AllFromCache which would populate cache and return if it isn't there and just return from it if it is.

I generally shy away from unit testing these. What are best practices for Unit Testing against this type of structure?

I considered populating cache on TestInitialize and removing on TestCleanup but that doesn't feel right to me, (tho it could well be).

答:
Your suggested approach is what I'd do. Given your description, the result of the method should be the same whether the object is present in the cache or not: you should still get the same result. That's easy to test by setting up the cache in a particular way before each test. There are probably some additional cases like if the guid is null or no object has the requested property; those can be tested too.

Additionally, you may consider it expected that the object be present in the cache after your method returns, regardless of whether it was in the cache in the first place. This is contentious, as some people (myself included) would argue that you care about what you get back from your interface, not how you get it (i.e. your testing that the interface works as expected, not that it has a specific implementation). Should you consider it important, you have the opportunity to test that.


答:
I considered populating cache on TestInitialize and removing on TestCleanup but that doesn't feel right to me

Actually, that is the only correct way to do. That is what those two functions are there for : to set the preconditions, and clean up. If the preconditions are not met, your program might not work.

Title: Best practices for unit testing methods that use cache heavily?

Body:
I have a number of business logic methods that store and retrieve (with filtering) objects and lists of objects from cache.

Consider

```<code>IList<TObject> AllFromCache() { ... }

TObject FetchById(guid id) { ... }

IList<TObject> FilterByPropertry(int property) { ... }
</code>```

Fetch.. and Filter.. would call AllFromCache which would populate cache and return if it isn't there and just return from it if it is.

I generally shy away from unit testing these. What are best practices for Unit Testing against this type of structure?

I considered populating cache on TestInitialize and removing on TestCleanup but that doesn't feel right to me, (tho it could well be).


答:
I was working on some tests that use cacheing recently. I created a wrapper around the class that works with the cache, and then had assertions that this wrapper was being called.

I did this mainly because the existing class that works with cache was static.


答:
If you want true Unit Tests, then you have to mock the cache: write a mock object that implements the same interface as the cache, but instead of being a cache, it keeps track of the calls it receives, and always returns what the real cache should be returning according to the test case.

Of course the cache itself also needs unit testing then, for which you have to mock anything it depends on, and so on.

What you describe, using the real cache object but initializing it to a known state and cleaning up after the test, is more like an integration test, because you are testing several units in concert.


答:
It looks like you want to test the caching logic, but not the populating logic. So I would suggest that you mock what you don't need to test - populating.

Your AllFromCache() method takes care of populating the cache, and that should be delegated to something else, like a supplier of values. So your code would look like

```<code>private Supplier<TObject> supplier;

IList<TObject> AllFromCache() {
    if (!cacheInitialized) {
        //whatever logic needed to fill the cache
        cache.putAll(supplier.getValues());
        cacheInitialized = true;
    }

    return  cache.getAll();
}
</code>```

Now you can mock the supplier for the test, to return some predefined values. That way, you can test your actual filtering and fetching, and not loading objects.

Body:
It looks like you want to test the caching logic, but not the populating logic. So I would suggest that you mock what you don't need to test - populating.

Your AllFromCache() method takes care of populating the cache, and that should be delegated to something else, like a supplier of values. So your code would look like

private Supplier<TObject> supplier;

IList<TObject> AllFromCache() {
    if (!cacheInitialized) {
        //whatever logic needed to fill the cache
        cache.putAll(supplier.getValues());
        cacheInitialized = true;
    }

    return  cache.getAll();
}

Now you can mock the supplier for the test, to return some predefined values. That way, you can test your actual filtering and fetching, and not loading objects.


答:
The Single Responsibility Principle is your best friend here.

First of all, move AllFromCache() into a repository class and call it GetAll(). That it retrieves from the cache is an implementation detail of the repository and shouldn't be known by the calling code.

This makes testing your filtering class nice and easy. It no longer cares where you're getting it from.

Second, wrap the class that gets the data from the database (or wherever) in a caching wrapper.

AOP is a good technique for this. It's one of the few things that it's very good at.

Using tools like PostSharp, you can set it up so that any method marked with a chosen attribute will be cached. However, if this is the only thing you're caching, you don't need to go as far as having an AOP framework. Just have a Repository and a Caching Wrapper that use the same interface and inject that into the calling class.

eg.

public class ProductManager
{
    private IProductRepository ProductRepository { get; set; }

    public ProductManager
    {
        ProductRepository = productRepository;
    }

    Product FetchById(guid id) { ... }

    IList<Product> FilterByPropertry(int property) { ... }
}

public interface IProductRepository
{
    IList<Product> GetAll();
}

public class SqlProductRepository : IProductRepository
{
    public IList<Product> GetAll()
    {
        // DB Connection, fetch
    }
}

public class CachedProductRepository : IProductRepository
{
    private IProductRepository ProductRepository { get; set; }

    public CachedProductRepository (IProductRepository productRepository)
    {
        ProductRepository = productRepository;
    }

    public IList<Product> GetAll()
    {
        // Check cache, if exists then return,
        // if not then call GetAll() on inner repository
    }
}
```
<p>See how you've removed the repository implementation knowledge from the ProductManager? See also how you've adhered to Single Responsibility Principle by having a class that handles data extraction, a class that handles data retrieval and a class that handles caching?</p>
<p>You can now instantiate the ProductManager with either of those Repositories and get caching ... or not. This is incredibly useful later when you get a confusing bug that you suspect is a result of the cache.</p>
<pre class="lang-csharp prettyprint-override"><code>productManager = new ProductManager(
    new SqlProductRepository());

productManager = new ProductManager(
    new CachedProductRepository(new SqlProductRepository()));
</code>```

(If you're using an IOC container, even better. It should be obvious how to adapt.)

And, in your ProductManager tests

IProductRepository repo = MockRepository.GenerateStrictMock<IProductRepository>();
```

No need to test the cache at all.

Now the question becomes: Should I test that CachedProductRepository? I suggest not. The cache is pretty indeterminate. The framework does things with it that are out of your control. Like, just removing stuff from it when it gets too full, for example. You're going to end up with tests that fail once in a blue moon and you'll never really understand why.

And, having made the changes I've suggested above, there's really not that much logic to test in there. The really important test, the filtering method, will be there and completely abstracted from the detail of GetAll(). GetAll() just ... gets all. From somewhere.

Body:
The Single Responsibility Principle is your best friend here.

First of all, move AllFromCache() into a repository class and call it GetAll(). That it retrieves from the cache is an implementation detail of the repository and shouldn't be known by the calling code.

This makes testing your filtering class nice and easy. It no longer cares where you're getting it from.

Second, wrap the class that gets the data from the database (or wherever) in a caching wrapper.

AOP is a good technique for this. It's one of the few things that it's very good at.

Using tools like PostSharp, you can set it up so that any method marked with a chosen attribute will be cached. However, if this is the only thing you're caching, you don't need to go as far as having an AOP framework. Just have a Repository and a Caching Wrapper that use the same interface and inject that into the calling class.

eg.

public class ProductManager
{
    private IProductRepository ProductRepository { get; set; }

    public ProductManager
    {
        ProductRepository = productRepository;
    }

    Product FetchById(guid id) { ... }

    IList<Product> FilterByPropertry(int property) { ... }
}

public interface IProductRepository
{
    IList<Product> GetAll();
}

public class SqlProductRepository : IProductRepository
{
    public IList<Product> GetAll()
    {
        // DB Connection, fetch
    }
}

public class CachedProductRepository : IProductRepository
{
    private IProductRepository ProductRepository { get; set; }

    public CachedProductRepository (IProductRepository productRepository)
    {
        ProductRepository = productRepository;
    }

    public IList<Product> GetAll()
    {
        // Check cache, if exists then return,
        // if not then call GetAll() on inner repository
    }
}

See how you've removed the repository implementation knowledge from the ProductManager? See also how you've adhered to Single Responsibility Principle by having a class that handles data extraction, a class that handles data retrieval and a class that handles caching?

You can now instantiate the ProductManager with either of those Repositories and get caching ... or not. This is incredibly useful later when you get a confusing bug that you suspect is a result of the cache.

productManager = new ProductManager(
    new SqlProductRepository());

productManager = new ProductManager(
    new CachedProductRepository(new SqlProductRepository()));

(If you're using an IOC container, even better. It should be obvious how to adapt.)

And, in your ProductManager tests

IProductRepository repo = MockRepository.GenerateStrictMock<IProductRepository>();

No need to test the cache at all.

Now the question becomes: Should I test that CachedProductRepository? I suggest not. The cache is pretty indeterminate. The framework does things with it that are out of your control. Like, just removing stuff from it when it gets too full, for example. You're going to end up with tests that fail once in a blue moon and you'll never really understand why.

And, having made the changes I've suggested above, there's really not that much logic to test in there. The really important test, the filtering method, will be there and completely abstracted from the detail of GetAll(). GetAll() just ... gets all. From somewhere.


问:
Cleaning Up Generated Code: Refactor or Map?
Context:

I recently had to deal with a class file generated by XSD.exe. It was 3500 lines long with ridiculously-verbose class / variable names (think someRidiculouslyLongPrefixThenMaybeOneThingUniqueAtTheEnd - difficult to compare at a glance with someRidiculouslyLongPrefixThenMaybeOneOtherThingChanged) and annotations all over the place. Bottom line is it took me ages to work out what the heck was going on. I read it and thought I would never put my name next to something so... Un-clean.

Question:

1) Is it bad practice to mess with generated code (i.e. clean it).

2) Would it be better practice to write a mapper to map the generated classes to my own nice, clean classes (which I could then get to work with, quite happily)?

EDIT:

Thanks for all the comments.

If I was actually going to do anything interesting with it (i.e. if there were domain objects which were anything but transport objects) then I think I'd map them to 'cleaner' classes, which I'd have to do anyway to get any kind of functionality out of them. In this case the classes are effectively DTOs so perhaps it makes sense that the naming matches the corresponding elements. As stated, I don't need to touch it - just to call accessors / mutators before passing the data down to another layer for processing.

For now, I think I'll leave them well alone.

答:
The danger with refactoring generated code to clearn and tidy it is that if it is regenerated again by the tool by yourself or another developer then the changes would be lost.

Your team could get yourselves in a position where you would be generating the code in another file and copying it into the cleaned version and refactoring to apply changes which just takes time and resource. (I've been there with the original version of Entity Framework.)

If you cannot live with the names generated, either change the source it generates from or do as you suggest in #2.


问:
Clustering algorithm that doesn't require predefining the number of clusters required
I have a set of user queries from a search engine that I want to cluster. The only clustering algorithm I have come across so far is the K-means clustering algorithm, which requires defining the number of clusters up front. But in this case, I do not know how many clusters exist in the data. Is there any clustering algorithm that performs clustering without predefining the number of clusters?</p>

答:
DBSCAN?

http://en.wikipedia.org/wiki/DBSCAN

DBSCAN requires two parameters: distance (eps) and the minimum number
  of points required to form a cluster (minPts).


答:
As a general rule, never touch generated code, because doing so means promising that you will never generate it again, or you'll have to redo all the changes you've made. If you want the generated code to look nicer, you have to automate both the code generation and the cleanup; for example, if you generate an XML file somewhere, you might want to run it through xmlindent as part of your build process.

For similar reasons, generated code does not belong in source control. You put the data and rules under source control, and let your build script handle the code generation.

Any changes to the generated code have to go through the code generator, that is, if you want the generated code to look different, change the code generator's inputs, the code generator itself, or apply scriptable post-processing. But don't hand-edit.

An exception are 'scaffolding'-style code generators, where you run the generator once to give you a skeleton from which you build further. With those, you'll never run the generator again for the same file, so you just treat the generated file as a regular source file and forget that it comes from a generator.

Title: Cleaning Up Generated Code: Refactor or Map?

Body:
Context:

I recently had to deal with a class file generated by XSD.exe. It was 3500 lines long with ridiculously-verbose class / variable names (think someRidiculouslyLongPrefixThenMaybeOneThingUniqueAtTheEnd - difficult to compare at a glance with someRidiculouslyLongPrefixThenMaybeOneOtherThingChanged) and annotations all over the place. Bottom line is it took me ages to work out what the heck was going on. I read it and thought I would never put my name next to something so... Un-clean.

Question:

1) Is it bad practice to mess with generated code (i.e. clean it).

2) Would it be better practice to write a mapper to map the generated classes to my own nice, clean classes (which I could then get to work with, quite happily)?

EDIT:

Thanks for all the comments.

If I was actually going to do anything interesting with it (i.e. if there were domain objects which were anything but transport objects) then I think I'd map them to 'cleaner' classes, which I'd have to do anyway to get any kind of functionality out of them. In this case the classes are effectively DTOs so perhaps it makes sense that the naming matches the corresponding elements. As stated, I don't need to touch it - just to call accessors / mutators before passing the data down to another layer for processing.

For now, I think I'll leave them well alone.


答:
I totally agree to @NikolaiDante (+1) answer.

In dotnet/c# you have the concept of "partial classes": The sourcecode for the "class to use" consist of 2 seperate files a generated part and of a manually added/edited part. Your api-beautifications go inte the "manual" file that won-t be overwritten by the codegenerator.

In the java/hybris world they use inheritance for the none-generated-code:

You have for example a class Customer with your "api-beautifications" that inherits from "GeneratedCustomer".


答:
The difference between C++ and Java is in what the languages consider their smallest unit of linkage.

Because C was designed to coexist with assembly, that unit is the subroutine called by an address.  (This is true of other languages that compile to native object files, such as FORTRAN.)  In other words, an object file containing a function foo() will have a symbol called _foo that will be resolved to nothing but an address such as 0xdeadbeef during linking.  That's all there is.  If the function is to take arguments, it's up to the caller to make sure everything the function expects is in order before calling its address.  Normally, this is done by piling things onto the stack, and the compiler takes care of the grunt work and making sure the prototypes match up.  There is no checking of this between object files; if you goof up the call linkage, the call isn't going to go off as planned and you're not going to get a warning about it.  Despite the danger, this makes it possible for object files compiled from multiple languages (including assembly) to be linked together into a functioning program without a lot of fuss.

C++, despite all of its additional fanciness, works the same way.  The compiler shoehorns namespaces, classs and methods/members/etc. into this convention by flattening the contents of classes into single names that are mangled in a way that makes them unique.  For example, a method like Foo::bar(int baz) might get mangled into _ZN4Foo4barEi when put into an object file and an address like 0xBADCAFE at runtime.  This is entirely dependent on the compiler, so if you try to link two objects that have different mangling schemes, you're going to be out of luck.  Ugly as this is, it means you can use an extern "C" block to disable mangling, making it possible to make C++ code easily accessible to other languages.  C++ inherited the notion of free-floating functions from C, largely because the native object format allows it.

Java is a different beast that lives in an insulated world with its own object file format, the .class file.  Class files contain a wealth of information about their contents that allows the environment to do things with classes at runtime that the native linkage mechanism couldn't even dream about.  That information has to start somewhere, and that starting point is the class.  The available information allows the compiled code to describe itself without the need for separate files containing a description in source code as you'd have in C, C++ or other languages.  That gives you all of the type safety benefits languages using the native linkage lack, even at runtime, and is what enables you to fish an arbitrary class out of a file using reflection and use it with a guaranteed failure if something doesn't match up.

If you haven't figured it out already, all of this safety comes with a tradeoff:  anything you link to a Java program has to be Java.  (By "link," I mean anytime something in one class file refers to something in another.)  You can link (in the native sense) to native code using JNI, but there's an implicit contract that says that if you break the native side, you own both pieces.

Java was big and not particularly fast on the available hardware when it was first introduced, much like Ada had been in the prior decade.  Only Jim Gosling can say for sure what his motivations were in making the class Java's smallest unit of linkage, but I'd have to guess that the extra complexity that adding free floaters would have added to the runtime might have been a deal-killer.


问:
Egoless programming, is it possible or even desirable
Is it possible to have egoless programming or is it even desirable?

As a profession we do seem to want to show off the latest gizmo, technique or say “look at this awesome piece of code I’ve written”. Yet we can get very defensive when asked to submit items of work for code reviews or get negative comments from other programmers (hearing the term WTF, has never been a good sign).

Can we as a profession be able to sit down and analyse a piece of code, data or architecture for its merits or constraints and a calm and respectful manner, without causing offense or antagonising our colleagues, or are we just the archetypal Dilbert character, raging against the system?

答:
[imo]

I think this is a fundamentally difficult question, and perhaps sociology, or just using a computer as your main form of communication, has a lot to do with it.

The computer is a tool, and the consciousness that it bears is incredibly beautiful, but you should never try to say that a piece of code is an object of your own creation... every piece of code is a discovery, a pattern that the computer bonds with and can produce a result that you enjoy.  The fact that we can discover these patterns iteratively and feel like we are creating is sheer blissful coincidence -- we're good at finding patterns.

We really need to look at computers as windows to discovery, and respect the autonomy of these little brains, even if they are just incredibly small cut-outs from a biological brain.  All of math and all of algorithms correspond to an inviolable truth upon which our very cosmos resonates. To declare that part of this is "mine!" (think sea gulls in Finding Nemo) misses so many points on so many levels.

It is hard to step away from this at times, when "laboring" to "devise" a new algorithm, but if you were to think that we are just humble maintainers of these elegantly precise windows into mathematical truth, the definition of the task changes no matter what your goals for your software.

Treat your computer as an equal first, the rest (egoless-ism, peer coordination, goal-oriented behavior) will necessarily follow.

Much of my motivation for studying computer science and becoming a programmer was because of this affinity I felt, not toward physical hardware or the thrill of solving problems, but the very fact that I am peering into a brain, a brain I can communicate with directly in my favorite fashion: patterns!

Take a step back and recall why you like the things you like to begin with.  Programming can be competitive and I think that fuels a lot of the ego in defending code and design choices, but

a) if someone is a better programmer,

I want to talk to them and learn from them more about this complex relationship to the electric consciousness.  We can share discovery, and that is amazing!

b) if my design choices or code could be changed to better suit the organization that I work for

then by all means change it!  I am an employee because I need money and I get money when the company gets money, and they get money when the code we use is awesome, not when I want to argue about whitespace or whatever. And as ChrisF points out, you essentially become just a really slow typist in a company setting as far as other laborers can tell.  You don't own the code, you don't own the implementation, you are just a medium of translation from idea/paper/specs to electric consciousness brainfood.  Translation to and from any pair of languages is an iterative process and you get better with it over time.  Criticism accelerates this process and should never be taken personally.  But anyway it seems like most agree that you should be egoless, just remember that coding for a company is a job, and you can learn and tinker and play with your code all you want, as long as you keep your bosses and your clients happy.

[/imo]


答:
There are several techniques that allow you to cluster unsupervised data. K-means is probably the most famous one. But as you have already seen, most k-means algorithms require the number of clusters to be specified in advance.

Nevertheless, at least two kinds of algorithms might suit your needs:

Connectivity based clustering (hierarchical clustering);

Density-based clustering (such as DBSCAN or OPTICS).

By the way, there is a similar question in StackOverflow.

Have fun!

Title: Clustering algorithm that doesn't require predefining the number of clusters required

Body: I have a set of user queries from a search engine that I want to cluster. The only clustering algorithm I have come across so far is the K-means clustering algorithm, which requires defining the number of clusters up front. But in this case, I do not know how many clusters exist in the data. Is there any clustering algorithm that performs clustering without predefining the number of clusters?</p>


问:
Velocity does not plateau over time, why?
I have plotted my team burn up chart and its velocity per iteration. To me it looks really bad (velocity fluctuates a lot). What should I be looking for to diagnose the root cause of this behaviour?

答:
It's perfectly normal to have a fluctuation at the first ten or so sprints, while the team is finding its rhythm.  After that, it's perfectly normal for velocity to fluctuate around an average.  Try plotting a running average of the last five sprints or so and you should see it level out.  If not, some of the following may be the culprits:

The team is trying to adjust their story point estimates based on their recent velocity, instead of keeping the story points constant and adjusting how many stories they take on.

You are not breaking the stories down small enough.

Too many of your stories are up in the higher granularities.  For example, you have a lot of 20 pointers that you are reluctant to either change to a 13 or 40.

You have a lot of "hangover" stories that don't quite get finished in one sprint.


答:
A Java file represents a class.  If you had a procedure outside the class, what would the scope be?  Would it be global?  Or would it belong to the class that Java file represents?

Presumably, you put it in that Java file instead of another file for a reason - because it goes with that class more than any other class.  If a procedure outside a class was actually associated with that class, then why not force it to go inside that class where it belongs?  Java handles this as a static method inside the class.

If an outside-class procedure were allowed, it would presumably have no special access to the class whose file it was declared in, thus limiting it to a utility function that doesn't change any data.

The only possible down-side to this Java limitation is that if you truly have global procedures that are not associated with any class, you end up making a MyGlobals class to hold them, and import that class in all your other files that use those procedures.

In fact, the Java importing mechanism needs this restriction in order to function.  With all the API's available, the java compiler needs to know exactly what to compile and what to compile against, thus the explicit import statements at the top of the file.  Without having to group your globals into an artificial class, how would you tell the Java compiler to compile your globals and not any and all globals on your classpath?  What about namespace collision where you have a doStuff() and someone else has a doStuff()?  It would not work.  Forcing you to specifiy MyClass.doStuff() and YourClass.doStuff() fixes these issues.  Forcing your procedures to go inside MyClass instead of outside it only clarifies this restriction and does not impose additional restrictions on your code.

Java got a number of things wrong - serialization has so many little warts that it is almost too difficult to be useful (think SerialVersionUID).  It can also be used to break singletons and other common design patterns.  The clone() method on Object should be split into deepClone() and shallowClone() and be type-safe.  All the API classes could have been made immutable by default (the way they are in Scala).  But the restriction that all procedures must belong to a class is a good one.  It serves primarily to simplify and clarify the language and your code without imposing any onerous restrictions.


问:
What is the right way to version control a password protected archive?
I'm working with a local file which I always extract from a password protected archive (RAR), do some modifications/additions to it and overwrite the old file inside the archive with the new file (I don't overwrite the archive. I overwrite the file inside it). The RAR is password protected for certain confidentiality reasons, and I need it to be that way.

I considered using Git for version control, but from my first use of Git, it appears to be meant for files that are not archived. I wanted to version control my RAR file. But what if I decide to add more files to the RAR and then commit the RAR? Doesn't that ruin the point of having version control where you can also do a diff to compare versions etc.? Does it make sense to use a version control when I'm extracting files from the RAR to do modifications and then putting them back in the RAR and committing the RAR?

Is version control the right way to do this or is there something better?

答: btw... RAR can handle version control natively (not in cahoots with the RAR folks just a satisfied user). See this link for details.</p>


问:
How and why to decide between naming methods with "get" and "find" prefixes
I always have trouble figuring out if I should name a certain method starting with getSomething versus findSomething.

The problem resides in creating helpers for poorly designed APIs. This usually occurs when getting data from an object, which requires the object as a parameter. Here is a simple example:

public String getRevision(Item item) {
    service.load(item, "revision");
    // there is usually more work to do before getting the data..
    try {
        return item.get_revision();
    }
    catch(NotLoadedException exception) {
        log.error("Property named 'property_name' was not loaded", exception);
    }
    return null;
}
```

How and why to decide between naming this method as getRevision() or findRevision()?

答:
I apply the following pattern:

Foo GetFoo() cannot return null and its complexity is at most O(log(n))

bool TryGetFoo(out Foo) can return null and its complexity is at most O(log(n))

Foo FindFoo() cannot return null and its complexity is at least O(log(n))

bool TryFindFoo(out Foo) can return null and its complexity is at least O(log(n))

That way the code is pretty clear on the intent and on the complexity you can expect.

Typically, the Getters are for direct list or dictionary/set access.

The Finders are deep search, full scan of list, etc...

In your case:

public bool TryGetRevision( Item item, out String revision )
{
    service.load( item, "revision" );
    // there is usually more work to do before getting the data..
    try
    {
        revision = item.get_revision();
        return true;
    }
    catch( NotLoadedException exception )
    {
        log.error( "Property named 'property_name' was not loaded", exception );
        revision = "";
        return false;
    }
}
```


问:
Best Practices Returning Read-Only Object
I have "best practices" question about OOP in C# (but it sort-of applies to all languages).

Consider having library class with object that is to be exposed to public, say via property accessor,
but we do not want the public (people using this library class) to change it.

```<code>class A
{
    // Note: List is just example, I am interested in objects in general.
    private List<string> _items = new List<string>() { "hello" }

    public List<string> Items
    {
        get
        {
            // Option A (not read-only), can be modified from outside:
            return _items;

            // Option B (sort-of read-only):
            return new List<string>( _items );

            // Option C, must change return type to ReadOnlyCollection<string>
            return new ReadOnlyCollection<string>( _items );
        }
    }
}
</code>```

Obviously the best approach is "option C", but very few objects have ReadOnly variant (and certainly no user-defined classes have it).

If you were the user of class A, would you expect changes to

```<code>List<string> someList = ( new A() ).Items;
</code>```

propagate to original (A) object?
Or is it okay to return a clone provided that it was written so in comments/documentation?
I think that this clone approach might lead to quite difficult-to-track bugs.

I remember that in C++ we could return const objects and you could only call
methods marked as const on it. I guess there is no such feature/pattern in the C#? If not why would they not include it? I believe it is called const correctness.

But then again my main question is about "what would you expect" or
how to handle Option A vs B.

答: Also note you should be programming to interfaces, and by and large, what you want to return from that property is an IEnumerable<string>. A List<string> is a bit of a no-no because it is generally mutable and I'll go so far to say that ReadOnlyCollection<string> as a implementor of ICollection<string> feels like it violates the Liskov Substitution Principle.


答:
I encountered a similar problem a while back and below was my solution, but it really only works if you control the library too:

Start with your class A

```<code>public class A
{
    private int _n;
    public int N
    {
        get { return _n; }
        set { _n = value; }
    }
}
</code>```

Then derive an interface from this with only the get portion of the properties (and any reading methods) and have A implement that

```<code>public interface IReadOnlyA
{
    public int N { get; }
}
public class A : IReadOnlyA
{
    private int _n;
    public int N
    {
        get { return _n; }
        set { _n = value; }
    }
}
</code>```

Then of course when you want to use the read only version, a simple cast is sufficient. This is just what you solution C is, really, but I thought I'd offer the method with which I achieved it

Title: Best Practices Returning Read-Only Object

Body:
I have "best practices" question about OOP in C# (but it sort-of applies to all languages).

Consider having library class with object that is to be exposed to public, say via property accessor,
but we do not want the public (people using this library class) to change it.

```<code>class A
{
    // Note: List is just example, I am interested in objects in general.
    private List<string> _items = new List<string>() { "hello" }

    public List<string> Items
    {
        get
        {
            // Option A (not read-only), can be modified from outside:
            return _items;

            // Option B (sort-of read-only):
            return new List<string>( _items );

            // Option C, must change return type to ReadOnlyCollection<string>
            return new ReadOnlyCollection<string>( _items );
        }
    }
}
</code>```

Obviously the best approach is "option C", but very few objects have ReadOnly variant (and certainly no user-defined classes have it).

If you were the user of class A, would you expect changes to

```<code>List<string> someList = ( new A() ).Items;
</code>```

propagate to original (A) object?
Or is it okay to return a clone provided that it was written so in comments/documentation?
I think that this clone approach might lead to quite difficult-to-track bugs.

I remember that in C++ we could return const objects and you could only call
methods marked as const on it. I guess there is no such feature/pattern in the C#? If not why would they not include it? I believe it is called const correctness.

But then again my main question is about "what would you expect" or
how to handle Option A vs B.

Body:
I encountered a similar problem a while back and below was my solution, but it really only works if you control the library too:

Start with your class A

public class A
{
    private int _n;
    public int N
    {
        get { return _n; }
        set { _n = value; }
    }
}

Then derive an interface from this with only the get portion of the properties (and any reading methods) and have A implement that

public interface IReadOnlyA
{
    public int N { get; }
}
public class A : IReadOnlyA
{
    private int _n;
    public int N
    {
        get { return _n; }
        set { _n = value; }
    }
}

Then of course when you want to use the read only version, a simple cast is sufficient. This is just what you solution C is, really, but I thought I'd offer the method with which I achieved it


答:
Besides @Jesse's answer, which is the best solution for collections: the general idea is to design your public interface of A in a way so that it does return only

value types (like int, double, struct)

immutable objects (like "string", or user defined classes which offer only read-only methods, just like your class A)

(only if you must): object copies, like your "Option B" demonstrates

IEnumerable<immutable_type_here> is immutable by itself, so this is just a special case of the above.


问:
MySQL setup for remote site failover
I'm designing a custom web-based inventory management and workflow system for a client of mine. One of their remote sites has fairly sketchy internet access, and needs to have no (or very little) interruption of business when their connection to the universe goes down.

I'm picturing some sort of local installation of the web app with MySQL replication keeping their local DB up to date. When their connection out of the building fails, they can manually kick over to the local URL, which is configured to hit their local MySQL, and they stay in business.

Question is, replicating those changes back up to the real master. I know of such a thing as master-master replication, but will that really hot-sync divergent tables back together when the connection comes back up?

It's some help that the remote site's use case is fairly unique, and it's likely (though not guaranteed) to write only to tables related to their wing of the business. I could perhaps limit the "failover mode" local application to only those pieces of the app that are unique to their location, which would be enough to keep them in business.

How would you approach this?

答:
I would probably set up multi-master replication and always have the remote site run locally.  That way there's no failover to deal with, the users will never get confused, and you've improved their latency.  Be sure your system retains the replication logs for much longer than the system may ever get disconnected. Also have systems and processes in place to monitor the status.

Another option is to have the remote site run locally on an independent database and have your own process to sync data.  Since your system is custom it's more work but also more streamlined and able to dynamically handle any situation (e.g. logical conflicts in the data).  This solution would actually be necessary if the data being synced needs logical checks.  You'd probably be best using GUIDs for IDs or your own system to avoid primary key conflicts.

Title: MySQL setup for remote site failover


问:
Specifying Query in Unit Test
When writing unit tests, should I specify the query that will be performed for interacting with the database?

I can see both sides of this.  On one hand, I want to make sure that the query that I specify is being performed.  But I can be making the test more brittle due to the formatting of the query.

EDIT:
Basically the code would be a mapper object that interacts with the db layer.  But I am wondering how specific do I need to be in my interactions with a Db object.

Edit:

As I want to mock any external dependencies to my objects when I am mocking my Database connection. Should I specify the query that is going to be performed.

For example:

```<code>testGettingUsers() {
    $mockDb = $this->getMockBuilder('My_DB_Connection')
         ->setMethods(array('query'))
         ->getMock();

    $mockDb->expects($this->once())
         ->method('query')
         ->with('SELECT id, name FROM USERS WHERE name = foo')
         ->will($this->returnValue($dbReturn));
</code>```

Here I have specified my query as the parameter that I am going to call my DB connection with.  Is that necessary?

答:
Each of your classes should have a specific responsibility (what it does) and it should only do that thing (a single responsibility) and it should do it well. Your testing should ensure that the object in question does what it is supposed to.

With regards to a mapper object we are concerned that it can properly map from a data source (we don't care what the underlying source is) to a given object (say a domain object). With this in mind it should be safe to assume the that actual query for your mapping is contained within methods on your mapper object.

public class CustomerMapper {
  private DatabaseObject _myDb;
  public Customer FindCustomerWithId(int id){
    return _myDb.Query("SELECT * FROM CUSTOMERS WHERE ID = " + id).FirstOrDefault();
  }
}
```

<p>In this case your unit test would simply test the <strong>FindCustomerWithId</strong> method and not concern itself with the given query. </p>

<pre class="lang-cs prettyprint-override"><code>public class CustomerMapperTests {
  [Test]
  public void test_finding_a_customer_by_id() {
     var systemUnderTest = new CustomerMapper();
     var customer = CustomerMapper.FindCustomerWithId(5);
     customer.ShoudlNotBeNull(); // Assertion
  }
}
</code>```

Now if we were to test the DatabaseObject class then it would make sense to test actual queries to ensure that said class handles them as expected.

public class DatabaseObjectTests {
  [Test]
  public void test_that_it_can_handle_a_count_query(){
    var systemUnderTest = new DatabaseObject();
    var numberOfCustomers = systemUnderTest.Query<int>("SELECT COUNT(*) FROM CUSTOMERS);
    numberOfCustomers.Should().Be(10);
  }
}
```

<p><strong>Edit</strong> To address the idea of mocking the database object during the CustomerMapper test I would not concern myself with queries or SQL there as well. Something along the lines of:</p>

<pre class="lang-cs prettyprint-override"><code>public void test_finding_a_customer(){
  var mock = new Mock<DatabaseObject>();
  mock.Setup(m => m.Query(It.IsAny<string>()).Returns(new Customer { Id = 5 });
  var systemUnderTest = new CustomerMapper(mock.Object);
  var customer = CustomerMapper.FindCustomerWithId(5);
  customer.ShoudlNotBeNull(); // Assertion
}
</code>```

Title: Specifying Query in Unit Test

Body:
Each of your classes should have a specific responsibility (what it does) and it should only do that thing (a single responsibility) and it should do it well. Your testing should ensure that the object in question does what it is supposed to.

With regards to a mapper object we are concerned that it can properly map from a data source (we don't care what the underlying source is) to a given object (say a domain object). With this in mind it should be safe to assume the that actual query for your mapping is contained within methods on your mapper object.

public class CustomerMapper {
  private DatabaseObject _myDb;
  public Customer FindCustomerWithId(int id){
    return _myDb.Query("SELECT * FROM CUSTOMERS WHERE ID = " + id).FirstOrDefault();
  }
}

In this case your unit test would simply test the FindCustomerWithId method and not concern itself with the given query.

public class CustomerMapperTests {
  [Test]
  public void test_finding_a_customer_by_id() {
     var systemUnderTest = new CustomerMapper();
     var customer = CustomerMapper.FindCustomerWithId(5);
     customer.ShoudlNotBeNull(); // Assertion
  }
}

Now if we were to test the DatabaseObject class then it would make sense to test actual queries to ensure that said class handles them as expected.

public class DatabaseObjectTests {
  [Test]
  public void test_that_it_can_handle_a_count_query(){
    var systemUnderTest = new DatabaseObject();
    var numberOfCustomers = systemUnderTest.Query<int>("SELECT COUNT(*) FROM CUSTOMERS);
    numberOfCustomers.Should().Be(10);
  }
}

Edit To address the idea of mocking the database object during the CustomerMapper test I would not concern myself with queries or SQL there as well. Something along the lines of:

public void test_finding_a_customer(){
  var mock = new Mock<DatabaseObject>();
  mock.Setup(m => m.Query(It.IsAny<string>()).Returns(new Customer { Id = 5 });
  var systemUnderTest = new CustomerMapper(mock.Object);
  var customer = CustomerMapper.FindCustomerWithId(5);
  customer.ShoudlNotBeNull(); // Assertion
}


答:
I will generally use Get to retrieve an object/value, and Find to retrieve its location (in an array, for example).

for ex:

```<code>object o = obj.GetItem( 'name');

integer i = somearray.Find( 'name');
</code>```

Title: How and why to decide between naming methods with "get" and "find" prefixes

Body:
I always have trouble figuring out if I should name a certain method starting with getSomething versus findSomething.

The problem resides in creating helpers for poorly designed APIs. This usually occurs when getting data from an object, which requires the object as a parameter. Here is a simple example:

public String getRevision(Item item) {
    service.load(item, "revision");
    // there is usually more work to do before getting the data..
    try {
        return item.get_revision();
    }
    catch(NotLoadedException exception) {
        log.error("Property named 'property_name' was not loaded", exception);
    }
    return null;
}
```

How and why to decide between naming this method as getRevision() or findRevision()?

Body:
I will generally use Get to retrieve an object/value, and Find to retrieve its location (in an array, for example).

for ex:

object o = obj.GetItem( 'name');

integer i = somearray.Find( 'name');


问:
What .NET objects should I use to create a cookie based session in MVC?
I'm writing a custom password reset application that uses a validation technique that doesn't fit cleanly with ASP.NET Membership Provider's challenge questions.

Namely I need to invoke a workflow and collect information from the end user (backup phone number, email address) after the user logs in using a custom form.

The only way I know to create a cookie-based session (without too much "innovation" on my part) is to use WIF.

What other standard objects can I use with ASP.NET MVC to create an authenticated session that works with non-windows user stores?

Ideally I can store "role" or claim information in the session object such as "admin", "departmentXadmin", "normalUser", or "restrictedUser"

The workflow would look like this:

User logs in with username and password

If the username and pw are correct a (stateless) cookie based session is created

The user gets redirected to a HTML form that allows them to enter their backup phone number (for SMS dual factor), or validate it if already set.

The user can then change their password using the form provided

The "forgot password" would look like this

User requests OTP code to be sent to the phone

User logs in using username and OTP

If the OTP is valid and not expired then create a cookie based session and redirect to a form that allows password reset

Show password reset form, and process results.

答:
First, check out the improved membership bits that were released with 4.5 -- it should be easier to implement custom options on top of than the old creaky structure.

In either case, you can do some very effective "let the membership providers handle the major authentication and use your own database to handle extensions." Even the old ones have a concept of account approval you can manage programmatically.

Another option would be to use ASP.NET's authentication bits but roll your own backing. You can start with this article to get some of the basics and add on parts from there.


问:
COM INTEROP Support - which is better? C# or VB
I keep hearing that c# is "better" than vb... but as far as I can see, aside from syntactical differences, both compile down to the same IL. I've found some good articles by googling that explain what the differences are between the two and so I feel comfortable in "diffusing" conversations between developers arguing over vb / c#.
=)

But I did read an article that said vb.net 2005 had better support for com interop stuff. But i'm wondering if this is still the case?  This is of interest to me because we are in the middle of redesigning an old vb6 app that communicates with some older COM components.

Does anyone have recent experience with .NET and COM interop?
Thanks.

答:
The only experience I have with COM interop is with C# and interacting with Excel in .NET 3.5. When calling interop functions from C#, you had to supply every single argument in the function call with a reference or value type. If you were not using the function argument, you had to use System.Type.Missing.Value in its place. However, if you were using VB, the unused arguments did not need to be supplied. This link contains both C# and VB code examples that show you how much of a difference it makes.

Microsoft has changed their COM api for Excel since .NET 3.5 to make calling these functions less verbose so it is not as bad as it used to be. A reason that one may have chosen VB over C# in the past is probably due to VB support for supplying all the missing arguments for you.


答:
I've worked on writing my own simple authentication system named FSCAuth. It's been out for a while, but it's by no means "guaranteed" secure. Anyway, if you're considering rolling your own authentication anyway(which is always risky), you might considering using my authentication framework as a jumping off point. It's designed to be trivial to extend, but only implement what people actually care about.

One of it's goals is to handle the cookie stuff for you and never leave you to mess with hash values(where you can make something insecure very subtely). However, your use case makes it sound like some modification would be in order.

I have a few "branches" of FSCAuth:

BSD licensed with paid support option at Binpress

Same copy as what's at Binpress but easily browsable at bitbucket

Integrated into my custom MVC framework(BSD). Not quite compatible with old version, but this version is in active development(and currently experimental), where the others are probably dead(due to lack of interest) at bitbucket

Title: What .NET objects should I use to create a cookie based session in MVC?

Body:
I'm writing a custom password reset application that uses a validation technique that doesn't fit cleanly with ASP.NET Membership Provider's challenge questions.

Namely I need to invoke a workflow and collect information from the end user (backup phone number, email address) after the user logs in using a custom form.

The only way I know to create a cookie-based session (without too much "innovation" on my part) is to use WIF.

What other standard objects can I use with ASP.NET MVC to create an authenticated session that works with non-windows user stores?

Ideally I can store "role" or claim information in the session object such as "admin", "departmentXadmin", "normalUser", or "restrictedUser"

The workflow would look like this:

User logs in with username and password

If the username and pw are correct a (stateless) cookie based session is created

The user gets redirected to a HTML form that allows them to enter their backup phone number (for SMS dual factor), or validate it if already set.

The user can then change their password using the form provided

The "forgot password" would look like this

User requests OTP code to be sent to the phone

User logs in using username and OTP

If the OTP is valid and not expired then create a cookie based session and redirect to a form that allows password reset

Show password reset form, and process results.


问:
Refactoring code smells in ASP.NET Web Forms Code Behind Files: Is it risky? Is it wise? Is it a waste of time?
Given a long method with Pac-Man ifs, would it be wise to:

Break down code blocks into regions.

Then refactor code blocks into methods.

Or would it be best to leave it alone?

Would it be risky?

Would it be a waste of time?

In the absence of automated unit-tests, I'm trying to understand the risk-reward relationship associated with this manuever.

答:
It's risky in the absence of automated tests. Of course you'll test as you go along, but that's tedious, and error-prone. Ideally you'd write unit tests capturing the current behaviour, but that's not always feasible.

Having been in this position before (with PHP), if the code really stinks, refactor it very slowly and very carefully.

I think your plan's sound - take the long method, break it up with whitespace into paragraphs, and try Extract Method. Quite how you do that depends on how many variables are in play, and how they interact. Try trace the dependencies of a paragraph before you start.

Is it worth it? If the code's really awful, and its awfulness is starting to spread, then it's time to get stuck in there. If it's been running fine for ages, then you risk introducing bugs into something that for now Just Works.


问:
Simulating a projector in openGL
I wish to simulate a projector screen in OpenGL, where an image is truly being projected onto the wall in real time. By this I mean if an object were to get in between the projector and the wall, that portion of the image will appear on the obstructing object. </p>

<p>Is this functionality possible in OpenGL? And what technolgies/libraries can I implement to facilitate this development? </p>

答:
What you're looking for is projective texture mapping, for which there are many tutorials and which has been possible in all versions of OpenGL: SGI's sample code was written for OpenGL 1.0.

What you do is essentially apply a perspective transformation on the texture when you apply it to an arbitrary surface.

Title: Simulating a projector in openGL


答:
Before .NET 4.0, VB.NET had a slight advantage over C# when calling COM interop code. This was especially true for code, such as the Office programmability model which, as mortalapeman notes, makes use of COM's support for optional parameters. This API often exposed methods with 10, 15 or more parameters (ugh!), all of which were ref params, which in C# had to be specified as references to Missing.Value. This was a pain.

It's important to note, though, that while this was common when calling Office code, I never really encountered it anywhere else. Most COM APIs I worked with were a lot more straightforward, and could be called from C# with little hassle.

However, in .NET 4.0, even this small problem has been corrected. For general purpose COM APIs, you can use the dynamic keyword to call late-bound code without the strong type checking C# usually demands, and thus call COM APIs without a hitch. Check out this article on general usages of dynamic, and this one specifically for working with COM objects.

If you're planning on using this to interop with Office components, C# 4.0 offers an even simpler solution. Check out this MSDN article on Office's optional parameters:

Visual C# enables you to omit optional ref parameters only for methods of interfaces, not classes.

[...]

If you want to write code that omits optional ref parameters of a method in the ThisDocument class, you can alternatively call the same method on the Microsoft.Office.Interop.Word.Document object returned by the InnerObject property, and omit the parameters from that method. You can do this because Microsoft.Office.Interop.Word.Document is an interface, rather than a class.

So instead of calling myWordDoc.CheckSpelling() with 12 (count'em, 12!) pointless optional parameters, you can call myWordDoc.InnerObject.CheckSpelling(IgnoreUpperCase: true), clean and neat.

Title: COM INTEROP Support - which is better? C# or VB

Body:
I keep hearing that c# is "better" than vb... but as far as I can see, aside from syntactical differences, both compile down to the same IL. I've found some good articles by googling that explain what the differences are between the two and so I feel comfortable in "diffusing" conversations between developers arguing over vb / c#.
=)

But I did read an article that said vb.net 2005 had better support for com interop stuff. But i'm wondering if this is still the case?  This is of interest to me because we are in the middle of redesigning an old vb6 app that communicates with some older COM components.

Does anyone have recent experience with .NET and COM interop?
Thanks.


问:
One Login amongst multiple ASP.NET MVC applications Questions
I have been working on an ASP.NET MVC 4 application that uses forms based authentication.  Users are validated against a membership provider based on a provider selection on the login form.  For example, at this moment there are two providers:  active directory and a custom provider that calls an external web service.  If the user is valid then we update information in our local users table, like last logon date, etc. If the user is valid and doesn’t exist in the local users table, we add them.  Once all of this is done we set a cookie and move on to the main content.  All of the controllers check User.Identity.IsAuthenticated and return to the login page if the test fails.

Now I am told that this application is going from a single web application to a multiple application solution.  Each application should exist independently from the others, share the same look and feel, and use the same login functionality.  Each application can require a new membership provider to be added.  We will also add data for limiting access to the applications.  One user may only have access to one of the applications while another user has access to all of them.

Setting up the data storage is not a problem.  Sharing the layout from one application to be used in the others is not a problem either.  What I am having trouble with is deciding how to “refactor” the login functionality such that adding a new membership provider and application “down the road” will not require us to republish existing applications.  Right now they would also not want a single sign-on launch page for the applications.  They would like for the users to go straight to an application, login, and do what they came there to do.  Thus, I may go to http://site1.mysites.com, login and do my work while the guy next to me goes to http://site2.mysites.com, logs in and does his work.

I am having trouble wrapping my head around the best way to do this.  My first thought was a WCF web service for the login.  I would have a controller action call the webservice passing the login credentials.  If the login was successful, then I could get a list of applications for the user from the local storage.  Adding more applications would not require us to republish existing apps due to no changes in the web service for those apps.

I also keep hearing about this ASP.NET Web API stuff.  Should I go that route instead?  I may want to build a site around administering the local user database so having a site that hosted the web api could be an option.

Is the Web API stuff wrong for this situation?  If not, does it hold advantages over the WCF web service?  Are there alternative methods of achieving my end result?

答:
It sounds like your unique requirements dictate that you are indeed on the right track that you will need an external universal authentication provider for all of your applications, especially the requirement that a change in Membership Provider as you put it cannot and should not result in a new release for your application.

At a universal authentication provider you can control which sites use which membership providers and can even subjugate responsibilities like delegating Roles and Privileges to various users after authenticating them.

I advise caution however on the WCF approach as it seems SOAP based web services are slowly being replaced in importance by REST based web services like what is offered in ASP.NET Web API.  The future of applications is mobile, and every major mobile development platform has strong support for REST based web services.

If you even have the slightest doubt that you might need to extend membership provider support to mobile applications eventually then it is a good idea to forgo WCF.  Furthermore, REST based web services allow better integration by third parties (perhaps clients wanting to integrate with your software package?) to communicate with your application and retrieve information from it.

As far as authentication protocols for REST based web services, there doesn't appear to be a good out of the box way to do this, but security is rather essential.  There is a good article on how to secure your authentication requests to your authentication provider application.  http://codebetter.com/johnvpetersen/2012/04/02/making-your-asp-net-web-apis-secure/


问:
Unit Testing DatabaseOpenHelper class in Android
I've written several unit tests for an already existing DatabaseOpenHelper class. I covered the creation scenario and migration from old schema to new one. Unfortunately, I don't feel that my tests are good. For example, for DB creation I check that SQLiteDatabase. execSQL() was called three times (we have three tables so far) and check that query string is in set of specified SQL strings.

The issue so far - if someone changes order of columns the test will fail. Probably this is OK.

But what are other ways of SQL schema unit testing? Or don't you have unit tests for that, and rely only on the integration one?

答:
A schema comparison could be useful. You have one database that is known to have the "correct" schema. This could be a copy of the development database after a code freeze. Then create your new database. Use a comparison tool to identify any differences between the 2 databases. Microsoft Visual Studio comes with a schema comparison tool. There are several 3rd party tools. I use my own custom-built comparison tool which lists all the differences in 2 databases.

Alternatively you could simply check if your schema creation scripts execute without error. The scripts ARE the schema definition, so in theory you do not need to compare or check anything other than the fact they executed without error.

But in my experience people introduce changes to a database without scripting them. A comparison tool can catch these unscripted changes.