问:
Terms for different types of development
I'm trying to figure out the right terminology for the different types of software development

Right now, the only development term I know is "web development", but I've also done a lot of Java and C# development for desktop applications. Obviously this isn't web development, but I'd like to be able to group these under a single term that is widely known and accepted within the industry.

Would the term for applications on the desktop be "desktop development"?

答: Desktop Application Development or Rich Client Development come to mind...</p>


答:
I still see quite a few ads for Desktop Application Development. I think you might get more play if you specify the platform - Windows Application Development or OSX Application Development, for example, along with the languages (eg, C++) and libraries/frameworks (eg, Qt), so something like:

Developed applications for Windows using C++ in Qt 4.5

ContentLicense: CC BY-SA 3.0

Title: Terms for different types of development

Body:
I'm trying to figure out the right terminology for the different types of software development

Right now, the only development term I know is "web development", but I've also done a lot of Java and C# development for desktop applications. Obviously this isn't web development, but I'd like to be able to group these under a single term that is widely known and accepted within the industry.

Would the term for applications on the desktop be "desktop development"?


问:
Writing low latency Java
Are there any Java-specific techniques (things which wouldnt apply to C++) for writing low latency code, in Java? I often see Java low latency roles and they ask for experience writing low latency Java- which sometimes seems a little bit of an oxymoron.

The only think I could think of is experience with JNI, outsourcing I/O calls to native code. Also possibly using the disruptor pattern, but thats not an actual technology.

Are there any Java specific tips for writing low latency code?

I am aware there is a Real Time Java Spec, but I have been warned real-time is not the same as low latency....

答:
There are a bunch of things to be aware of yes. I'm in Crete at the moment with limited net access so this will be (fairly) short. Also, I'm not a low-latency expert, but several of my colleagues play one in real life :-).

You need to appreciate Mechanical Sympathy (a term coined by Martin Thompson).  In other words you need to understand what your underlying hardware is doing.  Knowing how CPUs load cache lines, what their read/write bandwidth is, speed of main memory and much, much more is very important.  Why?  Because you'll need to reason how your Java source code affects the OperatingSystem/Hardware via the runtime JVM.  For example, is the way your field variables are laid out in your source code causing cache line evictions (costs you ~150 clock cycles), hmmm... :-).

Generally you want lock free algorithms and I/O. Even the most well designed concurrent application (that uses locks) is at risk of blocking, blocking in low latency is generally bad :-).

Understand Object Allocation and Garbage Collection. This is a massive topic, but basically you want to avoid GC pauses (often caused by the Stop the World nature of various GC collections). Specialist GC collectors like the Azul collector can in many cases solve this problem for you out of the box, but for most people they need to understand how to tune the Sun/Oracle GCs (CMS, G1, etc).

The Hotspot JIT is freaking amazing.  Learn about its optimizations, but generally speaking all of the good OO techniques (encapsulation, small methods, as much immutable data as possible) will allow JIT to optimize, giving you the sorts of performance levels that well crafted C/C++ code gives you.

Overall system architecture.  Be aware of the network, how machines are co-located, if you're connected to the exchange via fiber etc etc.

Be aware of the impact of logging.  logging binary or using coded output that you can parse off line is probably a good idea.

Overall I highly recommend going on Kirk Pepperdine's Java Performance Tuning course [Disclaimer:  I teach this course myself, so I'm biased]. You'll get good coverage of the various aspects of the JVM and its impact on underlying O/S and hardware.

PS:  I'll try to revisit this later and tidy it up some.

Title: Writing low latency Java


问:
How to set up a one-man research in the difference between BDD and Waterfall?
Earlier, I asked a question about how to measure the quality of a project. The outcome of that question was that the quality of the project can be divided into two parts:

Internal quality (code quality, measurable by code quality metrics)

External quality (Acceptance test, how well the software meets the requirements)

So based on that, I want to set up some research and validate the outcome of the project. The problem is, I will conduct this research on my own, so it's not possible to run the project once in BDD style and the other one in waterfall by myself. It's also not possible to compare BDD and waterfall projects on a larger scale, due to the fact that there are not enough BDD projects that can be measured because of the age of BDD.

So, my question is: did anybody face this problem? How could I execute my experiment in such a way that it is of scientific value?

答:
You have set yourself a very difficult task!

Each software project is basically unique, a different set of people implementing a different set of requirements for a different customer in a different environment.

Plus commercial organizations will not make any internal data on project costs, effort or even success or failure public. In fact nearly all projects for businesses are successful purely because "success" is redefined as whatever was delivered.

Government organizations on the other hand must operate in public and figures on costs, resources and success/failure are published. At least at the beginning of projects they tend to be quite open about what they are doing and how, so between press releases, audit reports and published accounts (and Freedom Of Information requests at a last resort) you should be able to build up a pretty good body of data to analyze.

The caveat is government projects (at least the ones my taxes pay for) have a long and sordid history of failure and generally follow unique and uniquely incoherent project management styles.


问:
Continuous integration - build Debug and Release every time?
Is it standard practice when setting up a Continuous Integration server to build a Debug and Release version of each project?  Most of the time developers code with a Debug mode project configuration set enabled and there could be different library path configurations, compiler defines, or other items configured differently between Debug/Release that would cause them to act differently.

I configured my CI server to build both Debug & Release of each project and I'm wondering if I'm just overthinking it.  My assumption is that I'll do this as long as I can get quick feedback and once that happens, then push the Release off to a nightly build perhaps. Is there a 'standard' way of approaching this?

答:
You should build everything for which there is any chance that it won't be tried by anybody for some time. Which means, that if you have just one build target, you don't need to do Debug in continuous integration, because the developers will notice quickly. But more often than not there are multiple components and than components not currently being worked on won't be built by developers, but may still be broken by changes in common code. In such cases you need to build all configurations so you won't find the build broken when you need to touch such components.

Now often building everything in every configuration takes a lot of time, so you can't build everything after each commit. In such case, do the release configurations of the most important components every time and add a nightly build of everything.

I currently work on project, where we have continuous build, which polls version control every 10 minutes and even though it's just selected configurations can still take over 1 hour after larger commits. Than we have nightly build, which builds all components in all configurations and always does a clean build, which takes about 5 hours. And than we have a weekly build, that builds releases in all customized variants and that takes more than whole day.


问:
Achieving forward compatibility with C++11
I work on a large software application that must run on several platforms.  Some of these platforms support some features of C++11 (e.g. MSVS 2010) and some don't support any (e.g. GCC 4.3.x).  I expect this situation to continue for for several years (my best guess: 3-5 years).

Given that, I would like set up a compatibility interface such that (to whatever degree possible) people can write C++11 code that will still compile with older compilers with a minimum of maintenance.  Overall, the goal is to minimize #ifdef's as much as reasonably possible while still enabling basic C++11 syntax/features on the platforms that support them, and provide emulation on the platforms that don't.

Let's start with std::move().  The most obvious way to achieve compatibility would be to put something like this in a common header file:

```<code>#if !defined(HAS_STD_MOVE)
namespace std { // C++11 emulation
  template <typename T> inline T& move(T& v) { return v; }
  template <typename T> inline const T& move(const T& v) { return v; }
}
#endif // !defined(HAS_STD_MOVE)
</code>```

This allows people to write things like

```<code>std::vector<Thing> x = std::move(y);
</code>```

... with impunity.  It does what they want in C++11 and it does the best it can in C++03.  When we finally drop the last of the C++03 compilers, this code can remain as is.

However, according to the standard, it is illegal to inject new symbols into the std namespace.  That's the theory.  My question is: practically speaking is there any harm in doing this as a way of achieving forward compatibility?

答:
Gcc started introducing C++11 (still C++0x at that time) in 4.3. This table says it already has rvalue references and some other less used features (you have to specify -std=c++0x option to enable them).

Many additions to standard library in C++11 were already defined in TR1 and GNU stdlibc++ provides them in std::tr1 namespace. So just do appropriate conditional using.

Boost defines most of the TR1 functions and can inject them into the TR1 namespace if you don't have it (but VS2010 does and gcc 4.3 does as well if you use GNU stdlibc++).

Putting anything in std namespace is an "undefined behaviour". That means the specification does not say what will happen. But if you know that on particular platform the standard library does not define something, just go ahead and define it. Just expect that you will have to check on each platform what you need and what you can define.

The proposal for rvalue references, N1690 mentions how to implement move semantics in C++03. That could be used to substitute unique_ptr. However, it would not be too useful, because it relies on collections actually using the move semantics and the C++03 ones obviously won't.


问:
Why Is it better to use unreadable bytes for client server communication?
I'm composing communication lyrics for client-server and what am I thinking about:

"authme username passord" (maybe encrytped)

"accept"

"get archive of H2O from 03.02.2005 to 20.12.2064"

transferring binary structure or "error descrtiption"

why I always need to do something like

0x0FA52FD + CRC

0x0D34423 + CRC

...

I can see some secure reasons but I think it's not the real reason so why I can't use strings in client-server communication?

答:
There are two reasons that spring to mind:

Encoded requests and responses are usually shorter than "plain text" requests. If you are sending a lot of data this could become important. It's less so these days with faster connections and if you are only sending relatively small amounts of data.

Security is a real concern. If requests and responses are sent in the clear they can be read while in transit (man in the middle attacks) and, given enough packets, your application could be compromised. People could send fake requests to get data out of your system, insert fake data into your system and generally make a mess of things. While this is also true for binary data, it's harder and less obvious what the data is.


答:
you CAN use strings (look at the http, ftp, ... protocol) but then you'll likely bump into encoding problems (less now that the UTF8 has become the de facto standard but you'll still bump into stuff like UTF16 and will need to account for that)

you would also need to write a string parser that handles malformed input correctly (if only to drop the connection instead of crashing)

however parsing binary often comes down to a sequence of switch(in&MASK){case FIRST_CASE:...} which is much more efficient

however next to all these is the amount of data you are transmitting: binary can be much more compact than strings for comparable protocol

Title: Why Is it better to use unreadable bytes for client server communication?

Body:
I'm composing communication lyrics for client-server and what am I thinking about:

"authme username passord" (maybe encrytped)

"accept"

"get archive of H2O from 03.02.2005 to 20.12.2064"

transferring binary structure or "error descrtiption"

why I always need to do something like

0x0FA52FD + CRC

0x0D34423 + CRC

...

I can see some secure reasons but I think it's not the real reason so why I can't use strings in client-server communication?


问:
How do you share your craft with non programmers?
Sometimes I feel like a musician who can't play live shows. Programming is a pretty cool skill, and a very broad world, but a lot of it happens "off camera"- in your head, in your office, away from spectators.

You can of course talk about programming with other programmers, and there is peer programming, and you do get to create something that you can show to people, but when it comes to explaining to non programmers what is it that you do, or how was your day at work, it's sort of tricky.

How do you get the non programmers in your life to understand what is it that you do?

NOTE: this is not a repeat of Getting non-programmers to understand the development process, because that question was about managing client expectations.

答: I don't even try. If they aren't tech oriented enough to have at least a basic understanding of programming, I am only going to bore them with the details. Usually I just go with something very high level like "I create web sites" or "I write computer programs to do X"</p>

ContentLicense: CC BY-SA 2.5


问:
why are transaction monitors on decline? or are they?
http://www.itjobswatch.co.uk/jobs/uk/cics.do

http://www.itjobswatch.co.uk/jobs/uk/tuxedo.do

Look at the demand for programmers (% of job ads that the keyword appears), first graph under the table. It seems like demand for CICS, Tuxedo has fallen from 2.5%/1% respectively to almost zero.

To me, it seems bizarre: now we have more networked and internet enabled machines than ever before. And most of them are talking to some kind of database. So it would seem that use of products whose developers spent last 20-30 years working on distributing and coordinating and optimizing transactions should be on the rise. And it appears they're not.

I can see a few causes but can't tell whether they are true:

we forgot that concurrency and distribution are really hard, and redoing it all by ourselves, in Java, badly.

Erlang killed them all.

Projects nowadays have changed character, like most business software has already been built and we're all doing internet services, using stuff like Node.js, Erlang, Haskell. (I've used RabbitMQ which is written in Erlang, "but it was small specialized side project" kind of thing).

BigData is the emphasis now and BigData doesn't need transactions very much (?).

None of those explanations seem particularly convincing to me, which is why I'm looking for better one. Anyone?

答:
Transaction Monitors are still all the rage, its just that "Websphere", "Weblogic", "JBOSS" and "GlassFish" are now the dominant brand names!

Or to put it another way the decline of CICS and Tuxedo is a symtom of the decline in COBOL and plain C as application languages. The rise of J2EE (Application containers are really transaction monitors!) is a reflection of the rise of Java as the language of big business.


答:
In addition to Martijn's comments I'd add:

Warm up your JVM.  Bytecode starts starts off being interpreted for Hotspot and
then gets compiled on the server after 10K observations.
Tiered Compilation can be a good stop gap.

Classloading is a sequential process that involves IO to disk.  Make
sure all the classes for your main transaction flows are loaded
upfront and that they never get evicted from the perm generation.

Follow the "Single Writer Principle" to avoid contention and
the queueing effect implications of Little's Law, plus study
Amdhal's Law for what can be parallel and is it worth it.

Model you business domain and ensure all your algorithms are O(1) or
at least O(log n).  This is probably the biggest cause of
performance issues in my experience.  Make sure you have performance
tests to cover the main cases.

Low-latency in Java is not just limited to Java.  You need to
understand the whole stack your code is executing on.  This will
involve OS tuning, selecting appropriate hardware, tuning systems
software and device drivers for that hardware.

Be realistic. If you need low-latency don't run on a hypervisor.  Ensure you have sufficient cores for all threads that need to be in the runnable state.

Cache misses are your biggest cost to performance.  Use algorithms
that are cache friendly and set affinity to processor cores either
with taskset or numactl for a JVM or JNI for individual threads.

Consider an alternative JVM like Zing from Azul with a pause-less
garbage collector.

Most importantly get someone involved with experience.  This will
save you so much time in the long run.  Shameless plug :-)

Real-time and low-latency are distinctly separate subjects although often related.  Real-time is about being more predictable than fast.  In my experience the real-time JVMs, even the soft real-time ones, are slower than the normal JVMs.

Title: Writing low latency Java

Body:
Are there any Java-specific techniques (things which wouldnt apply to C++) for writing low latency code, in Java? I often see Java low latency roles and they ask for experience writing low latency Java- which sometimes seems a little bit of an oxymoron.

The only think I could think of is experience with JNI, outsourcing I/O calls to native code. Also possibly using the disruptor pattern, but thats not an actual technology.

Are there any Java specific tips for writing low latency code?

I am aware there is a Real Time Java Spec, but I have been warned real-time is not the same as low latency....


问:
Is there a term for quasi-open source proprietary software?
Say a company wants to keep development of new features of a piece of software internal, but wants to make the source code for previous versions public, up to and including existing public features, so that other people can benefit from using and modifying the software themselves, and even possibly contribute changes that can be applied to the development branch.

Is there a term for this sort of arrangement, and what is the best way of accomplishing it using existing version control tools and platforms?

答:
This pattern is actually quite frequent in the industry. One example is when a product is available as a community edition and an enterprise edition. Typically, the community edition is available at no cost and can even be free/open source software and usually lacks some features marketed as being only needed by enterprise users. The enterprise edition is usually available by purchasing a commercial license and can be optionally bundled with a support package, integrated software updates, more features or more permissive usage conditions. This pattern can be seen in many forms and variations in the market. One example is the Eclipse platform vs the commercial products based on it such as IBM Lotus client. Another example is the inception of what is now known as Mozilla Firefox, which started as Netscape Browser vs Mozilla Browser. And the list can continue.

Another approach is to separate the product in two separate standalone products. This is suitable for big projects that are composed by many smaller sub-projects. One example is Fedora Linux vs Red Hat Enterprise Linux or OpenSuse vs Suse Linux Enterprise editions.


答:
Do you need to maintain readability of the protocol itself? If so, why? Human-readable(that is, text strings) protocols like the example you gave have multiple downsides. First obvious one is the bandwidth requirements; it is surely much more compact to send out 0x01, 0x00 than something like "USER LOGIN\n", let alone longer phrases as you described. The second is security. For example what do the bytes 0x3C, 0x02 and 0x7F mean in terms of protocol? The only way to figure out is to start reverse-engineering the binaries. Much more tedious work, and not always that trivial.

Frankly, I see absolutely no real benefits of using human readable protocols, unless they are specifically meant to be human readable - something which I don't see happening that often. I would strongy advice against strings in protocols. Please, don't.


答:
I would advocate a binary protocol based off something like Protocol Buffers. There's little benefit, in my opinion, in a human-readable protocol assuming the protocol isn't meant to be read by a human.

Using protocol buffers gives you the compactness of a binary protocol with the flexibility of a text-based protocol (e.g. with optional fields and the like). It also has the added benefit of being able to generate the serialization/deserialization code for you automatically, so you can use strong-typed objects in your program code.


问:
Should I use a separate class per test?
Taking the following simple method, how would you suggest I write a unit test for it (I am using MSTest however concepts are similar in other tools).

```<code>public void MyMethod(MyObject myObj, bool validInput)
{
 if(!validInput)
 {
  // Do nothing
 }
 else
 {
  // Update the object
  myObj.CurrentDateTime = DateTime.Now;
  myObj.Name = "Hello World";
 }
}
</code>```

If I try and follow the rule of one assert per test, my logic would be that I should have a Class Initialise method which executes the method and then individual tests which check each property on myobj.

```<code>public class void MyTest
{
    MyObj myObj;

    [TestInitialize]
    public void MyTestInitialize()
    {
        this.myObj = new MyObj();
        MyMethod(myObj, true);
    }

    [TestMethod]
    public void IsValidName()
    {
        Assert.AreEqual("Hello World", this.myObj.Name);
    }

    [TestMethod]
    public void IsDateNotNull()
    {
        Assert.IsNotNull(this.myObj.CurrentDateTime);
    }
}
</code>```

Where I am confused is around the TestInitialize. If I execute the method under TestInitialize, I would need separate classes per variation of parameter inputs. Is this correct? This would leave me with a huge number of files in my project (unless I have multiple classes per file).

答:
you should call the method you are going to test in TestMethod. the TestInitialize function sets everything up to avoid code duplication: it is called each time before a TestMethod function so you start from a blank slate

```<code>public class void MyTest {
     MyObj myObj;
      [TestInitialize]
     public void MyTestInitialize()
     {
         this.myObj = new MyObj();

      }

     [TestMethod]
     public void IsValidName()
     {
         MyMethod(myObj, true);
         Assert.AreEqual("Hello World", this.myObj.Name);
     }

     [TestMethod]
     public void IsInvalidName()
     {
         MyMethod(myObj, false);
         Assert.AreNotEqual("Hello World", this.myObj.Name);
     }

}
</code>```

Title: Should I use a separate class per test?

Body:
you should call the method you are going to test in TestMethod. the TestInitialize function sets everything up to avoid code duplication: it is called each time before a TestMethod function so you start from a blank slate

public class void MyTest {
     MyObj myObj;
      [TestInitialize]
     public void MyTestInitialize()
     {
         this.myObj = new MyObj();

      }

     [TestMethod]
     public void IsValidName()
     {
         MyMethod(myObj, true);
         Assert.AreEqual("Hello World", this.myObj.Name);
     }

     [TestMethod]
     public void IsInvalidName()
     {
         MyMethod(myObj, false);
         Assert.AreNotEqual("Hello World", this.myObj.Name);
     }

}


答:
You need to look at the "Arrange Act Assert" pattern.

For each test you:

Arrange the Code Under Test and any dependent variables.

Act by calling the method on the Code Under Test.

Assert what you need to ensure the test passes (this should be one thing per test).

In this case you will use:

```<code>[TestMethod]
public void MyMethod_CalledWithValidName_HasHelloWorldName()
{
    //Arrange
    bool isValid = true;
    MyObj objectParameter = new MyObj();
    ClassUnderTest objectUnderTest = new ClassUnderTest();

    //Act
    objectUnderTest.MyMethod(objectParameter, isValid);

    //Assert
    string expectedName = "Hello World";
    string actualName = this.myObj.Name;
    Assert.AreEqual(expectedName, actualName);
}
</code>```

This allows you to see exactly what you are testing and by copying this test and changing the variables, quickly write additional tests for other conditions.

Notice the MethodUnderTest_Condition_ExpectedResult naming pattern for the test.

To actually answer your question:

If I execute the method under TestInitialize, I would need seperate classes per variation of parameter inputs.

Why? TestInitialize is to set up the environment needed for every test in that class and run before each test. In my experience these tend to be relatively small since different methods you are testing have different dependencies and should be isolated enough that you won't need to set up too much the same for each test. If you need to vary the parameter inputs for each test, then it doesn't belong in TestInitialize, it belongs in the Arrange part of your test.

In this class you have a test against a method on an unspecified class (MyMethod), and a test against myObj. These tests should be in different classes.

You are confusing TestInitalize (setup before each test) as ClassInitialize (setup before all tests in that class).

ClassInitialize can be abused and can make the order of the tests important. A test should not depend on another test. You can end up with passes/fails based on the order of test execution, which in some cases can't be specified, and cause breaks when tests are updated or removed or run in isolation.

The way to make sure that myObj.CurrentDateTime isn't null when testing your unspecified class is to set it, you won't be asserting anything on that myObj in those tests. You can assert against them in the test class for myObj. One test class per object with one assert per test.

It also appears that MyMethod is static. This breaks test isolation by meaning that when the code under test runs with works with other production code. This turns this test into an integration test (because the code under test and the static class are both being run during the test); this is bad because a failure in the static class can cause failures in the code under test in methods which are perfectly functional.

I've covered a lot of different aspects of testing in this answer based on a lot of different principles. Grab the fantastic The Art Of Unit Testing and read it cover to cover. It's a great, easy read and is one of the books I'd grab (along with Code Complete) if the office caught fire.

Title: Should I use a separate class per test?

Body:
Taking the following simple method, how would you suggest I write a unit test for it (I am using MSTest however concepts are similar in other tools).

```<code>public void MyMethod(MyObject myObj, bool validInput)
{
 if(!validInput)
 {
  // Do nothing
 }
 else
 {
  // Update the object
  myObj.CurrentDateTime = DateTime.Now;
  myObj.Name = "Hello World";
 }
}
</code>```

If I try and follow the rule of one assert per test, my logic would be that I should have a Class Initialise method which executes the method and then individual tests which check each property on myobj.

```<code>public class void MyTest
{
    MyObj myObj;

    [TestInitialize]
    public void MyTestInitialize()
    {
        this.myObj = new MyObj();
        MyMethod(myObj, true);
    }

    [TestMethod]
    public void IsValidName()
    {
        Assert.AreEqual("Hello World", this.myObj.Name);
    }

    [TestMethod]
    public void IsDateNotNull()
    {
        Assert.IsNotNull(this.myObj.CurrentDateTime);
    }
}
</code>```

Where I am confused is around the TestInitialize. If I execute the method under TestInitialize, I would need separate classes per variation of parameter inputs. Is this correct? This would leave me with a huge number of files in my project (unless I have multiple classes per file).

Body:
You need to look at the "Arrange Act Assert" pattern.

For each test you:

Arrange the Code Under Test and any dependent variables.

Act by calling the method on the Code Under Test.

Assert what you need to ensure the test passes (this should be one thing per test).

In this case you will use:

[TestMethod]
public void MyMethod_CalledWithValidName_HasHelloWorldName()
{
    //Arrange
    bool isValid = true;
    MyObj objectParameter = new MyObj();
    ClassUnderTest objectUnderTest = new ClassUnderTest();

    //Act
    objectUnderTest.MyMethod(objectParameter, isValid);

    //Assert
    string expectedName = "Hello World";
    string actualName = this.myObj.Name;
    Assert.AreEqual(expectedName, actualName);
}

This allows you to see exactly what you are testing and by copying this test and changing the variables, quickly write additional tests for other conditions.

Notice the MethodUnderTest_Condition_ExpectedResult naming pattern for the test.

To actually answer your question:

If I execute the method under TestInitialize, I would need seperate classes per variation of parameter inputs.

Why? TestInitialize is to set up the environment needed for every test in that class and run before each test. In my experience these tend to be relatively small since different methods you are testing have different dependencies and should be isolated enough that you won't need to set up too much the same for each test. If you need to vary the parameter inputs for each test, then it doesn't belong in TestInitialize, it belongs in the Arrange part of your test.

In this class you have a test against a method on an unspecified class (MyMethod), and a test against myObj. These tests should be in different classes.

You are confusing TestInitalize (setup before each test) as ClassInitialize (setup before all tests in that class).

ClassInitialize can be abused and can make the order of the tests important. A test should not depend on another test. You can end up with passes/fails based on the order of test execution, which in some cases can't be specified, and cause breaks when tests are updated or removed or run in isolation.

The way to make sure that myObj.CurrentDateTime isn't null when testing your unspecified class is to set it, you won't be asserting anything on that myObj in those tests. You can assert against them in the test class for myObj. One test class per object with one assert per test.

It also appears that MyMethod is static. This breaks test isolation by meaning that when the code under test runs with works with other production code. This turns this test into an integration test (because the code under test and the static class are both being run during the test); this is bad because a failure in the static class can cause failures in the code under test in methods which are perfectly functional.

I've covered a lot of different aspects of testing in this answer based on a lot of different principles. Grab the fantastic The Art Of Unit Testing and read it cover to cover. It's a great, easy read and is one of the books I'd grab (along with Code Complete) if the office caught fire.


答:
First of all, there are more alternatives:

MSDTC (Microsoft Distributed Transaction Coordinator) — available since Windows 2000;

various implementations on JTA (Java Transaction API), including open-source such as JBossTS;

Second, there is much more emphasis on building decoupled, scalable systems. Thus instead of transaction you have message queues. Distributed transactions just don't scale well enough. It's about Eric Brewer's CAP theorem. With traditional transaction monitors you choose consistency & availability, while truly scalable systems need availability & partition tolerance, sacrificing consistency for eventual consistency.

Title: why are transaction monitors on decline? or are they?

Body:
http://www.itjobswatch.co.uk/jobs/uk/cics.do

http://www.itjobswatch.co.uk/jobs/uk/tuxedo.do

Look at the demand for programmers (% of job ads that the keyword appears), first graph under the table. It seems like demand for CICS, Tuxedo has fallen from 2.5%/1% respectively to almost zero.

To me, it seems bizarre: now we have more networked and internet enabled machines than ever before. And most of them are talking to some kind of database. So it would seem that use of products whose developers spent last 20-30 years working on distributing and coordinating and optimizing transactions should be on the rise. And it appears they're not.

I can see a few causes but can't tell whether they are true:

we forgot that concurrency and distribution are really hard, and redoing it all by ourselves, in Java, badly.

Erlang killed them all.

Projects nowadays have changed character, like most business software has already been built and we're all doing internet services, using stuff like Node.js, Erlang, Haskell. (I've used RabbitMQ which is written in Erlang, "but it was small specialized side project" kind of thing).

BigData is the emphasis now and BigData doesn't need transactions very much (?).

None of those explanations seem particularly convincing to me, which is why I'm looking for better one. Anyone?


问:
What is a good way to comment if-else-clauses?
Whenever I'm writing a typical if-else-construct in any language I wonder what would be the best way (in terms of readability and overview) to add comments to it. Especially when commenting the else clause the comments always feel out-of-place for me. Say we have a construct like this (examples are written down in PHP):

```<code>if ($big == true) {
    bigMagic();
} else {
    smallMagic()
}
</code>```

I could comment it like this:

```<code>// check, what kind of magic should happen
if ($big == true) {
    // do some big magic stuff
    bigMagic();
} else {
    // small magic is enough
    smallMagic()
}
</code>```

or

```<code>// check, what kind of magic should happen
// do some big magic stuff
if ($big == true) {
    bigMagic();
}
// small magic is enough
else {
   smallMagic()
}
</code>```

or

```<code>// check, what kind of magic should happen
// if:   do some big magic stuff
// else: small magic is enough
if ($big == true) {
    bigMagic();
} else {
    smallMagic()
}
</code>```

What are your best-practice examples for commenting this?

答:
I prefer either:

```<code>if ($magic == big) {
    bigMagic();
}
else {
    smallMagic();
}
</code>```

or:

```<code>if ($magic == big) {
    // big magic requires a big surprise, so I'm telling you about it here
    surprisingThing();
}
else {
    // give a magical feeling even if $magic is noMagicAtAll
    smallMagic();
}
</code>```

It seems a little silly to write a comment explaining what your condition checks unless the code doesn't clearly state it. Even then, better to rewrite the code to make it as clear as possible. The same goes for the bodies of the conditional blocks -- if you can make the reason for doing something obvious, do that instead of commenting.

I don't subscribe to the "never write comments" philosophy, but I do believe in avoiding comments that say what the code should be saying. If you write a comment like "check what kind of magic should happen" when the code could say if ($magic == big) {..., readers will stop reading your comments very quickly. Using fewer, more meaningful comments gives each of your comments more value, and readers are much more likely to pay attention to those that you do write.

Choosing meaningful names for your variables and functions is important. A well-chosen name can eliminate the need for explanatory comments throughout your code. In your example, $magic or maybe $kindOfMagic seems like a better name than $big since according to your example, it's the "kind of magic" that's being tested, not the "bigness" of something.

Say as much as you can in code. Save prose for the cases that demand more explanation than you can reasonably write in code.

Body:
I prefer either:

if ($magic == big) {
    bigMagic();
}
else {
    smallMagic();
}

or:

if ($magic == big) {
    // big magic requires a big surprise, so I'm telling you about it here
    surprisingThing();
}
else {
    // give a magical feeling even if $magic is noMagicAtAll
    smallMagic();
}

It seems a little silly to write a comment explaining what your condition checks unless the code doesn't clearly state it. Even then, better to rewrite the code to make it as clear as possible. The same goes for the bodies of the conditional blocks -- if you can make the reason for doing something obvious, do that instead of commenting.

I don't subscribe to the "never write comments" philosophy, but I do believe in avoiding comments that say what the code should be saying. If you write a comment like "check what kind of magic should happen" when the code could say if ($magic == big) {..., readers will stop reading your comments very quickly. Using fewer, more meaningful comments gives each of your comments more value, and readers are much more likely to pay attention to those that you do write.

Choosing meaningful names for your variables and functions is important. A well-chosen name can eliminate the need for explanatory comments throughout your code. In your example, $magic or maybe $kindOfMagic seems like a better name than $big since according to your example, it's the "kind of magic" that's being tested, not the "bigness" of something.

Say as much as you can in code. Save prose for the cases that demand more explanation than you can reasonably write in code.


问:
How do you track bugs in your personal projects?
I'm trying to decide if I need to reassess my defect-tracking process for my home-grown projects.  For the last several years, I really just track defects using TODO tags in the code, and keeping track of them in a specific view (I use Eclipse, which has a decent tagging system).

Unfortunately, I'm starting to wonder if this system is unsustainable.  The defects I find are typically associated with a snippet of code I'm working on; bugs which are not immediately understood tend to be forgotten, or ignored.  I wrote an application for my wife which has had a severe defect for almost 9 months, and I keep forgetting to fix it.

What mechanism do you use to track defects in your personal projects?  Do you have a specific system, or a process for prioritizing and managing them?

答:
If you're familiar with using TODO tags in Eclipse, a simple step up would be to use Mylyn.  At it's most basic, it's a simple todo list.  But, it also associates context with tasks - click on a task to activate it, do some stuff, and then next time you activate it, Eclipse will open up the relevant classes, and show you the relevant methods.  Even more powerfully, if you eventually move to some other bug tracking system, Mylyn can pull tasks from those systems and present them in your IDE.

Most Eclipse downloads these days have Mylyn bundled in as standard.  Just search out the Task List view and start adding tasks.


问:
Github Organization Repositories, Issues, Multiple Developers, and Forking - Best Workflow Practices
A weird title, yes, but I've got a bit of ground to cover I think.

We have an organization account on github with private repositories.  We want to use github's native issues/pull-requests features (pull requests are basically exactly what we want as far as code reviews and feature discussions).  We found the tool hub by defunkt which has a cool little feature of being able to convert an existing issue to a pull request, and automatically associate your current branch with it.

I'm wondering if it is best practice to have each developer in the organization fork the organization's repository to do their feature work/bug fixes/etc.  This seems like a pretty solid work flow (as, it's basically what every open source project on github does) but we want to be sure that we can track issues and pull requests from ONE source, the organization's repository.

So I have a few questions:

Is a fork-per-developer approach appropriate in this case?  It seems like it could be a little overkill.  I'm not sure that we need a fork for every developer, unless we introduce developers who don't have direct push access and need all their code reviewed.  In which case, we would want to institute a policy like that, for those developers only.  So, which is better?  All developers in a single repository, or a fork for everyone?

Does anyone have experience with the hub tool, specifically the pull-request feature?  If we do a fork-per-developer (or even for less-privileged devs) will the pull-request feature of hub operate on the pull requests from the upstream master repository (the organization's repository?) or does it have different behavior?

EDIT

I did some testing with issues, forks, and pull requests and found that.  If you create an issue on your organization's repository, then fork the repository from your organization to your own github account, do some changes, merge to your fork's master branch.  When you try to run hub -i <issue #> you get an error, User is not authorized to modify the issue.  So, apparently that work flow won't work.

答:
Is a fork-per-developer approach appropriate in this case? It seems like it could be a little overkill. I'm not sure that we need a fork for every developer, unless we introduce developers who don't have direct push access and need all their code reviewed. In which case, we would want to institute a policy like that, for those developers only. So, which is better? All developers in a single repository, or a fork for everyone?

Depends on your team's scale, I guess. I used to work in a small team where we just had a single repo and features got their own branches within that repo. That worked just fine for us.

However, I now regularly contribute to a larger open source project where a few dozen people have access to the central repo. We still do all major development in personal repos and submit PRs for features so code can be reviewed, although bugfixes can be pushed directly. The main repo only carries master and release branches, keeping it free of clutter. Feature branches are in personal repos, so they can still be seen by others (making early PRs for them will alert others in the team that work on a feature is underway). I can recommend this workflow for any project with more than a handful of developers; the only downside to it is having to work with multiple remotes.


答:
I try and use comments inside the block saying what that block does (your first sample).

Where this kinda' breaks down is when using elseif. I use Basic so there is no explicit end block and often have to comment what the condition is checking which goes on the line above (with a line break of course) if it's too long.

```<code>'Check XYZ
If Condition1 then
  'We need to do T and S
  DoCodeFor1();

'Check ABC
ElseIf Condition1 then
  'This requires something else to be done
  DoCodeFor2()

Else
  'We have no other option than to...
  DoCodeFor3()

End If
</code>```

Title: What is a good way to comment if-else-clauses?

Body:
Whenever I'm writing a typical if-else-construct in any language I wonder what would be the best way (in terms of readability and overview) to add comments to it. Especially when commenting the else clause the comments always feel out-of-place for me. Say we have a construct like this (examples are written down in PHP):

```<code>if ($big == true) {
    bigMagic();
} else {
    smallMagic()
}
</code>```

I could comment it like this:

```<code>// check, what kind of magic should happen
if ($big == true) {
    // do some big magic stuff
    bigMagic();
} else {
    // small magic is enough
    smallMagic()
}
</code>```

or

```<code>// check, what kind of magic should happen
// do some big magic stuff
if ($big == true) {
    bigMagic();
}
// small magic is enough
else {
   smallMagic()
}
</code>```

or

```<code>// check, what kind of magic should happen
// if:   do some big magic stuff
// else: small magic is enough
if ($big == true) {
    bigMagic();
} else {
    smallMagic()
}
</code>```

What are your best-practice examples for commenting this?

Body:
I try and use comments inside the block saying what that block does (your first sample).

Where this kinda' breaks down is when using elseif. I use Basic so there is no explicit end block and often have to comment what the condition is checking which goes on the line above (with a line break of course) if it's too long.

'Check XYZ
If Condition1 then
  'We need to do T and S
  DoCodeFor1();

'Check ABC
ElseIf Condition1 then
  'This requires something else to be done
  DoCodeFor2()

Else
  'We have no other option than to...
  DoCodeFor3()

End If


答:
There are actually very few good reasons to go binary. The one you mentioned -- security -- is a false promise. You are either encrypting your data or not; binary transmission in the clear is still in the clear it just requires a modicum more skill to read. Another reason mentioned -- text encoding -- is effectively a solved problem at this point. On the flip side, binary encoding can create interesting problems -- think big- versus little-endian.

The advantages of text-based protocols are that they are vastly easier to understand, build, debug, use and hack. You don't need nearly as much tooling to get anywhere. When you do need tooling there is an endless array of text-based tools at your disposal. With your custom binary protocol you get to define the toolset for better of for worse.

This is not to say that binary protocols don't have their place -- there are lots of applications that require that sort of compactness and pointedness. But binary should not be the default answer.


答: If all the code is owned by you, then you can relicence as you see fit. If anyone else has contributed into the code you want to close source, then you MUST get their permission or get them to sign copyright over to you. As for a name, I don;t know of a standard, maybe dual licencing? I think MySQL is in the same position, and maybe QT?</p>

Title: Is there a term for quasi-open source proprietary software?

Body:
Say a company wants to keep development of new features of a piece of software internal, but wants to make the source code for previous versions public, up to and including existing public features, so that other people can benefit from using and modifying the software themselves, and even possibly contribute changes that can be applied to the development branch.

Is there a term for this sort of arrangement, and what is the best way of accomplishing it using existing version control tools and platforms?


问:
Validating allowed characters or validating disallowed characters
I've always validated my user input based on a list of valid/allowed characters, rather than a list of invalid/disallowed characters (or simply no validation). It's just a habit I picked up, probably on this site and I've never really questioned it until now.

It makes sense if you wish to, say, validate a phone number, or validate an area code, however recently I've realised I'm also validating input such as Bio Text fields, User Comments, etc. for which the input has no solid syntax.

The main advantage has always seemed to be: Validating allowed chars reduces the risk of you missing a potentially malicious character, but increases the risk the of you not allowing a character which the user may want to use. The former is more important.

But, providing I am correctly preventing SQL Injection (with prepared statements) and also escaping output, is there any need for this extra barrier of protection? It seems to me as if I am just allowing practically every character on the keyboard, and  am forgetting to allow some common characters.

Is there an accepted practice for this situation? Or am I missing something obvious?

Thanks.

答:
Usually the set of disallowed characters is small, so searching for then in the input stream makes the most sense. For those times when it is reverse -- allowing only a few and disallowing many - it's usually better to match against the accepted characters. There is no universal rule. You have to fully understand what your allowable range of characters are.

You ask whether you're missing something obvious. What you're missing is the perspective of your users.  You're approaching the need for validation from a technical perspective, but most validation is all about the user.

Asking if it's important is as easy as asking if it improves the user experience. If it does, you should support it. If it doesn't, don't. User frustration is high when they enter a character only to be told on a separate screen or popup window or error message "sorry, you can't enter a "-" in your phone number". Frustration can be equally high if they know a "-" is legal but your software disallows it.

Most importantly, however, is to be absolutely certain the characters you disallow are genuinely necessary to disallow. For example, nothing frustrates me more than a web site that gives me an error when I put dashes in a phone number or social security number, or spaces in a credit card number. Computers are quite smart enough to simply remove them. Don't force me to input a string that matches your internal requirements when you can easily accept more and simply ignore characters that any reasonable human would know to just ignore.


答:
To me it depends on what the condition is.

If only certain characters are valid (passwords (dubious?), phone numbers, hostnames, ip addresses) then you should check that each character is valid.

If however anything is valid but special characters are problems (file names, sql, html) then you should explicitly check for them.

Title: Validating allowed characters or validating disallowed characters

Body:
I've always validated my user input based on a list of valid/allowed characters, rather than a list of invalid/disallowed characters (or simply no validation). It's just a habit I picked up, probably on this site and I've never really questioned it until now.

It makes sense if you wish to, say, validate a phone number, or validate an area code, however recently I've realised I'm also validating input such as Bio Text fields, User Comments, etc. for which the input has no solid syntax.

The main advantage has always seemed to be: Validating allowed chars reduces the risk of you missing a potentially malicious character, but increases the risk the of you not allowing a character which the user may want to use. The former is more important.

But, providing I am correctly preventing SQL Injection (with prepared statements) and also escaping output, is there any need for this extra barrier of protection? It seems to me as if I am just allowing practically every character on the keyboard, and  am forgetting to allow some common characters.

Is there an accepted practice for this situation? Or am I missing something obvious?

Thanks.


问:
Is there any practical trick to remember the difference between big-endian and little-endian?
I don't work every day with big-endian and little-endian problems and thus I find very difficult to remember which one is what.

Recently I got an interview asking the difference between the two; since I didn't remember I decided to "guess" (50% chance, after all) but I failed.

So, is there any wide known pratical trick to remember what is the difference between big endian and little endian?

答:
I've always thought that it's defined the wrong way, and that's also the tip to remember it. As a non-native English speaker, I see "end" as the opposite of "start" (although obviously "end" can mean either end - the start end, or the end end). Anyway, I just remember that "it's defined the wrong way" :)

In big endian, the most significant (biggest) byte is in the start.

In little endian, the least significant (littlest) byte is in the start.

Or, referring to bit endianness:

128 64 32 16 8 4 2 1 is big endian, because it ends to the little.

1 2 4 8 16 32 64 128 is little endian, because it ends to the big.

Even though the usual use of the word endianness refers to how bytes are ordered within a word, its generic meaning refers to the ordering of individually addressable sub-components within the representation of a larger data item (as explained in Wikipedia).


答:
Any implementation trying to detect "malicious characters" is flawed, when you look at the combined properties of such an implementation:

A "valid" subset of a character set is not so easy to define. Newline is a control character, and you definitely want to allow that in comments. You'd have work cut out for days or weeks to create a sensible subset of Unicode (and combinations of characters) which could be considered "valid" across the globe.

An "invalid" subset is also difficult, if you're making anything even remotely complex. For example, you don't want literal quotes in SQL, but you also don't want literal ampersands or inequality signs in HTML, or backslash in JavaScript. If you have a series of input and output languages, the only way to be sure is to escape user input/output for each one, and test that the escaping works.

The set is valid only for a single version of a single character set, so it's not future-proof.

You still need to test the full character range to see if there are any security holes.

If you're not careful with what you accept, you'll end up annoying users, and they will leave. If you're lucky, one in a thousand will file a bug report.

I'd go so far as to say that validating allowed characters reduces security, because it encourages sloppy implementation (lack of testing/escaping). If you escape where necessary you can instead just test the "nasty" characters, and if they work, you have pretty much guaranteed that other nasty characters will also be harmless to the system.

All this is of course not to say that some characters are nonsensical in some fields, such as two in a numeric field. But even this is often not trivial:

Different languages have different decimal and thousand separators. 1,000 == 1 in much of Europe, and 1'000 is a valid way to write 1000 in some places. You don't want to tell those users their way of writing is wrong.

Leading zeros, plus signs, hash and asterisk are all valid characters in a phone number. Some countries include an inland prefix ((0) in Switzerland) which you have to use within the country, and have to exclude when using a language code.

Contrary to many sloppy web developers, email addresses can contain dash and plus characters, and a whole lot of others which are routinely rejected as if to prove that the company policy is that they would do just fine if it wasn't for all those pesky customers.

Names can contain quote characters (Gerard 't Hooft), numbers (John Doe the 5th), punctuation (John Doe, M.D.), and SQL:


问:
Strategy for backwards compatibility of persistent storage
In my experience, trying to ensure that new versions of an application retain compatibility with data storage from previous versions can often be a painful process.

What I currently do is to save a version number for each 'unit' of data (be it a file, database row/table, or whatever) and ensure that the version number gets updated each time the data changes in some way. I also create methods to convert from v1 to v2, v2 to v3, and so on. That way, if I'm at v7 and I encounter a v3 file, I can do v3->v4->v5->v6->v7.

So far this approach seems to be working out well, but I haven't had to make use of it extensively yet so there may be unforseen problems. I'm also concerned that if the objects I'm loading change significantly, I'll either have to keep around old versions of the classes or face updating all my conversion methods to handle the new class definition.

Is my approach sound? Are there other/better approaches I could be using? Are there any design patterns applicable to this problem?

答:
that is a decent system besided the code bloat for upgrading data

my advise is to upgrade as much as possible while remaining lazy about it (you read a object from a previous version->you overwrite it with the same (now upgraded) object from the current version)

but you will run into trouble if you ever tighten up the data invariants or simply do a complete overhoal so an older version cannot be upgraded


答:
My own tip :

big endian = big-end first ! (the first byte (lowest address) is the MSB)

little endian = little-end first ! (the first byte (lowest address) is the LSB)

Title: Is there any practical trick to remember the difference between big-endian and little-endian?

Body:
I don't work every day with big-endian and little-endian problems and thus I find very difficult to remember which one is what.

Recently I got an interview asking the difference between the two; since I didn't remember I decided to "guess" (50% chance, after all) but I failed.

So, is there any wide known pratical trick to remember what is the difference between big endian and little endian?


答: If your objects is just data holders, you could use the Google protobuff library or something similar. For each member of the class/object exists a property that indicates if the member exists or not. For example a objects version V1 has a property called DataForV1 and a property called HasDataForV1. The version V2 does not use the DataForV1 anymore so HasDataForV1 will always returns false and the DataForV1 property could contains null or a default value. </p>

Title: Strategy for backwards compatibility of persistent storage

Body:
In my experience, trying to ensure that new versions of an application retain compatibility with data storage from previous versions can often be a painful process.

What I currently do is to save a version number for each 'unit' of data (be it a file, database row/table, or whatever) and ensure that the version number gets updated each time the data changes in some way. I also create methods to convert from v1 to v2, v2 to v3, and so on. That way, if I'm at v7 and I encounter a v3 file, I can do v3->v4->v5->v6->v7.

So far this approach seems to be working out well, but I haven't had to make use of it extensively yet so there may be unforseen problems. I'm also concerned that if the objects I'm loading change significantly, I'll either have to keep around old versions of the classes or face updating all my conversion methods to handle the new class definition.

Is my approach sound? Are there other/better approaches I could be using? Are there any design patterns applicable to this problem?


问:
How to change documentation expectations from "risk management" to a "project risk"
We are trying to introduce some agile concepts to the business folks but one of our biggest hurdles is their expectations for documentation - the quantity, the authorship/ownership of the documents, the lifecycle of the documents, the timing of the authorship, and the material that should be documented. The business folks are very much in a traditional mindset. For them, documentation is a risk management tool.

What can we do to help them view documentation as a risk and a requirement? We've tried indicating in the project timelines how much extra time is spent on documentation. Their response is it helps them estimate project costs and reduces risk. We respond with empirical evidence showing it hasn't worked to date. They respond with "that's IT's fault for reasons x, y, z" and then introduce more documentation to account for IT's failings. And then it spirals on and on until the baggage is un-bearable.

Regarding authorship, we've tried to convince the business that IT doesn't want or need certain documents and that the business is capable of producing them without IT if they feel it's needed. So they will produce something, and then hold us accountable for its contents even though it is (from our perspective) un-related to us and un-vetted by us. So when we try to vet their documents, we get sucked in and then the questions of ownership and maintenance rear their ugly heads.

From everything I've read, this is a no-win situation. Is there any advice or thoughts to help us transform their expectations of documentation to light-weight, just-in-time/just good enough documents?

If needed, find more background in my previous question.

答:
From what you're saying, it's probably going to be a long and arduous journey, for both you and the business.

Just remember that the business is entitled to any documents they ask for from you. If they are willing to prioritise it above other work that they want, that's up to them. I know documentation is boring, but it is part of the job.

On the plus side, you will find that there are two things you can do to ... discourage them.

First, you can make them think about the necessity for documentation. Every time they ask for a document, ask them who is going to read it and what they're going to do with it. These are valid questions anyway, you need to know how to target the docs. But it also makes them think; often these documents are things they think they should have, but they don't really know what they'll do with it.

Second, you can make the cost of documentation very visible to them. Make it a part of the task estimate, or even a separate task. When they start to lose out on functionality because of some documentation they never really needed, they'll start to rethink their documentation strategy.

Again, don't expect quick results here. The current situation is that you think they're being insanely old-school in asking for the documentation and they think you're being lazy by trying to avoid it. Teaching them that you're trying to help them out is a slow process.


答:
In C++ or C# I typically wouldn't comment simple cases (when its clear what's happening), and use this sort of style for commenting the final else ...</p>

```<code>if (pattern == AAA)
{
  DoSomethingAAA();
}
else if (pattern == BBB)
{
  DoSomethingBBB();
}
else // if (pattern == CCC)
{
  DoSomethingCCC();
}
</code>```

Body:
In C++ or C# I typically wouldn't comment simple cases (when its clear what's happening), and use this sort of style for commenting the final else ...

if (pattern == AAA)
{
  DoSomethingAAA();
}
else if (pattern == BBB)
{
  DoSomethingBBB();
}
else // if (pattern == CCC)
{
  DoSomethingCCC();
}


答:
Like others here I use either a text file or the bug tracker built into whatever dvcs hosting service.

Lots of it depends on what sort of "personal project" it is.  Is it something that is ever going to see the light of day or is it just an experiment?  Is this project in use by the public?

For example, one of my personal projects became moderately popular and setting up a Get Satisfaction site for it worked really well. Not really "bug tracking" but it worked great for bug/feature requests.

Title: How do you track bugs in your personal projects?

Body:
I'm trying to decide if I need to reassess my defect-tracking process for my home-grown projects.  For the last several years, I really just track defects using TODO tags in the code, and keeping track of them in a specific view (I use Eclipse, which has a decent tagging system).

Unfortunately, I'm starting to wonder if this system is unsustainable.  The defects I find are typically associated with a snippet of code I'm working on; bugs which are not immediately understood tend to be forgotten, or ignored.  I wrote an application for my wife which has had a severe defect for almost 9 months, and I keep forgetting to fix it.

What mechanism do you use to track defects in your personal projects?  Do you have a specific system, or a process for prioritizing and managing them?


答:
In your specific example, comments probably aren't necessary. As Caleb mentioned, if the code is clearly written and variables have semantic names, if statements are less likely to need commenting.

Compare your snippet with this:

```<code>if ($x) {
    func1();
} else {
    func2();
}
</code>```

In this case, you would definitely want to use comments to describe what x, func1, and func2 represent (and slap the person who named things by that scheme, especially if it was you). You can't even tell whether $x is supposed to be a boolean. But this too is a case where you don't necessarily need comments, if you're able to refactor and rename.

In general, I like to write comments for logical blocks that describe things the code can't on its own. A one-liner every ~10-20 lines that describes what the following handful of lines accomplish at one higher level of abstraction (e.g. // Make the right amount of magic happen for your example) will help keep you oriented and give a new reviewer insight into what you're doing and when.

I actually often write these one-liners in before I start writing code, so that I don't lose track of the flow that the segment is supposed to have.

Finally, if you really do prefer (or there's a mandate that requires) commenting clauses in an if block regardless of the code's readability, I recommend:

```<code>// Broad description of block
if (something) {
    //Do this because something
    something();
} else {
    //Do this because !something
    somethingElse();
}
</code>```

I feel it's the cleanest, because the comment lines up with the code to which it pertains. A comment describing what code does should be as close to the comment it describes as possible.

Body:
In your specific example, comments probably aren't necessary. As Caleb mentioned, if the code is clearly written and variables have semantic names, if statements are less likely to need commenting.

Compare your snippet with this:

if ($x) {
    func1();
} else {
    func2();
}

In this case, you would definitely want to use comments to describe what x, func1, and func2 represent (and slap the person who named things by that scheme, especially if it was you). You can't even tell whether $x is supposed to be a boolean. But this too is a case where you don't necessarily need comments, if you're able to refactor and rename.

In general, I like to write comments for logical blocks that describe things the code can't on its own. A one-liner every ~10-20 lines that describes what the following handful of lines accomplish at one higher level of abstraction (e.g. // Make the right amount of magic happen for your example) will help keep you oriented and give a new reviewer insight into what you're doing and when.

I actually often write these one-liners in before I start writing code, so that I don't lose track of the flow that the segment is supposed to have.

Finally, if you really do prefer (or there's a mandate that requires) commenting clauses in an if block regardless of the code's readability, I recommend:

// Broad description of block
if (something) {
    //Do this because something
    something();
} else {
    //Do this because !something
    somethingElse();
}

I feel it's the cleanest, because the comment lines up with the code to which it pertains. A comment describing what code does should be as close to the comment it describes as possible.


答:
Comments shouldn't paraphrase the code but explains things which aren't in the code (bigger picture, why, why an alternative hadn't been choosen...) And your example comments is just that: paraphrase of the code.

You may sometimes feel that a paraphrase is needed at the start of the else branch, but that is often a sign that your then branch is too big.


问:
Is it a waste of time to free resources before I exit a process?
Let's consider a fictional program that builds a linked list in the heap, and at the end of the program there is a loop that frees all the nodes, and then exits. For this case let's say the linked list is just 500K of memory, and no special space managing is required.

Is that a waste of time, because the OS will do that anyway?

Will there be a different behavior later?

Is that different according to the OS version?

I'm mainly interested in UNIX based systems, but any information will be appreciated. I had today my first lesson in OS course and I'm wondering about that now.

Edit: Since a lot of people here are concerned about side effects, and general 'good programming practice' and so. You are right! I agree 100% with your statements. But my question is only hypothetical, I want to know how the OS manages this. So please leave out things like 'finding other bugs when freeing all memory'.

答:
One thing to think about:

If your code is in a DLL, then you should clean up for sure otherwise if you assume that your process exit will free the resources then your DLL will not be reloadable.


问:
Difference between Javabean and Java Beans
I'm confused. I've seen two different terms: Javabean and Java Beans. Is there a significant difference between them?

答:
People tend to casually refer to Java classes as "Java beans" in conversations and articles.

When written as one word, however, "JavaBeans" are classes conforming to a particular convention. A JavaBean is a Java Object that is serializable, has a nullary constructor, and allows access to properties using getter and setter methods.  See Wikipedia's article on JavaBeans for more information.

It's also important to note the difference between a Javabean and an EJB.


问:
Basic design of a data access layer using the DataMapper pattern
I'm trying to understand the DataMapper pattern by implementing it myself for one of my domain objects.

So far, I have:

```<code>class MyDomainObject
  <attributes>
  <business logic methods>

class MyDomainObjectMapper
  save
  update
  delete
  get
</code>```

Now, I need get many MyDomainObjects.  These could possibly be held cached in memory (saving many calls to the database).  There could also be filter conditions.

Where does this functionality go, according to the pattern?  I'm not sure if I should add methods to MyDomainObjectMapper or create a new class.

答:
That's what an Identity Map is for.

Ensures that each object gets loaded only once by keeping every loaded object in a map. Looks up objects using the map when referring to them.

For a full description see P of EAA page 195

...An Identity Map keeps a record of all objects that have been read from the database in a single business transaction. Whenever you want an object, you check the Identity Map first to see if you already have it.


问:
If you favor "T *var", do you ever write "T*"?

Possible Duplicate:

  int* i; or int *i; or int * i;

Thinking about where we place our asterisks; how do those that prefer to keep the "pointerness" away from the type and with the identifier (int *i) write code when the identifier is missing?

```<code>void f(int*); // 1
void f(int *); // 2
</code>```

The former seems much more common, no matter what your preference when with the identifier.  Is this a special case?  What makes it an exception?

However, the first still isn't universal, because I have seen the latter style.  Besides consistency along the lines of "there's always a space with the identifier, so we have one without", are there any other reasons to prefer it?

What about casts or array and function types?  How would you re-write these:

```<code>(void*)var /*or*/ (void *)var

int[3] /*or*/ int [3]
// more relevant in C++ than C: Example<int[3]>

void(int) /*or*/ void (int)
// more relevant in C++ than C: std::function<void(int)>
</code>```

The latter two would rarely, if ever, be used in C, but are seen with C++ templates.

答:
Unless specifically instructed otherwise, I tend to follow the C paradigm for stuff like that; namely, T *, T [N], T (*)(), etc., whether I'm writing C or C++, because pointer-ness and array-ness and function-ness are all functions of the declarator, not the type specifier.

However, I will follow the common paradigm for the given language when declaring objects:

```<code>T* foo; // C++
T *bar; // C
</code>```

Is this an inconsistency in my coding style?  Yes.  Is it a big deal?  Not until someone complains about it.

OwnerDisplayName: Roger Pate

Body:
Unless specifically instructed otherwise, I tend to follow the C paradigm for stuff like that; namely, T *, T [N], T (*)(), etc., whether I'm writing C or C++, because pointer-ness and array-ness and function-ness are all functions of the declarator, not the type specifier.

However, I will follow the common paradigm for the given language when declaring objects:

T* foo; // C++
T *bar; // C

Is this an inconsistency in my coding style?  Yes.  Is it a big deal?  Not until someone complains about it.


问:
Where are all the functional programming design patterns?
OO programming literature is full of design patterns. Most books on object oriented programming dedicate a chapter or two to design patterns like factories and decorators. So what are the equivalent patterns in functional languages and why hasn't anyone written a book about them yet? Is there something special about functional languages that obviates the need for design patterns?</p>

答:
Jeremy Gibbons is writing the book. Until it's finished, you can read his blog, Patterns in Functional Programming. He recommends reading his posts from oldest to newest.

Browse his publications as well. He covers Gang of Four patterns in Design Patterns as Higher-Order Datatype-Generic Programs and describes the patterns of programming with recursive equations in Origami Programming (folds and unfolds).


问:
Turning your code inside out (functional style) compared to a OO paradigm
I have find this article Turning Your Code Inside Out and I want to know how this approach described in article is for OO programmers/languages.

Is this style of design used in OO programmers/languages? What's downsides and goodsides of this approach in a OO language?

Update:

OO objects have state and behavior, the design explained in article is stateless. Is not only Single Responsability Principle.

(If I'm talking shit, please explain to me instead of only downside/close votes)

答:
the article is just explaining single responsibility principle

the base line is don't have an object/function do stuff it shouldn't; either use a signal/slots pattern or let the caller handle it


答:
I'm not sure why you separate OO programmers from other programmers. The article is for everybody and all it really says is that your functions should do only one thing and do it well.

There's nothing OO specific about this.

Title: Turning your code inside out (functional style) compared to a OO paradigm

Body:
I have find this article Turning Your Code Inside Out and I want to know how this approach described in article is for OO programmers/languages.

Is this style of design used in OO programmers/languages? What's downsides and goodsides of this approach in a OO language?

Update:

OO objects have state and behavior, the design explained in article is stateless. Is not only Single Responsability Principle.

(If I'm talking shit, please explain to me instead of only downside/close votes)


答:
It is for OO (as well as any other paradigm) because it sets one on the way to loose decoupling. The function the writer was describing can only be used in one environment (i.e. a terminal). Usually, you would want to have modules, functions, or layers that handle a task and only that task. You don't want them either a.) doing something outside of the scope of the task, b.) limiting the future usage of the module to a particular environment or paradigm, or if possible c.) being aware of the other modules.

Furthermore, the function name says what it should do, it shouldn't do anything more than that.

This isn't just OOP by the way, this is just good design.


问:
Does it make sense to write build scripts in C++?
I'm using CMake to generate my projects IDE/makefiles, but I still need to call custom "scripts" to manipulate my compiled files or even generate code.

In previous projects I've been using Python and it was OK, but now I'm having serious trouble managing a lot of dependencies in two very big projects I'm working on so I want to minimize the dependencies everywhere.

Someone suggested to me to use C++ to write my build scripts instead of adding a language dependency just for that. The projects themeselves already use C++ so there are several advantages that I can see:

to build the whole project, only a C++ compiler and CMake would be necessary, nothing else (all the other dependencies are C or C++);

C++ type safety (when using modern C++) makes everything easier to get "correct";

it's also the language I know the better so I'm more at ease with it even if I'm able to write some good Python code;

potential gain in execution speed (but i don't think it will really be perceptible);

However, I think there might be some drawbacks and I'm not sure of the real impact as I didn't try yet:

might be longer to write the code (that said I'm not sure because I'm efficient enough in C++ to write something that work quickly, so maybe for this system it wouldn't be so long to write) (compilation time shouldn't be a problem for this case);

I must assume that all the text files I'll read as input are in UTF-8, I'm not sure it can be easilly checked at runtime in C++ and the language will not check it for you;

libraries in C++ are harder to manage than in scripting languages;

I lack experience and forsight so maybe I'm missing advantages and drawbacks.
So the question is: does it make sense to use C++ for this? do you have experiences to report and do you see advantages and disadvantages that might be important?

答:
I believe this to be case-specific question. I'd claim there's no correct answer whether it makes sense or not to use C++ for build scripts, the only way to figure it out is to try it out in practice.

Personally I'd see Python superior to C++ because of better expressiveness of the language and easier(personal opinion, of course) standard library tools to manage the task of manipulating binary files and generating code. Of course, sophisticated libraries developed for the task might be available, but if not, then I'd personally definitely bet for Python to be the "more often correct" answer - in general case.


问:
Does giving a developer a slower development machine result in faster/more efficient code?
Suppose I give my developers a screaming fast machine.  WPF-based VS2010 loads very quickly.  The developer then creates a WPF or WPF/e application that runs fine on his box, but much slower in the real world.

This question has two parts...

1) If I give a developer a slower machine, does that mean that the resulting code may be faster or more efficient?

2) What can I do to give my developers a fast IDE experience, while giving 'typical' runtime experiences?

Update:

For the record, I'm preparing my even-handed response to management. This isn't my idea, and you folks are helping me correct the misguided requests of my client.  Thanks for giving me more ammunition, and references to where and when to approach this.  I've +1'ed valid use cases such as:

- specific server side programming optimizations

- test labs

- the possibly buying a better server instead of top of the line graphics cards

答: Absolutely not.  Give your Programmers the best laptop money can buy, a keyboard of their choice, multiple great big screens, a private office, no phone, free soft drinks, all the books they want (that are relevent), annual trips to key tech conferences and you'll get great results.  Then test on upper and lower boundary hardware/software/browser/bandwidth combinations.</p>

OwnerDisplayName: addictedtoswdev


答:
Just use Python.

I develop in C++ and do my build scripts in Python, and I would find it painful to do build scripts in C++:

Python makes it trivial to manipulate dictionaries, lists, nested dictionaries of dictionaries of lists, etc.  (For example, one of my scripts uses a multi-level hierarchy of all of my tools, tools' versions, and tools' versions' paths.)  C++ can do the same with templates and custom classes, but it's much more verbose (which translates to more lines of code, which generally translates to lower productivity).

Python provides high-level libraries and routines like its XML and JSON handling, subprocess, and os.walk.  Again, C++ can do this, but it's a lot more work to find the libraries, learn their APIs, correctly assemble the calls (which are often lower level), etc.

Build scripts are a non-value-added activity (to borrow a term from lean).  It's better to use as high-level a language as possible, to get them done as quickly as possible, to get back to work which benefits your users.

In my experience, build scripts tend to grow in unforeseen ways.  Even if a task seems initially simple for C++, it can get complicated in a hurry.  When a new requirement comes up, it's often a lot simpler to tack on handling in a Python script than it is to do it in C++ (which may require finding or reading up on new library APIs, etc.).

Regarding the advantages which you list for C++:

Adding a single dependency (Python) shouldn't significantly complicate your build.  It's already standard on most Linux installations, for example.  Thanks to Python's "batteries included" libraries, it may even be easier to manage than the C++ libraries that your build scripts would depend on.

The type safety that C++ gives is most useful for large projects, not small scripts.

Python complements C++ very well (high-level versus lower-level, dynamically typed versus statically typed, etc.) and can even integrate with C++ very well (thanks to SWIG and Boost.Python) if you later want to do that, so it's worth learning for a C++ programmer.

As you said, execution speed should be a nonissue.

Title: Does it make sense to write build scripts in C++?

Body:
I'm using CMake to generate my projects IDE/makefiles, but I still need to call custom "scripts" to manipulate my compiled files or even generate code.

In previous projects I've been using Python and it was OK, but now I'm having serious trouble managing a lot of dependencies in two very big projects I'm working on so I want to minimize the dependencies everywhere.

Someone suggested to me to use C++ to write my build scripts instead of adding a language dependency just for that. The projects themeselves already use C++ so there are several advantages that I can see:

to build the whole project, only a C++ compiler and CMake would be necessary, nothing else (all the other dependencies are C or C++);

C++ type safety (when using modern C++) makes everything easier to get "correct";

it's also the language I know the better so I'm more at ease with it even if I'm able to write some good Python code;

potential gain in execution speed (but i don't think it will really be perceptible);

However, I think there might be some drawbacks and I'm not sure of the real impact as I didn't try yet:

might be longer to write the code (that said I'm not sure because I'm efficient enough in C++ to write something that work quickly, so maybe for this system it wouldn't be so long to write) (compilation time shouldn't be a problem for this case);

I must assume that all the text files I'll read as input are in UTF-8, I'm not sure it can be easilly checked at runtime in C++ and the language will not check it for you;

libraries in C++ are harder to manage than in scripting languages;

I lack experience and forsight so maybe I'm missing advantages and drawbacks.
So the question is: does it make sense to use C++ for this? do you have experiences to report and do you see advantages and disadvantages that might be important?


问:
How do I share common classes between windows forms and web applications using C#?
In our environment we have multiple ERP servers plus data that are coming from multiple sources. I need to create a development roadmap for the coming years as it is obvious that side applications will be needed for many things.

The choice is that the development will occur with Microsoft technologies. This means that I will be building either web apps (MVC, web forms e.t.c) or standard windows forms applications.

The thing here is that I will be creating classes that will encapsulate the business logic that I want to apply to the different projects.

What I thought so far:

1) Create class libraries dlls containing the required logic

2) Reuse these libs in my apps.

But what about the web apps? Should I use the dlls directly or I should encapsulate them in web services and consume the web services in web apps? In general I would like to find out how should I build my strategy.

答:
But what about the web apps? Should i use the dlls directly or i should encapsulate them in web services and consume the web services in web apps?

It really end up depending on how much you will need to scale.

I would say that you should start with simply reusing the libraries in your web applications directly. This has least cost in terms of speed of development, simplicity and low IO overhead (speaking about network traffic), as well as having the benefit of running in-process.

Using web services is an expensive proposition (extra hardware, maintenance costs, extra software layer, IO overheads etc...) and will be slower than going in-proc.


答:
This sounds like the ideal scenario for a Service Oriented Architecture using web services.

Encapsulate any discrete units of business knowledge, functionality that could be shared between one or more services, and allow other apps to use them.

This centralising of functionality means all apps can be updated if the central business functionality changes in some way.

If some change in the interface is to take place, you have to be aware that all consuming apps will be affected, but there are ways around this with versioning etc.

Title: How do I share common classes between windows forms and web applications using C#?

Body:
In our environment we have multiple ERP servers plus data that are coming from multiple sources. I need to create a development roadmap for the coming years as it is obvious that side applications will be needed for many things.

The choice is that the development will occur with Microsoft technologies. This means that I will be building either web apps (MVC, web forms e.t.c) or standard windows forms applications.

The thing here is that I will be creating classes that will encapsulate the business logic that I want to apply to the different projects.

What I thought so far:

1) Create class libraries dlls containing the required logic

2) Reuse these libs in my apps.

But what about the web apps? Should I use the dlls directly or I should encapsulate them in web services and consume the web services in web apps? In general I would like to find out how should I build my strategy.


问:
Tool to identify potential reviewers for a proposed change
Is there a tool that takes as input a proposed patch and a git repository, and identifies the developers are the best candidates for reviewing the patch? It would use the git history to identify the authors that have the most experience with the files / sections of code that are being changed.

Edit: The use case is a large open source project (OpenStack Compute), where merge proposals come in, and I see a merge proposal on a chunk of code I'm not familiar with, and I want to add somebody else's name to the list of suggested reviewers so that person gets a notification to look at the merge proposal.

答:
I'm not aware of any such tool, but a baseline one is easy enough to write:

```<code>(for file in $files_touched_by_patch; do
    git blame -p "$file"
done) \
  | grep '^author ' | sed 's/^author *//' \
  | sort | uniq -c | sort -nrk 1
</code>```

will list the people who last touched $files_touched_by_patch, sorted by how many of their lines of code survive in the current version.

Body:
I'm not aware of any such tool, but a baseline one is easy enough to write:

(for file in $files_touched_by_patch; do
    git blame -p "$file"
done) \
  | grep '^author ' | sed 's/^author *//' \
  | sort | uniq -c | sort -nrk 1

will list the people who last touched $files_touched_by_patch, sorted by how many of their lines of code survive in the current version.


问:
Is there a sequence to read through the Android developer site for a user new to Android?
To summarize: I can buy a book, people say "Just read the developer site", I like to be spoon-fed the early learning piece for a technology, what path through the developer site will give me that spoonfeeding I need (that is, examples tied directly to the section being discussed, a clear path, etc.).

I keep seeing that I don't need to buy an Android development book, that I should just read the Android developer site, because it has everything I need to know. I see it more as drinking from a fire hose.

But I'm one of those people who likes to be walked through the basics. I like to build up my knowledge, rather than being dropped into reference documentation. I also like to make sure I have see all/most of the topics covered. I'd hate to develop the wrong thing, because I don't know about Fragments, Content Providers, or whatever.

So, since it's a great resource, better than any book (we don't need no stinking books), how do I traverse the site to get the information provided in the same way as a book would lay it out?

答:
I'm one of those people who likes to be walked thru the basics. I like to build up my knowledge, rather than being dropped into reference documentation. I also like to make sure I have see all/most of the topics covered. I'd hate to develop the wrong thing because I don't know about Fragments, Content Providers, or whatever.

  ...how do I traverse the site to get the information provide in the same way as a book would lay it out?

The read through sequence that looks close to learning path that worked well for me few years ago is about as follows:

Open tab Dev Guide. Note the opening statement at its very beginning says:

The Dev Guide provides a practical introduction to developing applications for Android...

Read topics listed in left column one-by-one, from top to bottom

Note dealing with hyperlinks in Dev Guide topics might be tricky. For a first read-through I would avoid side-stepping to study these, reading through as if it's plain text - but this is rather a matter of personal preference.

  While it looks obvious when you see the arrows and the relevant tab opened at above screen shot, it's not automatically clear when a newbie developer first time lands right at the http://developer.android.com.

This just suggests I drink from the fire hose...

Well thing is, introductory guides like that are designed to allow for fast study; if you feel you can't read through fast, you better learn how to do that.

One can use this very Android guide to practice that skill. Set a reasonable time frame to complete reading - say something like one week or less. Set a realistic goal - "to become an expert in 3 days" won't do. Aim for getting a big picture, for understanding where to look for details on various topics one may need in future - stuff like that. Then, well, start reading guide topics, one-by-one, from top to bottom - trying to keep up with time frame set.

It is very unlikely that Android will be the only technology you'll need to learn through your career. There will be many others ahead.

Being able to recognize beginner's guides and quickly read through these will bring you a serious advantage in the long run. This will let you to quickly start working with new technology while other guys around will still be complaining about fire hoses and waste time looking for a royal road which indeed lies in front of them.


问:
What is a good way to comment if-else-clauses?
Whenever I'm writing a typical if-else-construct in any language I wonder what would be the best way (in terms of readability and overview) to add comments to it. Especially when commenting the else clause the comments always feel out-of-place for me. Say we have a construct like this (examples are written down in PHP):

```<code>if ($big == true) {
    bigMagic();
} else {
    smallMagic()
}
</code>```

I could comment it like this:

```<code>// check, what kind of magic should happen
if ($big == true) {
    // do some big magic stuff
    bigMagic();
} else {
    // small magic is enough
    smallMagic()
}
</code>```

or

```<code>// check, what kind of magic should happen
// do some big magic stuff
if ($big == true) {
    bigMagic();
}
// small magic is enough
else {
   smallMagic()
}
</code>```

or

```<code>// check, what kind of magic should happen
// if:   do some big magic stuff
// else: small magic is enough
if ($big == true) {
    bigMagic();
} else {
    smallMagic()
}
</code>```

What are your best-practice examples for commenting this?

答:
```<code>if (IsWeekDay(day))
{// weekday -> alarm at 7am
   ...
}
else if(day == DayOfWeek.Saturday)
{// saturday -> alarm at 11am
   ...
}
else
{// (sunday) -> no alarm
   ...
}
</code>```

I keep my brackets lined up and put it right after the bracket.

[Condition] -> [pseudo-code]

On an else, it technically means all other conditions failed, so I usually use parentheses.

([Condition]) -> [pseudo-code]

Note: this is for C#.

Body:
if (IsWeekDay(day))
{// weekday -> alarm at 7am
   ...
}
else if(day == DayOfWeek.Saturday)
{// saturday -> alarm at 11am
   ...
}
else
{// (sunday) -> no alarm
   ...
}

I keep my brackets lined up and put it right after the bracket.

[Condition] -> [pseudo-code]

On an else, it technically means all other conditions failed, so I usually use parentheses.

([Condition]) -> [pseudo-code]

Note: this is for C#.


问:
An XML file or Database?
I am re-writing a section of my site and am trying to decide how much of a rewrite this will be. At the moment I have a web service feed that generates an xml once per day. I then use this xml file on my website to generate the general structure. I am trying to decide if this information should be located in the database or stay in the xml file.

The file can range from 4mb - 12mb.

The files depth can go on and on so I have to recurse to find the data I want.

I use the .NET serializer classes and store the serialized file in a global variable to avoid re-serializing it each time the page is loaded.

My reasons for thinking a database would be better are:

I would know exactly where I am in the file by using an internal ID so I wouldn't have to recurse the file to get information.

I wouldn't have to load / serialize the XML and could just use my already open database connections.

Searching for the data in the file would be quicker(?) as I would just perform an SQL query rather than re-cursing the file.

Has anyone got any ideas which is better and which option uses more resources on the server or be quicker?

EDIT: The file is read every time the web page is loaded (although only serialized once). It isn't written to by standard users (only by an admin task that runs in the middle of the night).

This is my initial investigation before mocking up.

答:
If you talk about cost and resources then consuming a web service is more costly than fetching data from a local or server hosted database. </p>

<p>In your scenario, you also need to keep in view that whether your data is being updated via XML file data or not. If it does then also look into when that happens? By what time period you need to fetch the data from XML(say every 5 mins, 10 mins, 1 hour, 1 year)? </p>

Title: An XML file or Database?

Body:
If you talk about cost and resources then consuming a web service is more costly than fetching data from a local or server hosted database.

In your scenario, you also need to keep in view that whether your data is being updated via XML file data or not. If it does then also look into when that happens? By what time period you need to fetch the data from XML(say every 5 mins, 10 mins, 1 hour, 1 year)?


答:
I'm partly echoing what jberger has already said, it seems you've already talked yourself into doing it.

I'd be in favour of moving over to a database, twelve megs is quite a large file. As a series of tables you'd be able to use some of the power of a database to speed up queries, maybe do new things that you've found tedious or laborious before. Again other posters have asked how often are you querying this file. If it's once a day then maybe, just maybe, it might not be worth the effort. However, if it's a complex bit of XML, and 12 MB says it is, then you might find it's time well spent.

I'm not dismissing the idea of using XML files as datastores, I've used them in the past on websites, but then their size and the traffic on those websites mean it was worthwhile. The websites don't get many visitors a month, and the size of the XML files used are measured in KBs.

Title: An XML file or Database?

Body:
I am re-writing a section of my site and am trying to decide how much of a rewrite this will be. At the moment I have a web service feed that generates an xml once per day. I then use this xml file on my website to generate the general structure. I am trying to decide if this information should be located in the database or stay in the xml file.

The file can range from 4mb - 12mb.

The files depth can go on and on so I have to recurse to find the data I want.

I use the .NET serializer classes and store the serialized file in a global variable to avoid re-serializing it each time the page is loaded.

My reasons for thinking a database would be better are:

I would know exactly where I am in the file by using an internal ID so I wouldn't have to recurse the file to get information.

I wouldn't have to load / serialize the XML and could just use my already open database connections.

Searching for the data in the file would be quicker(?) as I would just perform an SQL query rather than re-cursing the file.

Has anyone got any ideas which is better and which option uses more resources on the server or be quicker?

EDIT: The file is read every time the web page is loaded (although only serialized once). It isn't written to by standard users (only by an admin task that runs in the middle of the night).

This is my initial investigation before mocking up.