问:
Finding possible positions for rectangle in a 2-d array
Well, the title is not very appropriate, please read on (I couldn't get a better one).

Note: Using Python 2.7, but an algorithm will help too.

I'm making a side scroller game, in which I am generating the obstacles on the fly. The trouble I am having is figuring out how to generate the obstacles. o_O

I have a some kind of a logic, but then I'm having trouble in figuring out the entire logic.

So here's my problem from an implementation perspective :

I have a Surface, in which I have put some Elements, which are all rectangles.

Think of it like:

```<code>0 0 0 0 0 0 0
0 0 0 0 1 1 0
0 0 0 0 1 1 0
0 0 0 0 1 1 0
0 0 0 0 0 0 0
0 1 1 0 0 1 1
0 0 0 0 0 1 1
</code>```

As in the above structure, how can I determine if a axb rectangle can be added without overlapping another rectangle (of 1s), and where all. Also, that with maintaining a distance of x elements (even diagonally) from all the other objects, that means the entire rectangle is  (x+3, x+4). Something like if x=1, a=3, b=4, there's only one possible arrangement:

(2s represent the new object)

```<code>2 2 2 0 0 0 0
2 2 2 0 1 1 0
2 2 2 0 1 1 0
2 2 2 0 1 1 0
0 0 0 0 0 0 0
0 1 1 0 0 1 1
0 0 0 0 0 1 1
</code>```

Basically, I need to find all the points, from which an rectangle of sides a and b can have it's, say, top-left corner. How this be achieved?

Note: Open to better ideas for generating the obstacles on the fly!

PS: Am I unclear?

答:
The following should work fairly well:

```<code>def find_valid_locations(grid, z, a, b):
    seen = set()
    check = [(0, 0, 0, 0)]
    w = z + b
    h = z + a
    while check:
        x, y, ox, oy = check.pop()
        if (x, y) in seen:
            continue
        seen.add((x, y))
        if x + w >= len(grid) or y + h >= len(grid[0]):
            continue
        for i, row in enumerate(grid[x+ox:x+w+1], x+ox):
            for j, val in enumerate(row[y+oy:y+h+1], y+oy):
                if val:
                    break
            else:
                continue
            check.extend([(i+1, y, 0, 0), (x, j+1, 0, 0)])
            break
        else:
            yield (x, y)
            check.extend([(x+1, y, w-1, 0), (x, y+1, 0, h-1)])
            continue
</code>```

The brute force method here would be to check all positions in every potential rectangle location and only return locations where the rectange didn't encounter a non-zero position.  This is essentially what we do here, with the following optimizations:

If we have found a valid location (x, y), we can check locations (x+1, y) and (x, y+1) easily, by only checking the new positions added to the rectangle by shifting it down or to the right.

If we encounter an obstacle at position (i, j) while checking location (x, y), we can skip checking any other location that includes (i, j) by starting our next checks at (i+1, y) and (x, j+1).

Note that I renamed the parameter x to z so that I could use x as a row index in the code.

Body:
The following should work fairly well:

def find_valid_locations(grid, z, a, b):
    seen = set()
    check = [(0, 0, 0, 0)]
    w = z + b
    h = z + a
    while check:
        x, y, ox, oy = check.pop()
        if (x, y) in seen:
            continue
        seen.add((x, y))
        if x + w >= len(grid) or y + h >= len(grid[0]):
            continue
        for i, row in enumerate(grid[x+ox:x+w+1], x+ox):
            for j, val in enumerate(row[y+oy:y+h+1], y+oy):
                if val:
                    break
            else:
                continue
            check.extend([(i+1, y, 0, 0), (x, j+1, 0, 0)])
            break
        else:
            yield (x, y)
            check.extend([(x+1, y, w-1, 0), (x, y+1, 0, h-1)])
            continue

The brute force method here would be to check all positions in every potential rectangle location and only return locations where the rectange didn't encounter a non-zero position.  This is essentially what we do here, with the following optimizations:

If we have found a valid location (x, y), we can check locations (x+1, y) and (x, y+1) easily, by only checking the new positions added to the rectangle by shifting it down or to the right.

If we encounter an obstacle at position (i, j) while checking location (x, y), we can skip checking any other location that includes (i, j) by starting our next checks at (i+1, y) and (x, j+1).

Note that I renamed the parameter x to z so that I could use x as a row index in the code.


问:
Are closures sufficient to characterize functional programming?
All functional programming languages that I know of (e.g. Haskell, Scala, Scheme, Clojure, SML, OCaml, ...) support a notion of closures.

Also, I often read that a language X can be considered functional because it supports closures.

On the other hand there are programming languages that do have closures but are not considered functional. The best example I know of is Smalltalk: Smalltalk has blocks (which in most implementations behave like closures, see e.g. here) but it is considered a pure object-oriented language.

So, while closures are a very common (or even essential) construct in functional programming, are they really sufficient to characterize functional programming? If the support of closures is sufficient for considering a language functional, why isn't Smalltalk considered functional too?

Or is the notion of a closure orthogonal to both functional and
object-oriented programming?

答:
To characterize functional language, one key feature is currying:

hom(AxB, C) == hom(A,C^B)

This is one of the most important features available in functional languages, because it allows adding a parameter to a function. Languages that only support left side of the ==, can't be considered functional.

ContentLicense: CC BY-SA 3.0


问:
Preventing password hashing algorithm from overloading CPU
These days password hashing algorithms are designed to be slow. While it prevents black hats from guessing the password (at least partially), it also gives additional work for the server.

I can imagine that, if someone wanted to make server run extremely slowly, they could simply send many log-in requests which would lead to many password hash calculations therefore dramatically increasing CPU usage.

What is the common practice for preventing such attacks? And how is it called?

答:
Your best method for protecting against this kind of attack is to implement some kind of limit to the number of times logins can be attempted before the account is locked.

You can do this in a number of ways, that have their advantages and disadvantages. Like everything in security, you have to balance the pros and cons.

Some possible solutions:

Option A: Limit number of login attempts from an IP address. However, IP addresses are easily spoofed and some bruteforcing tools may switch IPs between each attempt.

Option B: Lock the account after X number of failed login attempts. However, you would then need a way for a legitimate user to unlock their account.

Option C: Use a captcha. This might be annoying for legitimate users, but it will make it difficult for an automated system to attempt many logins.

OWASP have more details on preventing this kind of attack.


答:
Performing the hash isn't necessarily slow.  It's reversing the hash that's painfully slow. (This is only needed if you have the hash and need to calculate the original password - something that your log in functions don't need to do).

The attack would be called a Denial of Service (DoS).  The common defense is to start ignoring the IP address of the attacker after too many failed log in attempts.

Title: Preventing password hashing algorithm from overloading CPU

Body:
These days password hashing algorithms are designed to be slow. While it prevents black hats from guessing the password (at least partially), it also gives additional work for the server.

I can imagine that, if someone wanted to make server run extremely slowly, they could simply send many log-in requests which would lead to many password hash calculations therefore dramatically increasing CPU usage.

What is the common practice for preventing such attacks? And how is it called?


问:
What is so difficult about pointers/recursion?
In the perils of java schools Joel discusses his experience at Penn and the difficulty of "segmentation faults".  He says

[segfaults are difficult until you]
  "take a deep breath and really try to
  force your mind to work at two
  different levels of abstraction
  simultaneously."

Given a list of common causes for segfaults, I don't understand how we have to work at 2 levels of abstraction.

For some reason, Joel considers these concepts core to a programmers ability to abstract.  I don't want to assume too much.  So, what is so difficult about pointers/recursion?  Examples would be nice.

答:
```<code>  DATA    |     CODE
          |
 pointer  |   recursion    SELF REFERENTIAL
----------+---------------------------------
 objects  |   macro        SELF MODIFYING
          |
          |
</code>```

The concept of self referential data and code underlies the definition of pointers and recursion respectively. Unfortunately, widespread exposure to imperative programming languages has led computer science students to believe that they must understand the implementation via the operational behaviour of their runtimes when they ought to trust this mystery to the functional aspect of the language. Summing all the numbers up to a hundred seems a simple matter of starting with one and adding it to the next in the sequence and doing it backwards with the aid of circular self referential functions seems perverse and even dangerous to many not used to the safety of pure functions. Past experience with imperative languages has burnt them and the desire to comprehend all the intervening states is driven by a suspicion that variables are subject to change even if they grasp that the same name doesn't refer to the same thing when a function is invoked again from within itself.

The concept of self modifying data and code underlies the definition of objects (i.e. smart data) and macros respectively. I mention these as they are even harder to understand especially when an operational understanding of the runtime is expected from a combination of all four concepts - e.g. a macro generating a set of objects that implements a recursive decent parser with the aid of a tree of pointers. Rather than trace the entire operation of the program's state step-by-step through every layer of abstraction at once, imperative programmers need to learn to trust that their variables are only assigned once within pure functions and that repeated invocations of the same pure function with the same arguments always yields the same result (i.e. referential transparency), even in a language that supports impure functions as well, like Java. Running around in circles after the runtime is a fruitless endeavour. Abstraction should simplify.

Body:
  DATA    |     CODE
          |
 pointer  |   recursion    SELF REFERENTIAL
----------+---------------------------------
 objects  |   macro        SELF MODIFYING
          |
          |

The concept of self referential data and code underlies the definition of pointers and recursion respectively. Unfortunately, widespread exposure to imperative programming languages has led computer science students to believe that they must understand the implementation via the operational behaviour of their runtimes when they ought to trust this mystery to the functional aspect of the language. Summing all the numbers up to a hundred seems a simple matter of starting with one and adding it to the next in the sequence and doing it backwards with the aid of circular self referential functions seems perverse and even dangerous to many not used to the safety of pure functions. Past experience with imperative languages has burnt them and the desire to comprehend all the intervening states is driven by a suspicion that variables are subject to change even if they grasp that the same name doesn't refer to the same thing when a function is invoked again from within itself.

The concept of self modifying data and code underlies the definition of objects (i.e. smart data) and macros respectively. I mention these as they are even harder to understand especially when an operational understanding of the runtime is expected from a combination of all four concepts - e.g. a macro generating a set of objects that implements a recursive decent parser with the aid of a tree of pointers. Rather than trace the entire operation of the program's state step-by-step through every layer of abstraction at once, imperative programmers need to learn to trust that their variables are only assigned once within pure functions and that repeated invocations of the same pure function with the same arguments always yields the same result (i.e. referential transparency), even in a language that supports impure functions as well, like Java. Running around in circles after the runtime is a fruitless endeavour. Abstraction should simplify.


答: Track IP addresses and check login attempts per minute not allowing further attempts for awhile beyond some threshold.</p>


问:
Methods as verbs: is the object the subject?
Is there some recommended practice regarding methods as verbs in OOP? Should the object work syntactically as subject or as object/complement?

Should   object.doSomething() be normally understood as "the object iself does something" (subject) or "the caller does something with the object"?

I suspect that the first alternative is more right, and it sounds more natural with such a general verb... But consider for example "OutputStream.write(byte[])", which...

writes b.length bytes from the specified byte array to this output
  stream.

Here it's not the object who is the subject of the action, it's the caller. The Writer (rather confusingly) does not really "write", its the caller is who "writes" bytes to the Writer. Should this be considered incorrect?

答:
No, sometimes.

The object can also be the actor, in which the verb may accept a subject as a parameter. These types of objects are typically called 'service' or service class.

Take for instance a money transfer from one bank account to another. Which of the bank accounts is the subject? Perhaps they are both subject? Would one account transfer it's credit to the other account or is it better to have a third object that manages the money transfer?

Objects as subject are nice as long as the verb is operating mostly on it's own data/state.


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

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

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

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

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

答: Communication and teamwork skills are important, but I would add that being able to understand the customer's requirements and acquire business domain knowledge have also been very important in my experience. </p>

ContentLicense: CC BY-SA 2.5


答:
It is called a 'resource starvation' attack, that applies to any situation where the work on the part of the attacked party is greater than the work required by the attacker.

Hashing is actually very 'cheap' compared to the time to execute the database lookup to confirm it.

There are a few mitigations you can apply.  In the event of an attack you first off want to increase the 'cost' to the would-be attacker. CAPTCHA's can help with this, but they have a negative impact on accessibility generally.  So the best option is to have them 'turned off' and then enable them based on a server side trigger.

You can limit requests but it's easy to get around.  You can globally delay and limit the processing of each login request so each IP can only issue one login request at a time and has to wait before the server even begins to process it.  If they issue another request before the request is completed then you can dump it cheaply.  But this has the downside that it is more complex to implement and that it could affect clients behind a NAT router.  Everyone else would be happy to wait a couple of seconds with a 'please wait' type dialogue - again it ramps up the cost for the attacker as they require more sources to attack from.

If your system can make you aware of an attack in real time then you can work with your hosts to block or limit problem traffic (for example from regions you don't supply or serve) so the attackers essentially saturate their own routes into your servers or direct the attackers into various sinkholes and honeypots.

Ultimately it comes down to being just like any other distributed denial of service attack in that it's a case of managing load.  Ultimately if it's a cloud based system you might want to just spin up more capacity (although you need to be aware of the costs involved).

Ultimately the answer is to go read up on security to understand the whole landscape.  Have a look at Security.SE:If you could only have one book on web security, what would it be? for suggestions.


问:
SQL Server: When to use SSIS vs T-SQL for ETL Tasks
When should I use one versus the other? For instance, let's say I have 20 comma delimited, denormalized text files and I want to transform the data and load it into normalized SQL Server tables.</p>

答:
What are the size of the files?  T/SQL is pretty fast, I've done million record imports in less than three seconds.  Is this 'once' or 'scheduled'?

SSIS is for complicated scenarios.  SSIS can schedule FTPs, external commands (such as ZIP and unZIP), branching paths on step failures, etc.  Therefore it isn't as simple as importing data, it's the entire 'overnight data exchange process'.

Given that you have CSVs (presumably with column names) much of your work would appear to have been done already.  If you don't need to filter out bad records you're just doing 'group bys' to generate validation fact tables.

More information is needed.


问:
How do I deal with a difficult programmmer joining an open source project?
I have an open source script for a specific site (I'm trying not to call anything by name here) that I and a few other developers recently moved to GitHub. We've gotten several new developers since we moved to the new system, including one very active one in particular. However, this active one has started changing a lot of the project.

First of all, he deleted our versioning system (not like Git, but like that -- we called it versions v4.1.16) and said it would be better to simply push the code to the site when we think it's ready. Now there's no centralized place to put release notes, which has gotten annoying.

The thing that has gotten me just about ready to pack my bags and go was the push script. Another developer on the project wrote a simple Python-based push script. Since we keep multiple versions of the script online in various places, I began coding a larger Java program with a graphical interface that will replace the Python script. I went on IRC to notify everyone about it, and I got a very annoying response from the programmer saying that the old Python-based script can do everything mine can do and is so much more lightweight (he also commented about the fact that he thought Python was better than Java and so on). I looked over the code for the old push script and saw that none of the features he said existed were there.

So now I want to know what to do. I've spent a lot of my time on this project, so I don't want to just get up and leave, but I'm finding it hard to work with this new developer.  On the flip side, he is now the #1 committer on the project, with even more commits than the lead developer. I'm not really sure what to do about this. Has anybody else experienced this problem? If so, what did you do?

UPDATE 1: I have disabled everyone's commit access and I am requesting people go through pull requests. I also proposed several measures to fix the other issues. Everyone else hasn't shown any support for it. The troublesome dev has simply said that people who don't follow the "commit action" closely can think that the project is disorganized when it really isn't. I obviously don't agree with this, so I am seriously contemplating resigning from the project.

UPDATE 2: The lead developer began ranting about the fact that one of my commits supposedly deleted three newlines in the code (the revert commit showed up just after I posted the discussion, and doesn't even reference my "commit"), and then the two of them began discussing whether to revoke my commit access. So, I have done the logical thing and left the project. Thanks for your help with this everyone!

答:
I have seen the problem before. But after few years you really get tired of the project, so my solution was to leave the project. It might not work for you, but the problem is fundamentally that different people think in different ways. Features that you think are very important are not at all important to other people.

Good plan would be to divide tasks of different people. If you can agree with the persons which part of the project is in each person's responsibility, then only one person is doing decisions on certain part of the project. But teamwork is always difficult. You're still never going to like the decisions made by other programmers. Best solution is to never look at what other programmers decided. Just trust them to do the right thing is enough.

Common goal will focus your efforts to the important stuff. Everyone working to the same direction is difficult to get, and conflicts will anyway happen. Deciding common goal requires knowing what's the current status of the project, and then deciding where it needs to be after some time.

Here's example of stuff to avoid: For example, large number of c++ programmers think that code that is not using STL library is broken. Other programmers think that every dependency to external libraries is broken, including STL. Such conflicts just cannot be solved properly - both situations cannot be fullfilled simultaniously - and the most problematic people will push their opinion no matter what opposition they will encounter.


问:
Data Synchronization in mobile apps - multiple devices, multiple users
I'm looking into building my first mobile app. One of the core features of the application is that multiple devices/users will have access to the same data -- and all of them will have CRUD rights.

I believe the architecture should involve a central server where all the data is stored. The devices will use an API to interact with the server to perform its data operations (e.g. adding a record, editing a record, deleting a record).

I imagine a scenario where synchronizing the data will become a problem. Assume the application should work when it is not connected to the Internet, and thus cannot communicate with this central server. So:

User A is offline and edits record #100

User B is offline and edits record #100

User C is offline and deletes record #100

User C goes online (presumably, record #100 should get deleted on the server)

User A and B goes online, but the records they edited no longer exist

All sorts of scenarios similar to the above can come up.

How is this generally handled? I plan to use MySQL, but am wondering if it's not appropriate for such a problem.

答:
I'm currently working on a mobile/desktop/distributed app with exactly the same requirements and issues.

First of all, these requirements are not inherent to mobile apps per se, but to any disconnected/distributed client-server transactions (parallel programming, multithreading, you get the point). As such they are, of course, typical issues to address in mobile apps.

Generally, what this all boils down to is that you have a potential data record that is distributed to n clients, who may edit it at the same time. What you need is

a proper version control/locking mechanism,

a proper rights/access management,

a proper synchronization/caching strategy

For (1) you may apply some patterns: There are two frequently used locking strategies: Optimistic Offline Locking, and  Pessimistic Offline Locking. Some of these come applied in different version control "patterns", such as MultiVersion Concurrency Control (MVCC), which uses a counter (sort of a very simple "time stamp") for every data record, which is updated whenever the record is changed.

(2) and (3) are very broad issues themselves, which need to be dealt with independently of (1). Some advice from my experience:

Use a client-server technology that abstracts away most of the issues for you. I highly recommend some web technology such as CouchDb, which handles (1) via Optimistic Offline Locking + MVCC, (2) via Web API, and (3) via Http caching very well.

Try not to invent things yourself if you can rely on proven technologies and approaches. I believe any hour spent researching and comparing existing technologies/patterns is far better spent than trying to implement your own system(s).

Try to use homogeneous technologies if possible. By "homogeneous" I mean technologies that have been built with the same principles in mind, e.g. web 2.0 usage scenarios. An example: Using a proper CouchDb and REST Client (Web API) with a local caching strategy is a better choice than using SQL for mobile apps.

I strongly advise against the use of MySQL because it is a technology that was not explicitly made for such usage scenarios. It works, but you are much better off with a database system that already embraces the web communication and concurrency style (such as many NoSQL Databases).

By the way, I have settled for CouchDb with a custom local client working against the CouchDb APIs, which works and scales beautifully. I switched from using MSQL + (N)Hibernate and paid a high price for not making the right choice (meaning not doing enough research) in the first place.

ContentLicense: CC BY-SA 4.0


答:
First, you mentioned both an API and a database (MySQL). I very much recommend that you use an API and don't try to communicate directly between the databases. That latter route will not scale well at all.

One good starting point you should consider is using Apache CouchDB. It is schema-less, based on HTTP and JSON, and has a very good replication mechanism. We use it to solve a similar problem.

CouchDB's replication mechanism uses the same HTTP API that any other client uses. So in essence, it provides replication over an API.

For iOS, I recommend using the Couchbase Lite project. It works very well for synching data. For Android, the same company that makes the aforementioned Couchbase Lite project is working on a similar offering - Couchbase Lite for Android. It is not as complete as the iOS version and has some work left to accomplish.

There are a few things to consider with CouchDB however.

You will need to provide your own conflict resolution. Fortunately, if conflicts occur, CouchDB keeps the conflicted versions and picks and arbitrary, but deterministic conflict to have as the main version. So you could consider delaying conflict resolution for your initial version.

The replication mechanism is made for replicating databases, not synchronization per-se. So if you have a lot of deleted documents, your replication from the server to the client will take longer and longer. There is a way to avoid this using "database rotation." This essentially removes old deletes.

You can't control the replication order. You can, however, make some clever solutions to improve the replication performance such as using filtered replication to get some documents first, or even access the server directly on-demand.

The replication will not happen in the background on iOS. You can utilize the iOS SDK to provide some cases of background replication.

Finally, if you don't want to use CouchDB, you can at least use it as a good reference for how you could make a synchronizing algorithm using an HTTP API. My suggestion would be to start with CouchDB and then, if you need something more custom, to consider rolling your own.

Title: Data Synchronization in mobile apps - multiple devices, multiple users

Body:
I'm looking into building my first mobile app. One of the core features of the application is that multiple devices/users will have access to the same data -- and all of them will have CRUD rights.

I believe the architecture should involve a central server where all the data is stored. The devices will use an API to interact with the server to perform its data operations (e.g. adding a record, editing a record, deleting a record).

I imagine a scenario where synchronizing the data will become a problem. Assume the application should work when it is not connected to the Internet, and thus cannot communicate with this central server. So:

User A is offline and edits record #100

User B is offline and edits record #100

User C is offline and deletes record #100

User C goes online (presumably, record #100 should get deleted on the server)

User A and B goes online, but the records they edited no longer exist

All sorts of scenarios similar to the above can come up.

How is this generally handled? I plan to use MySQL, but am wondering if it's not appropriate for such a problem.


答:
You can't just quit without taking an effort to express your concerns and difficulties. I know it can be difficult. If fact if you and your team members are young enough not to experience first hand many social problems happening in any development team, it can be really hard.

Having said that, I strongly believe you should express your concerns. You can write them down in the email and show it to your trusted friends who are not part of the team and have no or little interest in what you do. In this case you may get a good feedback so that wording of your email is not too harsh. Stick to the facts though. Don't accuse or blame. Just facts that it's hard to do something for you because 'blah' is missing. Why 'blah' is missing should be clear to every team member, i.e. "the new programmer" deleted or didn't accomplish something.

Again, sending this email is hard but that by itself is a great experience that may be very useful for you going forward. Great lesson to learn.

PS: I didn't mean to sound too parental. However it's indeed something I would say to anyone including my kids.

Title: How do I deal with a difficult programmmer joining an open source project?

Body:
I have an open source script for a specific site (I'm trying not to call anything by name here) that I and a few other developers recently moved to GitHub. We've gotten several new developers since we moved to the new system, including one very active one in particular. However, this active one has started changing a lot of the project.

First of all, he deleted our versioning system (not like Git, but like that -- we called it versions v4.1.16) and said it would be better to simply push the code to the site when we think it's ready. Now there's no centralized place to put release notes, which has gotten annoying.

The thing that has gotten me just about ready to pack my bags and go was the push script. Another developer on the project wrote a simple Python-based push script. Since we keep multiple versions of the script online in various places, I began coding a larger Java program with a graphical interface that will replace the Python script. I went on IRC to notify everyone about it, and I got a very annoying response from the programmer saying that the old Python-based script can do everything mine can do and is so much more lightweight (he also commented about the fact that he thought Python was better than Java and so on). I looked over the code for the old push script and saw that none of the features he said existed were there.

So now I want to know what to do. I've spent a lot of my time on this project, so I don't want to just get up and leave, but I'm finding it hard to work with this new developer.  On the flip side, he is now the #1 committer on the project, with even more commits than the lead developer. I'm not really sure what to do about this. Has anybody else experienced this problem? If so, what did you do?

UPDATE 1: I have disabled everyone's commit access and I am requesting people go through pull requests. I also proposed several measures to fix the other issues. Everyone else hasn't shown any support for it. The troublesome dev has simply said that people who don't follow the "commit action" closely can think that the project is disorganized when it really isn't. I obviously don't agree with this, so I am seriously contemplating resigning from the project.

UPDATE 2: The lead developer began ranting about the fact that one of my commits supposedly deleted three newlines in the code (the revert commit showed up just after I posted the discussion, and doesn't even reference my "commit"), and then the two of them began discussing whether to revoke my commit access. So, I have done the logical thing and left the project. Thanks for your help with this everyone!


答:
You can quit. Not the most constructive thing to do, but sometimes it's the only option. If you do, don't sit around and moan about how you had to give it up, take that energy and put it straight into something else - 'move on' in other words.

You can fork it. There's no reason why you have to work with anyone. Fork, improve the code and let the others continue to have a little ego-fest of their own. Your new project will simply compete with the old and its up to you whether you make a success of it, or the old one beats you in terms of users and features.

You can engage with the rest of the development team on the project to voice your concerns. Don't make it personal, but make out that you're unhappy with code churn, or lack of established quality processes, or unhappy that the new decisions are just pushed out without agreement from everyone. You'll either be told that nothing's wrong enough to change, or you'll get a few others agreeing with you that the team needs to fix things up. That might end up with the disruptive guy losing his commit access. Maybe you'll all agree that some of the changes are not improvements and the project needs to be reverted. (This latter option is the most likely outcome, unless it turns into a massive argument of entrenched opinions.)

It can be difficult when someone comes along and changes the safe and comfy routines you've become used to, but it could be said that having someone come along and shake up the old, cozy practices are good things in themselves.


问:
What makes JVM so much versatile to support so many JVM languages?
JVM supports so many languages other than Java like Groovy,Clojure,Scala etc which are functional languages unlike Java(I am referring to Java before Version 8 where Lambda's are not supported) that doesn't support functional capabilities.On a high level what makes the JVM so versatile that it can support both Object Oriented as well as Functional languages?

答:
The JVM is the first virtual machine I'm aware of which combined garbage collection, performance, and a workable sandbox model.  The emergence of many languages to support the JVM is probably not so much a result of its "versatility", but rather the fact that the Java language lacks some significant features that people want in a programming language.  For example, while most machine languages have only half a dozen or so data types (e.g. byte, halfword, word, double-word, single-precision float, and double-precision float), the vast majority of programming languages allow code to use an arbitrary number of user-defined data types.  The JVM recognizes a few primitive types similar to those on a typical machine, plus one more type: the Promiscuous Object Reference.  The Java language likewise recognizes those primitives, and Promiscuous Object References.  While a variable may be constrained not to hold references to anything that isn't a particular class, the language makes no distinctions between any of the following kinds of field of type List<String> that might be held by instance MyThing class MyClass:

A reference to something code knows to be an immutable implementation of List<String>

A reference to an instance of a mutable list type which will never be exposed to anything that might mutate it.

A reference to a mutable list to which, except during the execution of MyThings's methods, no other reference could possibly exist anywhere in the universe.

A reference to a mutable list which is owned by some other object, which that other object would like MyThing to use in some fashion.

A reference to a mutable list which MyThing owns, but which it has also exposed to some other objects so they may do something with it.

Even though all of those fields could have type List<String>, they hold very different things.  An expressive language might allow a distinction among those meanings, but Java does not.  Since a language could attach meaning to such things (at least outside generic contexts) and run on the JVM, that leaves a lot of room for JVM-targetted languages to express concepts which Java cannot.


答:
Please forgive my bluntness, but your post reads like a rant.

You say it's the other guy who wants mindless changes, but then you contradict yourself when talking about this new shiny Java program of yours.

Take a break; it's not a one-way street, please try to find compromises (if you want to continue working on the project - forking is the easiest decision but it won't lead you anywhere useful, although it may stroke your ego).

Please also think thoroughly about the division of labor in the project - turf wars are inevitable if you don't have clean boundaries telling you who's competent at what. Yes, you have to trust other people's judgment sometimes.


答:
There has been a discussion on a similar subject on Linked in .

From what you said l would use TSQL.

One of the features l liked about SSIS was the ability to include some information regarding errors.  So if the package crashed l knew where to look and had some idea why.  Similar idea to this posting over kill for 20 CSV files.  Just another way to solve a challenge.

Title: SQL Server: When to use SSIS vs T-SQL for ETL Tasks

Body: When should I use one versus the other? For instance, let's say I have 20 comma delimited, denormalized text files and I want to transform the data and load it into normalized SQL Server tables.</p>


答:
It is already mentioned in several answers that division of labor is one way to reduce conflict. Here are some concrete examples:

Balance productivity vs. stability. To borrow an analogy from strategy games, a team must consist of a mix of Boom, Turtle and Rush, and teammates must be prepared to change roles in response to the situation.

When one is in a productivity craze, others can work on:

Other features

Bug fixing

Specifications (managing new feature requests, and verifying that the software meets the agreed-upon criteria)

Quality assurance

Manual (exploratory) testing

Automated testing

Documentation

Refactoring and code cleanup

Conduct user studies to collect ideas for improvements

etc.

A project can be modularized (as in software architecture), or even compartmentalized (as in project management) so that each module can be worked on independently.

In general, most software that contains both a front-end and a back-end should be modularized, because they have different velocity of development.

UX-rich software may be hard to modularize due to their heavy use of cross-module event routing.

A project's maintainer may want to keep the project simple by avoiding modularization.

Feature branch. Each developer can fork the project, work on his/her favorite pet feature, and request merge when the implementation is complete. The lead developer can have final say in whether to accept the merge.

Aside from the conflict-avoidance aspect, it is apparent that the project may have insufficient governance.

Why is governance important? Imagine one day a former teammate took a piece of the software, and sue the team for infringement. Or the team sued by patent troll. Or a nobody-knows-who decided to send DMCA notices to the project hosting site and demand the project's source code erased.

At the minimum:

The licence to be used by all contributed source code

The license(s) under which the project's source code can be published

In case a new public license is sought, how to get consent from every contributor

Who can have administrative access to the project

Who will be designated to respond to legal requests (such as the DMCA notices or patent trolls)

Who will manage the finance for the project (paying for server expense and bookkeeping the advertisement revenue or donations)

Who will have voting rights to include new members and expel members.

etc.

Most open-source project sites can provide ready-made project governance charters.


答:
Four choices, I'd reckon.

Kick him out. You're (allegedly) still the main dude on this project; revoke his push privileges and call it a day. The most likely outcome is him forking the project, dividing its community, and then a war breaks loose, and when the dust settles, one of the fork will be the popular one.

Fork it and continue elsewhere. Less aggressive than 1., but the consequences are otherwise the same. You will have to be active in the community though to pull people over to your side.

Leave it be: just let him do what he's doing, calm down, and hope for the best.

Discuss with the community, compromise as required, and resolve the situation.

Personally, I'd prefer option 4.


问:
Unit test strategy for layered (or derived) method calls
Forgive the title -- it needs work.  I am struggling to find better English to express my issue.  Edits encouraged.

Example to describe my issue:

Checker Method

I have an argument checking method called public static void StringArgs.checkIndexAndCount(String, int, int).  Given a string, an index, and a count, confirm the string is not null, and the index & counts are reasonable.  Unchecked (runtime) exceptions are used to report errors.  There is a battery of unit tests written to check all angles of this method.

Layered (or Derived) Method

The checker method is called by other methods, such as public static String removeByIndexAndCount(String, int, int).  The first line of this method checks the arguments by calling the above checker.

Unit Test Strategy

When I write unit tests for the second/layered/derived method, how do I account for the existing set of unit tests for the checker method?  It seems to violate duplication/copy-paste principles to simple re-add the same unit tests (modified slightly) from the checker method to the second method.

Please advise.

My code is Java, but I don't think that particularly relevant, as this same issue could occur in any language.

答: In general, I wouldn't focus too much on reducing repetition in unit tests. You need to test the lower-level module so it can be reliably re-used, and you need to test the higher-level one in case you change it to stop using the lower-level module. Writing these tests is a little laborious, but once they're written the repetition will not cause the same kind of code quality issues as repeated production code in my experience.</p>


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

Any suggestions?

答:
As well as writing Code Complete, Steve McConnell also wrote Rapid Development: Taming Wild Software Schedules which is about software development project management and for software project managers is almost as good as Code Complete.

He also wrote Software Estimation: Demystifying the Black Art and The Software Project Survival Guide both of which are worth a look, though Software Estimation is quite... detailed on a one specific subject so unless you're really interested in that you might want to steer clear.


答:
The concept of a closure is not specific to functional programming. It simply means that you have:

A variable in a scope

Something constructed inside this scope (typically a function or an object) that uses said variable

That something being passed to somewhere outside the originating scope

The textbook example is something like this:

```<code>function foo() {
    var x = 23;
    var bar = function(y) {
        return x + y;
    };
    return bar;
}
</code>```

...and we say that bar closes over x.

However, we can do the same with objects, really:

```<code>function foo() {
    var x = 23;
    var bar = {
        "y": x
    };
    return bar;
}
</code>```

The mechanism is the same, even though now it is not a function that closes over x, but an object. It is not usually called a closure though, because unlike function closures, this kind of behavior is "obvious" for an imperative programmer, and the idea is that we're just referencing a local variable to put a value into an object -- but then, that's exactly what a closure does, only that the object can also be a function.

Now, as far as functional programming goes: The most important concept is the function. Unlike functions in imperative languages, for which "routine" or "procedure" is actually a much better name, functional-programming functions are conceptually like Mathematical functions: mappings from things to things. By taking this concept and using it as the first and foremost expressive primitive, the other hallmarks of functional programming follow logically:

Functions map inputs to outputs; that is all they do. Side effects such as printing or maintaining mutable state don't fit this model, so functional programmers tend to avoid these things. This is what people call purity: a pure function is a function that does not have side effects. Different FP languages treat this matter differently; at the extreme end, there is Haskell, which does not allow any impure functions at all, while at the pragmatic end, there are Lisp, Scheme, JavaScript etc., which allow side effects to appear anywhere and leave their avoidance to boyscout programmers.

Functions are things, too, so it makes sense to have functions that take functions as input, or return functions as output, or both. Such functions are known as higher-order functions, and their use is ubiquitous in functional programming. The famous map function is such a higher-order function: one of its arguments is a function, which map applies to every element in the other argument (which is supposedly a list of some sort).

Functions can be expressed in terms of themselves, a.k.a. recursion. Recursive definitions are easier to write in a pure fashion; they do not rely on mutable-state constructs such as loop counter variables. Because of this, functional programmers tend to prefer recursive solutions over iterative ones, and functional programming languages provide various optimizations to avoid the problems that recursive programming can cause (stack overflow, memory leaks, etc.). Further, common types of recursion are available in a generic fashion in every functional programming language worth the label, the most famous ones being map and reduce (a.k.a. fold). With the help of these functions, a functional programmer can abstract away the details of the actual recursion, and thinking in terms of map, reduce and filter becomes second nature at some point.

The preferred type of function is the unary function, which takes only one argument. Using closures, any n-ary function can be rewritten as a unary function that returns an (n-1)-ary function, and by fully applying this logic, any n-ary function can be written as a nested chain of unary functions returning the next link in the chain. This process is called currying, while the concept of calling a function with an incomplete set of arguments, yielding another function that takes the rest of the arguments, is known as partial function application. As a simple example, if you have a function that you can call like so: foo(a, b, c), then the fully-curried version would be called as: foo(a)(b)(c); calling the curried foo like this: foo(a)(b) constitutes partial application and yields a function which, when called with c, gives the same result as the original call, e.g.: f = foo(a)(b); f(c).

Title: Are closures sufficient to characterize functional programming?

Body:
All functional programming languages that I know of (e.g. Haskell, Scala, Scheme, Clojure, SML, OCaml, ...) support a notion of closures.

Also, I often read that a language X can be considered functional because it supports closures.

On the other hand there are programming languages that do have closures but are not considered functional. The best example I know of is Smalltalk: Smalltalk has blocks (which in most implementations behave like closures, see e.g. here) but it is considered a pure object-oriented language.

So, while closures are a very common (or even essential) construct in functional programming, are they really sufficient to characterize functional programming? If the support of closures is sufficient for considering a language functional, why isn't Smalltalk considered functional too?

Or is the notion of a closure orthogonal to both functional and
object-oriented programming?

Body:
The concept of a closure is not specific to functional programming. It simply means that you have:

A variable in a scope

Something constructed inside this scope (typically a function or an object) that uses said variable

That something being passed to somewhere outside the originating scope

The textbook example is something like this:

function foo() {
    var x = 23;
    var bar = function(y) {
        return x + y;
    };
    return bar;
}

...and we say that bar closes over x.

However, we can do the same with objects, really:

function foo() {
    var x = 23;
    var bar = {
        "y": x
    };
    return bar;
}

The mechanism is the same, even though now it is not a function that closes over x, but an object. It is not usually called a closure though, because unlike function closures, this kind of behavior is "obvious" for an imperative programmer, and the idea is that we're just referencing a local variable to put a value into an object -- but then, that's exactly what a closure does, only that the object can also be a function.

Now, as far as functional programming goes: The most important concept is the function. Unlike functions in imperative languages, for which "routine" or "procedure" is actually a much better name, functional-programming functions are conceptually like Mathematical functions: mappings from things to things. By taking this concept and using it as the first and foremost expressive primitive, the other hallmarks of functional programming follow logically:

Functions map inputs to outputs; that is all they do. Side effects such as printing or maintaining mutable state don't fit this model, so functional programmers tend to avoid these things. This is what people call purity: a pure function is a function that does not have side effects. Different FP languages treat this matter differently; at the extreme end, there is Haskell, which does not allow any impure functions at all, while at the pragmatic end, there are Lisp, Scheme, JavaScript etc., which allow side effects to appear anywhere and leave their avoidance to boyscout programmers.

Functions are things, too, so it makes sense to have functions that take functions as input, or return functions as output, or both. Such functions are known as higher-order functions, and their use is ubiquitous in functional programming. The famous map function is such a higher-order function: one of its arguments is a function, which map applies to every element in the other argument (which is supposedly a list of some sort).

Functions can be expressed in terms of themselves, a.k.a. recursion. Recursive definitions are easier to write in a pure fashion; they do not rely on mutable-state constructs such as loop counter variables. Because of this, functional programmers tend to prefer recursive solutions over iterative ones, and functional programming languages provide various optimizations to avoid the problems that recursive programming can cause (stack overflow, memory leaks, etc.). Further, common types of recursion are available in a generic fashion in every functional programming language worth the label, the most famous ones being map and reduce (a.k.a. fold). With the help of these functions, a functional programmer can abstract away the details of the actual recursion, and thinking in terms of map, reduce and filter becomes second nature at some point.

The preferred type of function is the unary function, which takes only one argument. Using closures, any n-ary function can be rewritten as a unary function that returns an (n-1)-ary function, and by fully applying this logic, any n-ary function can be written as a nested chain of unary functions returning the next link in the chain. This process is called currying, while the concept of calling a function with an incomplete set of arguments, yielding another function that takes the rest of the arguments, is known as partial function application. As a simple example, if you have a function that you can call like so: foo(a, b, c), then the fully-curried version would be called as: foo(a)(b)(c); calling the curried foo like this: foo(a)(b) constitutes partial application and yields a function which, when called with c, gives the same result as the original call, e.g.: f = foo(a)(b); f(c).


问:
Distance calculation and data normalization
I'm designing a system which needs to report basic distance between zip codes. It will use the Google Distance Matrix API but I want to cache the results in the database so that multiple requests for the same data points don't result in duplicate API calls.

A basic Distance class might look like this:

A basic data structure for the cache might look like this:

Since I'm not using fine-grained detail concerned with changes due to one-way streets or divided highways, interchanges, etc., it therefore doesn't matter to me whether I'm getting the data from point A to point B or from point B to point A. But I'm not sure how to express this with full normalization in the database. With this primary key, it would be perfectly legal for the same distance to exist in two separate row, i.e.

```<code>var row1 = new Distance("00001", "00002");
var row2 = new Distance("00002", "00001");
</code>```

It would probably be a good idea for me to require a SortedList parameter in the constructor, but is there a way I can design it from the database side to enforce full normalization?

答:
If the location is being represented by a zipcode (fine for the cases where most distance estimates are within a tolerance for the same zipcode), then I'd impose a CHECK constraint on the fields Zip1 and Zip2 that Zip1 =< Zip2.

Of course this imposes a corresponding burden on the middleware logic to understand that the convention to assure only one record is putting the smaller zipcode in Zip1.  This is mainly a concern for INSERTing rows, as queries can be agnostic about which field is first, i.e. WHERE (Zip1 = myZip1 and Zip2 = myZip2) OR (Zip1 = myZip2 and Zip2 = myZip1).

However normalization is not violated by storing both orders in the compound key.  It uses something less than twice the number of records, but these look to be fairly small rows, so I'm not sure you can justify the logic complexity on efficiency grounds (least of all in terms of speed).


问:
Does non-virtual-by-default lead us to composition-over-inheritance?
There are some design guidelines about testable code in “The Art of Unit Testing”. The first one is “Make methods virtual by default”. I’m curious to know your idea about non-virtual-by-default behavior in C#. I’ve read about Hejlsberg opinions but I think one the most important reasons could be that it may lead us to “composition over inheritance” principal.

Could “composition over inheritance” be one of those reasons which make non-virtual-by-default preferred over virtual-by-default?

UPDATE

Regarding this subject, please consider test-driven point of view; where we want to write testable code. While we are encouraged to make all members virtual by default (at the mentioned book), we can follow "composition over inheritance" and keep going non-virtual-by-default. Isn't it better?

答:
I am puzzled as to why a book on testability would be recommending creating virtual methods, unless it is speaking more to a C++ audience than a C# audience.

Making methods virtual should be done only with great care and caution.  Most classes I write that are not explicitly abstract are generally sealed.  That way I can be sure that the code that I write is rhobust and thoroughly tested.  I can be sure that no other developer is going to inherit my class, violate the Liskov Substitution principle and throw some kind of exception in the code that I have written.

If a developer follows the SOLID principles, particularly the last one (dependency inversion), testability should not be a huge concern.  It's when these principles are violated that testing becomes a lot harder.

Here are my rules of thumb for writing testable code:

Avoid writing static methods/classes.

Follow the SOLID principles.

Seal as many classes as is practical and use the virtual keyword very sparingly.

With dependency injection and mocking tools tests are easy to write (even if some become long because of the data that is required to test against).


问:
Incrementally releasing software under the GPL
We have a Javascript codebase that we want to open-source in an incremental fashion. That is, we want to release the full code to some of our core modules first, keeping the rest of the "application" code in minified format. Eventually, we will also release the whole application code in full (although that may take a few months to happen).

We have determined that the GPL (v3) is the best license for our software project. However, we're concerned that it cannot be used in the way that I've described. Specifically, since our application code depends on the aforementioned core modules to function, it would become bound by the GPL the moment these modules are released under the GPL. If I understand correctly, this means that we would have to release a non-minified version of the application code from day one.

Is my understanding correct? And if so, is there a "next best" license that we could use for our core modules, which would provide us with similar protection against non-free derivatives, without the requirement that we release the whole application code in full from the start?

答: The GPL binds others, not you.  It places restrictions on what people and organizations who are not the copyght holder can do with the code.  The copyright holder can always do whatever they want with it.


答:
You've made it a little unclear exactly what your role is here.  The answer depends on how you fit in.

If you're leading the project and control the git repository

Take back control.  If this guy is making commits you don't like without consulting you, remove his direct commit access.  He can fork the project and make pull requests to merge his commits.  Thats how open source should work until a user builds up trust.  You don't need to, and shouldn't, give full access right away.

If somebody else controls the repo

Express your concerns to the person who does,and encourage a more disciplined process for planning and approving changes.  If the leadership is not amenable to a process, then you can choose to accept the status quo and keep contributing, you can fork the project and work on your own version (bringing along anyone who agrees with you), or you can choose to leave and work on other things.  In any case you are not required to continue to deal with this.


问:
At what stage of an Open Source project should you invite contributions from the community?
I've been wondering about getting contributions to a new open source product my team will be developing.  There's encouragement for us to get as much support from the wider community as we're able, but I can also see this absorbing a lot of time making sure that 3rd parties located outside our office are on track regards things like code quality.  Also at the start of the project we're likely to have a lot of informal discussions within the core team regarding design of the system, spikes etc. and taking these online to allow community involvement will be time consuming and I can imagine could make the discussion less effective.

There is a more human side to this which probably needs to be considered: allowing community involvement in the design process may also have its benefits regards perceived ownership of the project, and there's always a chance that early involvement could pick up on problems that the core team haven't noticed.

So the question: at what stage of an Open Source project should you invite contributions from the community?

答: This was discussed at length in the google IO talk Myth of the genious programmer, by Brian Fitzpatrick and Ben Collins-Sussman of Subversion. In short they concluded that it shouldn't be so early that there is nothing there yet (ie. "come look at my awesome project! there is not really much here yet but a lot of awesome things are planned!") or too late so all the decisions are already made (It's hard to get a word in on a solo project.)</p>


问:
I18n strategy for java web apps
Internationalized applications usually display links in header for translating in different languages, e.g.: Spanish | English | Italian.

These links usually translate current page in the selected language, and usually the url includes the language: http://www.mysite.com/myapp/es/somePage.

Questions:

Does this only apply to static content?

Is it possible to internationalize this way dynamic content? For instance, a page which results in some form submission, clicking  a different language might result in undesirable consequences. Some dynamic requests where transactions are not involved (simple queries, for example) might present no problems with this.

For authorized content, I guess I must ask for language in login page and use that language, i.e., disallow changing locale for authorized content, which, probably, has a lot of transactions. Is this correct?

How do I structure template files? One JSP for each of the languages? One JSP with a properties file for each of the languages?

How do I construct the link according to current request? I guess that url requested and param string should be remembered and reconstructed in JSP.

答:
An i18n strategy largely depends on the framework you are using, as most of them have built in support for internationalisation and it wouldn't make sense to custom roll your own if your framework already supports it.

Spring MVC, for example, uses properties files and a locale resolver to display different versions of pages. They append ?language=xx into the URL to determine which properties file to look for to display the messages.

Most of the Java frameworks that I've worked with have achieved internationalisation in a similar fashion. Multiple properties files that are suffixed with language code and then using <message>-type tags to render the content.

Does this only apply to static content?

No. Depending on how you are tracking what language the user has selected, you can easily pull dynamic content based on a language filter. If you're pulling data from a table, you can have a column in the table that specifies the language. That way, when you're SELECTing from the table you just do WHERE language = xx to get back the content that matches the language the user has selected.

Is it possible to internationalize this way dynamic content? For instance, a page which results in some form submission, clicking a different language might result in undesirable consequences. Some dynamic requests where transactions are not involved (simple queries, for example) might present no problems with this.

A user changing their preferred language on your website shouldn't break their current session, regardless of whether they're currently filling in a form or not. Don't do a SUBMIT on the form when they change the language and it shouldn't have any unexpected results.

For authorized content, I guess I must ask for language in login page and use that language, i.e., disallow changing locale for authorized content, which, probably, has a lot of transactions. Is this correct?

You can do that if you like, it depends on how you structure your web app and how you are handling sessions within your authorised area. You could ask someone to choose their language at the login page and then not allow them to change after that.

You could also let them change their language at any time and just "refresh" their current page, pulling in the content from the "new" language rather than forcing them to log out and back in again. (This is the better way to do it, IMO).

How do I structure template files? One JSP for each of the languages? One JSP with a properties file for each of the languages?

Again this depends on the framework you're using. Don't create a different JSP for each language as if you need to change the page you need to change it for every language. Whilst this is also true for properties files, you're changing content in them and not page layout/design. A properties file for each language is how I've generally seen it done, and seems to be recommended by most frameworks.

How do I construct the link according to current request? I guess that url requested and param string should be remembered and reconstructed in JSP.

You guessed correctly. Once a user selects a language save it in their session scope and pass it along with them.

Title: I18n strategy for java web apps


答:
Google had a couple of tech talks about this a few years ago. Watch them: 1,2.  In a nutshell:

Comprehend:  Understand your community's motivation to work on your project vs all other opportunity costs, and preserve those reasons.

Fortify:  Build a healthy community with social norms of politeness, respect, trust, and humility.

Identify:  Look for tell-tale signs of poisonous people (too many to list, but if you're asking this question then you probably already know many of them).

Disinfect:  Be calm and stand your ground, remain unreactive to insults, slights, challenges, disrespect, etc., and persistent in reinforcing your community norms.

A comprehensive written outline is available too if you prefer to read instead of watch.


问:
How Lua handles both integer and float numbers?
As far as I remember myself programming I was taught not to compare floating point numbers for equality. Now, while reading Programming in Lua about Lua number type, I found following:

The number type represents real (double-precision floating-point)
  numbers. Lua has no integer type, as it does not need it. There is a
  widespread misconception about floating-point arithmetic errors and
  some people fear that even a simple increment can go weird with
  floating-point numbers. The fact is that, when you use a double to
  represent an integer, there is no rounding error at all (unless the
  number is greater than 100,000,000,000,000). Specifically, a Lua
  number can represent any long integer without rounding problems.
  Moreover, most modern CPUs do floating-point arithmetic as fast as (or
  even faster than) integer arithmetic.

Is that true for all languages? Basically if we don't go beyond floating point in doubles, we are safe in integer arithmetic? Or, to be more in line with question title, is there anything special that Lua does with its number type so it's working fine as both integer and float-point type?

答:
Lua claims that floating point numbers can represent integer numbers just as exactly as integer types can, and I'm inclined to agree.  There's no imprecise representation of a fractional numeric part to deal with.  Whether you store an integer in an integer type, or store it in the mantissa of a floating point number, the result is the same: that integer can be represented exactly, as long as you don't exceed the number of bits in the mantissa, + 1 bit in the exponent.

Of course, if you try to store an actual floating-point number (e.g. 12.345) in a floating point representation, all bets are off, so your program has to be clear that the number is really a genuine integer that doesn't overflow the mantissa, in order to treat it like an actual integer (i.e. with respect to comparing equality).

If you need more integer precision than that, you can always employ an arbitrary-precision library.

Further Reading

What is the maximum value of a number in Lua?


问:
The Relative Cost Efficiency of (Acceptance) Test Driven Development
I would like to know what the overall impact of resource planning on a software project is, where the requirements and design of the project are driven by automated acceptance tests and unit tests, in contrast to a more "traditional" approach to software development.

What, in your experience, is the overall effect on resource requirements for completing a software project under TDD, as opposed to more "traditional" development methodologies?  It seems self-evident to me that quality would increase, and the amount of uncertainty decreases because testing is done earlier, but requiring tests up front seems like it would require more developer hours to accomplish.  How much does the development effort increase, or does it actually decrease due to the up-front elimination of bugs?

How much more effort is required from the customer?  Do they have to change the way they relate to the project, especially if they are used to big design up front?  Does the number of hours required of the customer overall increase, or does it actually decrease?

I would imagine that time estimates would be very vague in an iterative TDD process at the beginning of a TDD project (since there is no Software Development Plan).  Is there a point, say, 20% into a project, where confidence increases enough that a more or less stable time and money estimate can eventually be provided to the customer?

Note: I'm not looking for subjective opinions or theories here, so please don't speculate.  I'm looking more for real-world experience in TDD.

答:
requiring tests up front seems like it would require more developer hours to accomplish. How much does the development effort increase, or does it actually decrease due to the up-front elimination of bugs?

This is actually not true. If your developers are writing unit tests (and they should), then the time should be approximately the same, or better. I said better, since your code will be completely tested, and they will have to write only the code to fulfil the requirements.

The problem with developers is they tend to implement even things that are not required to make the software as generic as possible.

How much more effort is required from the customer? Do they have to change the way they relate to the project, especially if they are used to big design up front? Does the number of hours required of the customer overall increase, or does it actually decrease?

That shouldn't matter. Whoever do the requirements should do it as good as possible.

If you do agile way of development, then that does not mean big design up front. But, the better the requirements, architecture and design are done - the code quality will increase, and time to finish the software will decrease.

Therefore, if they like to do BDUF let them do it. It will make your life easier as developer.


答:
The first thing that needs to be stated is that TDD does not necessarily increase the quality of the software (from the user's point of view).  It is not a silver bullet.  It is not a panacea.  Decreasing the number of bugs is not why we do TDD.

TDD is done primarily because it results in better code.  More specifically, TDD results in code that is easier to change.

Whether or not you wish to use TDD depends more on your goals for the project.  Is this going to be a short term consulting project?  Are you required to support the project after go-live?  Is it a trivial project?  The added overhead may not be worth it in these cases.

However, it is my experience that the value proposition for TDD grows exponentially as the time and resources involved in a project grows linearly.

Good unit tests give the following advantages:

Unit tests warn developers of unintended side-effects.

Unit tests allow for rapid development of new functionality on old, mature systems.

Unit tests give new developers a faster and more accurate understanding of the code.

A side effect of TDD might be less bugs, but unfortunately it is my experience that most bugs (particularly the most nasty ones) are usually caused by unclear or poor requirements or would not necessarily be covered by the first round of unit testing.

To summarise:

Development on version 1 might be slower.
Development on version 2-10 will be faster.

Title: The Relative Cost Efficiency of (Acceptance) Test Driven Development

Body:
I would like to know what the overall impact of resource planning on a software project is, where the requirements and design of the project are driven by automated acceptance tests and unit tests, in contrast to a more "traditional" approach to software development.

What, in your experience, is the overall effect on resource requirements for completing a software project under TDD, as opposed to more "traditional" development methodologies?  It seems self-evident to me that quality would increase, and the amount of uncertainty decreases because testing is done earlier, but requiring tests up front seems like it would require more developer hours to accomplish.  How much does the development effort increase, or does it actually decrease due to the up-front elimination of bugs?

How much more effort is required from the customer?  Do they have to change the way they relate to the project, especially if they are used to big design up front?  Does the number of hours required of the customer overall increase, or does it actually decrease?

I would imagine that time estimates would be very vague in an iterative TDD process at the beginning of a TDD project (since there is no Software Development Plan).  Is there a point, say, 20% into a project, where confidence increases enough that a more or less stable time and money estimate can eventually be provided to the customer?

Note: I'm not looking for subjective opinions or theories here, so please don't speculate.  I'm looking more for real-world experience in TDD.


问:
Is there any theory or science for building complex visualization GUI's?
Most "Canvas" widgets in common GUI toolkits offer low level operations like drawing basic shapes of different colors/line-width/etc, or displaying sprites and images.  Some seem to have slightly more advanced features like the ability to treat primitives drawn on the canvas as "actors" so they can be moved and manipulated independently after drawing.

How do you go from such low-level operations to highly complex and interactive visualizations, for example the Integrative Genomics Viewer (screenshot 1, screenshot 2, screenshot 3)? This tool lets you navigate various annotations and features located on a genome in a variety of ways, at the single-nucleotide level (ie, very low detail) to the whole-genome level (very high level overviews), handles very large data sets (100's of gb) and incorporates many different plot types many of which are unique to the application or at least bioinformatics.

Beyond MVC, is there underlying theory or model as to how you should structure such an application?

答:
This is a topic with a lot of public funding (both for genomics and national security) and there are many scientific papers on the various details of it. As a starting point for further search here are some on designing the visualization side of such systems:

http://vis.stanford.edu/papers/protovis

http://vis.stanford.edu/papers/d3

http://faculty.utpa.edu/fowler/csci6175/papers/SooStasko_2007_InteractionIV_ProcIV.pdf

For big datasets performance is more important and there are specialized display techniques to fit as much information as possible into every single pixel, but I don't have any links at hand right now.


答:
There's a chapter in Making Software about Test-Driven Development, which cites the paper discussed here.

Case studies were conducted with three development teams at Microsoft and one at IBM that have adopted TDD. The results of the case studies indicate that the pre-release defect density of the four products decreased between 40% and 90% relative to similar projects that did not use the TDD practice. Subjectively, the teams experienced a 15–35% increase in initial development time after adopting TDD.

Whether these results are generalisable to your case is, of course, something that proponents of TDD will argue is obvious and detractors of TDD will argue is untrue.


问:
Should an algorithm handle invalid inputs and can it have hard-coded paths for certain input data?
I have certain preconditions on input data and I need to design an algorithm for that data. Should I also handle invalid preconditions? Also is it normal that my algorithm will work only  for a certain number of inputs, for example 4, and for any smaller number of inputs I'll have "if input.size" clause?

To clarify my question. Well, actually it's a task from a book but anyway. So it says i'm given an array sorted, then swapped so that it looks like "4567123" first part "123" is swapped. So, should I bother to design algoritm for non-swaped case? Also my algoritm doesn't work well for input size 1, 2, 3, so I inserted cases for 1 , 2 ,3. Is it ok. So here is the code:

```<code>//file SwapedArraySearch.java\
public class SwapedArraySearch {
    public static int search(int[] arr) {
        switch (arr.length){
            case 1:
                return arr[0];
            case 2:
                return arr[0] > arr[1]?arr[1]:arr[0];
            case 3:
                return arr[0] > arr [1] ? (arr[1] > arr[2]?arr[2]: arr[1]):
                                            (arr[0] > arr[2]?arr[2]: arr[0]);
        }
        int x = arr[0];
        int n = arr.length/2;
        int prevn = arr[n] > x ? arr.length -1 : 1;
        int t;
        while(prevn != n) {
            if (x < arr[n]) {
               if (arr[n] > arr [n + 1] )
                   return arr[n+1];
                t = n;
                n = n + (prevn - n)/2;
                prevn = t;
            } else {
                if (arr[n - 1] > arr[n])
                    return arr[n];
                t = n;
                n = prevn + (n - prevn)/2;
                prevn = t;
            }
        }
        return arr[0] > arr[1] ? arr[arr.length - 1] : arr[0];
    }

    public static void main(String... args) {
        int[] arr = new int[args.length];
        for (int i = 0; i < args.length; i++)
            arr[i] = Integer.valueOf(args[i]);
        System.out.println(search(arr));
    }
}
</code>```

Also I'm bother that it looks too complex. Doesn't it or is it fine?

Also, let's say, binary search, it obviously shouldn't check before run that array was sorted before, right?

答:
First you need to distinguish your algorithm and your implementation of this algorithm: the algorithm must consider all the possible cases while your implementation can assume there is a more restrictive context.

Practically there are different strategies you can adopt.
First, you can consider that the inputs may be not well formatted (invalid string, negative numbers, too small list, etc.), and it's your problem. Then it's your job to take these issues into account and to act appropriately. This will improve the robustness  ( the ability to behave smartly under special circumstances) of your implementation. In case of problem, you will typically raise an exception or return a particular value to indicate the problem.

Another strategy consists in programming using contracts. Each of your methods has a contract having a specification for the inputs and the outputs. In the input part, the data that must be given to the method is specified. For instance, you can write "the list must have at least four elements, and all the elements in the list must be positive. The output part describes what the method will retrieve if  the input part of the contract is respected. For instance "the minimal value of the elements is retrieved". If the input part is not respected, the method is free to act as  it wants: it can return an arbitrary  value,  raise an exception , etc. If the client does not respect its part of the contract, the method does not have to respect its own.

Contract programming can be implemented with languages that support it (for instance, Eiffel) or by using conventions + an assert  mechanism.


问:
What is the name of the part of the application, which manages people in general?
In each application there are concepts like users, customers, etc. and some part of the application tries to manage them. For example:

```<code>1. keeping the information of people, organizations, firms, etc.
2. managing users (people who work with the system)
3. managing customers (people who purchase goods/services)
4. distinguishing customers, from users, from ordinary people, etc.
5. ...
</code>```

Do we have a universal term for that area of each application (that subsystem, that module, that whatever)? Or if I want to ask more precisely, can we package customers, users, people, organizations, etc. all in one module, and name it like X? Is this a correct taxonomy of subsystems?

Update: Is Entities a good name?

答:
One of the products of my company is a web application meant to handle communication between agents and their clients for a company which doesn't sell directly to the end buyer but rather sells to agents.

Both the agents as well as the clients must be associated with a user account in this system, but the agents have the power to manage its clients any way he or she pleases, since they belong to that particular agent.  The way we handle authorization is not by establishing what agents and clients can do but rather assigning roles to the users.  The role of a user is its authorization essence without necessarily being associated to a particular user.  When the agent adds a new user, that user automatically has client role, since the way it is setup, the user of a particular role can create new users of an inferior role and client roles, not having any role inferior to it, cannot create new users.  Similarly, we have administrative roles which are superior to agents which can create and manage any agent or client role, but cannot create other administrative roles.

There exists one super-user account which can create and manage users with administrative roles, and of course, everything else.  (How does a super-user get created?  It doesn't. The username and password are established upon installation, and that username and password overrides any role or authorization.)

We call the section where users are created and managed and, in general, configuration of the behavior of the web application for said user since this is separate from the normal functioning of the web application, the backend.  The front-end, on the other hand, represents the part concerning the buying and selling of products (anything which has nothing to do with the behavior of the program, in short). Granted, backend is traditionally used to mean the part of the web application that the users don't see, so perhaps it wasn't the best name.  However, I thought I'd share this since it may be of use to the OP or anyone else reading.


答:
Dynamics of Software Development by Jim McCarthy is also good (as with the McConnell books it's Microsoft Press - a company notorious for shipping mediocre sofware late somehow managed to publish some very good books on how it should be done properly).

It contains 54 "rules" for software development - some obvious, some less so, pretty much all worth remembering and less than 200 pages (with pictures) so very readable (plus the 54 rules structure means it's in nice chunks).

Title: Best books on Managing a Software Development Team?

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

Any suggestions?


问:
How to avoid peppering the code with IFs
I need to add a new payment type to an existing code base. That means that I'm going to have a few methods looking like this:

```<code>if (old payment type)
    process old type of payment
else
    process new type of payment
</code>```

Now, if this could have been determined beforehand, I would have this method point to an interface implementing a common Pay method and then that interface would be implemented by one of two classes. Unfortunately, I only know which method the customer chooses at runtime, which means I need a way to determine which branch to use. Is there another way except for just having ifs spread through the code?

答:
This is textbook example of Strategy pattern.

...a software design pattern, whereby an algorithm's behaviour can be selected at runtime. Formally speaking, the strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it...

This will allow you to have different implementations of how you pay and you can create the concrete one at runtime based on data saved together with payment.


问:
How should you deal with a popular project that you no longer want to maintain?
I am the maintainer of a project which has a large non-technical userbase. I've been maintaining it for about 4 years now and adding new features as they've been requested.

I'd like to move on to other projects now and stop developing for this application. Because of the non-technical nature of the users, there have been very few code contributions in the past. I don't believe I will be able to find anyone else to take over the project in my stead.

Bugs, issues, feature requests - these are still coming in. I am still responding to emails for help, as I am not sure if I should ignore them, tell them that I'm not working on the application, or if I should respond to emails in only certain cases.

What is the best way to 'abandon' this project, but still let users use the application?

Update (July 2016) - It didn't go as planned.  I made an announcement in the README and soon after, I started receiving contributions of a more substantial nature.  Pull requests with bug fixes, features, documentation, issue activity.  Since then, the project has felt 'reinvigorated' and I'm now happily maintaining it along with newer projects.  I have collaborators as well.  At a guess, it may have been the kind of contributions which were affecting my view of the project and with the quality of contributions improving, it didn't feel like a chore any more.

答:
You have included the open-source tag, so I guess your project is software is open source.

there have been very few code contributions in the past

That's unfortunate, but understandable in case you were doing everything. Many users don't get involved as long as it works reasonably.

Some leaders like to delegate all responsibilities, and some leaders like to keep tighter control. While a balance is necessary, delegating as soon as possible is the key here.

I have created over 30 open source projects, and many are still active even though I have left them. Here is what I would recommend:

Give bug tracker access VERY generously, maybe to anyone who has ever contributed a line of code. If someone starts doing crazy things (very low probability), you still have admin control to remove them. Don't forget to give out other rights: source code control, wiki, crowd-translation, facebook page, twitter account, official website, google analytics, etc.

Post in the forum (and website notice) announcing that you are retiring, and looking for a new project leader.

Even if no one steps in as a team leader, fatal problems that could occur (silly example: a hard-coded URL becomes 404, making the app crash at startup), it will probably be fixed by someone. If nobody fixes fatal flaws, then it means you should not worry too much anymore, you have done what you could, but the project does not appear to be viable anymore.


答:
This situation is a typical one for people who are new to building systems.  The key here is about levels of abstraction which are not yet complete in your design.

Some typical taxonomy would be...

Contacts - anyone the system knows about.

Users - Contacts that need to access the system in some capacity.

In turn those Users could also be

Customers who've purchased in the past.

Staff who work in the business.

Suppliers who you purchase from.

Basically you refer to them at the level of abstraction that is important to you at that stage in your action or planning.  There is nothing stopping a User from a member of Staff and a Customer for example.

Title: What is the name of the part of the application, which manages people in general?

Body:
In each application there are concepts like users, customers, etc. and some part of the application tries to manage them. For example:

```<code>1. keeping the information of people, organizations, firms, etc.
2. managing users (people who work with the system)
3. managing customers (people who purchase goods/services)
4. distinguishing customers, from users, from ordinary people, etc.
5. ...
</code>```

Do we have a universal term for that area of each application (that subsystem, that module, that whatever)? Or if I want to ask more precisely, can we package customers, users, people, organizations, etc. all in one module, and name it like X? Is this a correct taxonomy of subsystems?

Update: Is Entities a good name?


问:
Determining the aggregates in this domain
I'm working on an ordering system where we receive tab delimited files that look like this (I removed most of the columns for clarity):

```
      OrderId   OrderLineId    BuyerName   BuyerAdress   RecipientName     RecipientAdress

         1            1         John        123, Street      Doe              25, street
         1            2         John        123, Street      Doe              25, street
         1            3         John        123, Street     Michael           11, France
         2            1         Sam         66,Somewhere     Lily             25, Poland
         2            2         Sam         66,Somewhere     Dave             25, Russia
         ...
```

Applying DDD I primarily extracted few entities as shown in the image below:

Some facts about the data received in the files:

An Order must have a Buyer and a Buyer must have an Order .

An Order contains at least 1 OrderLine and each OrderLine must belong to an Order.

Each OrderLine must have a Recipient and each Recipient must have an associated OrderLine.

The status history for the OrderLine must be tracked by the system (hence the Line History entity)

Questions:

Which entity is the aggregate root? Most of the articles I read about DDD would put the Customer entity as its own aggregate root and the Order with its OrderLines as the other root. However, since the Buyer and Recipient can NOT exist by themselves how can they be treated as their own aggregate root?

Should OrderLines be a value object or an entity? I have seen some answers on numerous blogs suggesting the OrderLines should be a VO, but I don't see why it shouldn't be treated as an entity since its identity will be needed in its relation with the History entity.

Similarly the Line History should be modeled as entity or value object

答:
I think you missed one thing in DDD. And that is relationship between entities is expensive and you should think twice before adding it. You only add entity relationship if it is completely necessary. In your case, Buyer has no need to have lists of his orders. And there is no need for Recipient having list of order Lines. There is no need for those to exist inside the entities, but they should instead be modeled inside the repository. So you can get orders of given buyer and order lines of given recipient.

Also, there can be as many aggregate roots as there are entities in the design. Nowhere it says to have single aggregate root in whole design.

If I go by my logic, then there are 3 aggregates and roots in your design : Buyer, Recipient and Order with Order lines and Line history.

2 and 3 : Yeah it is logical that Order lines would be entity thanks to the relationship to their history. But I think they should still be part of order aggregate.


问:
Does modular programming affect computation time?
Everyone says that I should make my code modular, but isn't it less efficient if I use more method calls rather than fewer, but larger, methods? What is the difference in Java, C, or C++ for that matter?

I get that it is easier to edit, read and understand, especially in a group. So is the computation time loss insignificant compared to the code tidiness benefits?

答:
First of all: Programs in a higher language is for being read by humans not by machines.

So write the programs so that you understand them. Don't think about performance (if you seriously have perfomance problems then profile your application and enhance the performance where it is needed).

Even if it is true that calling a method or function takes some overhead this does not matter. Today compilers should be able to compile your code into efficient machine language so that the generated code is efficient for the target architecture. Use the optimization switches of your compiler to get the efficient code.


问:
When is the register keyword actually useful in C?
I am confused about the use of register keyword in C. It is generally told that its use isn't needed like in this question on stackoverflow.

Is this keyword totally redundant in C due to modern compilers or are there situations in which it can still be useful? If yes, what are some situations in which use of register keyword is actually helpful?

答: It's not redundant in terms of language, it's just that by using it, you're telling the compiler, you would "prefer" to have a variable stored in register. There is however absolutely zero guarantee that this will actually happen during runtime.</p>


答:
Yes, it is irrelevant.

Computers are tireless, near-perfect execution engines working at speeds totally un-comparable to brains. While there is a measurable amount of time that a function call adds to the execution time of a program, this is as nothing compared to the additional time needed by the brain of the next person involved with the code when they have to disentangle the unreadable routine to even begin to understand how to work with it. You can try the calculation out for a joke - assume that your code has to be maintained only once, and it only adds half an hour to the time needed for someone to come to terms with the code. Take your processor clock speed and calculate: how many times would the code have to run to even dream of offsetting that?

In short, taking pity on the CPU is completely, utterly misguided 99.99% of the time. For the rare remaining cases, use profilers. Do not assume that you can spot those cases - you can't.

Title: Does modular programming affect computation time?

Body:
Everyone says that I should make my code modular, but isn't it less efficient if I use more method calls rather than fewer, but larger, methods? What is the difference in Java, C, or C++ for that matter?

I get that it is easier to edit, read and understand, especially in a group. So is the computation time loss insignificant compared to the code tidiness benefits?