答:
Object orientation is valuable specifically because these types of scenarios arise, and it gives you tools to reasonably design abstractions that allow you to encapsulate complexity.

The real question here is, where do you encapsulate that complexity?

So let me step back a moment and speak to what 'complexity' I'm referring to here. Your problem (as I understand it; correct me if I'm wrong) is a persistence model which is not an effectively usable model for the tasks you need to complete with the data. It may be effective and usable for other tasks, but not for your tasks.

So what do we do when we have data that does not present a good model for our means?

Translate. It's the only thing you can do. That translation is the 'complexity' I refer to above. So now that we accept we're going to translate the model, we need to decide on a couple of factors.

Do we need to translate both directions? Are both directions going to be translated the same, as in:

(Tbl A, Tbl B) -> Obj X (read)

Obj X -> (Tbl A, Tbl B) (write)

or do insertion/update/delete activities represent a different type of object such that you read data as Obj X, but data is inserted/updated from Obj Y? Which of these two ways you wish to go, or if no update/insert/delete is possible are important factors in where you want to put the translation.

Where do you translate?

Back to the first statement I made in this answer; OO allows you to encapsulate complexity and what I refer to here is the fact that not only should you, but you must encapsulate that complexity if you wish to ensure it doesn't leak out and seep into all of your code. At the same time, it's important to recognize you can't have a perfect abstraction so worry less about that than about having a very effective and usable one.

Again now; your problem is: Where do you put this complexity? Well you have choices.

You can do it in the database using stored procedures. This has the drawback of often not playing very well with ORMs but that's not always true. Stored procedures afford some benefits, including performance often. Stored procedures however can require a lot of maintenance, but it's up to you to analyze your particular scenario and say if the maintenance will be more or less than other choices. I personally am very skilled with stored procedures, and as such this fact of available talent reduces overhead; never underestimate the value of making decisions based on what you do know. Sometimes the suboptimal solution can be more optimal than the correct solution because you or your team know how to create and maintain it better than the optimal solution.

Another in-database option is views. Depending on your database server these may be highly optimal or sub-optimal or not even effective at all, one of the drawbacks can be query times depending on what indexing options are available in your database. Views become an even better choice if you never need to make any data modification (insert/update/delete).

Stepping past the database you have the old standby of using the repository pattern. This is a time-tested approach which can be very effective. Drawbacks tend to include boiler plate but well-factored repositories can avoid some amount of this, and even when these do result in unfortunate amounts of boiler plate, repository's tend to be simple code that's easy to understand and maintain as well as presenting a good API/abstraction. Also repositories can be good for their unit-testability which you lose with in-database options.

There are tools like auto-mapper out there that may make using an ORM plausible where they can do the translation between database-model from orm to usable models, but some of these tools can be tricky to maintain/understand behaving more like magic; though they create a minimum of overhead code resulting in less maintenance overhead when well understood.

Next you're stepping further and further from the database, which means there's going to be greater amounts of code that are going to deal with the un-translated persistence model, which is going to be genuinely unpleasant. In these scenarios you talk about putting the translation layer in your UI which it sounds like you may be doing now. This is generally a very bad idea, and decays terribly over time.

Now let's start talking crazy.

The Object is not the only end-all be-all abstraction that exists. There have been a profundity of abstractions developed over the many years that computer science has been studied and even before then from the study of math. If we're going to start getting creative, let's start talking about known abstractions available that have been studied.

There's the actor model. This is an interesting approach because it says all you do is send messages to other code which effectively delegates all the work to that other code, which is very effective in encapsulating the complexity away from all your code. This could work in so far as you send a message to an actor saying "I need Obj X sent to Y" and you have a receptacle waiting for a response at location Y which then processes Obj X. You could even send a message that instructs "I need Obj X and computation Y, Z done to it" and then you don't even need to wait; the translation occurs on the other side of that message pass and you can just move on if you don't need a read of it's result. This can be slight abuse of the actor model for your purposes, but it all depends; the main goal of Actor Model is asynchrony and concurrency to be handled easily and well, both of those things are however just abstractions which can act as boundaries to encapsulate the complexity we refer to here (or any form of complexity for that matter).

Another encapsulation boundary is process boundaries. These can be used for segregating complexity very effectively. You could create the translation code as a web service where the communication is simple HTTP, using SOAP, REST, or if you really want your own protocol (not suggested). STOMP isn't altogether a bad newer protocol. Or use a normal daemon service with a system-local publicized memory pipe for communicating very quickly again using whichever protocol you choose. This actually has some pretty good benefits:

You can have multiple processes running that do translation for older and newer version support at the same time allowing you to update the translation service to publicize an object model V2, and then separately at a later point update the consuming code to work with the new object model.

You can do interesting things like pinning the process to a core for performance, you also get an amount of security safety in this approach by making that the only process running with the security privileges to touch that data.

You will get a very strong boundary when you're talking about process boundaries that will stay fixed ensuring minimum leakage of your abstraction for a long time because writing code in the translation space will not be able to be called outside of the translation space since they won't share process scope, ensuring a fixed set of usage scenarios by contract.

Ability for asynchronous/non-blocking updates being simpler.

Drawbacks are obviously more maintenance than is commonly necessary, communication overhead affecting performance and the maintenance.

There is a great variety of ways to encapsulate complexity that may allow that complexity to be placed in ever more strange and curious places in your system. Using forms of higher order functions (often times faked using strategy pattern or various other odd forms of object patterns), you can do some very interesting things.

That's right, let's start talking about a monad. You could create this translation layer in a very independent fashion of small specific functions that do the independent translations necessary, but hide all of those translation functions away not visible so they are hardly accessible to outside code. This has the benefit of reducing reliance on them allowing them to change easily without effecting much external code. You then create a class that accepts higher order functions (anonymous functions, lambda functions, strategy objects, however you need to structure them) which work on any of the nice OO model type objects. You then let the underlying code which accepts those functions do the literal execution using the appropriate translation methods.

This creates a boundary where all the translation not only exists on the other side of the boundary away from all your code; it is only used on that side allowing the rest of your code to not even know anything about it other than where the entry point for that boundary is.

Ok, yeah that really is talking crazy, but who knows; you might just be that crazy (seriously, do not undertake monads with a craziness rating below 88%, there is real risk of bodily injury).

Title: How to create better OO code in a relational database driven application where the database is poorly designed

Body:
I am writing a Java web application that consists mainly of a bunch of similar pages in which every page has several tables and a filter that applies to those tables. The data on these tables comes from an SQL database.

I am using myBatis as ORM, which may not be the best choice in my case, since the database is poorly designed and mybatis is a more database oriented tool.

I am finding that I am writing a lot of duplicate code because, due to the database's poor design, I have to write different queries for similar things as those queries can be very different. That is, I cannot easily parametricise the queries. This propagates into my code and instead of populating rows on columns in my table with a simple loop I have code like:

get A Data (p1, ..., pi);

get B Data (p1, ..., pi);

get C Data (p1, ..., pi);

get D Data (p1, ..., pi);
...

And this soon explodes when we have different tables with different columns.

It also adds to the complexity the fact that I'm using "wicket", which is, in effect a mapping of objects to html elements in the page. So my Java code becomes an adapter between the database and the front end, which has me creating a lot of wiring, boilerplate code with some logic intermingled in it.

Would the correct solution be wrapping the ORM mappers with an extralayer that presents a more homogeneous interface to the db or is there a better way to deal with this spaghetti code I'm writing?

EDIT: More info about the database

The database holds mainly phone calls information. The poor design consists of:

Tables with an artificial ID as primary key that has nothing to do with the domain knowledge.

No unique, triggers, checks or foreign keys whatsoever.

Fields with a generic name that match different concepts for different records.

Records that can be categorised only by crossing with other tables with different conditions.

Columns which should be numbers or dates stored as strings.

To sum it up, a messy / lazy design all around.


问:
Unit-Tests and databases: At which point do I actually connect to the database?
There are answers to the question on how test classes that connect to a database, e.g. "Should service test classes connect ..." and "Unit testing - Database coupled app".

So, in short let's assume you have a class A that needs to connect to a database. Instead of letting A actually connect, you provide A with an interface that A may use to connect. For testing you implement this interface with some stuff - without connecting of course. If class B instantiates A it has to pass a "real" database connection to A. But that means B opens a database connection. That means to test B you inject the connection into B. But B is instantiated in class C and so forth.

So at which point do I have to say "here I fetch data from a database and I will not write a unit test for this piece of code"?

In other words: Somewhere in the code in some class I must call sqlDB.connect() or something similar. How do I test this class?

And is it the same with code that has to deal with a GUI or a file system?

I want to do Unit-Test. Any other kind of test is not related to my question. I know that I will only test one class with it (I so agree with you Kilian). Now, some class has to connect to a DB. If I want to test this class and ask "How do I do this" many say: "Use Dependency Injection!" But that only shifts the problem to another class, doesn't it? So I ask, how do I test the class that really, really establishes the connection?

Bonus question: Some answers here boil down to "Use mock objects!" What does that mean? I mock classes that the class-under-test depend upon. Shall I mock the class-under-test now and actually test the mock (which comes close to the idea of using Template Methods, see below)?

答:
Performing unit tests against a database connection is perfectly normal, and a common practice. It's simply not possible to create a purist approach where everything in your system is dependency injectable.

The key here is to test against a temporary or testing only database, and having the lightest possible start up process for building that test database.

For unit testing in CakePHP there are things called fixtures. Fixtures are temporary database tables created on the fly for a unit test. The fixture has convenience methods for creating them. They can recreate a schema from a production database inside the testing database, or you can define the schema using a simple notation.

The key to success with this is to not implement the business database, but to focus only on the aspect of the code you are testing. If you have a unit test that verifies that a data model reads only published documents, then the table schema for that test should only have the fields required by that code. You don't have to re-implement an entire content management database just to test that code.

Some additional references.

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

http://phpunit.de/manual/3.7/en/database.html

http://book.cakephp.org/2.0/en/development/testing.html#fixtures

ContentLicense: CC BY-SA 4.0

ContentLicense: CC BY-SA 3.0


问:
How to create better OO code in a relational database driven application where the database is poorly designed
I am writing a Java web application that consists mainly of a bunch of similar pages in which every page has several tables and a filter that applies to those tables. The data on these tables comes from an SQL database.

I am using myBatis as ORM, which may not be the best choice in my case, since the database is poorly designed and mybatis is a more database oriented tool.

I am finding that I am writing a lot of duplicate code because, due to the database's poor design, I have to write different queries for similar things as those queries can be very different. That is, I cannot easily parametricise the queries. This propagates into my code and instead of populating rows on columns in my table with a simple loop I have code like:

get A Data (p1, ..., pi);

get B Data (p1, ..., pi);

get C Data (p1, ..., pi);

get D Data (p1, ..., pi);
...

And this soon explodes when we have different tables with different columns.

It also adds to the complexity the fact that I'm using "wicket", which is, in effect a mapping of objects to html elements in the page. So my Java code becomes an adapter between the database and the front end, which has me creating a lot of wiring, boilerplate code with some logic intermingled in it.

Would the correct solution be wrapping the ORM mappers with an extralayer that presents a more homogeneous interface to the db or is there a better way to deal with this spaghetti code I'm writing?

EDIT: More info about the database

The database holds mainly phone calls information. The poor design consists of:

Tables with an artificial ID as primary key that has nothing to do with the domain knowledge.

No unique, triggers, checks or foreign keys whatsoever.

Fields with a generic name that match different concepts for different records.

Records that can be categorised only by crossing with other tables with different conditions.

Columns which should be numbers or dates stored as strings.

To sum it up, a messy / lazy design all around.

答:
I can see how your existing database schema causes you to write more specific code and queries for tasks which might otherwise be abstracted with a better-designed schema, but it shouldn't hamper your ability to write good object-oriented code.

Remember the SOLID principles.

Write code that can easily unit-tested (which often comes through following SOLID principles).

Keep your business logic separate from your display logic.

Read the Apache Wicket documentation and examples - this framework can probably save you more boilerplate code than you think, so learn how to use it effectively.

Keep the logic that has to deal with the database in a separate layer that provides a clean interface that your business logic can work with. This way, if you (or a future maintainer) ever get a chance to improve the schema, they can do so without too many changes to the business logic.

When you find yourself working with a database schema that isn't perfect, it's easy to gripe about all the ways that it makes your job harder, but at some point you have to set aside those complaints and make the best of it.

Think of it as an opportunity to use your creativity to write clean, reusable, easily maintainable code in spite of the imperfect schema.


问:
Can't I just use all static methods?
What's the difference between the two UpdateSubject methods below? I felt using static methods is better if you just want to operate on the entities. In which situations should I go with non-static methods?

```<code>public class Subject
{
    public int Id {get; set;}
    public string Name { get; set; }

    public static bool UpdateSubject(Subject subject)
    {
        //Do something and return result
        return true;
    }
    public bool UpdateSubject()
    {
        //Do something on 'this' and return result
        return true;
    }
}
</code>```

I know I will be getting many kicks from the community for this really annoying question but I could not stop myself asking it.

Does this become impractical when dealing with inheritance?

Update:

Its happening at our work place now.
We are working on a 6 month asp.net web application with 5 developers. Our architect decided we use all static methods for all APIs. His reasoning being static methods are light weight and it benefits web applications by keeping server load down.

答:
There's a lot of great answers here, and they're a lot more insightful and knowledgeabe than anything I could ever come up with as an answer, but I feel there's a little something that's not being adressed:

"Our architect decided we use all static methods for all APIs. His reasoning being static methods are light weight and it benefits web applications by keeping server load down."

(bold emphasis is mine)

My 2c on this part of the question is this:

Theoretically, what is said there is true: calling a static method only has the overhead of actually invoking that method. Calling a non-static (instance) method has the extra overhead of first instantiating the object, and at some point, destroying the instance (either manually or via some form of automatic garbage collection, depending on the platform used).

The play a little of devil's advocate on this: we can go even further and say stuff such as:

this can become really bad if an instance is created for each invocation of the (instance) method (as opposed to just leaving it static and calling it like that)

depending of the complexity of the constructor, the type hyerarchy, other instance members and other such unknown factors, the extra overhead of the non-static method call can vary and become really big

In truth, IMHO, the above points (and the general approach of "let's use statics because they're faster") are straw man arguments/assesments:

if the code is good, and, more specific to this argument, if the instances are created only when necessary (and destroyed in a an optimal fashion), then you do not get any extra overhead from using insance methods when appropriate (because if you only create the needed objects, those would have been created even if that method was declared as static some where else = same instanciation overhead)

by abusing static methods declaration in this way, it's possible to hide some issues with your code in regard to how instances are created (since the method is static an incorrect instanciation code may pass unnoticed until later, and that's never good).

Title: Can't I just use all static methods?


问:
Best books on Managing a Software Development Team?
The canonical books on software development is fairly well established. However, after reading through a dreadful book full of bad advice on managing programming teams this weekend I am looking for recommendations for really good books that focus on the management side of programming (recruiting, performance measurement/management, motivation, best practices, organizational structure,  etc.) and not as much on the construction of software itself.

Any suggestions?

答:
After your read Peopleware (a bit outdated, 1999, but is THE classic) here a more recent one from the same author (Tom DeMarco
)

Slack Getting Past Burnout, Busywork, and the Myth of Total Efficiency

ContentLicense: CC BY-SA 2.5


问:
necessary to migrate to Web API from MVC controller
Since the released of Web API, I've been wondering whether it's necessary or helpful to migrate/change MVC controllers to use Web API calls. Since it's hard to describe code I'll show an example of what I mean:

Current controller, the _userService is basically the Business layer of the application that is then calling the repository and getting all users.

```<code>[HttpGet]
public ActionResult List()
{
    var list = _userService.GetAll();
    if(list.Any())
        return View("List",list);
    return View(list.ToList());
}
</code>```

Would it be better to use Web API, and get the list of the users as JSON object and then bind it to the view with some JavaScript? (possibly knockout if applicable).

Not sure if my question is clear enough, so let me know if I have to explain something.

答:
What you are asking is not about how to code in MVC or in ASP.NET, I think it's about using a different approach.

The architecture that you kind of describe is very similar to a Single Page Application (SPA) where the server-side turns into a thin API and all the logic of the application is moved to client-side code written in JavaScript (logic, navigation, routing, rendering, etc..).

Of course you could have a "hybrid" solution using MVC to deal with logic, navigation, routing, etc.. and have some API's to serve some data to some pages that use some client-side code.

However, I think this would add complexity to the application because there would be a thick-server (MVC) and in some parts a semi-thick-client plus the fact that there would be an API.
So it would end up with MVC + API + Client-Side code.

The SPA approach follows the thin-server / thick-client. The server only acts as a repository of Data + some authentication logic (if needed) and the client has all the logic to run the application in the browser. All the communications between the browser and server use AJAX. This way we reduce the complexity by only using an API + JavaScript.

I don't know if this answer would help you, but I think that if you start to think about using AJAX more and more in MVC, you will end up with a SPA.


答:
This is a very interesting question that tends to have very different answers depending on the community you are asking it. Other answers seems to be C# or java bias : a programming language that is (mostly) pure object oriented and have a kind of idiomatic view of what object orientation is.

In other communities, such as C++, object orientation is interpret with more liberality. Some well knows experts have study the subject and, actually, conclude that free functions improve encapsulation (emphasis is mine):

We've now seen that a reasonable way to gauge the amount of
  encapsulation in a class is to count the number of functions that
  might be broken if the class's implementation changes. That being the
  case, it becomes clear that a class with n member functions is more
  encapsulated than a class with n+1 member functions. And that
  observation is what justifies my argument for preferring non-member
  non-friend functions to member functions

Scott Meyers

For those unfamiliar with C++ terminology:

member function = method

non-member function = static method

non-friend = use only public API

non-member non-friend function = static method that use only public API

Now, to answer you question :

Can't I just use all static methods?

No, you should not always use static methods.

But, you should use them whenever you only need to use the public API of a class.

Title: Can't I just use all static methods?

Body:
What's the difference between the two UpdateSubject methods below? I felt using static methods is better if you just want to operate on the entities. In which situations should I go with non-static methods?

```<code>public class Subject
{
    public int Id {get; set;}
    public string Name { get; set; }

    public static bool UpdateSubject(Subject subject)
    {
        //Do something and return result
        return true;
    }
    public bool UpdateSubject()
    {
        //Do something on 'this' and return result
        return true;
    }
}
</code>```

I know I will be getting many kicks from the community for this really annoying question but I could not stop myself asking it.

Does this become impractical when dealing with inheritance?

Update:

Its happening at our work place now.
We are working on a 6 month asp.net web application with 5 developers. Our architect decided we use all static methods for all APIs. His reasoning being static methods are light weight and it benefits web applications by keeping server load down.


问:
How do I output an error when I'm determining how to output an error?
I've written an API which can be configured to signal errors in different ways (return values, debugging messages, or exceptions). I've got a check in place when the API is first initialized to see what type of error handling the client is requesting - what should I do when they request an undefined error handling type?

This is kind of similar to "Where do I log the error that I can't write to the logfile?"

Do I go with the idiom of the language, do I fatal? Do I throw an exception, even though if they aren't asking for exception handling they probably haven't wrapped the call in a try/catch? The language I'm writing doesn't have an ENUM type so it can't be a type error.

答:
I see only a couple of choices:

Assume the most common default and use it

Crash (fatal error / configuration error)

Walk through your different options until you find one that works (i.e., if the most common default is 'write to file' and you don't have a file name, keep falling back to different options until you get to one you know works)

Use all available options at the same time (i.e., throw a config error AND return and error code AND write to the console AND ...)

Personally, I'd probably crash with a fatal configuration error, but your situation may be different.


问:
Unit testing and coupling
As a developer I should strive for low coupling between classes.

But low coupling doesn't mean "No coupling" so sometimes I have to allow some code flexibility and use "new" inside a domain model. (I'm sure everybody does this in real production code)

If I use "new" inside a method to create an object and I have to unit test this method I got a problem. Because I can't mock the objects I can't test the method in isolation. I know I can use dependency injection or I can send objects as parameters but I'm not talking about this clear cases.

Is this acceptable in practice?

Is there a better way? (I can't always inject)

Is this a unit test or an integration test?

PHP example:

```<code>class Car {
   protected $objDoor;
   public function createComponents() {
       $this->objDoor = new Door();
   }
}
</code>```

答:
```<code>  > Is there a better way? (I can't always inject)
</code>```

Not better but easier and equivalent:

If you donot want to inject a factory-method/class to create a new domain-child item you can implement a local protected virtual factory method

```<code>public class MyClassUnderTest {
   virtual protected MyObjectType createMyObjectType() {return new MyObjectType();}
}
</code>```

and use this factory method instead of calling new MyObjectType() directly.

In the test you either inherit from MyClassUnderTest and overwrite createMyObjectType() with some mocking code or ask the mocking-framework to return some special childobject if createMyObjectType() is called.

Title: Unit testing and coupling

Body:
  > Is there a better way? (I can't always inject)

Not better but easier and equivalent:

If you donot want to inject a factory-method/class to create a new domain-child item you can implement a local protected virtual factory method

public class MyClassUnderTest {
   virtual protected MyObjectType createMyObjectType() {return new MyObjectType();}
}

and use this factory method instead of calling new MyObjectType() directly.

In the test you either inherit from MyClassUnderTest and overwrite createMyObjectType() with some mocking code or ask the mocking-framework to return some special childobject if createMyObjectType() is called.


答: Lots of books mentioned above for the Software front. I would say no one book or a couple is sufficient. Management itself is a difficult task and is very subjective. I would recommend someone who is considering Management to read a lot of Harvard Business review books. They all have a concrete idea to share and these take a lot of reading and effort in implementation to sink in.</p>

Title: Best books on Managing a Software Development Team?

Body:
The canonical books on software development is fairly well established. However, after reading through a dreadful book full of bad advice on managing programming teams this weekend I am looking for recommendations for really good books that focus on the management side of programming (recruiting, performance measurement/management, motivation, best practices, organizational structure,  etc.) and not as much on the construction of software itself.

Any suggestions?


问:
Why does the US government disallow dynamic languages for secure projects?
I know some people that are currently working on a project for the US military (low security level, non-combat human resources type data).

An initial state of the project code was submitted to the military for review, and they ran the program through some sort of security analyzer tool. It returned a report of known security issues in the code and required changes that needed to be implemented before delivery of the final product.

One of the items that needed to be resolved was removal of part of the project that was written in Ruby as it is a dynamic language.

What is the background/reason for not allowing a dynamic language to be used in a secure setting? Is this the government being slow to adopt new technologies? Or do dynamic languages pose an additional security risk compared to static languages (ala C++ or Java)?

答:
Dynamic languages can be used in defense and military applications. I've personally used and delivered Perl and Python in DoD applications. I've also seen PHP and JavaScript used and deployed. In my experiences, most of the non-compiled code that I've seen has been shell scripts and Perl because the environments required are approved and installed on a variety of possible target systems.

The fact that these languages are dynamic most likely isn't the problem. The interpreters for these languages must be approved for use on the target systems. If the interpreter is not approved for use (or, perhaps, it is, but it is not deployed on the target systems), then the language can't be used. Using a given interpreter (or any application) on a secure system requires any number of security hurdles: analysis of the source, the ability to compile from source for target environments, additional analysis of the binaries, ensuring no conflicts with existing infrastructure, etc.


答:
There is, somewhere in your codebase, a line of code that performs the actual action of connecting to the remote DB. This line of code is, 9 times in 10, a call to a "built-in" method provided by the runtime libraries specific to your language and environment. As such, it's not "your" code and so you don't need to test it; for the purposes of a unit test, you can trust that this method call will perform correctly. What you can, and should, still test in your unit test suite are things like ensuring the parameters that will be used for this call are what you expect them to be, such as making sure the connection string is correct, or the SQL statement or stored procedure name.

This is one of the purposes behind the restriction that unit tests should not leave their runtime "sandbox" and be dependent upon external state. It's actually quite practical; the purpose of a unit test is to verify that the code you wrote (or are about to write, in TDD) behaves the way you thought it would. Code that you didn't write, such as the library you are using to perform your database operations, shouldn't be part of the scope of any unit test, for the very simple reason that you didn't write it.

In your integration test suite, these restrictions are relaxed. Now you can design tests that touch the database, to make sure that the code you did write plays nicely with code you didn't. These two test suites should remain segregated, however, because your unit test suite is more effective the faster it runs (so you can quickly verify that all the assertions made by developers about their code still hold), and almost by definition, an integration test is slower by orders of magnitude because of the added dependencies on external resources. Let the build-bot handle running your full integration suite every few hours, executing the tests that lock up external resources, so that the developers aren't stepping on each other's toes by running these same tests locally. And if the build breaks, so what? A lot more importance is placed on ensuring the build-bot never fails a build than probably should be.

Now, how strictly you can adhere to this is dependent on your exact strategy for connecting to and querying the database. In many cases where you must use the "bare-bones" data access framework, such as ADO.NET's SqlConnection and SqlStatement objects, an entire method developed by you may consist of built-in method calls and other code that is dependent on having a database connection, and so the best you could do in this situation is mock the entire function and trust your integration test suites. It also depends on how willing you are to design your classes to allow specific lines of code to be replaced for testing purposes (such as Tobi's suggestion of the Template Method pattern, which is a good one because it allows "partial mocks" that exercise some methods of a real class while overriding others that have side effects).

If your data persistence model relies on code in your data layer (such as triggers, stored procs, etc) then there simply is no other way to exercise code you yourself are writing than to develop tests that either live inside the data layer or cross the boundary between your application runtime and the DBMS. A purist would say this pattern, for this reason, is to be avoided in favor of something like an ORM. I don't think I'd go quite that far; even in the age of language-integrated queries and other compiler-checked, domain-dependent persistence operations, I see the value in locking the database down to only the operations exposed via stored procedure, and of course such stored procedures must be verified using automated tests. But, such tests are not unit tests. They are integration tests.

If you have a problem with this distinction, it's usually based on a high importance placed on complete "code coverage" aka "unit test coverage". You want to ensure every line of your code is covered by a unit test. A noble goal on its face, but I say hogwash; that mentality lends itself to anti-patterns stretching far beyond this specific case, such as writing assertionless tests that execute but do not exercise your code. These types of end-runs solely for the sake of coverage numbers are more harmful than relaxing your minimum coverage. If you want to ensure that every line of your codebase is executed by some automated test, then that's easy; when computing code coverage metrics, include the integration tests. You could even go one step further and isolate these disputed "Itino" tests ("Integration in name only"), and between your unit test suite and this sub-category of integration tests (which should still run reasonably fast) you should get darn near close to full coverage.

Title: Unit-Tests and databases: At which point do I actually connect to the database?

Body:
There are answers to the question on how test classes that connect to a database, e.g. "Should service test classes connect ..." and "Unit testing - Database coupled app".

So, in short let's assume you have a class A that needs to connect to a database. Instead of letting A actually connect, you provide A with an interface that A may use to connect. For testing you implement this interface with some stuff - without connecting of course. If class B instantiates A it has to pass a "real" database connection to A. But that means B opens a database connection. That means to test B you inject the connection into B. But B is instantiated in class C and so forth.

So at which point do I have to say "here I fetch data from a database and I will not write a unit test for this piece of code"?

In other words: Somewhere in the code in some class I must call sqlDB.connect() or something similar. How do I test this class?

And is it the same with code that has to deal with a GUI or a file system?

I want to do Unit-Test. Any other kind of test is not related to my question. I know that I will only test one class with it (I so agree with you Kilian). Now, some class has to connect to a DB. If I want to test this class and ask "How do I do this" many say: "Use Dependency Injection!" But that only shifts the problem to another class, doesn't it? So I ask, how do I test the class that really, really establishes the connection?

Bonus question: Some answers here boil down to "Use mock objects!" What does that mean? I mock classes that the class-under-test depend upon. Shall I mock the class-under-test now and actually test the mock (which comes close to the idea of using Template Methods, see below)?


问:
Are missed deadlines common in programming jobs?
It was my freelancer job at oDesk. I have done several jobs earlier in given time, but is was the first time I missed the deadline. It was a very lengthy job and I tried my best but I still missed the deadline. Now, I am very scared. Because it's my fault that I missed the deadline.

My question is: Is this is a big concern or are missed deadlines common among programming jobs, so I shouldn't worry too much about this?

答:
It's fairly common, but you can get better at it.  You might want to look into estimating using something abstract like story points, and keep track of your velocity to calculate your actual estimates.  Those concepts are most commonly associated with scrum, but can be used even if you don't do scrum.

The amazing thing about velocity is it encompasses all the intangible things like interruptions and unexpected complexity that developers have a difficult time accounting for in their estimates.  All the probabilities average out over time.  Averaged over 10 weeks, our velocity estimates have been accurate within about 5%.  Yet when we estimate the same tasks in hours, the exact same team consistently underestimates by 30-50%.


答:
There are a number of 'neat' things that can be done in dynamic languages that can be tucked away in parts of the code that aren't immediately obvious to another programmer or auditor as to the functionality of a given piece of code.

Consider this sequence in irb (interactive ruby shell):

```<code>irb(main):001:0> "bar".foo
NoMethodError: undefined method `foo' for "bar":String
        from (irb):1
        from /usr/bin/irb:12:in `<main>'
irb(main):002:0> class String
irb(main):003:1> def foo
irb(main):004:2> "foobar!"
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> "bar".foo
=> "foobar!"
</code>```

What happened there is I tried to call the method foo in a String constant.  This failed.  I then opened up the String class and defined the method foo o return "foobar!", and then called it.  This worked.

This is known as an open class and gives me nightmares every time I think of writing code in ruby that has any sort of security or integrity.  Sure it lets you do some neat things quite fast... but I could make it so every time someone stored a string, it stored it to a file, or sent it over the network.  And this little bit of redefining the String can be tucked anywhere in the code.

Many other dynamic languages have similar things that can be done.  Perl has Tie::Scalar that can behind the scenes change how a given scalar works (this is a bit more obvious and requires a specific command that you can see, but a scalar that is passed in from somewhere else could be a problem).  If you have access to the Perl Cookbook, look up Recipe 13.15 - Creating Magic Variables with tie.

Because of these things (and others often part of dynamic languages), many approaches to static analysis of security in code doesn't work.  Perl and Undecidability shows this to be the case and points out even such trivial problems with syntax highlighting (whatever  / 25 ; # / ; die "this dies!"; poses challenges because the whatever can be defined to take arguments or not at runtime completely defeating a syntax highlighter or static analyzer).

This can get even more interesting in Ruby with the ability to access the environment that a closure was defined in (see YouTube: Keeping Ruby Reasonable from RubyConf 2011 by Joshua Ballanco).  I was made aware of this video from an Ars Technica comment by MouseTheLuckyDog.

Consider the following code:

def mal(&block)
    puts ">:)"
    block.call
    t = block.binding.eval('(self.methods - Object.methods).sample')
    block.binding.eval <<-END
        def #{t.to_s}
          raise 'MWHWAHAW!'
        end
    END
end

class Foo
    def bar
        puts "bar"
    end

    def qux
        mal do
            puts "qux"
        end
    end
end

f = Foo.new
f.bar
f.qux

f.bar
f.qux
```

<p>This code is fully visible, but the <code>mal</code> method could be somewhere else... and with open classes, of course, it could be redefined somewhere else.</p>

<p>Running this code:</p>

```
~/$ ruby foo.rb
bar
>:)
qux
bar
b.rb:20:in `qux': MWHWAHAW! (RuntimeError)
    from b.rb:30:in `'
~/$ ruby foo.rb
bar
>:)
qux
b.rb:20:in `bar': MWHWAHAW! (RuntimeError)
    from b.rb:29:in `'
```

<p>In this code, the closure was able to access <em>all</em> of the methods and other bindings defined in the class at that scope.  It picked a random method and redefined it to raise an exception.  (see the Binding class in Ruby to get an idea of what this object has access to)</p>

<blockquote>
  <p>The variables, methods, value of self, and possibly an iterator block that can be accessed in this context are all retained.</p>
</blockquote>

<p>A shorter version that shows the redefinition of a variable:</p>

<pre class="lang-rb prettyprint-override"><code>def mal(&block)
    block.call
    block.binding.eval('a = 43')
end

a = 42
puts a
mal do
  puts 1
end
puts a
</code>```

Which, when run produces:

```
42
1
43
```

This is more than the open class that I mentioned above that makes static analysis impossible.  What is demonstrated above is that a closure that is passed somewhere else, carries with it the full environment that it was defined in.  This is known as a first class environment (just as when you can pass around functions, they are first class functions, this is the environment and all of the bindings available at that time).  One could redefine any variable that was defined in the scope of the closure.

Good or bad, complaining about ruby or not (there are uses where one would want to be able to get at the environment of a method (see Safe in Perl)), the question of "why would ruby be restricted in for a government project" really is answered in that video linked above.

Given that:

Ruby allows one to extract the environment from any closure

Ruby captures all bindings in the scope of the closure

Ruby maintains all bindings as live and mutable

Ruby has new bindings shadow old bindings (rather than cloning the environment or prohibiting rebinding)

With the implications of these four design choices, it is impossible to know what any bit of code does.

More about this can be read at Abstract Heresies blog.  The particular post is about Scheme where such a debate was had. (related on SO: Why doesn't Scheme support first class environments?)

Over time, however, I came to realise that there was more difficulty and less power with first-class environments than I had originally thought. At this point I believe that first-class environments are useless at best, and dangerous at worst.

I hope this section shows the danger aspect of first class environments and why it would be asked to remove Ruby from the provided solution.  Its not just that Ruby is a dynamic language (as mentioned else-answer, other dynamic languages have been allowed in other projects), but that there are specific issues that make some dynamic languages even more difficult to reason about.

Title: Why does the US government disallow dynamic languages for secure projects?

Body:
I know some people that are currently working on a project for the US military (low security level, non-combat human resources type data).

An initial state of the project code was submitted to the military for review, and they ran the program through some sort of security analyzer tool. It returned a report of known security issues in the code and required changes that needed to be implemented before delivery of the final product.

One of the items that needed to be resolved was removal of part of the project that was written in Ruby as it is a dynamic language.

What is the background/reason for not allowing a dynamic language to be used in a secure setting? Is this the government being slow to adopt new technologies? Or do dynamic languages pose an additional security risk compared to static languages (ala C++ or Java)?

Body:
There are a number of 'neat' things that can be done in dynamic languages that can be tucked away in parts of the code that aren't immediately obvious to another programmer or auditor as to the functionality of a given piece of code.

Consider this sequence in irb (interactive ruby shell):

irb(main):001:0> "bar".foo
NoMethodError: undefined method `foo' for "bar":String
        from (irb):1
        from /usr/bin/irb:12:in `<main>'
irb(main):002:0> class String
irb(main):003:1> def foo
irb(main):004:2> "foobar!"
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> "bar".foo
=> "foobar!"

What happened there is I tried to call the method foo in a String constant.  This failed.  I then opened up the String class and defined the method foo o return "foobar!", and then called it.  This worked.

This is known as an open class and gives me nightmares every time I think of writing code in ruby that has any sort of security or integrity.  Sure it lets you do some neat things quite fast... but I could make it so every time someone stored a string, it stored it to a file, or sent it over the network.  And this little bit of redefining the String can be tucked anywhere in the code.

Many other dynamic languages have similar things that can be done.  Perl has Tie::Scalar that can behind the scenes change how a given scalar works (this is a bit more obvious and requires a specific command that you can see, but a scalar that is passed in from somewhere else could be a problem).  If you have access to the Perl Cookbook, look up Recipe 13.15 - Creating Magic Variables with tie.

Because of these things (and others often part of dynamic languages), many approaches to static analysis of security in code doesn't work.  Perl and Undecidability shows this to be the case and points out even such trivial problems with syntax highlighting (whatever  / 25 ; # / ; die "this dies!"; poses challenges because the whatever can be defined to take arguments or not at runtime completely defeating a syntax highlighter or static analyzer).

This can get even more interesting in Ruby with the ability to access the environment that a closure was defined in (see YouTube: Keeping Ruby Reasonable from RubyConf 2011 by Joshua Ballanco).  I was made aware of this video from an Ars Technica comment by MouseTheLuckyDog.

Consider the following code:

def mal(&block)
    puts ">:)"
    block.call
    t = block.binding.eval('(self.methods - Object.methods).sample')
    block.binding.eval <<-END
        def #{t.to_s}
          raise 'MWHWAHAW!'
        end
    END
end

class Foo
    def bar
        puts "bar"
    end

    def qux
        mal do
            puts "qux"
        end
    end
end

f = Foo.new
f.bar
f.qux

f.bar
f.qux

This code is fully visible, but the mal method could be somewhere else... and with open classes, of course, it could be redefined somewhere else.

Running this code:

~/$ ruby foo.rb
bar
>:)
qux
bar
b.rb:20:in `qux': MWHWAHAW! (RuntimeError)
    from b.rb:30:in `'
~/$ ruby foo.rb
bar
>:)
qux
b.rb:20:in `bar': MWHWAHAW! (RuntimeError)
    from b.rb:29:in `'

In this code, the closure was able to access all of the methods and other bindings defined in the class at that scope.  It picked a random method and redefined it to raise an exception.  (see the Binding class in Ruby to get an idea of what this object has access to)

The variables, methods, value of self, and possibly an iterator block that can be accessed in this context are all retained.

A shorter version that shows the redefinition of a variable:

def mal(&block)
    block.call
    block.binding.eval('a = 43')
end

a = 42
puts a
mal do
  puts 1
end
puts a

Which, when run produces:

42
1
43

This is more than the open class that I mentioned above that makes static analysis impossible.  What is demonstrated above is that a closure that is passed somewhere else, carries with it the full environment that it was defined in.  This is known as a first class environment (just as when you can pass around functions, they are first class functions, this is the environment and all of the bindings available at that time).  One could redefine any variable that was defined in the scope of the closure.

Good or bad, complaining about ruby or not (there are uses where one would want to be able to get at the environment of a method (see Safe in Perl)), the question of "why would ruby be restricted in for a government project" really is answered in that video linked above.

Given that:

Ruby allows one to extract the environment from any closure

Ruby captures all bindings in the scope of the closure

Ruby maintains all bindings as live and mutable

Ruby has new bindings shadow old bindings (rather than cloning the environment or prohibiting rebinding)

With the implications of these four design choices, it is impossible to know what any bit of code does.

More about this can be read at Abstract Heresies blog.  The particular post is about Scheme where such a debate was had. (related on SO: Why doesn't Scheme support first class environments?)

Over time, however, I came to realise that there was more difficulty and less power with first-class environments than I had originally thought. At this point I believe that first-class environments are useless at best, and dangerous at worst.

I hope this section shows the danger aspect of first class environments and why it would be asked to remove Ruby from the provided solution.  Its not just that Ruby is a dynamic language (as mentioned else-answer, other dynamic languages have been allowed in other projects), but that there are specific issues that make some dynamic languages even more difficult to reason about.


答:
Unit tests should never connect to a database. By definition, they should test a single unit of code each (a method) in total isolation from the rest of your system. If they don't, then they are not a unit test.

Semantics aside, there are a myriad of reasons why this is beneficial:

Tests run orders of magnitude faster

Feedback loop becomes instant (<1s feedback for TDD, as an example)

Tests can be run in parallel for build/deploy systems

Tests don't need a database to be running (makes build much easier, or at least quicker)

Unit tests are a way to check your work. They should outline all of the scenarios for a given method, which typically means all of the different paths through a method. It is your specification that you are building to, similar to double-entry bookkeeping.

What you are describing is another type of automated test: an integration test. While they are also very important, ideally you will have much less of them. They should verify that a group of units integrate with each other properly.

So how do you test things with database access? All of your data access code should be in a specific layer, so your application code can interact with mockable services instead of the actual database. It shouldn't care whether those services are backed by any type of SQL database, in-memory test data, or even remote webservice data. That is not their concern.

Ideally (and this is very subjective), you want the bulk of your code covered by unit tests. This gives you confidence that each piece works independently. Once the pieces are built, you need to put them together. Example - when I hash the user's password, I should get this exact output.

Let's say that each component is made up of roughly 5 classes - you'd want to test all of the points of failure within them. This equates to a lot less tests just to ensure everything is wired properly. Example - test you can find the user from the database given a username/password.

Finally, you want some acceptance tests to actually ensure you are meeting business objectives. There are even less of these; they may ensure the application is running and does what it was built to do. Example - given this test data, I should be able to log in.

Think of these three types of tests as a pyramid. You need a lot of unit tests to support everything, and then you work your way up from there.


问:
Is it better to specialize in a single field I like, or expand into other fields to broaden my horizons?
This is a dilemma about which I have been thinking for quite a while.

I'm a graduate student and my topics of interest are programming language design, code analysis, compilation, etc. So far, this field has been very interesting and rewarding for me, so I was thinking about finding a job in that field and continuing to specialize in it. I feel like it's a relatively solid field which won't "get out of style" anytime soon. I've always thought that in such complex fields it's better to be a real expert than just another guy who superficially understand what the experts are talking about.

On the other hand, I feel that by specializing this way I really limit my future option. I have always been a strong believer in multidisciplinary approaches to problems. Maybe I should go search for a general programming job in which I could gain experience in other fields, as well as occasionally apply my favorite field for solving problems. Specializing in only one or two fields can prevent me from thinking outside the box and cause stagnation.

I would really like to hear more opinions about this choice. The truth is I'm already leaning towards one of the choices, so basic psychology says nothing will change my mind, but I would still love to hear some feedback.

答:
Specialise if you enjoy it

As you are aware, if you specialise you are automatically incurring an opportunity cost in that you won't be immediably eligible for other technologies (e.g. Java programmers don't often immediately get accepted for compiler optimisation jobs). However, you have to balance this with your love of the complexity inherent in your chosen discipline.

You say you want to be an expert - well go ahead and take the time to learn your chosen discipline. We as a community always need new experts. However, my advice is to follow the pragmatic programmer recommendation of "Learn a new language every year". That way, while you're engaging in deep lexical analysis of algorithmic encoding, you can also be churning out a little iPhone app that interests you on the side. You never know, the cross pollenation of different paradigms may cause you some insight that will extend your specialisation into new areas.


答:
Assuming the evaluation was security-only, and not just an acceptance scan (that is, they do not accept Ruby because they don't want to support Ruby) then:

Security analysis tools typically have a bad time with dynamic behaviors.

For example:

Run any .NET project written with modern features like ASP.NET MVC and Entity Framework through something like Veracode and see what kind of laundry list of false positives you receive in your report.

Veracode even lists many basic techniques within .NET 4 core libraries as "unsupported frameworks" as unsupported or beta only even though most of them are several years old at this point.

If you are dealing with an entity that has a strong reliance on such a tool they are almost forced to consider those insecure if they do not have the technical expertise, and the resources, to manually evaluate a project and see if it is properly written and secure.

In civilian operations where computer systems generally don't control anything dangerous or terribly expensive the mitigation is that you discuss the false positives and they are generally accepted as such in bulk.

In banking operations you still have a chance of a false positive mitigation, but you are going to spend a lot more time discussing the minutiae of each item. This rapidly becomes cost prohibitive and you start using more traditional methods.

In the military, aviation, heavy industry and the like, systems can control things that have terrible failure modes those systems so they may have very strict rules about languages, compilers, etc.

Organizations also generally write their security policy for the worst case they know of, so even if you are writing something trivial, if you are writing it for an organization that has non-trivial systems, the default is generally going to be to hold it to a higher standard unless someone requests a specific exception.


问:
Decoupling Components - Design
I have created a a number of non-trivial scripts in Python that do some long running reporting. At first these reports started off as scripts with a config file. Then I added in the a database component to persist data. Now I'm building a GUI to sit on top of all of them.

My question relates to design. Each time I add an additional requirement, I find myself wishing I had decoupled the components of these scripts more than I did. When I consider how to recfactor my code to be more modular, I find that I need to pass quite a few references around the code, to the point that class initializers and function signatures have a ton of parameters. Also I find myself having parameters that are simply "pass-through", in that one object needs them only to pass to another object, doing nothing with them.

What is the best way to approach modularity in Python while minimizing the need to pass references all over the place?

I understand this is very conceptual and will vary by design, but I'm looking for guiding principles.

答:
That's an interesting question, I have done exactly the same thing but in Ruby.  Basically my reporting solution evolved like this:

A temporary script to show management what is going on.  The script only ran as a console app, so I copy and pasted the resulting data into Excel and added some nice formatting and graphs.

The temporary solution became a weekly report and since vacation was near, I added an HTML output through a separate script.  The commonly used functions were put into a common library file.

Some business software producing the input changed but the reports were popular.  At that point I ported the report to Go and changed from a procedural/functional style to an Object Oriented style.  (Go became the lingua franca at my job, you can do the same thing in Python as well)

I assume you are in my world at step 1. or 2.  As you have seen yourself, modularization is needed.  Some patterns I remember when doing that:

passing many parameters repeatedly to a function?  Create a class that can carry all those parameters.  Probably some functions could become methods of that class

go abstract: I did many grouping, counting and aggregation operations.  I created classes (types) that perform those operations for many possible table formats.  If you have such a transformation class, you can define transformation functions/lambdas (functional!) for the data types you need.  So in a nutshell: remove all the business from the problem at just think of the data types and the transformations you need.

For the second point: as you are using Python, I think Numpy is a perfect tool for that job.  It has some very powerful transformation tools for tabular data.

Title: Decoupling Components - Design


答:
(link to the book)

Agile or not, a retrospective process (looking back on a team's recent work and looking for ways to do it better) is important.  This book has lots of useful techniques a team can use to reconstruct what went on, figure out root causes, and decide what to do going forward.


问:
Real world analogy for a clustered index
A db index is analogous to a table of contents. This helps me understand db index in an easy way. My question is are tehre any real world analogies for a clustered index?</p>

答: The library (or bookstore if you prefer) puts books into groups (all history books here, scifi books there) because they assume that if you want one history book, you're more likely to want other history books rather than different kinds of books. Likewise, it becomes easier to sort the scifi books by author rather than all books by author.


问:
Object Oriented Design
Suppose you have the following:

```<code>     +--------+     +------+
     | Animal |     | Food |
     +-+------+     +----+-+
       ^                 ^
       |                 |
       |                 |
  +------+              +-------+
  | Deer |              | Grass |
  +------+              +-------+
</code>```

Deer inherits from Animal, and Grass inherits from Food.

So far so good. Animal objects can eat Food objects.

Now lets mix it up a bit. Lets add a Lion which inherits from Animal.

```<code>     +--------+     +------+
     | Animal |     | Food |
     +-+-----++     +----+-+
       ^     ^           ^
       |     |           |
       |     |           |
  +------+ +------+     +-------+
  | Deer | | Lion |     | Grass |
  +------+ +------+     +-------+
</code>```

Now we have have a problem because Lion can eat both Deer and Grass, but Deer is not  Food it is Animal.

With out using multiple inheritance, and using object oriented design, how do you solve this problem?

FYI: I used http://www.asciiflow.com to create the ASCII diagrams.

答:
You have just come across the basic limitation of OO.

OO works well with hierarchical structures. But once you get away from strict hierarchies the abstraction does not work so well.

I know all about metamorphosis compositions etc. that are used to get around these limitations but they are clumsy, and, more importantly lead to obscure and hard to follow code.

Relational data bases were invented primarily to get away from the limitations of strict hierarchical structures.

To take your example grass could also be a building material, a raw material for paper, a clothing material, a weed or a crop.

A deer could be a pet, livestock, a zoo animal  or a protected species.

A lion could also be a zoo animal or a protected species.

Life is not simple.

Title: Object Oriented Design


答: I would describe a clustered index as "the actual order." Thus, for example, a physical dictionary has a clustered index on word, ordered in ascending order from A-Z (slight simplification).  This also explains why a clustered index is so much faster to read than a regular index.  If I feel the urge to read a book by index entry it will require me to jump all over the book, since consecutive index entries tend to point to different pages.  On the other hand, reading every word starting with "The" in a dictionary requires less jumping around; merely find "The", then read in order.</p>

Title: Real world analogy for a clustered index

Body: A db index is analogous to a table of contents. This helps me understand db index in an easy way. My question is are tehre any real world analogies for a clustered index?</p>


问:
Is it valid to initialize an instance of a class within the same class?
I was wondering if it's valid to initialize an instance of a class within the same class? For example:

```<code>public class Person()
{
   string name;
   string age;
   public Person getPerson()
   {
       Person p1=new Person();
       //some logic here
       return p1
    }
}
</code>```

I ended up doing that for a part of my code at work and thus wanted to know if there are any potential problems with doing something like this.

答:
Yes, it is valid.

That's called a factory method.

The Calendar class, for example has a getInstance() method.

That's also useful when implementing the Singleton pattern.

Here you have a SO question about when to use a factory method.


答:
If the Table of Contents in the front of the book is non-clustered, the page numbering itself (on the actual pages) is the clustered index. The "clustered" nature of an index indicates that the records are stored with the index nodes (or at least in the same order). So, the page-number analogy is actually very accurate in this respect, because like a real clustered index, the page numbers are located on the pages, which are bound together in page number order.

Put another way:

When you use the book's TOC (non-clustered index), you search for a term which points to a page number. You then have to perform another search to locate the actual page.

The search performed against the page number (clustered index) leads you directly to the page. No secondary searching is required.

Not incidentally, this is why you can only have one clustered index. It defines the physical order of the records.

https://stackoverflow.com/questions/1251636/what-do-clustered-and-non-clustered-index-actually-mean

http://en.wikipedia.org/wiki/Database_index#Clustered


答:
Yes. Often done when you need to build large, dynamic, self-maintaining structures like Trees:</p>

```<code>class Tree {

  public IComparable Value;
  private Tree Left;
  private Tree Right;

  public Add (IComparable value) {
    if (this.Value == null) {
      this.Value = value;

      // RIGHT HERE!
      this.Left = new Tree();
      this.Right = new Tree();

    } else {
      if (value > this.Value) {
        this.Right.Add(value);
      } else {
        this.Left.Add(value);
      }
    }
  } // Add()

} // class Tree
</code>```

Title: Is it valid to initialize an instance of a class within the same class?

Body:
I was wondering if it's valid to initialize an instance of a class within the same class? For example:

```<code>public class Person()
{
   string name;
   string age;
   public Person getPerson()
   {
       Person p1=new Person();
       //some logic here
       return p1
    }
}
</code>```

I ended up doing that for a part of my code at work and thus wanted to know if there are any potential problems with doing something like this.

Body:
Yes. Often done when you need to build large, dynamic, self-maintaining structures like Trees:

class Tree {

  public IComparable Value;
  private Tree Left;
  private Tree Right;

  public Add (IComparable value) {
    if (this.Value == null) {
      this.Value = value;

      // RIGHT HERE!
      this.Left = new Tree();
      this.Right = new Tree();

    } else {
      if (value > this.Value) {
        this.Right.Add(value);
      } else {
        this.Left.Add(value);
      }
    }
  } // Add()

} // class Tree


答:
Managing Humans

Not many concrete action items, but it's an interesting read and provides perspective.


问:
Does increasing the number of classes increase code complexity?
To illustrate the question, let's say we have two programmers of comparable skill that both solve the same problem. The code they turn out has roughly the same lines of code, but one programmer uses 5 classes while another programmer uses 20.

Both sets of code get comparable wtfs per minute in code review, implying that the code is not hard to follow and doesn't do anything silly.

Is the code with 20 classes more complex than the code that utilizes 5?

答:
I generally have one class for each of my tables, plus as many enums as necessary for enumerated fields, plus data services and user controls and all kinds of stuff.  The only WTFs I get are from users, although the rate is measured in hours, not in minutes.

Since there's no discussion of language specifics, I can only go by what I do in C#.  I generally try to create highly repeatable design patterns, so that someone looking at one class will pretty much find something similar in the other ones.  I'm not sure how I could 'cut down the number' given my design rules.


答:
Cohesion and Coupling are the two relevant terms, however, there is no possible fixed answer due to their contradicting nature.

Cohesion

Cohesion is a measure of how closely together the methods of a class are working. If you have high cohesion, then most of your methods are responsible for a similar thing, whereas if you have low cohesion, the methods are doing more or less independent things. This is slightly related to the Single Responsibility Principle (SRP), which tells us that you should aim for high cohesion.

Coupling

In contrast to cohesion, coupling refers to interdependencies between modules/classes. If you have high coupling, this means that your modules interact a lot with each other, whereas low coupling means that there are only few interactions, i.e. small interfaces. Again, it is easy to see that low coupling is better.

Contradiction

The problem with achieving both, high cohesion and low coupling, is that changing your code to improve one means that you reduce the other.

As a simple example, consider all of your functionality to be contained in one single class. This means you have extremely low coupling, which is good! But of course, you also have extremly low cohesion, because all of your class's methods will have to do different things.

On the other extreme consider splitting things up into soooo many classes, that each of them only contains a single small method. That means you have very high cohesion, which is good again, but to do the actual work, these methods would have to access a lot of other classes. These dependencies mean you get very high coupling as well, which is not good either.

Tradeoff

Essentially, you end up with a tradeoff: Focus on cohesion, and you lose out on coupling. Focus on coupling, and you lose out on cohesion. To make a good choice about where you should go on that tradeoff scale, coupling and cohesion themselves are insufficient criteria. Instead you should look to other criteria of code quality, and see where on the scale they lead you.

For example, the SRP gives you good cohesion, and there are people out there who advocate this as the only truth, but of course if you follow the SRP very strictly, you end up with lots of classes and high coupling. Sometimes, you may find that all of that coupling is giving you lots of trouble. In such a case you may justify that it's worth to reduce the coupling at the cost of cohesion by violating the SRP (or let's just say re-interpreting what that single reponsiblity is).

As a best practice approach, I believe that the SRP is a save bet. In general, high coupling is more problematic than low cohesion, simply because we find it easier to deal with a problem localized to one class (cohesion). Therefore, if you follow the SRP it'll lead you towards the low coupling end of the tradeoff scale, which is at the very least a good point to start from.

Title: Does increasing the number of classes increase code complexity?

Body:
To illustrate the question, let's say we have two programmers of comparable skill that both solve the same problem. The code they turn out has roughly the same lines of code, but one programmer uses 5 classes while another programmer uses 20.

Both sets of code get comparable wtfs per minute in code review, implying that the code is not hard to follow and doesn't do anything silly.

Is the code with 20 classes more complex than the code that utilizes 5?


问:
Garbage collectors and pointers/ reference
I am a little confused on both pointers and reference.  From my understanding pointers are addresses in memory.  If I pass a variable using pointers and reference to a function, any manipulations of the variable in the function will change the original variable.

I read online that passing by reference is not encouraged because it can get messy code easily.  I also read here (https://stackoverflow.com/questions/7058339/c-when-to-use-references-vs-pointers), that we should avoid pointers if we can.

My question is when should pointers and reference be used?  I know pointers are used in Xcode for strong references and reference counting.  Since, higher level languages like c# have garbage collector, does that mean we shouldn't ever use pointers and reference in them?

答:
I think of a pointer as what is returned from "alloc" or equivalent.  In real systems, it's typically a reference to a chunk of memory, but not necessarily.
Passing pointers around completely kosher, although undesciplined manipulation
of the objects pointed to can be messy.

I think of a reference as a pointer I created rather than alloc, and for which "free" would not be appropriate.   Use them extremely cautiously, for example
to return multiple values from a function.  It's extremely dangerous to treat
a reference like a proper pointer, because if mistreated your pointer becomes a time bomb - for example taking a reference to a structure allocated on the stack, and allowing it to be retained when the stack frame it refers to is reclaimed.


答:
There are 3 main reasons to use a pointer

You do actually want to modify the things that you're passed. For example, in a swap function, you'd really like to actual swap the variables your given and not just copies of them. A more common example here is the this pointer/reference in C++, C#, Java, etc.

For whatever reason, copying your argument is not feasible. For example, you don't want to copy a whole new 100000 item list, it's just too slow. Or if you have something that shouldn't be copied, like a mutex or some other valuable resource.

This is much less common but sometimes you want to be able to go reach out and actually touch some random piece of memory or a register. This is very rare but quite possible in low enough code.

In these cases, pass a reference where possible and a pointer otherwise. Generally prefer references to pointers where possible since a pointer can be null.

Title: Garbage collectors and pointers/ reference

Body:
I am a little confused on both pointers and reference.  From my understanding pointers are addresses in memory.  If I pass a variable using pointers and reference to a function, any manipulations of the variable in the function will change the original variable.

I read online that passing by reference is not encouraged because it can get messy code easily.  I also read here (https://stackoverflow.com/questions/7058339/c-when-to-use-references-vs-pointers), that we should avoid pointers if we can.

My question is when should pointers and reference be used?  I know pointers are used in Xcode for strong references and reference counting.  Since, higher level languages like c# have garbage collector, does that mean we shouldn't ever use pointers and reference in them?


问:
When being a technical genius is not enough to be a successful developer?
There has been a lot of discussion around the excellent question "Will high reputation in Stack Overflow help to get a good job?". I immediately agreed with JoshK that basically said "No" (I'll will explain why), but Joel chimed in with lot of convincing facts which made me upvote him as well.

My question is what other skills (other than being a technical genius) do you require from a developer? To get the job, or to keep it.

I believe being a genius is far from being enough. I have met many technical geniuses in various companies I have worked for that impressed me a lot, but sadly in lot of cases, they were simply fired after a few months or put in ivory towers (mainly because of internal mutiny from other developers). I've seen many in personal distress as a result of this which I understand.

That's why I'm a big fan of non-technical questions for technical positions. I like to know how the candidate will interact with others (including non-technical employees), how much consideration he will have for the business, if he will work for the desired outcome, and so on.

I would like to know what you require from your developers and WHY it's important (after all, you hire someone to write code, don't you? Why would you want him to be assertive?). Ideally, I'd like you to come up with an example question you would ask during interviews in support of your answer.

答:
Its never enough.

Common Sense. Problem Solving. Interest in technology as a whole. Attitude of doing things the right way. Persistence. Team play/Communication.

Rate each skill on 1-5. If you got min 3 and on an average 20+. You will have a good life as a developer. 25+ will be successful developer. Less than 10 and you need to seriously think about a career in software development.


答:
My theory (unproven) is that humans have evolved to underestimate complicated jobs by two or three to one.  Any time Congress asks NASA something like: what will it cost to build a shuttle or travel to the moon they come back within a week with a number.  After they run out all the expected costs they discover it will cost three times as much.

We had a joke in the 1970s: take any programmer estimate, double the number, then move it to the next unit of time.  Therefore, if a programmer says it can be done in two weeks, he will finish it in four months.

If anyone has remodeled a kitchen they generally think 'Well I'll have this done in two weeks'.  They finish it about six weeks later.

Title: Are missed deadlines common in programming jobs?

Body:
It was my freelancer job at oDesk. I have done several jobs earlier in given time, but is was the first time I missed the deadline. It was a very lengthy job and I tried my best but I still missed the deadline. Now, I am very scared. Because it's my fault that I missed the deadline.

My question is: Is this is a big concern or are missed deadlines common among programming jobs, so I shouldn't worry too much about this?


问:
How far should one take e-mail address validation?
I'm wondering how far people should take the validation of e-mail address. My field is primarily web-development, but this applies anywhere.

I've seen a few approaches:

simply checking if there is an "@" present, which is dead simple but of course not that reliable.

a more complex regex test for standard e-mail formats

a full regex against RFC 2822 - the problem with this is that often an e-mail address might be valid but it is probably not what the user meant

DNS validation

SMTP validation

As many people might know (but many don't), e-mail addresses can have a lot of strange variation that most people don't usually consider (see RFC 2822 3.4.1), but you have to think about the goals of your validation: are you simply trying to ensure that an e-mail message can be sent to an address, or that it is what the user probably meant to put in (which is unlikely in a lot of the more obscure cases of otherwise 'valid' addresses).

An option I've considered is simply giving a warning with a more esoteric address but still allowing the request to go through, but this does add more complexity to a form and most users are likely to be confused.

While DNS validation / SMTP validation seem like no-brainers, I foresee problems where the DNS server/SMTP server is temporarily down and a user is unable to register somewhere, or the user's SMTP server doesn't support the required features.

How might some experienced developers out here handle this? Are there any other approaches than the ones I've listed?

Edit: I completely forgot the most obvious of all, sending a confirmation e-mail! Thanks to answerers for pointing that one out. Yes, this one is pretty foolproof, but it does require extra hassle on the part of everyone involved. The user has to fetch some e-mail, and the developer needs to remember user data before they're even confirmed as valid.

答:
The three main levels of email validation:

1) regular expression check for a properly formatted email address email@email.com

2) email domain check against MX records to see if the domain name has an email service

3) sending a confirmation email with a confirmation link or code

Level 1:

In Visual Studio, you can use the "Regular Expression Validator". And in the "ValidationExpression" property you can click on the "..." button that has a wizard to add in the regular expression format for email addresses.

Level 2:

Here is my C# code below to use nslookup to verify whether an email domain has valid MX records. Runs quick and ok on Win 2008 R2 and Win 7.

using System.Net.Mail;
using System.Diagnostics;

public static bool checkMXRecords(string email)
    {
        MailAddress addr = new MailAddress(email);
        string domain = addr.Host;

        string command = "nslookup -querytype=mx " + domain;
        ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);

        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.UseShellExecute = false;

        procStartInfo.CreateNoWindow = true;

        Process proc = new Process();
        proc.StartInfo = procStartInfo;
        proc.Start();
        string result = proc.StandardOutput.ReadToEnd();

        if (result.ToLower().Contains("mail exchanger"))
        {
            return true;
        }
        else return false;

     } // checkMXRecords
```

another option is to use the Arsofttools nuget package but it may be slow on Windows Server 2008 R2 as I have experienced but runs quick on Win 7.

Level 3:

For email confirmation, you can either generate an email specific hex url (using encryption functions) etc http://domain.com/validateEmail?code=abcd1234 to validate the email address when the user clicks on it. There is no need to store this url in memory.

OwnerDisplayName: Mike Tomasello

Body:
The three main levels of email validation:

1) regular expression check for a properly formatted email address email@email.com

2) email domain check against MX records to see if the domain name has an email service

3) sending a confirmation email with a confirmation link or code

Level 1:

In Visual Studio, you can use the "Regular Expression Validator". And in the "ValidationExpression" property you can click on the "..." button that has a wizard to add in the regular expression format for email addresses.

Level 2:

Here is my C# code below to use nslookup to verify whether an email domain has valid MX records. Runs quick and ok on Win 2008 R2 and Win 7.

using System.Net.Mail;
using System.Diagnostics;

public static bool checkMXRecords(string email)
    {
        MailAddress addr = new MailAddress(email);
        string domain = addr.Host;

        string command = "nslookup -querytype=mx " + domain;
        ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);

        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.UseShellExecute = false;

        procStartInfo.CreateNoWindow = true;

        Process proc = new Process();
        proc.StartInfo = procStartInfo;
        proc.Start();
        string result = proc.StandardOutput.ReadToEnd();

        if (result.ToLower().Contains("mail exchanger"))
        {
            return true;
        }
        else return false;

     } // checkMXRecords

another option is to use the Arsofttools nuget package but it may be slow on Windows Server 2008 R2 as I have experienced but runs quick on Win 7.

Level 3:

For email confirmation, you can either generate an email specific hex url (using encryption functions) etc http://domain.com/validateEmail?code=abcd1234 to validate the email address when the user clicks on it. There is no need to store this url in memory.


问:
Pointers in C vs No pointers in PHP
Both languages have the same syntax.

Why does C have the weird * character that denotes pointers (which is some kind of memory address of the variable contents?), when PHP doesn't have it and you can do pretty much the same things in PHP that you can do in C, without pointers? I guess the PHP compiler handles this internally, why doesn't C do the same?

Doesn't this add unneeded complexity in C? For example I don't understand them :)

答:
You can accomplish much of the functionality of pointers by using integer operations on an array index. This integer index is often called a cursor.

Instead of manipulating a pointer value char *p, you manipulate the cursor int i and index the array arr[i] to yield the value in that memory cell.

To some extent, this is just putting fancy names on obvious facts. But having names lends credibility to a valid technique that may be easily overlooked. :)

The real advantage is that familiarity with this allows you to translate code using pointers into languages that don't have them.

Title: Pointers in C vs No pointers in PHP


问:
Why does Java compiler decide whether you can call a method based on the "reference" type and not on actual "object" type?
I was just wondering why does Java compiler decide whether you can call a method based on the "reference" type and not on actual "object" type? To explain I would like to quote an example:

```<code>class A {
    void methA() {
        System.out.println("Method of Class A.");
    }
}

class B extends A {
    void methB() {
        System.out.println("Method of Class B.");
    }
    public static void main(String arg[]) {
        A ob = new B();
        ob.methB();       // Compile Time Error
    }
}
</code>```

This will produce a Compile Time Error that method methB() not found in class A, although Object Reference "ob" contains an object of class B which consists of method methB(). Reason for this is that Java Compiler checks for the method in Class A (the reference type) not in Class B (the actual object type). So, I want to know whats the reason behind this. Why does Java Compiler looks for the method in Class A why not in Class B(the actual object type)?

答:
Your example is in some way a special case. In a non trivial program you can typically not determine the class of the object a certain reference points to better than "it is of the type the reference was declared for or a subtype of it".

The whole concept of polymorphism is based on the fact that the concrete class is only known at runtime but not at compile time. This of course means that the compiler must ensure that the methods that are called on a reference will be available on the referenced object at runtime. The only methods for which this holds true are the methods of the class for which the variable was declared (including all inherited methods from the super classes).

EDIT

As @David said in his comment the reason behind this is that typing is done statically by the programmer in java. If java had type inference the typing would be done by the compiler (but still statically) and your example would be absolutely valid. But it could be invalidated by another line doing this ob = new A().

If java were a dynamic typed language the compiler wouldn't care at all (at compile time) what methods you call on an object.


答:
I am a little confused on both pointers and reference.
  From my understanding pointers are addresses in memory.

In general, they are both addresses in memory. Usually one speaks of pointers if arithmetic can be done on them and of references if not, but there are some language-specific variations.

If I pass a variable using pointers and reference to a function, any
  manipulations of the variable in the function will change the original
  variable.

Yes.

But there are three reasons for passing something by reference:

To avoid copying large amount of data to the stack.

To modify the original variable.

To support polymorphism, as the generated code does not know in advance how large the object will be and how to properly copy it.

Code using the second option is always more difficult to read than code using return values and if it is not used consistently, it is a sure way to create real mess. To distinguish this case from the other two C++ has the const modifier. Unfortunately Java and C# don't (and their authors don't seem to understand why it is useful; might have something to do with the fact that many people sell const in C/C++ as optimization tool which it certainly isn't).

I read online that passing by reference is not encouraged because it can get messy code easily.

High level languages hold everything by reference, because of the reason 3 above. They may pass the reference by value (just like passing pointer in C; the function can't change where the pointer points, but it can change content of the pointed object) or by reference (that is pointer-to-pointer; the function can make the variable point to different object), but it is always a reference to the actual object.

Java does that except for primitive numeric types and C# does it for everything that is class.

But passing by reference for the reason 2 above is indeed discouraged. One simply does not normally expect function to modify it's arguments, so better not do it.

I also read here (https://stackoverflow.com/questions/7058339/c-when-to-use-references-vs-pointers), that we should avoid pointers if we can.

That is C++-specific. References can't be set to nullptr (0 in older C++) so you don't have to check and references can't be deleted, so you don't have to think whether you should.

My question is when should pointers and reference be used?

Wherever they have to be.

I know pointers are used in Xcode for strong references and reference
  counting. Since, higher level languages like c# have garbage
  collector, does that mean we shouldn't ever use pointers and reference
  in them?

Managed languages (C#, Java, ...) don't have pointers at all. Pointers allow access to raw memory and that would interfere with the managed runtime. So they don't have them (C# does, in the special, "unsave", extension, to allow interfacing with plain old C libraries).

But references can't be not used in them. Everything is a reference. Because allocated memory can only be handled by pointer/reference. And because of polymorphism. So all variables of object types are references in Java and C# and all variables are references in anything dynamically typed like smalltalk, perl, python, ruby, javascript etc.


答:
By declaring ob as a A, you're telling the compiler that you are using an object that behaves like A.

Java performs strict type checking. If you tell that ob is an instance of A, then, whatever you assign to it, Java will make sure you keep to A's contract.

This can help you create loosely coupled code that resists to future changes.

Let's say at some point you need another implementation of A :

```<code>class C extends A {
    void methA() {
        System.out.println("Overriden methA.");
    }
}
</code>```

And that you have sticked to A contract :

```<code>   public static void main(String arg[])
   {
      A ob = new B();
      ob.methA();  // output: "Method of Class A."
   }
</code>```

Then, you can easily substitute C to B, because your code does not depend on B :

```<code>   public static void main(String arg[])
   {
      A ob = new C();
      ob.methA(); // output: "Overriden methA."
   }
</code>```

Java lets you choose the level of abstraction you need and then help you enforce it.

If you write code that needs to call methB (and so depends on B class), then tell it to Java:

```<code>   public static void main(String arg[])
   {
      B ob = new B();
      ob.methB();  // output: "Method of Class B."
   }
</code>```

Now only instances of B or subclasses of B can be assigned to ob.

Title: Why does Java compiler decide whether you can call a method based on the "reference" type and not on actual "object" type?

Body:
I was just wondering why does Java compiler decide whether you can call a method based on the "reference" type and not on actual "object" type? To explain I would like to quote an example:

```<code>class A {
    void methA() {
        System.out.println("Method of Class A.");
    }
}

class B extends A {
    void methB() {
        System.out.println("Method of Class B.");
    }
    public static void main(String arg[]) {
        A ob = new B();
        ob.methB();       // Compile Time Error
    }
}
</code>```

This will produce a Compile Time Error that method methB() not found in class A, although Object Reference "ob" contains an object of class B which consists of method methB(). Reason for this is that Java Compiler checks for the method in Class A (the reference type) not in Class B (the actual object type). So, I want to know whats the reason behind this. Why does Java Compiler looks for the method in Class A why not in Class B(the actual object type)?

Body:
By declaring ob as a A, you're telling the compiler that you are using an object that behaves like A.

Java performs strict type checking. If you tell that ob is an instance of A, then, whatever you assign to it, Java will make sure you keep to A's contract.

This can help you create loosely coupled code that resists to future changes.

Let's say at some point you need another implementation of A :

class C extends A {
    void methA() {
        System.out.println("Overriden methA.");
    }
}

And that you have sticked to A contract :

   public static void main(String arg[])
   {
      A ob = new B();
      ob.methA();  // output: "Method of Class A."
   }

Then, you can easily substitute C to B, because your code does not depend on B :

   public static void main(String arg[])
   {
      A ob = new C();
      ob.methA(); // output: "Overriden methA."
   }

Java lets you choose the level of abstraction you need and then help you enforce it.

If you write code that needs to call methB (and so depends on B class), then tell it to Java:

   public static void main(String arg[])
   {
      B ob = new B();
      ob.methB();  // output: "Method of Class B."
   }

Now only instances of B or subclasses of B can be assigned to ob.


答:
Your object is type of 'A' and 'A' is not having a definition of 'methB()'. That is why Java compiler is not allowing.

```<code>class ClassA
{
    void MethodA() { }
}

class ClassB extends ClassA
{
    void MethodB() { }
}

class Program
{
    public static void main(string[] args)
    {
        ClassA a = new ClassB();
        a.MethodB(); // Compile time error.
    }
}
</code>```

If you override any method in the derive class then at runtime it will call the actual method depending on the object which is pointed by the reference.

```<code>class ClassA
{
    void MethodA()
    {
        Console.WriteLine("A");
    }
}

class ClassB extends ClassA
{
    void MethodA()
    {
        Console.WriteLine("B");
    }
}

class Program
{
    public static void main(string[] args)
    {
        ClassA a = new ClassB();
        a.MethodA(); // Output: B
    }
}
</code>```

Body:
Your object is type of 'A' and 'A' is not having a definition of 'methB()'. That is why Java compiler is not allowing.

class ClassA
{
    void MethodA() { }
}

class ClassB extends ClassA
{
    void MethodB() { }
}

class Program
{
    public static void main(string[] args)
    {
        ClassA a = new ClassB();
        a.MethodB(); // Compile time error.
    }
}

If you override any method in the derive class then at runtime it will call the actual method depending on the object which is pointed by the reference.

class ClassA
{
    void MethodA()
    {
        Console.WriteLine("A");
    }
}

class ClassB extends ClassA
{
    void MethodA()
    {
        Console.WriteLine("B");
    }
}

class Program
{
    public static void main(string[] args)
    {
        ClassA a = new ClassB();
        a.MethodA(); // Output: B
    }
}