答:
It is possible and it has been already done decades ago. Currently only challenge is to streamline it sufficiently to be run on mobile devices.

Examples

http://www.youtube.com/watch?v=PrzjLg4voEM

http://www.youtube.com/watch?v=k-wOJ8rFy8Q

Currently that kind of result can be achieved with off-the-shelf open source libraries such as OpenCV.

In fact there is webinar related to that very topic tomorrow. "Beyond Real-time Video Surveillance Analytics with GPUs"

Title: Is it possible to build software that interprets video in real time?

Body:
Say you had a live video stream of a popular boardwalk that had thousands of pedestrians walk down it each day. Could software be written to count the number of people that walk by a camera and collect data on things like how many of them were wearing red clothing or how many were over 6' tall?

I'm thinking the software would be a layer between the camera and the people. But I have no idea if that is even possible. If something like this already exists, I wasn't able to find it but please share if it does.

Thanks!


问:
What mistakes in managing software products must be avoided to keep people from hating the vendor?
A previous question about why people hate Microsoft was closed. This is an attempt at a somewhat more constructive question along the same general line. This one is both broader and narrower though. It's more general by being about software vendors in general, not just Microsoft. It's narrower by dealing only with management of software products.

So, what steps should be taken (and/or avoided) in managing individual software products to assure that not only the individual products, but the company as a whole is respected/liked/seen in a positive light?

答:
When you are a big corporation like Microsoft, Google or Facebook with lot of exposure in the news, you can't keep people from hating you. It's impossible.

The more you are successful, the more you are hated.

Vendor should create a metric called the hatemeter to measure their success! ;)

That is why one of the biggest quality of an entrepreneurs is being able to understand and handle that. And more importantly, use the bad feedback to improve their products (any feedback is interesting).

The real challenge vendors must face is not hate, but indifference.

EDIT: I found this very interesting website. It allows anyone to give his opinion on a various of popular companies and product. The amount of votes is too low to make the results relevant, but what is interesting voters must explain why. I let you discover the comments and judge by yourself human nature is complex.

ContentLicense: CC BY-SA 2.5


问:
Writing Tests for Existing Code
Suppose one had a relatively large program (say 900k SLOC in C#), all commented/documented thoroughly, well organized and working well. The entire code base was written by a single senior developer who no longer with the company. All the code is testable as is and IoC is used throughout--except for some strange reason they did not write any unit tests. Now, your company wants to branch the code and wants unit tests added to detect when changes break the core functionality.

Is adding tests a good idea?

If so, how would one even start on something like this?

EDIT

OK, so I had not expected answers making good arguments for opposite conclusions. The issue may be out of my hands anyway. I've read through the "duplicate questions" as well and the general consensus is that "writing tests is good"...yeah, but not too helpful in this particular case.

I don't think I am alone here in contemplating writing tests for a legacy system. I'm going to keep metrics on how much time is spent and how many times the new tests catch problems (and how many times they don't). I'll come back and update this a year or so from now with my results.

CONCLUSION

So it turns out that it is basically impossible to just add unit test to existing code with any semblance of orthodoxy. Once the code is working you obviously cannot red-light/green-light your tests, it usually not clear which behaviors are important to test, not clear where to begin and certainly not clear when you are finished. Really even asking this question misses the main point of writing tests in the first place. In the majority of cases I found it actually easier to re-write the code using TDD than to decipher the intended functions and  retroactively add in unit tests. When fixing a problem or adding a new feature it is a different story, and I believe that this is the time to add unit tests (as some pointed out below). Eventually most code gets rewritten, often sooner than you'd expect--taking this approach I've been able to add test coverage to a surprisingly large chunk of the existing codebase.

答:
OK, I'm going to give the contrary opinion....

Adding tests to an existing, working system is going to alter that system, unless that is, the system is all written with mocking in mind from the start. I doubt it, though it's quite possible it has good separation of all components with easily definable boundaries that you can slip your mock interfaces into. But if it isn't, then you are going to have to make what are quite significant (relatively speaking) changes that may well break things. In the best case, you're going to spend a load of time writing these tests, time that could be better spent writing a load of detailed design documents, impact analysis documents or solution configuration documents instead. After all, that's the work your boss wants done more than unit tests. Isn't it?

Anyway, I would not add any unit tests whatsoever.

I would concentrate on external, automated testing tools that will give you a reasonable coverage without changing a thing. Then, when you come to make modifications... that's when you can start adding unit tests inside the codebase.

ContentLicense: CC BY-SA 3.0

Title: Writing Tests for Existing Code


问:
Is a data service API a good idea?
We currently have a monolithic Rails application that is really three separate applications that make use of the same data. In trying to figure out how to properly compartmentalize and break the mono-app into smaller pieces, it seems apparent that we need to have a central data service that the various pieces can communicate with, because I don't want to have to redefine a model layer for each application and/or copy models between the various applications (DRY). The idea is that since the database/persistance (including concepts like validations) are going to be the same for each component, it would make sense to have a service that can be used to query for data and create/update/delete it.

What I'm picturing is essentially a JSON API that sits between the database and my application layer. The API will not immediately be exposed to the "outside world" (although that may be something that occurs in the future); for now, it would just be communicated with internally (ie. to/from a public-facing Rails (or other) app). This API would essentially just abstract persistence logic.

Is this a good idea? Are there any pitfalls that I'm not thinking of?

If this is a good idea, does anyone know of a Rails ORM or a gem that can be used to coordinate this sort of endeavour? My vision is that once a data service is running, the actual Rails application wouldn't be using ActiveRecord anymore, but rather something which would handle querying the data API.

答:
To answer your questions:

1) Yes, it's a great idea for the good reasons cited by @syrion. You are really talking about taking a (buzzword warning) service-oriented approach. If you view it that way then you might want to broaden the scope of your services beyond CRUD operations and include business logic that you may use across your applications (it's hard to give a specific example without knowing more about your app). A very good treatment of this topic, including Rack and Sinatra options, is Service-Oriented Design with Ruby and Rails by Paul Dix.

2) There are a few ways to handle this from the perspective of the Rails apps that consume the services. In a similar situation I wrote my own gem as an SDK to my services. Internally I used HTTParty for communicating with the services. You might also want to check out ActiveModel as a replacement for ActiveRecord in this situation.

Title: Is a data service API a good idea?


问:
"Everything is a Map", am I doing this right?
I watched Stuart Sierra's talk "Thinking In Data" and took one of the ideas from it as a design principle in this game I'm making. The difference is he's working in Clojure and I'm working in JavaScript.  I see some major differences between our languages in that:

Clojure is idiomatically functional programming

Most state is immutable

I took the idea from the slide "Everything is a Map" (From 11 minutes, 6 seconds to > 29 minutes in).  Some things he says are:

Whenever you see a function that takes 2-3 arguments, you can make a case for turning it into a map and just passing a map in.  There are a lot of advantages to that:

You don't have to worry about argument order

You don't have to worry about any additional information.  If there are extra keys, that's not really our concern.  They just flow through, they don't interfere.

You don't have to define a schema

As opposed to passing in an Object there's no data hiding.  But, he makes the case that data hiding can cause problems and is overrated:

Performance

Ease of implementation

As soon as you communicate over the network or across processes, you have to have both sides agree on the data representation anyway.  That's extra work you can skip if you just work on data.

Most relevant to my question. This is 29 minutes in: "Make your functions composable". Here's the code sample he uses to explain the concept:

```<code>;; Bad
(defn complex-process []
  (let [a (get-component @global-state)
        b (subprocess-one a)
        c (subprocess-two a b)
        d (subprocess-three a b c)]
    (reset! global-state d)))

;; Good
(defn complex-process [state]
  (-> state
    subprocess-one
    subprocess-two
    subprocess-three))
</code>```

I understand the majority of programmers aren't familiar with Clojure, so I'll rewrite this in imperative style:

```<code>;; Good
def complex-process(State state)
  state = subprocess-one(state)
  state = subprocess-two(state)
  state = subprocess-three(state)
  return state
</code>```

Here are the advantages:

Easy to test

Easy to look at those functions in isolation

Easy to comment out one line of this and see what the outcome is by removing a single step

Each subprocess could add more information on to the state.  If subprocess one needs to communicate something to subprocess three, it's as simple as adding a key/value.

No boilerplate to extract the data you need out of the state just so that you can save it back in.  Just pass in the whole state and let the subprocess assign what it needs.

Now, back to my situation:  I took this lesson and applied it to my game.  That is, almost all of my high level functions take and return a gameState object.  This object contains all the data of the game.  EG: A list of badGuys, a list of menus, the loot on the ground, etc.  Here's an example of my update function:

```<code>update(gameState)
  ...
  gameState = handleUnitCollision(gameState)
  ...
  gameState = handleLoot(gameState)
  ...
</code>```

What I'm here to ask about is, have I created some abomination that perverted an idea that is only practical in a functional programming language?  JavaScript isn't idiomatically functional (though it can be written that way) and it's really challenging to write immutable data structures.  One thing that concerns me is he assumes that each of those subprocesses are pure. Why does that assumption need to be made?  It's rare that any of my functions are pure (by that, I mean they often modify the gameState.  I don't have any other complicated side effects other than that).  Do these ideas fall apart if you don't have immutable data?

I'm worried that one day I'll wake up and realize this whole design is a sham and I've really just been implementing the Big Ball Of Mud anti-pattern.

Honestly, I've been working on this code for months and it's been great.  I feel like I'm getting all the advantages he's claimed.  My code is super easy for me to reason about.  But I'm a one man team so I have the curse of knowledge.

Update

I've been coding 6+ months with this pattern. Usually by this time I forget what I've done and that's where "did I write this in a clean way?" comes into play. If I haven't, I'd really struggle. So far, I'm not struggling at all.

I understand how another set of eyes would be necessary to validate its maintainability. All I can say is I care about maintainability first and foremost. I'm always the loudest evangelist for clean code no matter where I work.

I want to reply directly to those that already have a bad personal experience with this way of coding.  I didn't know it then, but I think we're really talking about two different ways of writing code.  The way I've done it appears to be more structured than what others have experienced.  When someone has a bad personal experience with "Everything is a map" they talk about how hard it is to maintain because:

You never know the structure of the map that the function requires

Any function can mutate the input in ways you'd never expect.  You have to look all over the code base to find out how a particular key got into the map or why it disappeared.

For those with such an experience, perhaps the code base was, "Everything takes 1 of N types of maps." Mine is, "Everything takes 1 of 1 type of map". If you know the structure of that 1 type, you know the structure of everything.  Of course, that structure usually grows over time.  That's why...

There's one place to look for the reference implementation (ie: the schema). This reference implementation is code the game uses so it can't get out of date.

As for the second point, I don't add/remove keys to the map outside of the reference implementation, I just mutate what's already there. I also have a large suite of automated tests.

If this architecture eventually collapses under its own weight, I'll add a second update.  Otherwise, assume everything is going well :)

答:
Whether this is a good idea or not will really depend on what you're doing with the state inside those subprocesses. If I understand the Clojure example correctly, the state dictionaries that are being returned are not the same state dictionaries that are being passed in. They are copies, possibly with additions and modifications, that (I assume) Clojure is able to efficiently create because the functional nature of the language depends on it. The original state dictionaries to each function are not modified in any way.

If I'm understanding correctly, you are modifying the state objects you pass into your javascript functions rather than returning a copy, which means you are doing something very, very, different from what that Clojure code is doing. As Mike Partridge pointed out, this is basically just a global that you explicitly pass to and return from functions for no real reason. At this point I think it's simply making you think you're doing something that you actually aren't.

If you actually are explicitly making copies of the state, modifying it, and then returning that modified copy, then carry on. I'm not sure that's necessarily the best way to accomplish what you're trying to do in Javascript, but it is probably "closeish" to what that Clojure example is doing.


问:
Do SAS coding "best practices" exist?
At work, we develop a lot of our applications in SAS, from start to finish. One of the problems with this approach is that SAS is a very verbose language with very few language constructs; there is limited support for variables, very limited support for basic functions, and no such thing as classes. They have a concept called "macros" which essentially text substitution; you define a macro, and on invocation, it simply drops in the content of the macro.

My question is, is anyone familiar with "best coding practices" for use in developing SAS applications? Are there software design patterns for SAS? I've gone through Code Complete, and some of what he writes is applicable to SAS, but much of it is not, as the concepts don't exist in SAS. Can anyone give recommendations for writing maintainable, well-designed SAS code?

答:
This is tough to answer in the abstract, since SAS is such a huge product you could be talking about GUI applications, ETL routines, risk modeling applications, and probably other things I'm not thinking about.

That said, some googling will turn up this classic toungue-in-cheek paper:
Programming For Job Security Revisited: Even More Tips And Techniques To Maximize Your Indispensability (pdf).

That offers an excellent guide to what should be avoided.

If your applications are ETL-ish at all, you might have a look at this blog post I wrote a year ago.

Title: Do SAS coding "best practices" exist?


答:
What you seem to be doing is, effectively, a manual State monad; what I would do is build a (simplified) bind combinator and re-express the connections between your logical steps using that:

```<code>function stateBind() {
    var computation = function (state) { return state; };
    for ( var i = 0 ; i < arguments.length ; i++ ) {
        var oldComp = computation;
        var newComp = arguments[i];
        computation = function (state) { return newComp(oldComp(state)); };
    }
    return computation;
}

...

stateBind(
  subprocessOne,
  subprocessTwo,
  subprocessThree,
);
</code>```

You can even use stateBind to build the various subprocesses from subsubprocesses, and continue down a tree of binding combinators to structure your computation appropriately.

For an explanation of the full, unsimplified State monad, and an excellent introduction to monads in general in JavaScript, see this blog post.

Title: "Everything is a Map", am I doing this right?

Body:
I watched Stuart Sierra's talk "Thinking In Data" and took one of the ideas from it as a design principle in this game I'm making. The difference is he's working in Clojure and I'm working in JavaScript.  I see some major differences between our languages in that:

Clojure is idiomatically functional programming

Most state is immutable

I took the idea from the slide "Everything is a Map" (From 11 minutes, 6 seconds to > 29 minutes in).  Some things he says are:

Whenever you see a function that takes 2-3 arguments, you can make a case for turning it into a map and just passing a map in.  There are a lot of advantages to that:

You don't have to worry about argument order

You don't have to worry about any additional information.  If there are extra keys, that's not really our concern.  They just flow through, they don't interfere.

You don't have to define a schema

As opposed to passing in an Object there's no data hiding.  But, he makes the case that data hiding can cause problems and is overrated:

Performance

Ease of implementation

As soon as you communicate over the network or across processes, you have to have both sides agree on the data representation anyway.  That's extra work you can skip if you just work on data.

Most relevant to my question. This is 29 minutes in: "Make your functions composable". Here's the code sample he uses to explain the concept:

```<code>;; Bad
(defn complex-process []
  (let [a (get-component @global-state)
        b (subprocess-one a)
        c (subprocess-two a b)
        d (subprocess-three a b c)]
    (reset! global-state d)))

;; Good
(defn complex-process [state]
  (-> state
    subprocess-one
    subprocess-two
    subprocess-three))
</code>```

I understand the majority of programmers aren't familiar with Clojure, so I'll rewrite this in imperative style:

```<code>;; Good
def complex-process(State state)
  state = subprocess-one(state)
  state = subprocess-two(state)
  state = subprocess-three(state)
  return state
</code>```

Here are the advantages:

Easy to test

Easy to look at those functions in isolation

Easy to comment out one line of this and see what the outcome is by removing a single step

Each subprocess could add more information on to the state.  If subprocess one needs to communicate something to subprocess three, it's as simple as adding a key/value.

No boilerplate to extract the data you need out of the state just so that you can save it back in.  Just pass in the whole state and let the subprocess assign what it needs.

Now, back to my situation:  I took this lesson and applied it to my game.  That is, almost all of my high level functions take and return a gameState object.  This object contains all the data of the game.  EG: A list of badGuys, a list of menus, the loot on the ground, etc.  Here's an example of my update function:

```<code>update(gameState)
  ...
  gameState = handleUnitCollision(gameState)
  ...
  gameState = handleLoot(gameState)
  ...
</code>```

What I'm here to ask about is, have I created some abomination that perverted an idea that is only practical in a functional programming language?  JavaScript isn't idiomatically functional (though it can be written that way) and it's really challenging to write immutable data structures.  One thing that concerns me is he assumes that each of those subprocesses are pure. Why does that assumption need to be made?  It's rare that any of my functions are pure (by that, I mean they often modify the gameState.  I don't have any other complicated side effects other than that).  Do these ideas fall apart if you don't have immutable data?

I'm worried that one day I'll wake up and realize this whole design is a sham and I've really just been implementing the Big Ball Of Mud anti-pattern.

Honestly, I've been working on this code for months and it's been great.  I feel like I'm getting all the advantages he's claimed.  My code is super easy for me to reason about.  But I'm a one man team so I have the curse of knowledge.

Update

I've been coding 6+ months with this pattern. Usually by this time I forget what I've done and that's where "did I write this in a clean way?" comes into play. If I haven't, I'd really struggle. So far, I'm not struggling at all.

I understand how another set of eyes would be necessary to validate its maintainability. All I can say is I care about maintainability first and foremost. I'm always the loudest evangelist for clean code no matter where I work.

I want to reply directly to those that already have a bad personal experience with this way of coding.  I didn't know it then, but I think we're really talking about two different ways of writing code.  The way I've done it appears to be more structured than what others have experienced.  When someone has a bad personal experience with "Everything is a map" they talk about how hard it is to maintain because:

You never know the structure of the map that the function requires

Any function can mutate the input in ways you'd never expect.  You have to look all over the code base to find out how a particular key got into the map or why it disappeared.

For those with such an experience, perhaps the code base was, "Everything takes 1 of N types of maps." Mine is, "Everything takes 1 of 1 type of map". If you know the structure of that 1 type, you know the structure of everything.  Of course, that structure usually grows over time.  That's why...

There's one place to look for the reference implementation (ie: the schema). This reference implementation is code the game uses so it can't get out of date.

As for the second point, I don't add/remove keys to the map outside of the reference implementation, I just mutate what's already there. I also have a large suite of automated tests.

If this architecture eventually collapses under its own weight, I'll add a second update.  Otherwise, assume everything is going well :)

Body:
What you seem to be doing is, effectively, a manual State monad; what I would do is build a (simplified) bind combinator and re-express the connections between your logical steps using that:

function stateBind() {
    var computation = function (state) { return state; };
    for ( var i = 0 ; i < arguments.length ; i++ ) {
        var oldComp = computation;
        var newComp = arguments[i];
        computation = function (state) { return newComp(oldComp(state)); };
    }
    return computation;
}

...

stateBind(
  subprocessOne,
  subprocessTwo,
  subprocessThree,
);

You can even use stateBind to build the various subprocesses from subsubprocesses, and continue down a tree of binding combinators to structure your computation appropriately.

For an explanation of the full, unsimplified State monad, and an excellent introduction to monads in general in JavaScript, see this blog post.


问:
Match two strings but allow for a degree of error
How can I match two strings, but at the same time allow for X number of characters to be incorrect in the match. The number of errors should be a controllable variable.

While X number of characters can not match in the string there should be a limit as to how many run in a sequence. Given two strings I might allow 5 characters to be different, but not more than 2 in a row.

I'm looking for a recommended algorithm for comparing these two strings, or maybe there is already a known solution for this.

答:
An approximate string search starting point is that of the Levenshtein distance.  This algorithim counts the number of single character edits (insert, delete, and substitution) to change one word into another.

An example of this is kitten --> sitting which has an edit distance of three

kitten -> sitten (substitute 's' for 'k')

sitten -> sittin (substitute 'i' for 'e')

sittin -> sitting (add 'g' at the end)

There are variations on this algorithm, notably the Damerau–Levenshtein distance which allows for the transposition of two adjacent characters ('hte' to 'the' has a DL Distance of 1 and a Levenshtein distance of 2) and thus is often more appropriate for spell checking.  Other variations exist for applications where gaps are important (DNA strings).

The Levenshtein distance is well known and not too difficult to find (I once had cause to hunt up an implementation of it as a function in oracle - it was much faster than pulling down all the data and then running the query code side).  Rosettacode has a multitude (54) of implemntations of the Levenshtein distance (note that some languages have this as part of the string library somewhere - if you are doing Java, look at the apache commons lang).  Wikibooks has 31 implementations and a cursory glance at the two do not show the same code for the same language.

The way this works is it builds up a matrix that corresponds to the relationship between the two strings:

```<code> .kitten
.0123456
s1123456
i2212345
t3321234
t4432123
i5543223
n6654332
g7765443
</code>```

The . row and column represent that you can get to the target string by 'just' inserting each letter from a empty string.  This isn't the ideal case, but it is there to seed the algorithm.

If the value is the same that spot ('i' == 'i'), the value is the same as value diagonally up to the left. If the two spots are dissimilar ('s' != 'k') the value is the the minimum of:

diagonal up and to the left + 1 (a substitution)

directly above + 1 (an insertion)

directly to the left + 1 (a deletion)

The edit distance return value is the value in the lower right of the matrix.

If you follow from the lower right to the upper left with the minimum, you can see the edits done:

```<code> .kitten
.0.   .
s.1   .
i  1  .
t   1 .
t    1.
i.....2
n      2
g......3
</code>```

Do note that this is the rather memory intensive approach.  It can be reduced in memory scope by not building the full matrix - all the algorithm cares about is a subset of the data and it can be reduced from N*M space to 2*max(N,M) space by just storing the the previous row (and what has been calculated on the current row).  Code Project shows how this can be done (with C# code to download).

Body:
An approximate string search starting point is that of the Levenshtein distance.  This algorithim counts the number of single character edits (insert, delete, and substitution) to change one word into another.

An example of this is kitten --> sitting which has an edit distance of three

kitten -> sitten (substitute 's' for 'k')

sitten -> sittin (substitute 'i' for 'e')

sittin -> sitting (add 'g' at the end)

There are variations on this algorithm, notably the Damerau–Levenshtein distance which allows for the transposition of two adjacent characters ('hte' to 'the' has a DL Distance of 1 and a Levenshtein distance of 2) and thus is often more appropriate for spell checking.  Other variations exist for applications where gaps are important (DNA strings).

The Levenshtein distance is well known and not too difficult to find (I once had cause to hunt up an implementation of it as a function in oracle - it was much faster than pulling down all the data and then running the query code side).  Rosettacode has a multitude (54) of implemntations of the Levenshtein distance (note that some languages have this as part of the string library somewhere - if you are doing Java, look at the apache commons lang).  Wikibooks has 31 implementations and a cursory glance at the two do not show the same code for the same language.

The way this works is it builds up a matrix that corresponds to the relationship between the two strings:

 .kitten
.0123456
s1123456
i2212345
t3321234
t4432123
i5543223
n6654332
g7765443

The . row and column represent that you can get to the target string by 'just' inserting each letter from a empty string.  This isn't the ideal case, but it is there to seed the algorithm.

If the value is the same that spot ('i' == 'i'), the value is the same as value diagonally up to the left. If the two spots are dissimilar ('s' != 'k') the value is the the minimum of:

diagonal up and to the left + 1 (a substitution)

directly above + 1 (an insertion)

directly to the left + 1 (a deletion)

The edit distance return value is the value in the lower right of the matrix.

If you follow from the lower right to the upper left with the minimum, you can see the edits done:

 .kitten
.0.   .
s.1   .
i  1  .
t   1 .
t    1.
i.....2
n      2
g......3

Do note that this is the rather memory intensive approach.  It can be reduced in memory scope by not building the full matrix - all the algorithm cares about is a subset of the data and it can be reduced from N*M space to 2*max(N,M) space by just storing the the previous row (and what has been calculated on the current row).  Code Project shows how this can be done (with C# code to download).


问:
Maintaining log stream after file name change
I'm trying to log to a file and move it about every 30 seconds.  I'm using the os.rename function (which I do from a supervisord process) in python to move the current log to a timestamped log (which will then be processed by another python script) and then I continue to log to the old stream from my uwsgi server.

I've also tried the RotatingFileHandler and the timed version, but they don't seem to work properly.  I wasn't sure if that was a function of using it with the uwsgi server or not.

My question is, what is the simplest and most efficient way to continue logging to the same file name after the file it was "pointing to" has been moved?

答: I appreciate the help, but in the end the simplest method was just opening and closing the file in python for each write.  It may not be the most efficient, but its working.</p>


问:
SOLID vs. Avoiding Premature Abstraction
I understand what SOLID is supposed to accomplish and use it regularly in situations where modularity is important and its goals are clearly useful.  However, two things prevent me from applying it consistently across my codebase:

I want to avoid premature abstraction.  In my experience drawing abstraction lines without concrete use cases (the kind that exist now or in the foreseeable future) leads to them being drawn in the wrong places.  When I try to modify such code, the abstraction lines get in the way rather than helping.  Therefore, I tend to err on the side of not drawing any abstraction lines until I have a good idea of where they would be useful.

I find it hard to justify increasing modularity for its own sake if it makes my code more verbose, harder to understand, etc. and doesn't eliminate any duplication.  I find simple, tightly coupled procedural or God object code is sometimes easier to understand than very well-factored ravioli code because the flow is simple and linear.  It's also much easier to write.

On the other hand, this mindset often leads to God objects.  I generally refactor these conservatively, adding clear abstraction lines only when I see clear patterns emerging.  What, if anything, is wrong with God objects and tightly coupled code if you don't clearly need more modularity, don't have significant duplication and the code is readable?

EDIT:  As far as individual SOLID principles, I meant to emphasize that Liskov Substitution is IMHO a formalization of common sense and should be applied everywhere, since abstractions make no sense if it isn't.  Also, every class should have a single responsibility at some level of abstraction, though it may be a very high level with the implementation details all crammed into one huge 2,000 line class.  Basically, your abstractions should make sense where you choose to abstract.  The principles I question in cases where modularity isn't clearly useful are open-closed, interface segregation and especially dependency inversion, since these are about modularity, not just having abstractions make sense.

答: An important point that seems to have been overlooked is that SOLID is practiced along with TDD. TDD tends to highlight what is "appropriate" in your particular context and help alleviate a lot of the equivocation that appears to be in the advice.</p>


问:
Why are reference-counting smart pointers so popular?
As I can see, smart pointers are used extensively in many real-world C++ projects.

Though some kind of smart pointers are obviously beneficial to support RAII and ownership transfers, there is also a trend of using shared pointers by default, as a way of "garbage collection", so that the programmer do not have to think about allocation that much.

Why are shared pointers more popular than integrating a proper garbage collector like Boehm GC? (Or do you agree at all, that they are more popular than actual GCs?)

I know about two advantages of conventional GCs over reference-counting:

Conventional GC algorithms has no problem with reference-cycles.

Reference-count is generally slower than a proper GC.

What are the reasons for using reference-counting smart pointers?

答:
Some advantages of reference counting over garbage collection:

Low overhead.  Garbage collectors can be quite intrusive (e.g. making your program freeze up at unpredictable times while a garbage collection cycle processes) and quite memory-intensive (e.g. your process's memory footprint unnecessarily grows to many megabytes before garbage-collection finally kicks in)

More predictable behavior.  With reference counting, you are guaranteed that your object will be freed the instant the last reference to it goes away.  With garbage collection, on the other hand, your object will be freed "sometime", when the system gets around to it.  For RAM this isn't usually a big problem on desktops or lightly loaded servers, but for other resources (e.g. file handles) you often need them be closed ASAP to avoid potential conflicts later on.

Simpler.  Reference counting can be explained in a few minutes, and implemented in an hour or two.  Garbage collectors, especially ones with decent performance, are extremely complex and not many people understand them.

Standard.  C++ includes reference counting (via shared_ptr) and friends in the STL, which means that most C++ programmers are familiar with it and most C++ code will work with it.  There isn't any standard C++ garbage collector, though, which means that you have to choose one and hope it works well for your use case -- and if it doesn't, it's your problem to fix, not the language's.

As for the alleged downsides of reference counting -- not detecting cycles is an issue, but one that I've never personally ran into in the last ten years of using reference counting.  Most data structures are naturally acyclical, and if you do come across a situation where you need cyclical references (e.g. parent pointer in a tree node) you can just use a weak_ptr or a raw C pointer for the "backwards direction".  As long as you are aware of the potential problem when you're designing your data structures, it's a non-issue.

As for performance, I've never had a problem with the performance of reference counting.  I have had problems with the performance of garbage collection, in particular the random freeze-ups that GC can incur, to which the only solution ("don't allocate objects") might as well be rephrased as "don't use GC".


答:
At a very low level, it is necessary to open and close the files to change the file a logger is writing to.  This isn't an option, it is a necessity with how files really work.

The open() call takes a pathname and returns back a file descriptor.  This points to the file itself.  Modifications to the directory entry for the file are outside of this and the file descriptor really doesn't care.

A demonstration of this can be seen on a unix system -

Fire up three shell windows and cd to /tmp in each of them

In the first shell, cat > file.tmp

In the second shell, tail -f file.tmp

At this point, anything you type in the cat window will appear on the tail window (hitting return may be required)

In the third shell, mv file.tmp file.foo

You've moved the file, but if you keep typing in the cat window, it will still appear in the tail window.

In the third shell, rm file.foo

And still, what you type in the cat window shows up in the tail window, even though there's no file in /tmp anymore.

In #2, a file was opened, created on the disk, and a file descriptor was used by the shell.  In #3, the file on the disk was opened for read, and it got back a file descriptor too.  The two processes are reading and writing to a file that they each access with their own file descriptor.  Moving the file on the disk doesn't affect these processes - they are still accessing the same file.  Whats more, even though the file is deleted from the directory entry, the file exists as a common place for those two processes (until they each close the file).

The only way to break the connection between a file descriptor and the file is to close the file and open a new file to get back a new file descriptor.

Many logging frameworks have a way of doing this open and close for you.  In Python (I admit I'm just reading the docs on this), the TimedRotatingFileHandler will rotate the log (and this is probably the important bit) on the next log after the time it is set to rotate.

Lets say you have a logger that spits out about one message every 10 minutes on the 5 (so 5, 15, 25, 35, 45, and 55).  You also have a log rotator that rotates on the hour.  The log file will not appear to be rotated until the 05 log message is written, then the logger realizes that it is after the time it needs to rotate and then rotates the log file, writing the new 05 message to a new file.

A not uncommon way to solve this (and get logs rotating when you want them to) is to log a message of high enough priority to cause the logs to rotate just after the time to rotate them.

The other thing to do is wait awhile.  Do you really need to move that log file now? Or can it sit for awhile?

Title: Maintaining log stream after file name change

Body:
I'm trying to log to a file and move it about every 30 seconds.  I'm using the os.rename function (which I do from a supervisord process) in python to move the current log to a timestamped log (which will then be processed by another python script) and then I continue to log to the old stream from my uwsgi server.

I've also tried the RotatingFileHandler and the timed version, but they don't seem to work properly.  I wasn't sure if that was a function of using it with the uwsgi server or not.

My question is, what is the simplest and most efficient way to continue logging to the same file name after the file it was "pointing to" has been moved?


答:
The Devil's Advocate

I think this question deserves a devil's advocate (but of course I'm biased).  I think @KarlBielefeldt is making very good points and I'd like to address them.  First I want to say that his points are great.

Since he mentioned this isn't a good pattern even in functional programming, I'll consider JavaScript and/or Clojure in my replies.  One extremely important similarity between these two languages is they're dynamically typed.  I'd be more agreeable with his points if I were implementing this in a statically typed language like Java or Haskell.  But, I'm going to consider the alternative to the "Everything is a Map" pattern to be a traditional OOP design in JavaScript and not in a statically typed language (I hope I'm not setting up a strawman argument by doing this, please let me know).

For example, try to answer the following questions about each subprocess function:

Which fields of state does it require?

Which fields does it modify?

Which fields are unchanged?

In a dynamically typed language, how would you normally answer these questions?  A function's first parameter may be named foo, but what is that?  An array? An object?  An object of arrays of objects?  How do you find out?  The only way I know of is to

read the documentation

look at the function body

look at the tests

guess and run the program to see if it works.

I don't think the "Everything is a Map" pattern makes any difference here.  These are still the only ways I know of to answer these questions.

Also keep in mind that in JavaScript and most imperative programming languages, any function can require, modify and ignore any state it can access and the signature makes no difference: The function/method could do something with global state or with a singleton.  Signatures often lie.

I'm not trying to set up a false dichotomy between "Everything is a Map" and poorly designed OO code.  I'm just trying to point out that having signatures that take in less/more fine/coarse grained parameters doesn't guarantee you know how to isolate, setup and call a function.

But, if you would allow me to use that false dichotomy: Compared to writing JavaScript in the traditional OOP way, "Everything is a Map" seems better.  In the traditional OOP way, the function may require, modify or ignore state that you pass in or state that you don't pass in.  With this "Everything is a Map" pattern, you only require, modify or ignore state that you pass in.

Can you safely rearrange the order of the functions?

In my code, yes. See my second comment to @Evicatos's answer.  Perhaps this is only because I'm making a game though, I can't say.  In a game that's updating 60x a second, it doesn't really matter if dead guys drop loot then good guys pick up loot or vice versa.  Each function still does exactly what it's supposed to do regardless of the order they're run.  The same data just gets fed into them at a different update calls if you swap the order.  If you have good guys pick up loot then dead guys drop loot, the good guys will pick up the loot in the next update and it's no big deal.  A human won't be able to notice the difference.

At least this has been my general experience.  I feel really vulnerable admitting this publicly.  Maybe considering this to be okay is a very, very bad thing to do.  Let me know if I've made some terrible mistake here.  But, if I have, it's extremely easy to rearrange the functions so the order is dead guys drop loot then good guys pick up loot again.  It will take less time than the time it took to write this paragraph :P

Maybe you think "dead guys should drop loot first. It would be better if your code enforced that order".  But, why should enemies have to drop loot before you can pick loot up?  To me that doesn't make sense.  Maybe the loot was dropped 100 updates ago.  It's not necessary to check if an arbitrary bad guy has to pick up loot that's already on the ground.  That's why I think the order of these operations is completely arbitrary.

It's natural to write decoupled steps with this pattern, but it's difficult to notice your coupled steps in traditional OOP.  If I were writing traditional OOP, the natural, naive way of thinking is to make the dead guys drop loot return a Loot object that I have to pass into the good guys pick up loot.  I wouldn't be able to reorder those operations since the first returns the input of second.

In an object oriented language, the pattern makes even less sense, because tracking state is what objects do.

Objects have state and it's idiomatic to mutate the state making its history just disappear... unless you manually write code to keep track of it.  In what way is tracking state "what they do"?

Also, the benefits of immutability go way down the larger your immutable objects get.

Right, as I said, "It's rare that any of my functions are pure".  They always only operate on their parameters, but they mutate their parameters.  This is a compromise I felt I had to make when applying this pattern to JavaScript.


问:
Inheritance, commands and event sourcing
In order not to redo things several times I wanted to factorize common stuff. For Instance, let's say we have a cow and a horse.  The cow produces milk, the horse runs fast, but both eat grass.

```<code>public class Herbivorous
{
   protected int Quantity;

   public void EatGrass(int quantity)
   {
      var evt= Build.GrassEaten
                    .WithQuantity(quantity);
      RaiseEvent(evt);
   }

   public void Apply(GrassEaten evt)
   {
       _quantity= evt.Quantity;
   }
}

public class Horse : Herbivorous
{
   private bool _HasFastRun;

   public void RunFast()
   {
      var evt= Build.FastRun;
      RaiseEvent(evt);
   }

   public void Apply(FastRunevt)
   {
       _HasFastRun= true;
   }
}

public class Cow: Herbivorous
{
   private bool _IsMilkProduced;

   public void ProduceMilk()
   {
      var evt= Build.MilkProduced;
      RaiseEvent(evt);
   }

   public void Apply(MilkProduced evt)
   {
       _IsMilkProduced= true;
   }
}
</code>```

To eat Grass, my application receives a command in Json or xml, or whatever that deserialise into this class:

```<code>namespace Herbivorous
{
   public class EatGrass : CommandBase
   {
      public Guid IdHerbivorous {get; set;}
      public Guid CommitId {get; set;}
      public long Version {get; set;}
      public int Quantity {get; set;}
   }
}
</code>```

The command handler should be :

```<code>public class EatGrassHandler : CommandHandler<EatGrass>
{
   public override CommandValidation Execute(EatGrass cmd)
   {
      Contract.Requires<ArgumentNullException>(cmd != null);
      Herbivorous herbivorous= EventRepository.GetById<Herbivorous>(cmd.Id);
      if (herbivorous.IsNull())
         throw new AggregateRootInstanceNotFoundException();
      herbivorous.EatGrass(cmd.Quantity);
      EventRepository.Save(herbivorous, cmd.CommitId);
   }
}
</code>```

so far so good. I get a Herbivorous object , I have access to its EatGrass function, whether it is a horse or a cow doesn't matter really. The only problem is here :

```<code>EventRepository.GetById<Herbivorous>(cmd.Id)
</code>```

Indeed, let's imagine we have a cow that has produced milk during the morning and now wants to eat grass.  The EventRepository contains an event MilkProduced, and then come the command EatGrass.
With the CommandHandler, we are no longer in the presence of a cow and the herbivorious doesn't know anything about producing milk . what should it do?

Should I have explicit contextual command  like :

```<code>namespace Herbivorous.Cow
    {
       public class EatGrass : CommandBase
       {
          public Guid IdHerbivorous {get; set;}
          public Guid CommitId {get; set;}
          public long Version {get; set;}
          public int Quantity {get; set;}
       }
       public class ProduceMilk : CommandBase
       {
          public Guid IdHerbivorous {get; set;}
          public Guid CommitId {get; set;}
          public long Version {get; set;}
       }
    }
</code>```

This would mean that the external component that asks my herbivorous to eat grass should know that in this bounded context we talk about a cow. in the former command, we were talking about a general Herbivorous behavior, so the context of my call was not important. We knew that we needed herbivorous to eat grass that was all.

This is my main problem possible leakage of domain specific from one bounded context to another.
Actually It might mean too that I cannot support abstraction when working on interfaces between several applications. I wonder...

Or I could just accept any event when rebuilding my herbivorous. It means that if it does not find a method to apply this event to, he will goes just fine trying to apply the next one of its stream. This is the real simple solution, but it does not put me at ease to know  that events might not be applied ( and not producing errors) when rehydrating my object. (Actually the more I think about it the less I feel guilty..)

Thanks for your help, I am just beginning with these kinds of problem, and I would be glad to have some news from someone more experienced.

答:
It's okay for your Herbivorous class to skip events it doesn't understand.

I learned a lot about this and related issues in this thread.

Hope that helps. I wish there were cows and horses in my domain!!


答:
I've supported an application where 'everything is a map' before. It's a terrible idea. PLEASE don't do it!

When you specify the arguments that are passed to the function, that makes it very easy to know what values the function needs.  It avoids passing extraneous data to the function that just distracts th programmer - every value passed implies that it's needed, and that makes the programmer supporting your code have to figure out why the data is needed.

On the other hand, if you pass everything as a map, the programmer supporting your app will have to fully understand the called function in every way to know what values the map needs to contain.  Even worse, it's very tempting to re-use the map passed to the current function in order to pass data to the next functions.  This means that the programmer supporting your app needs to know all functions called by the current function in order to understand what the current function does.  That's exactly the opposite of the purpose for writing functions - abstracting problems away so that you don't have to think about them!  Now imagine 5 calls deep and 5 calls wide each.  That's a hell of a lot to keep in your mind and a hell of a lot of mistakes to make.

"everything is a map" also seems to lead to using the map as a return value.  I've seen it.  And, again, it's a pain.  The called functions need to never overwrite each other's return value - unless you know the functionality of everything and know that the input map value X needs to be replaced for the next function call.  And the current function needs to modify the map to return it's value, which must sometimes overwrite the previous value and must sometimes not.

edit - example

Here's an example of where this was problematic.  This was a web application.  User input was accepted from the UI layer and placed in a map.  Then functions were called to process the request.  The first function set would check for erroneous input.  If there was an error, the error message would be put in the map.  The calling function would check the map for this entry and write the value in the ui if it existed.

The next function set would start the business logic.  Each function would take the map, remove some data, modify some data, operate on the data in the map and put the result in the map, etc.  Subsequent functions would expect results from prior functions in the map.  In order to fix a bug in a subsequent function, you had to investigate all prior functions as well as a the caller to determine everywhere the expected value might have been set.

The next functions would pull data from the database.  Or, rather, they'd pass a the map to the data access layer.  The DAL would check if the map contained certain values to control how the query executed.  If 'justcount' was a key, then the query would be 'count select foo from bar'.  Any of the functions that was previously called might have ben the one that added 'justcount' to the map.  The query results would be added to the same map.

The results would bubble up to the caller (business logic) which would check the map for what to do.  Some of this would come from things that were added to the map by the initial business logic. Some would come from the data from the database.  The only way to know where it came from was to find the code that added it.  And the other location that can also add it.

The code was effectively a monolithic mess, that you had to understand in it's entirety to know where a single entry in the map came from.


答:
To get good performance out of a GC, the GC needs to be able to move objects in memory. In a language like C++ where you can interact directly with memory locations, this is pretty much impossible. (Microsoft C++/CLR doesn't count because it introduces new syntax for GC-managed pointers and is thus effectively a different language.)

The Boehm GC, while a nifty idea, is actually the worst of both worlds: you need a malloc() that is slower than a good GC, and so you lose the deterministic allocation/deallocation behavior without the corresponding performance boost of a generational GC. Plus it is by necessity conservative, so it won't necessarily collect all your garbage anyway.

A good, well-tuned GC can be a great thing. But in a language like C++, the gains are minimal and the costs are often just not worth it.

It will be interesting to see, however, as C++11 becomes more popular, whether lambdas and capture semantics start to lead the C++ community towards the same kinds of allocation and object lifetime problems that caused the Lisp community to invent GCs in the first place.

See also my answer to a related question over on StackOverflow.

Title: Why are reference-counting smart pointers so popular?

Body:
As I can see, smart pointers are used extensively in many real-world C++ projects.

Though some kind of smart pointers are obviously beneficial to support RAII and ownership transfers, there is also a trend of using shared pointers by default, as a way of "garbage collection", so that the programmer do not have to think about allocation that much.

Why are shared pointers more popular than integrating a proper garbage collector like Boehm GC? (Or do you agree at all, that they are more popular than actual GCs?)

I know about two advantages of conventional GCs over reference-counting:

Conventional GC algorithms has no problem with reference-cycles.

Reference-count is generally slower than a proper GC.

What are the reasons for using reference-counting smart pointers?


问:
What's the best way to scale and split an agile team building a web app?
I've recently joined a company where I'm working as the scrum master on an agile development project building a web app.

The team is just about to be the maximum size for an agile team (expecting 9 next week).  We have talked about potentially splitting the team into two teams, not so much to shorten standups (which aren't excessive at the moment) but to stop people from being completely bored in sprint planning sessions (which again aren't excessively long).

There are two very distinct layers to the project - high technical backend dev (like seriously complex), and UI design/build/integration.  It seems that when the backend guys are talking technical the UI guys zone out, and vice versa.  It seems like the logical way to split the team if only to be more time efficient, but I have one massive reservation in that all I might really be doing is reducing collaboration and knowledge sharing.  The two teams just won't really have a good idea about what the rest of the team are building.

Does anyone have any experience in dealing with something like this?

答:
It is indeed a good idea to split the independent parts of the team into new teams. In larger project, it is almost impossible for the developers to be closely familiar with the whole project, so the splitting is still present formal or informal.

Each of the new teams should have a team-leader/technical manager, who has a solid knowledge about the scope of their team and who is familiar with other teams' work as well.

After that each team can have separate scrum meetings, and the leaders of the other teams can be present. This way you will reduce the number of "bored" people, but still the teams will know what others are working on and will be able to successfully collaborate.

The collaboration becomes more important if the scopes of the teams are intersecting or one team depends on the other. But again there is no need for the whole team to be present _ the team-leader can coordinate the collaboration.


问:
Java vs C#: What should a junior programmer (just finished degree) consider when entering the programming world?
I just want to hear some pro and con's - it's obvious that there is no right answer

So would Java be a better choice for a long term career? Or should I opt for .Net since it could be the platform having all the latest advances?

I just want to hear others point of view, so please don't close this question just because it's being subjective

答:
I think you should consider being proficient in both, since I believe it will make you a better programmer than one just knowing one.

For where to start?  Well, where you can pick up a job.  Then learn what they need you to know, and then start picking up what you lack.

Programming is a never ending process of learning, because there is so much to learn that you cannot keep up.

OwnerDisplayName: oneWhoNeedGuidenss


答: When splitting up teams I'm always trying to keep in mind the fact that a team needs to be able to deliver value to the customer. In your case it would be having both backend and frontend developers in the team.</p>

Title: What's the best way to scale and split an agile team building a web app?

Body:
I've recently joined a company where I'm working as the scrum master on an agile development project building a web app.

The team is just about to be the maximum size for an agile team (expecting 9 next week).  We have talked about potentially splitting the team into two teams, not so much to shorten standups (which aren't excessive at the moment) but to stop people from being completely bored in sprint planning sessions (which again aren't excessively long).

There are two very distinct layers to the project - high technical backend dev (like seriously complex), and UI design/build/integration.  It seems that when the backend guys are talking technical the UI guys zone out, and vice versa.  It seems like the logical way to split the team if only to be more time efficient, but I have one massive reservation in that all I might really be doing is reducing collaboration and knowledge sharing.  The two teams just won't really have a good idea about what the rest of the team are building.

Does anyone have any experience in dealing with something like this?


问:
Displaying items with a countdown timer
I am creating a widget for rotating topics. The functionality is as follows:

Each topic is displayed one by one on the homepage and has a duration of 30 seconds.

A countdown timer is displayed on the page. When it reaches zero the current topic gets marked as "shown" and the next topic is displayed.

Users can choose to "like" topics, in which case the topic gets an additional 5 seconds duration (the countdown timer needs to update itself at this point).

The countdown timer always needs to display the current allocated duration for the topic, so for example if a user "likes" a topic then people accessing the site should instantly see the updated duration for that topic. Basically everything needs to be shown in "realtime".
The topics are stored in a MySQL database. I am having trouble with the last two points in the above.

The way I have tried to do it so far is to have an AJAX request which is sent every second to deduct 1 second from the current topic duration. The request response is the current topic duration.

The problem with this is that it will only work so long as somebody is accessing the site. I know I can probably create a CRON script to run this process in the background, but for it to run every second would be a bit crazy. There has got to a be a much simpler solution to this problem. What would be the most efficient way to go about this?

答:
If every user will be calling your website every second you're in for trouble. It's not really a scaleable solution and I assume you want this site to be big and famous with thousands of users.

My suggestion is as follows:

The topic table should store information on until what time a given topic is the current topic, named expire_time or something.

When a user requests a page, send the page together with the number of seconds that are left until the page expires and a new one has to be loaded. Since the time can't be shorter (just longer) there's no idea to make a check every second. You did mention that you wanted remaining time to be "realtime" but I suggest you "fake" that by only making a request every 5-10 seconds or something. You can still have a countdown timer. And since the Like adds 5 seconds there is no point in having greater accuracy than that in your "realtime".

When someone presses the like button you just add 5 seconds to the expire_time column.

When a user's page times out (or every 5-10 seconds) a request is made to the server with the current viewed topic id as argument. If someone has pressed Like then an Extended timeout response can be given with the extended amount of seconds that the topic is to be shown. You don't need to pass the topic text again because the user already has it. If no one has pressed like, and the topic has expired, then the next topic is returned and shown and the user page starts a new 30 sec countdown.

If no user is online then there is no need to be rotating topics so I don't see the need of a cron job. Instead, when the first user comes and there is no active topic, the server just selects a new topic as active and sets a 30 sec expiration on it.

I would also suggest:

Set a maximum allowed amount of likes for a certain topic. If 1000 people likes a topic it'll be shown for almost 1,5 hours and for those that want to see the next topic that might be boring. Maybe you can have steps in time added, so the first likes add 5 sec but when you have 10 likes it only add 4 sec, after 50 likes only 3 sec, after 200 likes only 2 sec and so on.

You probably also need some kind of IP or cookie registration per topic id so not one user makes a script that spams you with likes and then one topic is shown forever.

Realtime is nice but also very heavy for the server when you have many users. My suggestion is at least to have a parameter in your code where you can switch in-between refreshing every 5-10 seconds and to wait all the way down to expiry time before making a new refresh. This way you have a turbo injection for your program in case you get too many users and temporarily need extra speed on the server.

Good luck with your project!

Title: Displaying items with a countdown timer


问:
Take dynamic PHP array, package up as separate files inside a single zip file
I have this dynamic data in PHP, but for each array record, I want to allow a downloadable option to save a zip archive of all records (each as a separate file).

These are actually DNS zone records that are housed in a database. It wouldn't make much sense if they're in a single text file, so I'm looking to somehow have the data exported as separate files inside the zip.

I could save them to disk, but know there's a way in PHP to use the headers to force a download of data as a certain filetype. What I don't know is how to zip that data on the fly. Maybe I can't, and will have to save each to a temp folder, then zip the contents of that folder, then delete the contents after downloaded.

Thought maybe the community might have an idea of where to start. Thanks!

答:
You'll want to look at using the Zip module that's available in PHP (more specifically, the ZipArchive class). You'll need to use this class to generate the Zip file itself.

After generating the zip file, you would be able to use a combination of readfile(), and the sending of a Content-Type header of application/zip.

It should be a simple case of unlink()'ing the file after the readfile() function call to be able to remove the generated zip file. I'm not sure whether this would interrupt the transfer to the client though.


问:
Creating a web application to concatenate strings to unify naming conventions
I've been tasked with creating a small web application, where users have a series of forms (I can't currently think of a better way of doing this) which will contain, either a)text input fields or b)drop down lists. The form will look as follows:

form1
input box input box drop down box

form2
input box input box drop down box input box

form3
input box dropdown box input box drop down box

Now for each form, I would need to concatenate the resulting string stored within each box (drop down or input). Seems fairly simple and I already have a working prototype. Problem is for each form there can be multiple rows.

What would be the easiest way to approach this? My skills are limited to HTML, CSS, JavaScript and some (light) JQuery. The working prototype I currently have the user fills out all the fields and then presses a "generate" button which generates all the conctenated strings. Is this approach that you would take?

答: Using JavaScript can perfectly do your job. </p>


答:
A key aspect of Scrum is self organizing.

I suggest that you discuss the question with the team, and let them solve it.

Your concerns are all well-founded, but remember that as the Scrum Master, your job is to coach and facilitate. So ask them how they will solve these issues. They will own the solutions, and make it work.

I would add: in general, cross-functional teams are the way to go.


问:
How to architect a P2P application
[Moved here at the suggestion of SO users (10k SO+)]

I'd like to develop a peer-to-peer application. While I have a lot of experience in LOB apps, I'm new to the P2P arena.

I've got a rough idea of how things should work but need some more details to fill out my understanding.

What I know (believe) I need to do:

A significant proportion of clients need to enable inbound connections (ala uPnP/NAT rules)

Nodes should share other known nodes to provide resiliency if any particular node goes down

Some form of synchronisation/route finding is required for sending data between arbitrary clients

Possibly some resource sniffing to differentiate between "dumb" clients and and more powerful "super nodes" to handle sync/sharing of node lists and maybe relay messages

Clients without inbound support should hold open an outbound connection through which they can receive info of nodes to connect to

In short, I'm hoping to offer (at first) a chat/messenger service which doesn't rely on a connection to any particular central server. While I imagine I'll need to supply a number of centralised "supernodes" to get things started (or after significant upgrades), these should be optional once a functional P2P network has been established.

I can see a slew of problems and don't know how to address them. Mainly how to...

Authenticate users to other nodes without a central authority to verify

Co-ordinate which nodes know about which other nodes (min-max number/by latency/???)

Allow a given user to determine if another user (or node) is online

Deal with a situation where 2 groups of nodes are physically disconnected (airgapped) and how to resync on reconnection of the groups

Etc etc

I know this is a pretty open-ended question, so while high-level design patterns would be appreciated, what I'm really looking for is a decent guide to how others have handled these problems (and the ones I haven't considered yet).

答:
Designing a protocol and building an application on it is a huge project. Take as much as possible from existing protocols.

Most relevant protocols (beyond skype, which is a peer-to-peer messaging, but it's protocol is secret) are those that provide resources over peer-to-peer network which means especially the part of TOR providing the .onion domain and freenet.

Most things you list in "need to do" are handled by freenet and many of them in TOR too.

Identity of users has to be cryptographic. Associating real-word identities with the cryptographic keys requires some form of web-of-trust like in PGP/GPG.

Important reason for using peer-to-peer messaging is privacy. Off-the-record messaging is basically mandatory (specifies how the authentication must work).

Presence has to be off-the-record too, basically a special kind of message.

Disconnects are no special problem beyond general fault tolerance. For each side it looks like the other nodes failing and joining again.

You may want to use the protocol as signalling for some streaming protocol, probably sRTP. That protocol handles NAT traversal, so you can clone the mechanism for the p2p protocol itself.

Title: How to architect a P2P application


问:
Is it good idea to change complete enterprise level application into ajax web app
I am working on an application which is completely written using server side method in asp.net web forms with c# .

Now my team lead wants to convert whole application to ajax , in that approach we need to do the following tasks one by one .

1- Change all page methods into web methods so all will be static methods

2- Use jquery ajax to call all web methods .

3- bind html using json response.

Now i am  looking for some framework which can make my json html binding work bit easy but most of the frameworks like (knockout) are much compatible with MVC framework .

I have two main concern points:

Is it good idea to convert all  methods into web methods and use ajax ?

If i have to work with this approach what is the best framework i can use ?

答:
Is it good idea to convert all methods into web methods and use ajax ? If i have to work with this approach what is the best framework i can use ?

There is no silver bullet, however it would make sense to convert once that have validation check on business logic. Thus, you may warn/feedback user before post-backing the form by reducing the costly round-trips to the server.

There is a MVP design pattern for ASP.NET web forms. It may help to reduce concerns of tight binding that you get out of the box with ASP.NET forms.

ps: If you start a new project, then ASP.net MVC is a better choice. But if you just want to refactor an existing project as you just said, then MVP is the option because there is no easy way to convert those web-forms codes to MVC pattern.

References that might be helpful:

MVP Pattern Implementation in ASP.NET

Convert ASP.net website to AJAX enabled website?

Converting from ASP.net to Ajax enabled web site

Migrating ASP.NET Web Forms to the MVC Pattern with the ASP.NET Web API


问:
Is it better to have one method that takes a bool as a parameter or two methods with different names?
I'm writing a factory class for a Selenium Web Driver and I came across a choice that I couldn't figure out which is cleaner. Having two methods with the same parameter.

```<code>GetWebDriver(string browser)
GetRemoteWebDriver(string browser)
</code>```

or adding a boolean parameter

```<code>GetWebDriver(string browser, bool isRemote)
</code>```

I would think in this case the first option might be better because there is very likely no reason I need to extend it to more than a boolean of choices and hence two methods.  On the other hand, I can see the second one to better for when there is more than two choices or likely many extensions in the second parameter in the future.

Which would you choose for Cleaner Code and why?

答:
I tend to avoid such boolean flags in function interface, because they are a "separation of concern" related smell. So I would rather go for the 2 functions.

However depending on your architecture I would maybe try to have a IDriverStore service with  multiple implementations. A configuration object or whatever decides if the store provides a remote driver or not. Then you call myDriverStore.GetWebDriver(browser)


答:
A unit of code (subroutine, procedure, function, module, object, class, component, service, application, …) should do only one thing. How big that thing is, depends: obviously a class should have a bigger responsibility than a method.

A subroutine which takes a boolean flag almost always does two things: one thing when the flag is true, another thing when the flag is false. Therefore, it violates "do one thing" rule and should be split into two subroutines (or possibly three: a private one for the common behavior between the other two).

Title: Is it better to have one method that takes a bool as a parameter or two methods with different names?

Body:
I'm writing a factory class for a Selenium Web Driver and I came across a choice that I couldn't figure out which is cleaner. Having two methods with the same parameter.

```<code>GetWebDriver(string browser)
GetRemoteWebDriver(string browser)
</code>```

or adding a boolean parameter

```<code>GetWebDriver(string browser, bool isRemote)
</code>```

I would think in this case the first option might be better because there is very likely no reason I need to extend it to more than a boolean of choices and hence two methods.  On the other hand, I can see the second one to better for when there is more than two choices or likely many extensions in the second parameter in the future.

Which would you choose for Cleaner Code and why?


答:
Always err on the side of a readable client ...

```<code>GetWebDriver("IE", true)
</code>```

or

```<code>GetRemoteWebDriver("IE")
</code>```

It seems self-evident to me which one is easier to read.

Of course, another option would be

```<code>GetWebDriver("IE", DriverType.Remote)
</code>```

Which is equally readable, but I wouldn't go down that road unless there are likely to be more than two options.

Body:
Always err on the side of a readable client ...

GetWebDriver("IE", true)

or

GetRemoteWebDriver("IE")

It seems self-evident to me which one is easier to read.

Of course, another option would be

GetWebDriver("IE", DriverType.Remote)

Which is equally readable, but I wouldn't go down that road unless there are likely to be more than two options.


答:
Optimizing for readability, I'd choose the first option for the public interface: The intent of GetRemoteWebDriver("IE") is more obvious than GetWebDriver("IE", true).

Of course, from an implementation point of view, it might make sense for both GetWebDriver(string) and GetRemoteWebDriver(string) to call private GetWebDriverImpl(string, bool).


答:
I would prefer the first suggestion - to define two different methods.

A developers know immediately what that method is supposed to do (just by reading the name of it), he needs not to bother by the value of parameter of function to know what it will do.
As you wrote, when you will need more extensions, the bool type will be not sufficient and maybe you will change it to integer (or Enum to be more readable). But it has some disadvantages:

Your method would contain some awful switch or if/else construction to find out what should be the appropriate functionality

The orientation in code would be little bit more difficult, you would need to know, what does it mean 0 or 1 as a parameter, or in case of integer it would be even worse

If you define two methods, I recommend to implement the common parts of functionality in one separate (private) method and call it in both methods. Your methods
would contain just the unique commands and you would avoid duplicities.


答:
Both languages are very similar.

I came from a Java background but took a C# job. I got some time to learn the language and it felt like a Java copy.

Pick one and the second language will not be hard to learn.

Title: Java vs C#: What should a junior programmer (just finished degree) consider when entering the programming world?

Body:
I just want to hear some pro and con's - it's obvious that there is no right answer

So would Java be a better choice for a long term career? Or should I opt for .Net since it could be the platform having all the latest advances?

I just want to hear others point of view, so please don't close this question just because it's being subjective


答:
What do you mean by "good idea"?

Good idea for whom?

Does your company have pots of money to spend on the whim of an architect?

Using ajax techniques can provide a much better user experience, there is no doubt about that.

Are your end clients, whoever they are, complaining about the way the app works? Is this just going to be part of a rewrite that is planned anyway?

Title: Is it good idea to change complete enterprise level application into ajax web app

Body:
I am working on an application which is completely written using server side method in asp.net web forms with c# .

Now my team lead wants to convert whole application to ajax , in that approach we need to do the following tasks one by one .

1- Change all page methods into web methods so all will be static methods

2- Use jquery ajax to call all web methods .

3- bind html using json response.

Now i am  looking for some framework which can make my json html binding work bit easy but most of the frameworks like (knockout) are much compatible with MVC framework .

I have two main concern points:

Is it good idea to convert all  methods into web methods and use ajax ?

If i have to work with this approach what is the best framework i can use ?


答:
In my experience bools as parameters are rarely a good idea as they affect readability in the callers code, so go for two methods rather than one.

Alternatively, use an enum to make the intent more explicit.


答:
There are two parts to your question - "is this a good idea" and "should we do it like this"

To answer the first you need to answer the question of what problem are you trying to solve i.e. what issues with the current system do you (does your project lead or whoever requires the change) believe that this change will resolve? Unless you have a clear understanding of this then its a "because we can" project and that's not a good idea.

To answer the second, erm, no... that doesn't look like a sensible approach. You first need to isolate business logic and data access from the user interface (c.f. the MVP references provided by others) that done you can look at the architecture you want for your client side application and build the API you need to support that (which should sit upon substantially the same business and data access logic you've separated from your existing user interface).


问:
Professional Development: Finding that "pet project" to work on - then managing other commitments
At the moment, like a fair few of you I'd imagine, I spend at least 40 hours working on projects that use a specific set of technologies. Sometimes I'm doing maintenance, and have those technologies enforced, and sometimes I'm doing new builds.. but have the skillset of the team enforced due to maintenance reasons (quite rightly).

Yet I've got a growing set of technologies I want to test the waters with; be they custom Javascript frameworks or new Mobile Development techniques.

I have no idea how to get to grips with them though! Take for instance the Chromium Embedded Framework; I've always been a fan of the UI of the GitHub app, and also been impressed by both Evernote and Spotify. When I found out how those UIs were produced I naturally tried thinking of a project that I could toy about with and produce over a weekend. (Which, with minimal OS interaction (perhaps simple file manipulations), should be ample) But I can't think of a single thing to develop.

At work I've been tasked with retraining on Ruby on Rails; not wanting to go in blind - I decided to hammer a few books and try a few techniques. Now aside from the usual Lynda.com examples, and the very good "Agile Web Development with Rails" project from the book... I can't think of a single thing to develop.

Ordinarily I'd consider contributing to some FOSS software, and with frameworks I use regularly I do actually have a few ideas and I belong to the relevant mailing lists - i.e The Apache Cordova mailing list, and have signed the appropriate paperwork where required. (i.e For Apache Licensed projects)

Sometimes it's good to have an idea and run with it from scratch though, especially if you're new to the technology. So how do my fellow programmers manage doing that, and more importantly, how do you get ideas that allow you to use that specific framework/technology? It's hard to shoehorn ideas in to specific technologies at times.

I fear this is half the battle and the other half is how to keep such a project when work commitments can eat in to your personal time, and as it is, downtime is quite a valuable commodity!

答:
All of us are short on time, but somehow manage to find it for our pet projects. The reason? There is usually a problem that needs solving or a cool tool/feature we want that is not readily available elsewhere.

Without this spark of inspiration, you're going to find it that much harder to get going.

If I were in your shoes, I'd maybe find a piece of software you admire/like and try the following:

Write a clone (or subset of it)

Write a complementary/value-added feature


问:
I feel lost among the various web technologies and I'm looking for pointers
In the last 3-5 months I was thinking about which web programming language I should use. PHP? Ruby? C#? Python? Perl? And you know what? Every time I keep changing my mind like crazy! I learn some things about each one of them and then move to another.  Now I'm totally lost, totally lost guys. :)

What I want form you is to help me to decide, not to decide for me, and give me some recommendations. It would be more than helpful if you can tell me a short story about yourself, how you got started, where you are now and what web programming technologies you are using.

答:
Looking for pointers? Maybe you'd like to implement your website in C++ with CPPCMS or Wt? :D

...

Sorry, couldn't resist. It's not really true as most C++ programmers avoid pointers...

That said, for your information, if you try to setup a resource-critical website, CPPCMS might be an excellent choice.


问:
Semantic Versioning and splitting apart a library, providing a bundled build
I've got a nice, fairly popular JavaScript library that is following Semantic Versioning.

The current library has a few dependency libraries, which are available either as separate downloads or as part of a single bundled download. I see a need to head down this path further. I want to extract additional, smaller libraries out of the one larger library. Each of these extracted libraries would be available as separate files, or inside of the one bundled build, again.

If I go down this path of extracting the libraries, and providing a bundled version of the final code, does this require a full version change in semantic versioning? Would I have to bump from 1.x to 2.x?

My first thought it no: I will not change any public API, so I don't have to change the major version number. But then I wonder... well, I am restructuring a lot of things, even though the final API for the bundled version would be the same.

Is there a clear answer from semver on something like this? Do I need to bump first, second or third dot? Or something else?

答:
You should increment the patch (third) number.

While you are not fixing a bug, you are refactoring, that is modifying the structure of the problem without changing its behavior. You are also not changing the public API.

Relevant quotes from Semantic Versioning below

What should I do if I update my own dependencies without changing the
  public API?

That would be considered compatible since it does not affect the
  public API. Software that explicitly depends on the same dependencies
  as your package should have their own dependency specifications and
  the author will notice any conflicts. Determining whether the change
  is a patch level or minor level modification depends on whether you
  updated your dependencies in order to fix a bug or introduce new
  functionality. I would usually expect additional code for the latter
  instance, in which case it's obviously a minor level increment.

-

Patch version Z (x.y.Z | x > 0) MUST be incremented if only backwards
  compatible bug fixes are introduced. A bug fix is defined as an
  internal change that fixes incorrect behavior.


问:
Clean, Modular Code vs MV* Frameworks
I've been hearing a lot about the "new" MV* frameworks. I've tinkered with KnockoutJS (I created an invoicing application), but I much prefer to write clean, modular code in raw JavaScript - leveraging utility APIs and other libraries when necessary.

Given a methodical/structured/SOLID approach to writing a JavaScript application, where OOP, SOC, SRP and other design principles are adhered to, wouldn't the usage of MV* frameworks be superfluous?

Are there any articles that express/address these concerns?

I've found one in the past

note - I've migrated this question from SO to this site, as its more appropriate for this audience.

答:
I see following reasons for the use of such frameworks:

Not everyone structures code by default. Frameworks enforce structure to some extent. This extent is variable based on how much scaffolding they provide. Too much scaffolding usually translates to steeper learning curve.

Frameworks generally encapsulate design/programming patterns from the collective experience of a larger community, so in some cases there will already be structure in there, while you will build and refactor.

Mostly developers will come and go, using popular/standard frameworks makes it easier for developers other than the original developer to understand the structure. Good frameworks are well documented and have active communities to be able to ask for help.

As you move from project, you most likely copy/paste code from previous project that you now require in a new project. In case you use a framework even if its your own MV* framework, you just import and include this framework each time.

Title: Clean, Modular Code vs MV* Frameworks


问:
What constitutes "improper use" of the javascript Eval feature?
Eval is a notoriously controversial language feature.  Douglas Crockford flat out rejects it. I'm wondering what specific risks Eval brings about.  According to this question, Improper use of eval opens up your code for injection attacks.

What are some improper uses of the Eval command, and what security holes do they open up?

答:
I have a situation where eval looks like the way to go, evaluating a string and returning an existing variable whose name is the same as the string.

I have several tables: table1ResultsTable, table2ResultsTable, tableNResultsTable.  I have variables set up with the same names, that are jQuery datatable objects.  I use these to set up the tables and call jQuery datatable functions on them.

Each table has class="resultsTable" assigned.  Now, I need to get the variable when the table is clicked.  I'm doing this:

```<code>$('resultsTable).on('click', 'td', function(event) {
    var cResultsTable = eval(event.target.parentElement.parentElement.parentElement.id);
    [etc]
});
</code>```

So I'm getting the id of the table in which the cell was clicked, which has the same name as the datatable object variable corresponding to it. If someone has a suggestion for an improvement I'd like to know about it.

Body:
I have a situation where eval looks like the way to go, evaluating a string and returning an existing variable whose name is the same as the string.

I have several tables: table1ResultsTable, table2ResultsTable, tableNResultsTable.  I have variables set up with the same names, that are jQuery datatable objects.  I use these to set up the tables and call jQuery datatable functions on them.

Each table has class="resultsTable" assigned.  Now, I need to get the variable when the table is clicked.  I'm doing this:

$('resultsTable).on('click', 'td', function(event) {
    var cResultsTable = eval(event.target.parentElement.parentElement.parentElement.id);
    [etc]
});

So I'm getting the id of the table in which the cell was clicked, which has the same name as the datatable object variable corresponding to it. If someone has a suggestion for an improvement I'd like to know about it.


问:
Utilizing a Java Concurrent Utility from a Web App
I have the following lines of code in my application:

```<code>return "Service is alive since: " + TimeUnit.MILLISECONDS.toMinutes(mxBean.getUptime()) + " minutes";
</code>```

It uses the following package:

```<code>import java.util.concurrent.TimeUnit;
</code>```

My application is a web application. Does it means that I have something wrong logically if I use something from concurrent package at a web application?

答:
I can't see any problem with this, and in fact using TimeUnit.MILLISECONDS.toMinutes(...) was the solution to the SO question that I found when googling "java convert milliseconds to minutes".

If you were in a constrained environment where you were worried about the size of your deployment, you might need to try cutting out any packages or jars that you don't need, but since java.util.concurrent is part of core Java, removing it would take some work.


问:
How should I architect a RESTful webservice to use 3rd party (i.e. Google, Facebook, Twitter) for authentication?
For my job we have a nice RESTful webservice we've built out that we use to drive a couple websites we have.  Basically the webservice lets you create and work with support tickets, and the website is responsible for the front end.  Any webservice requests use an auth header which we use to validate the user and their password for each call.

This year we're looking to expand our login options so that users on the website can log in via Google, Twitter, and Facebook (possibly others).  However I'm having a lot of trouble figure out how to architect this so the webservice can use the 3rd party authentication providers to ensure the users is who they say they are.  Is there any best practices out there for how to do this?

Currently we're thinking of having the website handle authenticating the users themselves, and then use a new setSessionId call that registers their current session with the webservice back end.  Each additional request to the webservice will pass along that sessionId and will validate it.  These seems okay, but I have that feeling in the back of my head that I'm not thinking this through and all my forum browsing and reading oauth and openid specs is just confusing me more.  Any tips for how to tackle this?

答:
My two cents: I've never done anything like this before nor do I know how the FB, Twitter or Google login mechanisms work, but a few issues popped up in my head as soon as I read your question:

Multiple logins: What happens if I log in with my Facebook account one day and my Google account the next? Or simultaneously? Do you treat these two accounts as unique, separate accounts or should you have some method to associate the two and allow me to access my tickets either way?

Relying on external identifiers: What happens when Facebook or Twitter decide to change the way their account identifiers look? To illustrate, if BazLogin represents a unique account using the code {4382-af56} but decides that from now on accounts will have 12 digits because 8 were not enough, how do you tell {1234-4382-af56} from {0000-4382-af56}?

We can deal with these two issues by choosing to associate the external accounts with an internal account, not just a session ID. Then, an external login could be just a gateway to the creation of an internal account. Should I authenticate with more than one login method, you could tell me I'm already logged in. If an external auth provider changes something we rely on, we could ask the user to provide the name and password of their internal account and create a new association for future logins.

I'm not sure I addressed the issues you had in mind, but you didn't really mention any concrete concerns. I hope my answer was helpful, either way.


问:
How does a program process concurrent executions
I have a console application that is run by taking the input parameters. It can be configured by passing report name parameter, for e.g. reportgen.exe sales.xml. It hits the database and retrieves the data into xml file and sends to sftp site.

Whole of this process is logged and the xml files are being saved in a directory. This exe needs to be scheduled on daily basis, so what happens if the exe is scheduled at same time with different parameters.

Is it thread safe and can be considered as independent execution doing what it supposed to do?

Edit:
It uses log4net for logging the debug info. into a file. So, how does it write log statements into single file for concurrent executions.

答:
Each instance of your application will be loaded into its own application domain.

They will be separated from each other.

You can find more information about Application Domains on MSDN - Application Domains (C# Programming Guide)

If you're dealing with a lockable resource which obtains a lock when operation starts, and releases a lock when operation ends (either on OS or application level), then you must have a single point of access.

You can't use singleton pattern here because each instance of your application will be loaded into its own application domain, and singleton has AppDomain scope. ok?

So, if you can't use singleton, what can you do?

Well, you can create another application to communicate with lockable resource and all communication between your application (multiple instances of your application) and lockable resource must go through that application.


答:
Your code will be thread safe as it's running within different process instances.

What's you'll need to be careful with is there you log to. Make sure the various instances don't try to create log files with the same name, otherwise you'll have problems.

Title: How does a program process concurrent executions

Body:
I have a console application that is run by taking the input parameters. It can be configured by passing report name parameter, for e.g. reportgen.exe sales.xml. It hits the database and retrieves the data into xml file and sends to sftp site.

Whole of this process is logged and the xml files are being saved in a directory. This exe needs to be scheduled on daily basis, so what happens if the exe is scheduled at same time with different parameters.

Is it thread safe and can be considered as independent execution doing what it supposed to do?

Edit:
It uses log4net for logging the debug info. into a file. So, how does it write log statements into single file for concurrent executions.