问:
Is licensing an issue for Git Submodules?
For example: an MIT-licensed project wishes to simplify the build process for its users by including part of the GNU toolchain (which is of course GPL) as a Submodule.

Could that be interpreted as making the MIT project a "derived work" of the GNU toolchain?

Does including a project as a Git Submodule (or some similar mechanism - maybe even just a Bash script that downloads it) count as "redistributing" that project?

What about if the Makefile also built the sub-project?

In my opinion (and probably most peoples') the answer to those questions is a clear "no". I'm more interested in hearing the precedents and norms surrounding this issue than discussing our own interpretations of copyright law. Do you know of any open-source projects that have Submodules under incompatible licenses? Do you know of anyone who has deliberately avoided them, and why they did that?

Just to clarify for non-Git-people: Submodules don't cause the actual content to be included, they're like "pointers" to a different repo. In the example I gave, when you cloned the MIT project and ran git submodule init, Git would automatically download the GNU code from a GNU repo.

答:
I doubt you're going to find significant legal materials on git-submodules per se. One angle would be to make an analogy to another system with a bigger community / longer history / more precedents. To that end, let's define "git submodules" -- it is, in fact, two things:

A file-format (".gitmodules") which lists the names/addresses of other software packages. (For brevity, let's call these files "manifest.")

A tool which reads the file format and downloads software.

Now, let's pick another project which meets these criteria -- for example, Debian's package manager provides both:

A file-format which lists the names/addresses of other software packages. (For brevity, let's call these files "manifests".)

A tool which reads the file format and downloads software.

(For performance reasons, the file-format for Debian's package manager is much more complex/nuanced. This is because a typical Debian deployment works with thousands of packages distributed across thousands of servers -- whereas a typical git-submodules deployment works with maybe a dozen packages and a handful of servers. If you review the early history of dpkg/apt, you'll find that performance was a central design issue and a significant achievement. However, on a general "informational" level, the formats are similar.)

The Debian project is a particularly useful example because -- within the FOSS community -- debian-legal has a particularly long history / strong reputation for evaluating licensing issues that arise from mixing software.

So: if we look at the Debian project, do we find they publish manifests which reference packages that have incompatible licenses? Yes. For example, PHP4 was licensed under the "PHP License" which the FSF judges to be incompatible with GPL (https://www.gnu.org/licenses/license-list.html). Here is the manifest:

http://archive.debian.org/debian/pool/main/p/php4/php4_4.0.3pl1-0potato3.dsc

Observe that this has "Build-depends" on "bison". Bison is a tool licensed by FSF under GPL.

If a user types "apt-build install php4", then the consequence will be that he downloads "php4" and "bison", loading both on the same computer/filesystem. Have we violated the GPL? No. Why not? Any of these points should be individually persuasive:

The combination of packages has been approved through Debian's process.

We have only downloaded software -- we haven't modified or redistributed the software. Under GPL, that's a significant distinction.

The GPL specifically distinguishes modification/derivation from mere aggregation and disclaims virality under mere aggregation.

The manifest shows bison as a "Build-depends" and not a "Depends" -- to wit, bison is a build-tool that helps to process php4 (like a text-editor does); php4 does not include (or even link) the bison code; at runtime, you can execute php4 without executing bison.

If you swapped Debian's package manager with git-submodules here, would it suddenly change from "OK" to "violation"? No, that's silly -- it's the same mix of codes with the same mix of licenses and the same compilation steps. All that's changed is the download tool.

(Note: None of this is to say that your specific usage of git-submodules with its specific dependencies is acceptable or unacceptable. In our example from Debian, several key factors were specific to the particular licenses and packages -- and so it goes with anything.)


问:
is it a bad programming practice to return a new object in a property
I've always had this question and I think it is time to settle the answer, is it a heavy load to do this :

```<code>    public Rect Bounds
    {
        get
        {
            var Top = (_targetPosition + _translationY) - _size / 2.0f;
            return new Rect(callOutStarMargin, Top, _size, _size);
        }
    }
</code>```

as an alternative of this :

```<code>    private Rect _boundsRect = new Rect();

    public Rect Bounds
    {
        get
        {
            var Top = (_targetPosition + _translationY) - _size / 2.0f;
            _boundsRect.Y = Top;
            return _boundsRect;
        }
    }
</code>```

doing this I'm avoiding to create a new Rect everytime the property is invoked, I'm not sure if doing this in the first way would create an extra memory/speed overweight, if lets say this property is accessed 1 or 2 times on a list of 100 items each second.

So which one should I choose ?

EDIT:

I think I'm not being clear here, my real question is How heavy is it making this new instructions of simple objects like Rect, How efficient is C# garbage collector for example in this scenario of 100 new rects per second , and the cpu cycle difference between one or the other way

答:
Properties are syntactic sugar for approximating fields. They allow us to implement constraints or logic in our class while preserving the abstraction barrier of a POCO. The typical property in .NET does not change each time we "observe it". The semantics of auto-properties are clearly defined to use a compiler generated backing field, so properties should stick to those semantics rather than redefine them.

Creating a new instance each getter call is a mega-bug waiting to happen.

It is also needless expense to accomplish nothing but a smaller object. new() is certainly more expensive than a simple test and branch, especially if its a new reference type on the heap, so it isn't an optimization anyway.

The misleading aspect of the example is it makes this approach look not so dangerous, because of talk of "immutability", and the fact that we don't usually modify Bounds properties, or other struct fields.

There are three big problems with this approach:

```<code>public Something Something
{
    get
    {
        return new Something();   // bad - bug waiting to happen
    }
}
</code>```

It is inconsistent with real fields or typical .NET properties

It is a liability, needlessly allocating objects. A simple lambda across a collection of rectangles would generate pathological GC churn for large collections.

It is misleading and impossible to reason about without looking at the implementation. One should never have to look into the implementation of an intrinsic such as a field. Nobody will expect that we've redefined the semantics of a lazy property.

One would not expect the following to be an infinite loop:

```<code>while(obj.Something.X < max)
    obj.Something.X++;
</code>```

Regarding performance and GC, the point isn't whether the GC is efficient. The point is the GC shouldn't even be in this conversation. Imagine your code was reused in the Bing search engine. (Not a fan of Bing, just referring to a high performance .NET system)

If there is a need for this, its best to use a method that clearly signifies that is what you are doing.

ContentLicense: CC BY-SA 3.0

Body:
Properties are syntactic sugar for approximating fields. They allow us to implement constraints or logic in our class while preserving the abstraction barrier of a POCO. The typical property in .NET does not change each time we "observe it". The semantics of auto-properties are clearly defined to use a compiler generated backing field, so properties should stick to those semantics rather than redefine them.

Creating a new instance each getter call is a mega-bug waiting to happen.

It is also needless expense to accomplish nothing but a smaller object. new() is certainly more expensive than a simple test and branch, especially if its a new reference type on the heap, so it isn't an optimization anyway.

The misleading aspect of the example is it makes this approach look not so dangerous, because of talk of "immutability", and the fact that we don't usually modify Bounds properties, or other struct fields.

There are three big problems with this approach:

public Something Something
{
    get
    {
        return new Something();   // bad - bug waiting to happen
    }
}

It is inconsistent with real fields or typical .NET properties

It is a liability, needlessly allocating objects. A simple lambda across a collection of rectangles would generate pathological GC churn for large collections.

It is misleading and impossible to reason about without looking at the implementation. One should never have to look into the implementation of an intrinsic such as a field. Nobody will expect that we've redefined the semantics of a lazy property.

One would not expect the following to be an infinite loop:

while(obj.Something.X < max)
    obj.Something.X++;

Regarding performance and GC, the point isn't whether the GC is efficient. The point is the GC shouldn't even be in this conversation. Imagine your code was reused in the Bing search engine. (Not a fan of Bing, just referring to a high performance .NET system)

If there is a need for this, its best to use a method that clearly signifies that is what you are doing.


问:
What's the most absurd myth about programming issues?
To put it another way... What is the most commonly held and frustrating misunderstanding about programming, you have encountered?

Which widespread and longstanding myths/misconceptions do you find hard for programmers to dispel/correct.

Please, explain why this is a myth.

答: That any M.Sc. with one programming course is enough to be hired as a software developer.</p>

ContentLicense: CC BY-SA 2.5


问:
If someone offers you an unverified statement regarding software development practices, do you respond with "citation needed"?
Recently I attended a lecture given by Greg Wilson (Chief Scientist of Software Carpentry). From the abstract:

The idea that claims about software development practices should be based on evidence is still foreign to software developers,
  but this is finally starting to change: any academic who claims that a particular tool or practice makes software development
  faster, cheaper, or more reliable is now expected to back up that claim with some sort of empirical study.

Overall, the lecture was very informative and left me thinking quite deeply about my approach to development. In particular, I now find myself looking for citations to back up a lot of statements. Previously, I had slipped into the habit of simply repeating offered truths, with perhaps a mental note to go check up on it later.

Putting it bluntly, I was being gullible.

Here's an example taken from the lecture:

"If more than 25% of the code needs refactoring, it's quicker to rewrite it".

Sounds plausible, but is it true? Where's the study backing this up? Is it true for all languages? And so on.

OK, it's quite possible to take this to an extreme and not believe anything by anyone unless you have derived it yourself from
first principles. That way lies madness (or maybe mathematics ;-) ). But, if someone comes up to you with a statement along the
lines of "Hey, by doing this in [pick language of moment] we'll be able to boost productivity by [pick multiple of 10]%" are you
inclined to just accept it, or are you going to ask for proven evidence?

If it's the latter (and I hope it is) then

where would you go to find this evidence?

how stringent would you be?

In short, if someone offers you an unverified statement, will you respond with "citation needed"?

答:
It depends. :)  When someone's statement contradicts repeated, reflected-on, and personally verified experience, then yes, I'd want to see some sort of reference of a study.  On the other hand, if someone echoes an idea you've seen and lived many times, there's not much reaction provoked (doesn't mean that the idea is infallible though).

As an example, the book "Code Complete" cites scores of studies in each chapter to make its points, sometimes about seemingly small matters (like indentation and spacing, or variable name length).  I recall some (younger) developers whom I introduced the book to thinking that that level of detail and evidence was silly. But a few months later with more production coding experience and after a few code reviews, some of those same developers had the honesty to admit that yes, even the number of spaces in indentation does matter.  Good comments matter.  Encapsulation matters.  etc. etc.

On the other end, when a vendor claims some new IDE is 50% more productive, my first reaction is bull$%^&!


问:
Designs for outputting to a spreadsheet
I'm working on a project where we are tasked to gather and output various data to a spreadsheet.

We are having tons of problems with the file that holds the code to write the spreadsheet. The cell that the data belongs to is hardcoded, so anytime you need to add anything to the middle of the spreadsheet, you have to increment the location for all the fields after that in the code. There are random blank rows, to add padding between sections, and subsections within the sections, so there's no real pattern that we can replicate. Essentially, anytime we have to add or change anything to the spreadsheet it requires a many long and tedious hours.

The code is all in this one large file, hacked together overtime in Perl. I've come up with a few OO solutions, but I'm not too familiar with OO programming in Perl and all my attempts at it haven't been great, so I've shied away from it so far. I've suggested we handle this section of the program with a more OO friendly language, but we can't apparently. I've also suggested that we scrap the entire spreadsheet idea, and just move to a webpage, but we can't do that either.

We've been working on this project for a few months, and every time we have to change that file, we all dread it. I'm thinking it's time to start some refactoring. However, I don't even know what could make this file easier to work with. The way the output is formatted makes it so that it has to be somewhat hardcoded.

I'm wondering if anyone has insight on any design patterns or techniques they have used to tackle a similar problem. I'm open to any ideas. Perl specific answers are welcome, but I am also interested in language-agnostic solutions.

答:
Spreadsheet::WriteExcel writes XLS files, which I use. Spreadsheet::WriteXLSX writes XLSX files. Spreadsheet::WriteExcel is not being developed anymore but is still a real solid tool. (Some of our customers are way behind the tech curve so XLS files seemed like a good common denominator. And I don't need a Ferrari to get the groceries, nor do we have money for a Ferrari.)

In both of these you can specify which cell to write. In my case, all the spreadsheets I output are output in sequence, line by line, by not addressing cells. My spreadsheets are summary reports for executives and are divided into sections, and there is a variable number of rows with each section.

The nice thing about both Perl modules is you can add formatting to individual cells when you write each cell.

Now, this might be a management problem too. If it takes you 200 hours to keep Perl and do the same thing, and 30 hours for you to use a tool you already know to give the same output, and you told management this, that is a management problem. The manager doesn't understand how efficiency, and saving money, works. In my case, Perl can do a LOT of things our other tools cannot.

IMO, learning Perl OO is worth it as you can use LOTS of handy modules, most modules are written this way.

My answer: rewrite sections of the existing code to use Spreadsheet::WriteExcel. Test the subroutine thoroughly, then put it into production. Then repeat with a new section of code/subroutine. When you get a tool that works, it's really worth it to stick with it. Perl is one of those tools.

Title: Designs for outputting to a spreadsheet


问:
How much should I break up my unit tests?
I recently learned about unit tests and TDD and have started going back to add unit tests to my code (and thank god, it's proven that some things I've written are much more broken than I thought).

That being said, I'm having a hard time figuring out how much (or little) should go into each test.  For example, consider a class I wrote with several methods.  Right now I do something like

```<code>class test_myclass(unittest.TestCase):

    def setUp(self):
        ...
    def test_func1(self):
        ...
    def test_func2(self):
        ...
    etc
    def tearDown(self):
        ...
</code>```

But what if each (or at least some) method can fail in a lot of different ways, or has a lot of edge cases?  Then I'm stuck doing something like

```<code>def test_func1(self):
    self.assertEqual(func1(working_input), expected_value)
    self.assertRaises(Some_error, func1, args)
    self.assertRaises(Some_other_error, func1, args)
    etc
</code>```

And it could get pretty hairy, and rather unclear where things are failing.  So I thought I might break it up more, and devote a class to each function (that has enough test cases to be worth it) but that seems like a lot of work, and more time than I should probably be spending on testing.

I also considered writing multiple tests for a given method, such as

```<code>def test_func1(self):
    self.assertEqual(func1(working_input), expected_val)
def test_func1_bad_input_1(self):
    self.assertRaises(error, func1, bad_args)
def test_func1_bad_input_2(self):
    self.assertRaises(error2, func1, bad_args_2)
...
</code>```

And this seems cleaner than a unit testing class per method, but still seems like it might be to little in any given test.

How much testing is too much?

While my example was given in python, I'm open to language agnostic answers

答:
You struggle since you are not doing TDD - you are currently adding unit tests afterwards instead of before. Using TDD "by the book" means: work with extremely short cycles of

think of the next requirement/change you want to implement

write a test for exactly that requirement

run the test (gives you typically "red")

implement a change for the requirement

run test again (hopefully green, fix the code if not "green")

refactor (code and tests)

When you work this way, the answer to your question "How much testing is too much?" becomes obvious:  you write exactly the amount of tests you need to be sure the next feature or change to your class or function work as intended - no more, no less.

TDD is less a testing, but more an implementation technique. Writing the tests and implementing your code is completely interwoven. When you have implemented a feature or change, you should have already written most of the tests beforehand you need for assuring the feature works as intented - so when the function is "ready", there are no tests to write any more. When you feel you might have missed some edge cases, and you are not sure your function handles them correctly, then add some extra tests afterwards. But that also may be a sign of implementing already too many things at once - better just add a test and the code for the "happy path" first, afterwards add the test and the code dealing with those edge case - in exactly that order.

In a real-world scenario, working always that way may be too idealistic, but once you understand how TDD was meant, the question of "how much to tests to write" is much easier to answer.

Once you figured out how many tests you really have to write, the question how to break them up is much easier to answer: you break them up (in the refactoring step above) until you think the design and structure of the tests you have written so far is good enough. The SOLID principles are a good guideline not only for your production code, but also for your tests as well. So for example, as long as you have just one test for a function, a single test function is fine. When you discover you need more tests for the same function, there may be the point where you need a full class of tests just for a single function.

ContentLicense: CC BY-SA 4.0


问:
Is it a good practice to run unit tests in version control hooks?
From the technical point of view it is possible to add some pre/post push hooks which will run unit tests before allowing some specific commit to be merged to remote default branch.

My question is - is it better to keep unit tests in build pipeline (thus, introducing broken commits to repo) or it's better just not to allow "bad" commits to happen.

I do realize that I'm not limited with this two options. For instance, I can allow all commits to branched and tests before pushing merge commit to repo. But if you have to choose between exactly this two solutions, which one you'll choose and for what exactly reasons?

答:
Having to wait for successfull build and tests on each commit to the main branch is really awful, I think everyone agrees on this.

But there are other ways to achieve a consistent main branch.  Here's one suggestion, a bit similar in the vein of gated check-ins in TFS, but which is generalizable to any version control system with branches, although I'll mostly use git terms:

Have a staging branch to which you can only commit merges between your dev branches and the main branch

Setup a hook that starts or queues a build and tests on commits done on the staging branch, but that does not make the committer wait

On successful build and tests, automatically make the main branch go forward if it's up to date

Note: don't automatically merge into the main branch, because the tested merge, if not a forward merge from the main branch's point of view, might fail when merged into the main branch with commits in between

As a consequence:

Forbid human commits to the main branch, automatically if you can, but also as part of the official process if there's a loophole or if it's not technically feasible to enforce this

At least, you can make sure no one will do it unintentionally, or without malice, once it's a ground rule.  You shouldn't attempt to do it, ever.

You'll have to choose between:

A single staging branch, which will make otherwise successful merges actually fail if a previous yet not build and not tested merge fails

At least, you'll know which merge failed and have someone correct it, but the merges in between are not trivially traceable (by the version control system) for the results of further build and tests.

You can look at file annotation (or blaming), but sometimes a change in a file (e.g. configuration) will generate errors in unexpected places.  However, this is a rather rare event.

Multiple staging branches, which will allow non-conflicting successful merges to get to the main branch

Even in the event that some other staging branch has non-conflicting failing merges.  Traceability is a bit better, least the case that one merger did not expect an affecting change from another merger.  But again, this is rare enough to not worry about every day or every week.

To have non-conflicting merges most of the time, it's important to split staging branches sensibly, e.g. per team, per layer, or per component/project/system/solution (whateven you name it).

If the main branch was meanwhile forwarded to another merge, you have to merge again.  Hopefully, this is not an issue with non-conflicting merges or with very few conflicts.

In comparison to gated check-ins, the advantage here is that you have a guarantee of a working main branch, because the main branch is only allowed to go forward, not to automatically merge your changes with whatever was committed in between.  So the third point is the essential difference.


问:
Code that converts a value into a different representation, then converts it back to where it started is bad, but how?
I was reading an article about bad programming practices.

It mentioned -

"Yo-Yo code" that converts a value into a different representation, then converts it back to where it started (eg: converting a decimal into a string and then back into a decimal, or padding a string and then trimming it)

I don't understand why the particular example he gives is a bad way to write programs.  It seems okay to me to convert back if the situation requires it so that the value can be used.

Can anyone explain more about this?

答:
Why? Because even the best of us can make mistakes.

Look at what happened when Microsoft tried to implement a "round-trip" format specifically for making sure float <-> string conversions were safe: https://stackoverflow.com/q/24299692/541686


答:
When I was at school (and post school in electrical engineering) we were taught to divide after multiplying. Division often many digits and get rounded. Multiplying after division multiplies the division error.

Type conversions are the same, you risk losing data. CInt(1.3) = 1.

In my language, Basic, we only do type conversions (a VB6 program spends 90% of it's time doing ANSI/Unicode conversion, for all API calls the runtime makes).

Type conversion is implied in all we do.

```<code> Print 5
</code>```

The string "5" gets printed from the numeric literal.

```<code>form1.caption = "My Form"
</code>```

The unicode string literal gets converted into a ANSI string and sent to SetWindowsTextA by the forms package.

Even this works in basic

```<code>a = "5"
b = 3

c = a + b (= 8)
</code>```

I'm a variant programmer these days - I don't even think about type. I just rely on the autoconversions.

Anyway my 3 pet peeves are

Assigning string literals to variables to use them (wastes memory and slow)

Pointless functions when code could be inline (and the compiler will probably undo your function and inline it anyway)

Setting all objects to nothing as the last lines before an End Function or end of program.

and a 4th for short programs

Pointlessly dimming your 3 variables in a 5 line program.

Title: Code that converts a value into a different representation, then converts it back to where it started is bad, but how?

Body:
I was reading an article about bad programming practices.

It mentioned -

"Yo-Yo code" that converts a value into a different representation, then converts it back to where it started (eg: converting a decimal into a string and then back into a decimal, or padding a string and then trimming it)

I don't understand why the particular example he gives is a bad way to write programs.  It seems okay to me to convert back if the situation requires it so that the value can be used.

Can anyone explain more about this?

Body:
When I was at school (and post school in electrical engineering) we were taught to divide after multiplying. Division often many digits and get rounded. Multiplying after division multiplies the division error.

Type conversions are the same, you risk losing data. CInt(1.3) = 1.

In my language, Basic, we only do type conversions (a VB6 program spends 90% of it's time doing ANSI/Unicode conversion, for all API calls the runtime makes).

Type conversion is implied in all we do.

 Print 5

The string "5" gets printed from the numeric literal.

form1.caption = "My Form"

The unicode string literal gets converted into a ANSI string and sent to SetWindowsTextA by the forms package.

Even this works in basic

a = "5"
b = 3

c = a + b (= 8)

I'm a variant programmer these days - I don't even think about type. I just rely on the autoconversions.

Anyway my 3 pet peeves are

Assigning string literals to variables to use them (wastes memory and slow)

Pointless functions when code could be inline (and the compiler will probably undo your function and inline it anyway)

Setting all objects to nothing as the last lines before an End Function or end of program.

and a 4th for short programs

Pointlessly dimming your 3 variables in a 5 line program.


答:
The problem with these sort of statements is that even if you had empirical evidence supporting the claim it would be very difficult to determine if the study that lead to the evidence applied to your current situation.

Almost everything in the profession has a caveat, or several so every improvement in one place has the likelihood of being a disservice someplace else.

The folks down in the trenches know the difference through experience and generally do not have the funding/time/resources to try to prove it through a scientific study.

The folks that try to prove it through a scientific study obviously have resources to dedicate to such studies and are therefore highly likely to be selling you something so I would say that you should be even more stringently applying your own personal experience to anything that claims to be backed up by empirical research.

If someone told you "If more than 2% of the code needs refactoring, it's quicker to rewrite it" you would know that to be false as much as if someone told you "Only if more than 98% of the code needs refactoring, it's quicker to rewrite it".  Where the actual number is depends on what you are doing and how far from ideal the current code is.

The idea that after a certain point it is easier to do a nuclear refactor is obviously true in many cases, but the actual percentage is more of an example that you need to consider through the lens of your (team's) own experience and current situation.

Title: If someone offers you an unverified statement regarding software development practices, do you respond with "citation needed"?

Body:
Recently I attended a lecture given by Greg Wilson (Chief Scientist of Software Carpentry). From the abstract:

The idea that claims about software development practices should be based on evidence is still foreign to software developers,
  but this is finally starting to change: any academic who claims that a particular tool or practice makes software development
  faster, cheaper, or more reliable is now expected to back up that claim with some sort of empirical study.

Overall, the lecture was very informative and left me thinking quite deeply about my approach to development. In particular, I now find myself looking for citations to back up a lot of statements. Previously, I had slipped into the habit of simply repeating offered truths, with perhaps a mental note to go check up on it later.

Putting it bluntly, I was being gullible.

Here's an example taken from the lecture:

"If more than 25% of the code needs refactoring, it's quicker to rewrite it".

Sounds plausible, but is it true? Where's the study backing this up? Is it true for all languages? And so on.

OK, it's quite possible to take this to an extreme and not believe anything by anyone unless you have derived it yourself from
first principles. That way lies madness (or maybe mathematics ;-) ). But, if someone comes up to you with a statement along the
lines of "Hey, by doing this in [pick language of moment] we'll be able to boost productivity by [pick multiple of 10]%" are you
inclined to just accept it, or are you going to ask for proven evidence?

If it's the latter (and I hope it is) then

where would you go to find this evidence?

how stringent would you be?

In short, if someone offers you an unverified statement, will you respond with "citation needed"?


问:
Database Normalisation - stuck with "categories"
Context:

I'm working on a database of museums and Categories will be one way the user will be able to search. e.g click on "Music" and a list of music related museums turn up. Pretty simple.

Problem:

I'm stuck and confused on the Normalisation process of the "Categories" table...

categories_PK IF is the entry point to the ID rows table,  how do I reference the actual categories themselves? Such as "Architecture" and "Artists Architects and Writers Homes" in one query? I'm confused — I don't see how the values will be referenced?

As one museum may fall into more than one category, the only other idea I had was to ad a Foreign Key to every category such as Archaeology_PK?
Here's what I've got....

答:
This is where you use a bridge table, a table that exists to link your two tables.

So, you have a table MuseumCategory which has at least two columns, Museuem and Category.  Both being foreign keys to their respective tables.

You might want to have other columns, such active or start and end dates, depending upon your system you might want auditing information (created by, date created, approvedby, whatever).


答:
You need two tables:

```<code>Categories
MuseumCategories
</code>```

They will look like this:

```<code>Categories
    CategoryID   (Primary Key)
    CategoryName

MuseumCategories
    MuseumID     (Foreign Key)
    CategoryID   (Foreign Key)
</code>```

The MuseumCategories table connects each specified category to a museum.  Each record in the MuseumCategories table connects one category to one museum.  You can have many such records for the same museum.

Title: Database Normalisation - stuck with "categories"

Body:
Context:

I'm working on a database of museums and Categories will be one way the user will be able to search. e.g click on "Music" and a list of music related museums turn up. Pretty simple.

Problem:

I'm stuck and confused on the Normalisation process of the "Categories" table...

categories_PK IF is the entry point to the ID rows table,  how do I reference the actual categories themselves? Such as "Architecture" and "Artists Architects and Writers Homes" in one query? I'm confused — I don't see how the values will be referenced?

As one museum may fall into more than one category, the only other idea I had was to ad a Foreign Key to every category such as Archaeology_PK?
Here's what I've got....

Body:
You need two tables:

Categories
MuseumCategories

They will look like this:

Categories
    CategoryID   (Primary Key)
    CategoryName

MuseumCategories
    MuseumID     (Foreign Key)
    CategoryID   (Foreign Key)

The MuseumCategories table connects each specified category to a museum.  Each record in the MuseumCategories table connects one category to one museum.  You can have many such records for the same museum.


问:
Eliminating choices of programming languages?
There are a number of programming languages available today. My question is given a specific job what is the criteria one should use to eliminate other programming language choices which serve the same purpose? What does one need to know about the language to say confidently that we are not going to use this language for this job?
I am specifically looking for languages which are used to write back-end of web applications? How did you decide that you wanted to write back-end in language x and not in y,z or w?

EDIT

Ok. Here is more specific version.

1) My question is only concerned about languages which are used to write web-applications.

2) Given 3-4 top choices, could you describe what features of language one must study in order to know if the language is suitable for the task. I just want a list of criteria which must be considered before making a choice of language?

答:
Usually you cannot choose a language per job. There just isn't enough time to learn it, master it, and do the same for its packaging techniques, its ecosystem of available modules and programmers, its debugging quirks, and so on. To say nothing of the economics of investing in tools, learning, and ramp-up time.

So usually language choices are based more on "What do I know?" and "What's the closest thing I know to the job being considered?" Only rarely to we truly encounter zero-based, greenfield, clean sheet of paper decisions. Your pre-existing skills and your baggage must be taken into account.

But occasionally we do get to add another language / environment / approach to our skillset, or modernize/reconsider what tools we're using in a field we already know. Then we have a much less bounded decision. Some common considerations for eliminating possibilities:

Degree of Difference If I were a professional Perl coder (i.e. for a pragmatic, dynamic, usually single-threaded language), I might decide to jump over to Python or Ruby (other dynamic, pragmatic languages). I'm a lot less likely to wholeheartedly embrace languages like OCaml and Haskell that are functional, statically typed, often focus on immutable data structures, require extensive meta/conceptual programming, etc. Some might, as a "stretching out" exercise. But most people and organizations consider "adjacent opportunities," not "change everything all at once" shifts.

Popularity, Community, and Presumed Trajectory People are social creatures. And choosing languages/environments is also an investment. So we tend to want to shift to places that are popular, that have strong and supportive communities, where exciting changes and innovations are going on, and which seem to be "the wave of the future." It doesn't matter how much I love Smalltalk, say; it's not the wave of the future, doesn't have a large community, etc. (Sorry, Smalltalkers! I love it too!) Instead, I'd be much more likely to choose Ruby or another language that is Smalltalk-inspired, rather than Smalltlak per se. You see this "go where vibrant communities are forming" dynamic all over (favoring the likes of CoffeeScript, Go, Rust, Scala...).

Approach and Capabiltiies Despite decades of aiming for this goal, there is no one language that's best at all jobs. Languages that try to span from systems and embedded computing all the way to abstracted business logic tend to end up as jumbly messes. (Looking at you PL/I, Ada, and C++! Apologies to their proponents!) So if you're trying to do embedded or systems work, you would tend to choose a C, D, Go, Rust, or similar language with features, performance, and communities that are particularly suited. You might look to "stretch" normal boundaries. There are good examples, e.g., of Java, JavaScript, and Python used in embedded work, or the likes of Python in servers. But you're not going to program the core of your next operating system in Perl, nor your next highly-threaded server in Python, Ruby, or COBOL. There's a reason that the highest level languages and systems are still, to this day, implemented in C and similar.

So, in summary, there are many, many reasons you can discard language choices, but their logical and cultural distance from your current skills; their relative popularity, community, and trajectory; and their "zone of competence" being different from the jobs on which you want to focus--those are major exclusionary forces.


问:
How to handle a large amount of authenticated clients
I currently have made a multithreaded server in c++ for windows, where each connected client gets a thread to themselves which stays running until the client's socket is closed.

When a client connects, they can run commands (fetch motd, get current version of the client) or can login to get access to other commands (fetch user information, download files, etc).

So far this basic approach has worked well, but I have noticed that there have been an increasing amount of clients using my service and I'm starting to worry about how efficient this approach is.  I've read online that a process in windows can only handle somewhere around 1500-2000 threads, and I think I might soon hit that amount of simultaneous users, and I think it's about time I changed my design around.

What would be the best way to handle this amount of users?

答:
The 2000 thread limit per process is the result of the default maximum stack size for a Windows process. As the linked article states, you can increase the limit by having one dedicated thread per client but is not the most scalable solution.

Instead, use asynchronous I/O completion ports to handle your I/O. Instead of blocking the thread, the code waits on a group of open sockets and is notified when data is received from one of them. Pass the data as a "work item" to a thread pool, a group of threads that Windows manages. Windows even provides BindIoCompletionCallback to automate it for you. This should make much better use of the resources you have, allowing you to scale up further.

The other option is to shard your users into separate groups, each on a different server. If the users do not need to communicate with each other and the data (such as the motd or files you mention above) is stored centrally. You can add a new server every few thousand users.

Similarly, if users need not be tied to specific servers, you can allocate users to servers dynamically in a cluster, meaning a failed server is handled transparently. You do not say where your server is hosted but load balancing is cheap these days, available as part of AWS and Azure.


问:
Explicitly define enum values, even if the default value is the same?
There are times when an enum's values are important: it is not necessary for them to be unique, they also need to have specific values. In such cases, should the values be explicitly defined, even if they coincide with the defaults? An example:

```<code>enum Car {
    DeLorean = 0,
    Lada = 1
};
</code>```

Imagine that for whatever reason your application assumes that DeLorean and Lada have those specific values. Incidentally, they are the same as the default values but does that mean it is no longer necessary to use explicit definitions?

Leaving them implicit makes me uneasy. It seams to me that having them explicitly defined is communicating to future programmers that the specific values are important and it helps prevent mistakes like this:

```<code>enum Car {
    Dacia,
    DeLorean,
    Lada
};
</code>```

In the example above, another programmer who is not aware of the restriction I mentioned introduces a new enum value and, wanting to keep the code tidy, puts it in alphabetical order. However, DeLorean and Lada now have different numerical values and a potentially silent bug has been introduced.

My reasoning seems correct to me (obviously), but the code review team for a company I used to work with didn't agree. Am I wrong?

答:
If you are storing or transmitting those values then they must be specific.
Normally you would define the protocol in a header file shared by both parties. If you have some kind of versioning mechanism then it will not be a big problem. But it is always better to be explicit. I am a fan of 'say what you mean' in the code. It doesn't cost anything and your code is much more readable.

But for internal modules and APIs where the enum values are not stored, the defaults are just fine.
It communicates that the value of the enum itself is not important. What is important is what those value mean and the fact that you are using stonger typing to make it clear what the code is using and help to prevent invalid values from vein used.


问:
How to fit testing in Scrum sprints and how to write user stories in Scrum
I am the development team lead of a new project at my company. This is the first project where the company will use Scrum. We have a waterfall/iterative SDLC. The BAs write requirements docs, hands over to dev and test, dev start developing and will release to testing in iterations. Testers take long time to test a release by which devs continue development but also bug fixes for the current release.
I have a few questions

In a sprint with say 5 stories when do you release for testing ? Is it as soon as a story is completed by dev or after all stories are completed but before end of sprint giving test the required time to test.

If the BA writes user stories what should be the detail. Traditionally it takes long time to write a spec with all UI layout, behaviour, text etc to be finalised. I guess my question is how to write stories that are implementable and testable.

Our test team is non-technical. How important it is to have automated UI testing for Scrum. The UI is based on WPF.

I have solid development experience using agile methods (TDD, code reviews, refactoring etc.) but new to scrum.

edit: By iterations I mean that if there are 100 requirements we may release to testing when we have finished 30, 35, 35 requirements rather than wait till all 100 requirements have been completed.

答:
Just want to share some experiences as below, hope that it's helpful to you.

In a sprint with say 5 stories when do you release for testing ? Is it as soon as a story is completed by dev or after all stories are completed but before end of sprint giving test the required time to test.

For each big user story, it should be broken down to many sub-tasks and when sub-tasks are completely done by developer, they should be released to QC for testing immediately. Defect should be fixed after testing for those sub-tasks finish testing.

If the BA writes user stories what should be the detail. Traditionally it takes long time to write a spec with all UI layout, behaviour, text etc to be finalised. I guess my question is how to write stories that are implementable and testable.

In Scrum, user stories should be in any format so long as the conversations surrounding the stories occur and not expected to be completed or static. A small story, called simply a user story, is one that is well understood and can be implemented within a sprint.
 A story’s priority can be based on ROI, business value, or whatever else the user agree is important. This is activities by PO.

Our test team is non-technical. How important it is to have automated UI testing for Scrum. The UI is based on WPF

Applying the automation testing in Scrum is quite difficult activity. Because all functions are implemented incompletely and not really stable to let QC implement the test case by some auto-testing tool. It should be done for a sprint called: regression.


问:
Duplicate Account Detection - New Approach?
So I'm working on a web based game, and need to deal with the age old issue of duplicate account detection.

Players often create multiple accounts to farm money/items etc to their main account, or to stash items etc.

In the games I've typically played the main way this is done is by preventing multiple account to be used on the same IP within a certain window of days. If it happens repeatedly the newest account is banned.

The problem here in my mind is that the determined players just use a proxy, and it also prevents family members from both playing the game in the same home.

My idea, is to handle this another way. To keep a running tally of the 'networth' of items, money, etc that each account has given to another such as in trades, gifts, lost duels, etc as well as track the # of such interactions. Then if I detect accounts which have a large # of trades between the same two account with a growing networth traded from one account to the other (usually from the feeder account the the main one) I can then reasonably assume that these are suspected of being dupe accounts. If the amounts or small, and/or the frequency is small then it can be allowed.

Am I missing any glaring holes in this approach? I realize that small fish may stay under the radar, but these people would likely use a proxy anyway. And it will not prevent family members who make occasional trades between eachother.

答:
On the client, create a randomly generated "machine id" and set that as a cookie.  Then send this value up with requests.  This will avoid many of the issues you mention with IP addresses.  (Though not all.)  On the server, you can maintain a list of all machines associated with a given account.  If you see the same id on two different accounts you know that both accounts were used in the same browser under the same local account on the same machine.  You still might have false positives as family members sometimes don't bother with separate accounts, but it should be a lot better than IP.

Of course, this is something a savvy users can pretty easily avoid, so you'd want to keep this soft and give no indication on the client that you are doing this.  You probably want to make it look like this device id is just part of the sign in process.  (For instance, sign the user out of the game if the cookie goes missing.)  If you use some sort of cookie-based session mechanism then embed the machine id in the session id.

If the user doesn't notice the cookie before creating the duplicate account, you can catch proxy use by seeing the same machine ID come from two different IPs.  (Though this again can have false positives.)

You idea of looking for "cheating behavior" isn't bad as long as you make sure to keep a human in the loop.  Use this to trigger alerts for those humans.  Once a human is in the loop, they can catch patterns that no computer ever will.  For instance, one player consistently making profoundly bad trades to another player.

A lot of this sounds like "Security by Obscurity".  It absolutely is.  In a web-based application, the user has huge amounts of control of what runs and so no real security scheme is feasible.


问:
Where is it implied that REST is not based on HTTP
I've been over Fielding's paper on REST and all over the web, including StackOverflow trying to understand this.

After reading everything I can about REST, I was under the impression that Fielding based REST on top of HTTP, using it as a stepping stone. But then I go and read some people saying that REST  actually doesn't depend on HTTP and it could be implemented perfectly over FTP (to give an example).

Is there any official paper (mainly from Fielding) specifically explaining this?

答:
This answer is based on excerpts from Architectural Styles and
the Design of Network-based Software Architectures, Roy T. Fielding's PhD

The Introduction to Fielding's dissertation reads, in part:

Over the past six years, the REST architectural style has been used to guide the design and development of the architecture for the modern Web

Chapter 5 Section 5.3.2  reads, in part:

REST does not restrict communication to a particular protocol, but it
does constrain the interface between components, and hence the scope
of interaction and implementation assumptions that might otherwise be
made between components.

For example, the Web's primary transfer
protocol is HTTP, but the architecture also includes seamless access
to resources that originate on pre-existing network servers, including
FTP, Gopher, and WAIS. Interaction with those services
is restricted to the semantics of a REST connector.

This constraint
sacrifices some of the advantages of other architectures, such as the
stateful interaction of a relevance feedback protocol like WAIS, in
order to retain the advantages of a single, generic interface for
connector semantics. In return, the generic interface makes it
possible to access a multitude of services through a single proxy.  If
an application needs the additional capabilities of another
architecture, it can implement and invoke those capabilities as a
separate system running in parallel, similar to how the Web
architecture interfaces with "telnet" and "mailto" resources.

Further more in Chapter 6 Fielding goes on to give examples of how REST was the drive behind URI (5) and HTTP (6, 7)

From this we can see that Fielding's contributions to Web standards (including HTTP) was driven by the defining principles, or constraints of the REST architectural style.


答:
I saw a good quote recently (can't find the link now) that flipped the equation around: that HTTP is a RESTful protocol. In other words, if you want to implement a RESTful service, leveraging HTTP is a good bet because it embodies the principles of REST.

So rather than saying that REST is built on HTTP -- which is how I always thought of it -- it's better to say that REST is a set of design principles which are realized in the HTTP protocol. HTTP is a RESTful protocol.

(Now it may actually have played out that REST emerged from observing what worked well with HTTP, but for argument's sake we can pretend that REST encompasses a certain set of eternal principles and HTTP just happened to take form around them. ;) )

Title: Where is it implied that REST is not based on HTTP

Body:
I've been over Fielding's paper on REST and all over the web, including StackOverflow trying to understand this.

After reading everything I can about REST, I was under the impression that Fielding based REST on top of HTTP, using it as a stepping stone. But then I go and read some people saying that REST  actually doesn't depend on HTTP and it could be implemented perfectly over FTP (to give an example).

Is there any official paper (mainly from Fielding) specifically explaining this?


问:
When would someone be considered a bad programmer?
How would you consider that a programmer is bad at what he or she is doing?

If possible... How should he/she improve?

答:
I think that the only way a programmer can be bad at programming is if he stops listening to what other people have to say.

Programming is about information. One needs to keep the ears, and eyes wide open.

A programmer can only improve by hitting the books, and working hard. But, you should also focus on learning new things, not learning the same over and over (look for new experiences inside your specific field/industry).

Good luck.


问:
Algorithm to "de-rasterize" a voxel image of a polyhedron
Rasterizing a polygon in this question consists of creating an array of binary pixels that "represent" the polygon.  The reverse of this process is easy to approximate in linear time to the perimeter in pixels.  It uses a "detail threshold" D to define how complex the polygon will be.  As D decreases, the polygon becomes more detailed.

Keep in mind this is just for polygons and 2D images, not 3D, yet.

Remove the inside of the image if there is one, so we have a 1-pixel thick perimeter

Assign an arbitrary "first" pixel as our active pixel, and the first point on our polygon

Iterate in one direction around the image, using each pixel as a "test pixel"

For each test pixel, keep track of the minimum and maximum angle between the active pixel and our test pixels

If the difference between these two angles becomes greater than D*2, create points on the polygon for these two minimum and maximum points

Set the active pixel to whichever of the min/max was second (that is, the furthest clockwise)

From now on, iterate around the image and add another vertex whenever |(angle of previous line segment) - (angle between active/test pixel)| > D.  The active pixel becomes the most recently added vertex.

This doesn't produce an optimal polygon, but it works.  It's the best I could think of, anyway.  If you rasterize the result, it would produce something similar to the original image.  It could be potentially improved by setting a minimum distance between vertexes.

However, how could an algorithm like this be extended to 3D, where the rasterized "image" is 3D/has volume, and we are producing a polyhedron?  I'm at a loss as to how to figure this out.

Thanks in advance for the help.

答:
Note: I am assuming there is only one object (all voxels are connected). If this may not be the case, then you need to start by separating "all voxels" into "one set of voxels per object".

Start by assuming each voxel is a cube with 6 faces. For each face the neighbouring voxel may be empty (the face has to be in the mesh) or present/solid (the face is omitted from the mesh). This is basic hidden surface removal.

Next; merge adjacent faces. For example, for a 3*3*3 cube of voxels the top would be 9 little faces and these little faces can be merged to form 1 large face. You may (if desired) generate textures for each "large face" while doing this or after doing this (where textel colour = corresponding voxel colour).

Next; find "inner bubbles". Choose any face and mark is as belonging to "mesh 1". Then find any faces that share an edge with that face and mark them as belonging to "mesh 1" too. Continue doing this recursively to find all faces that belong to "mesh 1". If there are any faces left over, choose one and mark it as belonging to "mesh 2", and find all other faces that belong to "mesh 2". Do this until there are no faces left over (all faces belong to a mesh). Cast a ray from any far away point (e.g. along the Z axis from "Z = infinity") and see which face it hits first. Whichever mesh this face belongs to is the "exterior mesh". All other meshes are "inner bubbles".

Note 1: If the camera can't be inside any inner bubble, then you can discard the inner bubble meshes. If the camera can be inside an inner bubble, then for each inner bubble mesh you can find its centre point and radius (the furthermost vertex from the centre point), and then only render the inner bubble if the distance from camera to the bubble's centre point is less than the distance from the bubble's centre to its furthermost vertex.

Note 2: I described it like this to make it easier to understand. In practice you'd want to cast the ray first, and use the face it hits first as the first face of "mesh 1". This means that "mesh 1" is always the exterior mesh; and (if the camera can't be within an inner bubble) allows you to discard all faces that aren't part of "mesh 1" without building any inner bubble meshes.

Note 3: You'd also cast that ray in the opposite direction to what I described; starting from a face and finding the last face the ray passes through. This avoids the chance of "ray misses all faces".

Finally, (if desired) smooth the mesh/es by finding the edges between faces and generating new faces such that the surface normal of the new face is the dot product of the surface normals of the old faces. You may do this multiple times (e.g. 90 degree corners become 45 degree "chamfered corners", which become 22.5 degree "triple chamfered corners", and so on) until you're approximating curves with a radius equal to half the width of a voxel.


问:
Do GC generations have any purpose (for an application programmer) other than answer to "tricky" HRs' questions?
I'm using C# for a several years, never actually willing (until I really need to) to dig into its "deep" mechanics and to intervene in GC work, IL-compiled code and so on.

My point is if you ever need to even call GC manually (speaking general - to "help" C#'s or .NET's mechanics and frameworks to do their job), then it is 99% probability that you doing something wrong versus 1% that you actually need this.

This is some kind of "old school" programmers' whim - rather than study existing libraries and frameworks they will invent a bicycle again and again, flaunting with their "deep" knowledge, putting every "pattern" they heard of and producing some "masterpiece" which nobody will be able to understand (even creator himself several moths later).

However, back to the topic. GC generations is one of quite popular questions I was asked by some HRs, also I've read some "getting ready for the interview" articles touching this topic, but never seen anything about how you actually can deal with them yourself. I suppose, you can't? I did some surface search and found only "understanding GC" kind of stuff. Like, you can know how compiler actually works, but do you going to "help" it to compile your program? And if not going to, then maybe better use your time to study some ASP.NET vNext kind of stuff?

So, do GC generations have any purpose (for an application programmer) other than answer to "tricky" HRs' questions?

答:
(I don't know C# and I never coded for a Windows OS)

Read at least the wikipage on Garbage Collection, and if possible the GC handbook

A generational copying GC makes the hypothesis (generally rightly) that most young values are temporary and will die soon, and conversely that most old values will last more (and won't die soon). So a generational GC is focusing its work on the youngest generation. Older generations are not processed often, because they contain long-living values which are likely to last more.

See also this SO question.

You might want (usually wrongly, because it does not worth the effort) to force GC when you know that a bunch of old values is becoming garbage. So generational GC are very useful (because they are efficient), but there are few cases where you want to force a GC in them (I won't do that, unless benchmarking tells you that it is worth the effort).


问:
Real-time theory: how is period transformation implemented with delay requests?
To deal with transient overloads with a real-time system scheduled with rate-monotonic scheduling, one can use period transformation to reduce the period of important processes so that they have greater priority. In Scheduling Hard Real-Time Systems: a Review, A. Burns says (pages 4 to 5 of the PDF) that this can be done by:

either adding two delay requests into the body of the code.

or instructing the runtime system to schedule it as three shorter processes.

I understand how splitting it into smaller pieces can work, but how does adding delay requests work?  Does the scheduler look at the process and use the the delay requests as the dividing points for splitting it into three pieces, meaning that for #1 above the programmer is explicitly telling the scheduler how to divide it into pieces, while for #2 the scheduler is guessing about how to split it?

NOTE: I'm trying to understand the theory of period transformation; I'm not asking for the purpose of implementing anything.

答:
Burns's paper (being a review) leaves out the affects of inserting delays, so we'll have to turn elsewhere for an answer. The particular section references "Task scheduling in distributed real-time systems" by L. Sha, J.P. Lehoczky, and R. Rajkumar. If you could get ahold of that paper, it should clarify how delay insertion reduces task periods. Sadly, I cannot, so have to resort to supposition.

What happens is almost what you suppose, but the scheduler doesn't need to inspect the process's internals (i.e. it doesn't need to check for the delay) or make any decisions; the natural affects of the delay do the necessary scheduling work. By "delay", I'm assuming a call to sleep() (or similar), which allows for cooperative multitasking. When considering the following, keep in mind how the scheduler determines period: by keeping track of how long it's been since the process was last schedulable (perhaps using exponential averaging).

There are two relevant affects of sleep(): the process will be suspended (i.e. removed from scheduling), and will get rescheduled after the delay ends. When the process wakes up, the most recent period began when the process was last schedulable, which is the delay period plus the previous execution time. Consequently, the subtasks can be considered to be tasks with period equal to the delay plus the previous subtask's execution time (note that the subtask period could be considered as consisting of other delay times or subtask execution times; to simplify matters, the requested delays should be equal and the subtask execution times be as close as possible). In other words, a task of period p and average execution time e is turned into n subtasks with period p/n and execution time e/n.

For example, Burns's paper mentions a process P2 that runs every 30 seconds for 3 seconds. Inserting two calls to sleep(9) around 1 and 2 seconds along transforms the task into three subtasks of period 10 and average execution time of 1 second. I believe the delay must be 9 seconds, as shortening the delays would increases the period between the 3rd and 1st subtasks (when it comes around again), increasing the period for the 1st subtask, which would reduce its priority.


答:
Knowledge of generational GC -- the general idea, not how large exactly the young space is in Microsoft's CLR version x.y -- is much like knowledge of JIT compilation, operating system principles, assembly, hardware. You virtually never need it to produce a working product. However, it can accelerate bug hunts and optimization attempts by telling you what probably won't work. Two examples:

Reusing/pooling objects sounds like a good idea to reduce GC work. If you don't know a whole lot about GC internals, that is. But in a generational setting, an object that would otherwise be short-lived will be bumped up a generation or two if it goes into a pool. Deallocating it now takes a major GC, which is both rarer and more expensive, and an assortment of other small effects can also take place (depending on whether the object contain references and whether those references point at young objects).

Hence, a pool generally has to prevent many object creations to be profitable, which you now know without having to implement, profile and scrap pooling for something that probably won't benefit from pooling.

Deeper knowledge also curbs nonsensical hypotheses. I wish I had a quarter for every confused beginner who I've seen hunting a (very real) bug by chasing imagined stupidity on the side of the GC. This is mostly related to reachability things, but I've also seen a few questions that could have jumped two edits ahead if poster had had a good grasp on generations.

Finally, don't you think GCs are plain interesting to hear about? Maybe that's just me, and to be honest it's perhaps the biggest reason why I learned these things to begin with.

Title: Do GC generations have any purpose (for an application programmer) other than answer to "tricky" HRs' questions?

Body:
I'm using C# for a several years, never actually willing (until I really need to) to dig into its "deep" mechanics and to intervene in GC work, IL-compiled code and so on.

My point is if you ever need to even call GC manually (speaking general - to "help" C#'s or .NET's mechanics and frameworks to do their job), then it is 99% probability that you doing something wrong versus 1% that you actually need this.

This is some kind of "old school" programmers' whim - rather than study existing libraries and frameworks they will invent a bicycle again and again, flaunting with their "deep" knowledge, putting every "pattern" they heard of and producing some "masterpiece" which nobody will be able to understand (even creator himself several moths later).

However, back to the topic. GC generations is one of quite popular questions I was asked by some HRs, also I've read some "getting ready for the interview" articles touching this topic, but never seen anything about how you actually can deal with them yourself. I suppose, you can't? I did some surface search and found only "understanding GC" kind of stuff. Like, you can know how compiler actually works, but do you going to "help" it to compile your program? And if not going to, then maybe better use your time to study some ASP.NET vNext kind of stuff?

So, do GC generations have any purpose (for an application programmer) other than answer to "tricky" HRs' questions?


问:
How does the consumer-producer solution work?
I'm only a beginner, and my book doesn't cover this subject. I have researched my problem and found that an implementation of the consumer-producer pattern is the ideal solution, and have Googled it, read what I could, tried to enact examples... but haven't been lucky. I would really appreciate a bit of guidance.

I am writing in Ruby, if that makes a difference.

Background

I am writing a script that scrobbles my music library backlog to Last.FM. For anyone who isn't familiar with that service, that simply means that I am making lots of POST requests to a HTTP API.

I start with an array, each element is a hash/dictionary like {artist: "The Cure", track: "Siamese Twins"}. I make the calls by iterating over the array and issuing the simple method call lastfm.track.scrobble artist: song[:artist], track: song[:track].

Problem

Doing this in a straightforward one-at-a-time blocking style works perfectly, but is very very slow, because I'm waiting ~2 seconds for each HTTP request to travel the world and return. I could finish 5 times faster if I sent 5 HTTP requests simultaneously.

I asked on Stack Overflow what the best solution would be. Split the array into five parts, and give part a thread running one request at a time? Something like JavaScript's XMLHttpRequest which has an event loop and a callback function? They told me that this qusetion would be better suited to Programmers SE, and that I probably want the consumer-producer pattern. I looked it up, and it does sound like the kind of thing I need.

My Attempt in Ruby, based off a StackOverflow post

```<code># "lastfm" is an object representing my Last.FM profile,
# its .track.scrobble method handles a POST request and waits until it returns
# "songs" is an array of hashes like {artist: "The Cure", track: "One Hundred Years"}

queue = SizedQueue.new(10) # this will only allow 10 items on the queue at once

p1 = Thread.new do
  songs.each do |song|
      scrobble = lastfm.track.scrobble artist: song[:artist], track: song[:track]
      puts "Scrobbled #{song[:track]} by #{song[:artist]}."
      queue << scrobble
  end
  queue << "done"
end

consumer = Thread.new do
  blocker = queue.pop(true) # don't block when zero items are in queue
  Thread.exit if blocker == "done"
  process(blocker)
end

# wait for the consumer to finish
consumer.join
</code>```

My Error

Failure on the "pop" method call with error "queue empty".

I don't know enough about this stuff to really understand what's happening. It looks like one thread is filling a queue with API calls to make, never more than 10 at a time, while another thread is consuming from that queue and performing them. But why pop? Where is it ever actually executed? Why am I appending "done" to the queue?

I'm concerned about the line scrobble = lastfm.track.scrobble artist: song[:artist], track: song[:track]. Won't this call the lastfm.track.scrobble method outright, and store its return value as scrobble? Should I be using a proc or lambda instead, and calling that proc/lambda in the consumer?

答:
It seems like each song is processed independently, in which case you just need parallel collection. Quick googling found this gem: https://github.com/grosser/parallel

Code would be something like that:

```<code>Parallel.each(songs) do |song|
  lastfm.track.scrobble artist: song[:artist], track: song[:track]
  puts "Scrobbled #{song[:track]} by #{song[:artist]}."
end
</code>```

or that, if you want to see the results:

```<code>scrobbled_songs = Parallel.map(songs) do |song|
  scrobbled_song = lastfm.track.scrobble artist: song[:artist], track: song[:track]
  puts "Scrobbled #{song[:track]} by #{song[:artist]}."
  scrobbled_song
end
</code>```

As doc says, you can adjust the number of processes/threads to match your number of CPU cores.

It is still blocking on each HTTP request (assuming lastfm.track.scrobble is blocking operation), you're just doing it in parallel by using multiple cores (assuming you have more than one).

If you need async HTTP requeest, you might just look for a good tool, like em-http-request or similar.

Body:
It seems like each song is processed independently, in which case you just need parallel collection. Quick googling found this gem: https://github.com/grosser/parallel

Code would be something like that:

Parallel.each(songs) do |song|
  lastfm.track.scrobble artist: song[:artist], track: song[:track]
  puts "Scrobbled #{song[:track]} by #{song[:artist]}."
end

or that, if you want to see the results:

scrobbled_songs = Parallel.map(songs) do |song|
  scrobbled_song = lastfm.track.scrobble artist: song[:artist], track: song[:track]
  puts "Scrobbled #{song[:track]} by #{song[:artist]}."
  scrobbled_song
end

As doc says, you can adjust the number of processes/threads to match your number of CPU cores.

It is still blocking on each HTTP request (assuming lastfm.track.scrobble is blocking operation), you're just doing it in parallel by using multiple cores (assuming you have more than one).

If you need async HTTP requeest, you might just look for a good tool, like em-http-request or similar.


问:
Why static members can't be abstract and don't implement polymorphism?
I'm a Java/C#/PHP developer, through my OOP programming experience I find myself asking the same question: Why static members can't be abstract and don't implement polymorphism especially in situations involving factory methods, for example:

abstract class Resource {
    public void doSomething();
}

abstract class User<resource extends Resource> {

    //if i want to instantiate resource,
    //my only option here is to create an abstract factory in user class
    protected abstract resource create();

    public void use(){
        create().doSomething();
    }

}

class FileUser extends User<File> {

    //It's a bit clumsy, isn't it?
    @Override
    protected Internet create() {
        return new File();
    }

}
```

<p>when abstract static methods would get me rid of these abstract factories:
(Below is not a valid code)</p>

<pre class="lang-java prettyprint-override"><code>abstract class Resource {
    public void doSomething();
    //Im not sure how the child class would get passed though
    static abstract Resource create();
}

abstract class User<resource extends Resource> {

    public void use(){
        resource.create().doSomething();
    }

}
</code>```

So, does anyone know why this kind of behavior wouldn't be approved in OOP? Sorry if it sounds like a silly question.

答:
I'm no expert in any of the aforementioned languages, but a quick perusal of the Java docs and the implicit assertion by you that they all act the same leads me to the understanding that they all use static in the same way that C++ does:

static methods/variables/whatevers are a class-level thing, NOT an object level thing. To call a static method or access a static variable, you MUST specify (implicitly or explicitly) the class whose static member you want to access. And you can do it without EVER creating an object of that class's type.

If there's no object, there CAN'T be polymorphism as it exists in these languages. It doesn't make sense because these languages all do polymorphism based on the actual type of an object. No object, no polymorphism.


答:
Static methods are not a part of Object Oriented Programming, they are practically namespaced functions. Think about it this way, what does it mean to have an abstract method, or to have polymorphic behavior?

It means that you can do this:

```<code>function connectToDB(/*string */ $dsn, /* string */ $user, /*string*/ $password, /*Logger*/ $logger) {
    $pdo = new PDO($dsn, $user, $password);
    //This is polymorphic, you can pass any object that implements the Logger
    //interface, without exposing to this function what logging mechanism is used.
    $logger->log('Connected to database!');
}
</code>```

How would you do this with static methods? Static methods include the class name in the call, you can't (reasonably) pass any logger you want, and use the log static method on it.

The same goes for abstract. There's just no point of an abstract static method.

Title: Why static members can't be abstract and don't implement polymorphism?

Body:
I'm a Java/C#/PHP developer, through my OOP programming experience I find myself asking the same question: Why static members can't be abstract and don't implement polymorphism especially in situations involving factory methods, for example:

abstract class Resource {
    public void doSomething();
}

abstract class User<resource extends Resource> {

    //if i want to instantiate resource,
    //my only option here is to create an abstract factory in user class
    protected abstract resource create();

    public void use(){
        create().doSomething();
    }

}

class FileUser extends User<File> {

    //It's a bit clumsy, isn't it?
    @Override
    protected Internet create() {
        return new File();
    }

}
```

<p>when abstract static methods would get me rid of these abstract factories:
(Below is not a valid code)</p>

<pre class="lang-java prettyprint-override"><code>abstract class Resource {
    public void doSomething();
    //Im not sure how the child class would get passed though
    static abstract Resource create();
}

abstract class User<resource extends Resource> {

    public void use(){
        resource.create().doSomething();
    }

}
</code>```

So, does anyone know why this kind of behavior wouldn't be approved in OOP? Sorry if it sounds like a silly question.

Body:
Static methods are not a part of Object Oriented Programming, they are practically namespaced functions. Think about it this way, what does it mean to have an abstract method, or to have polymorphic behavior?

It means that you can do this:

function connectToDB(/*string */ $dsn, /* string */ $user, /*string*/ $password, /*Logger*/ $logger) {
    $pdo = new PDO($dsn, $user, $password);
    //This is polymorphic, you can pass any object that implements the Logger
    //interface, without exposing to this function what logging mechanism is used.
    $logger->log('Connected to database!');
}

How would you do this with static methods? Static methods include the class name in the call, you can't (reasonably) pass any logger you want, and use the log static method on it.

The same goes for abstract. There's just no point of an abstract static method.


问:
If someone offers you an unverified statement regarding software development practices, do you respond with "citation needed"?
Recently I attended a lecture given by Greg Wilson (Chief Scientist of Software Carpentry). From the abstract:

The idea that claims about software development practices should be based on evidence is still foreign to software developers,
  but this is finally starting to change: any academic who claims that a particular tool or practice makes software development
  faster, cheaper, or more reliable is now expected to back up that claim with some sort of empirical study.

Overall, the lecture was very informative and left me thinking quite deeply about my approach to development. In particular, I now find myself looking for citations to back up a lot of statements. Previously, I had slipped into the habit of simply repeating offered truths, with perhaps a mental note to go check up on it later.

Putting it bluntly, I was being gullible.

Here's an example taken from the lecture:

"If more than 25% of the code needs refactoring, it's quicker to rewrite it".

Sounds plausible, but is it true? Where's the study backing this up? Is it true for all languages? And so on.

OK, it's quite possible to take this to an extreme and not believe anything by anyone unless you have derived it yourself from
first principles. That way lies madness (or maybe mathematics ;-) ). But, if someone comes up to you with a statement along the
lines of "Hey, by doing this in [pick language of moment] we'll be able to boost productivity by [pick multiple of 10]%" are you
inclined to just accept it, or are you going to ask for proven evidence?

If it's the latter (and I hope it is) then

where would you go to find this evidence?

how stringent would you be?

In short, if someone offers you an unverified statement, will you respond with "citation needed"?

答:
If someone offers you an unverified statement regarding software development practices, do you respond with “citation needed”?

No, I post it here and see if it gets any upvotes. Social proof is better than no proof!


答:
There is a current programming model called Object Oriented, and at it's center is objects.

Various kinds of polymorphism are supported, even though some people like to argue about it. But the main kind and the most powerful, is dynamic binding of your method call to the method implemented by the actual type of the target object.

Different languages provide different levels of support for this. Some, like smalltalk, don't guarantee that the method is implemented in the target class, and so at runtime you can get a "message not found" error (smalltalk calls methods messages, loosely speaking). Other, strongly typed languages like C++ and Java, have syntax rules whereby the compiler can guarantee that after your code has compiled correctly that the method call will go somewhere appropriate.

So you could create a language where the kind of thing you want, can happen. It just wouldn't be Java or C++. You would need to think through all the corner cases to ensure that code always worked the way you define it to work in your language definition. Try reading the Java specification to get an idea how carefully this must be done.

A solution many people arrive at, instead of a new language, is Design Patterns. See OO Design and Wikipedia. Design Patterns provide common solutions to common problems, and give a common vocabulary for the solution.

You will always come across cases where "it would be nice if ..." - and so many people produce solutions such as the multitude of open-source frameworks that exist. In comparison to a new complied language a framework is relatively easy to produce and disseminate.

So why can't static members be abstract and don't implement polymorphism? Because it would not be object oriented, and designing a language to do that and get people to accept and use it, is hard.


问:
Why doesn't array declaration guarantee proper memory allocation whereas malloc does?
In C++,
when I declare an array like int array[10];,
it sometimes uses memory assigned to other variable where as this problem is non-existent when I use an integer pointer and malloc to allocate memory to it. Why does this happen?

答:
So I think you're working from a false premise.

I believe you assume that if there are non-zero values in the memory space that was just allocated that the memory was somehow not properly allocated.  And that's a horrific assumption that's going to burn you badly later on as you build more complex programs.  Suffice it to say that the memory is being properly allocated, but you're misinterpreting what you got back.

Neither C nor C++ guarantee that allocated memory will be initialized.  Initialized memory (and variables) is memory or variables that are set to a specific value (typically 0, "",  or false) when the variable is declared or the memory is allocated.

So this is where I think you have a false premise.  I believe you're looking at the contents of the recently allocated variable; seeing non-zero values; and assuming the allocation went wrong.

Complicating your observations is the two approaches you are using to allocate memory.

```<code>int myArray[10];  // allocates on stack

int *myPointer;
myPointer = malloc(10*sizeof(int));  // allocates on heap
</code>```

As pointed out in the comments, one approach allocates on the stack and the other allocates from the heap.

Allocating from the stack will be more likely to reuse memory space that's been used and released! by another area so the memory won't show up as initialized.

Allocating from the heap is probably less likely to reuse space that's already been used and released as it's a larger area of memory to pull from.  And there's a distinctly possibility that you're forgetting to free your memory after allocating it, so you wouldn't run into any reuse cases.

Regardless of all that, the correct thing to do in C and C++ is to initialize all memory and variables prior to using it.  Get into the habit of immediately calling memset after you allocate your memory.

Body:
So I think you're working from a false premise.

I believe you assume that if there are non-zero values in the memory space that was just allocated that the memory was somehow not properly allocated.  And that's a horrific assumption that's going to burn you badly later on as you build more complex programs.  Suffice it to say that the memory is being properly allocated, but you're misinterpreting what you got back.

Neither C nor C++ guarantee that allocated memory will be initialized.  Initialized memory (and variables) is memory or variables that are set to a specific value (typically 0, "",  or false) when the variable is declared or the memory is allocated.

So this is where I think you have a false premise.  I believe you're looking at the contents of the recently allocated variable; seeing non-zero values; and assuming the allocation went wrong.

Complicating your observations is the two approaches you are using to allocate memory.

int myArray[10];  // allocates on stack

int *myPointer;
myPointer = malloc(10*sizeof(int));  // allocates on heap

As pointed out in the comments, one approach allocates on the stack and the other allocates from the heap.

Allocating from the stack will be more likely to reuse memory space that's been used and released! by another area so the memory won't show up as initialized.

Allocating from the heap is probably less likely to reuse space that's already been used and released as it's a larger area of memory to pull from.  And there's a distinctly possibility that you're forgetting to free your memory after allocating it, so you wouldn't run into any reuse cases.

Regardless of all that, the correct thing to do in C and C++ is to initialize all memory and variables prior to using it.  Get into the habit of immediately calling memset after you allocate your memory.


问:
Organise C++ classes around SQL database
My question is about how best to organize C++ classes around a database
model, and I understand this may appear very elementary.

The software I propose to create will do the following. It is intended for
small FMCG manufacturers and will use Qt.

The software will record non-conformance's, product complaints,
corrective actions and serve as an electronic record keeping

There will be quite a bit of 'business logic', as the standout
feature is intended to be comprehensive reporting, searching and
analysis, as well as a flexible approach to entering data to smooth
workflows (ie, a break away from the "one screen per process stage"
paradigm)

The persistent store is an SQL database, with a table(s) for each
type of record (some records will be broken into separate tables for
investigations/solutions. This means that most work will be done on a
single row).

Each customer who uses this software will require amendments,
additional columns/fields or changes in column names and maybe
business logic. Each customer may require updates which change the
database structure. The idea is that anything that can be customized, any
client specific requirements will live within these classes.

There will be reporting, mainly involving pulling records matching
particular categories, word searches, etc.

I have the database schema done and laid out, and the logic of how tables
relate is clear.  I have used this very schema in another framework, but a web based app doesn't seem to offer the level of power to the user with regards to the GUI, which is essential to differentiate this product from the others.

My question is how to design classes around the database. It is important to
get this right, so that the architecture of the software doesn't have to be
redesigned later on. The classes will not be responsible for the persistent
store, only for acting as an intermediary between the layer responsible for
the persistent store and the controllers/views.

The most logical approach appears to be to have a class for non-conformances/complaints etc, which solely store the instance (ie, one object per non-conformance record), validate data and class specific business logic (ie, computations).  One class per table seems logical in this case.  What I'm struggling with, is whether it is standard practice to simply have data members for each column, define getters and setters, or whether I should create a class which isn't hard-coded to map to a table, but which populates a map based on the database schema or external file which defines the database and column properties. The former means that the coding for the class needs to be changed when columns are modified, the latter potentially not.  Keep in mind, for the majority of the time, each user will be working with one record at at time.

In short, I can see example of how to do SQL queries, but I've struggled to
find anything which shows me what the class should look like (or if this entire approach is wrong) and I'd like to compare what I'm doing, with what the industry would define as good practice (I'm not a programmer by trade).

Thank you,

答:
First, for me the most natural way of approaching projects like this is first design the domain model and only after that design how that model is implemented in the selected programming language or how it's stored in a relational database.

Second, since each customer will require amendments, additional columns and customized column names, I would forget the idea of storing each record on a single row and instead use the entity attribute value (EAV) model or even consider using a document oriented database instead of a relational one. It should be noted though that some people consider the EAV model as an anti-pattern because it will cause some additional complexities.

Finally, I'm guessing that all records will still have some shared subset of core attributes that are same for all customers. To me the cleanest approach would be to separate this core part from customer specific extensions altogether. The core attributes I would keep as data members in your C++ classes for ease of use and design a separate mechanism for the extensions. Also, if you decide to use something in the likes of the EAV model, I would still keep these core attributes in dedicated columns instead of splitting them to their own rows.


问:
Pluggable UI Components in Java Web Applications
I will try to explain my question with an example. Let us say that we are writing a hotel reservation system which is web based using Java. We will have different payment methods, say Paypal, Master Card and Visa Card. Different customers of our product will use different payment methods only. (Hotel XYZ, who is our customer, will only want to use Paypal and hotel ABC will only want to show the Master Card and Visa options).

In this kind of scenario, the normal way of doing things would be to have the different payment methods as plugins. And install only the needed plugins in a customer environment.

My question is, how to achieve this in a web application? From my understanding, the folder structure for a Java web application is as shown here. But if we have pluggable payment methods, the plugins themselves should be able to register the user interface components rather than having them in the main application. (For example let us say there is master_card.jsp file which handles the master card payments and visa.jsp etc..). With the above linked kind of folder structure, it seems to me that we would have to put in those jsp files in the webapp even though some customers might not need it.

Can someone please let me know whether

if my above observation is correct

how to have above kind of plugin structure with Java/Java EE?

are there any frameworks in Java that
supports above kind of thing?

答:
if my above observation is correct

It depends. You may as well include everything in distribution and only configure each installation to use what it needs. Possible reasons for going with plugins are:

You charge your plugins separately and don't want to give away features for free

Your plugins are "heavy" in some way, e.g. there are lots of them or they have a significant size.

If those reasons do not apply to your case, you may think of a simpler solution.

how to have above kind of plugin structure with Java/Java EE?

If you're OK with rolling your own simplest solution, here is the general scheme:

In app's configuration (which can be *.properties file or a record in DB, whichever works better for your users, because they should be able to change it), add a property which contains a path to plugins directory

Define and expose plugin API - a set of interfaces which a plugin must implement to allow application to invoke this plugin

Write custom ClassLoader which uses the property from #1 and maybe checks if each plugin properly implements your API

Load your plugins' classes and resources using this class loader

Invoke plugins' functions via interfaces of plugin API or, if plugin consists of resources only, include those as needed

are there any frameworks in Java that supports above kind of thing?

Yes. Googling for "java plugin framework" reveals some.

Other questions on the topic:

Best way to build a Plugin system with Java

Java plugin framework choice

How to create a pluginable Java program?

Java Web application “plugin” architecture

Update:

To load JSP files (or any files), use classloader as described above: see getResource and getResourceAsStream methods.

Update 2:

After meditating some more, I see you just need to conditionally include a JSP into another JSP. This is not "pluggable UI components" or anything like that, it's just conditional inclusion. There are no frameworks for that since it's just about 4-6 lines of code. It seems like you think it's more complex than it actually is.

I think this will do:

have a property in a file (or in DB) which tells which plugin should be used

assign a value of this property to a variable, say String plugin = ...

pass it to your JSP: request.setAttribute("plugin", plugin); or something similar

use it like <jsp:include page="${plugin}" flush="true" />

Could be wrong in syntax, but the point should be clear. Hope it helps.


问:
How can a new programmer impress the software engineer (boss)?
I'm working at my first programming job. My boss is a very smart software engineer, and I feel
like I have very little to offer compared to him. Problem is, he is always busy, and needs someone to help him out. I feel like I'm not good enough, but I still want to succeed. I want to be a great programmer.

What can I do to impress him?

Thank you.

答:
I have to agree with some of the others here in that you are likely to fail in your objective -- because you are focusing on the wrong problem, or at least your focus is too narrow.

You want to be a great programmer -- does the subjective opinion of one software engineer bestow that title and ability on you (other than Joel)?  If you focus only on impressing your boss then you are not concentrating on the work, or on improving your skills -- you are not focused on your goal of becoming a great programmer.  You are trying to be granted respect rather than earning it.

Let's take the worst case (because programmers like to do that) -- your boss absolutely hates you for no objective reason (you wore a Patriots hat the first day, whatever).  He's just never going to have a good opinion of you. If you concentrate on completing your tasks assigned, on solving problems efficiently and elegantly, and furthering your technical skill set -- you will improve yourself -- then in the end you are the winner -- independent of what your boss thinks.

Ashton job was a recipe for failure not because his code went unused, but because the job provided him with no practical benefit other than safety on Maslow's hierarchy.   Was he learning new skills? No. Did his work allow him to be creative? No. Did it earn him respect? No.

Being that this is your first position, it will offer you most of these properties by default.  You will have your first experiences programming professionally, you will be given new challenges both technical and non-technical.  But there will come a time when you outgrow the position or it outgrows you, and you need to be continually improving yourself so you're not caught off-guard by it.

One more thing, if Ashton's going to measure his self worth merely by the amount of people using his code then I suggest he join a frequent-flier club.  The only lasting happiness in life is that which we create for ourselves.  Living strictly according to what other people's opinions of us are produces tragic and inauthentic human beings.


问:
How to associate both a collection and one item of this collection to an entity
I'm working on a project in which I have an entity, we may call Users and another entity Address.

I want to define the entity User in a way that an user would have a collection of addresses (typically a One-To-Many relationship), but I also need an attribute of user, let it be main_address that would target a single value of the previous collection.

I would like to know if there is a conventional of doing this. The only solution I have now is to create another One-to-One relationship between Users . main_address and Address but this does not insure that the main_address will actually be part of the User . addresses collection.

I don't know if it could be important, because it's a rather theoretical question, but I'm using Doctrine 2.

答:
Add a field main_address_id in the User entity or table that holds the ID of the Address record that is the main one.  Make that a one to one relationship, if you like.

The only solution I have now is to create another One-to-One relationship between Users.main_address and Address but this does not insure that the main_address will actually be part of the User.addresses collection.

Quite right.  You need another mechanism that insures that.  You can either validate that in your business logic, or use a stored procedure or trigger on the RDBMS during creation or editing of User that enforces this restriction.

Doctrine might have a "business rules" module that you can use.  The point is that the additional restriction you are imposing on main_address_id doesn't have anything to do with your database schema, per se.  It's a business rule, basically.