问:
Git submodule vs Git clone
I am working on an open source project on GitHub.

It has a subdirectory /Vendor in which it has a copy of several external libraries. Original maintainer of the project  updated this directory with newer copy of external library once in a while.

One developer send me a pull request with idea to replace this copy by git submodule.

And I am considering whether it's good idea or not.

Git submodule Pros:

Submodules were specifically designed for similar scenarios

It removes possibility of accidental commit to Vendor which will be overwritten while next update

Git submodule Cons:

It looks like git submodules pushes complexity from maintainer to a person who will clone/pull the project (additional steps required after you clone to start working with the project: "git submodule init", "git submodule update"

What's your opinion on this?

One more thing. This problem is reasonably small size library with very limited external dependencies. I think any build tool would be overkill for it for now.

答:
This is an ideal place to use submodules. They reduce the size and complexity of your repository and make it easier to update the external libraries to a new version.

They're not hard to figure out how to use, and they are fairly commonly used in this situation, so just make a note in the project's README that you make use of submodules and what to do, and people ought to be able to figure it out. The first time I encountered a repository with a submodule I got it up and running in 10 to 15 minutes and have never had an issue with figuring out what to do since.

Title: Git submodule vs Git clone


问:
How cache works in ServiceStack web services
I am new to caching and trying to understand how it works in general. Below is code snippet from ServiceStack website.

```<code>public object Get(CachedCustomers request)
{
    //Manually create the Unified Resource Name "urn:customers".
    return base.RequestContext.ToOptimizedResultUsingCache(base.Cache, "urn:customers", () =>
    {
         //Resolve the service in order to get the customers.
         using (var service = this.ResolveService<CustomersService>())
                return service.Get(new Customers());
     });
}

public object Get(CachedCustomerDetails request)
{
    //Create the Unified Resource Name "urn:customerdetails:{id}".
    var cacheKey = UrnId.Create<CustomerDetails>(request.Id);
    return base.RequestContext.ToOptimizedResultUsingCache(base.Cache, cacheKey, () =>
    {
        using (var service = this.ResolveService<CustomerDetailsService>())
        {
             return service.Get(new CustomerDetails { Id = request.Id });
        }
    });
}
</code>```

My doubts:

I've read that cached data is stored on same/distributed server RAM. So, how much data can it handle, suppose in first method if customers count is more than 1 million, doesn't it occupy too much memory.

Ingeneral, do we apply caching only for GET operations and invalidate if it gets UPDATE'd.

Please suggest any tool to check memory consumption of caching.

答:
Please read ServiceStack's wiki page on Caching.

What memory gets used is entirely determined by the Caching provider used, e.g. if you use a distributed cache like Redis or Memcached the memory of the process of that daemon/service will grow to retain the cached data. If you use ServiceStack's MemoryCachedClient (the Default) then caches are just kept in memory, i.e. in the ASP.NET w3wp.exe process.

How big the cache is, is directly effected by what is cached.

Your caching strategy you choose is independent of the caching providers, the example provided uses a caching pattern that lazily creates the cache when fetched and invalidates it when changed.

ContentLicense: CC BY-SA 3.0


问:
How to suggest using an ORM instead of stored procedures?
I work at a company that only uses stored procedures for all data access, which makes it very annoying to keep our local databases in sync as every commit we have to run new procs.  I have used some basic ORMs in the past and I find the experience much better and cleaner.  I'd like to suggest to the development manager and rest of the team that we look into using an ORM Of some kind for future development (the rest of the team are only familiar with stored procedures and have never used anything else).  The current architecture is .NET 3.5 written like .NET 1.1, with "god classes" that use a strange implementation of ActiveRecord and return untyped DataSets which are looped over in code-behind files - the classes work something like this:

```<code>class Foo {
    public bool LoadFoo() {
        bool blnResult = false;
        if (this.FooID == 0) {
            throw new Exception("FooID must be set before calling this method.");
        }

        DataSet ds = // ... call to Sproc
        if (ds.Tables[0].Rows.Count > 0) {
            foo.FooName = ds.Tables[0].Rows[0]["FooName"].ToString();
            // other properties set
            blnResult = true;
        }
        return blnResult;
    }
}

// Consumer
Foo foo = new Foo();
foo.FooID = 1234;
foo.LoadFoo();
// do stuff with foo...
</code>```

There is pretty much no application of any design patterns.  There are no tests whatsoever (nobody else knows how to write unit tests, and testing is done through manually loading up the website and poking around).  Looking through our database we have:  199 tables, 13 views, a whopping 926 stored procedures and 93 functions.  About 30 or so tables are used for batch jobs or external things, the remainder are used in our core application.

Is it even worth pursuing a different approach in this scenario?  I'm talking about moving forward only since we aren't allowed to refactor the existing code since "it works" so we cannot change the existing classes to use an ORM, but I don't know how often we add brand new modules instead of adding to/fixing current modules so I'm not sure if an ORM is the right approach (too much invested in stored procedures and DataSets).  If it is the right choice, how should I present the case for using one?  Off the top of my head the only benefits I can think of is having cleaner code (although it might not be, since the current architecture isn't built with ORMs in mind so we would basically be jury-rigging ORMs on to future modules but the old ones would still be using the DataSets) and less hassle to have to remember what procedure scripts have been run and which need to be run, etc. but that's it, and I don't know how compelling an argument that would be.  Maintainability is another concern but one that nobody except me seems to be concerned about.

答:
I'm in a similar situation, actually our hardware, power and politics lean to the database side, so everything goes through a stored procedure.  Unfortunately, they are a pain for a coder, especially when it comes to metadata and code generation, as there isn't as rich meta data in stored procedures as tables.

Regardless, you can still write elegant, and clean code using stored procs.  I'm currently implementing the Repository pattern myself with all stored procedures.  I'd suggest looking at FluentAdo.net for his brilliant idea when mapping from datareader back to your business objects.  I took a piece of that idea, and repurposed it for my homegrown solution.


问:
Prevent developers from using constants
I have one one software system which allows developers to specify an ID or name to create NodeReferences. Both work fine, but ID's are not guaranteed to be the same across different environments. I've tried in documentation and in conversations to stress that ID's should not be hard coded (since they will break when deployed to a different environments), but some developers are still using them.

This works only in dev environment:

```<code>var level = LevelReference.ById(20);
var node = NodeReference.ByName(level, _patientGuid.ToString());
</code>```

This works in all environments:

```<code>var app = ApplicationReference.ByName("Reporting");
var area = AreaReference.ByName(app, "Default");
var level = LevelReference.ByName(area, "Patient");
var node = NodeReference.ByName(level, _patientGuid.ToString());
</code>```

I think the appeal is just that it produces fewer lines of code. The use of ID's is not by itself bad (since there are valid use cases like caching the ID returned from the server and using it later for faster look-up), but hard-coded ID's are bad. Most of the time the first code will throw an exception, but it's possible that it could be a valid ID for a different object than the developer intended, and this could result in very bad problems.

What's the best way to discourage the use of such constants in code? Ideally I'd like throw some kind of compiler error when I see code like the first example, or at least throw an exception before the call gets down to the database.

答:
It appears you are trying to find a technical solution to a social problem.  You should call it out in code reviews.  Do something in your testing/staging environment to ensure that node numbers will never match a clean dev environment - perhaps insert a random number of garbage rows on each install - and throw it back to the dev as broken.

If people insist on trying to work around this, you let them go.

Title: Prevent developers from using constants


答:
Using submodules prevents you from making local changes to the code & creates a dependency on the external repository.  If you're sure you will never want to customize a library or make local bug fixes and can be sure that the external server will always be available when you want to clone a new copy they're the way to go.

In short - do you want to simply use the libs or do you consider them part of your codebase?  If they're not 'your' code, why are they in version control and not just something you require to have installed?

ContentLicense: CC BY-SA 4.0

Title: Git submodule vs Git clone

Body:
I am working on an open source project on GitHub.

It has a subdirectory /Vendor in which it has a copy of several external libraries. Original maintainer of the project  updated this directory with newer copy of external library once in a while.

One developer send me a pull request with idea to replace this copy by git submodule.

And I am considering whether it's good idea or not.

Git submodule Pros:

Submodules were specifically designed for similar scenarios

It removes possibility of accidental commit to Vendor which will be overwritten while next update

Git submodule Cons:

It looks like git submodules pushes complexity from maintainer to a person who will clone/pull the project (additional steps required after you clone to start working with the project: "git submodule init", "git submodule update"

What's your opinion on this?

One more thing. This problem is reasonably small size library with very limited external dependencies. I think any build tool would be overkill for it for now.


问:
When should JavaScript generate HTML?
I try to generate as little HTML from JavaScript as possible. Instead, I prefer to manipulate existing markup whenever I can and only generate HTML when I need to dynamically insert an element that isn't a good candidate for using Ajax. This, I believe, makes it far easier to maintain the code and quickly make changes to it because the markup is easier to read and trace. My rule of thumb is: HTML is for document structure, CSS is for presentation, JavaScript is for behavior.

However, I've seen a lot of JS code that generates mounds of HTML, including entire forms and content-heavy modal dialogs. In general, which method is considered best practice? In what circumstances should JavaScript be used to generate HTML and when should it not?

答:
I am generating html code in jquery because I'm using a portlet and after the jsp code execution, I need to make a loop with html code, that I can't get in a java for loop with some javascript code within.
So I am rendering java arraylist in a javascript array and the using the strings to generate html.</p>

Title: When should JavaScript generate HTML?


问:
What's the protocol for a autoexecuting JQuery plugin?
I have a jQuery Plugin that I use myself which modifies the selected value of select items on a page.

In my own code the plugin automatically executes as soon as it is included in the page this code in the plugin file.

```<code>jQuery('Document').ready(function(){
jQuery('select').SelectOptions();
});
</code>```

this automatically executes my plugin on all select items on the page.

Obviously for people other than myself who might come across this on github I'd like to change this slightly.  I still think the idea of auto execute is  good but my question is.

Don't setup autoexecute at all, let the developer call the code.

Call autoexecute on All Select items and let the developer modify the code if he doesnt want that.

Autoexecute, but modify my selector called in the plugin js file to modify only select tags with a particular class - such as below

.

```<code>jQuery('Document').ready(function(){
jQuery('select .selectoptions').SelectOptions();
});
</code>```

Is there any established protocol for this?

答:
The plugin usually specifies a default behavior and also accepts a selector, e.g:

```<code>jQuery().selectOptions() // default selector 'select'
jQuery( '.selectOptions' ).selectOptions() // Custom selector, gives the user more control.
</code>```

I would not recommend automatically executing under any circumstances.

Body:
The plugin usually specifies a default behavior and also accepts a selector, e.g:

jQuery().selectOptions() // default selector 'select'
jQuery( '.selectOptions' ).selectOptions() // Custom selector, gives the user more control.

I would not recommend automatically executing under any circumstances.


问:
Where can I turn to if I can't fix a bug?
I am looking for resources to turn to when I don't have the answer for something. I lead a team of software developers. We have been rolling out new software releases on a monthly basis.

When there is a bug that my team cannot fix, it lands on me. Most of the time I can resolve the issue, but there are times when I get stuck.

Unfortunately, I'm the top of the line at our company. There is nobody I can ask for help or assistance in figuring something out.

Do you happen to have any recommendations or guidance for situations like this?

答:
Define "Can't".....

In addition to the excellent answer by @Karl, A change the attitude might be useful. Nothing, absolutely nothing, is "unfixable". I have spend weeks hunting down issues in the past. I have given up on some for sure, but I have never said "this is unfixable, I can't do it", always put a reason behind the "I cannot" - "". Make sure your "I cannot" is not "I don't want to" or "I can't be bothered", "I'm bored"....... I that is case, it is what it is, be honest about it (at least to yourself).  Not everyone is cut out to be a maintenance programmer - in my current job I can go weeks and write a few lines of useful code, so if you measure success buy SLOC, you will struggle.

Occasionally I say "I am not a good enough programmer to fix this". That is a major step - being prepared to admit that you are not good enough means being prepared to ask for help. Park you ego and ask, you are a better programmer if you can.


问:
How many are too many nested function calls?
Quoted from MSDN about StackOverflowException:

The exception that is thrown when the execution stack overflows because it contains too many nested method calls.

Too many is pretty vague here. How do I know when too many is really too many? Thousands of function calls? Millions? I assume that it must be related in some way to the amount of memory in the computer but is it possible to come up with a roughly accurate order of magnitude?

I'm concerned about this because I am developping a project which involves a heavy use of recursive structures and recursive function calls. I don't want the application to fail when I start using it for more than just small tests.

答:
By default, the CLR allocates 1 MB to the stack for each thread (see this article).  So, it's however many calls it takes to exceed this amount.  That will vary depending on how much space on the stack each call is using for things like parameters and local variables.

You can even make it throw a StackOverflowException with a single call if you're willing to be a bit unorthodox:

```<code>private static unsafe void BlowUpTheStack()
{
    var x = stackalloc byte[1000000000];
    Console.WriteLine("Oh no, I've blown up the stack!");
}
</code>```

Body:
By default, the CLR allocates 1 MB to the stack for each thread (see this article).  So, it's however many calls it takes to exceed this amount.  That will vary depending on how much space on the stack each call is using for things like parameters and local variables.

You can even make it throw a StackOverflowException with a single call if you're willing to be a bit unorthodox:

private static unsafe void BlowUpTheStack()
{
    var x = stackalloc byte[1000000000];
    Console.WriteLine("Oh no, I've blown up the stack!");
}


答:
I'm concerned about this because I am developping a project which involves a heavy use of recursive structures and recursive function calls. I don't want the application to fail when I start using it for more than just small tests.

Unless your language environment supports tail call optimization (and your recursion is a tail call), a basic rule of thumb is: recursion depth should be guaranteed to be O(log n), i.e. using algorithms or data structures based on divide-and-conquer (like trees, most sorting alogorithms, etc.) is OK, but anything linear (like recursive implementations of linked list handling) is not.

Title: How many are too many nested function calls?

Body:
Quoted from MSDN about StackOverflowException:

The exception that is thrown when the execution stack overflows because it contains too many nested method calls.

Too many is pretty vague here. How do I know when too many is really too many? Thousands of function calls? Millions? I assume that it must be related in some way to the amount of memory in the computer but is it possible to come up with a roughly accurate order of magnitude?

I'm concerned about this because I am developping a project which involves a heavy use of recursive structures and recursive function calls. I don't want the application to fail when I start using it for more than just small tests.


问:
Do you estimate all user stories in iteration zero?
After our product backlog is created and prioritized, are we meant to briefly estimate all the stories in the product backlog?  I assume they have to be in order to create a product burndown chart, however if you have a lot of stories this could take a long time initially.

Additionally, should a user story have acceptance criteria when added to the backlog or are those criteria added when the story transfers from the product backlog to the sprint backlog? It would be harder to estimate without them.

答:
All of these suggestions seem to make a pretty big assumption - that user stories can be prioritized at all without estimations.  I've never worked in an environment where that is possible - maybe huge companies who have tons of money to burn.  To me, it seems like the quickest way to burn through VC money... trying to build the most important / impossible things first without also building some low-hanging fruit that can help trickle in some money.

We use the following method:

Estimate what you know

Refuse to estimate what you don't and call it a spike

Tell management that at least 1 spike must be performed.  The outcome / deliverable of a spike is either an estimation for the story (or stories) the spike was created for or some sort of email, document, powerpoint, etc indicating the lessons learned and the suggested next steps for the next spike.

After 2, 3, or more spikes for a business requirement, it quickly becomes apparent to the entire team and management that this is a risky area full of uncertainty.  And, since management doesn't like things that don't have estimates, they tend to prioritize spikes... they want their estimates.  Either way, it works out well - all of the high-risk and uncertain stories start bubbling to the beginning of the release and so everyone gets a much clearer picture early on about what is realistic and what isn't.

Even better, leadership learns quickly that spikes are not commitments since they are not estimated and our teams don't commit to things that aren't estimated.

The final step - a critical path for various components of the application (UI, Database, Services, etc)... this means spikes and stories.  I know this may seem waterfallish and you don't need to do a full systems engineering critical path, but just a quick "this prevents or blocks this from being worked on".  You need to quickly identify what can be done in parallel, what is blocked, etc.  This really helps define release plans, sprint themes, and milestones.


答:
And I tend to disagree with the shared sentiment here that entire backlogs should not be estimated or how it can be misleading.

In my experience, if estimating the entire backlog is a chore or misleading, the backlog is too big or the team isn't good at estimating or the team just doesn't know, the release is too ambitious, etc.  We shoot for 3 month releases (6 sprints).

We also have two definitions of done... the one most people are familiar with and the "validated definition of done".  The one most of you are probably familiar with is pretty lax for us.  We need something just done enough to put it out there, get feedback, and learn from it.  Typically, this is your product owner "accepting" the story.  However, we've learned very early on that product owners make poor proxies for users and so we don't "validate" a story until its been put through the test of real end users.  Once we feel comfortable, haven't received any glaring negative feedback or been through some usability testing, we'll mark it "validated".  Once validated, this means we should shore it up with some automated UI testing, quality controls, etc.  We want to etch this user story in stone - our product owner said it met the business requirement and our users don't have a problem with it either.

I've heard of other cases where even if the users don't have a problem with it, it might never be validated so it actually gets put into an "scrapped code" repository.  If it won't help the app sell, users never use it, users don't necessarily love it... why spend the resources to shore it up, maintain it, etc.

Title: Do you estimate all user stories in iteration zero?

Body:
After our product backlog is created and prioritized, are we meant to briefly estimate all the stories in the product backlog?  I assume they have to be in order to create a product burndown chart, however if you have a lot of stories this could take a long time initially.

Additionally, should a user story have acceptance criteria when added to the backlog or are those criteria added when the story transfers from the product backlog to the sprint backlog? It would be harder to estimate without them.


问:
Business Objects - Containers or functional?
This is a question I asked a while back on SO, but it may get discussed better here...

Where I work, we've gone back and forth on this subject a number of times and are looking for a sanity check. Here's the question: Should Business Objects be data containers (more like DTOs) or should they also contain logic that can perform some functionality on that object.

Example - Take a customer object, it probably contains some common properties (Name, Id, etc), should that customer object also include functions (Save, Calc, etc.)?

One line of reasoning says separate the object from the functionality (single responsibility principal) and put the functionality in a Business Logic layer or object.

The other line of reasoning says, no, if I have a customer object I just want to call Customer.Save and be done with it. Why do I need to know about another class to save a customer if I'm consuming the object?

Our last two projects have had the objects separated from the functionality, but the debate has been raised again on a new project.

Which makes more sense and why??

答:
The answer is really going to depend on what your architecture/design is - a DDD architecture with a domain model is going to be very different from a CRUD data-centric model when it comes to object design.

In general though, bear in mind that in object-orientated-design you are trying to encapsulate state and expose behaviour. This means that you will usually try to get the state associated with a behaviour as close as possible to that behaviour - when you fail to do this you are forced to expose state in one form or another.

In a domain model you want to separate out the business model from infrastructure concerns - so you absolutely want to avoid methods like '.Save'. I don't remember the last time I "saved" a customer in real life!

In a CRUD application then the data is the first class citizen so a ".Save" is entirely appropriate.

Most sizable real-world applications will have a mixture of these paradigms - domain model where there are rapidly changing or complex business rules, DTOs for transferring data across boundaries, CRUD (or something between CRUD and a domain model, such as activerecord) elsewhere. There is no one-size fits all rule.

ContentLicense: CC BY-SA 2.5


问:
Do not declare interfaces for immutable objects
Do not declare interfaces for immutable objects

[EDIT] Where the objects in question represent Data Transfer Objects (DTOs) or Plain Old Data (PODs)

Is that a reasonable guideline?

Up to now, I've often created interfaces for sealed classes that are immutable (data cannot be changed). I've tried to be careful myself to not use the interface anywhere where I care about immutability.

Unfortunately, the interface begins to pervade the code (and it's not just my code I'm worried about). You wind up being passed an interface, and then wanting to pass it to some code that really wants to assume that the thing being passed to it is immutable.

Because of this problem, I'm considering never declaring interfaces for immutable objects.

This might have ramifications with respect to Unit Testing, but other than that, does this seem a reasonable guideline?

Or is there another pattern I should be using to avoid the "spreading-interface" problem I'm seeing?

(I'm using these immutable objects for several reasons: Mainly for thread safety since I write a lot of multi-threaded code; but also because it means I can avoid making defensive copies of objects passed to methods. Code becomes a lot simpler in many cases when you know something is immutable - which you don't if you've been handed an interface. In fact, often you can't even make a defensive copy of an object referenced via an interface if it doesn't provide a clone operation or any way of serialising it...)

[EDIT]

To provide a lot more context for my reasons for wanting to make objects immutable, see this blog post from Eric Lippert:

Link

I should also point out that I'm working with some lower-level concepts here, such as items that are being manipulated/passed around in multi-threaded job queues. These are essentially DTOs.

Also Joshua Bloch recommends the use of immutable objects in his book Effective Java.

Follow Up

Thanks for the feedback, all. I've decided to go ahead and use this guideline for DTOs and their ilk. It's working well so far, but it's only been a week... Still, it's looking good.

There are some other issues relating to this that I want to ask about; notably something I'm calling "Deep or Shallow Immutability" (nomenclature I stole from Deep and Shallow cloning) - but that's a question for another time.

答:
I used to make a big fuss about making my code invulnerable to misuse. I made read-only interfaces for hiding mutating members, added lots of constraints to my generic signatures, etc., etc. It turned out that most of the time I made design decisions because I didn't trust my imaginary co-workers. "Perhaps someday they'll hire a new entry-level guy and he won't know that class XYZ can't update DTO ABC. Oh no!" Other times I was focusing on the wrong problem - ignoring the obvious solution - not seeing the forest through the trees.

I never create interfaces for my DTOs anymore. I work under the assumption that the people touching my code (primarily myself) know what is allowed and what makes sense. If I keep making the same stupid mistake, I usually don't try to harden my interfaces. Now I spend most of my time trying to understand why I keep making the same mistake. It's usually because I'm over-analyzing something or missing a key concept. My code has been much easier to work with since I gave up being paranoid. I also end up with fewer "frameworks" that required an insider's knowledge to work on the system.

My conclusion has been to find the simplest thing that works. The added complexity of making safe interfaces just wastes development time and complicates otherwise simple code. Worry about things like this when you have 10,000 developers using your libraries. Believe me, it will save you from a lot of unnecessary tension.

OwnerDisplayName: Matthew Watson


问:
Avoiding ubiquitous language clashes
I have been reading DDD Quickly and wondered about how to avoid naming clashes with technical terms and domain terms. For example, if I commonly used the repository pattern (with classes such as AddressRepository), but a customer also has something called a repository in their domain. How would I best avoid confusing the two?</p>

答:
In most of cases, when a clash exists you can find with the domain expert a perfect synonymous that removes the ambiguity.

When no synonymous exist, the problem is not about the code base since the compiler knows about namespaces (and btw no repository should be in the same module of the domain), but about developers' cognitive load.

Indeed, when new developers join the project, they will have to learn about the "different" meaning of "that one term" and they will take some time to become able to understand from the context if you are talking about the technical concept or the domain's one (and believe me, this is both annoying and expensive).

Thus, I suggest to

Find a satisfying synonymous with the domain expert

If (and only if) no synonymous exist

use the term from the ubiquitous language as it is (Repository, in your case) for the domain concept

use the naming convention that developers already adopt for the technical concept (AddressRepository here) or you will have to spend time to explain the exception a lot of times!

This might look too pragmatic, but works well.

Title: Avoiding ubiquitous language clashes


问:
Is memory management in programming becoming an irrelevant concern?
Background

I revisited an old (but great) site I had not been to for ages - the Alioth Language Shootout (http://benchmarksgame.alioth.debian.org/).

I started out programming in C/C++ several years ago, but have since then been working almost exclusively in Java due to language constraints in the projects I have been involved in. Not remembering the figures, I wanted to see, approximately, how well Java fared against C/C++ in terms of resource usage.

The execution times were still relatively good, with Java at worst performing 4x slower than C/C++, but on average around (or below) 2x. Due to the nature of the implementation of Java itself, this was no surprise, and it's performance time was actually lower than what I expected.

The real brick was the memory allocation - at worst, Java allocated:

a whopping 52x more memory than C

and 25x more than C++.

52x the memory ... Absolutely nasty, right? ... or is it?  Memory is comparatively cheap now.

Question:

If we do not speak in terms of target platforms with strict limits on working memory (i.e. embedded systems and the like), should memory usage be a concern when picking a general purpose language today?

I am asking in part because I am considering migrating to Scala as my primary language. I very much like the functional aspects of it, but from what I can see it is even more expensive in terms of memory than Java. However, since memory seems to be getting faster, cheaper and more plentiful by the year (it seems to be increasingly hard to find a consumer laptop without at least 4GB of DDR3 RAM), could it not be argued that resource management is becoming increasingly more irrelevant as compared to (possibly implementation-wise expensive) high-level language features which allow for faster construction of more readable solutions?

答:
As with all things, it's a trade-off.

If you are building an application that is going to run on a single user desktop and can reasonably be expected to control a large fraction of the RAM on that machine, it may be worth it to sacrifice memory usage for implementation speed.  If you're targeting that same machine but you're building a small utility that is going to be competing with a bunch of other memory-hungry applications that are running simultaneously, you may want to be more cautious about that trade-off.  A user may be fine with a game that wants all of their memory when it's running (though, as World Engineer points out, they'll be concerned if the garbage collector decides to pause the action periodically to do a sweep)-- they are likely to be much less enthused if the music player they run in the background while doing other things decides to gobble up a ton of memory and interferes with their ability to work.  If you're building a web-based application, any memory you use on the servers limits your ability to scale out forcing you to spend more money on more application servers to support the same set of users.  That can have a major impact on the economics of the company so you may want to be very cautious about making that trade-off.


答:
Memory management is utterly relevant since it governs how fast something appears even if that something has a great deal of memory. The best and most canonical example are AAA-title games like Call of Duty or Bioshock. These are effectively real-time applications that require massive amounts of control in terms of optimization and usage. It's not the usage per se that's the issue but rather the management.

It comes down to two words: Garbage Collection. Garbage Collection algorithms can cause slight hiccups in performance or even cause the application to hang for a second or two. Mostly harmless in an accounting app but potentially ruinous in terms of user experience in a game of Call of Duty. Thus in applications where time matters, garbage collected languages can be hugely problematic. It's one of the design aims of Squirrel for instance, which seeks to remedy the issue that Lua has with its GC by using reference counting instead.

Is it more of a headache? Sure but if you need precise control, you put up with it.

Title: Is memory management in programming becoming an irrelevant concern?

Body:
Background

I revisited an old (but great) site I had not been to for ages - the Alioth Language Shootout (http://benchmarksgame.alioth.debian.org/).

I started out programming in C/C++ several years ago, but have since then been working almost exclusively in Java due to language constraints in the projects I have been involved in. Not remembering the figures, I wanted to see, approximately, how well Java fared against C/C++ in terms of resource usage.

The execution times were still relatively good, with Java at worst performing 4x slower than C/C++, but on average around (or below) 2x. Due to the nature of the implementation of Java itself, this was no surprise, and it's performance time was actually lower than what I expected.

The real brick was the memory allocation - at worst, Java allocated:

a whopping 52x more memory than C

and 25x more than C++.

52x the memory ... Absolutely nasty, right? ... or is it?  Memory is comparatively cheap now.

Question:

If we do not speak in terms of target platforms with strict limits on working memory (i.e. embedded systems and the like), should memory usage be a concern when picking a general purpose language today?

I am asking in part because I am considering migrating to Scala as my primary language. I very much like the functional aspects of it, but from what I can see it is even more expensive in terms of memory than Java. However, since memory seems to be getting faster, cheaper and more plentiful by the year (it seems to be increasingly hard to find a consumer laptop without at least 4GB of DDR3 RAM), could it not be argued that resource management is becoming increasingly more irrelevant as compared to (possibly implementation-wise expensive) high-level language features which allow for faster construction of more readable solutions?


答:
Since Cole Campbell noted memory size and Michael Borgwardt noted tail call optimization I won't cover those.

Another thing to be aware of is CPS which can be used to optimize several interwoven functions where tail call optimization is for single functions.

You can increase the size of the stack as we did here, and be aware that 64-bit code eats the stack faster than 32-bit code.

Of note is that we ran one of the examples under F# interactive for more than 40 hours without blowing the stack. Yes it was one function call that ran by itself continuously to successful completion.

Also, if you need to do code coverage to figure out where the problems occur and don't have code coverage with VS you can use, TestDriven.NET


问:
Identifying Domain Services & Application Services when doing DDD
-I'm trying to figure out how to identify Application Services in my application. I think I can identify a Domain service by 2 things:

It acts as a facade to the repository.

It holds business logic that can't be encapsulated in a single entity.

So I thought about this simple use case that I'm currently working on:

An admin should be able to ban a certain user. The operation must be logged and an email must be sent to the banned user.

I have a UserRepository which has a function getUserById(). I have a User entity which has a ban() function.

I'll create a UserService like this:

```<code>class UserService{
    static void banUser(int userId){
          User user= UserRepository.getUserById(userId);
          user.ban();
          UserRepository.update(user);
    }
}
</code>```

The userId is a POST variable that I'll be receiving in my Controller from a web form.

Now where does the logging go? I need to log the ban process twice before and after the operation ex:

```<code>Logger.log("Attempting to ban user with id:"+userId);
//user gets banned
Logger.log("user banned successfully.");
</code>```

Second, I need to send a mail to the user. where should the call go?

I thought about putting the logging and email  is the UserService class itself, but I think there are better solutions.

Third, where do Application Services fit in all of this?

答:
Domain Service vs Application Service

A domain service is a class that hold a behaviour that does not belong to any other entity or value-object in the domain. Nothing to do with repository.
If you model the user's roles, many domain services become methods of the role itself.

Instead applicative services are the interface used by the outside world to use the domain. Thus, UserService is an applicative service.

Handling technical side effects

I use observable entities to handle such domain events.

This way:

the repository can observe a Banned event and update the persistence state without the need of an explict save call

the UserService can subscribe the event with an handler that logs and send the email just if the ban() command succeed.


答:
Doctor!  Doctor!  It hurts when I do this!

Developers are misusing your API because you've provided it to them.  If you think LevelReference.ById() isn't good to use, you shouldn't provide it.  If you take it away, they'll stop using it :-)

Since we're talking about C#, you can also mark ById() as [Obsolete] and they'll at least get a deprecation warning.  For those who compile with all-warnings-are-errors, that's fatal.

Title: Prevent developers from using constants

Body:
I have one one software system which allows developers to specify an ID or name to create NodeReferences. Both work fine, but ID's are not guaranteed to be the same across different environments. I've tried in documentation and in conversations to stress that ID's should not be hard coded (since they will break when deployed to a different environments), but some developers are still using them.

This works only in dev environment:

```<code>var level = LevelReference.ById(20);
var node = NodeReference.ByName(level, _patientGuid.ToString());
</code>```

This works in all environments:

```<code>var app = ApplicationReference.ByName("Reporting");
var area = AreaReference.ByName(app, "Default");
var level = LevelReference.ByName(area, "Patient");
var node = NodeReference.ByName(level, _patientGuid.ToString());
</code>```

I think the appeal is just that it produces fewer lines of code. The use of ID's is not by itself bad (since there are valid use cases like caching the ID returned from the server and using it later for faster look-up), but hard-coded ID's are bad. Most of the time the first code will throw an exception, but it's possible that it could be a valid ID for a different object than the developer intended, and this could result in very bad problems.

What's the best way to discourage the use of such constants in code? Ideally I'd like throw some kind of compiler error when I see code like the first example, or at least throw an exception before the call gets down to the database.


答:
As Donald Knuth said, premature optimization is the root of all evil.  Unless you have a reason to believe that memory is going to be the bottleneck, don't worry about it.  And given that Moore's law is still delivering increased memory capacity (even though we're not getting faster single-threaded code out of it), there is every reason to believe that in the future we'll be even less constrained on memory than we are today.

That said, if optimization is not premature, by all means do it.  I'm personally working on a project right now where I understand my memory usage in great detail, I actually need precise control, and a garbage sweep would kill me.  I am therefore doing this project in C++.  But that choice seems to be a once every several years event for me.  (Hopefully in a few weeks I won't touch C++ again for a few more years.)


问:
Storing in-text metadata in a discrete data structure
I am developing an application which will need to store inline, intext metadata. What I mean by that is the following: let's say we have a long text, and we want to store some metadata connected with a specific word, or sentence of the text.

What would be the best way to store this information?

My first thought was to include in the text some kind of Markdown syntax that would then be parsed on retrieving. Something looking like this:

```<code>Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diam __nonummy nibh__[@note this sounds really funny latin]
euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
</code>```

This would introduce two problems I can think of:

A relatively small one, is that if said syntax happen to be fortuitously on the said text, it can mess with the parsing.

The most important one is that this doesn't maintain this metadata separate from the text itself.

I would like to have a discrete data structure to hold this data, such a different DB Table in which these metadatas are stored, so that I could use them in discrete ways: querying, statistics, sorting, and so on.

EDIT: Since the answerer deleted his answer, I think it might be good to add his suggestion here, since it was a workable suggestion that expanded on this first concept. The poster suggested to use a similar syntax, but to link the metadata to the PRIMARY KEY of the metadata database table.

Something that would look like this:

```<code>Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diam __nonummy nibh__[15432]
euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
</code>```

Where 15432 would be the ID of a table row containing the necessary, queriable information, as per example below.

My second thought was to store information of this kind in a DB Table looking like this:

```<code>TABLE: metadata

ID    TEXT_ID    TYPE    OFFSET_START    OFFSET_END    CONTENT
1     lipsum     note    68              79            this sounds really funny latin
</code>```

In this way the metadata would have a unique id, a text_id as a foreign key connected to table storing the texts and it would connect the data with the text itself by using a simple character offset range.

This would do the trick of keeping the data separated from the metadata, but a problem that I can immediately see with this approach is that the text would be fundamentally not editable. Or, if I wanted to implement the editing of the text after the assignation of metadata, I would basically have to calculate characters additions, or removal compared to the previous version, and check whether each of this modifications adds or remove characters before or after each of the associated metadata.

Which, to me, sounds like a really unelegant approach.

Do you have any pointers or suggestions for how I could approach the problem?

Edit 2: some XML problems

Adding another case which would make quite necessary for this separation of data and metadata to happen.

Let's say I want to make it possible for different users to have different metadata sets of the same text, with or without the possibility of each user actually displaying the other user metadata.

Any solution of the markdown kind (or HTML, or XML) would be difficult to implement at this point. The only solution in this case that I could think about would be to have yet another DB Table which would contain the single user version of the original text, connecting to the original text table by the use of a FOREIGN KEY.

Not sure if this is very elegant either.

XML has a hierarchical data model: any element which happens to be within the borders of another element is considered as its child, which is most often not the case in the data model I'm looking for; in XML any children element must be closed before the parent tag can be closed, allowing for no overlapping of elements.

Example:

<note content="the beginning of the famous placeholder"> Lorem ipsum
  dolor sit <comment content="I like the sound of amet/elit"> amet </note>,
  consectetuer adipiscing elit </comment> , <note content="adversative?"> sed
  diam <note content="funny latin"> nonummy </note> nibh </note> euismod
  tincidunt ut laoreet dolore magna aliquam erat volutpat.

Here we have two different problems:

Different elements overlapping: The first comment starts within the first note, but ends after the end of the first note, i.e. it's not its child.

Same elements overlapping: The last note and the boldfaced note overlap; however, since they are the same kind of element, the parser would close the lastly opened element at the first closure, and the first opened element at the last closure, which, in this circumstance, is not what is intended.

答:
This is a typical kind of engineering question in that all your options have different tradeoffs, and which is best depends on what is important to you.  Unfortunately, you haven't given enough information to make the determination.

You also haven't appeared to consider an important semantic problem.  Lets say the original text is

My friend Bob lent me five dollars

Someone adds a comment around "Bob" saying

Bob is a complete idiot

Then the original text is edited to

Jane lent Bob five dollars which he later lent to me

You might make some sense of this particular case using a text matching algorithm such as what is used to show a diff file, but character offsets are going to make the metadata attach to the "Jan" in "Jane".

Worse is if the text is edited to

My friend Steve lent me five dollars

You could manage to figure out how to attach the metadata to "Steve", but how do you know if it applies?

Also, have you decided if the metadata itself can have metadata?  That might change your implementation.

Beyond semantic issues, it isn't very clear what you are doing with the data.  I thought perhaps it was very inconvenient to have the original text "polluted" with any markup, but then you were sort of OK with having ID values in it.  Which doesn't make a lot of sense if the metadata applies to a section of text instead of being inserted into a point in the text.

My guess is that for most purposes storing marked up text is easier, or, second choice, going all SQL and having the text and markup represented by a node hierarchy - basically a DOM in table form.  If your data is hierarchical than it may be easier to use XML and get existing parsers for free, versus writing your own.

It is quite possible that there is some fairly simple solution that is good enough for your exact situation, but I can't tell you what that is because it really depends on just what you are trying to do, in detail.

I would strongly suggest you encapsulate whatever strategy you choose as much as you can, though this is fairly hard to do if much of your implementation needs to be visible to many SQL queries.

Sorry that the reply is so scattered and so full of "it depends", but real world design questions are like that.


答:
The real brick was the memory allocation - at worst, Java allocated a
  whopping 52x more memory than C, and 25x more than C++.

Do you understand the numbers you base your question upon?

How much memory was allocated?

What were the programs doing?

When there's a big disparity between those Java and C programs, it's mostly the default JVM memory allocation versus whatever libc needs:

n-body

Java program 13,996KB :: C program 320KB :: Free Pascal 8KB

Look at the tasks that do require memory to be allocated (or use additional buffers to accumulate results from multicore programs):

mandelbrot

Java program 67,880KB :: C program 30,444KB

k-nucleotide

Java program 494,040KB :: C program 153,452KB

reverse-complement

Java program 511,484KB :: C program 248,632KB

regex-dna

Java program 557,080KB :: C program 289,088KB

binary-trees

Java program 506,592KB :: C program 99,448KB

...should memory usage be a concern when picking a general purpose
  language today?

It depends whether the specific usage, for your specific approach to solving the specific problems you need to solve, will be constrained by the specific limits of available memory on the specific platform that will be used.


答:
An alternative to a submodule is to use git subtree. This gives the benefits of git submodule but without pushing the complexity to the end user. The third party repository is merged into the main project tree, but there is metadata stored in such a way that you can:

extract the third party repository later, if any interesting changes have been made

merge in new updates from the third party repository (note merge, not overwrite)

For Git users who are not sophisticated enough to understand submodules, the subtree approach makes getting a clone of your project no more difficult than any other clone. A short blurb from the documentation:

Subtrees allow subprojects to be included within a subdirectory
  of the main project, optionally including the subproject's
  entire history.

For example, you could include the source code for a library
  as a subdirectory of your application.

Subtrees are not to be confused with submodules, which are meant for
  the same task. Unlike submodules, subtrees do not need any special
  constructions (like .gitmodule files or gitlinks) be present in
  your repository, and do not force end-users of your
  repository to do anything special or to understand how subtrees
  work. A subtree is just a subdirectory that can be
  committed to, branched, and merged along with your project in
  any way you want.

I had set up a project at work using submodules, and the troubles with keeping submodules up to date in everybody's clones was too much work. I recently changed to using subtrees everywhere and those problems disappeared.

Note that git-subtree is part of the git/contrib directory, and must be installed separately.

Title: Git submodule vs Git clone


问:
What are the most popular sorting algorithms in practice?
Although I suspect the answer to this question may be something along the lines of "popularity is irrelevant; each algorithm has its trade-offs", I'm interested in a list of popular sorting algorithms used in practice.

I was browsing Wikipedia's page on sorting algorithms and while there are not that many algorithms listed there, I still don't see all of them being used. For example, surely selection sort wouldn't be used in practice since it has a O(n^2) right? So, which are used in practice a lot?

答:
First, read What are the criteria for choosing a sort algorithm?  We'll wait.

Are you done reading?  OK.  By way of answering your actual question, Microsoft chose an "unstable" quicksort algorithm for their Array.Sort() method.  I figure that's as good an endorsement of a sorting algorithm as any other.


答:
The typical default sort implementation for most of the languages I've used is either Mergesort or Quicksort. Python and increasingly some other languages use Timsort which is a Mergesort that looks for preordered chunks to speed up the sort. Typically Mergesort would be picked if your worst case is important but your space isn't and Quicksort if the reverse is true.

You're going to only see a fairly small subset not just because of the time and space complexities but also because parallelization favors algorithms that divide and conquer which both Mergesort and Quicksort make use of.

Parallelization opens up new sorts though like the Bitonic Sort which operates in log^2(n) time though the space is nlog^2(n).

Title: What are the most popular sorting algorithms in practice?

Body:
Although I suspect the answer to this question may be something along the lines of "popularity is irrelevant; each algorithm has its trade-offs", I'm interested in a list of popular sorting algorithms used in practice.

I was browsing Wikipedia's page on sorting algorithms and while there are not that many algorithms listed there, I still don't see all of them being used. For example, surely selection sort wouldn't be used in practice since it has a O(n^2) right? So, which are used in practice a lot?


问:
How do I prove to management that our programmers could work more productively?
Backstory

I graduated less than a year ago with a degree in Computer Science (with extra courses in software engineering), and another degree in Software Engineering. I'd like to think that I'm familiar with modern software development methodologies (CI, Scrum, XP, etc).

The problem

I got my first (and current) job at a medium sized game development company. The company has been around for several years, has multiple successful titles under its belt, and is full of talented and dedicated people. However, software development happens in an entirely ad hoc way. We use SVN and a bugtracker, but beyond that there's very little planning, no automated testing, and you'd be hard pressed to find a UML diagram anywhere in the office.

I can think of two possible reasons for this:

Programmers are simply not given enough time to do things properly. Sadly, upper management has a tendency to set deadlines without consulting the programmers, which has led to many an all-nighter.

The programmers themselves lack the education / motivation to employ proper techniques.

The question

I recently spoke to management about this, and I was encouraged to suggest alternatives. I figure the first thing to do would be to rule out the second possibility listed above: that is, give management some evidence that if the programmers were given more breathing space, they could become much more productive. However, I'm not quite sure how to go about that. My best idea so far is to conduct a survey among programmers about what software development methodologies they know, as well as their attitudes towards code quality and such.

tl;dr

How can I assess whether our programmers are able/willing to employ modern software development methods, and prove to management that this would lead to increased productivity?

EDIT

I obviously failed to bring my point across properly, so let me elaborate on the situation a little.

First, I am fully aware that this looks like a bright-eyed fresh grad trying to save the world with his l33t education and changing the wicked ways of those grumpy old fossils who just don't know any better. I also understand that there is always a difference between theory and practice, and what they teach you in school is never what happens in real life. That is exactly why I'm being incredibly cautious here, trying to get a feel for the current situation instead of charging in going "Hai guise, let's all do teh unit tests from now on!"

Second, @psr raises a very good point. Let me name some of the reasons why I think that there is room for improvement:

Our senior developer, who's a bit of a maverick but ultimately a skilled and reasonable guy, agrees that at this point in the company's history, we should have more sophisticated and repeatable development processes in place.

One of the programmers who's much more experienced than me and has used modern methods agrees with me that we could be more productive if we used some of them.

In some cases, throwing features together during an all-nighter and then hunting down all the bugs takes a ridiculous amount of time; I cannot imagine that doing things any other way could be any worse.

The lack of planning, design, refactoring and documentation leads to poor code quality. Perhaps more importantly, it leads to programmers forgetting how their own code works in a matter of months (while they're still working on the same project!), and no way to find out apart from trial and error or wading through the spaghetti.

Even management suffers, as the only quantifiable measure of progress in a project is the number of unresolved bugs. But since anything is liable to break at any time (due to the reasons above), this is a very unreliable figure.

Third, I don't actually believe that my colleagues lack the knowledge of modern methods or the willingness to use them, it's just that at this point, I cannot rule it out as a possibility. Which is kind of the reason why I came here asking what's the best way to find out what they know (besides lengthy personal interviews, obviously).

Fourth, don't take those comments about UML and automated testing too seriously, they were just examples. The fact is, code is produced on the immediate whims of designers/upper management without any kind of software design, and only ever tested by a couple guys pressing buttons to see if anything breaks. Perhaps I'm still wearing my university-issued rose tinted glasses, but that strikes me as garage-development at its finest.

tl;dr

I'm trying really hard not to be arrogant about this, but both my fellow programmers and management seem to be expressing a desire for more predictable and productive ways of developing software. How can I find a solution that could make for an easy transition?

答:
First, be clear on the specific steps that management should take.  "Give your programmers more breathing room" is too vague, not actionable, and not measurable.

Second, identify the actual problem.  Why are your programmers pulling all-nighters?  There's always a root cause or causes, and that root cause does extend beyond "we don't have enough time get things done."

Third, find authoritative sources and case studies that back up your claims.  There are many resources available that discuss solutions to software development problems; use them to make your case.

Fourth, appeal to management on their own terms.  What are they looking for?  Chances are good that they're looking for ways to be more productive in terms that they can measure.  In other words, "show them the money."

Fifth, consider the possibility that the problem that you are describing doesn't really exist.  If the company is profitable, and they are adequately compensating the developers, all-nighters boil down to simple planning problems and inadequate management of expectations.  In other words, it might not be a problem with your software development methods, but more a problem of managing scope and feature changes.

Finally, take into account your lack of practical experience.  Education is great; I have one too, but that education didn't automatically make me an expert.  What it did was ground me in the fundamentals.  Getting into the "real world," and finding out how things really work is a whole 'nother education.


答:
It should be fairly simple. Employ modern software development methods to be noticeably more productive than the others, and credit your productivity to these methods.

So... make a list of where your productivity will excel, and then over time, for each item on the list, keep a record of the methods you used to achieve excellence.

Title: How do I prove to management that our programmers could work more productively?

Body:
Backstory

I graduated less than a year ago with a degree in Computer Science (with extra courses in software engineering), and another degree in Software Engineering. I'd like to think that I'm familiar with modern software development methodologies (CI, Scrum, XP, etc).

The problem

I got my first (and current) job at a medium sized game development company. The company has been around for several years, has multiple successful titles under its belt, and is full of talented and dedicated people. However, software development happens in an entirely ad hoc way. We use SVN and a bugtracker, but beyond that there's very little planning, no automated testing, and you'd be hard pressed to find a UML diagram anywhere in the office.

I can think of two possible reasons for this:

Programmers are simply not given enough time to do things properly. Sadly, upper management has a tendency to set deadlines without consulting the programmers, which has led to many an all-nighter.

The programmers themselves lack the education / motivation to employ proper techniques.

The question

I recently spoke to management about this, and I was encouraged to suggest alternatives. I figure the first thing to do would be to rule out the second possibility listed above: that is, give management some evidence that if the programmers were given more breathing space, they could become much more productive. However, I'm not quite sure how to go about that. My best idea so far is to conduct a survey among programmers about what software development methodologies they know, as well as their attitudes towards code quality and such.

tl;dr

How can I assess whether our programmers are able/willing to employ modern software development methods, and prove to management that this would lead to increased productivity?

EDIT

I obviously failed to bring my point across properly, so let me elaborate on the situation a little.

First, I am fully aware that this looks like a bright-eyed fresh grad trying to save the world with his l33t education and changing the wicked ways of those grumpy old fossils who just don't know any better. I also understand that there is always a difference between theory and practice, and what they teach you in school is never what happens in real life. That is exactly why I'm being incredibly cautious here, trying to get a feel for the current situation instead of charging in going "Hai guise, let's all do teh unit tests from now on!"

Second, @psr raises a very good point. Let me name some of the reasons why I think that there is room for improvement:

Our senior developer, who's a bit of a maverick but ultimately a skilled and reasonable guy, agrees that at this point in the company's history, we should have more sophisticated and repeatable development processes in place.

One of the programmers who's much more experienced than me and has used modern methods agrees with me that we could be more productive if we used some of them.

In some cases, throwing features together during an all-nighter and then hunting down all the bugs takes a ridiculous amount of time; I cannot imagine that doing things any other way could be any worse.

The lack of planning, design, refactoring and documentation leads to poor code quality. Perhaps more importantly, it leads to programmers forgetting how their own code works in a matter of months (while they're still working on the same project!), and no way to find out apart from trial and error or wading through the spaghetti.

Even management suffers, as the only quantifiable measure of progress in a project is the number of unresolved bugs. But since anything is liable to break at any time (due to the reasons above), this is a very unreliable figure.

Third, I don't actually believe that my colleagues lack the knowledge of modern methods or the willingness to use them, it's just that at this point, I cannot rule it out as a possibility. Which is kind of the reason why I came here asking what's the best way to find out what they know (besides lengthy personal interviews, obviously).

Fourth, don't take those comments about UML and automated testing too seriously, they were just examples. The fact is, code is produced on the immediate whims of designers/upper management without any kind of software design, and only ever tested by a couple guys pressing buttons to see if anything breaks. Perhaps I'm still wearing my university-issued rose tinted glasses, but that strikes me as garage-development at its finest.

tl;dr

I'm trying really hard not to be arrogant about this, but both my fellow programmers and management seem to be expressing a desire for more predictable and productive ways of developing software. How can I find a solution that could make for an easy transition?


问:
What arguments exist against ever using the keyword substitution feature in many version control systems?
Are there any arguments out there against using the keyword substitution (i.e. replacing $Revision$ with $Revision: 5$) feature in many version control systems?  Are there any overall bad practices that using the feature encourages?  Are the any pervasive and hard-to-solve problems that it causes when you do use it?

Here an argument against using it:

Keyword Substition: Why You Don't Need It

It's succinct, and I think the argument about the problems it causes for binary files is cogent, but I don't find it compelling.

答:
Any revision system that cannot tell text files from binary is completely broken.

I think though that they are right that you don't really need it; you often need a way to get your revision # to show-up automatically and they provide an example.

I think most other usages of this feature are superfluous but not harmful.


问:
Is XHTML5 dead or is it just an synonym of HTML5?
So what happened to XHTML5?

http://www.w3.org/TR/html5/

That page is a draft for both xhtml5 and html5?
So there's no difference between these doctypes?

答:
Yes unfortunately XHTML is gone.

Adding 1 more reason to MainMa's great answer:

When XHTML was created, it was meant to be used by WebApps to serve structured content that would be understood by non-browser softwares, that would not have tag-soup HTML parsers. For ScreenReaders XHTML is still great, but for any other kind of software, WebServices fit that need, and they mostly use XML or JSON. SOAP itself has its own XML Schema, simpler than XHTML and operation-oriented.

As long as I know, there's not even 1 WebApp in the world that serves the same HTTP message to both browsers and other clients. Even REST architecture, which was meant to serve the same representation of a content in multiple content types based on client's preference, isn't used to serve XHTML/feed browsers.

In Java EE for example, using Eclipse we can deploy a unique war file holding Servlets+JSPs to serve HTML, together with Axis2 to serve a WebService. It's simply easier to develop separated softwares aimed for browsers and WebService than have a unique, complex software that serves them all.

The major reason for REST being rejected is exactly the complexity (and it was meant to be simple!) of developing a server that serves the same content for any type of client without knowing anything about it. And it's also hard to handle Web's need of fast evolving, together with keeping a stable definition that would not force non-browser clients to be updated every time an XHTML changes, say it to keep the XHTML valid when it's built by many different modules.

In the same way, it's very hard to develop a non-browser client that parses an XHTML document, even it being valid, because of all those XML elements that are meant to structure the browser-rendered layout, and not meant to hold content.

If REST adopters already complain about SOAP's XML complexity, which is WAY simpler than an XHTML meant for a browser, imagine how hard it is to handle XHTML for multiple client types, server and client-side.

In practice: use HTML, XML-like if you want, to build WebSites for browsers, and any kind of WebService solution for non-browser clients.

BUT, I also think that XHTML5 must be created. XHTML 1.1 (ok, 1.0, 1.1 is unusable) will become outdated with HTML5, and we still need a validator that accepts HTML5's elements and validates XML wellformedness.


答:
It depends on a number of factors, especially the scale at which you're working.

Just for the sake of argument, let's assume a 30x difference in memory and 2x in CPU usage.

If you're dealing with an interactive program that would take 10 megabytes of memory and 1 millisecond of CPU if written in C, it's pretty much inconsequential -- 300 megabytes of memory and 2 milliseconds to execute is normally entirely irrelevant on a typical desktop, and unlikely to mean much even on a phone or tablet.

The difference between needing around half the resources of 1 server and needing 15 servers is a much bigger step though -- especially since scaling out to 15 servers is likely to require a lot of extra work to develop instead of less. As far as future expansion goes, the same factors you mention tend to suggest that unless your customer base undergoes massive growth, that if it'll run on one server now, chances are pretty good that when you outgrow that server, you'll be able to replace that with one newer server without any problem.

The other factor you really need to consider is exactly how much difference in development cost you're going to see for your particular task. Right now, you're basically looking at one side of an equation. To get a good idea of costs vs. benefits, you (obviously enough) need to look at both costs and benefits, not just one in isolation. The real question is basically: "is x greater than y?" -- but you can't determine that by looking only at x. You clearly need to look at y as well.


答:
Have you spoken to your development colleagues about this? How do you know they lack education? That's quite a sweeping statement and you'll probably find you're wrong.

I don't think it'd go down too well if a new grad started meddling with processes without understanding why they're like that in the first place. Managers love processes and love tracking and love being made look good, so the battle-hardened skeptic in me feels that your manager is interested in your views because it's something they can take credit for.

Also, be aware that software development methods in text-books don't always map out like that in the Real World. They're certainly not one-size-fits-all, and can look at the world through rose-tinted spectacles. UML is a load of crap, not having that around isn't that big of a deal.


答:
First of all note that sorting is the most heavily studied problem in CS.  And for good reason.  At one point the majority of all computer time went into sorting.  Hash lookups likely exceed that today, but I would expect that sorting is still in the top 2 productive uses of CPU time.  (If you're dealing with big data sets, it is almost certainly #1.)

Therefore there are a lot of sorting algorithms, and people have studied exactly when each is best.  Anything you can think of from the cost of a branch misprediction to the speed of accessing different kinds of data storage has been heavily analyzed.  People have tried a ton of sorts and found useful roles for them.  Yes, even selection sort.

For small lists, quicksort was long the favorite.  These days Timsort is gaining popularity (used by default in Python and Java).  Many, many other sorting algorithms have been used, and in general whatever is the default in your language will not prove to be a bottleneck.

For large data sets, the clear winner is merge sort.  Its ability to stream data (which works well with large disks), ease of parallelization, and lack of hot spots give it a huge boost.  Of course there are many details that you need to figure out.  And so there are many different specific sorts that are used, but under the hood the good ones are overwhelmingly based on merge sort.

See http://sortbenchmark.org/ for a picture of what is possible at the high end, and descriptions of how they do it.

Every serious programming language has built in sorting algorithms.  For small lists you should just use that and you'll be fine.  If your data is larger, just use the Unix sort utility (which, for large data, will be a merge sort under the hood).  If your data is so big that it needs to be parallelized, you should find something pre-built for your use case.  (MapReduce frameworks use sort internally, so Hadoop is often "good enough".)  If you have some other custom need not met by those three use cases, my experience says that you'll likely be implementing some variation on a merge sort.


问:
When a garbage collector compacts objects in the heap, does it change the references on the stack?
This seems like a simple question, but after a lot of reading on the subject, I still haven't found a definitive answer (perhaps because it is so simple).

My question is this: when a garbage collector compacts objects in the heap, how are the references to those objects in the stack updated? I can think of two possible solutions:

Go through the stack (and references in the heap) and update the reference to point to the new location of the object. In an analogy to moving, this would be like sending a letter to anyone who has your address and asking them to update their address book with your new address.

Provide some sort of look up table. This would be like leaving a forwarding address with the local post office.

Do garbage collectors predominantly use one of these two methods? Some other method? Both?

答:
I have no specific expertise on this, but my understanding is that the first method is generally used.

The garbage collector has to analyze the stack anyways to find what things in the heap are referred to from the stack.  Once it decides to move something, it has to correct references to it anyways, and there is no reason to differentiate between heap and stack at that point.

The lookup table approach in principle could work.  However that would make all pointer accesses need to take 2 steps.  That would be a huge performance impact on normal run times.  Particularly for the use case of many small objects.  (Which is a case where state of the art GC programs usually beat reference counting.)


问:
How can robots beat CAPTCHAs?
I have a website e-mail form. I use a custom CAPTCHA to prevent spam from robots. Despite this, I still get spam.

Why? How do robots beat the CAPTCHA? Do they use some kind of advanced OCR or just get the solution from where it is stored?

How can I prevent this? Should I change to another type of CAPTCHA?

I am sure the e-mails are coming from the form, because it is sent from my email-sender that serves the form messages. Also the letter style is the same.

 For the record, I am using PHP + MySQL, but I'm not searching for a solution to this problem. I was interested in the general situation how the robots beat these technologies. I just told this situation as an example, so you can understand better what I'm asking about.

答:
To echo the other answers, you're likely encountering bots that use human farms to enter the captchas for them.

I've recently discussed a technique (and released an accompanying Drupal module) that blocks spam bots by requiring client-side JavaScript. As far as I'm aware, this has worked with 100% efficiency on all sites that have used this code. The idea is to use AJAX to generate a unique hash and submit it along with the other form data, and then compute that same hash on the backend once the form is submitted, and compare the two values.

Full details in my blog post (coincidentally, since you mentioned using PHP + MySQL, these are the same technologies described there) - Module release: Badbot; eliminating spam...

Title: How can robots beat CAPTCHAs?


答:
How can I assess whether our programmers are able/willing to employ
  modern software development methods?

It sounds like the first thing you need to do is talk to your coworkers.  From your question I  get the impression that you don't understand why the current processes or lack there of, are in place.  So ask somebody!  It may be that your coworkers are aware of the alternatives and are choosing not to use them for reasons you can then discuss, or it may be that they are unaware, and you can then introduce them and have a discussion about how they could benefit your team.

Trying to change team culture, procedure, or structure without understanding WHY it is currently the way it is makes a recipe for disaster.  Work to understand the concerns that have led to this place and then discuss some solutions that you may see.  A fresh pair of eyes is great, but only if you engage in dialog with people who know the territory well.


问:
Moving from local storage to a remote database: how should I cache the data locally?
I have a .NET (C#) application that I am releasing soon, which also has some support files.  Right now I am storing those files as .txt files, and I update them as necessary whenever the application version changes.  I am wondering if a better solution might be to store the information in those files in a central database instead, and have all clients access that database when then launch.  This would prevent the need to update the software each time those reference files change.

My question for the gurus here is this:  Is this a wise decision, and if so...what would be the best method of storing the downloaded data?  A temp file? in memory? something else?  Any help would be appreciated.

答: I would strongly suggest breaking this into a generic subcomponent of your application suite. I solved this a while back with a very small loader app that contained thoroughly tested logic that checked a url defined in the registry. With a series of registry keys for version info of components and a web service that contains patching logic you can optionally check for updates on load. The main benefit of a loader is that it can update multiple applications with the logic to update application deployed at a later time. </p>

OwnerDisplayName: Jeagr


答:
Most methodologies and best practices for software design never claim to be able to get an application built in the shortest time. Your company is proof that you can "cowboy code" your way through a few all-nighters and get something released. Management is happy because they got "it" done on time and usually that means on budget.

Bugs show-up and you'll spend a disproportionate amount of time on them, but management doesn't care. Somehow they're reinforced for hitting that release date. Someone will have a great idea to enhance your games, but your code is too brittle to make such a drastic change. Oh well, what might have been.

Suggest applying one of your ideas to a particular area of one of your games. This way you can track and measure any benefits compared to your other apps and present these findings to management. Example: Suggest automated testing. Your fellow programmers will display very quickly whether or not they can implement it. Very few devs who have taken the time to become fluent at unit testing protest their use.

IMHO - the arbitrary time-frame management determines development is your biggest problem. You need to get the devs closer to the users and/or decision makers who are coming up with these ideas. Discover the true requirements. Just because there is a need to make games available on different platforms, doesn't mean they have to run in a browser. There's always more than one way to skin a cat.


问:
Space between negative sign and variable name
I tried doing a Google search, as well as searching this Stack Exchange site but could not find a question relating directly to this.

The PEP 8 -- Style Guide for Python Code has lots of good style recommendations, however I don't think they mention anything about whether a space between a minus or negative sign (-) and a variable name would be more or less readable.

Question

It's a (very) minor issue, but which is considered more readable?

false_positive = -true_positive

false_positive = - true_positive

Motivation

I ask not to be trivial, but because in my past math courses I often find myself not noticing the negative sign in important equations, such as the equation for computing entropy:

I often don't notice the - sign before the summation, so making it stand out would be desirable.

答:
I think the second one is marginally less bad, because the minus sign is a little less likely to be read as part of the word; However, I think both forms are bad.

I would replace the whole thing with

```<code>false_positive = -1 * true_positive
</code>```

or even

```<code>false_positive = (-1) * true_positive
</code>```

in any language.

Body:
I think the second one is marginally less bad, because the minus sign is a little less likely to be read as part of the word; However, I think both forms are bad.

I would replace the whole thing with

false_positive = -1 * true_positive

or even

false_positive = (-1) * true_positive

in any language.


答:
Keep things simple:

Your application already works with text files, no need to change any of that. Once, on startup, pull a file from your central server that just has a simple version number in it. If the version has increased, pull down the updated files.

All of your currently existing code doesn't change. The logic on startup is dead simple, and shouldn't take more than a few hours to implement. And best of all, if your server is down for some reason, the app can still function.

Title: Moving from local storage to a remote database: how should I cache the data locally?

Body:
I have a .NET (C#) application that I am releasing soon, which also has some support files.  Right now I am storing those files as .txt files, and I update them as necessary whenever the application version changes.  I am wondering if a better solution might be to store the information in those files in a central database instead, and have all clients access that database when then launch.  This would prevent the need to update the software each time those reference files change.

My question for the gurus here is this:  Is this a wise decision, and if so...what would be the best method of storing the downloaded data?  A temp file? in memory? something else?  Any help would be appreciated.


答:
The copybook way of managing products/business can't fit for all. I have worked with medical imaging projects. Everyone wanted to work in agile way but the problem is  we can't be completely agile because we've regulatory controlling our business and it's required to have hell lot of documentation to prove this product meets their terms and conditions to sell the product.

People who are core in to the work actually not much bothered about the process. They consider it as a shit job and slow up their activities (yeah most programmers do).

I guess they work with people make their plans and ship the product. That's how it's worked so far and they're not ready to change it.

For e.g companies like Valve doesn't have any managers at their office. It's an extreme case but most of the product companies have a vision, hack on certain things and ship. On introducing a process, ask these basic questions

What exactly the problem you're going to solve?

Are you finding without a proper process we are shipping low quality product? Or on introducing a new process, you're confident about improving the way of working within the team?

If it's a problem with management, then which has to be fixed first? Are they lacking employee morale?

How can you improve quality of the product. Some simple things like unit testing, coverage, coding standards and guidelines suits your project can improve the quality of the code

How it's possible to streamline the process without hurting them much?

Agile way of working has to be tailored well to the needs of your business. For web development, it might work well. They ship early and often.

The UML diagrams are kind of nasty things and mostly people doesn't really need a too detailed level of diagram it's good to have but it's time consuming. Agile modelling is entirely different and fairly simple. May be you can ask people to follow this approach and they will be ready to do it as most of us discuss things in terms of simple hand-drawn diagrams.

People hate changes and I am pretty sure the survey is not really going to help you much because if they're frustrated about the way of working they would have fixed it much early as a self learning organization. But here you're asking their help to solve your problem than theirs also you can't clearly say what really works for them. I suggest you to sit back and think, and make the process streamlined into the development process without much overheads.

In the abstract level you will have to clearly identify the problems at every level and tackle it one by one (start from top, that's easy) and thrive the team towards success.


问:
Basic hardware for iPhone development
I am new to Mac. If I want to start a iPhone development, what is the basic hardware I require? Would Mac Mini be good enough?</p>

答:
You need a Mac.  Almost any Intel based Mac will do, if it has memory enough, where "enough" depends on how willing you are to wait.

At least 2 Gb RAM and better if more.

You will need to download the appropriate iPhone SDK from Apple to write code.  For any non-trivial application you will need a physical device too, as the emulator is not 100% accurate.

Title: Basic hardware for iPhone development


问:
Building automated unit tests for tools which don't have an xUnit implementation

Preamble

I have been bitten by the bug of automated unit testing. I feel the benefits and the confidence in a code base it can deliver.

I am also feel I have a reasonable intuition as to what parts of the code deserve to be unit tested. Code that has logic, code that might be messy (because it's dealing with some messy requirements) that you are happy to have encapsulated, that may have a few odd fringe conditions.

Unusual Context

At my place of work, we leverage a number of tools which are fairly niche. For the sake of argument I can certainly see that these tools deliver on features and efficiencies making them indispensable for getting their particular job done.

The challenge I have is that I certainly have implemented logic in these tools which my gut says "this should be covered with a unit test".

I have tested these bits of code...

In a throwaway fashion - try several inputs to exercise the code until satisfied it works - this has the downside of being lost effort when it comes to people maintaining the code in the future

In a less throwaway but manual fashion - writing up the test cases and steps to poke the code to produce output; with test data; such that the above throwaway tests are not thrown away - this retains the investment in time, but is slow and subject to people knowing about the tests and running them

In a slightly more automated fashion - with scripts to do the poking of the code; perhaps even check the output to a degree - this is more pleasant to re-run but is far from leveraging a mature framework in a consistent manner like when writing the xUnit style tests

While the progression feels nice, I feel I'm missing the xUnit experience I've become a fan of.

What kind of languages/platforms exactly am I dealing with here?

An ETL tool, akin to SSIS, called RedPoint Data Management: not open source so I'll describe, it has projects which have tools, these tools are linked by data flows, and some have some logic I wish to unit test

A composition tool, this for the uninitiated is a product which takes a data file and outputs a print file. Imagine XML in, PDF out.

DOS script functions: I promise this is a real feature of good'ol'fashioned DOS scripts, tutorial here

(I'll add this to the list, not strictly what I need help with, but may be more clear than the above if my tool set is difficult to empathise with) COBOL data transformations, using CopyBooks to specify the formats of the flat file inputs and outputs. When compiled results in one big-ball-of-mud binary even if functionally decomposed inside

The Actual Question

Are xUnit style tests fundamentally aimed at Object Oriented languages?

答:
The distinction is not about object-oriented or not, but rather about testing individual functions ("white box testing") versus complete programs ("black box testing"). And it's not so much separate cases as a continuum in which various techniques are mixed and matched.

The tools are also not monolithic. An testing framework consists of four things:

The test runner

The result reporter

The collection of assertions

(optionally) Mock object framework

These components are mostly orthogonal and independent implementations of them can be combined, both the different types of components and multiple implementations of each one.

The test runner

The component that finds all test cases and executes them. Multiple test runners can be combined together by simply calling a test runner as test case from another runner. Can be as simple as a for-loop over a directory of scripts and that's exactly what one often ends up using as top-level runner when combining tests written in different frameworks.

You can find some universal test runners in CTest or shUnit. Both also provide methods for comparing outputs in files.

The perl Test::Harness can also be considered general runner as it runs scripts in directory. And perl is well suited for testing external programs too.

The reporter

Each unit test framework produces list of failed tests. If you combine multiple frameworks, you may either just collect all individual reports or use something like subunit or the Test::Harness's "tap" to provide nice uniform progress and summary.

The assertions

This is how you test the tested code produces expected results and is tied to how the code is interfaced.

For libraries in various programming languages there are the assertions provided by the respective xUnit.

For command-line tools there are various file comparators. CTest and shUnit have some, but they are mostly easy to homebrew if needed.

For interactive command-line tools there is expect (or it's Perl variant).

For GUI applications there is LDTP (linux desktop testing project, but they now have windows and macos versions too)

I am sure I've seen something for web applications (mean in-browser ones; you test services through their client library), but I don't remember any name. You'll surely find a few with google.

Mocking tool

This is about constructing the test environment without setting up all real dependencies. E.g. set up a database access layer that will return specified set of responses without setting up actual database and such.

It's not possible to automate much as you need to provide the data it should return. It is often not what you want anyway. I.e. you need to test the application against sample data in the actual database or other data source anyway and additional fine-grained module testing is often a lot of work with diminishing returns.

I don't think much can be generalized for this. For most xUnits there is something that will generate dummy implementations for interfaces, but that's about it. Than you simply have to consider what the component to be tested requires and what's the easiest way to provide it locally. For something that communicates with some web service you may run some lightweight http server locally. For database tool that uses ODBC (or DBI or whatever universal API) you can test it against sqlite even though it normally uses big server etc. For tools that just read files you obviously just copy in suitable test data set.


问:
What is detailed design? what are the advantage disadvantages using it?
Im really not getting  the idea:

What is Detailed Design.

Why use Detailed Design.

Advantages/Disadvantage of using Detailed Design.

Any alternative methods other than using Detailed Design.

Could some one please guide/explain me ?

答: When most people talk about detailed design, they are referring to a process known as top-down design. In short, when you think about the problem you are trying to solve, you start at the highest level and then work yourself into the details. This approach works very well when you have an overall structure you want your application to live within. At the macro level you are considering how many machines will be needed to host your application, which existing services you will need to use, etc. As you dive deeper, you are looking at use cases (or user stories if you prefer that terminology), and error handling (use cases have both normal and error condition paths to worry about). As you go even further into the details, you are looking at your algorithm, state transitions, logical sequence, and how internal parts of the code work together</p>


问:
What makes a series of machine instructions an actual process/thread in memory?
Every instruction comes from main memory/RAM.

In a bootloader, at least on Intel and IBM PC firmware and designations, you
have BIOS jump to a memory address to begin fetching instructions for whatever
purpose, usually to load a kernel, when or where does the instance of a process begin?

The hardware stacks are limited, but does using them constitute for a process or thread?

My point simply is what makes something a "thread" or "process", as to opposed to
just more bytes from memory? Where is the line drawn in this situation?

答:
The BIOS finds the kernel bootloader and jumps to that. The kernel loads process zero -- at this point the kernel has total control over the processor and RAM. When the kernel wants to start a new process, it saves the state of the current process, allocates space in memory for the new process, and loads the initial state of the new process. The stack is just some available space given to a process by the kernel. It works like the memory is allocated before the process starts and freed after the process ends. The stack is limited because for C it needs to be contiguous -- to extend the stack, the kernel has to use the MMU to map a space in memory. If you don't ask the kernel first, you go past the end of the stack into a region that the MMU marked as unreadable, causing an overflow.

There are a few ways to separate tasks.

Green threads are done by saving state and switching tasks without kernel support. The process would need to allocate new stack space or share stack space. Switching with green threads requires them to release control of the CPU and give it to another with a yield command or similar.

Kernel threads are given their own stack and task switching is done by the kernel's scheduler, and the kernel creates space for the thread's stack, but they belong to the same process so they can access the same memory.

Processes are segregated by the kernel, so they cannot access the memory of other processes under normal conditions. forking creates a new process with a copy (on write) of the memory used by the parent process.

I think these definitions are fairly typical, but a lot does depend on what the kernel and even language writers decided. For example, in Erlang, they use what I would refer to as "green threading" to create very light-weight threads, but describe these as "processes" (not unjustifiably, since they don't share memory).


问:
Best Architecture for ASP.NET WebForms Application
I have written an ASP.NET WebForms portal for a client.  The project has kind of evolved rather than being properly planned and structured from the beginning.  Consequently, all the code is mashed together within the same project and without any layers.  The client is now happy with the functionality, so I would like to refactor the code such that I will be confident about releasing the project.  As there seems to be many differing ways to design the architecture, I would like some opinions about the best approach to take.

FUNCTIONALITY

The portal allows administrators to configure HTML templates.   Other associated "partners" will be able to display these templates by adding IFrame code to their site.  Within these templates, customers can register and purchase products.  An API has been implemented using WCF allowing external companies to interface with the system also.  An Admin section allows Administrators to configure various functionality and view reports for each partner. The system sends out invoices and email notifications to customers.

CURRENT ARCHITECTURE

It is currently using EF4 to read/write to the database.  The EF objects are used directly within the aspx files.  This has facilitated rapid development while I have been writing the site but it is probably unacceptable to keep it like that as it is tightly coupling the db with the UI.  Specific business logic has been added to partial classes of the EF objects.

QUESTIONS

The goal of refactoring will be to make the site scalable, easily maintainable and secure.

What kind of architecture would be best for this?  Please describe what should be in each layer, whether I should use DTO's / POCO / Active Record pattern etc.

Is there a robust way to auto-generate DTO's / BOs so that any future enhancements will be simple to implement despite the extra layers?

Would it be beneficial to convert the project from WebForms to MVC?

答:
Use MVP pattern for separate and logic and UI , so in the future you can move to a different UI technology re-using existing logic

Use the repository pattern between BL and DAL so that you can change to any RDBS reusing the business logic

bring separate layers (Dlls) for BO and DAL which is minimizing maintenance.