问:
Is it normal to spend as much, if not more, time writing tests than actual code?
I find tests a lot trickier and harder to write than the actual code they are testing. It's not unusual for me to spend more time writing the test than the code it is testing.

Is that normal or am I doing something wrong?

The questions “Is unit testing or test-driven development worthwhile?”, “We are spending more time implementing functional test than implementing the system itself, is this normal?” and their answers are more about whether testing is worth it (as in "should we skip writing tests altogether?"). While I'm convinced tests are important, I'm wondering if my spending more time on tests than actual code is normal or if it's only me.

Judging by the number of views, answers and upvotes my question received, I can only assume its a legitimate concern that isn't addressed in any other question on the website.

答:
It is.

Even if you take just unit testing, it is not unusual to have more code within the tests than the actually tested code. There is nothing wrong with it.

Consider a simple code:

public void SayHello(string personName)
{
    if (personName == null) throw new NullArgumentException("personName");

    Console.WriteLine("Hello, {0}!", personName);
}
```

What would be the tests? There are at least four simple cases to test here:

The person name is null. Is exception actually thrown? That's at least three lines of test code to write.

The person name is "Jeff". Do we get "Hello, Jeff!" in response? That's four lines of test code.

The person name is an empty string. What output do we expect? What is the actual output? Side question: does it match the functional requirements? That means another four lines of code for the unit test.

The person name is short enough for a string, but too long to be combined with "Hello, " and the exclamation point. What happens?¹

This requires a lot of testing code. Moreover, the most elementary pieces of code often require setup code which initializes the objects needed for the code under test, which also often leads to writing stubs and mocks, etc.

If the ratio is very big, in which case you may check a few things:

Is there code duplication across the tests? The fact that it's test code doesn't mean that the code should be duplicated (copy-pasted) between similar tests: such duplication will make the maintenance of those tests difficult.

Are there redundant tests? As a rule of thumb, if you remove a unit test, the branch coverage should decrease. If it doesn't, it may indicate that the test is not needed, since the paths are already covered by other tests.

Are you testing only the code you should test? You are not expected to test the underlying framework of the third-party libraries, but exclusively the code of the project itself.

With smoke tests, system and integration tests, functional and acceptance tests and stress and load tests, you add even more test code, so having four or five LOC of tests for every LOC of actual code is not something you should be worried about.

A note about TDD

If you're concerned about the time it takes to test your code, it might be that you are doing it wrong, that is code first, tests later. In this case, TDD may help by encouraging you to work in iterations of 15-45 seconds, switching between code and tests. According to the proponents of TDD, it speeds up the development process by reducing both the number of tests you need to do and, more importantly, the quantity of business code to write and especially rewrite for testing.

¹ Let n be the maximum length of a string. We can call SayHello and pass by reference a string of length n - 1 which should work just fine. Now, at Console.WriteLine step, the formatting should end up with a string of length n + 8, which will result in an exception. Possibly, due to the memory limits, even a string containing n / 2 characters will lead to an exception. The question one should ask is whether this fourth test is a unit test (it looks like one, but may have a much higher impact in terms of resources compared to average unit tests) and if it tests the actual code or the underlying framework.

Body:
It is.

Even if you take just unit testing, it is not unusual to have more code within the tests than the actually tested code. There is nothing wrong with it.

Consider a simple code:

public void SayHello(string personName)
{
    if (personName == null) throw new NullArgumentException("personName");

    Console.WriteLine("Hello, {0}!", personName);
}

What would be the tests? There are at least four simple cases to test here:

The person name is null. Is exception actually thrown? That's at least three lines of test code to write.

The person name is "Jeff". Do we get "Hello, Jeff!" in response? That's four lines of test code.

The person name is an empty string. What output do we expect? What is the actual output? Side question: does it match the functional requirements? That means another four lines of code for the unit test.

The person name is short enough for a string, but too long to be combined with "Hello, " and the exclamation point. What happens?¹

This requires a lot of testing code. Moreover, the most elementary pieces of code often require setup code which initializes the objects needed for the code under test, which also often leads to writing stubs and mocks, etc.

If the ratio is very big, in which case you may check a few things:

Is there code duplication across the tests? The fact that it's test code doesn't mean that the code should be duplicated (copy-pasted) between similar tests: such duplication will make the maintenance of those tests difficult.

Are there redundant tests? As a rule of thumb, if you remove a unit test, the branch coverage should decrease. If it doesn't, it may indicate that the test is not needed, since the paths are already covered by other tests.

Are you testing only the code you should test? You are not expected to test the underlying framework of the third-party libraries, but exclusively the code of the project itself.

With smoke tests, system and integration tests, functional and acceptance tests and stress and load tests, you add even more test code, so having four or five LOC of tests for every LOC of actual code is not something you should be worried about.

A note about TDD

If you're concerned about the time it takes to test your code, it might be that you are doing it wrong, that is code first, tests later. In this case, TDD may help by encouraging you to work in iterations of 15-45 seconds, switching between code and tests. According to the proponents of TDD, it speeds up the development process by reducing both the number of tests you need to do and, more importantly, the quantity of business code to write and especially rewrite for testing.

¹ Let n be the maximum length of a string. We can call SayHello and pass by reference a string of length n - 1 which should work just fine. Now, at Console.WriteLine step, the formatting should end up with a string of length n + 8, which will result in an exception. Possibly, due to the memory limits, even a string containing n / 2 characters will lead to an exception. The question one should ask is whether this fourth test is a unit test (it looks like one, but may have a much higher impact in terms of resources compared to average unit tests) and if it tests the actual code or the underlying framework.


答:
It can be for many people, but it depends.

If you're writing tests first (TDD), you may be experiencing some overlap in the time spent writing the test is actually helpful to writing the code. Consider:

Determining results and inputs (parameters)

Naming Conventions

Structure - where to put things.

Plain old thinking

When writing tests after writing the code, you may discover your code is not easily testable, so writing tests is harder/takes longer.

Most programmers have been writing code a lot longer than tests, so I would expect most of them to not be as fluent. Plus you get to add the time it takes to understand and utilize your testing framework.

I think we need to change our mindset about how long it takes to code and how unit testing is involved. Never look at it in the short-term and never-ever just compare the total time to deliver a particular feature because you have to consider not only are you writing better/less buggy code, but code that is easier to change and still make it better/less buggy.

At some point, we're all only capable of writing code that is so good, so some of the tools and techniques can only offer so much in improving our skills. It's not like I could build a house if I only had a laser guided saw.

ContentLicense: CC BY-SA 3.0

Title: Is it normal to spend as much, if not more, time writing tests than actual code?

Body:
I find tests a lot trickier and harder to write than the actual code they are testing. It's not unusual for me to spend more time writing the test than the code it is testing.

Is that normal or am I doing something wrong?

The questions “Is unit testing or test-driven development worthwhile?”, “We are spending more time implementing functional test than implementing the system itself, is this normal?” and their answers are more about whether testing is worth it (as in "should we skip writing tests altogether?"). While I'm convinced tests are important, I'm wondering if my spending more time on tests than actual code is normal or if it's only me.

Judging by the number of views, answers and upvotes my question received, I can only assume its a legitimate concern that isn't addressed in any other question on the website.


答:
I remember from a software engineering course, that one spends ~10% of development time writing new code, and the other 90% is debugging, testing, and documentation.

Since unit-tests capture the debugging, and testing effort into (potentially automate-able) code, it would make sense that more effort goes into them; the actual time taken shouldn't be much more than the debugging and testing one would do without writing the tests.

Finally tests should also double as documentation! One should write unit-tests in the way the code is intended to be used; i.e. the tests (and usage) should be simple, put the complicated stuff in the implementation.

If your tests are hard to write then, the code they test is probably hard to use!


问:
What's the use of lint warnings vs errors?
I'm hoping someone who's given this some thought can help me understand.  Most style-checking tools seem to distinguish between warnings and errors.

To me, the point of a lint error is that a style issue ought to be fixed.

In the cases where the tool isn't smart enough to see that an exception can be made, many tools have a syntax for ignoring/acknowledging the errors.  This makes sense too.

What cases are left where a warning might be of any use?

答:
Lint is not just about style-checking.

Many Lint tools do static code analysis.  As such, they can detect errors in your code that would otherwise go undetected.  That means you can screen your program for bugs before you run it.

The first Lint program was for the C language; it detected things like divide by zero, variables being used before they are set, and calculations likely to overflow.  As the language standard improved and compilers got better at detecting and reporting errors, the Lint program gradually became less important.  Today there's a Lint program for Javascript that Douglas Crockford wrote, which is still important because Javascript is still a young language.

Anyway, the point is that most of the coding style issues are more than likely warnings and not errors, because they are not going to cause your program to break.  Things that will cause your program to break are classified as errors, not warnings.


答:
Lint errors mean something is broken, or will break at runtime. Guaranteed null pointer errors, divide by zero errors, etc. Stuff like this that is legal syntactically, but is nonsensical:

```<code>int x = 5 / 0;
int y = *((int *) 0);
</code>```

Warnings should mean "this is risky, but not guaranteed to cause a runtime error or exception." Example:

```<code>bool x = true;
while (x = true) { // Oops! Should be "==" or just omit the test
  ...
}
</code>```

Implicit conversions in C++ also come to mind, as does mixing signed and unsigned variables in a single expression. They can cause some very hard to track down bugs, but Lint or a good compiler can point them out as warnings. Not errors, because the programmer might actually mean it and it is not an error that will crash your program.

Title: What's the use of lint warnings vs errors?

Body:
I'm hoping someone who's given this some thought can help me understand.  Most style-checking tools seem to distinguish between warnings and errors.

To me, the point of a lint error is that a style issue ought to be fixed.

In the cases where the tool isn't smart enough to see that an exception can be made, many tools have a syntax for ignoring/acknowledging the errors.  This makes sense too.

What cases are left where a warning might be of any use?

Body:
Lint errors mean something is broken, or will break at runtime. Guaranteed null pointer errors, divide by zero errors, etc. Stuff like this that is legal syntactically, but is nonsensical:

int x = 5 / 0;
int y = *((int *) 0);

Warnings should mean "this is risky, but not guaranteed to cause a runtime error or exception." Example:

bool x = true;
while (x = true) { // Oops! Should be "==" or just omit the test
  ...
}

Implicit conversions in C++ also come to mind, as does mixing signed and unsigned variables in a single expression. They can cause some very hard to track down bugs, but Lint or a good compiler can point them out as warnings. Not errors, because the programmer might actually mean it and it is not an error that will crash your program.


问:
License issues re: using Angularjs and Bootstrap in a desktop app
I'm a web developer and I've been asked to write a desktop app. The languages and libraries that I'm most familiar with are standard web tools (Javascript, Angular, Bootstrap). I'd like to use these same tools.

Lately there's been considerable interest in using web tools for Desktop, and frameworks have appeared that make it technically feasible.

I wonder, though, about legal feasibility. Are there "gotchas" with the licenses on Angular and Bootstrap that would complicate using them on the Desktop? Can these libraries be packaged into standalone applications, downloaded, and run offline, outside of a browser? Or can they only be run in the standard web app paradigm (i.e. loaded from a server, and run online in an internet-connected browser)?

These questions may have obvious answers, but I wanted to be crystal clear about them before I start the project.

答:
On further inspection, Bootstrap and Angular both use the MIT License. This is a very permissive license that allows software to be distributed in commercial applications, so long as the license text is included. I see no reason why these libraries couldn't be included in a desktop app.

If I'm wrong, someone please correct me!


答:
I find it to be the most important part.

Unit testing is not always about "seeing if it works right", it's about learning. Once you test something enough it becomes "hardcoded" into your brain and you eventually lower your unit testing time and can find yourself writing entire classes and methods without testing anything until your done.

This is why one of the other answers on this page mention that in a "course" they did 90% testing, because everyone needed to learn the caveats of their objective.

Unit testing is not only a very valuable use of your time as it literally enhances your skills, it is a good way to go over your own code again and find a logical mistake along the way.


答:
I think it is important to distinguish between two types of test strategies: unit testing and integration/acceptance testing.

Although unit testing is necessary in some cases, it is frequently hopelessly over-done. This is exacerbated by meaningless metrics forced on developers, like "100% coverage". http://www.rbcs-us.com/documents/Why-Most-Unit-Testing-is-Waste.pdf provides a compelling argument for this. Consider the following issues with aggressive unit testing:

An abundance of pointless tests that does not document a business value, but simply exist to get closer to that 100% coverage. Where I work, we have to write unit tests for factories that does nothing else but create new instances of classes. It adds no value. Or those lengthy .equals() methods generated by Eclipse - there is no need to test these.

In order to make testing easier, developers would sub-divide complex algorithms into smaller testable units. Sounds like a win, right? Not if you need to have 12 classes open to follow a common code path. In these cases, unit testing can actually decrease code readability. Another problem with this is that if you chop your code into too small pieces, you end up with a magnitude of classes (or pieces of code) that seem to have no justification beyond being a sub-set of another piece of code.

Refactoring of highly coveraged code can be difficult, as you also need to maintain the abundance of unit tests that depend on it working just so. This is exacerbated by behavioural unit tests, where part of your test also verifies the interaction of a class's collaborators (usually mocked out).

Integration/acceptance testing, on the other hand, is a hugely important part of software quality, and in my experience you should spend a significant amount of time on getting them right.

A lot of shops have drunk the TDD kool-aid, but as the link above shows, a number of studies shows that its benefit is inconclusive.


答:
Is it normal to spend as much, if not more,
  time writing tests than actual code?

Yes for writing unit-tests (Test a module in isolation) if the code is highly coupled (legacy, not enough separation of concerns, missing dependency injection, not tdd developed)

Yes for writing integration/acceptance-tests if the logic to be tested is only reachable via gui-code

No for writing integration/acceptance-tests as long as the gui-code and the business-logic is separated (The test does not need to interact with the gui)

No for writing unit-tests if there is seperaton of concerns, dependency injection, code was testdriven developped (tdd)


答:
Yes, it is normal if you're talking about TDDing. When you have automated tests, you secure the desired behaviour of your code. When you write your tests first, you determine whether existing code already has the behaviour you desire.

This means that:

If you write a test that fails, then correcting the code with the simplest thing that works is shorter than writing the test.

If you write a test that passes, then you have no extra code to write, effectively spending more time to write test than code.

(This does not account for code refactoring, which aims at spending less time writing subsequent code. It is balanced by test refactoring, which aims at spending less time writing subsequent tests.)

Yes too if you're talking about writing tests after the fact, then you will be spending more time:

Determining desired behaviour.

Determining ways of testing desired behaviour.

Fulfilling code dependencies to be able to write the tests.

Correcting code for tests that fail.

Than you'll spend actually writing code.

So yes, it is an expected measure.


答:
Can not be generalised.

If I need to implement a formula or algorithm from physically based rendering, it can very well be that I spend 10 hours on paranoid unit tests, as I know the slightest bug or inprecision can lead to bugs that are almost impossible to diagnose, months later.

If I just want to logically group together some lines of code and give it a name, only used in file scope, I may not test it at all (if you insist on writing tests for every single function, without exception, programmers might fall back to writing as few functions as possible).


问:
how to refactor many singletons
I have a medium-sized python program (~5000 lines of code), which I've built up over time, with no particular plan as I went ahead. The architecture I've ended up with consists of 5-6 large Singleton objects, each of which handle a certain aspect of my program, such as database comms, a web-server, data gathering client, and internal calculations.

I feel that using several singletons like this doesn't really make use of the true benefit of OO-programming, namely that it easily allows you to create numerous related objects. My singletons are also fairly dependent on each other, and if I grew the program a lot (say ~50 000 lines), I can see my approach becoming spaghetti code.

So I'm wondering if there is a better way of structuring my program. E.g. should my singletons actually be separate modules? But then how would I approach the attributes that are very neatly organised in my singleton objects? Are there other architecture options?

I'm not very familiar with design patterns (the GOF book is on my todo-list) so perhaps there are design and/or architectural patterns that would be better for my program?

答:
Fear of singletons is greatly exaggerated. If you do need reliably, exactly one of several large constructs, there's nothing wrong with having them. (If you'd like to write static modules instead, and the language doesn't let you, then that may be a problem with the language, not your program.)

A common argument against singletons is that they are hard to mock for unit testing. But that applies only if you hardcode their class in your code. The singleton pattern is really concerned with making sure that only one instance of a specific class exists. No one stops you from having several implementations of a service interface, each of which is a singleton, and swap/inject/dynamically load them as needed for business and testing purposes.

Title: how to refactor many singletons


答:
Singletons are considered 'evil' by many, and while a singleton pattern has its uses they are few and far between... I've worked with several large codebases and have pretty much always managed to move them away from singletons.

The easiest way to eliminate singletons is:

Introduce an interface on top of your singleton: this allows you to separate the contract from the implementation and reduces the coupling you have to the actual implementation

Provide a setter on your singleton so you can set the singleton instance from a unittest: this allows you to 'swap out' the singleton instance in a unittest.

Create unittests on the class you want to change: using a mock of the interface introduced in 1 and the setter introduced in 2 you can now write unittests on the class.

Inject the singleton instead of getting it statically: in the class that depends on your singleton, have a way to inject that singleton (via the interface created in 1). You can use constructor injection, method-based setters.... Whatever works for you. This is the first time you actually touch the class you are refactoring! The tests introduced in 3 will help you refactor without breaking anything

You can avoid step 2 if you want to do it 'all in', but this approach is the most pure in the sense that you avoid changing any classes that do not have tests to check that nothing breaks. Personally, I find this to be a bit overkill but YMMV. The basic premise is that you move towards a class that has dependencies injected instead of fetched statically.

Also, even IF you have legitimate uses for a singleton you should STILL inject them. Singletons are about ensuring that only a single instance of a given class exists. The classic pattern with a globally-accessible static function achieves this, but unfortunately also moves us away from injecting that instance due to bad example code floating about the web. There are plenty of frameworks out there that can handle dependency injection for you and most of them will have a way to configure an object so that the same instance is reused throughout the application, effectively making this a singleton.

Title: how to refactor many singletons

Body:
I have a medium-sized python program (~5000 lines of code), which I've built up over time, with no particular plan as I went ahead. The architecture I've ended up with consists of 5-6 large Singleton objects, each of which handle a certain aspect of my program, such as database comms, a web-server, data gathering client, and internal calculations.

I feel that using several singletons like this doesn't really make use of the true benefit of OO-programming, namely that it easily allows you to create numerous related objects. My singletons are also fairly dependent on each other, and if I grew the program a lot (say ~50 000 lines), I can see my approach becoming spaghetti code.

So I'm wondering if there is a better way of structuring my program. E.g. should my singletons actually be separate modules? But then how would I approach the attributes that are very neatly organised in my singleton objects? Are there other architecture options?

I'm not very familiar with design patterns (the GOF book is on my todo-list) so perhaps there are design and/or architectural patterns that would be better for my program?


问:
Can a pimpl variation be implemented without any performance penalty?
One of the issues of pimpl is the performance penalty of using it (additional memory allocation, non-contiguous data members, additional indirections, etc..). I would like to propose a variation on the pimpl idiom that will avoid these performance penalties at the expense of not getting all the benefits of pimpl. The idea is to leave all private data members in the class itself and move only the private methods to the pimpl class.
The benefit compared to basic pimpl is that the memory remains contiguous (no additional indirection). The benefits compared to not using pimpl at all are:

It hides the private functions.

You can structure it so that all these functions will have internal linkage and allow the compiler to more aggressively optimize it.

So my idea is to make the pimpl inherit from the class itself (sounds a bit crazy I know, but bear with me).
It would look something like this:

In A.h file:

```<code>class A
{
    A();
    void DoSomething();
protected:  //All private stuff have to be protected now
    int mData1;
    int mData2;
//Not even a mention of a PImpl in the header file :)
};
</code>```

In A.cpp file:

```<code>#define PCALL (static_cast<PImpl*>(this))

namespace //anonymous - guarantees internal linkage
{
struct PImpl : public A
{
    static_assert(sizeof(PImpl) == sizeof(A),
                  "Adding data members to PImpl - not allowed!");
    void DoSomething1();
    void DoSomething2();
    //No data members, just functions!
};

void PImpl::DoSomething1()
{
    mData1 = bar(mData2); //No Problem: PImpl sees A's members as it's own
    DoSomething2();
}

void PImpl::DoSomething2()
{
    mData2 = baz();
}

}
A::A(){}

void A::DoSomething()
{
    mData2 = foo();
    PCALL->DoSomething1(); //No additional indirection, everything can be completely inlined
}
</code>```

As far as I see there are absolutely no performance penalties in using this vs no pimpl and some possible performance gains and cleaner header file interface. One disadvantage this has vs standard pimpl is that you can't hide the data members so changes to those data members will still trigger a recompilation of everything that depends on the header file. But the way I see it, it's either get that benefit or the performance benefit of having the members contiguous in memory (or do this hack - "Why Attempt #3 is Deplorable"). Another caveat is that if A is a templated class the syntax gets annoying (you know, you can't use mData1 directly, you need to do this->mData1, and you need to start using the typename and maybe template keywords for dependent types and templated types, etc.). Yet another caveat is that you can no longer use private in the original class, only protected members, so you can't restrict access from any inheriting class, no just the pimpl. I tried but couldn't get around this issue. For example, I tried making the pimpl a friend template class in the hopes of making the friend declaration broad enough to allow me to define the actual pimpl class in an anonymous namespace, but that just doesn't work. If anyone has any idea of how to keep the data members private and still allow an inheriting pimpl class defined in an anonymous namepsace to access those, I'd really like to see it! That would eliminate my main reservation from using this.

I feel though, that these caveats are acceptable for the benefits of what I propose.

I tried looking online for some reference to this "function-only pimpl" idiom but couldn't find anything. I'm really interested in what people think about this. Are there other issues with this or reasons I shouldn't use this?

UPDATE:

I've found this proposal that more or less tries to accomplish exactly what I am, but doing so by changing the standard. I completely agree with that proposal and hope it will make it into the standard (I know nothing of that process so I have no idea of how likely that's to happen). I'd much rather have it possible to do this through a built in language mechanism. The proposal also explains the benefits of what I'm trying to achieve much better than me. It also doesn't have the issue of breaking encapsulation like my suggestion has (private -> protected). Still, until that proposal makes it into the standard (if that ever happens), I think my suggestion makes it possible to get those benefits, with the caveats I listed.

UPDATE2:

One of the answers mentions LTO as a possible alternative to getting some of the benefits (more aggressive optimizations I'm guessing). I'm not really sure exactly what goes on in various compiler optimization passes but I do have a bit of experience with the resulting code (I use gcc). Simply putting the private methods in the original class will force those to have external linkage.

I might be wrong here, but the way I interpret that is that the compile-time optimizer cannot eliminate the function even if all its call instances are completely inlined inside that TU. For some reason even LTO refuses to get rid of the function definition even if it seems that all the call instances in the entire linked binary are all inlined. I found some references stating that it's because the linker doesn't know if you'll somehow still call the function using function pointers (though I don't understand why the linker can't figure out that the address of that method is never taken).

This is not the case if you use my suggestion and put those private methods in a pimpl inside an anonymous namespace. If those get inlined, the functions will NOT appear in (with -O3, that includes -finline-functions) the object file.

The way I understand it, the optimizer, when deciding whether or not to inline a function, takes into account its impact on code size. So, using my suggestion I'm making it slightly "cheaper" for the optimizer to inline those private methods.

答:
You could use std::aligned_storage to declare storage for your pimpl at the interface class.

```<code>class A
{
std::aligned_storage< 128 > _storage;
public:
  A();
};
</code>```

In the implementation you can in-place construct your Pimpl class at _storage:

```<code>class Pimpl
{
  int _some_data;
};

A::A()
{
  ::new(&_storage) Pimpl();
  // accessing Pimpl: *(Pimpl*)(_storage);
}

A::~A()
{
  ((Pimpl*)(_storage))->~Pimpl(); // calls destructor for inline pimpl
}
</code>```

Body:
You could use std::aligned_storage to declare storage for your pimpl at the interface class.

class A
{
std::aligned_storage< 128 > _storage;
public:
  A();
};

In the implementation you can in-place construct your Pimpl class at _storage:

class Pimpl
{
  int _some_data;
};

A::A()
{
  ::new(&_storage) Pimpl();
  // accessing Pimpl: *(Pimpl*)(_storage);
}

A::~A()
{
  ((Pimpl*)(_storage))->~Pimpl(); // calls destructor for inline pimpl
}


问:
Efficiently editing text files
I have been working with some fairly large text files containing about two million lines of text. I don't know the length of the content or the lines in advance, just the number of lines. I have been considering two problems that probably have a similar solution:

How do I change or add to the nth line, without my computer having to shift everything after the nth line in the computer memory.

Say I want to delete all the lines in the text file except for the last 100k, every time the number of lines grows beyond two million. How do I keep these last 100k lines, without having to read the entire text file, deleting everything, and rewriting the last 100k?

I suppose it could be solved by putting everything in some database system. However, as that is no option for me, I want to know how to work with text files efficiently.

答:
How do I change or add to the nth line

You can't.

You have lines of different lengths so you cannot overwrite a chunk of the file, "knowing" it to contain an entire "line".

If your records were all the same length, then you would probably be able tom using direct file access.

How do I keep these last 100k lines, without having to read the entire text file, deleting everything, and rewriting the last 100k?

You can't.

You don't know where each line begins and ends, so you have to read them line-by-line, starting at the beginning.  In this case, the best you can do is to create a new file containing only the last 100k lines and then overwrite the original file with it.

Looking beyond your immediate problem(s), you're now well into the sort of sizes where a database would massively simplify your code and massively improve your application's performance.

Title: Efficiently editing text files


问:
Difference between 3-Tier architecture and MVC (Model, View Controller) in ASP.Net
I would love to know how the 3-Tier architecture differs from MVC (Model, View Controller) in ASP.Net as it seems to me that the same architecture applies.

In 3-tier we have User Services Layer, BusinessLayer and DataAccessLayer, on the other hand we have Model, View, and Controller. This seems same architecture to me.

Can anyone explain if what really differs of the two architecture, how each layer differs to each other?

答:
I know there will be tons of different answers, but I'll give you my view on this.

It's the most famous answer in software engineering "It depends".

Essentially if you look at it, besides various implementation and theoretical differences these are very similar patterns with similar flows.

Where the it depends comes in is the application you're building, a simple webapp may have only an MVC layer talking through an ORM to the DB. A more complex one may have MVC handling the front-end in the User Layer, with more complex non-user exposed operations happening in the BL layer, with the data layer consisting of multiple sources.


问:
Writing a programming api for an existing project?
I've an already existing project in C#. Now I should implement a plugin system. For this plugin system I've to implement a programming api/sdk. I dont want, that plugins have access to the assemblies that are used by my existing programm. This is the reason why I'll write a programming api that limit/specifcy the access to my programm. But how I could do this? Have I just create a new assemblies which references my assemblies from my existing project and expose specific functions with additional help of a facade pattern?
Have someone another idea how I could solve this problem?</p>

答:
Do the plugins have to be dynamically loaded .NET assemblies? If you want to isolate the plugins from accessing your assemblies you might want to consider other plugin architectures:

The plugins can be separate process and use interprocess communication to interact with the main application. That way the plugins will only have access to the API you choose to expose.

C# has various was to do IPC - https://msdn.microsoft.com/en-us/library/windows/desktop/aa365574%28v=vs.85%29.aspx - and there is also WCF which doesn't seem to be menitioned in that article - https://msdn.microsoft.com/en-us/library/ms735119.aspx

You can embed a scripting language inside your main application, and the plugins will be written in that language. One of the most common scripting language used for this purpose is Lua, and the top google result for hosting it in .NET applications is NLua. You'll need to Sandbox the scripts to prevent them from accessing your assemblies. The interaction between the plugins and the main application is done by passing objects to the scripts.


问:
Protection against XSS using a tiny virtual-machine in browser
I'm working on a web framework that makes surveys. It has an expression manager which lets admin users run code on the client as Javascript. There could be a potential XSS problem when there's more than one admin, as is the case for universities. So, instead of compiling expressions to Javascript, what about compiling them to a bitcode and have a tiny VM in the browser to check for XSS? Do you think it could give better protection than just analyzing Javascript?</p>

答:
Building your own "VM" could be a pretty heavy lift.

Just treat any code you didn't write as part of an external application. Execute the script off-page, ideally in a sandbox on the server. (Use a spidermonkey plugin or a node sandbox.) At the very least, execute untrusted scripts in a frame on a totally separate domain.


问:
Multi user website for MVC project: one, or multiple solutions?
We're starting a new MVC 5 project that will ultimately consist of 4 websites depending on the kind of user:

One internal, for company employees.

One for agents, independent contractors that work for the company.

One for external salespeople.

One for customers, which could be any one from their homes.

The internal website will have considerably more functionality than all the others, and since it will be only accessed by employees it will be kept in a private network. The solution is structured like this:

A Data project where the model classes are kept. This has been done using Entity 6 Database First.

A Resources project with strings and images.

A Web project with the MVC stuff: controllers and views, plus CSS/JS, etc.

Given that one website will be internal while the other will be accessed through a public IP, my first thought was having two separate solutions: one for the internal web and one for all the other ones (changing between views based on the logged user), deploying them to a different server. The downside I see is that there would be a ton of duplicate code between the solutions: not only the Data project would have to be very similar or even the same, the resources and the views would have a lot of common stuff.

I'm wondering if it would make more sense to have just one solution with a Data project, a Resources project and then InternalWeb and ExternalWeb projects. Maybe even having three different projects instead of ExternalWeb: AgentsWeb, CustomerWeb, SalesWeb.

I have as little experience bulding MVC applications as I have deploying them, so I would like to know which way is seen as best practice for this kind of situation.

答:
OK, I think you are missing a layer which would make your problem easier to solve

I would have:

Data Layer - EF, sql client whatever

Repository - hides data layer

Service Layer/Business Logic - does the actual work

Authentication Service

Application Layer : MVC website

controllers : call services

views

view models

etc

By splitting your business logic away from both the data and application layer you are free to use it over multiple application layers. Classically this would be a website and a windows forms application, but in your case it could be a public facing and an internal website.

Similarly with the authentication layer, by splitting it out you are able be much more flexible with where you apply your user rights. Checking in the MVC websites controller, before calling a service, or in the service itself, or both.

Overall by reducing the amount of code in your website, you reduce the amount of code duplication required if you need to have more than one version of the Website.

In regards as to whether having multiple websites vs one muti-role website is the best choice. I would say that having a single website is the easiest for users, but having two, with the critical functions behind a firewall is more secure and more easy to demonstrate security


问:
Is it correct to implement all the payment flow in the client rather than in the server?
I’m building a mobile app marketplace and I came to the point of integrating a payment tool. I'm using Mangopay, which has a nice workflow for making payments in a marketplace using its REST api. The question is: is it correct to implement all the workflow and the interactions in the mobile app, or should I implement it in my server?
I would like to listen for the pros and cons of the two approaches, thanks!</p>

答:
I Haven't used mango pay but usualy these things work in three parts:

client redirects user to third party payment app. eg paypal or 3d secure

third party tells client payment has been made

third party tells merchant they have been paid (via an unrelated server)

server completes the transaction, eg. ships the goods, unlocks the download

client checks status with server

So you can see that the third party requires that you have a service somewhere for it to send you the 'money received' message. Normally you give them a url to which they POST a predefined message, xml or json, or they have a website you can log into and check.

You could easily trust the third party message to the client, but this would leave you open to that message being changed or faked. Say I change my network dns to send pay-pal requests to my own server, where I fake a 'successful payment' return message.


答:
It's hard to give a firm answer without knowing just how much functionality is going to be shared between each website.

However, you always want to avoid duplication as much as possible.

Ewan's suggestion is worth following: create a separate data layer that handles all the database access and business logic. This is always worth doing, regardless of how your front end is going to be designed.

Next, I would look at creating a single front end, and implement user permissions - add checks to see if the logged in user is an agent, salesperson or customer, then adjust what information is displayed depending on this.

you can always split up pages into multiple files, and include each one as needed depending on the user level. e.g for a Create Customer form, do the following:
   - CreateCustomer.cshtml - contains all generic fields available to all users
   - CreateCustomer_agent.cshtml - contains additional fields for agents
   - CreateCustomer_sales.cshtml - contains additional fields for salespeople

Don't forget you can also change the _Layout.cshtml used on each page, so you can pragmatically change the _Layout.cshtml file used, thereby changing the menu used, or even the whole look and feel of the application.

Title: Multi user website for MVC project: one, or multiple solutions?

Body:
We're starting a new MVC 5 project that will ultimately consist of 4 websites depending on the kind of user:

One internal, for company employees.

One for agents, independent contractors that work for the company.

One for external salespeople.

One for customers, which could be any one from their homes.

The internal website will have considerably more functionality than all the others, and since it will be only accessed by employees it will be kept in a private network. The solution is structured like this:

A Data project where the model classes are kept. This has been done using Entity 6 Database First.

A Resources project with strings and images.

A Web project with the MVC stuff: controllers and views, plus CSS/JS, etc.

Given that one website will be internal while the other will be accessed through a public IP, my first thought was having two separate solutions: one for the internal web and one for all the other ones (changing between views based on the logged user), deploying them to a different server. The downside I see is that there would be a ton of duplicate code between the solutions: not only the Data project would have to be very similar or even the same, the resources and the views would have a lot of common stuff.

I'm wondering if it would make more sense to have just one solution with a Data project, a Resources project and then InternalWeb and ExternalWeb projects. Maybe even having three different projects instead of ExternalWeb: AgentsWeb, CustomerWeb, SalesWeb.

I have as little experience bulding MVC applications as I have deploying them, so I would like to know which way is seen as best practice for this kind of situation.


问:
Where is the appropriate place to put application configuration files for each popular operating system?
Before writing this question, I'd like to state that I use Linux on a daily basis, which is why I'm creating this post to extend my knowledge of the other main operating systems I'm concerned about, Mac OS and Windows.

I'm developing a NodeJS application that needs to read a configuration in order to understand what to do, and allow the user to customize some of the inner workings of the application through this configuration.

However, this application is also cross-platform.  And I know that operating systems have different file structures for their own convenience.
On a lot of Linux operating systems (if not all) I know that configurations are most commonly put in ~/.config/<app> or likely ~/.<app>/config if the first is not applicable...

Although, my knowledge does not expand far enough to know if that's the same for Windows and Mac OS?

What is considered the best place to put application configurations on Linux, Mac OS, and Windows?

答:
Windows uses the %APPDATA% directory for user specific application configuration files. The %PROGRAMDATA% folder is used for system wide application configuration files, though it appears that Windows Store apps may not be able to use %PROGRAMDATA%.

The registry is a hierarchical database included as part of Windows that can also store some config data as key-value pairs.

For Mac OS X, the ~/Library folder is usually used, with /Library for global configs.

Usually these are nested further, so your preferences would go in ~/Library/Preferences/ and some other files would go in ~/Library/Application Support

Note that in all of the above cases, you'll probably want to put your configurations under your app's name within the base location.


答:
No, they are not the same.

MVC is a design pattern for structuring user interface code. It could be used in a three-tier architecture, in which case the pattern would belongs in the user services layer. But it can also be used for the UI in an applications which is not three-tier - e.g. a calculator with no underlying persistence, and therefore no data access layer.

In a three-tier architecture with a MVC frontend, the domain objects used as the model would be the objects from the business layer, but the MVC pattern does not really specify what kind of objects the model is, only what their role in the pattern is. For example in the MVVM variant, the models are UI-specifc adapters on top of the domain objects. In this case even the model belongs in the user service layer.

Title: Difference between 3-Tier architecture and MVC (Model, View Controller) in ASP.Net

Body:
I would love to know how the 3-Tier architecture differs from MVC (Model, View Controller) in ASP.Net as it seems to me that the same architecture applies.

In 3-tier we have User Services Layer, BusinessLayer and DataAccessLayer, on the other hand we have Model, View, and Controller. This seems same architecture to me.

Can anyone explain if what really differs of the two architecture, how each layer differs to each other?


问:
What is the best PHP ORM library?
I want to build a PHP web application, but always faces the problem that I need to connect to database and deal with related objects. So I tried codeigniter and it uses a special way to connect to database named Active Record and I've known that its an ORM technique and there are lot of ORM libraries out there.

What is the best, the easiest and the fastest of them?

答:
I really have to sing praise for PHP.ActiveRecord.  It's nearly a direct clone from Rails' ActiveRecord ORM.  It has been nothing but awesome for me an is relatively painless to integrate into modern frameworks.

Both developers are regularly available on their support forums to give help to those in need.

It's a brilliant project and I highly recommend it - especially if you're from a Rails background.

Title: What is the best PHP ORM library?

ContentLicense: CC BY-SA 2.5


答:
This is like asking what the difference is between an apple and an apple core. These two architectures aren't replacements for one another. I think a more accurate view is that the 3-tier architecture augments MVC.

The MVC Architecture

Models: These represent "stuff" in your application. This layer has gotten a little fuzzy in recent years, as I will explain later.

Views: The user interface. The thing the user interacts with.

Controllers: The programming code that responds to the user and to changes in the model layer

The 3-Tier Architecture

With the 3-tier architecture, you have layers with different responsibilities.

User Services: (or "services" in general) This layer is more about coordinating the retrieval and modifications of the "model" layer. Complex, multi-step actions get performed here

Business Layer: This represents the business rules etched into programming code. What "The Business" wants is enforced in this layer.

Data Access Layer: One or more classes responsible for accessing a persistent data store.

The only part of the 3-tier architecture that intersects with MVC is the "Business Layer". The "Models" in MVC and the "Business Layer" in 3-tier architecture are trying to achieve the same goal.

The "M" in MVC has gotten fuzzy

The "model" layer in MVC has expanded in recent years. From what I've seen, there are two, possibly three kinds of models:

Domain Models: These represent the "things" that "The Business" cares about -- the Business Domain. These classes hold data and all procedures that operate on that data in order to enforce business rules. Frequently Domain Models are tied to tables in a database. This seems to fit the "Business Layer" of the 3-tier architecture.

View Models: These are classes used to massage the data from the domain models into something more palatable to the view. This doesn't fit anywhere in the 3-tier architecture because view models do not implement business logic, nor do they provide any sort of service or data access.

Business Models: In complex applications, the need to decouple the Domain Model from the Business Logic arises. The Business Models contain data and procedures operating on that data to implement business rules, and the Domain Models are relegated to "Property Bags" -- objects that just hold data but contain no behavior. Domain Models become another form of Data Transfer Object between the database and the application.

Nowhere in MVC is data access mentioned. In some cases, you'll see that data access belongs in the "model" layer of MVC, which as we've seen is not a clear cut layer anymore. Really I see 3-tier architecture being paired with MVC to create a whole application. One augments or improves upon the other:

Models

Domain Models (MVC/3-tier)

View Models (MVC)

(optionally) Business Models (MVC/3-tier)

Views (MVC)

Controllers (MVC)

Data Access (3-tier)

Services (3-tier)

There is some intersection, but they are largely separate, and together are used to decouple and isolate various components of a larger system.


问:
Exception to the 'Nested types should not be visible' rule?
Is the following an acceptable breach of the 'Nested types should not be visible' (Microsoft .NET code analysis CA1034) rule?

I have a have a class, let's call it 'TandAOLL', this is tree and an array of linked lists all in one, this is a QuadTree implementation which allows for grouping of points.

Inside this class I have another class, let's call this one 'TandAOLLGroup', this is the type that the iterator of 'TandAOLL' returns, this is therefore declared Public.

This class contains a reference to the tree from which it was created and also an integer representing the index of the group the object represents.

The iterator of 'TandAOLLGroup' returns Points by following a linked list trail using the leaf stored in the TandAOLL.Array(Index) as the head.

The reason I have nested the second class inside the first is because it needs priviliged access to the private members of the 'TLLOLL' class.

答: You can use a public interface (e.g. ITandAOLLGroup) which the iterator can return.  The implementation TandAOLLGroup can remain a private nested class or whatever the is the best option for your design.


问:
How is one supposed to deal with the intermediate buffer of DataReader class?
Summary

I am developing a WAV file format reader under WinRT, and for this I need to read random amounts of structs consisting of fundamental types such as int, uint, float and so on.

Back in desktop development one would rely on BinaryReader, now in WinRT it has been replaced by DataReader which works asynchronously.

Problem

I cannot grasp how to use this new class since now, an intermediate buffer must be filled using LoadAsync(), prior calling reading methods such as ReadInt32().

In contrast, with the old BinaryReader there was no notion of having to fill an intermediate buffer prior reading primitives from the source.

Every example I have seen on the web are 'naive' in the sense that they entirely read the source stream in memory, but in my case a WAV file is in the range of hundred megabytes and possibly gigabytes.

I have sketched the following helper methods which pre-fills the intermediate buffer with only what's needed and basically frees me from systematically calling LoadAsync every time before reading something from the stream:

```<code>internal static class DataReaderExtensions
{
    public static async Task<string> ReadStringAsync(this DataReader reader, uint length)
    {
        await LoadAsync(reader, length);
        return reader.ReadString(length);
    }

    private static async Task LoadAsync(DataReader reader, uint length)
    {
        var u = await reader.LoadAsync(length);
        if (u < length) throw new InvalidOperationException();
    }
}
</code>```

But I'm not entirely sure whether it is the way to go when using DataReader.

Question

How is one supposed to pre-fill the intermediate buffer in my case ?

should one load only the needed amount as shown above ?

or should one load a constant size (e.g. 65536 bytes), keep track of reading position then possibly pre-fetch more on larger requests ? (basically wrapping a DataReader in a helper class)

EDIT

By looking at BinaryReader source code there doesn't seem to be any kind of magic behind the scene, i.e. bytes are fetched on demand. So for my case, even if it sounds a bit silly to read primitives asynchronously, I guess it's the simplest and safest way to do it; in contrast to wrapping a DataReader, tracking read position, handling an intermediate buffer and finally, the inability to derive from it as public WinRT types must be sealed ... not sure it is worth it for the outcome.

Unfortunately WINMD assemblies sources are unavailable, it would have been pretty interesting to see how they do it at Microsoft as these newer types can be used as older types, with these extension methods.

答:
should one load only the needed amount as shown above ?

You should load into the buffer all that you can feasibly expect to process with the code that follows. In the DataReader documentation example, they read the entire stream into the buffer, because they are going to process it all immediately.

The reason for the buffer is that IO is slow (usually). So the amount of data you specify is loaded with asynchronous IO into the memory buffer up front. Then you can subsequently read it without waiting for IO on every read. That's a good thing for performance. IO is batched which will improve perf on many devices (e.g. mechanical hard drives). Your code's execution is suspended (due to async/await) until IO is finished, so it isn't tying up CPU cycles.

or should one load a constant size (e.g. 65536 bytes), keep track of reading position then possibly pre-fetch more on larger requests ? (basically wrapping a DataReader in a helper class)

Sometimes the size of the data will be too large to load in memory all at once. .NET itself sets a memory limit of 2GB per object (well, sortof). So if the data you are reading is close to 2GB, then you will definitely want to keep track of stream's read position, and read only part of the file into the buffer. Then once you get to the end of the buffer, fill it back up again from the next read position and continue processing. Repeating as necessary until you've processed the whole file.


问:
sessions and stateless rest
I believe this question has been discussed to death.

I was reading up on REST and it says that it has to be stateless.

Yes i do understand there are application states and resource states.

Taking for example a normal web service,

Say if i were to log in from PC A and perform some transactions without logging out.

I log in from PC B and perform another set of transactions. However, at this point it seems that the web server is able to know which set of transactions i performed previously. Does it mean that the server stores all my sessions details?

For some services they do even bring you back to the page that you were previously looking at on PC A. How is this possible if it is supposed to be stateless?

答:
Being stateless does not mean that NO information is present on the server, it just means that the request performed by the client contains all the needed information for the server to handle the request.

When you log on to a server, that server can send you a session token so it can persist information across requests. All the server needs is for you to send the token back to the server on every subsequent request and it can associate the token with the information that was persisted. This is indeed the way the HTTP-part of the web works.

Compare this to what FTP does: when you connect, the server sets certain variables for you. For example, it 'knows' what your current working directory is and what file transfer mode (ASCII, Binary...) you will use. Uploading a file to an FTP server does not involve you sending the file, along with what the current directory should be, what the transfer mode is, etc... The server 'knows' this because this information is associated with your session.

As for your changing a PC: when you log into a website, it can log what page you are on as you visit them and persist this to a database. If you explicitly log out this information is removed from the database. As long as you don't do that, logging into the website from another client simply puts you back onto the last page you were: the application identifies you by your username, fetches the last page from the database and shows it to you.

Title: sessions and stateless rest


问:
Finding methods that return null instead of a collection (in Java)
I'm looking for a good tool or technique to search a Java code base and find every method that says it returns a collection (List, Set, Map, etc.) but sometimes returns null.  This would be a cool IntelliJ IDEA inspection and could work without annotating @NotNull.

答:
This involves a bit of work with pmd, but I believe that this is a valid case for writing a custom rule.

The way that custom rules in pmd work are to "compile" the Java code into the xml version of the abstract symbol tree and then run xpath queries against it.

To get an idea of the structure, the non-tag xml looks something like:

```<code>class Example {
 void bar() {
  while (baz)
   buz.doSomething();
 }
}
</code>```

gets 'compiled' to:

CompilationUnit
 TypeDeclaration
  ClassDeclaration:(package private)
   UnmodifiedClassDeclaration(Example)
    ClassBody
     ClassBodyDeclaration
      MethodDeclaration:(package private)
       ResultType
       MethodDeclarator(bar)
        FormalParameters
       Block
        BlockStatement
         Statement
          WhileStatement
           Expression
            PrimaryExpression
             PrimaryPrefix
              Name:baz
           Statement
            StatementExpression:null
             PrimaryExpression
              PrimaryPrefix
               Name:buz.doSomething
              PrimarySuffix
               Arguments
```

For example: //WhileStatement[not(Statement/Block)] will identify while statements that are not followed by a statement or a block with statements - an empty block.

While it would take a bit more digging into the specifics, it should be possible to write the appropriate xpath query to identify a method deceleration where the type is something and that it has a return statement nested under it that returns a null.  Writing one that flags any return null; should be trivial (a starting point to refine for the rule).  I have written a pmd rule in the past (don't have it with me anymore) that identified static SimpleDateFormat ... decelerations which could cause problems as SimpleDateFormat isn't thread safe).

If the xpath isn't powerful enough to identify the specific rule, its possible to write a class that gets plugged into pmd that will get the AST and any transformations can be done on that to identify problems. This is a bit more difficult to plug into pmd though (compared to a xpath rule).

Body:
This involves a bit of work with pmd, but I believe that this is a valid case for writing a custom rule.

The way that custom rules in pmd work are to "compile" the Java code into the xml version of the abstract symbol tree and then run xpath queries against it.

To get an idea of the structure, the non-tag xml looks something like:

class Example {
 void bar() {
  while (baz)
   buz.doSomething();
 }
}

gets 'compiled' to:

CompilationUnit
 TypeDeclaration
  ClassDeclaration:(package private)
   UnmodifiedClassDeclaration(Example)
    ClassBody
     ClassBodyDeclaration
      MethodDeclaration:(package private)
       ResultType
       MethodDeclarator(bar)
        FormalParameters
       Block
        BlockStatement
         Statement
          WhileStatement
           Expression
            PrimaryExpression
             PrimaryPrefix
              Name:baz
           Statement
            StatementExpression:null
             PrimaryExpression
              PrimaryPrefix
               Name:buz.doSomething
              PrimarySuffix
               Arguments

For example: //WhileStatement[not(Statement/Block)] will identify while statements that are not followed by a statement or a block with statements - an empty block.

While it would take a bit more digging into the specifics, it should be possible to write the appropriate xpath query to identify a method deceleration where the type is something and that it has a return statement nested under it that returns a null.  Writing one that flags any return null; should be trivial (a starting point to refine for the rule).  I have written a pmd rule in the past (don't have it with me anymore) that identified static SimpleDateFormat ... decelerations which could cause problems as SimpleDateFormat isn't thread safe).

If the xpath isn't powerful enough to identify the specific rule, its possible to write a class that gets plugged into pmd that will get the AST and any transformations can be done on that to identify problems. This is a bit more difficult to plug into pmd though (compared to a xpath rule).


问:
Interface Design: Specific vs General parameter (A Minimal design vs anticipated use variation)

Code

public interface IVehicle {
   string VehicleMake { get; }
   int MonthsSincePurchase { get; }
   bool IsApprovedUsed { get; }
   ...
}

public class WarrantyPopUpHandler {
   virtual bool ShouldShowPopup(IVehicle vehicle /* What should I pass in?*/) {
      return false;
   }
}

public class VolvoWarrantyPopupHandler : WarrantyPopUpHandler {
   override bool ShouldShowPopup(IVehicle vehicle) {
      return vehicle.MonthsSincePurchase < 12;
   }
}

public class BMWWarrantyPopupHandler : WarrantyPopUpHandler {
   override bool ShouldShowPopup(IVehicle vehicle) {
      return vehicle.MonthsSincePurchase < 12;
   }
}
```

<h2>Explanation</h2>

<p>Looking at the above pseudo-code there is a <code>IVehicle</code> type that exposes lots of properties of a vehicle. Now some customers want to show a popup for some vehicles and the programmer starts designing a <code>WarrantyPopupHandler</code>. Currently Volvo and BMW want the popup, and they both want it based on the vehicles age. The programmer implements the two derived classes as shown.</p>

<p>What should the <code>ShouldShowPopup()</code> method take as parameter. The only thing it needs from the current use cases is the <code>int MonthsSincePurchase</code>. Is it at present premature to supply the entire IVehicle, should it only take an <code>int</code>?</p>

<p>But there is a very good chance that during the demo another customer, say Ford, for example, will catch the idea and will want the same popup but maybe only for ApprovedUsed (<code>IsApprovedUsed</code>) vehicles in the future. The programmer knows this by experience.</p>

<p>Also there will be lots of other code written that will rely on calling <code>WarrantyPopUpHandler::ShouldShowPopup</code>.</p>

<p>Also note that the base class version ignores the incoming parameter and returns false all the time, so here it seems wasteful to supply a parameter at all.</p>

<h2>Bonus question</h2>

<p>Does the answer change if IVehicle is part of a larger interface.</p>

<pre class="lang-cs prettyprint-override"><code>interface VehicleInvoice
{
    IVehicle Vehicle { get; }
    ICustomer Customer { get; }
    ...
}
</code>```

Should the programmer have rather supplied VehicleInvoice to ShouldShowPopup given that customer data may (a rare case by experience) also factor in the decision to show a popup in the future?

See Also

The question is a variation of my earlier question: Minimal design vs anticipated use.
How should the answers there influence the design choice here?

Polite Note

I would like answers to try and focus on or discuss what design principles apply here and not treat this as a refactoring exercise that avoids the question being asked. Thanks.

答:
Making the parameter an IVehicle is better. You're essentially using your WarrantyPopupHandler hierarchy to add a method with a default implementation to IVehicle. Making the parameter an int wouldn't make sense because ShouldShowPopup is assuming a certain "meaning" for the int (ie. that the int holds IVehicle.MonthsSincePurchase). Since it would be a logic error to call ShouldShowPopup with an int that means something else, the parameter might as well be an IVehicle. Using an IVehicle also improves encapsulation since the caller of ShouldShowPopup doesn't have to examine the internal structure of IVehicle.

As for the VehicleInvoice interface, that comes down to domain knowledge. It seems to me that a warranty is associated with a particular transaction, which seems to be what VehicleInvoice represents. So in the particular case of your "bonus" question, I'd be inclined to make it ShouldShowPopup(VehicleInvoice). Obviously if you carry this thinking too far you end up with global variables, so you have to use your best judgment.


问:
Creating menu structure using DB - is that fine?
I have an application that basically consists of a lot of screen so user needs to navigate a lot (like go from A>B>C>D) etc.

I have been thinking how to implement this (mainly hold the structure of the menu) as it needs to be on-screen menu (custom, no menu strips etc.) and did not find any solution.

Now I realized I could use a database for that, which would allow me also to change strings later with no impact.

My idea would be to have a table for each level (1-5) with options available. Once the user selects an option, the program will select any items in the table (CurrentLevel+1) based on its ID. Seems really a good solution to me but I am not a programmer (rather than an analyst).

Or even I could use specific table names and hold the table name in a specific column, for example:

User selects Settings

Program checks that with Settings, there is a table associated "SETTINGS" and query this table for all items available

Does this make sense?

答:
Generally treating menu items as data is a great idea. And, if the end users are managing the menuing like in a CMS then you can make a great argument for storing the menuing in the database. But if end users aren't dealing with it I would strongly consider dealing with it as data but in code rather than in an external store.

The basic mechanic here -- and I won't get specific as we don't have a platform indication -- would be to define a data structure for menu items according to requirements and then statically define the menu data as a global variable of an appropriate sort. This typically isn't horribly difficult to edit with modern development environments and you should have some continuious delivery worked out by now to get it in the client's hands quickly. You also get tracability of changes from source control.

That is all nice but the biggest advantage imho is that you don't have to pay the database "tax" if you will -- especially if the data is largely statically defined by developers anyhow. The tax comes in working out how to refresh the database with the right version of the data. It comes in having to have a database running to kick tires and test things. It comes in the defensive code you write because sue from accounting who has access to the DB server can now change things in the menu in unpredictable ways.

There is a lot less overhead and a lot less involved in just using an object graph over storing it in the DB. Plus, if you ever need to store it in the DB (or another external source such as a config file) you only need to change how it is loading into the object graph -- the client code remains the same.


问:
How do you stick to an ABI/API in a commercial library?
How does a commercial library vendor manage to stick to the same ABI over years of development (or is this a bad assumption on my part)?

I would have thought that over time a lot of functions and class interfaces would be completely rewritten. Do they just have the foresight to know what footprint to use from the get-go?

Is it a bad idea to not change the ABI? Like you end up with a horrid ugly mess if you don't; which is more important, ABI staying the same, or the API making sense for a newbie?

EDIT: I may be getting ABI and API confused. As I understand it, changing the API would change the ABI?

答:
ABI and API are different, but related things. An API is a contract that describes functionality. The ABI describes how modules communicate at the machine code level. The two can change independently, and one doesn't (or shouldn't) affect the other.

The ABI describes things like how parameters are passed to functions (on the stack, in registers, etc.), how system calls are made, how shared data is accessed, etc. An ABI is machine specific, operating system specific, often language specific, and also application specific.

An API, on the other hand, is often machine, operating system, and language agnostic.

There are large parts of the ABI that you don't often have to think about, because your language compiler and linker handle them for you. You might have to worry about it in some cases, for example the difference between cdecl and stdcall in C and C++. Staying with the same ABI for years is pretty easy for these parts. For example, code written for Win32 in 1995 will still work today. Even libraries that were compiled 20 years ago can be linked with 32-bit code that was compiled today.

Whatever API you're using should be steady across versions. Vendors make a concerted effort to keep the APIs backward compatible so that code written to use version 1 of the library can still link with version 5. Or 17. Granted, the API might be extended, but functions that were available in the first version usually exist and work the same many versions later. The internal implementation might be (very often is) different, but as long as the functions meet the initial contract, it doesn't matter how they're implemented.

Now, where you can get into trouble is if you change a data structure that's passed to or returned by an API function. As was pointed out in the comments, a change to a data structure that doesn't require a source code change, is considered an ABI change. That could be something as simple as changing structure packing, or rearranging the order of the individual structure members. Not to mention adding a field. In any of those cases, the size of the data structure could change, but the new version might very well link with an object module that was compiled for the older version. That would definitely break things.

Those are breaking changes that, although they don't require any source code changes, do require a recompile. And in my opinion if it requires a recompile, then it should be considered an API change.


问:
Long running DB query with concurrent write at the same time
We have a big table with a lot of data and a select query which takes 3 seconds to run.

However we are a highly concurrent environment and each second we get 100 new records in our database.

So let's say we have this query, and before that query starts, we have 1000 items which satisfy those query. The query takes 3 seconds, and each second there are 50 new items that match those query added to the DB.

My question is: What result will this query return to me (is it 1000, 1150 or somewhere in between), and how does this vary depending on the DB engine (SQL, NoSQL).

It's not a question about the exact number, more - why it would actually be that number.

答:
i have ms-sql server experience with reporting queries on a (realtime replica) database receiving hundreds of rows / second.

The short answer is always going to be "it depends"; the answer can be anything between 1000 and 1150 (or even 990 to 1160 if you run with "Read Uncommitted" as mentioned by Robbie Dee)

in sql server a table scan,by default,  locks pages as it reads, ensuring that re-use of the same row doesn't give a silly-inconsistent answer. Writes which happen after you start the query, but involving a row/data page you have not yet read will be visable to you.

Adjusting the isolation level can change this in 2 ways:

1) with 'snapshot' isolation, you will not see writes which are requested after you start the query. (what happens to the write depends on if you have a row versioning database, such as oracle, or sql-server with snapshot isolation enabled)

2) when you use a more permissive "allow dirty reads" Read Uncommited (or a table 'with (nolock)' ) then what happens is undefined. in particuar 3 extra scenarios can occur when reading a full page that is about to be split:

a) you are at the end of a page, and the new page is ahead of the table scan; you will read all the rows copied to the new page twice. (potential primary key violation in the returned results, or if this is an update, the same row returned twice with different data.)

b) you are at the beginning of the page, and the new page is behind of the current table scan; the rows which are moved from the current page to the new one will not be read, resulting in matching rows not being returned.

c) your read query isn't a table scan, but utilizes an index, which holds row id's: the change in rowID caused by the page split may result in the row changing and results which don't' match your query being returned.

note: in sql server almost any modification (including deleting rows) can cause a page split.


问:
Why shouldn't I use the repository pattern with Entity Framework?
During a job interview, I was asked to explain why the repository pattern isn't a good pattern to work with ORMs like Entity Framework. Why is this the case?</p>

答:
After trying out repository pattern on small project I strongly advise not to use it; not because it complicates your system, and not because mocking data is nightmare, but because your testing becomes useless!!

Mocking data allows you add details without headers, add records that violate database constraints, and remove entities that the database would refuse to remove. In the real world a single update may affect multiple tables, logs, history, summaries, etc., as well as columns such as the last-modified-date field, auto generated keys, computed fields.

In short running your test on real database gives you real results and you can test not only your services and interfaces but also database behavior. You can check if your stored procedures do the right thing with data, return the expected result, or that the record you sent to delete really deleted! Such tests can also expose issues such as forgetting to raise errors from stored procedure, and thousands of such scenarios.

I think entity framework implements repository pattern better than any of the articles I have read so far and it goes far beyond what they are trying to accomplish.

Repository was best practice on those days where we were using  XBase, AdoX and Ado.Net, but with entity!! (Repository over repository)

Lastly I think too many people invest lots of time on learning and implementing repository pattern and they refuse to let it go. Mostly to prove to themselves that they did not waste their time.

OwnerDisplayName: StringBuilder


问:
Why are source control systems still mostly backed with files?
It seems that more source control systems still use files as the means of storing the version data.  Vault and TFS use Sql Server as their data store, which I would think would be better for data consistency as well as speed.

So why is it that SVN, I believe GIT, CVS, etc still use the file system as essentially a database,  (I ask this question as we had our SVN server just corrupt itself during a normal commit) instead of using actual database software (MSSQL, Oracle, Postgre, etc)?

EDIT:  I think another way of asking my question is "why do VCS developers roll their own structured data storage system instead of using an exisiting one?"

答:
Fossil is an excellent Distributed Version Control System (DVCS) and uses SQLite for storage, no plain-text files.

I really like that it has integrated: bug tracking, Wiki and that it is really distributed. I mean you can really work offline and fix bugs.

Fossil uses Sqlite as it's application file format.
In the keynote at PgCon Dr. Richard Hipp explains what are the advantages of using sqlite as an Application File System, and makes a pretty convincing argument of the benefits of using a database as filesystem.

The second main topic was that SQLite should be seen as an application file format—an alternative to inventing own file formats or using ZIPped XMLs. The statement “SQLite is not a replacement for PostgreSQL. SQLite is a replacement for fopen()” nails that (slide 21). Finally, Richard put a lot of emphasis on that fact that SQLite takes care of your data (crash safe, ACID) use-the-index.com

Now Dr. Hipp has addressed the concerns on saving code on a database

Why is Fossil based on SQLite instead of a distributed NoSQL database?

Fossil is not based on SQLite. The current implementation of Fossil uses SQLite as a local store for the content of the distributed database and as a cache for meta-information about the distributed database that is precomputed for quick and easy presentation. But the use of SQLite in this role is an implementation detail and is not fundamental to the design. Some future version of Fossil might do away with SQLite and substitute a pile-of-files or a key/value database in place of SQLite. (Actually, that is very unlikely to happen since SQLite works amazingly well in its current role, but the point is that omitting SQLite from Fossil is a theoretical possibility.)

ContentLicense: CC BY-SA 4.0


问:
Developer, QA product management accountability using public shaming?
So our scrum team was talking this morning about some mega screw ups that one team continues to make but could apply in any situation and how we could bring transparency and accountability for the entire department. We have around 80 people including documentation, QA, development, product management, technical operations so teams are very isolated but can easily be the victim of another person or other team that breaks a build, doesn't look for dependencies and breaks your code, etc.

I was deemed abnormal, no disagreement, because I said that if I messed something up and caused others pain then I want to be called out because how will I know and learn if I don't get told and am accountable to a group to do better. I said we should have a public shaming where offenders get called out to explain their side of the problem and give them a chance to own it and say what they will do about the problem. I love this idea because often people are so uncomfortable with confrontation that even when there is a specific person in mind, they will feel like they have to use generic descriptions or try to hide behind anonymity meanwhile the person who is really the problem may be in denial, or may not realize they are the reason for the discussion and bad behavior goes unchanged and no one is accountable. It's not about being rude or hurting feelings, it's about everyone trying to do what is best to produce the best software for your clients.

This team that causes everyone else problems, always, is somewhat protected by their manager who really is feeding them the terrible plans to implement "because this worked at my last job" so even though small discussions are had, it quickly is swept aside and passed off as someone else's problem. Like I said though, it could be any somewhat serious or continuing problem. Everyone outside of their team spends so much time complaining about how terrible they are and it has really killed morale. The biggest complaint I have found against public shaming in the programming world is that it hurts morale but isn't it better to curb the problem, address it, and be accountable when the problem occurs and deal with hurt feelings than to let stuff fester and continue to grow as more problems are caused with no avenue to drive ownership?

If I am called out for a bug that caused 200 calls from angry customers, should I not be asked to explain my understanding of what I did wrong and how I am going to fix it and finally, what I am going to do to make sure it doesn't happen again? There might be someone in that group of 80 that says "I had that same problem 5 years ago and here is what I did to fix the code and learning X area in Y framework really helped me understand what I did wrong and the right way to do it" but without an audience of brilliant minds who have all screwed up, you have to handle it on your own. The comradery that I have with the person who wasn't involved but could help me was created which can be very positive.

I'm curious about if any of you have had this type of process at your work and if you liked it, hated it, etc. and ultimately if it seemed to be effective in solving problems.

答:
The answer @gnat pointed to considers public shaming as "amateurish". I'd go a step further and even call it harmful.

There are a million things to be said about your situation and way more ways for me to guess what really is going wrong there, however, there is one fundamental aspect that public shaming has: In a scenario in which you can point to single people for blaming, no one is safe any longer.

This may sound simple, but the impacts are enormous. Some people may straight out leave (and I'd completely understand that!), but with a guarantee everyone will try his or her hardest to not be blamed. And do not fall into the trap here: not getting blamed isn't the same thing as producing quality software - not at all!

Public shaming was used in medieval times as a means to ensure conformism. As we have seen time and time again, conformism is not something to strive for in the 21st century. It hinders your innovation (you have to be able to make mistakes for that - and getting blamed stands in the way of that), it doesn't help you to set yourself apart from the competition, etc.

Finally, you should think carefully about the work climate you are producing. In a climate of fear of being blamed, you will not get a productive environment. I have been in companies where there wasn't even a public notion of shaming, but management did not address that shaming was happening on a daily basis. None of these companies ever got anything decent done and working there is bizarre to say the least. Everything you do, you only do after you have ensured there is someone else to blame it to!

In essence: Public shaming leads to a climate in which the employees of the company are up against each other. This is a recipe for failure considering that everyone in the company should strive for the same goals and work together to achieve them.


问:
How to avoid business rules in repository?
If there is Employees repository and I have to get Employee that has a greatest salary - where to put filtering that represents business rule for that?
I can get all Employees and filter them out in Domain Model, but that does not seem as efficient as SQL Query in repository. What patterns come into play?</p>

答: The business logic is not the query that returns the Employee with the greatest salary -- databases are great at that and you should let the database do that for you via the repository. The business logic is what you do after you retrieve the most highly compensated employee.</p>


问:
When designing input interfaces for a library, when to use data class rather than data interface?
Let's say library A, B and C are all custom math libraries. They may or may not use the same data types. The libraries have methods that accept data type inputs like ProcessAsync(DataTypeA) or ClusterByFeature(IDataTypeB, Func<IDataTypeB, double>).

These libraries will be developed for modularity. E.g. Future graduate students can come and use the library for their needs.

I thought that these libraries should:

either provide concrete implementations of the inputs they accept and manipulate on, like DataTypeA, DataTypeB, DataTypeC

or, request that the users supply an object of type IDataTypeA, IDataTypeB, IDataTypeC

Which design I should choose seems to be dependent on many factors.

I've come up with a few:

If it's just a container, it should be an interface

If DataTypeA, DataTypeB, DataTypeC are all very similar, use interfaces -  let the invoking framework supply the implementation by inheriting all the interfaces

If DataTypeA requires complicated operations, use classes

If DataTypeA requires simple operations, use interfaces and a DataTypeAManipulator class

I see other open-source libraries overwhelmingly use classes instead of interfaces, so I think classes should be the way to go. But is there no place for data interfaces then?

--

A better example of the problem that I faced:
There's a 3D object that needs to be manipulated on. Library A handles vertices, Library B handles vertices and deformation by bone joints, Library C handles vertices and texture mapping. 3 different data objects - they're actually different views of a single data object, but the libraries don't/shouldn't know that! So should I use data interfaces to show different perspectives of a 3D object, or copy values to data classes instead.

答:
Data classes make sense when they are used as a container for data (a POCO in .NET or POJO in Java). They contain no behaviour and simply serve to encapsulate data and simplify code, eg:

int CalculateSurface(int x1, int y1, int x2, int y2)

vs

int CalculateSurface(Rect area)

When you pass a data class, you could actually write the method so as to take the arguments separately. You obviously don't WANT to do that because it seriously clutters up your code, but you could.

A data interface makes sense when you actually need logic or behaviour, which means methods on the passed object need to be implemented. It also makes sense when you want the object you are working on to perform some operation and you need to pass it a parameter, in which case you need a contract for this method, hence an interface.


答:
You need to two layers to perform this. One will be the DAO layer and one will be the Service layer. All your business logic have to be defined(or injected from external source) in Service layer. All the methods that you need to retrieve data either from database or some other source should be defined in DAO layer. Service layer will call the DAO layer and apply some rules on top of it and return the result. This will help you complete decoupling of your rules and persistence.

But in your case, executing a direct SQL query with employees having greater salary will have high performance than following Service-DAO pattern.In this case, you can modify your business logic in service layer to pick the appropriate query string in Service layer and pass the query(or where condition of the query) to DAO and get the result.

Title: How to avoid business rules in repository?

Body:
If there is Employees repository and I have to get Employee that has a greatest salary - where to put filtering that represents business rule for that?
I can get all Employees and filter them out in Domain Model, but that does not seem as efficient as SQL Query in repository. What patterns come into play?</p>


问:
How do I avoid having to compute square roots for every element in a data set?
I have a list of points, with floating point coordinates, of which I've computed the square of the Euclidean distance between these points. I have not computed the actual Euclidean distance between these points because computing a square root is an expensive operation. So, I have a list of floating point squares {a², b²...}.

My goal is to find the arithmetic mean of the actual Euclidean distance values, (a + b + ...) / n).

Is there a way to avoid computing square root for every element?

答:
1. change presentation data

Don't save squares but the square roots when inserting them, since a*a is cheaper than sqrt(aa)

2. fixed cache

I assume that just integers are used.

If you know, that there are many duplicates, maybe between 1*1 and 1000*1000 e.g. than caching them in a hashmap might accelerate computation.

3. LRU-cache

If you know that there are many duplicates in it, then a LRU-Cache might help you.

4. approximation

Instead of using sqrt you could implement it yourself, but only with a few iterations.


问:
What is the most readable way of passing arguments to the function?
I'm using JavaScript but the question can be generalized to all the languages.

In a nutshell - I'm checking if a browser connecting to my site is a microwave and cater for that accordingly.

What would be the best way to structure my code?

Best way is in "most readable", "most maintainable" (insert your own metric here)...

Option 1.0

```<code>var iammicrowave = /(microwave)/.test(navigator.userAgent);
if (iammicrowave) {
    var settings = { blah : 42 };
    magicFunction(settings);
} else {
    magicFunction();
}
</code>```

Option 1.1

```<code>var iammicrowave = /(microwave)/.test(navigator.userAgent);
if (iammicrowave) {
    magicFunction({ blah : 42 });
} else {
    magicFunction();
}
</code>```

Option 2.0

```<code>var iammicrowave = /(microwave)/.test(navigator.userAgent);
var settings;
if (iammicrowave) {
    settings = { blah : 42 };
}
magicFunction(settings);
</code>```

Option 2.1

```<code>var iammicrowave = /(microwave)/.test(navigator.userAgent);
var settings = imamicrowave ? { blah : 42 } : undefined;
magicFunction(settings);
</code>```

Option 2.1.1

```<code>var iammicrowave = /(microwave)/.test(navigator.userAgent);
var settings = imamicrowave ? { blah : 42 } : {};
magicFunction(settings);
</code>```

Option 2.2

```<code>var iammicrowave = /(microwave)/.test(navigator.userAgent);
magicFunction(imamicrowave ? { blah : 42 } : {});
</code>```

Option 2.3

```<code>magicFunction(/(microwave)/.test(navigator.userAgent) ? { blah : 42 } : {});
</code>```

Many thanks!

答:
You should not repeat the function call since it is always executed anyway. So 1.0 and 1.1 is out.

The distinction between 2.1 and 2.1.1 is really a question of the contract of the called function. The function might interpret undefined and {} differently, and we can't tell what is correct without knowing the contract of the function. I find option 2.1.1 clearer than option 2.0 and 2.1, but is is also semantically different so hard to say what is correct.

Should you have settings and iammicrowave as variables or just have the expressions inline as in 2.3? I find 2.3 nearly unreadable, so I would argue you should. Especially the variable name iammicrowave makes it a lot clearer what is going on.


问:
Porting an Apache licensed library from Java to C#, do I need to update the licensing to protect myself?
I'm porting the ESRI geometry-api-java library to C#. They have placed the library under the Apache License. They also mention it in the README.md.

They have a small bit of licensing info in the header of each source file. Do I have to replace that header with my own Apache 2.0 License disclaimer? Or do I just leave theirs in place? Or do I append my own disclaimer below theirs?

Basically, I want to make this ported version of the library publicly available, but I don't want to get screwed by some lawyer.

答:
The answer can be found right in the license text (emphasis mine).

Redistribution. You may reproduce and distribute copies of the
  Work or Derivative Works thereof in any medium, with or without
  modifications, and in Source or Object form, provided that You
  meet the following conditions:

(a) You must give any other recipients of the Work or
     Derivative Works a copy of this License; and

(b) You must cause any modified files to carry prominent notices
     stating that You changed the files; and

(c) You must retain, in the Source form of any Derivative Works
     that You distribute, all copyright, patent, trademark, and
     attribution notices from the Source form of the Work,
     excluding those notices that do not pertain to any part of
     the Derivative Works; and

(d) If the Work includes a "NOTICE" text file as part of its
     distribution, then any Derivative Works that You distribute must
     include a readable copy of the attribution notices contained
     within such NOTICE file, excluding those notices that do not
     pertain to any part of the Derivative Works, in at least one
     of the following places: within a NOTICE text file distributed
     as part of the Derivative Works; within the Source form or
     documentation, if provided along with the Derivative Works; or,
     within a display generated by the Derivative Works, if and
     wherever such third-party notices normally appear. The contents
     of the NOTICE file are for informational purposes only and
     do not modify the License. You may add Your own attribution
     notices within Derivative Works that You distribute, alongside
     or as an addendum to the NOTICE text from the Work, provided
     that such additional attribution notices cannot be construed
     as modifying the License.

You may add Your own copyright statement to Your modifications and
  may provide additional or different license terms and conditions
  for use, reproduction, or distribution of Your modifications, or
  for any such Derivative Works as a whole, provided Your use,
  reproduction, and distribution of the Work otherwise complies with
  the conditions stated in this License.

So, in short, yes, you must keep their original copyright notice and add a notice that you edited the file. In addition, you may add your own copyright notice if you want.  (I recommend you do.)