问:
Should I use a jump table or a switch case statement for similar functions but may have some parameters that are not relevant
I had a HUGE switch/case statement, which I converted to use a function dispatch table using an enum list for the index.

My colleague (who is doing a code review on this change) agreed that it should be broken down into several functions, but there was a later change that required that some parameters that weren't used in all of the functions.

The code has not yet been finalized and his suggestion is that instead of a dispatch table, that I use a switch/case that calls the functions.  His rationale is that because not all of the functions will use all of the parameters, that calling each individual function with slightly different signatures would be better than using a single function signature for all of the individual functions.

I'm not too adverse to this idea, but I'm not really convinced that this is really a step up either.  The parameters are all POD types, and only the last two which are forwarded are not necessarily used.  So there is no real performance issue, even if it were called a lot, which it is not (requires that the user press a button, making the dispatch time insignificant in comparison).

This is roughly the current layout that I migrated:

```<code>enum FindWhere_e : int
{
    FindInA = 0, // Find Where
    FindInD = 1,
    FindInC = 2,
    FindInE = 3,
    FindInH = 4,
    FindInI = 5,
    FindInJ = 6,
    FindInK = 7,
    FindInL = 8,
    FindInM = 9,
    FindInB = 10,
    FindInN = 11,
    FindInO = 12,
    FindInP = 13,
    FindInQ = 14,
    FindInF = 15,
    FindInR = 16,
    FindInG = 17,
};

BOOL Paragraph::DoSearch(IParaIdxC ipara, int ichar, FindReplaceData *frData, BOOL firstpara, BOOL lastpara, int *nchanges, CDocEnvironment& docEnv, bool allowRedisplay, Section *pSection, CTextWindow* textWindow)
{
    typedef decltype(&Paragraph::DoSearch) vtableElement_t;
    static vtableElement_t const jumpTable[] =
    {
        &Paragraph::DoSearch_FindInA,        // FindInA
        &Paragraph::DoSearch_FindIn_D_C_B_O, // FindInD
        &Paragraph::DoSearch_FindIn_D_C_B_O, // FindInC
        &Paragraph::DoSearch_FindInE,        // FindInE
        &Paragraph::DoSearch_FindInH,        // FindInH
        &Paragraph::DoSearch_FindInI,        // FindInI
        &Paragraph::DoSearch_FindInJ,        // FindInJ
        &Paragraph::DoSearch_FindIn_K_L,     // FindInK
        &Paragraph::DoSearch_FindIn_K_L,     // FindInL
        &Paragraph::DoSearch_FindInM,        // FindInM
        &Paragraph::DoSearch_FindIn_D_C_B_O, // FindInB
        &Paragraph::DoSearch_FindInN,        // FindInN
        &Paragraph::DoSearch_FindIn_D_C_B_O, // FindInO
        &Paragraph::DoSearch_FindInP,        // FindInP
        &Paragraph::DoSearch_FindInQ,        // FindInQ
        &Paragraph::DoSearch_FindInF,        // FindInF
        &Paragraph::DoSearch_FindInR,        // FindInR
        &Paragraph::DoSearch_FindInG,        // FindInG
    };
    // frData->Where is of type FindWhere_e
    if ((unsigned int)(frData->Where) < std::extent<decltype(jumpTable)>::value)
    {
        return (this->*jumpTable[frData->Where])(ipara, ichar, frData, firstpara, lastpara, nchanges, docEnv, allowRedisplay, pSection, textWindow);
    }
    ASSERT(false); // Not handling an unregistered find type in the dispatch table.
    return FALSE;
}
</code>```

So, I'd like to know if there is any reason for using one over the other, or if it really doesn't matter from a design standpoint.  I personally don't really find either clearer to read.

答:
Well, let's first look at the performance-argument:

Your dispatcher has the same arguments as the implementation-functions.

All the arguments are trivially copyable.

Your dispatcher must not do any cleanup after dispatching, and returns the called implementation-functions arguments verbatim.

Thus, your implementation should perform tail-call-optimization, if it doesn't simply inline them.

It is quite possible that the compiler is more inclined to inline the calls in the switch-statement than in your indexed-call, which might reverse who has the advantage.

As always, if you really care about it, measure!

Aside from that, even if you decide to allow for out-of-range arguments in production (as a way to future-proof??), consider putting the unexpected case in an if-block, as that often leads to better performance.

```<code>BOOL Paragraph::DoSearch(IParaIdxC ipara, int ichar, FindReplaceData *frData,
    BOOL firstpara, BOOL lastpara, int *nchanges, CDocEnvironment& docEnv,
    bool allowRedisplay, Section *pSection, CTextWindow* textWindow)
{
    static const decltype(&Paragraph::DoSearch) jumpTable[] = {
        ... omitted
    };
    if((unsigned)frData->Where >= std::extent<decltype(jumpTable)>()) {
        assert(0);
        return FALSE;
    }
    return (this->*jumpTable[frData->Where])(ipara, ichar, frData, firstpara,
        lastpara, nchanges, docEnv, allowRedisplay, pSection, textWindow);
}
</code>```

Body:
Well, let's first look at the performance-argument:

Your dispatcher has the same arguments as the implementation-functions.

All the arguments are trivially copyable.

Your dispatcher must not do any cleanup after dispatching, and returns the called implementation-functions arguments verbatim.

Thus, your implementation should perform tail-call-optimization, if it doesn't simply inline them.

It is quite possible that the compiler is more inclined to inline the calls in the switch-statement than in your indexed-call, which might reverse who has the advantage.

As always, if you really care about it, measure!

Aside from that, even if you decide to allow for out-of-range arguments in production (as a way to future-proof??), consider putting the unexpected case in an if-block, as that often leads to better performance.

BOOL Paragraph::DoSearch(IParaIdxC ipara, int ichar, FindReplaceData *frData,
    BOOL firstpara, BOOL lastpara, int *nchanges, CDocEnvironment& docEnv,
    bool allowRedisplay, Section *pSection, CTextWindow* textWindow)
{
    static const decltype(&Paragraph::DoSearch) jumpTable[] = {
        ... omitted
    };
    if((unsigned)frData->Where >= std::extent<decltype(jumpTable)>()) {
        assert(0);
        return FALSE;
    }
    return (this->*jumpTable[frData->Where])(ipara, ichar, frData, firstpara,
        lastpara, nchanges, docEnv, allowRedisplay, pSection, textWindow);
}


问:
Getting a Candidate to do a Pull Request Review in an Interview
I was wondering if somebody has done this, and to what success compared to the classic method of getting them to solve a problem as you watch on?

Any pointers on how to carry this out in an interview situation?

I was thinking about doing this for mid/senior candidates which should definitely have the technical knowledge to do a pull request. I was thinking it may be a good character test, if they give a full fleshed out and kind response, as apposed to "NO!". You may really see how they will get on in a team.

Edit:

The way I was thinking about doing this, was to send a candidate a small piece of code before the test. They could read through it, ensure they understand what it does and any technologies it uses (I hate interviews that confuse peoples ability to program with how well they know an API).

Then when the candidate comes to the interview you give them the pull request, using Jira to do a git pull request. Since the web interface just shows you what has changed and the ability to view the raw before and after I do not think this would be large barrier to entry.

My core theory of this was to try to remove the memory game test/pressure cooker a lot of interviews can be and try to let somebody show how they would work on a daily basis with you.

To summarise, I want to know if any one is currently doing/done something similar and what outcomes they see. Also any pain points that became evident when you started this process.

Aside:

Thank to everyone who has answered so far they are helpful and very valid but my original intent with this idea was to stop interviews looking for cookie cutter programmers and to let raw talent shin. I wanted to test this theory here with fellow programmers that would possibly go through such a process.

答:
I haven’t done this, but I think it would be a good idea, provided you’re willing to field questions from the candidate about the codebase and what the change is intended to accomplish. They can’t be expected to immediately understand a foreign system, but they should demonstrate how they would go about understanding it.

I would expect an experienced developer to be able to give a high-level technical critique, e.g.:

Are the data structures and algorithms appropriate for the problem?

What are some possible sources of errors?

How could this code be made clearer?

That is, you’re not looking for “this whitespace is wrong”, you’re looking for “an RB-tree would be better than a hash table here, because X” or “this comment could be improved, by mentioning Y”. You’re also looking for the candidate’s ability to talk intelligently about code, and interact well with team members.

ContentLicense: CC BY-SA 3.0


答:
From the moment that some functions have been introduced which accept a different number of parameters than the rest, you do not have a perfectly uniform situation in your hands anymore.

It is not a mortal sin to try to shoehorn a non-perfectly uniform situation into a perfectly uniform situation, but I have a slight aversion to doing that sort of thing: if there is a lack of uniformity, I do not like pretending that there is uniformity.

And the future usually proves that over time, the small lack of uniformity tends to become an ever increasing lack of uniformity, requiring more hacks and tricks to try to maintain the illusion of uniformity.

So, I would go with the switch case.

(I am not mentioning performance considerations, since you have already stated that performance is not an issue.)

Title: Should I use a jump table or a switch case statement for similar functions but may have some parameters that are not relevant

Body:
I had a HUGE switch/case statement, which I converted to use a function dispatch table using an enum list for the index.

My colleague (who is doing a code review on this change) agreed that it should be broken down into several functions, but there was a later change that required that some parameters that weren't used in all of the functions.

The code has not yet been finalized and his suggestion is that instead of a dispatch table, that I use a switch/case that calls the functions.  His rationale is that because not all of the functions will use all of the parameters, that calling each individual function with slightly different signatures would be better than using a single function signature for all of the individual functions.

I'm not too adverse to this idea, but I'm not really convinced that this is really a step up either.  The parameters are all POD types, and only the last two which are forwarded are not necessarily used.  So there is no real performance issue, even if it were called a lot, which it is not (requires that the user press a button, making the dispatch time insignificant in comparison).

This is roughly the current layout that I migrated:

```<code>enum FindWhere_e : int
{
    FindInA = 0, // Find Where
    FindInD = 1,
    FindInC = 2,
    FindInE = 3,
    FindInH = 4,
    FindInI = 5,
    FindInJ = 6,
    FindInK = 7,
    FindInL = 8,
    FindInM = 9,
    FindInB = 10,
    FindInN = 11,
    FindInO = 12,
    FindInP = 13,
    FindInQ = 14,
    FindInF = 15,
    FindInR = 16,
    FindInG = 17,
};

BOOL Paragraph::DoSearch(IParaIdxC ipara, int ichar, FindReplaceData *frData, BOOL firstpara, BOOL lastpara, int *nchanges, CDocEnvironment& docEnv, bool allowRedisplay, Section *pSection, CTextWindow* textWindow)
{
    typedef decltype(&Paragraph::DoSearch) vtableElement_t;
    static vtableElement_t const jumpTable[] =
    {
        &Paragraph::DoSearch_FindInA,        // FindInA
        &Paragraph::DoSearch_FindIn_D_C_B_O, // FindInD
        &Paragraph::DoSearch_FindIn_D_C_B_O, // FindInC
        &Paragraph::DoSearch_FindInE,        // FindInE
        &Paragraph::DoSearch_FindInH,        // FindInH
        &Paragraph::DoSearch_FindInI,        // FindInI
        &Paragraph::DoSearch_FindInJ,        // FindInJ
        &Paragraph::DoSearch_FindIn_K_L,     // FindInK
        &Paragraph::DoSearch_FindIn_K_L,     // FindInL
        &Paragraph::DoSearch_FindInM,        // FindInM
        &Paragraph::DoSearch_FindIn_D_C_B_O, // FindInB
        &Paragraph::DoSearch_FindInN,        // FindInN
        &Paragraph::DoSearch_FindIn_D_C_B_O, // FindInO
        &Paragraph::DoSearch_FindInP,        // FindInP
        &Paragraph::DoSearch_FindInQ,        // FindInQ
        &Paragraph::DoSearch_FindInF,        // FindInF
        &Paragraph::DoSearch_FindInR,        // FindInR
        &Paragraph::DoSearch_FindInG,        // FindInG
    };
    // frData->Where is of type FindWhere_e
    if ((unsigned int)(frData->Where) < std::extent<decltype(jumpTable)>::value)
    {
        return (this->*jumpTable[frData->Where])(ipara, ichar, frData, firstpara, lastpara, nchanges, docEnv, allowRedisplay, pSection, textWindow);
    }
    ASSERT(false); // Not handling an unregistered find type in the dispatch table.
    return FALSE;
}
</code>```

So, I'd like to know if there is any reason for using one over the other, or if it really doesn't matter from a design standpoint.  I personally don't really find either clearer to read.


问:
Is 4-5 years the “Midlife Crisis” for a programming career?
I’ve been programming C# professionally for a bit over 4 years now. For the past 4 years I’ve worked for a few small/medium companies ranging from “web/ads agencies”, small industry specific software shops to a small startup. I've been mainly doing "business apps" that involves using high-level programming languages (garbage collected) and my overall experience was that all of the works I’ve done could have been more professional. A lot of the things were done incorrectly (in a rush) mainly due to cost factor that people always wanted something “now” and with the smallest amount of spendable money. I kept on thinking maybe if I could work for a bigger companies or a company that’s better suited for programmers, or somewhere that's got the money and time to really build something longer term and more maintainable I may have enjoyed more in my career. I’ve never had a “mentor” that guided me through my 4 years career. I am pretty much blog / google / self taught programmer other than my bachelor IT degree.

I’ve also observed another issue that most so called “senior” programmer in “my working environment” are really not that senior skill wise. They are “senior” only because they’ve been a long time programmer, but the code they write or the decisions they make are absolutely rubbish! They don't want to learn, they don't want to be better they just want to get paid and do what they've told to do which make sense and most of us are like that. Maybe that’s why they are where they are now. But I don’t want to become like them I want to be better. I’ve run into a mental state that I no longer intend to be a programmer for my future career. I started to think maybe there are better things out there to work on. The more blogs I read, the more “best practices” I’ve tried the more I feel I am drifting away from “my reality”. But I am not a great programmer otherwise I don't think I am where I am now. I think 4-5 years is a stage that can be a step forward career wise or a step out of where you are.

I just wanted to hear what other have to say about what I’ve mentioned above and whether you’ve experienced similar situation in your past programming career and how you dealt with it. Thanks.

答:
I find the red thread I have seen in my career has been that when I feel I get caught up in a situation where I have no control of my environment that is when I start feeling bored. Just doing what everybody else is telling you. It is important to have an area that is one's own (IMHO) to have complete responsibility of - maybe that is missing in your work as well?

In that case you should speak with your manager, maybe there is some solution in your current job? Asking for more responsibility is always a good move.

OwnerDisplayName: Jeff

ContentLicense: CC BY-SA 2.5

OwnerDisplayName: Anders K.


问:
Managing manual test protocols
We have to organize a very long list of manually running tests. Currently we use Word documents, print them out check them off etc. Ugh-ly but works, with problems.

Problems with the current solution

Requesting and executing subsets, depending on the actual changes. (i.e. tests 153, 155-157 and 24325 need to run only if Module X changed)

Branching. Different releases (which are still maintained, up to a point) have different test protocols, but share a lot of contents. Manual copy & paste.

Tracking "Test Template" vs. "Test executed" documents. It's a bit old-style office with lots of forms & binders.

Tracking in source control together with the source changes. Ideally, the same way features are copied / moved between branches, i.e. as diffable document that is included in a commit.

Content requirements

Documents need images (usually, hardware setup specs and screenshots of "good" curves), "Pass / Fail" checkboxes and minor formatting (suc has emphasis, steps, distinction of "things to do" and "what to expect")

Current idea

One of my devs suggested we use HTML documents instead (Sharepoint Designer is deemed appropriate for editing), "normalize them somehow so they are diffable", and put them under version control together with the sources.

I am personally not wholly sold on this idea for various reasons, but I agree with the other guys that if it works it's better than Word documents.

The Question:

How do you / would manage the documentation for manual tests? Do you know any tools?  Ideas?

We are open to other solutions, but only with minimal custom development efforts. Example: installing dedicated software, configuring some templates would be great, but we have no resources for "hack something together with XML and stylesheets and a database, that should be easy".

Some facts&figures

Source control is git, 6 software developers, ~1.5KLoc, one run through all manual tests takes about 4 man-weeks (order of magnitude) and roughly the same in wall time with all modifications and repetitions. (A full run only happens for major releases.)

Note: Please don't tell me to test automatically, thank you but we already do. The manual tests involve integration, visualization and external measurement equipment. In addition, the information produced is so multifold that the discoveries of a engineer who knows the whole system discovers a whole lot more  with a single look at a chart.

Example: A typcial test setup would look like this, every question mark needs to be "checked", it's usually 5..15 tests per setup

Connect 10Ohm resistor to S1. Connect amplifier input to OUT 1. Run test Template 42.

Measure voltage over resistor. ~1Vrms ?

In Table 3 Uac = 1.0 V, Iac= 0.1 A, ISNR+D > 45 dB ?

U(f) Spectrum flat for f > 10 Hz?

...

答:
We're using http://www.gurock.com/testrail/ to manage our manual testing. It allows us to create a bunch of different test/templates that can be combined into different test suites and the QAs execute the test suites depending on what type of test they need to do (smoke test, pre-release, post release) etc.

Each test run is tracked separately and you can create reports on different aspects. There's a lot of flexibility of how to structure the individual test, so you can fail either the whole test (e.g. Test 42) or an individual step in the test (e.g. "In Table 3 Uac = 1.0 V, Iac= 0.1 A, ISNR+D > 45 dB ?").

I wouldn't advice any process to rely on shared documents, even if they're source controlled, as you end up spending a lot of time on maintenance. Better go with a tool that's build for the purpose :)

Title: Managing manual test protocols


问:
Does immutability hurt performance in JavaScript?
There seems to be a recent trend in JavaScript towards treating data structures as immutable. For example, if you need to change a single property of an object, better to just create a whole new object with the new property, and just copy over all the other properties from the old object, and let the old object be garbage collected. (That's my understanding anyway.)

My initial reaction is, that sounds like it would be bad for performance.

But then libraries like Immutable.js and Redux.js are written by smarter people than me, and seem to have a strong concern for performance, so it makes me wonder if my understanding of garbage (and its performance impact) is wrong.

Are there performance benefits to immutability I'm missing, and do they outweigh the downsides of creating so much garbage?

答:
ImmutableJS is actually quite efficient. If we take an example:

```<code>var x = {
    Foo: 1,
    Bar: { Baz: 2 }
    Qux: { AnotherVal: 3 }
}
</code>```

If the above object is made immutable and you modify the value of the Baz property, what you would get is:

```<code>var y = x.setIn('/Bar/Baz', 3);
y !== x; // Different object instance
y.Bar !== x.Bar // As the Baz property was changed, the Bar object is a diff instance
y.Qux === x.Qux // Qux is the same object instance
</code>```

This creates some really cool performance improvements for deep object models, where you only need to copy value types on objects on the path to the root. The larger the object model and the smaller the changes you make, the better the memory and CPU performance of the immutable data structure as they end up sharing lots of objects.

As the other answers have said, if you contrast this to trying to provide the same guarantees by defensively copying x before passing it into a function that could manipulate it then the performance is significantly better.

Body:
ImmutableJS is actually quite efficient. If we take an example:

var x = {
    Foo: 1,
    Bar: { Baz: 2 }
    Qux: { AnotherVal: 3 }
}

If the above object is made immutable and you modify the value of the Baz property, what you would get is:

var y = x.setIn('/Bar/Baz', 3);
y !== x; // Different object instance
y.Bar !== x.Bar // As the Baz property was changed, the Bar object is a diff instance
y.Qux === x.Qux // Qux is the same object instance

This creates some really cool performance improvements for deep object models, where you only need to copy value types on objects on the path to the root. The larger the object model and the smaller the changes you make, the better the memory and CPU performance of the immutable data structure as they end up sharing lots of objects.

As the other answers have said, if you contrast this to trying to provide the same guarantees by defensively copying x before passing it into a function that could manipulate it then the performance is significantly better.

ContentLicense: CC BY-SA 4.0


问:
How to find hard to misspell given names?
Here is a question that I believe could be solved with some data mining and a sophisticated algorithm, but I don't quite know how. Any pointers as to what data sources to use and what algorithm to apply are welcome.

Background: I'm a Romanian-Hungarian who is expecting a baby with a Polish-Ukrainian and haven't quite set up our mind in what country we want to settle down. As you may expect, choosing a given name is of uttermost importance and a hot debate. From my side, I am still traumatised by all the hassle I had to go through when somebody would misspell my name as I moved from one country to another. For example, if you were called "Adrian" you would be blessed in Romania, only to find that you ended up being "Adrien" is some official French document. So my only requirement is to make it extremely unlikely for the baby's name to be misspelled in some European countries.

Problem Statement: Given a set of countries, e.g., France, Germany, Sweden, Poland and Romania, find the list of given names that, when pronounced properly, are unlikely to be misspelled by the locals.

More formally: Let p(c, n) be a function that returns the probability of name n being misspelled in country c. Given C a set of countries and
p₀ a probability, find N a set of given names, such that

for all n∈N and c∈C, p(c, n) < p₀

Initial thoughts: The core problem is how to implement p(c,n). One could try to approximate it with a heuristic. Clearly a name is likely to be misspelled in two cases:

It is little used in that country.

It is similar to a different name, that is not little used in that country.

I'm not sure how I could use the Internet, e.g.,
Wikipedia, to efficiently answer these two questions. How would one
list only fairly frequently used names in a country? How would one look for similarly spelled ones?

答:
There are some approaches that would work better for some languages than others.  For example, soundex (and another description I like) was designed for English pronunciations of names.  With soundex, Michael becomes M240.  This has several steps:

First letter is isolated. (M and ichael)

All vowels are removed from remainder (M and chl)

Consonants are replaced

c -> 2

l -> 4

Left pad zeros.

The grouping of the consonant conversions are based on their phonetic similarity - B, F, P and V all map to 1.

And there are variations on this over time. It is particularly useful in genealogy where the spelling of a name may change over time, but the pronunciation remains similar.

There is also approaches such as match rating which was developed by the airlines for names (rather than American genealogy).

The encoding of match rating approach (MRA) is:

Delete all non-leading vowels (Michael becomes Mchl and Anthony becomes Anthny)

Remove the second constant of any doubles

If the string is longer than 6 characters, reduce the remaining string to 6 characters by taking the first three and last three.

The full specification for this can be found on archive.org - note that it is "not small" (the printed form is 214 pages).

The comparisons have a matching threshold based on how long the text is.

There are other phonetic algorithms too.

So, what I would encourage you to do is either take the soundex as is, take the match rating approach as is, or modify the soundex based on the Romanian consonants and Polish consonants.

Remember that with soundex, the consonants are grouped (In Polish, m, n, ɲ are all nasal consonants to be grouped, and you would likely group the labial, dental, and alveolar plosives - be they voiceless or voiced together - granted, I don't know Polish so don't know if I'm just saying things that aren't true there).

Then just covert all the names in the database to the two different soundex systems and find out what names have the lowest set of collisions in the different languages.  This gives you distinct names. So that Smith doesn't show up as Smyth.

This, however, only solves the "name likely to collide with other names and be misheard."  It doesn't address the other way of the "name heard correctly, written down incorrectly" and for that, one should focus their attention on common names.

For example, Michael was a very common name in the US from early 1950 to late 1970. It was really popular.  However, for some reason, the name Micheal was kind of popular in the 1950s (got up to the 83rd most common name at its peak).  And I am certain that people named Micheal constantly got their name misspelled.

Thus, you should focus on names where there is one name that dominates the popularity of the name for a given pronunciation.  Glancing at another data consumer for the names by year, you can see that names beginning with Jam... for a boy are a mess with Jamaal, Jamal, Jamar and others.  Incidentally, these names have slightly different soundexes for American (J540, J540 and J560 - the l and r are in different groups even though they are closely related in phonetics).  However, for someone from, say Japan, the there is only one sound in the phonetic region where l and r are pronounced in American English.  This may also pose a challenge with the leading consonants using soundex that one should be aware of (I once worked with a Japanese woman who called herself Risa (with an 'R') rather than Lisa as a Romanization of her Japanese name).

You will note that my examples are for the United States. That data is easily accessible.  Apparently there are some things for Poland and Hungarian, and only hints at Hungarian name commonality... I suspect that searching in a language other than English might be helpful there.

So, given the soundex for a name, few collisions and the actual spelling is in the set of collisions.  Preferably, this is a common name.  Looking at that hungarian list, going with Krisztián would likely get misspellings while, Zoltán less likely so (#22 most common baby name in 2011 in Hungary!).  That said, you can't go wrong with Michael.


答:
In a straight line, immutable code has the overhead of object creation, which is slower. However, there are a lot of situations where mutable code becomes very difficult to manage efficiently (resulting in a lot of defensive copying, which is expensive too), and there are a lot of clever strategies for mitigating the cost of 'copying' an object, as mentioned by others.

If you have an object such as a counter, and it's getting incremented many times a second, having that counter be immutable might not be worth the performance penalty. If you have an object that's being read by many different parts of your application, and each of them want to have their own slightly different clone of the object, you'll have a far easier time orchestrating that in a performant manner by using a good immutable object implementation.

Title: Does immutability hurt performance in JavaScript?

Body:
There seems to be a recent trend in JavaScript towards treating data structures as immutable. For example, if you need to change a single property of an object, better to just create a whole new object with the new property, and just copy over all the other properties from the old object, and let the old object be garbage collected. (That's my understanding anyway.)

My initial reaction is, that sounds like it would be bad for performance.

But then libraries like Immutable.js and Redux.js are written by smarter people than me, and seem to have a strong concern for performance, so it makes me wonder if my understanding of garbage (and its performance impact) is wrong.

Are there performance benefits to immutability I'm missing, and do they outweigh the downsides of creating so much garbage?


答:
When I was 6 and my brother was 11, he made me eat a ball of feathers. It wasn't easy. I choked it down with a lot of dry gagging and a little wet vomit. Partially I did it because I was afraid of my brother. But mostly I did it because it proved I was harder than him. He was this vicious, brilliant, beautiful boy. I was a puny, weak, and ugly gerbil. Regardless, it was I who could do the impossible. I suffered through his torments without the tears he craved because I knew it made me stronger than him. I exposed his inherent weakness.

My point, as it relates to your question, is that I have a set of values that are important to me. It sounds as if you value pull requests like I value obstinate individualism, and that's great. The thing is, I've since learned that I miss out on the textures and insights of many interesting people if I insist that they share all of my values. If you insist on a live pull request during an interview, you'll likely vote many capable (and interesting) people off the island on the basis of that one task.

I know, I know, a pull request should be a bare minimum for a professional at the level you're describing. Right? I agree in regards to short-term opportunities, but with the long-term... I throw out those restrictions. I don't want to miss that hidden gem. What about the brilliant physicist who didn't get tenure and is looking at new opportunities? She's an amazing programmer, has a completely original way of looking at the world, and would elevate your team. But... she can't submit a pull request to save her life. What about the old IBM iSeries developer? He has a nuanced and subtle understanding of software after decades in the business. He's experimented with a ton of technology outside of work -- Haskell, npm, Docker, Haxe -- he loves it! He's a great poker player because he can read people. This is indispensable for intuiting what is really needed in his software. But... he's never submitted a pull request. To make matters worse, he gets a little nervous and sweaty in interviews. I don't want to miss the opportunity to work with these people!

Also, be careful of "get on in a team," "get along on a personal level," and "interact well with team members." Don't focus on that too much early (but don't sack good sense). Yes, you want to avoid the lunatics, but "getting on" is often one of those "cultural fit" requirements, and those are often code for people-who-are-just-like-me. Instead, focus on people who are wicked smart and have unique insights into problems, maybe even if they're not your problems. That's what an interview is for me; it's a flexible platform for promoting smarts. I want it to be loose enough so that your peculiar brilliance shines through on its own terms. Our best hire in the last 10 years came to us as an intern from a woman's correctional facility. I suppose that's a story for another time.

Title: Getting a Candidate to do a Pull Request Review in an Interview

Body:
I was wondering if somebody has done this, and to what success compared to the classic method of getting them to solve a problem as you watch on?

Any pointers on how to carry this out in an interview situation?

I was thinking about doing this for mid/senior candidates which should definitely have the technical knowledge to do a pull request. I was thinking it may be a good character test, if they give a full fleshed out and kind response, as apposed to "NO!". You may really see how they will get on in a team.

Edit:

The way I was thinking about doing this, was to send a candidate a small piece of code before the test. They could read through it, ensure they understand what it does and any technologies it uses (I hate interviews that confuse peoples ability to program with how well they know an API).

Then when the candidate comes to the interview you give them the pull request, using Jira to do a git pull request. Since the web interface just shows you what has changed and the ability to view the raw before and after I do not think this would be large barrier to entry.

My core theory of this was to try to remove the memory game test/pressure cooker a lot of interviews can be and try to let somebody show how they would work on a daily basis with you.

To summarise, I want to know if any one is currently doing/done something similar and what outcomes they see. Also any pain points that became evident when you started this process.

Aside:

Thank to everyone who has answered so far they are helpful and very valid but my original intent with this idea was to stop interviews looking for cookie cutter programmers and to let raw talent shin. I wanted to test this theory here with fellow programmers that would possibly go through such a process.


问:
Code attribution to a post quoting an original source
If I found a post that properly quotes code (gives attribution) from an original source, is it ok if I attribute to that post only, or do I need to give attribution to the original source?

For instance: I found this StackExchange answer: https://unix.stackexchange.com/a/4533/92787 :

Remove color codes (special characters) with sed

```<code>sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
</code>```

Which properly links to the original source: http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed

Is it ok if in my code I put only this:

```<code># sed expression taken from https://unix.stackexchange.com/a/4533/92787
[my code] .... sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" ... [my code]
</code>```

答:
Surely the IANAL answer is, it depends upon the licence of the original source, and how that may be adjusted by the licence of the attributing site :-(

If there are multiple attributions, I'd leave your example as is, perhaps adding which was from multiple sources allowing anyone to follow the link themselves to find the others.

For your exact example, of course, the simple adjustment is:

```<code># sed expression taken from http://unix.stackexchange.com/a/4533/92787
# which obtained it from http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
[my code] .... sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" ... [my code]
</code>```

And if that's how you found it, it attributes both "sources".

Body:
Surely the IANAL answer is, it depends upon the licence of the original source, and how that may be adjusted by the licence of the attributing site :-(

If there are multiple attributions, I'd leave your example as is, perhaps adding which was from multiple sources allowing anyone to follow the link themselves to find the others.

For your exact example, of course, the simple adjustment is:

# sed expression taken from http://unix.stackexchange.com/a/4533/92787
# which obtained it from http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
[my code] .... sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" ... [my code]

And if that's how you found it, it attributes both "sources".


问:
Evaluating concurrent application design approaches on Linux
Working on relatively low-speed hardware, I need to grab a snapshot of a rapidly changing datastream and then post-process the snapshot data and save it to disk.

I need help with designing the overall approach given the processing constraints from the hardware.

The snapshot phase needs to complete relatively instantaneously, because it blocks the datastream as it performs the capture.  I believe I want to run this as a separate process, or at least as a stand-alone thread.  This phase needs to be given priority for processing and accessing resources.

The post-processing phase relies on external libraries that do not use a repeated-small-iteration API/function call model I would be able to throttle the call speed of - instead, a single function call performs the entire operation.

However, I don't care when these post-processing tasks finish, and I would prefer this routine be run in such a way that it vies for very little CPU/IO while working.  My thinking is that this would encourage future snapshot events to complete with a minimum of latency.  The post-processing only takes a few seconds to complete, but I far prioritize snapshot latency over post-processing speed.

Taking all of these requirements into account, it makes sense to me to run both of these actions as separate, concurrent tasks, and tell the kernel the snapshot task is extremely high priority, and the post-process task is extremely low priority.

I generically say task here, and I said "concurrent" in the title, because I'm not sure whether I want to use separate processes for this, or implement some form of multi-threading.

I have only a little experience with fork() and the like, and no real experience with threading, so I have a few questions about how/where I should get started:

Which method would be preferable - threading or separate processes?  Does utilizing threads simplify sharing memory access between the two tasks?

Can I set kernel scheduling/IO priority when using threads?  Or am I worrying too much about setting kernel priority at this point?

To keep the snapshot process fast, it would be ideal for the snapshot task to simply leave data to process sitting in memory for the post-process task to pick up and run with, so the snapshot task is immediately clear and waiting for the next event.  This seems to be best addressed with a queuing approach, but I don't know if there are approaches that provide other advantages.

If I was using fork(), I could simply leave unprocessed data in a pipe() for the post-process task to clean up after.  I'm confident that the pipe wouldn't become full and block the snapshot process.

I'm not as certain what I can do with threading.  Could I possibly allocate memory from the snapshot thread; share the pointers via IPC; then free() the data from the post-process thread?  Do I gain any processing speed or other benefits by taking this approach?

To help focus my question, I have two broad paths I'm considering.

Two separate processes using the OS's pipe() functionality to provide communication between them.

A multi-threaded process using IPC or some other communication mechanism.

I need to be able to prioritize one task over the other, and possibly over other processes running on the system.  I have time constraints for executing one of the tasks, but I don't care about execution time for the second task.

Are my two potential paths equivalent, or is there a preferred approach given my constraints?  Or is there a third approach that is better than those two?

答:
Which method would be preferable - threading or separate processes? Does utilizing threads simplify sharing memory access between the two tasks?

Threads do simplify (and greatly improve the performance of) shared memory access. You would use processes if you wanted to make sure that in the event that one of the tasks crashes the other task remains unaffected, but in your particular scenario, that would not buy you anything: if the snapshot task crashes, the post-processing task has no work to do, so it is moot point if it survives the crash.  On the other hand, if the post-processing task crashes, the raw data obtained by the snapshot task will be lost.  So, unless you are willing to temporarily buffer the raw data on the disk, two processes are not needed; two threads will work just fine.

Can I set kernel scheduling/IO priority when using threads? Or am I worrying too much about setting kernel priority at this point?

Yes, can do it with threads.  And you probably won't even have to go to extremes: the snapshot task only needs to have a slightly higher priority than the the post-processing task in order to make sure that the post-processing task never preempts the snapshot task.  However, if you want to make sure that nothing else on the machine delays the snapshot task, use real-time (max) priority for the snapshot task, and normal priority for the post-processing task.

To keep the snapshot process fast, it would be ideal for the snapshot task to simply leave data to process sitting in memory for the post-process task to pick up and run with, so the snapshot task is immediately clear and waiting for the next event. This seems to be best addressed with a queuing approach, but I don't know if there are approaches that provide other advantages.

Yes, queuing will work just fine. You might want to make use not of a queue of data, but of a queue of pointers to memory blocks containing data, so as to minimize copying of data around.

Now, all that having been said, you should examine the possibility of using the disk as a buffer between your two tasks, because this way, if you ever have to temporarily stop the post-processing task, you will not lose the data generated by the snapshot task while the post-processing task is stopped. This will in turn enable you to also use separate processes for the two tasks, which keeps things clean, and provides additional safety: if one of the tasks crashes, the other can continue working.  If the post-processing task crashes, the snapshot task can continue generating raw data which will not be lost because it is being persisted, and if the snapshot task crashes, the post-processing task may have a backlog of previously persisted raw data to keep itself busy with.

If you are sure you will never need to persist raw data to the disk, then another issue becomes evident: neither multi-threading nor multi-tasking buys you anything!

You see,the post-processing task will never be allowed to preempt the snapshot task, but it will always have to be consuming raw data at the same or higher rate than the rate at which the snapshot task produces raw data, or else data will be lost. So, this means that you can have both "tasks" work in lockstep fashion, in the same thread: first the snapshot task is invoked to generate data, then the post-processing task is invoked to process the data, then you may or may not wait until it is time to take another snapshot, and repeat.


问:
iOS Core data & NSUserDefaults
I am creating an App where users can search items from an external API. If the users likes an item they can store them into their personal "favourites item list".

My question is, is it a good practice to save this data as part of core data or NSUserDefault. Currently the data is stored within my database. Every time the user tries to access their favourites list I have to make an HTTP command to retrieve the data from the database.

This process is slow, so I want to save the favourites list data into core data or NSUserDefault. Is this a good approach.

Keeping in mind that the list could grow fast, depends on the users.

Is it a good idea to store large amount of data in core data or UserDefault?

答:
To answer the last question, it's not a good idea to store large amounts of data in NSUserDeafaults.

NSUserDefaults is not made for parge chunks of data. It's a singleton, and all of it's data is loaded at all times, also you can't easily have multi-threading support with NSUserDefaults.

Using Core Data would be better than NSUserDefaults.

What I would suggest in your situation is to cache data that is accessed often. This way, you provide a more responsive experience without taking up a bunch of memory on the user's device.

Title: iOS Core data & NSUserDefaults


答:
In my experience, the first few weeks (months at most) are crucial for the general quality of a project. If you happen to start working at a place where other programmers have already created a mess (bad coding standards, no version control etc.) it's very difficult for a manager, and mostly impossible for a new peer, to establish any improvements. Later, deadlines and budged issues will cause some ugliness an almost every project, but if the foundation is well done, the damage will be limited and manageable.

For that reason, if you find yourself in a situation where you have to work with bad peers on a bad project, try to get assigned to a new project (if this is possible in your company) or find a new job. Don't wait too long, since bad habits are contagious.

Title: Is 4-5 years the “Midlife Crisis” for a programming career?

Body:
I’ve been programming C# professionally for a bit over 4 years now. For the past 4 years I’ve worked for a few small/medium companies ranging from “web/ads agencies”, small industry specific software shops to a small startup. I've been mainly doing "business apps" that involves using high-level programming languages (garbage collected) and my overall experience was that all of the works I’ve done could have been more professional. A lot of the things were done incorrectly (in a rush) mainly due to cost factor that people always wanted something “now” and with the smallest amount of spendable money. I kept on thinking maybe if I could work for a bigger companies or a company that’s better suited for programmers, or somewhere that's got the money and time to really build something longer term and more maintainable I may have enjoyed more in my career. I’ve never had a “mentor” that guided me through my 4 years career. I am pretty much blog / google / self taught programmer other than my bachelor IT degree.

I’ve also observed another issue that most so called “senior” programmer in “my working environment” are really not that senior skill wise. They are “senior” only because they’ve been a long time programmer, but the code they write or the decisions they make are absolutely rubbish! They don't want to learn, they don't want to be better they just want to get paid and do what they've told to do which make sense and most of us are like that. Maybe that’s why they are where they are now. But I don’t want to become like them I want to be better. I’ve run into a mental state that I no longer intend to be a programmer for my future career. I started to think maybe there are better things out there to work on. The more blogs I read, the more “best practices” I’ve tried the more I feel I am drifting away from “my reality”. But I am not a great programmer otherwise I don't think I am where I am now. I think 4-5 years is a stage that can be a step forward career wise or a step out of where you are.

I just wanted to hear what other have to say about what I’ve mentioned above and whether you’ve experienced similar situation in your past programming career and how you dealt with it. Thanks.

OwnerDisplayName: ammoQ


问:
iOS Best practice for handling model objects
There are different levels I'm asking this question at.

Case 1: Let's think about the typical drill-down design. Say a table view controller has an array of custom objects, and tapping a cell will push a view controller that allows the user to modify the object represented by the cell. In this case, should the pushed view controller have the custom object as a property of its own, or use a data source/delegate protocol to edit the custom object but not own it.

Case 2: A similar but slightly different situation is this. I'm using a singleton store to handle an array of bank accounts in my app. A view controller will show a list of the accounts, and I'm wondering if I should have the array of accounts as a property in my view controller or get the array via the store. (The array of accounts is accessed quite often.) I guess the only difference is a single object vs. an array of objects. I'm curious about how heavy these arrays can be, so whether it's faster to load the array from the store each time or have it as a property in the view controller.

Case 3: When should the local file system be used? In my app's example, bank accounts are accessed quite often, so I have them unarchived and set as properties upon launching the app, but for much bigger data, I only load them from the file system when they should be displayed or edited. I'm still not sure what the right way is.

答:
In case 1, you should probably use the store and/or some kind of Data Manager. The reason to this is that 1) it shouldn't be the ViewController's responsibility to change the data, and if you build it into the VC, you would have to change a bunch of them to change the functionality.

There's also the issue of copying data around. When you're working with copies, you don't know what's going on with the original, and you're not making changes in real-time.

Same goes for case 2. The controller only displays the data, ideally the true reperentation of the data, not a copy.

If loading speed is an issue, you could split up your database to be able to load only the data you need (if you can), and you can also cache data that is used often.

In case 3, you can also cache the data, keeping in mind to track any changes that might happen in between caching and using the data. Caching is better that simply storing because your app won't take up a lot of space on the user's device.

UX/UI design elements like spinners/activity indicators go a long way in making the app feel more responsive.


问:
Why is %s better than + for concatenation?
I understand that we should use %s to concatenate a string rather than + in Python.

I could do any of:

```<code>hello = "hello"
world = "world"

print hello + " " + world
print "%s %s" % (hello, world)
print "{} {}".format(hello, world)
print ' '.join([hello, world])
</code>```

But why should I use anything other than the +? It's quicker to write concatenation with a simple +. Then if you look at the formatting string, you specify the types e.g. %s and %d and such. I understand it could be better to be explicit about the type.

But then I read that using + for concatenation should be avoided even though it's easier to type. Is there a clear reason that strings should be concatenated in one of those other ways?

答:
Using + should not be avoided in general. In many cases is the correct approach. Using %s or .join() are only preferable in particular cases, and it is usually quite obvious when they are the better solution.

In your example you are concatenating three strings together, and the example using + is clearly the simplest and most readable, and therefore the recommended.

%s or .format() are useful if you want to interpolate strings or values in the middle of a larger string. Example:

```<code>print "Hello %s, welcome to the computer!" % name
</code>```

In this case using %s it is more readable since you avoid chopping the first string into multiple segments. Especially if you are interpolating multiple values.

.join() is appropriate if you have a variable size sequence of strings and/or you want to concatenate multiple strings with the same separator.

Body:
Using + should not be avoided in general. In many cases is the correct approach. Using %s or .join() are only preferable in particular cases, and it is usually quite obvious when they are the better solution.

In your example you are concatenating three strings together, and the example using + is clearly the simplest and most readable, and therefore the recommended.

%s or .format() are useful if you want to interpolate strings or values in the middle of a larger string. Example:

print "Hello %s, welcome to the computer!" % name

In this case using %s it is more readable since you avoid chopping the first string into multiple segments. Especially if you are interpolating multiple values.

.join() is appropriate if you have a variable size sequence of strings and/or you want to concatenate multiple strings with the same separator.


问:
Passing MVVM objects to other classes
So I'm using C# WPF and binding to a Model View to display states.  The data that needs to be displayed is spread across a couple different classes.  For instance I have a class that reads out digital IO values that need to be displayed on the main interface as red or green lights.

Is it better to simply pass the Model object to the classes have them update the properties directly?  This seems easier in the short term but smells weird.

EDIT

I think a better word for what I'm asking about is encapsulation.  Encapsulation seems to be an important concept in OOP.

In case 1 the model is "encapsulated" in the Main class. Other objects do not interact with the Model directly.  delegate methods are used.  This seems verbose and a bit messy, but this is the solution I'm looking at to keep the data encapsulated.

In case 2 the model is passed to a secondary object as an argument in the constructor, which is acting on the model concurrently with the original instantiater.  Seems to break the encapsulation rule, and could be confusing in the shear number of places the model may be accessed.  This will be especially true where various classes have data that needs to be viewed.

This could be entirely opinion based.  I see benefits and pitfalls of both approaches.  Case 2 doesn't lend itself to being reused.  Whereas Case 1 could be implemented elsewhere.

In practice is one preferred over the other? How important is encapsulation? Should I maintain it at the expense of readability?

MSDN Implementing the MVVM Pattern

Well-designed view, view model, and model classes will not only
  encapsulate the correct type of code and behavior; they will also be
  designed so that they can easily interact with each other via data
  binding, commands, and data validation interfaces.

END EDIT

The solution I'm working on is using delegates.  For instance:

Case 1

```<code>public delegate void PrependLog(string log);

public class MyDemoClass
{

   private void DoStuff(string str)
   {
     //Do stuff
     OnPrependLog("Did stuff");
   }
   public PrependLog PrepLog
   public virtual void OnPrependLog(string str)
   {
      if(PrepLog != null)
         PrepLog(str);
   }

}
</code>```

Then from the main:

```<code>public MainWindow()
{

   MyDemoClass objDem = new MyDemoClass;
  //Assign delegate
   objDem.PrepLog = (val) => {Model.LogText += val;}

}
</code>```

But from a readibility standpoint, it seems like it would be easier to just pass the MyDemoClass the model and update the log text directly.

Case2

```<code>public class MyDemoClass
{
  MyModel Model;

  public MyDemoClass(MyModel model)
  {
    Model = model;

  }

  private void DoStuff(string str)
   {
     //Do stuff
     Model.LogText += "Did Stuff";
   }

}
</code>```

答:
I hope you understand that there is no objective answer to your question, and as such it is liable to be closed as primarily opinion based. But funnily, the one close vote so far is for "unclear what you are asking", which simply means that someone did not understand what you are asking.  So, here is an opinion.

Assuming that these "classes" of yours conceptually belong to the model, (otherwise, nothing makes sense and you have other, serious issues,) it is, in my opinion, perfectly okay to pass the model to them, so that they may use the services offered by the model.

Lately I have been successfully making use of this scheme that I have come up with, which I call Subject Oriented Design and Subject Oriented Programming. It is inspired from Domain Driven Design. It basically says that every single object which belongs to a domain (and models are virtually always domains) is owned by the domain, so it is a subject of the domain.  (Think of domain as a kingdom, where inhabitants are subjects.)

Various rules apply, for example nobody is allowed to instantiate a subject except the domain itself, so domains act as factories for their subjects, but the rules which you might find to be of interest for your particular situation are that:

Every single subject receives as its first constructor parameter a reference to its own domain.  Conceptually, this is related to how every single instance method of a class receives as its first parameter a (hidden) this pointer. Instance methods need the this pointer to access members of the object, subjects need the domain pointer to access services of the domain.

The domain makes various services available to its subjects, and since each subject has a reference to its own domain, it gets whatever service it needs from the domain and uses it.  This way, inversion of control (dependency injection) is only necessary at the domain level, not at the individual subject level.  And of course domains can be subjects of other domains, so dependency injection can be achieved very easily by passing services to domain constructors, without the need for magic dependency injection frameworks like Spring.

Title: Passing MVVM objects to other classes


问:
Is it possible to write an operating system in C?
A while back I saw a comment on a Stack Overflow question that surprised me:

No one can write an operating system in C. It is not possible. What
  one can do is write an operating system in a C-based/C-like language
  that adds/defines a considerable amount of non-standard capabilities
  on top of C core.

Is it useful to define C so narrowly? People say Linux, or a GPU driver, or an embedded system was "written in C", not "written in C-with-some-extensions-to-accommodate-the-hardware."

Is it incorrect to do so?

答:
There is one big problem with writing a kernel purely in a high level language: No high level language in existence allows you access to all the CPU instructions that an OS has to use.

Take for example the X86 architecture: It implements virtual memory by providing a single special purpose register which points to a tree of page tables. Now, a kernel that needs to set up the virtual address space for a process needs to do two things: 1. build the tree of page tables, and 2. load a pointer to the root of that tree into the special purpose register. However, that special purpose register is inaccessible from C. Step 1 can be done in C, step 2 can't. - The set of instructions that can come out of a C compiler is a true subset of the available CPU instructions, and that subset lacks all those special purpose instructions that are required to manipulate the processing mode of the CPU itself.

That is why every kernel in existence has to include some amount of assembler code. Of course, kernel programmers try to keep the amount of assembler code as low as possible, writing the bulk of the kernel in a high level language like C. This bulk of code is the reason why people say "Linux is written in C", because 99% of the Linux kernel is indeed C code. But C alone cannot do the trick.


答:
To add on to this (already excellently answered) question:

The short answer is yes; it will hurt performance because you're only ever creating objects instead of mutating existing ones, resulting in more object creation overhead.

However, the long answer is not really.

From an actual runtime point of view, in JavaScript you already create quite a lot of runtime objects - functions and object literals are everywhere in JavaScript and no-one seems to think twice about using those. I would argue that object creation is actually quite cheap, though I have no citations for this so I wouldn't use it as a stand-alone argument.

For me, the biggest 'performance' increase isn't in runtime performance but in developer performance. One of the first things I learned while working on Real World (tm) applications is that mutability is really dangerous and confusing. I've lost many hours chasing down a thread (not the concurrency type) of execution trying to work out what is causing an obscure bug when it turns out to be a mutation from the other side of the damn application!

Using immutability makes things a lot easier to reason about. You're able to know immediately that X object is not going to change during its lifetime, and the only way for it to change is to clone it. I value this a lot more (especially in team environments) than any micro-optimisations that mutability might bring.

There are exceptions, most notably data structures as noted above. I've rarely come across a scenario where I have wanted to alter a map after creation (although admittedly I am talking about pseudo-object-literal maps rather than ES6 maps), same for arrays. When you're dealing with larger data structures, mutability might pay off. Remember that every object in JavaScript is passed as a reference rather than a value.

That said, one point brought up above was the GC and its inability to detect duplications. This is a legitimate concern, but in my opinion it is only a concern when memory is a concern, and there are far easier ways to code yourself into a corner - for example, circular references in closures.

Ultimately, I would prefer to have an immutable codebase with very few (if any) mutable sections and be slightly less performant than have mutability everywhere. You can always optimise later on if immutability, for some reason, becomes a concern for performance.


答:
I have these on a recurring basis. What happens is that i generally lose interest in my work and try to learn new skills. After that i try to put these skills into my work, but I rarely get the chance. This is due to micro-management and constantly changing company policy.

Worst thing is that i'm more or less stuck at my current employer unless i move to a different part of the country.

The only "senior" developer with us is actually not a developer, at least in my eyes, he's really productive, but i've never seen him write any code. And to my knowledge he hasn't got much longer work experience than the rest of us.

I'd really like to do something else, like building a boat or something. However i feel that i've really past the turning back point after 8 yrs.

But if i had the chance i'd be doing something different, but i do feel somehow that I would return because this is who I am. I know code.

OwnerDisplayName: Peter Lindqvist


问:
What is the name of this design pattern?
Magento has a class called Varien_Object with a pattern I'm trying to identify. The pattern creates hidden data and allows access to it but the  main difference between this and a normal facade is that methods are dynamically created to allow access to this data.

Here's a (very rough) example of pattern.

```<code>$vo = new Varien_Object();

// The Varian_Object allows data to be set using the "setData" method.
$vo->setData('something', 'one');
$vo->getData('something'); // -> "one"

// Null is returned if the data isn't recognised.
$vo->getData('does_not_exist'); // -> null

// But here's the pattern I'm curious about. By default, the Varien_Object does
// not have a "setSomethingElse" method.
$vo->setSomethingElse('two');
// Behind the scenes, the method name is checked and converted. The line above
// is equivalent to this:
$vo->setData('something_else', 'two');
</code>```

The magic set method has equivalent methods for get, has and uns ("delete").

My question is: does this pattern have a name and, if so, what is it?

答:
There's two parts here - handle an undefined method call.

The first part is a means by which the language allows an object to handle a call to a method which was not defined. On Rosetta it's called  'Respond to an unknown method call'. Language capabilities are the opposite of design patterns - patterns exist to work around lack of capabilities in languages.

The second part is to use the unknown method call capability to turn a call of the form setFoo(x) to a dictionary entry "Foo" -> x. I'm not aware of this being a named pattern; similar use cases in the other languages I know either are static and would use a dictionary here, or are fully dynamic and allow ad-hoc properties to be added.


问:
What's the best way to return an array as a response in a RESTful API?
Assume we have resources like this,

```<code>book:
    type: object
    properties:
        author: {type: string}
        isbn: {type: string}
        title: {type: string}

books:
    type: array
    items: book
</code>```

So, when someone makes a GET on the books resource, we would be returning the following

```<code>[{"author": "Dan Brown", "isbn": "123456", "title": "Digital Fortress"},
 {"author": "JK Rowling", "isbn": "234567", "title": "Harry Potter and the Chamber of Secrets"}]
</code>```

I heard from someone at work that the recommended REST practice is to always return responses as JSON objects, which would mean that our schema for books would look like this,

```<code>books:
    type: object
    properties:
        list:
            type: array
            items: book
</code>```

So, now, response would look like this,

```<code>{
    "list": [{"author": "Dan Brown", "isbn": "123456", "title": "Digital Fortress"},
             {"author": "JK Rowling", "isbn": "234567", "title": "Harry Potter and the Chamber of Secrets"}]
}
</code>```

Which of these is the best REST practice?

答:
A dictionary with a single meaningless key "list" and an array value is pointless - return an array instead.

If the same service could return books, CDs or DVDs, then you could return a dictionary with a key "books" and an array value. There could be another key "DVDs" with an array of DVDs. For example if a customer can enquire a list of all their purchases.

If you are certain that the response will only be interpreted as a list of books (if the request said "give me a list of books") then just an array is fine.


问:
Records defining a partial memory layout
I'm working on a solution that should work in C++-Builder and Delphi, that's why I use Object Pascal syntax, but I'm not very familiar with it. I try to access a file mapping with a size that is not defined at runtime with Free Pascal/Delphi. I tried

```<code>type
  TFileMappingLayout = packed record
    Size: DWORD;
    Data: array [0..0] of byte;
  end;
  PFileMappingLayout = ^TFileMappingLayout;
</code>```

but I'm not sure if this is conform with range checking. It seems to be impossible to google for it, I found nothing useful so far. Reading descriptions about creating structured types did not mention cases like this. I also had a look into the Lazarus source code, but I gave up after 100 trivial record definitions...

I use the Data field only for binary copying, for instance, writing to it:

```<code>  PFileMappingLayout(FData)^.size := cbData;
  Move(myData, PFileMappingLayout(FData)^.Data, cbData);
</code>```

How is this normally done in Delphi/Free Pascal?

How would you name this kind of record definition (open record, partial record)?

答:
To avoid range-check errors, you should rather define the data array as follows:

```<code>Data: array[0..MaxInt-1] of Byte;
</code>```

In general, for a type TMyType, you can define such an open-ended array as follows:

```<code>Data: array[0..MaxInt div SizeOf(TMyType) - 1] of TMyType;
</code>```

But this is all if you specifically want to have a record structure as you currently have. Personally I would just use a dynamic array:

```<code>Data: array of Byte;
</code>```

Use SetLength() to initialize its count and Length() to get its current count. You don't have to allocate or de-allocate any memory for this as you would have to for your implementation.

(Sorry, I don't know what they call such record types as you indicated...)

Body:
To avoid range-check errors, you should rather define the data array as follows:

Data: array[0..MaxInt-1] of Byte;

In general, for a type TMyType, you can define such an open-ended array as follows:

Data: array[0..MaxInt div SizeOf(TMyType) - 1] of TMyType;

But this is all if you specifically want to have a record structure as you currently have. Personally I would just use a dynamic array:

Data: array of Byte;

Use SetLength() to initialize its count and Length() to get its current count. You don't have to allocate or de-allocate any memory for this as you would have to for your implementation.

(Sorry, I don't know what they call such record types as you indicated...)


答:
In practice, the second option is the best practice. The reason for this is that you cannot extend the resource at all when you just return an array.

For example: If you need to add a count of all records you are already done with the array-only approach.

If that happens in one list api then you want to keep it consistent so make all an object then your api becomes more consistent and easier to use for developers.

For example: Let's say a developer writes generic code to use your api to show list and detail pages. He does not want to build an exception, because sometimes it's an array, and sometimes it's an object with a list property.

This answer in total has nothing to do with principles about rest, HATEOAS and other protocols but just being real about the data you need to send to the client. If you decide to follow for example HATEOAS then, of course, stick to their standards (which are also objects btw).

Title: What's the best way to return an array as a response in a RESTful API?

Body:
Assume we have resources like this,

```<code>book:
    type: object
    properties:
        author: {type: string}
        isbn: {type: string}
        title: {type: string}

books:
    type: array
    items: book
</code>```

So, when someone makes a GET on the books resource, we would be returning the following

```<code>[{"author": "Dan Brown", "isbn": "123456", "title": "Digital Fortress"},
 {"author": "JK Rowling", "isbn": "234567", "title": "Harry Potter and the Chamber of Secrets"}]
</code>```

I heard from someone at work that the recommended REST practice is to always return responses as JSON objects, which would mean that our schema for books would look like this,

```<code>books:
    type: object
    properties:
        list:
            type: array
            items: book
</code>```

So, now, response would look like this,

```<code>{
    "list": [{"author": "Dan Brown", "isbn": "123456", "title": "Digital Fortress"},
             {"author": "JK Rowling", "isbn": "234567", "title": "Harry Potter and the Chamber of Secrets"}]
}
</code>```

Which of these is the best REST practice?


问:
Stubbing Properties with private setters for tests
We have the object

```<code>public class MyObject{
    protected MyObject(){}

    public string Property1 {get;private set;}
    public string Property2 {get;private set;}
    public string Property3 {get;private set;}
    public string Property4 {get;private set;}
    public string Property5 {get;private set;}
    public string Property6 {get;private set;}
    public string Property7 {get;private set;}
    public string Property8 {get;private set;}
    public string Property9 {get;private set;}
    public string Property10 {get;private set;}
}
</code>```

In our production code we populate this object through automapper. It can access the properties and set them correctly.

Now when we want to test this class in a future pipeline it is not possible to populate the properties with dummy values (to be tested against).

There are a few options available.

Custom constructors to accept the parameters required for the tests and set the properties, currently 3 constructors are required. This is not clean since the constructors do not serve any business functionality.

Make the properties virtual so the class can be stubbed. But marking the properties virtual does not provide any business value and pollute my class.

Add an object builder to the class to internally construct the object. Again no added business value. Perhaps a bit cleaner but still a lot of non relevant code in the domain objects.

Any suggestions, advice or alternative options here?

答:
You have a number of options.

Go ahead and use custom constructors, virtual properties, or an object builder. The rationale behind this is that an object should stand on its own, and not depend on some magic like the automapper. A class which is completely useless unless there is some magic going on is not a very well thought of class. "Business value" is not the only determinant of a good design.

Include the automapper in the testing process.  Some will say this is not a unit test anymore.  It does not matter.  Unit testing is not the only kind of testing.

Implement something which provides the functionality of automapper specifically for testing.  You can easily write a little utility which will use reflection to populate your object from a dictionary containing property names and values.

Also take a look at this question & answer: Would you rather make private stuff internal/public for tests, or use some kind of hack like PrivateObject?


答:
I think you might be confusing disaffection with 'A job' and disaffection with 'THE job'. As others have said, you just might need a change of situation.

Product-focused development, rather than Line-of-Business (LOB) projects, might give you that stronger relationship between your efforts/desire for quality and outcomes. When you are trying to make a commercial product the best it can be, you are very focused and aligned with the efforts of others, especially if your market is in the tech/programmer area - maybe you need to become a software tool developer for example?

At any rate I would certainly advise you against seeking out larger companies with more money - where do you think the average & below average developers go to hide?

OwnerDisplayName: Gordon Mackie JoanMiro


问:
Abstract classes with static methods in PHP
Pre: I am looking at a quite big PHP project developed by a company which we need to partly interact with while building a new webapp.

The whole application is developed in PHP 5.

Most of the classes in this project, and in particular all the classes that interact with the database, are declared abstract and contain only static functions.

Take for example this file:

```<code>abstract class DataHelper_Command
{

    /**
     * @param unknown $id_device
     * @param unknown $latest_command_id
     * @return Command[]
     */
    public static function getCommandsToSend($id_device, $latest_command_id)
    {
        $rows = DBUtility::executePreparedQuery('SPS_COMMANDS_TO_SEND', array(":id_device" => $id_device, ":latest_command_id" => $latest_command_id));
        return DataHelper_Command::commandsFromDataTable($rows);
    }

    public static function getFW()
    {
        $rows = DBUtility::executePreparedQuery('SPS_FW_VERSION');
        return $rows;
    }

    /**
     * @param unknown $rows
     * @return Command[]
     */
    private static function commandsFromDataTable(&$rows)
    {
        $res = array();
        foreach ($rows as &$row)
        {
            $res[] = DataHelper_Command::commandFromDataRow($row);
        }

        return $res;
    }

    /**
     * @param array $row
     * @return Command
     */
    private static function commandFromDataRow(&$row)
    {
        return new Command($row["id_command"], $row["type"], $row["text"]);
    }
}
</code>```

I don't even see any trace of using a Singleton pattern in the project!

That sounds a bit strange to me, as I always heard that using static classes is a nightmare in terms of performance.

This server was meant to be able to run on both standard webservers and on a really performance-limited embedded device (An ARM running at 400MHz, with 128 MB of RAM).

Am I missing something here? Are there any use-cases in which this is considered a good practice?

答:
Performance is not even the biggest problem here, secret dependencies and lying APIs are.

From a testable point of view, static methods are awful, because they usually represent global state, which, surprisingly, is awful as well, because it means if you run a function two times, you can get different results even when the inputs are the same (that is what global state does). How do you test that?

I cannot really see any case, where I would prefer static methods instead of normal ones. If you can, do not use them, if you must use them, your design is most likely wrong and needs refactoring.

Check Clean Code Talks by Miško Hevery from 2008, they are still relevant and provide great explanations on why static methods and singletons should be avoided.

Some youtube references to some talks by Miško Hevery:

Global State and Singletons

Inheritance, Polymorphism, & Testing

How to Write Clean, Testable Code


答:
both are json and adhere to REST. I would make the response more descriptive, in your case change list to books.
Or something like this :</p>

```<code>{ "responceObject" : {

   results : 2,

    "Books": [
        {"author": "Dan Brown", "isbn": "123456", "title": "Digital Fortress"},
        {"author": "JK Rowling", "isbn": "234567", "title": "Harry Potter and the Chamber of Secrets"}
    ]

}}
</code>```

Body:
both are json and adhere to REST. I would make the response more descriptive, in your case change list to books.
Or something like this :

{ "responceObject" : {

   results : 2,

    "Books": [
        {"author": "Dan Brown", "isbn": "123456", "title": "Digital Fortress"},
        {"author": "JK Rowling", "isbn": "234567", "title": "Harry Potter and the Chamber of Secrets"}
    ]

}}


答: You probably want to look into the Double Metaphone phonetic algorithm, which is designed to handle how words are pronounced in different languages. There is also a Metaphone 3, but that costs money to use.</p>

Title: How to find hard to misspell given names?

Body:
Here is a question that I believe could be solved with some data mining and a sophisticated algorithm, but I don't quite know how. Any pointers as to what data sources to use and what algorithm to apply are welcome.

Background: I'm a Romanian-Hungarian who is expecting a baby with a Polish-Ukrainian and haven't quite set up our mind in what country we want to settle down. As you may expect, choosing a given name is of uttermost importance and a hot debate. From my side, I am still traumatised by all the hassle I had to go through when somebody would misspell my name as I moved from one country to another. For example, if you were called "Adrian" you would be blessed in Romania, only to find that you ended up being "Adrien" is some official French document. So my only requirement is to make it extremely unlikely for the baby's name to be misspelled in some European countries.

Problem Statement: Given a set of countries, e.g., France, Germany, Sweden, Poland and Romania, find the list of given names that, when pronounced properly, are unlikely to be misspelled by the locals.

More formally: Let p(c, n) be a function that returns the probability of name n being misspelled in country c. Given C a set of countries and
p₀ a probability, find N a set of given names, such that

for all n∈N and c∈C, p(c, n) < p₀

Initial thoughts: The core problem is how to implement p(c,n). One could try to approximate it with a heuristic. Clearly a name is likely to be misspelled in two cases:

It is little used in that country.

It is similar to a different name, that is not little used in that country.

I'm not sure how I could use the Internet, e.g.,
Wikipedia, to efficiently answer these two questions. How would one
list only fairly frequently used names in a country? How would one look for similarly spelled ones?


答:
Maybe take a look at The Passionate Programmer by Chad Fowler... quoting from the webpage:

In most cases, remarkable careers don’t come by chance. They require thought, intention, action, and a willingness to change course when you’ve made mistakes. Most of us have been stumbling around letting our careers take us where they may. It’s time to take control.

OwnerDisplayName: Adam


问:
Is "string literal" always the same as "hard-coded string"?
I am learning programming and often see the term "string literal" being used in various places. From the looks of it, it seems to be a synonym for "hard-coded string", but on another hand the term looks quite specific.

Is "string literal" always the same as "hard-coded string"?

答:
Yes, the terms do mean the same.

A string literal means the way a string appears in the code (say in JavaScript, that would be quoted in either single or double quotes ('literal' or "literal"), in the same way that you would have numbers appearing in code - again, a literal (so, in the code var n = 2;, the 2 is a numeric literal).

As you can see - this is the essentially the same as hard coding strings/numbers.

There are subtle differences, but for all practical uses - they are the same thing.


答:
Am I the only one who reads left to right?

To me, using %s is like listening to German speakers, where I have to wait until the end of a very long sentence to hear what the verb is.

Which of these is clearer at a quick glance?

```<code>"your %s is in the %s" % (object, location)
</code>```

or

```<code>"your " + object + " is in the " + location
</code>```

Title: Why is %s better than + for concatenation?

Body:
I understand that we should use %s to concatenate a string rather than + in Python.

I could do any of:

```<code>hello = "hello"
world = "world"

print hello + " " + world
print "%s %s" % (hello, world)
print "{} {}".format(hello, world)
print ' '.join([hello, world])
</code>```

But why should I use anything other than the +? It's quicker to write concatenation with a simple +. Then if you look at the formatting string, you specify the types e.g. %s and %d and such. I understand it could be better to be explicit about the type.

But then I read that using + for concatenation should be avoided even though it's easier to type. Is there a clear reason that strings should be concatenated in one of those other ways?

Body:
Am I the only one who reads left to right?

To me, using %s is like listening to German speakers, where I have to wait until the end of a very long sentence to hear what the verb is.

Which of these is clearer at a quick glance?

"your %s is in the %s" % (object, location)

or

"your " + object + " is in the " + location


答: For me, a string literal is a language construct, an hardcoded string is a string whose value is fixed in a program (you may discuss if it needs to be fixed at compile time or could be somewhat determined by some runtime consideration).  An hardcoded string is often a string literal but could come from other sources (say concatenation of character literals or even some fixed computation like concatenation of a product name with a product version when the version is stored in a pair of constant integer variables).

Body:
I am learning programming and often see the term "string literal" being used in various places. From the looks of it, it seems to be a synonym for "hard-coded string", but on another hand the term looks quite specific.

Is "string literal" always the same as "hard-coded string"?


问:
Is 4-5 years the “Midlife Crisis” for a programming career?
I’ve been programming C# professionally for a bit over 4 years now. For the past 4 years I’ve worked for a few small/medium companies ranging from “web/ads agencies”, small industry specific software shops to a small startup. I've been mainly doing "business apps" that involves using high-level programming languages (garbage collected) and my overall experience was that all of the works I’ve done could have been more professional. A lot of the things were done incorrectly (in a rush) mainly due to cost factor that people always wanted something “now” and with the smallest amount of spendable money. I kept on thinking maybe if I could work for a bigger companies or a company that’s better suited for programmers, or somewhere that's got the money and time to really build something longer term and more maintainable I may have enjoyed more in my career. I’ve never had a “mentor” that guided me through my 4 years career. I am pretty much blog / google / self taught programmer other than my bachelor IT degree.

I’ve also observed another issue that most so called “senior” programmer in “my working environment” are really not that senior skill wise. They are “senior” only because they’ve been a long time programmer, but the code they write or the decisions they make are absolutely rubbish! They don't want to learn, they don't want to be better they just want to get paid and do what they've told to do which make sense and most of us are like that. Maybe that’s why they are where they are now. But I don’t want to become like them I want to be better. I’ve run into a mental state that I no longer intend to be a programmer for my future career. I started to think maybe there are better things out there to work on. The more blogs I read, the more “best practices” I’ve tried the more I feel I am drifting away from “my reality”. But I am not a great programmer otherwise I don't think I am where I am now. I think 4-5 years is a stage that can be a step forward career wise or a step out of where you are.

I just wanted to hear what other have to say about what I’ve mentioned above and whether you’ve experienced similar situation in your past programming career and how you dealt with it. Thanks.

答:
I suggest that, given the opportunity of course, you do something else for a time. Like IT consulting, data warehousing, sales, or even support.

You'll either rediscover your passion for programming and be so glad to get back to it (which is what happened to me) or you'll be happy with your new job and pursue a different career that you hadn't thought of before.

If you get back to programming then, it might also open up the possibility to work on different projects in other industries using other technologies than before, which can also be a good thing.

OwnerDisplayName: Robert Petermeier


问:
How can I diagnose async/await deadlocks?
I am working with a new codebase that makes heavy use of async/await. Most of the people on my team are also fairly new to async/await. We generally tend to hold to Best Practices as Specified by Microsoft, but generally need our context to flow through the async call and are working with libraries that don't ConfigureAwait(false).

Combine all of those things and we run into async deadlocks described in the article... weekly. They don't show up during unit testing, because our mocked data sources (usually via Task.FromResult) aren't enough to trigger the deadlock. So during runtime or integration tests, some service call just goes out to lunch and never returns. That kills the servers, and generally makes a mess of things.

The problem is that tracking down where the mistake was made (usually just not being async all the way up) generally involves manual code inspection, which is time consuming and not automate-able.

What's a better way of diagnosing what caused the deadlock?

答:
Are you using Async/Await so you can parallelize expensive calls like to a database?  Depending on the execution path in the DB this might not be possible.

Test coverage with async/await can be challenging and there's nothing like real production usage to find bugs.  One pattern that you may consider is passing a correlation ID and logging it down the stack, then have a cascading timeout that logs the error.  This is more of a SOA pattern but at least it would give you a sense of where it's coming from.  We used this with Splunk to find deadlocks.


答:
It depends.

Here, a string literal is used which is also a hard coded string.

```<code>Print("Robbie");
</code>```

But here, a string literal is used to define the constant (which is also a hard coded string). However, the print statement no longer uses a hard coded string since it depends on the value of the constant.

```<code>const name = 'Robbie';
Print(name);
</code>```

Title: Is "string literal" always the same as "hard-coded string"?

Body:
It depends.

Here, a string literal is used which is also a hard coded string.

Print("Robbie");

But here, a string literal is used to define the constant (which is also a hard coded string). However, the print statement no longer uses a hard coded string since it depends on the value of the constant.

const name = 'Robbie';
Print(name);


问:
Data Layer vs Business Layer
I think I'm in the minority on this but would be curious to see other perspectives.

A lot of times I see people talk about the Data Layer like having functions like UpdateCustomer() or Summarize(). I really feel like the Data Layer should be more abstract than that. I currently use a generic Repository pattern on top of entity framework in my data layer. So I get functions like Find(int id), Update(), Save(), All(), etc. Very generic functions.

Then I make Business Logic layers that have more specific functions like Summarize() that work on the generic Data Layer. For me just isolating the data access technology is enough for the Data Layer. That's a big enough job on it's own. Why provide these seemingly query functions in your Data Layer? The business logic I write a large part of the time is inside Linq queries themselves so for me all specific queries should be in the Business Layer and not the Data Layer, but this isn't what I often see.

On top of that, because my Data Layers are not very generic, I can share them, they are DLL's, with other Business Layers (often web services) so that I can combine very different pieces of data together in a Business Layer to join queries between the 2 (since I have generic access to the underlying data) and it also helps in not having to recreate Data Layers for each separate solution. To me it's great resuabiltiy.

I'm wondering what the opposite view is on my view here. Why am I "wrong" in thinking this way and why is the other way "right"?

答:
That's not exactly an uncommon scenario.

You can certainly implement your DAL in such a manner, but you will likely want another layer of abstraction between that and your business layer. I've seen it referred to as a Repository or Service layer in the past.

Your DAL offers very generic functions for finding, retrieving, or storing data.  The Service layer acts as a provider to the business layer and gets these objects within certain contexts and parameters, which it receives from the business layer.

Title: Data Layer vs Business Layer


问:
How to periodically serialise object's state and deserialise on initialisation in Python?
We're designing an application that is supposed to monitor a system for certain events, email interested parties to prompt them to take action when relevant and parse their replies.

The application is comprised of several threads that handle separate parts (one for monitoring the state and sending emails, another for parsing replies and updating the state etc.).

The flow of the application depends on several flags that we want to serialise periodically and should the application fail, on startup it needs to load those serialised flags or if no serialised file is found, to just initialise them to a default.

How can we best achieve this? We've tinkered with different ideas, such as:

overriding Python's @property setters and getters to automatically save/load those flags every time they are requested/modified

having a separate thread that serialises those flags often enough (such as every minute)

have each thread take care of serialising the flags it depends on

What we want to avoid is a case where the system takes some action, sets some flags as a result and then crashes before those modifications are serialised.

We cannot use a database and so we've relied on cPickle as the serialisation protocol.

Another issue that results from the above problem is whether those flags should be kept centrally in an object or distributed across the different components that rely on them.

答:
I'd use a separate 'state saver' thread that receives flag values from worker threads via a queue.

The state saver thread would keep the complete picture of flags for all threads, initially either loaded from storage or sent by worker threads.

When a new message arrives via a queue with a new subset of a worker thread flags, the saver thread updates its consolidated flag state and writes it to a new file. Then it can atomically rename the new file to the canonical file name if you only want one snapshot, or remove an old file if you want to keep several latest snapshots.

All worker threads would write their flag updates to the same queue, possibly as simple {flag_name: value} dicts. The queue will have a reasonably large buffer size, allowing each thread to write their updates 2-3 times, so that a burst of changes, e.g. on stratup, won't make the workers wait too much.

A worker thread should post the flags on every flag change event, most probably via put_nowait. If putting a message fails because the queue is full, the worker thread probably should just log this event and proceed, and you should restart the whole thing with a larger queue size eventually.

The saver thread would mostly sleep in Queue.get(True) calls, maybe with a reasonable timeout to allow it periodically write an 'I'm alive' message to the log or something. It should try to get several messages using get_nowait and until it receives an Empty exception, write the state, and then go to the blocking get again.

BTW you're likely not stuck with pickle serialization; you can e.g. use JSON which is easier to inspect if needed.


答:
"String literal" is just a construct of the programming language that you are using. In C or C++

```<code>char* p = "This" " is " "a string" " made from four string literals";
</code>```

is a single hardcoded string made from four string literals. Swift on the other hand has string literals that are not hardcoded strings, like

```<code>let aString = "i + j = \(i + j)"
</code>```

Body:
"String literal" is just a construct of the programming language that you are using. In C or C++

char* p = "This" " is " "a string" " made from four string literals";

is a single hardcoded string made from four string literals. Swift on the other hand has string literals that are not hardcoded strings, like

let aString = "i + j = \(i + j)"


问:
Which data store is best for my scenario?
I'm working on an application that involves very high execution of update / select queries in the database.

I have a base table (A) which will have about 500 records for an entity for a day. And for every user in the system, a variation of this entity is created based on some of the preferences of the user and they are stored in another table (B). This is done by a cron job that runs at midnight everyday.

So if there are 10,000 users and 500 records in table A, there will be 5M records in table B for that day. I always keep data for one day in these tables and at midnight I archive historical data to HBase. This setup is working fine and I'm having no performance issues so far.

There has been some change in the business requirements lately and now some attributes in base table A ( for 15 - 20 records) will change every 20 seconds and based on that I have to recalculate some values for all of those variation records in table B for all users. Even though only 20 master records change, I need to do recalculation and update 200,000 user records which takes more than 20 seconds and by then the next update occurs eventually resulting in all Select queries getting queued up. I'm getting about 3 get request / 5 seconds from online users which results in 6-9 Select queries. To respond to an api request, I always use the fields in table B.

I can buy more processing power and solve this situation but I'm interested in having a properly scaled system which can handle even a million users.

Can anybody here suggest a better alternative? Does nosql + relational database help me here? Are there any platforms / datastores which will let me update data frequently without locking and at the same time give me the flexibility of running select queries on various fields in an entity?

答:
If you are running in Amazon I would consider DynamoDB. It's flash memory based.  Here is a link to it:  https://aws.amazon.com/dynamodb/.

What kinds of RDBMS are you using?  You may be able to increase performance by using a UDF, or a calculated field in a view.  Are you running the calculation in the database via a single update query, or do you select the data out of the database, run the calculations in another process and then load them back in?

Oracle is configured by default to use snapshot mode execution, meaning the rows are not locked during update and concurrent selects get the original value.  SQL  Server is configured by default with pessimistic concurrency, so concurrent selects will block until the update is complete.  Some versions of SQL Server can be put into snapshot mode, however it greatly increases the stress on the temp table.

What kind of environment are you running in?  If it's an RDBMS on an EC2 instance in Amazon then try putting the DB datafiles on the the local flash disk.  I've seen an order of magnitude difference in moving the files from EBS to the local disk.