问:
Is a try-catch-finally clause appropriate for production code?
While browsing around on SO, I came across an answer that suggested using a try-catch-finally clause to fix the problem he was having. (A rare bug that sometimes happened to his users). Someone commented that it was a valid solution, but he said it was not recommended for production code.

Is this true? Why is it not recommended for production code?

It seems perfectly valid to me if it is used for catching exceptions. In this context it was for an app and users are very discouraged from continuing if the app just crashes seemingly randomly.

He also mentioned that it could fail at anytime.

Is this true as well?

答:
Notice how the Try-Catch block in your linked answer doesn't actually do anything. He's not doing anything to mitigate the error, not even so much as reporting it.

This is why it might fail at any time. If whatever is causing that error happens again, and you try and use the object inside of the try block, he could cause an error later in the code, at seemingly random spots.

Try-Catch-Finally blocks are perfectly acceptable (and really almost mandatory) in production code, as they offer a means of falling back in exception enabled languages. You'd be hard pressed to find a piece of production java code that doesn't have any try-catch blocks of some sort.

To get into a little more detail, Exceptions are basically a way of telling the calling function that something went wrong. You could do this by returning a null, but then you have to go through all the remaining steps in that method. If you're in a 150 line method that's very resource heavy (querying a database, making calls to an external API) and you know right off the bat something is wrong (you didn't provide a username, you forgot to set a mandatory field) you can bypass all those extra steps and just raise an exception, exiting the function immediately. It also allows for you to bypass intermediary functions. If you're utilizing a lower class that throws an exception and you don't know what to do with it, you just pass it up to the higher class, where that exception might make more sense.


答:
In the linked-to question/answer, the correct solution is to fix the fact that the unsubscribe code is called more than the subscribe code. If there is not time to fix it, then ignoring that specific exception may be a usable short-term workaround. But we all know what that means in production code.

In the general case, there is no clear "one size fits all" answer. There are valid arguments for and against exceptions in general, but the truth is that certain languages and libraries use them so you may as well get used to dealing with them effectively.

Sometimes an exception is relatively harmless and should trigger other behavior than a rethrow. For example, if input is not a valid number (e.g. java.lang.NumberFormatException) and cannot be stored in a numeric field. Maybe there is a way to indicate this on the UI and retry the input operation.

Sometimes an exception is much more serious (e.g. std::bad_alloc) and your program might want to catch it, perform some minimal cleanup, and rethrow to kill itself. In the case of a std::bad_alloc it would, of course, be a bad idea to allocate more memory during cleanup.

What this means is you really need to evaluate every situation. Think "what caused this exception to be thrown?" and "what is the realistic outcome during this exception handler?" It could mean anything from input validation catching something wrong, to something so severe there is no way to recover other than to write a stack trace or line number to the log and quit.

ContentLicense: CC BY-SA 3.0

Title: Is a try-catch-finally clause appropriate for production code?

Body:
While browsing around on SO, I came across an answer that suggested using a try-catch-finally clause to fix the problem he was having. (A rare bug that sometimes happened to his users). Someone commented that it was a valid solution, but he said it was not recommended for production code.

Is this true? Why is it not recommended for production code?

It seems perfectly valid to me if it is used for catching exceptions. In this context it was for an app and users are very discouraged from continuing if the app just crashes seemingly randomly.

He also mentioned that it could fail at anytime.

Is this true as well?


问:
How to handle growth of an open source project?
I've been involved in helping out doing support for an open source project for a year or two now, and the project has gained a lot of popularity since I started. The program sees more than 100,000 downloads per week, and is used by more than 60% of people in its primary field, so we're obviously delighted that people have enjoyed using it so much.

However, the problem is that the development and support base has not grown nearly as fast, and we're starting to hit some growing pains. The small handful of developers (the main developer in particular) are getting stretched pretty thin, and the tech support volunteers are starting to get burned out.

Thus far, it has pretty much just been a bunch of dudes hanging out on IRC, writing this program and helping users. There's no 501(c)(3) organization or LLC or anything like that.

Right now, we don't have a very formal bug tracker or issue database (we do have a forum with a category dedicated to bug reports), which I do admit is something that we could improve to get more developers on board. But I suppose my real question is, how does one make the transition from small personal project to a real...thing? How have the big boys like GIMP, FFmpeg, Blender, etc. handled this transition?

And on top of this, is there a way to offer compensation with a FOSS project? I suppose donations help, but that only goes so far...it seems strange to make a living off of free software, but if the program is going to continue to get better, I don't see how we can continue without compensating people for full time work.

Basically, we're having some growing pains, and feeling "too big for our britches." What can we do to manage this transition and not get burnt out on doing too many things at once?

答:
The REALLY big boys set up all the mechanisms you know about - they run large server farms, they run (sometimes write) bug trackers and build systems, etc.  They often have 501(c)3 foundations that own the copyrights, etc. They get big corporate donations, and companies lend them developers, etc.  You know, BIG stuff.

The not-so-big boys get a lot of help from elsewhere.  The Software Freedom Conservancy, for example, will help moderately-large projects get their legal underpinnings right, and facilitate donations.  There are lots of options for code hosting and bug tracking these days - heck, anybody can get a GitHub site.  And you'll find that many small-to-medium software companies will donate licenses for their proprietary products to support organized Open Source projects - especially when they align with their business somehow.


问:
How to chain together points in an array
I have a series of points with lengths and rotations like this:

I need to create separate chains from points whose lines overlap but I’m having real trouble doing this efficiently.

I have an array of simple Point objects, in no particular order, and I can loop through them and test them with a simple "intersect" function. I need to end up with an array of chains, each with an ordered list of points. (Or another way of representing the chains).

At the moment every avenue I explore seems to involve a convoluted hack of arrays, nudges and fudges. Having never studied Computer Science I wonder if there is some sort of data structure or technique that would lend itself well to this sort of thing.

答:
A naive algorithm for this takes O(n^2) time, that is, for every line you add to the total number of lines, it takes (very roughly) the square of that number of lines in terms of time. So if checking one line took one second, then if we have 5 lines, it'd take 25 seconds.

In order to efficiently process the intersections, we can use a number of different algorithms. Most of these are sweep line algorithms. A common one is the Bentley-Ottmann algorithm. It's not the fastest algorithm for this sort of task but it's fairly easy to implement. You'll need a binary search tree and a priority queue. They are both kinds of trees. Specifically you'll need a red-black tree or similar self-balancing binary tree. Also a binary heap is needed to build the priority queue.

This will process the number of intersections in O(n+k)*log(n)) time. Logarithmic time is less than linear time so this algorithm is more efficient than our naive attempt. There are of course faster options but they are generally more complex.


答:
The stage your project is at is really exciting and crucial, it's very easy to crash and burn (out) but it's also where you can make some crucial decisions that, if everything works, will  help ensure long term viability.

Here are some suggestions.

Read Karl Fogel's great book Producing Open Source Software. He
covers most of the major immediate issues. Although I don't agree
with everything he says, that's just opinion. He totally understands
the open source world.

As @Ross Patterson said, you must absolutely set up a tracker and a mailing list or something similar in order to avoid total chaos. What are you using for version control? If you are on github you can start with their tracker or you can integrate with Jira or something similar  or if you want you can go to SourceForge for now and use their free infrastructure.  You don't say where people are downloading from but you want to make sure you have that set up in a reliable manner and with good download counting.

There is no reason that you can't earn a living on free software if that's what you want, lots of people do it, but it takes a lot of different forms.  You need to decide how you want to do that before making major organizational decisions.  For example, you can and should probably set up a corporation to hold the trademark and copyrights which will also provide some legal protection if ever needed.  However then you will need a president or treasurer. What kind of organization it should be (non profit or for profit, LLC, co-op, partnership) really depends on your goals and should be discussed with a good lawyer. If you got accepted by Software Freedom Conservacy they would help you figure that out and also help with the accounting and tax issues and so on. There are also a few other FOSS incubators such as Software in the Public Interest. Also,  I think Outercurve is a possibility.

In terms of how you earn a living, this is going to depend a lot on the nature of your project.  This is also why I wouldn't immediately jump to say you need a 501c3 (and you may not get it ... see the Yorba project).   Blender supports itself mainly by selling documentation. Other projects have small business ecosystems and/or consulting surrounding them and the developers earn their livings from that. Other projects flat out have dual licensing models so they sell supported versions (that's why MySQL did and why it could be sold to Sun and of course there is RedHat) and have a separate communty release. Others like WordPress have the hosted version as a business model. So there are all kinds of options and you need to figure out what makes sense for you and your community.

Pick someone now to be your community manager to start. And read Jono Bacon's book after you finish Fogel's.

Decide now on a road map that makes sense for your core team; be realistic and don't be bullied by people who are not contributing. Road map doesn't just mean technical plans and features, it's about where you want to go as a project.

Don't be shy about talking to other projects you admire or that are in the same space for that matter. Find out what worked and didn't work for them. Just send an email. Also, you can go to some of the open source general events and just talk to the other projects. On the whole foss people are pretty helpful.

Good luck, it's an exciting thing to be at this stage.

Title: How to handle growth of an open source project?

Body:
I've been involved in helping out doing support for an open source project for a year or two now, and the project has gained a lot of popularity since I started. The program sees more than 100,000 downloads per week, and is used by more than 60% of people in its primary field, so we're obviously delighted that people have enjoyed using it so much.

However, the problem is that the development and support base has not grown nearly as fast, and we're starting to hit some growing pains. The small handful of developers (the main developer in particular) are getting stretched pretty thin, and the tech support volunteers are starting to get burned out.

Thus far, it has pretty much just been a bunch of dudes hanging out on IRC, writing this program and helping users. There's no 501(c)(3) organization or LLC or anything like that.

Right now, we don't have a very formal bug tracker or issue database (we do have a forum with a category dedicated to bug reports), which I do admit is something that we could improve to get more developers on board. But I suppose my real question is, how does one make the transition from small personal project to a real...thing? How have the big boys like GIMP, FFmpeg, Blender, etc. handled this transition?

And on top of this, is there a way to offer compensation with a FOSS project? I suppose donations help, but that only goes so far...it seems strange to make a living off of free software, but if the program is going to continue to get better, I don't see how we can continue without compensating people for full time work.

Basically, we're having some growing pains, and feeling "too big for our britches." What can we do to manage this transition and not get burnt out on doing too many things at once?


问:
How does a clicked event work at a low level?
Specifically in regard to a drag and drop operation, how does a button, text box or whatever know that it has been clicked (or vice versa how does the display manager know what it clicked).

I read some about the COM way of dragging and dropping and I think I get the general idea: on MouseDown, detect what was clicked.

While mouse is still down and cursor position moves a certain amount store the data associated with the drag source.

When the MouseUp event fires, drop the data if the mouse cursor is over a drop target. If it is not a drop target then cancel the operation.

You can expand this to the scenario where you click and drag to select multiple files.. how does the UI know what files it selected?

Does the display manager have a calculated pixel range for every element and compare mouse position clicks to that? If so, how does the display manager know this?

If the answer is too long or difficult to explain here, can you point me to some resources that I could read? I understand that you store data on a mouse click event, but that is a higher level "just call these API functions" view of it.

答:
It is operating system specific. On Linux, the X11 server manage a tree of X-windows (a tree hierarchy of nested rectangular areas). So the X server knows which X-window is concerned by a mouse click.

If you have access to a Linux + Xorg system, try the xev  command.

X11 might be replaced by Wayland in the future.

Underneath, the X11 server program itself manage the mouse location (by integrating the relative movement given by the mouse device) and knows in which X-window is the mouse cursor. It transmit that information -mouse movement & cursor entry/exit events- to the application, here your Java Swing application. See also X Window System protocols & architecture & X Window Core Protocol wikipages.

The trend is to use less server-side X-windows, and move most of the area detection to the client side. Some X11 widget toolkits allocate only one toplevel X-window, and manage widgets internally (but older toolkits used one X-window area per widget).

On other OSes I guess that some software layer (Aero on Windows, or some software layer below that) is receiving mouse displacement events and computing in which rectangular area the mouse cursor has to be displayed.


问:
Help with database design with a strict and structured subset of clinical data
I'm developing an application to keep some of the data in chronic patients, the data came from a very strict subset of data, vital signs, exams, ecc.

On a first glance I thought to create a Patient model with has_many encounters, and put every info in a different field like (age, height, weight, ecc), but it will result in a big model. So I wondered about embedding to make it easier to maintain and scale if in a couple of months I need to register a new exam, right now I have been thinking in something like this:

Patient

```<code>name: string,
address: string,
phone: integer,
diagnose: string,
etc...
</code>```

Encounters

```<code>patient_id: ObjectID,
age: Integer,
vital_signs: [ { height, weight, bmi, systolic_pressure, diastolic_pressure } ],
exams: [ { creatinine, total cholesterol... } ],
next_encounter: Date
</code>```

I'm going to still need to access to bmi for example if I want to get how many people in the community is obese (hint: an awful lot), so embedding maybe it's not a very good idea. In any case, all the "analysis" will only be done only monthly (or less).

I picked mongodb, because I'm afraid the requisites for this database will be changing from time to time, so flexibility it's important.

Maybe in this case since data is structured, every document will have the same types of fields, a relational database could be better? I don't think it makes sense using a nosql database in a sql way, right?

PS.- I read this article from mongodb blog, and I'm more confused about if this really it's the best approach.

答:
Maybe in this case since data is structured, every document will have the same types of fields, a relational database could be better?

Most likely.

I don't think it makes sense using a nosql database in a sql way, right?

Nope. To hesitate a guess, people will start asking you questions like:

What age was bob smith on their third examination?

When did john doe's weight drop below 90kgs?

How many examinations have we done on people with high blood pressure?

So, read up on data modelling, look at a few other example schemas and go for it.


问:
Distributed game development and security of source code
I think this question has already been asked many times, but I can't find any of them. I have a game project (I'm using Marmalade),and a few trusted developers, but I want to invite more. For adding some functionality, for writing scripts and so on. The question is - how can I don't give them the whole source code (only compiled part of it maybe), and/or give them limited rights to the project for doing them their work, but dont have the opportunity to steal the code?</p>

答:
You'll have to split the code repository (assuming you want to give them access to the repo) or have a SCN that allows you to set access permissions so certain groups cannot read parts of the repo. If you use git, you're stuck with the former - splitting the code into modules and only giving out access to some modules.

You'll have to determine how much of the code is required for their access, if they only need binaries (and headers/wdsl/etc) then you could store binaries and required sources in a new repo (populated automatically by your CI system) and give them access to that.

You'll have to tell more in order to get better answers, without knowing the specific details, your question is a bit 'generic'.


问:
Is splitting up a function into several inner functions an anti-pattern?
Imagine a long and complicated process, which is started by calling function foo(). There are several consecutive steps in this process, each of them depending on result of the previous step. The function itself is, say, about 250 lines. It is highly unlikely that these steps would be useful on their own, not as a part of this whole process.

Languages such as Javascript allow creating inner functions inside a parent function, which are inaccessible to outer functions (unless they are passed as a parameter).

A colleague of mine suggest that we can split up the content of foo() into 5 inner functions, each function being a step. These functions are inaccessible to outer functions (signifying their uselessness outside of the parent function). The main body of foo() simply calls these inner function one by one:

```<code>foo(stuff) {
    var barred = bar(stuff);
    var bazzed = baz(barred);
    var confabulated = confabulate(bazzed);
    var reticulated = reticulate(confabulated);
    var spliced = splice(reticulated);

    return spliced;

    // function definitions follow ...
}
</code>```

Is this an anti-pattern of some sort? Or is this a reasonable way to split up long functions? Another question is: is this acceptable when using OOP paradigm in Javascript? Is it OK to split an object's method this way, or does this warrant another object, which contains all these inner functions as private methods?

See also:

Is it OK to split long functions and methods into smaller ones even though they won't be called by anything else? - a previous question of mine, which leads into this one.

@KilianFoth's answer to a very similar question which provides a different perspective compared to the answers given here.

答:
It is not an anti-pattern to split a big function into smaller functions.

However declaring all these functions inside the main one presents a certain risk: because the inner functions have access to all the variables of the outer function, and because in JavaScript variables are mutable, you cannot guarantee that the inner functions won't modify the outer function's state.

In this sense, by nesting them you lose most of the benefits of having small functions. Since they all share a common mutable state, you cannot easily reason about each one independently.

So, keep a flat structure. If you want to hide the private functions, you can use the module pattern or one of the many available JavaScript module libraries.


答:
Rights and capabilities are very different. In fact, legal rights were invented largely because access issues couldn't be solved by enforcing technical capabilities. Sure, we put locks on our cars and houses; but that's really only a first level of defense to deter casual vandalism - a career criminal isn't deterred, and not much hindered either. There is also a layer of legal convention that we rely on, and all things considered, the legal protection is more important: it doesn't prevent individual abuses, but it has the full weight of the state and the community behind it, and when it hits, the perp is going down.

Applied to your question: software law, inconvenient and difficult as it can be, is almost certainly more important for protecting your assets than access control. It's unlikely that you can really separate parts of your application so that others can do useful work on only parts. Overall it is almost certainly better to get a legal agreement in place and hold them to it than try to establish technical controls that are probably either ineffective or hinder their work.

If you're contracting into an area where you don't trust the legal system to protect you, then you're pretty much on your own. In such a situation I would seriously reconsider whether you should be outsourcing at all.

Title: Distributed game development and security of source code

Body: I think this question has already been asked many times, but I can't find any of them. I have a game project (I'm using Marmalade),and a few trusted developers, but I want to invite more. For adding some functionality, for writing scripts and so on. The question is - how can I don't give them the whole source code (only compiled part of it maybe), and/or give them limited rights to the project for doing them their work, but dont have the opportunity to steal the code?</p>


问:
Which Continuous Integration framework do you use and why?
There are quite a few different Continuous Integration (CI) frameworks out there and I'm wondering which is the most popular.  Which frameworks have you used at firms where you work?

Is there any reason one CI framework is more popular than another - perhaps this is to do with the features it offers, things that integrate into it or maybe its just marketing?

It seems like continuous integration is used more in the Java and .net worlds than say ruby or python.  Why is this?

答:
I use TeamCity at work and at home. It has great support for a variety of build runners and is extensible via plugins.

Not dealing with piles of XML for configuration is a huge plus in my books and the free version is sufficient for my home needs.

One problem I ran into with TeamCity has to do with trying to get it to automatically version .NET assemblies. I had to set up a relatively complicated workaround, but once it was in place, it worked like a charm.

ContentLicense: CC BY-SA 2.5


问:
alerting that an object cannot be deleted (due to constraints)
Assume an application with a rich domain model with many classes (e.g School, Classroom, Teacher, Student, Course, Exam, Submission, ...) linking to each other. Model and links are mapped to the database which uses appropriate FKs and constraints (without cascade delete). In the admin panel the user has delete buttons next to each object.

Attempting to delete an object has one of the following two outcomes:

the object isn't being referenced from any other object, so it is deleted

the object is being referenced by at least one other object so it cannot be deleted. An alert is shown to the user.

I've got two ways to implement this:

prior to executing the sql delete, do whatever queries are necessary to discover whether this object can be deleted. If it cannot, then alert user.

go ahead and execute the sql delete and if that fails (due to the rdbms constraints) catch the sql exception and alert the user that it cannot be deleted.

Both ways work well. The first way allows me to give the user a detailed reason why the object cannot be deleted (e.g it is being referenced by 2 Courses and 1 Classroom). The second way allows me to solve the whole problem by not writing any constraints checking code and rely on the solid (and existing) implementation of the db.

Is there a reason why I should definitely choose one over the other?

答:
One reason why I wouldn't use 2nd option : testability. You simply cannot test case, which depends on SQL server throwing an exception. But you can easily test case, where you request specific data and behave based on those data.

Testability is one of the main reasons why many developers move away from having logic inside DB server or relying on DB-related behavior and putting all behavior inside the application (server) itself.


问:
Python case statement?
I made a simple Python script that takes user input in order to generate a series of mathematical responses. In one part I did this:

```<code>while True:
</code>```

And iterated through the loop until I used an if statement to break out of it, something similar to:

```<code>if answer == something_whatever:
  break
</code>```

I know that the examples are vague, but I am not asking for a solution to a programming problem, what happened here is that my professor told me that break statements are not to be used in this way.....she said that break statements should only be used in case statements, using them otherwise is considered bad practice. I boldly told my instructor that there was no case switch construct in Python. Is using a break statement in such way considered bad practice? I understand that there are a lot of ways in which one could break out of a loop, but is using while True really that bad?

答:
When I was in college, once, I was helping my friend solving an exercise. The final result was similar to:

public int ComputeSomething(int a, int b)
{
    if (a == 0) {
        return 0;
    }

    // Business logic (5-6 LOC) goes here.
    return result;
}
```

<p>Then the teacher came, looked at the solution and told us that it's wrong. He then rewrote it <em>the right way</em>:</p>

<pre class="lang-js prettyprint-override"><code>public int ComputeSomething(int a, int b)
{
    int result;
    if (a == 0) {
        result = 0;
    } else {
        // Business logic (5-6 LOC) goes here.
    }

    return result;
}
</code>```

Later, reading books of popular authors, I learnt that what I used is called a guard clause and in this example would actually be a preferred solution (especially since it makes it easier to separate exceptional conditions from the actual logic and to avoid additional indentation.

Long story short, bad practice is usually slightly subjective and highly dependent on the context. Something which would be a bad practice in one language fits well in a different language. Something which is used by experienced developers in the industry would be avoided by teachers in college.

In Python, using break to exit a while has nothing wrong. The case is similar to the example above: do you want to have a single exit point from your while, or do you accept to break out anywhere within the while block?

Rewritten:

```<code>answer_found = False
while not answer_found:

    # Business logic goes here.

    if answer == something_whatever:
        answer_found = True
</code>```

the code has its benefit. The readability is slightly increased if you pick a good name for the variable. Here, a maintainer understands, without having to read the full piece of code, that while processes some questions until it finds some answer.

Also, don't confront your teacher telling that someone on a Q&A website told you so. Instead, rewrite your code to avoid break statements, showing that you actually know how to do that.

Title: Python case statement?

Body:
When I was in college, once, I was helping my friend solving an exercise. The final result was similar to:

public int ComputeSomething(int a, int b)
{
    if (a == 0) {
        return 0;
    }

    // Business logic (5-6 LOC) goes here.
    return result;
}

Then the teacher came, looked at the solution and told us that it's wrong. He then rewrote it the right way:

public int ComputeSomething(int a, int b)
{
    int result;
    if (a == 0) {
        result = 0;
    } else {
        // Business logic (5-6 LOC) goes here.
    }

    return result;
}

Later, reading books of popular authors, I learnt that what I used is called a guard clause and in this example would actually be a preferred solution (especially since it makes it easier to separate exceptional conditions from the actual logic and to avoid additional indentation.

Long story short, bad practice is usually slightly subjective and highly dependent on the context. Something which would be a bad practice in one language fits well in a different language. Something which is used by experienced developers in the industry would be avoided by teachers in college.

In Python, using break to exit a while has nothing wrong. The case is similar to the example above: do you want to have a single exit point from your while, or do you accept to break out anywhere within the while block?

Rewritten:

answer_found = False
while not answer_found:

    # Business logic goes here.

    if answer == something_whatever:
        answer_found = True

the code has its benefit. The readability is slightly increased if you pick a good name for the variable. Here, a maintainer understands, without having to read the full piece of code, that while processes some questions until it finds some answer.

Also, don't confront your teacher telling that someone on a Q&A website told you so. Instead, rewrite your code to avoid break statements, showing that you actually know how to do that.


问:
How does std::string in c++ allocate memory?
I've noticed that the string library doesn't require one to allocate space before writing the input/output. How does the string library allocate that memory dynamically, i know of the 'new' keyword in c++ and 'malloc' in c but when i try something like this, my program stops working.

```<code>char* str = new char[strlen(str) + 1];
cin>>str;
</code>```

i can't get it to work that way, how does the string library work?

I also know of vectors in C++ but not a big fan of using them, i would like to know if there's a way to dynamically allocate memory without having to allocate a size before, something close but not as sophisticated as the string.h library.

How does the string library get unlimited input from user without the programmer specifying buffer size?

答:
If you're not a fan of std::vector you may not want to hear this but, by default, it'll work pretty much the same as std::vector.

When a string reads from a stream, it push_back's each character. What it does when it runs out of space is actually defined by the allocator you've used for the string. The default will be to keep doubling as it does with vectors.

You can change this, of course, by changing the allocator but it sounds like that's not really your question. There's no way to pre-allocate an unknown amount of memory. You've only really got 3 options: allocate none, allocate a fixed amount or allocate an initial amount and then dynamically change it as needs be. Strings and vectors do the latter so that they do what you'd expect but with the trade off that you may over allocate memory depending on the exact length of the string.


问:
Should ORM data access methods be wrapped or used directly?
We're using an internally-built ORM. Each table is represented by a Model class, which inherits from a base model class that has a handful of methods like GetAll, GetWhere (to get rows with specified column values), GetFirstWhere, etc. to access data from the table. There's a question of whether these methods should be protected (so that any database access needs to call a public wrapper method for the exact query) or public methods (so that code anywhere can call GetAll, GetWhere, etc.)

I see some pros and cons for both sides. Which style would be considered best practice and more maintainable?

Protected

Pros: it's easy to see all queries against a table, just open the Model file. This could be useful for checking whether the table is indexed properly, etc.

Cons: extra work and cognitive overhead for every data access. I now have to check whether a query method already exists and create it if not; I have to look up method names, which tend to be specific like "GetThisThingByIDAndName"; also, if I want to change a query, I need to make sure nothing else was using that method or create a new method. Many of these queries are only ever going to be used once, and in practice there end up being tons of these methods whose names don't always clarify exactly what the method does.

Public

Pros: the standardized data access interface allows developers to easily memorize the few data access methods and use them consistently across tables, which makes development faster and simpler. Also, they can make changes directly where the query is taking place without breaking something elsewhere in the codebase.

Cons: requires a search to find all queries against a table. If not careful, there could be some duplication.

答:
You should use Repository pattern (domain driven design) that is decoupling business logic from infrastructure. Details (implementation) should depend upon abstractions, not other way.

Repository pattern let you change underlying persistence storage (database) whenever you want, even in runtime and makes code much easier to test.


答:
How does std::string in c++ allocate memory?

as needed, transparently to the user.

i know of the 'new' keyword in c++ and 'malloc' in c but when i try something like this, my program stops working.

```<code>char* str = new char[strlen(str) + 1];
cin>>str;
</code>```

Well ... yeah; strlen counts the consecutive characters at the provided address, until it encounters a zero. In your case, the specified address is not set (it will be set after strlen is evaluated) so you are effectively calling strlen with some random input - there is no way that would work.

I also know of vectors in C++ but not a big fan of using them,

You really really (really really really) should be. They are an extremely thin (read: fast) wrapper over creating dynamic contiguous memory blocks (exactly what you are trying to solve), that provide type safety, memory bounds safety and automatic memory management.

i would like to know if there's a way to dynamically allocate memory without having to allocate a size before

No.

something close but not as sophisticated as the string.h library.

The string.h is not able to allocate memory without knowing the size. It computes the size every time it needs to allocate memory, at a runtime cost (by, for example, using strlen when needed).

How does the string library get unlimited input from user without the programmer specifying buffer size?

It doesn't. What it does (in rough pseudocode) is this:

compute the size of the input (probably requesting it from the input steam through an API)

compute the new size the string should have after reading

allocate memory (depending on new size), if required

copy data in new memory

delete old memory and use new instead (if any old memory/value was set)

Body:
I've noticed that the string library doesn't require one to allocate space before writing the input/output. How does the string library allocate that memory dynamically, i know of the 'new' keyword in c++ and 'malloc' in c but when i try something like this, my program stops working.

```<code>char* str = new char[strlen(str) + 1];
cin>>str;
</code>```

i can't get it to work that way, how does the string library work?

I also know of vectors in C++ but not a big fan of using them, i would like to know if there's a way to dynamically allocate memory without having to allocate a size before, something close but not as sophisticated as the string.h library.

How does the string library get unlimited input from user without the programmer specifying buffer size?

Body:
How does std::string in c++ allocate memory?

as needed, transparently to the user.

i know of the 'new' keyword in c++ and 'malloc' in c but when i try something like this, my program stops working.

char* str = new char[strlen(str) + 1];
cin>>str;

Well ... yeah; strlen counts the consecutive characters at the provided address, until it encounters a zero. In your case, the specified address is not set (it will be set after strlen is evaluated) so you are effectively calling strlen with some random input - there is no way that would work.

I also know of vectors in C++ but not a big fan of using them,

You really really (really really really) should be. They are an extremely thin (read: fast) wrapper over creating dynamic contiguous memory blocks (exactly what you are trying to solve), that provide type safety, memory bounds safety and automatic memory management.

i would like to know if there's a way to dynamically allocate memory without having to allocate a size before

No.

something close but not as sophisticated as the string.h library.

The string.h is not able to allocate memory without knowing the size. It computes the size every time it needs to allocate memory, at a runtime cost (by, for example, using strlen when needed).

How does the string library get unlimited input from user without the programmer specifying buffer size?

It doesn't. What it does (in rough pseudocode) is this:

compute the size of the input (probably requesting it from the input steam through an API)

compute the new size the string should have after reading

allocate memory (depending on new size), if required

copy data in new memory

delete old memory and use new instead (if any old memory/value was set)


问:
What to do about "stopping point syndrome"?
I have noticed a behavior in myself that I call "stopping point syndrome"... it means I cannot stop working until I reach a "stopping point" (or I become exhausted).  In other words, if I am in the zone working on getting a feature completed, I won't stop until I get it done.   If it's a bug I'm working on, this effect is even more pronounced... I won't stop until it's fixed, or at the very least, understood.  Even though I work from a laptop and have VPN and could easily go home and pick up on the work later that evening, that fact does not help me leave work... it's like I'm afraid I will die before I find the bug.

I have a very hard time explaining this to family members who can't figure out why I never come home from work at the same time and sometimes come home at 11 pm.  Even managers have been mystified by this, and I've noticed that most of my colleagues have no trouble working the same hours every day.

Does anyone else have this problem?  Should I be worried about this and/or try to change it?  If so how?

答:
I think it happens to all programmers at times.  I think it is caused by fear.  Fear that the next day you will completely forget what you are doing now.  Then you would have to spend a lot of time going over stuff you've already gone over to find out what you left half-done.  This is very inefficient.  In my experience, most programmers hate inefficiency.

To combat this, take notes of where you are and what you're doing for the next day, then bribe yourself with something not at work to get yourself moving.


问:
What is the name of λx.λf.fx (like reverse apply) in lambda calculus? Does the corresponding function have a standard name in programming?
What is the name of λx.λf.fx in lambda calculus?

Does the corresponding function have a standard name in functional programming languages, like Haskell?

In object oriented programming, is there a usual name for a method foo which takes a function as an argument, such that x.foo(f) returns f(x)?

答:
I hope I understand your question correctly, but I believe this is known as the (reverse) Pipe Operator in ML languages.

```<code>[1; 2; 3] |> List.map sq // let it = [1; 4; 9]
</code>```

There is also the Reverse Pipe Operator which helps with order of operations.

```<code>printf "The value is.." <| 2 + 3 // let it = "The value is..5"
</code>```

This is useful because the unpiped form

```<code>printf "The value is.." 2 + 3 ;; error
</code>```

would error because printf would try to evaluate "The value is.." 2 and error because there is no defined + operator. In order to make that work, use parenthesis:

```<code>printf "The value is.." (2 + 3) // let it = "The value is..5"
</code>```

As for practical use, the |> operator is incredibly useful and the bread-and-butter of many ML and ML-inspired languages such as F#, LiveScript and Elixir. <| is less common and typically only used when it increases readability.

Body:
I hope I understand your question correctly, but I believe this is known as the (reverse) Pipe Operator in ML languages.

[1; 2; 3] |> List.map sq // let it = [1; 4; 9]

There is also the Reverse Pipe Operator which helps with order of operations.

printf "The value is.." <| 2 + 3 // let it = "The value is..5"

This is useful because the unpiped form

printf "The value is.." 2 + 3 ;; error

would error because printf would try to evaluate "The value is.." 2 and error because there is no defined + operator. In order to make that work, use parenthesis:

printf "The value is.." (2 + 3) // let it = "The value is..5"

As for practical use, the |> operator is incredibly useful and the bread-and-butter of many ML and ML-inspired languages such as F#, LiveScript and Elixir. <| is less common and typically only used when it increases readability.


问:
Should I parse XML on the server or provide a proxy and let the browser parse it?
I need to interface with a 3rd party API. With this API I make a GET request from within the end user's browser and receive an XML response. This data is to be used in a browser based application where the user can search through it, use it to make decisions, etc. The main issue is that most browsers have locked down cross-domain XML use, so I can't simply get the XML from the API.

The overall data, though, is basically broken into two sets.

The first set of data is public and only needs to be updated every so often, so it can be cached for all users on the server side, lightening the traffic considerably.

The second set of data is private and individual to each user. This data is also updated in the API more frequently. This leads caching to be much less effective.

For scalability reasons I would like to keep the server's load a small as possible.

I see two options before me:

Provide a proxy that can be used to route XML requests to the 3rd party server and directly back and forth between client and 3rd party API.

Have the server do the conversion from XML to JSON and strip out unnecessary information. This essentially means making a new API for our server, which translates into requests from the 3rd party API

What would be the best way to provide the data to the user? (Does not have to be one of the two options)

答:
There is a third option which you may not have seen: Cross Origin Resource Sharing (CORS).

The CORS standard works by adding new HTTP headers which allow servers to serve resources to permitted origin domains. Browsers support these headers and respect the restrictions they establish.

Example: Say your site is http://my-cool-site.com and, you have a third party API at domain http://third-party-site.com, which you can access via AJAX.

And let's assume that a page you server from my-cool-site.com made a request to third-party-site.com. Normally, users browser will decline AJAX calls to any other site other than your own domain/subdomain per the Same-Origin Security Policy. But if the browser and the third party server supports CORS, the following things happen:

Browser will send the following HTTP header to third-party-site.com

```<code>Origin: http://my-cool-site.com
</code>```

If the third party server accepts requests from your domain, it will respond with the following HTTP header:

```<code>Access-Control-Allow-Origin: http://my-cool-site.com
</code>```

To allow all domains, third party server can send this header:

```<code>Access-Control-Allow-Origin: *
</code>```

If your site is not allowed, browser will throw an error.

If the client's have fairly modern browsers that support CORS, and your third party server supports CORS as well, you can definitely go for it with minor changes to your code.

I found a nice explanation on CORS, on which you will also find another way to do this: JSONP. But JSONP would require a fair amount of changes to your code.

To make a CORS request you simply use XMLHttpRequest in Firefox 3.5+, Safari 4+ and Chrome and XDomainRequest object in IE8+. When using XMLHttpRequest object, if the browser sees that you are trying to make a cross-domain request it will seamlessly trigger CORS behaviour.

Here is a javascript function that helps you create a cross browser CORS object.

```<code>function createCORSRequest(method, url){
    var xhr = new XMLHttpRequest();
    if ("withCredentials" in xhr){
        // XHR has 'withCredentials' property only if it supports CORS
        xhr.open(method, url, true);
    } else if (typeof XDomainRequest != "undefined"){ // if IE use XDR
        xhr = new XDomainRequest();
        xhr.open(method, url);
    } else {
        xhr = null;
    }
    return xhr;
}
</code>```

Since you say that "most browsers have locked down cross-domain XML use", I guess your third party server might not support CORS. Then you have to find alternative approaches.

Body:
There is a third option which you may not have seen: Cross Origin Resource Sharing (CORS).

The CORS standard works by adding new HTTP headers which allow servers to serve resources to permitted origin domains. Browsers support these headers and respect the restrictions they establish.

Example: Say your site is http://my-cool-site.com and, you have a third party API at domain http://third-party-site.com, which you can access via AJAX.

And let's assume that a page you server from my-cool-site.com made a request to third-party-site.com. Normally, users browser will decline AJAX calls to any other site other than your own domain/subdomain per the Same-Origin Security Policy. But if the browser and the third party server supports CORS, the following things happen:

Browser will send the following HTTP header to third-party-site.com

Origin: http://my-cool-site.com

If the third party server accepts requests from your domain, it will respond with the following HTTP header:

Access-Control-Allow-Origin: http://my-cool-site.com

To allow all domains, third party server can send this header:

Access-Control-Allow-Origin: *

If your site is not allowed, browser will throw an error.

If the client's have fairly modern browsers that support CORS, and your third party server supports CORS as well, you can definitely go for it with minor changes to your code.

I found a nice explanation on CORS, on which you will also find another way to do this: JSONP. But JSONP would require a fair amount of changes to your code.

To make a CORS request you simply use XMLHttpRequest in Firefox 3.5+, Safari 4+ and Chrome and XDomainRequest object in IE8+. When using XMLHttpRequest object, if the browser sees that you are trying to make a cross-domain request it will seamlessly trigger CORS behaviour.

Here is a javascript function that helps you create a cross browser CORS object.

function createCORSRequest(method, url){
    var xhr = new XMLHttpRequest();
    if ("withCredentials" in xhr){
        // XHR has 'withCredentials' property only if it supports CORS
        xhr.open(method, url, true);
    } else if (typeof XDomainRequest != "undefined"){ // if IE use XDR
        xhr = new XDomainRequest();
        xhr.open(method, url);
    } else {
        xhr = null;
    }
    return xhr;
}

Since you say that "most browsers have locked down cross-domain XML use", I guess your third party server might not support CORS. Then you have to find alternative approaches.


问:
Is it a newbie mistake to avoid branching?
My team is new to versioning and we're trying to keep things simple, as to not overwhelm ourselves with tools-headaches. Also, our product can't be compiled and run locally, so our work is actually more like the whole team modifying a single test server.

So we're thinking that we don't want to make too many branches while we work -- branching means merging, and merging requires practice to do right consistently. (We have to use use TortoiseSVN "because reasons" so better alternatives are not relevant now.)

Is it a "newbie mistake" to think that having many branches is a bad thing because it makes the landscape complicated; of fear that having many branches could more easily confuse us when we need to merge things back together again?

答:
It is certainly a mistake, but not necessarily only a newbie mistake.

At my last job (a highly competitive motorsports job) my boss was adamant about avoiding branching.  He had about 20 years experience and still thought it was a bad idea.

I completely disagreed with him and challenged his misconceptions.
There are times where branching isn't worth the potential hassle it brings.  Yet, there are other times where it is clearly the only sane solution (eg, two teams doing two parallel but separate iterations of work on the same source code).  Just like everything else in life, you shouldn't have a blanket rule about it.

So, yes, it's a mistake.  No, it's not just a newbie mistake, though it should be.


答:
The policy of deliberately refusing to branch is usually a mistake, but I don't think branching is feasible in your case. Not because you are using SVN - which is the usual reason for not using branches, since SVN's handle on branches is especially crappy, even compared to SVN's general crappiness - but because of this:

our product can't be compiled and run locally, so our work is actually
  more like the whole team modifying a single test server.

This, IMO, is your main problem, and as long as you don't fix this - I don't think you can use branching even if you wanted to. Different developers can't work on separate branches since they are forced to share a working copy. If one developer checks out a branch, that branch will be checked out for the the developers, which means that you miss the main advantage of branching - allowing multiple developers to work on different features of the same project without getting between each other's legs.

So, you can't use branches to coordinate the collaborations, but solo developers(that use SCMs that are not total garbage - which is not your case here, but let's ignore it for the point of the debate) also use branching - e.g. to test new features that may or may not be merged or to explore the system in a scrapable branch.

Can you use branches in such a way? Well, even if you are OK with moving the whole team to a feature branch or to a scrapable branch - how do you plan to coordinate it? Will you use email, or install a PA system where you can announce WE ARE NOW MOVING TO FEATURE X'S BRANCH? And what will you do when some dev is in the middle of something in the current branch? Will you make them roll it back, or make the entire team wait for that developer to finish?

Do you really want that headache?

Another point to consider - branching usually needs some level of cleanness in the build process and project tree structure, so artifacts of one branch won't leak to other branches when you switch branches. SCMed projects usually have that - or else they wouldn't be able to build on all the machines of all the developers - but seeing that your project can only build on the test server I doubt you have that minimal level of cleanness. Oh, your branches will probably work - at least in the beginning, until a leaky artifact will surprise you with some weird, hard-to-detect bugs.

Title: Is it a newbie mistake to avoid branching?

Body:
My team is new to versioning and we're trying to keep things simple, as to not overwhelm ourselves with tools-headaches. Also, our product can't be compiled and run locally, so our work is actually more like the whole team modifying a single test server.

So we're thinking that we don't want to make too many branches while we work -- branching means merging, and merging requires practice to do right consistently. (We have to use use TortoiseSVN "because reasons" so better alternatives are not relevant now.)

Is it a "newbie mistake" to think that having many branches is a bad thing because it makes the landscape complicated; of fear that having many branches could more easily confuse us when we need to merge things back together again?


答:
Keep in mind that the only purpose for branching is isolation.  You branch when you want to be able to make changes in location A without impacting location B.  Having a branch adds work, so you want to create a branch when the benefit of having the branch outweighs the pain of the overhead involved.

For example, creating a branch to fix a small bug is crazy - the work involved in creating a branch and merging is probably going to be way more than what it takes to fix the bug. On the other hand, you absolutely want a branch for a major release.  You need to have a stable code base with controlled fixes.

Anything in between is a judgement call and really depends on the size of the team and how the team works.  Here, I work on a team of 3 people and we only have the mainline and release branches.  If I was working in a group of 10+ people, I'd expect that we'd have more branches.

Also keep in mind that branch depth is important because it dictates how many merges you need to do.  One common setup is to use individual developer branches that hang off of an integration branch, which then hangs off of main.  In this scenario, a bug fix from a developer branch going to a release needs to be merged, built, and tested 4 times (dev->integration->main->release).  That's a lot of overhead, so there better be tangible benefits from having those branches.


问:
Better support for controlling printing options like headers and footers from Javascript
Why is it that browsers do not provide a mecanism to set the printing defaults regarding headers and footers from within the app?

Web apps are fully fledged apps nowadays, and modern browsers are capable of sending a very well formatted page to a printer (or even download it as a PDF), but web apps cannot rely on this method because browsers don't allow them to control the metadata completely.

Current possibilities don't allow websites to store the printing settings along with the  report or file on the server.

Each browser provides it's own mandatory dialog form to set printing settings that could be replaced when needed by a more convenient experience: in-app options, adapted to the use case, cloud-persistent and consistent along different browsers.

The options that are currently available are:

Delegating part of the work on the user (i.e., having him/her change the metadata options and paper size/orientation themselves).

Using other methods that can quickly become too expensive and complicated, like generating PDF's on the server for the user to download.

Opting for number 1 is not always our decision.  As professional web developers, the final users are usually not our direct clients, but our client's target audience.  Also, we would be forcing the final user to do extra work.  Experience says that it wouldn't be done and reports will be printed with the default settings of the browser, usually with headers and footers that the user don't really want (and the associated paper waste if he/she decides to print it againg without them).

Option 2 --generating server side PDF's-- is becoming less of an option every day.  Modern web apps are heavy users of javascript, and reports benefit from excellent plotting libraries made with JS that can generate charts in HTML canvas elements that could be printed right away if we weren't using this "do it all over again on the server" approach.
The resulting reports never look the same, and programmers have to maintain two versions of every report.

Moreover, generating PDF's on the server can be really slow and resource consuming.

So, is there a compelling reason why browsers programmers opted not to support this kind of customization?

答:
There is a definite tension between designs appropriate for interactive use and those that look good on the printed page. All of the interactive functions that have evolved into HTML pages/apps (e.g. forms, buttons, navigational controls, dynamic AJAX data loads, and interactivity) do not rest well on a static piece of paper. The print-outs of many web pages positively screams "I am a printed web page! I was not designed to be printed!"

But that doesn't mean it can't work. Most of the sites that really care about printed output have a separate printer icon that generates a "designed to be printed page." That requires some extra programming, but is generally manageable with templated output.

Modern CSS has an @media that designates what output realm a CSS rule apples to. Different CSS style sheets can be imported for screen and print:

```<code><link rel="stylesheet" type="text/css" href="style.css" media="screen,print">
<link rel="stylesheet" type="text/css" href="paper.css" media="print">
</code>```

Or you can designate rules directly inside a stylesheet, like so:

```<code>@media screen {
    body {
        background-color: green;
        margin: 20px;
    }
    p {
        font-family: verdana,sans-serif;
        font-size: 14px;
    }
}

@media print {

    @page {
        size: letter portrait;
        margin: 0mm;
    }
    body {
        background-color: white;
        margin: 1in;
    }
    p {
        font-family: sans-serif;
        font-size: 20px;
        color: black;
    }
}
</code>```

Note the @page selector sets page size, orientation, and margins. There are specific mechanisms for handling first pages, left pages, and right pages; for managing pagination (where page breaks occur, how widows and orphans are managed, etc.) On some browsers (Chrome and Opera e.g., and possibly Firefox, setting the page margin to 0 will eliminate the annoying automatically supplied headers and footers you mentioned. But like all things Web, this is browser-specific. On some browsers (looking at you, IE!), the user may need to turn off those embellishments when printing. Microsoft products also seem to obey some Microsoft-specific page controls such as mso-header-margin, mso-footer-margin, and mso-paper-source which might help you squelch undesirable extras.

So, long story short, good browser support for printing is feasible and available. CSS's page-oriented design features don't have 100% the precision of a word processing or page layout format, but they're pretty close. They should be more commonly used.

Here is the relevant CSS3 specification, a good quick overview of the @page rule, and a good article on how to set up a print style sheet.

Body:
There is a definite tension between designs appropriate for interactive use and those that look good on the printed page. All of the interactive functions that have evolved into HTML pages/apps (e.g. forms, buttons, navigational controls, dynamic AJAX data loads, and interactivity) do not rest well on a static piece of paper. The print-outs of many web pages positively screams "I am a printed web page! I was not designed to be printed!"

But that doesn't mean it can't work. Most of the sites that really care about printed output have a separate printer icon that generates a "designed to be printed page." That requires some extra programming, but is generally manageable with templated output.

Modern CSS has an @media that designates what output realm a CSS rule apples to. Different CSS style sheets can be imported for screen and print:

<link rel="stylesheet" type="text/css" href="style.css" media="screen,print">
<link rel="stylesheet" type="text/css" href="paper.css" media="print">

Or you can designate rules directly inside a stylesheet, like so:

@media screen {
    body {
        background-color: green;
        margin: 20px;
    }
    p {
        font-family: verdana,sans-serif;
        font-size: 14px;
    }
}

@media print {

    @page {
        size: letter portrait;
        margin: 0mm;
    }
    body {
        background-color: white;
        margin: 1in;
    }
    p {
        font-family: sans-serif;
        font-size: 20px;
        color: black;
    }
}

Note the @page selector sets page size, orientation, and margins. There are specific mechanisms for handling first pages, left pages, and right pages; for managing pagination (where page breaks occur, how widows and orphans are managed, etc.) On some browsers (Chrome and Opera e.g., and possibly Firefox, setting the page margin to 0 will eliminate the annoying automatically supplied headers and footers you mentioned. But like all things Web, this is browser-specific. On some browsers (looking at you, IE!), the user may need to turn off those embellishments when printing. Microsoft products also seem to obey some Microsoft-specific page controls such as mso-header-margin, mso-footer-margin, and mso-paper-source which might help you squelch undesirable extras.

So, long story short, good browser support for printing is feasible and available. CSS's page-oriented design features don't have 100% the precision of a word processing or page layout format, but they're pretty close. They should be more commonly used.

Here is the relevant CSS3 specification, a good quick overview of the @page rule, and a good article on how to set up a print style sheet.


答:
Have the core of your game engine as a binary, but implement most gameplay-relevant systems and components in form of separately compiled plugins which use a common API to communicate with the core engine. This should enable developers to create plugins with just the binary versions of the core and the other plugins. This architecture can then also be used by modders to create 3rd party add-ons for your game.

However, this will be a lot of work and won't be the most productive way of working for your team members. When you want to be productive, you should just give them access to the source and control them legally through having them sign a non-disclosure agreement. Also remember that you retain the copyright of your code even when you show it to others. So when they really steal your code and you can prove it, you can sue them for copyright violation.


问:
Has there really not been one thing in the past 20 years that provided huge software development gains?
In No Silver Bullet, Fred Brooks makes a variety of predictions about the future of software engineering, best summed up by:

There is no single development, in either technology or in management technique, that by itself promises even one order-of-magnitude improvement in productivity, in reliability, in simplicity.

His argument is very convincing. Brooks was writing in 1986: was he right? Do developers in 2014 produce software at a rate less than 10x faster than their counterparts in 1986? By some appropriate metric -- how large has the gain in productivity actually been?

答:
Do developers in 2014 produce software at a rate less than 10x faster than their counterparts in 1986?

I would imagine that there's been at least an order of magnitude improvement in productivity since then.  But not by leveraging one single development, in either technology or in management technique.

Increases in productivity have come about by a combination of factors. Note: This is not a comprehensive list:

Better compilers

Vastly more powerful computers

Intellisense

Object orientation

Functional orientation

Better memory management techniques

Bounds checking

Static code analysis

Strong typing

Unit Testing

Better programming language design

Code generation

Source code control systems

Code reuse

And so on.  All of these techniques combine to produce productivity gains; there isn't a single silver bullet that has ever produced an order of magnitude speedup.

Note that some of these techniques have existed since the sixties, but have only observed widespread recognition and adoption recently.


问:
What is your approach to debugging a transient problem
I am working on a system that controls a label printer and occasionally the label printer will print the same label 2,3,...,10 times.  The error is transient, occurring once per 3000-4000 (once a month) or so labels.

I am adding tracing to a log file to see what commands are being sent to the printer and state of the printer (we have a state machine defining allowable commands). But since this is a transient problem that may tell me that we had an error, but not why we had the error so I can fix the problem.

I am also trying to get the print job files for the errant jobs so that I can emulate the job in my test environment.  Hopefully with these I can reliably make the error occur

I am curious how other might approach diagnosing this type of transient error.

答:
In rough order of difficulty:

Recall that the error may be out of your system. Maybe some person in accounting submitted the same label 10 times. Debug the database it came from, etc. Sometimes when you debug a unit test, it can take you hours to realize there's a typo in your test data.

Honestly reading your problem my suspicion is your components really
are working as designed, they just, somewhere, somehow, are actually receiving the instruction 10 times, from something you're not actively considering (human error, duplication happening somewhere else in program, human clicking 10 times on a frozen screen, etc.)

Be sure to print the input of the component causing the error. Then you can reproduce and test locally. Your code is unit-testable, right?

Lots and lots and lots of trace.

Dump a lot of information of this error. Print all stack trace. Create a heap dump.

If you can successfully run it on your local machine rapidly and transiently reproduce it, do so and put a breakpoint where you can. Then you can look around the program much better.

Make usual code improvements. Review all culprit files. Refactor anything that strikes you fishy or wrong. In particular see my first point and refactor your buggy components so they have isolated inputs and are more testable. This is similar to cleaning your room until you find your keys.


答:
I regularly use approach two. Downside is that tests require a database. Some will argue that depending on a database is not 'unit-testing'. Indeed.

The problem with this critique is that it considers unit tests as the One Size Fits All. Clearly unit tests are not tailored to be used with foreign systems. That does not mean you should not rely on those systems ; or worst still, not test their interaction with your code. But anyways, you will have to test the case of database foreign key violations because it would be a Bad Thing not to have foreign key constraints, and a Bad Thing not to test them.

To a small extent, you can unit test foreign systems with mocks throwing the expected exceptions... if you know what to expect ! (This may not be the case since the problem statement sounds very schoolish).

In your case, the problem sample seems so small that the extra round trip of solution 1 does not really matter and you need not rely on the database for performance. But solution 1 would require a little extra coding from you, while foreign key constraints are already enforced by the database.

Be smart, be lazy.

Title: alerting that an object cannot be deleted (due to constraints)

Body:
Assume an application with a rich domain model with many classes (e.g School, Classroom, Teacher, Student, Course, Exam, Submission, ...) linking to each other. Model and links are mapped to the database which uses appropriate FKs and constraints (without cascade delete). In the admin panel the user has delete buttons next to each object.

Attempting to delete an object has one of the following two outcomes:

the object isn't being referenced from any other object, so it is deleted

the object is being referenced by at least one other object so it cannot be deleted. An alert is shown to the user.

I've got two ways to implement this:

prior to executing the sql delete, do whatever queries are necessary to discover whether this object can be deleted. If it cannot, then alert user.

go ahead and execute the sql delete and if that fails (due to the rdbms constraints) catch the sql exception and alert the user that it cannot be deleted.

Both ways work well. The first way allows me to give the user a detailed reason why the object cannot be deleted (e.g it is being referenced by 2 Courses and 1 Classroom). The second way allows me to solve the whole problem by not writing any constraints checking code and rely on the solid (and existing) implementation of the db.

Is there a reason why I should definitely choose one over the other?


问:
Protected variable & protected method in the era of TDD
I understand protected method is good for unit testing since you can easily mock by the class by overriding the protected method for the sake of testing.

However, protected variable is just a global variable (although limited) and should be avoided at all.

Especially if you've all the essential getter/setter declared, you should have zero protected variable and it should not affect anything else.

So, in the future, if I am going to elimiate all protected variables in my codes, are there any caveat that I am not aware of?

答: Protected variables exist for a reason: they give parent classes the ability to let their children know about the their internal representation.  If your public get/set methods aren't just pass-through (maybe the get/set is typed as a DateTime, but internally you store everything as Unix time in an int field, for example), then the classes that derive from that class may need to access the underlying value in the field, and not what the object exposes to the outside world (via the get function).</p>


答:
Robert Harvey's answer is good, but I think he left out what may be the biggest reason why programmers are more productive than ever:  widespread availability of software libraries.

When I started my career there was no STL, .NET, QT, etc.  You had raw C or C++, and that was about it.

Things that used to take days or weeks or work (XML parsing, TCP/IP connections, HTTP communciation) can now be done with a handful of lines of C# code.  This is where we have gotten orders of magnitude better in terms of overall developer productivity.

Our current product uses a docking window framework that we purchased from a vendor.  This let me have a fully functional docking window UI in a matter of minutes.  I shudder to think of what it would take to do that in the days of using the win32 API.

Title: Has there really not been one thing in the past 20 years that provided huge software development gains?

Body:
In No Silver Bullet, Fred Brooks makes a variety of predictions about the future of software engineering, best summed up by:

There is no single development, in either technology or in management technique, that by itself promises even one order-of-magnitude improvement in productivity, in reliability, in simplicity.

His argument is very convincing. Brooks was writing in 1986: was he right? Do developers in 2014 produce software at a rate less than 10x faster than their counterparts in 1986? By some appropriate metric -- how large has the gain in productivity actually been?


答:
There is no need to test private or protected member variables, and getter/setter methods are almost always part of the public API, so I'm going to assume that you're talking about unit testing private or protected methods.

This is probably going to be an unpopular opinion, but here goes anyway.

Principle Number One: If your unit testing framework cannot test methods unless they are public, virtual or backed by an interface, your unit testing framework sucks and you need to find a new one.

Now that you have a testing framework that can test private methods...

Principle Number Two: You don't need to mock a private method.  Ever.

Do you see why?  Private methods are not part of the public API of your class, so mocking them doesn't make any sense.

I know there's a school of thought that says that you should make all methods public so that they can be easily unit tested, and that, if your method is not public, it doesn't need to be unit tested, because it gets touched by some public method anyway.

Hogwash.

First of all, why should I pollute my public API with methods that may never be called directly by the consumer?

Second, why shouldn't I be able to test my private methods, directly?  The whole point of refactoring code into separate methods is to sequester functionality behind those methods.  Methods are a little testable black box, and the need to create them and test them independently doesn't diminish merely because I have marked them private.

Don't even get me started on the "one interface for every class" practice.

Title: Protected variable & protected method in the era of TDD

Body:
I understand protected method is good for unit testing since you can easily mock by the class by overriding the protected method for the sake of testing.

However, protected variable is just a global variable (although limited) and should be avoided at all.

Especially if you've all the essential getter/setter declared, you should have zero protected variable and it should not affect anything else.

So, in the future, if I am going to elimiate all protected variables in my codes, are there any caveat that I am not aware of?


答:
First I want to make clear this:

Database must use FK, PK and check constraints, regardless of the application doing preventive validations or not.

Having made that clear,

Advantages of preventive validations

Less page reloads needed

Preventive validations allows to disable delete button accordingly

More user friendly

Disadvantages of preventive validations

More complex presentation logic

More code to maintain

More developing time

In my case I tend to favor code simplicity avobe all else, so I'd choose option 2, but that doesn't seem to be your case so I recommend you go with option 1.

What you should never, ever do is having the database NOT enforce PK, FK or check constraints and rely only in the application for data consistency.


答:
I would suggest that trends pulling us in the other direction (i.e. toward lower productivity) are at least as strong as the trends you asked about. The experience of doing client/server development tool like VB6 or PowerBuilder came pretty close to the "Rapid Application Development" (RAD) ideal. You got to draw your forms, and there were obvious hooks for your procedural or SQL code.

Web development, at least initially, destroyed a lot of the techniques and infrastructure that made these things possible, and many client/server developers just stopped being developers, or clung desperately to, say, VB6.

The transition to heavily client-driven Web development was yet another slash-and-burn experience; Microsoft was getting back to the RAD ideal with WebForms, and then it quickly fell out of favor. Instead developers were expected to abuse the infrastructure (e.g. XMLHttpRequest, which is seldom used for XML) and otherwise monkey around at a low level of abstraction in an effort to make their websites more interactive

There are good reasons behind all of these transitions, and it's not fair to compare a process that yielded a native .EXE (requiring installtion and management at the individual client) to Web development, nor is it completely fair to compare a process that relies heavily on postbacks with one that yields a more seamless experience. But I will say that the practices currently in vogue result in some surprisingly low-level thought processes among people who missed out on client/server, RAD, and the like. Client/server buttons just worked, and one certainly did not have to worry about the underlying data channels that got data to the event handlers that made this happen.

Conversely, more contemporary developers tend to think that those of us who did client/server development (or even postback-based Web development) have a tendency to resort to shortcuts (and mean that in a bad way). That's understandable, but I still think that the way contemporary development is done is surprisingly low level, and the days of searching for a "silver bullet" seem to have given way to the era of mocking those of us who question the wisdom of mining and smelting our own lead.


答:
When I hit a really painful real-world problem, sometimes it helps to escape to a land of make-believe where absurd assumptions can be made - and then work backwards from this more pleasant world.

So, let's remodel your problem as one that...well, sucks less to have to deal with.

Let's say you know that the next time this function runs and someone tries to print a label, you will get this duplicate behavior. However, this isn't a perfect world - you can't change the request or use step-through debugging to walk through the problem one line at a time.

So, how will you figure out what caused it this time, when you can't just "do that again" and have it happen again?

In short, logging out stack traces and debug info, identifying the most critical question you need to answer. The most obvious is, at a minimum, what EXACTLY is being sent to the printer? Is the printer being told once to print an item, or is it being told over and over and over, or is it being told once but with a quantity specified?

One "simplest possible thing that might work" is to log exactly every single print request, with the expectation that this might be a big log (deal with that in planning). At the very last step before a request is sent to the printer, log exactly what is being sent. Depending in your implementation this might mean wrapping a streamwriter in a buffer, etc - whatever you need to do).

This log should allow you to "reprint" any label using only the information right there in the log, so you can just "resubmit" the request and have it work EXACTLY like if the request was created by the system in the normal fashion.

Then, the human component - you need someone to make a note of when the bug happens, as close to exactly in time as when it happens (so you can go hunting for a rough timestamp), and even attach one of the duplicate labels to your little "bug report" piece of paper with the time on it. If they could make a note of what came before and what came after, that would be even better - but this is obviously subject to business workflows that may be out of your hand. The suspect label and a rough timestamp would be hugely helpful though.

Then you search your potentially huge log files for that event, and you study it. You resubmit that label and see if it happens with that exact, perfect copy of the request - or if it works normally. You look closely at each byte of the request and make sure it is perfectly according to spec and just like any other proper request.

You make sure that this really is exactly what is being sent to the printer, even if you have to have someone setup a networking appliance as an invisible repeater that logs network traffic so you can make sure it really works like you think it works.

In short, you narrow it down - is it the request to the printer that's bad, or not?

Regardless, you'll learn something from this. Either you learn it's software screwing up the request, or it's "something else". This is defining and limiting the scope of the problem.

Then what? Well, you can always try a different printer, a new computer generating the requests, new network/cable to the printer, etc. Diagnosis by part-swapping is a time-honored tradition in all mechanical fields :)

If all else fails, list this duplicate result as a "rare occurrence, approximately once per month", unable to reliably replicate, known work-around "throw extra labels away", etc...declare it's caused by ghosts, with relevant XKCD:

...and accept that some things are not meant for man to know. Some day the problem will go away, no one will know why, and you get a good story to share with senior programmers some day when "unexplained programming mysteries" becomes the lunch topic.

Title: What is your approach to debugging a transient problem

Body:
I am working on a system that controls a label printer and occasionally the label printer will print the same label 2,3,...,10 times.  The error is transient, occurring once per 3000-4000 (once a month) or so labels.

I am adding tracing to a log file to see what commands are being sent to the printer and state of the printer (we have a state machine defining allowable commands). But since this is a transient problem that may tell me that we had an error, but not why we had the error so I can fix the problem.

I am also trying to get the print job files for the errant jobs so that I can emulate the job in my test environment.  Hopefully with these I can reliably make the error occur

I am curious how other might approach diagnosing this type of transient error.


答: I'd say the internet is a pretty good candidate. StackOverflow and Google are a modern-day developer's most powerful tools. Instant knowledge-sharing on a global basis! These days you don't need to know the answers, you only need to know how to get to know the answers - and that is a perfectly adequate subsitute that allows developers to spend less time reading and more time coding, and thus being productive. It means that every programmer in the world has access to all of the answers, and all they need to do is know how to ask the right questions.


答:
I'd also go for option 2. Speaking of testability concern, this obviously cannot be really unit tested, but, matter of fact, it shouldn't be. Such tests are integration tests, and should be executed in a real (albeit controlled) environment, ideally as a part of continuous integration (CI), where you would test every change in DB schema and every change of application's source code against each other.

Why would I prefer option 2? Well, because one should use right tools to do the work. Keeping up the data integrity is one of the key points of RDBMS, and not using it is just like not picking up the money laying on the ground.


答:
For the sake of argument, I disagree with the assertion of Fred Brooks.

There is an improvement in technology which allowed alone an order-of-magnitude improvement in productivity: internet, and more precisely the progressive availability of internet connection in every home in the last two decades.

Being able to find instantly an answer to nearly every problem you can encounter as a developer enables a huge boost in productivity. Don't know how to select nth element in JQuery? Google search leads to an answer of the exact question on Stack Overflow. Don't know how to use overflow in CSS? Mozilla's MDN is here. Forgot what virtual keyword means in C#? Google helps, again.

When I started programming, I didn't have internet. I had books, as well as some digital-format documentation stored locally, but searching through books and documentation was a slow process, and no matter how much books I had, there were many issues that weren't mentioned there.

More importantly, nearly every problem you encounter was already encountered by somebody else before. Internet helps finding people who had this error and successfully solved it. This is not a sort of information you find in books or official documentation like MSDN. Instead, such information is usually found:

On Stack Overflow, obviously. For example, I haven't seen any book which mentioned this problem.

In blogs. I found a lot of help on blogs when I had particular problems, would it be with WCF configuration or a proxy server which doesn't start or a cryptic bug when using a specific API on a machine with culture different from en-US.

In bug reports concerning open source software. For example, it was very helpful recently when I attempted to use Ubuntu's MAAS and when I used PXE. You don't find this sort of information in books either.

The importance of the immediate availability of an answer to most issues one can encounter is especially noticeable if we take in account that most of the time a team spends on a project is spent maintaining it.

Internet doesn't help much during architecture and design phases (Programmers.SE might help, but it would be much more valuable for an architect to read books about architecture and design, to learn design patterns, etc.).

It starts to be helpful during testing and implementation steps, when actual issues arise.

But most issues appearing during the maintenance, it's there when help from others through internet appears crucial. Basic example: a WCF service works perfectly well on my machine, but fails with no exception whatsoever when deployed in production, while it worked for the last two weeks. This happened to me, and I'm thankful to blog writers and Stack Overflow community for helping me solving the issue in a matter of hours rather than weeks.

Would it justify a x10 increase in productivity? Difficult to tell.

On one hand, there are cases where you find an answer immediately, while you could have spent days searching for it alone (or be forced to rewrite a large part of the application).

On the other hand, the overall productivity improved based on multiple factors, such as better project management (Agile, TDD etc. comes to mind), better team management (Radical Management by Stephen Denning comes to mind), better tools (debuggers, profilers, IDEs, etc.), better hardware (no, I won't be very productive if forced to write in Assembler for a slow CPU and very limited RAM), etc.


问:
Is this looping solution possible with recursion?
Eventually, I would like to generalize this solutions to work with a Tuple of any length. I think recursion is required for that, but I haven't been able to do it.

```<code>def combineRanges(maxValues) :
    for x in range(0, maxValues[0]) :
        for y in range(0, maxValues[1]) :
            for z in range(0, maxValues[2]) :
                print (str(x) + '-' + str(y) + '-' + str(z));

m = (6,9,20);
combineRanges(m);
</code>```

http://repl.it/WiZ

答:
There is no need for recursion here; you can use itertools.product, map and tuple unpacking:

```<code>from itertools import product

def combine_ranges(max_values):
    for t in product(*map(range, max_values)):
        print("-".join(map(str, t)))
</code>```

A shorter example:

```<code>>>> combine_ranges((2, 2, 2, 2))
0-0-0-0
0-0-0-1
0-0-1-0
0-0-1-1
0-1-0-0
0-1-0-1
0-1-1-0
0-1-1-1
1-0-0-0
1-0-0-1
1-0-1-0
1-0-1-1
1-1-0-0
1-1-0-1
1-1-1-0
1-1-1-1
</code>```

Note use of PEP-8-compliant names.

Body:
There is no need for recursion here; you can use itertools.product, map and tuple unpacking:

from itertools import product

def combine_ranges(max_values):
    for t in product(*map(range, max_values)):
        print("-".join(map(str, t)))

A shorter example:

>>> combine_ranges((2, 2, 2, 2))
0-0-0-0
0-0-0-1
0-0-1-0
0-0-1-1
0-1-0-0
0-1-0-1
0-1-1-0
0-1-1-1
1-0-0-0
1-0-0-1
1-0-1-0
1-0-1-1
1-1-0-0
1-1-0-1
1-1-1-0
1-1-1-1

Note use of PEP-8-compliant names.


问:
Why is cleverness considered harmful in programming by some people?
I've noticed a lot of questions lately relating to different abstraction techniques, and answers saying basically that the techniques in question are "too clever." I would think that part of our jobs as programmers is to determine the best solutions to the problems we are given to solve, and cleverness is helpful in doing that.

So my question is: are the people who think certain abstraction techniques are too clever opposed to cleverness per se, or is there some other reason for the objection?

EDIT: This parser combinator is an example of what I would consider to be clever code. I downloaded this and looked it over for about half an hour. Then I stepped through the macro expansion on paper and saw the light. Now that I understand it, it seems much more elegant than the Haskell parser combinator.

答:
Because often people don't know a language, idioms and patterns. They could take a book and learn it, but they don't. And because of those people you should write simple code.

It's not a cleverness. It's a knowledge.


答:
You seem to have a misconception that is leading to your confusion.

However, protected variable is just a global variable (although limited) and should be avoided at all.

A protected field in a class limits its visibility to the class, other classes in the same package, and subclasses.  This is not a global variable.

To have a 'global' variable in Java one would have something that looks roughly like:

```<code>class Foo {
    public static int bar;
}
</code>```

The key points there is that there is only one Foo.bar in the code and it is accessible to everything.  Changing this to protected static int bar will reduce its visibility, but it is still something that is a global shared state.

On the other hand a class that looks like:

```<code>class Foo {
    protected int bar;
}
</code>```

This has a protected field that is only accessible to some things (as mentioned above) and is an instance variable - that there is one instance per object.  It is not a global variable.  There are good reasons to have code that looks like this (you will often find it in abstract classes).

Especially if you've all the essential getter/setter declared, you should have zero protected variable and it should not affect anything else.

Getters and setters are different than accessibility.  The bean model for Java often dictates their existence.  Typically, one has private properties rather than protected in this case.

Though I will point out again that a protected field is one of where it is visible.

In the context of testing...

One approach to making specific methods return a specific value is to extend the class and rewrite that method.  This is only possible if the method is not final or private.

So, yes, you could indeed use that approach.

However, there is awkwardness here in that you need to make sure that your rewritten class is in test packages only to make sure it doesn't slip into your release code.

The other thing is that when you are doing this, you are very tempted to write additional logic in there that substantially differs from the extended class.  This introduces the danger that you won't actually be testing what you think you are testing.

Both of these issues can be addressed by instead mocking the class in the test code and specifically returning the desired values.  It makes it very clear what you are sticking in where with no additional logic and that the code doesn't get into production (mocked classes only exist at runtime in tests).

There are still gotchas to watch out for in here, because it still hasn't removed the logic (just ripped it out) and you still need to make sure that part works correctly.

Ahh, but "you can't mock or unit test private methods easily" people will point out.  "If you can t do it easily, people will either not do it, or do it wrong." And that is true.

But there is another option.  The default level protection within Java.  See Controlling Access to Members of a Class

```<code>package com.se.prog.demo;

class Foo {
    public int bar() { return 42; }
    protected int baz() { return 4; }
    int qux() { return 7; }
    private int xyzzy() { return 13; }
}
</code>```

The methods bar, baz and qux can all be mocked easily.  qux can only be accessed by other classes in the same package as com.se.prog.demo -- and you control that list.

With the maven directory structure, one would have

```<code>src/main/java/com/se/prog/demo/Foo.java
   ^^^^^^
</code>```

but there's also

```<code>src/test/java/com/se/prog/demo/TestFoo.java
   ^^^^^^
</code>```

which can access qux() in Foo allowing for unit testing of the gutsier parts of Foo, without exposing it to the world (and Foo's subclasses).

In some circles, this is seen as a better approach for methods that are the innards of the class but still need to be tested or mocked than protected or private.

Body:
You seem to have a misconception that is leading to your confusion.

However, protected variable is just a global variable (although limited) and should be avoided at all.

A protected field in a class limits its visibility to the class, other classes in the same package, and subclasses.  This is not a global variable.

To have a 'global' variable in Java one would have something that looks roughly like:

class Foo {
    public static int bar;
}

The key points there is that there is only one Foo.bar in the code and it is accessible to everything.  Changing this to protected static int bar will reduce its visibility, but it is still something that is a global shared state.

On the other hand a class that looks like:

class Foo {
    protected int bar;
}

This has a protected field that is only accessible to some things (as mentioned above) and is an instance variable - that there is one instance per object.  It is not a global variable.  There are good reasons to have code that looks like this (you will often find it in abstract classes).

Especially if you've all the essential getter/setter declared, you should have zero protected variable and it should not affect anything else.

Getters and setters are different than accessibility.  The bean model for Java often dictates their existence.  Typically, one has private properties rather than protected in this case.

Though I will point out again that a protected field is one of where it is visible.

In the context of testing...

One approach to making specific methods return a specific value is to extend the class and rewrite that method.  This is only possible if the method is not final or private.

So, yes, you could indeed use that approach.

However, there is awkwardness here in that you need to make sure that your rewritten class is in test packages only to make sure it doesn't slip into your release code.

The other thing is that when you are doing this, you are very tempted to write additional logic in there that substantially differs from the extended class.  This introduces the danger that you won't actually be testing what you think you are testing.

Both of these issues can be addressed by instead mocking the class in the test code and specifically returning the desired values.  It makes it very clear what you are sticking in where with no additional logic and that the code doesn't get into production (mocked classes only exist at runtime in tests).

There are still gotchas to watch out for in here, because it still hasn't removed the logic (just ripped it out) and you still need to make sure that part works correctly.

Ahh, but "you can't mock or unit test private methods easily" people will point out.  "If you can t do it easily, people will either not do it, or do it wrong." And that is true.

But there is another option.  The default level protection within Java.  See Controlling Access to Members of a Class

package com.se.prog.demo;

class Foo {
    public int bar() { return 42; }
    protected int baz() { return 4; }
    int qux() { return 7; }
    private int xyzzy() { return 13; }
}

The methods bar, baz and qux can all be mocked easily.  qux can only be accessed by other classes in the same package as com.se.prog.demo -- and you control that list.

With the maven directory structure, one would have

src/main/java/com/se/prog/demo/Foo.java
   ^^^^^^

but there's also

src/test/java/com/se/prog/demo/TestFoo.java
   ^^^^^^

which can access qux() in Foo allowing for unit testing of the gutsier parts of Foo, without exposing it to the world (and Foo's subclasses).

In some circles, this is seen as a better approach for methods that are the innards of the class but still need to be tested or mocked than protected or private.


答:
Assuming you've already checked the default logs for exceptions ...

Explain the problem to your debug duck.

When it stares at you blankly, go into more detail. Tell the duck how it's supposed to work. If the stupid duck is still staring at you blankly, go into more detail about how it's supposed to work.

As you explain each component and interaction, ask the duck whether it can think of any reason (bad input, bad assumptions, protocol/communication mismatch, or errors therein) that could manifest the problem. If neither you or the duck can think of a reason the component could be misbehaving, move on (for now).

But at each step during your explanation, if you have even a faint suspicion of misbehavior, add some tests to your suite.

And when your coworkers ask who you're talking to, simply tell them, "Oh, I wasn't talking. The duck was."