问:
Sharing dependency configuration over multiple projects (Maven)
From a big SVN repository we want to switch to Maven/Gradle for handling dependencies so that programmers can work with a single project without checking out the whole thing.

We are writing plugins for a server (its interface). Some of our plugins depend on other plugins that are also installed on the server. So our plugins should always be build against the versions that are currently (or going to be) used on the server.

For example lets say my plugin A depends on an external plugin B. Today B is installed with version 1.2.3, but tomorrow it could be installed with 1.2.4. When building A it is important to always build against the right version.

My problem is that we have hundreds of plugins like A so it would be nearly impossible to manually update the versions of all dependencies just if one plugin like B is used with a new version.

Now I thought about two options even if I am not sure how to accomplish them.

Using some kind of global configuration for dependency versions (no idea if or how this could be done)

Always hosting the current used artifacts in a seperate repository

Global dependency resolver

Since I am new to Maven I do not know if this might be a possible or even acceptable solution. I just thought about creating some kind of artifact, that contains all the dependencies that plugins like A could have. The really bad thing (even if this would work) is that plugin A would have dependencies it does not really need.

Global version configuration

Then I thought about a way to tell Maven/Gradle what the latest dependency version of a specific artifact is. I dont know if this is possible or if this would lead to other problems.

Hosting the artifacts

At the moment im testing hosting the external dependencies on a seperate repository as snapshots so that our plugins will be able to always resolve them to the highest available version. Currently this leads to problems when resolving the dependency dependencies but I guess that I just missted something in the configuration. However I am unsure if this could be a right way to go.

I hope that you understand what we need so maybe you can give me some advices or keywords for concepts that could solve our issue. We want to switch to a dependency management system (at the moment all dependencies are included locally) but all options seem more complicated than staying with our messed up repository.

答:
If I understand your situation clearly, it seems like you have many A type Maven artifacts, and these mostly all define a dependency to a few B type artifacts.

The A type artifacts are product of your development Group.  The B type artifacts that A depends on are from an external Group.  Your runtime environments for A type and B type Artifacts will have bundled into them the B Artifacts already.

The problem is that when the Version of a B Artifact changes on the server, you do not want to be hassled with manually updating many POM files with all of the dependency changes that happened in B initially.

Your first suggestion I think is the best approach:

I just thought about creating some kind of artifact, that contains all the dependencies that plugins like A could have.

You can absolutely do this and it is an accepted practice.  What you do is define a parent POM file that declares all of the dependencies an A type Artifact could need.  This parent POM file can be deployed to your Maven repository as it's own unique Group, Artifact and Versioned module. This parent POM module can then be referenced in your A type projects POM files using the <parent> element.  The benefit is that you can update all of the B type plugin versions in the parent project and reversion just the parent POM.  Now your A type POM's only need to update their version number if they are to get the new dependencies.  For more information on Parent POM projects see below.

https://stackoverflow.com/questions/14400642/maven-parent-pom

The really bad thing (even if this would work) is that plugin A would have dependencies it does not really need.

But you see this isn't actually a bad thing!

```<code><dependency>
  ...
  <scope>provided</compile>
</dependency>
</code>```

Using the provided scope on a dependency to a B plugin is basically telling Maven that it is ONLY going to use this Artifact for compiling sources and unit testing.  It will ignore the dependency specifically during packaging of the build artifact with the assumption that the JDK or the application container will provide this for the plugin during runtime.

http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

If some of the dependencies aren't being used it won't matter.  They won't be bundled, they are only downloaded and used to compile and run test cases.

Body:
If I understand your situation clearly, it seems like you have many A type Maven artifacts, and these mostly all define a dependency to a few B type artifacts.

The A type artifacts are product of your development Group.  The B type artifacts that A depends on are from an external Group.  Your runtime environments for A type and B type Artifacts will have bundled into them the B Artifacts already.

The problem is that when the Version of a B Artifact changes on the server, you do not want to be hassled with manually updating many POM files with all of the dependency changes that happened in B initially.

Your first suggestion I think is the best approach:

I just thought about creating some kind of artifact, that contains all the dependencies that plugins like A could have.

You can absolutely do this and it is an accepted practice.  What you do is define a parent POM file that declares all of the dependencies an A type Artifact could need.  This parent POM file can be deployed to your Maven repository as it's own unique Group, Artifact and Versioned module. This parent POM module can then be referenced in your A type projects POM files using the <parent> element.  The benefit is that you can update all of the B type plugin versions in the parent project and reversion just the parent POM.  Now your A type POM's only need to update their version number if they are to get the new dependencies.  For more information on Parent POM projects see below.

https://stackoverflow.com/questions/14400642/maven-parent-pom

The really bad thing (even if this would work) is that plugin A would have dependencies it does not really need.

But you see this isn't actually a bad thing!

<dependency>
  ...
  <scope>provided</compile>
</dependency>

Using the provided scope on a dependency to a B plugin is basically telling Maven that it is ONLY going to use this Artifact for compiling sources and unit testing.  It will ignore the dependency specifically during packaging of the build artifact with the assumption that the JDK or the application container will provide this for the plugin during runtime.

http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

If some of the dependencies aren't being used it won't matter.  They won't be bundled, they are only downloaded and used to compile and run test cases.


问:
Why design a modern language without an exception-handling mechanism?
Many modern languages provide rich exception handling features, but Apple's Swift programming language does not provide an exception handling mechanism.

Steeped in exceptions as I am, I'm having trouble wrapping my mind around what this means. Swift has assertions, and of course return values; but I'm having trouble picturing how my exception-based way of thinking maps to a world without exceptions (and, for that matter, why such a world is desirable). Are there things I can't do in a language like Swift that I could do with exceptions? Do I gain something by losing exceptions?

How for example might I best express something like

```<code>try:
    operation_that_can_throw_ioerror()
except IOError:
    handle_the_exception_somehow()
else:
     # we don't want to catch the IOError if it's raised
    another_operation_that_can_throw_ioerror()
finally:
    something_we_always_need_to_do()
</code>```

in a language (Swift, for example) that lacks exception handling?

答:
There are some great answers here, but I think one important reason has not been emphasized enough: When exceptions occur, objects can be left in invalid states. If you can "catch" an exception, then your exception handler code will be able to access and work with those invalid objects. That is going to go horribly wrongly unless the code for those objects was written perfectly, which is very, very difficult to do.

For example, imagine implementing Vector. If someone instantiates your Vector with a set of objects, but an exception occurs during the initialization (perhaps, say, while copying your objects into the newly allocated memory), it is very hard to correctly code the Vector implementation in such a way that no memory is leaked. This short paper by Stroustroup covers the Vector example.

And that is merely the tip of the iceberg. What if, for example, you had copied over some of the elements, but not all of the elements? To implement a container like Vector correctly, you almost have to make every action you take reversible, so the whole operation is atomic (like a database transaction). This is complicated, and most applications get it wrong. And even when it is done correctly, it greatly complicates the process of implementing the container.

So some modern languages have decided it is not worth it. Rust, for example, does have exceptions, but they cannot be "caught," so there is no way for code to interact with objects in an invalid state.

ContentLicense: CC BY-SA 4.0

ContentLicense: CC BY-SA 3.0


问:
Parameterized tests - When and why do you use them?
Recently at work we've been having some differences of opinion with regard to Parameterized testing. Normally we use a TDD-style (or at least try to) so I understand the benefits of that approac. However, I'm struggling to see the gain parameterized tests bring. For reference, we work on a service and it's libraries which are exposed via a RESTful interface.

What I've seen so far is tests that are, at least using JUnit within Eclipse:

Lacking in detail - when a test fails it's very hard to see the parameters which caused it to fail

Often complicated to create

Tend to be created after the code has been written - strictly not a drawback as such but do people set out with parameterized tests in mind when they start a piece of code?

If anyone has any examples of where they are really useful or even any good hints for using them that would be fantastic. I want to make sure I'm not just being obstinate because I personally don't choose to use them and see whether they are something we should consider being part of our testing arsenal.

答:
Parameterized tests work well for testing functions/features that have simple input when you want to test a variety of input.

They don't work well for testing different functionality and complex input. They should not be used as a convenience structure to write less code.

ContentLicense: CC BY-SA 2.5


问:
How to save complex data to file/disk?
I am working on a side project for a small business owner (my father) and part of the requirements for the application include saving customer and job related data in a way that it can easily be backed up and/or restored on demand.

I am familiar with how to save data to a flat file, and I am familiar with how to save data to a database. What I feel like I am missing is what are "typical" strategies for saving data such as I described above to a file?

For example I noticed Quickbooks has their own file format that encapsulates an entire "business" in one file that can easily be backed up and/or used for restoration. (Briefly) How is that accomplished?

I have come across integrated databases such as sqlExpress and am left wondering if this  is how end user applications typically manage data or if it is something else entirely.

In short, how can I encapsulate a large amount of complex data into a single file either of a simple data type (i.e. .DAT/.TXT) or possibly a custom file format?

EDIT: As an added bit of information I would like to avoid having to install a stand-alone RDBMS in order to save data to a database.

答:
I am on mobile so bear with me.

This is a basic walk through of object serialization.

I think that there are pros and cons to either solution. You have to weigh them out and decide which is right for you, but I would go with a database. I would say my reasoning is quite arbitrary but this is exactly the purpose of a database. However...

If I were going to use object serialization and save to a file, and I wanted the single file approach, I would save everything in a big singleton. I'm not a expert though.

Title: How to save complex data to file/disk?


问:
Algorithm for a fast search
I am looking for an algorithm (pseudocode, R code) for my problem here. This looks like a lot to read but is actually very very easy to read, and I am just trying to be verbose.

I have a list (say coords) with 8 million elements. They look like this:

```<code>> head(coords)
  cstart  cend
1  35085 35094
2  35089 35098
3  35374 35383
4  35512 35521
5  35513 35522
6  35514 35523
...
98 12600309 12600318
</code>```

What these elements represent are coordinates on a DNA string. Ie, 35374 refers to the position on the DNA string. I don't care whether its a A C G T for what its worth. AND ALL coords are length 10. So the cend field can be calculated. (I don't actually use this cend in my code)

I have a secondary list (say alignments) (~100 elements) that also have a start and end coordinate. This defines are area of interest. It looks like this

```<code> chr_name       chr_strand      start_index        end_index
  "chr1"              "+"       "12600311"          "12600325"
</code>```

The columns chr_name, chr_strand is not important. In this case, our "area of interest" could be larger than 10.

My problem

Now, what I want to do is go through all 8 million records and see if each record is contained in the interests list (up to 80% overlap, I will explain this). For the above example,  the first 6 elements are nowhere "near" the area of interest.

Row 98 starts 2 "base pairs" before our area of interest, BUT overlaps the area of interest up to 80%. Ie, 80% of the "site" is contained in the area of interest.

Result

Basically, I am looking for a list (I can preallocate it), where each element is a TRUE/FALSE. So from above I'll have a list like the following

FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, ..., TRUE (97'th index)

My current R code
I have documented R code that is very easy to read

```<code>  ## mdply is a function that acts essentially like a loop
  ## the .data is the data I am working with.. the 8 million records
  ## for each row, it executes a function where the signature of the function
  ## matches the column names.
  ## the best part about this .. IS THAT ITS PARALLELIZED
  ## and I can use up to 24 cores to do this.
  b <- mdply(.data = coords,
             function(cstart, cend){

               #the function is executed for each row.
               #loop through our "area of interests"
               for(j in 1:nrow(alignments)){
                 chr_info <- extract_information_from_alignment(alignments[j, ])
                 interesting <- 0
                 uninteresting <- 0
                 hits_index_start <- cstart
                 hits_length <- 10 ## the length of our matrix
                 aoe_start <- as.numeric(chr_info[["start_index"]])
                 aoe_end <- chr_info[["end_index"]]

                 ## go through EACH INDEX AND CALCULATE THE OVERLAP
                 for(j in hits_index_start:(hits_index_start + hits_length-1)){
                   if((aoe_start <= j) && (j <= aoe_end)){
                     interesting <- interesting + 1
                   } else {
                     uninteresting <- uninteresting + 1
                   }
                 } #end for - interesting calc
                 substr_score = interesting/sum(interesting + uninteresting)
                 if(substr_score >= substr_score_threshold){
                   return(TRUE)
                 } else {
                   return(FALSE)
                 }
               } #end for
             }, .parallel = TRUE)
</code>```

I ran this code last night for 13 hours and it wasn't completed. I estimate it takes about 16 hours..

Ideas
I think the bottleneck is that for each row, it has a for loop (100 elements) and within that for loop, there is a second one that calculates the overlap.

But I am thinking, I don't need to go through EACH alignment. I should first test if my cstart even is close to the area of interest.

Maybe some sort of binary search?

答:
As you've noticed, nested loops can get to be expensive.  8 million iterations of the outer loop times 100 iterations of the middle loop times (I'm not sure) iterations of the inner loop leads to a lot of processing.

I think you're on the right track though - try to eliminate as many of those 8 million coords as possible before doing any processing on them.  Binary Search is definitely your friend.

The approach I'd take would look roughly like

```<code>sort the coords list // (done once, use a library function, happens fast)
create your output array, initialize all to 'false'
for each alignment in alignments // this loop should iterate 100x
  // the first alignment value that would fit your 80% coverage
  // would be alignment.start_index - 2 (since all coords are of length 10
  // and you need an 80% overlap)
  let searchStart = alignment.start_index - 2
  call a binary search function for searchStart in the coords list
  // you'll either find the first area of interest, or the location
  // in the list where that first one would go.

  // do the same for the search end (since the alignment appears to be variable length)
  // and because iterating over the list of coords from the starting point
  // would probably be slower
</code>```

I would expect this to run non-parallel fairly quickly - certainly not requiring hours.

All it's doing is calling binary Search on an 8 million entry list, 100 or 200 times (depending on if you search for the end_index or iterate to it).

Title: Algorithm for a fast search

Body:
As you've noticed, nested loops can get to be expensive.  8 million iterations of the outer loop times 100 iterations of the middle loop times (I'm not sure) iterations of the inner loop leads to a lot of processing.

I think you're on the right track though - try to eliminate as many of those 8 million coords as possible before doing any processing on them.  Binary Search is definitely your friend.

The approach I'd take would look roughly like

sort the coords list // (done once, use a library function, happens fast)
create your output array, initialize all to 'false'
for each alignment in alignments // this loop should iterate 100x
  // the first alignment value that would fit your 80% coverage
  // would be alignment.start_index - 2 (since all coords are of length 10
  // and you need an 80% overlap)
  let searchStart = alignment.start_index - 2
  call a binary search function for searchStart in the coords list
  // you'll either find the first area of interest, or the location
  // in the list where that first one would go.

  // do the same for the search end (since the alignment appears to be variable length)
  // and because iterating over the list of coords from the starting point
  // would probably be slower

I would expect this to run non-parallel fairly quickly - certainly not requiring hours.

All it's doing is calling binary Search on an 8 million entry list, 100 or 200 times (depending on if you search for the end_index or iterate to it).


答:
Depending on what kind of requirements you have for the application you're trying to build. (Maybe if you are a litle more specific I can help you more, otherwise is a too generic question).

There's many ways of saving complex data. For example, and naming only a few ways to do it, you can use a small database like SQL Compact or SQLite, or you can just use a normal file to save information. A csv file, a xml file or a json file to store date. There are many ways to do it, it all depends on what you are trying to save.
Is the amount of information big? Is the data somehow confidential? In every one of the refered examples you can chose the format that suits you better.

regards

Title: How to save complex data to file/disk?

Body:
I am working on a side project for a small business owner (my father) and part of the requirements for the application include saving customer and job related data in a way that it can easily be backed up and/or restored on demand.

I am familiar with how to save data to a flat file, and I am familiar with how to save data to a database. What I feel like I am missing is what are "typical" strategies for saving data such as I described above to a file?

For example I noticed Quickbooks has their own file format that encapsulates an entire "business" in one file that can easily be backed up and/or used for restoration. (Briefly) How is that accomplished?

I have come across integrated databases such as sqlExpress and am left wondering if this  is how end user applications typically manage data or if it is something else entirely.

In short, how can I encapsulate a large amount of complex data into a single file either of a simple data type (i.e. .DAT/.TXT) or possibly a custom file format?

EDIT: As an added bit of information I would like to avoid having to install a stand-alone RDBMS in order to save data to a database.


问:
What to do when time estimation goes wrong?
Let’s say you estimated time for a case to be 3 days.
In day two you notice that the case is growing and new scenarios are popping up which were not counted when the time estimation where done.  The new finding leads to 2 days extra (total 5 days). This is a typical problem that you will face sooner or later as developer.

Which strategy can be used when you are going to notify project leader the new time of delivery?

Often you get the question why? How do you motivate the new time of delivery?

The fact is that many projects don’t put much time on analysis & design during SDLC.

EDIT:
In very complex projects no matter how much reasonable time you put on Analysis & Design there are always surprises since the business rules are too complex. However in such cases I believe the project leader must be aware of the complexity and have right attitude when unexpected surprises comes up. The question is how to tackle project leaders who don’t understand the complexity.

答:
Delivering Bad News

You absolutely need to raise the matter promptly, however if you can do so in a reasonable timescale (that is a few hours, not more) you should do a bit of an impact assessment before you do.

As with all bad news it's best to provide detailed information (rather than just blurt out "it's going to be late") so provide as much / many of:

1) Revised estimates / timescales for the tasks which have slipped.

2) Revised estimates / timescales for future tasks which you now think, in light of knowing that some things have already over run, may take longer.

3) Very brief reasons why the slippage has occurred (don't spin, just the truth, but don't sound like you're making excuses).  In this case you state "We estimated based on rules X and Y but they've now included Z which was never mentioned".  He may be able to use this in explaining the delay to clients and educating them on the importance of being thorough in the first place.

4) If possible alternatives to bring things back on track (usually reducing scope but there may be other options - other parts of the project may be ahead of time and it may be possible to move tasks around).

Remember with slippages the psychological / credibility impact is culmulative.  You may be able to get away with one but the second one will be a lot tougher and the third on tougher still.

That's why point 2 is important - revise not just what's already slipped but also future tasks which you now think may take longer than originally anticipated.  Slipping happen in ITs, not learning from your mistakes is a bigger sin.

Preventing Having To Deliver Bad News

There are two scenarios here:  first, you didn't do the estimates yourself in which case there's not much you can do other than to push to be involved in the estimates next time round.

Second, you did do the estimates yourself in which case you need to look at how to do better estimates.  For me the key phrase in the question is "there are always surprises since the business rules are too complex".

With respect, if it always happens, it shouldn't be a surprise.  If you only ever get half of the business rules then you need to assume that in your estimates and allow for the feature creep.

You can either do this by increasing the estimates for the rules you do have (it works but you're not educating anyone as to what's really happening), but better is to state with your estimates "Historically the rules we get are a simplified version of what they really want.  The rules they've stated will take 3 days to implement, however we should allow another 3 days contingency for the rules that haven't been mentioned but are likely to be discovered during development and testing."

If the PM questions this then you need to remind him of all the times it's been true (with examples - it's hard to argue examples) and also gently suggest that it's in his interest to deliver on time as well as yours so isn't it better to be conservative?

But the bottom line: if you always underestimate because of a specific factor (in this case feature creep) then figure that into your estimates.


问:
Should the expected value be hard-coded into the Assert
My own personal preference would be to use a variable, but maybe there are reasons against this?  I haven't been able to find any resources that state the pros or cons of using one of these over the other.

Option 1:

```<code>[TestClass]
public PersonAgeTest
{
  [Test]
  public void GetFullNameWithPrefix_Parameterless_ReturnsValueWithPrefix
  {
    // Arrange
    var person = new person(Gender.Male);
    var person.LastName = "Philips";
    var expected = "Mr. Philips";

    // Act
    var actual = person.GetSalutation();

    // Assert
    Assert.That(actual, Is.EqualTo(expected));
  }
}
</code>```

Option 2:

```<code>[TestClass]
public PersonAgeTest
{
  [Test]
  public void GetFullNameWithPrefix_Parameterless_ReturnsValueWithPrefix
  {
    // Arrange
    var person = new person(Gender.Male);
    var person.LastName = "Philips";

    // Act
    var actual = person.GetSalutation();

    // Assert
    Assert.That(actual, Is.EqualTo("Mr. Philips"));
  }
}
</code>```

Maybe it's too minor of a difference?  I'm not sure, it seems better to ask.

答:
I personally prefer to use the [TestCase] where the input and expected outputs are all passed as arguments to the test function, whenever possible:

```<code>[TestClass]
public class PersonAgeTest
{
    [TestCase("Philips","Mr. Phillips")]
    public void GetFullNameWithPrefix_SimpleValue_ReturnsValueWithPrefix(string input, string expected)
      {
          // Arrange
          var person = new person(Gender.Male);
          var person.LastName = input;

          // Act
          var actual = person.GetSalutation();

          // Assert
          Assert.That(actual, Is.EqualTo(expected));
      }
}
</code>```

There is even a way to also do some sort of specified return value as your TestCase arguments in the case of NUnit, but I don't remember off the top of my head how that works or what it's syntax is.

In situations where you can't or it isn't appropriate to pass your inputs and outputs in as parameters, I would define them all up front so you can quickly eyeball what you are feeding to your test. I have been burned plenty of times debugging stuff due to a failing test case, only to find a typo in my input. Grouping all that together makes things easier to deal with.

Body:
I personally prefer to use the [TestCase] where the input and expected outputs are all passed as arguments to the test function, whenever possible:

[TestClass]
public class PersonAgeTest
{
    [TestCase("Philips","Mr. Phillips")]
    public void GetFullNameWithPrefix_SimpleValue_ReturnsValueWithPrefix(string input, string expected)
      {
          // Arrange
          var person = new person(Gender.Male);
          var person.LastName = input;

          // Act
          var actual = person.GetSalutation();

          // Assert
          Assert.That(actual, Is.EqualTo(expected));
      }
}

There is even a way to also do some sort of specified return value as your TestCase arguments in the case of NUnit, but I don't remember off the top of my head how that works or what it's syntax is.

In situations where you can't or it isn't appropriate to pass your inputs and outputs in as parameters, I would define them all up front so you can quickly eyeball what you are feeding to your test. I have been burned plenty of times debugging stuff due to a failing test case, only to find a typo in my input. Grouping all that together makes things easier to deal with.


问:
Multitenancy and nested set trees
The application I work on utilizes nested sets1 to represent tree structures within our database.

We need to expand a particular area of access control to support multiple clients who should not be able to see other client's information (aka multi-tenancy).

We have preferred the nested set approach over the more common, "naive" approach for a number of reasons as summarized in this presentation on slide 69.

At first glance, nothing within a nested set structure appears to be a problem that would prevent its use within a multi-tenant environment.  However, one of the concerns with nested sets is the number of updates required leaf nodes are added or removed.  For example, all entries to the right of the inserted leaf node end up needing their left and right values updated.

For the table in question, it will always remain relatively small (~5-10k rows, tops) even with multiple tenants.

We also have clients who will want additional access restrictions beyond those provided by ACLs expressed through a nested set tree.

In order to satisfy the client access restrictions and also to address potential database update concerns, we're considering adding a ClientId field to our nested set table.  The idea being that updates to one client's tree won't need to affect the trees of other clients.  Likewise, we'll have a hard block in place if Client A tries to inadvertently access Client B's data because the Client field won't match.

```
| RowId | ClientId | Left | Right | Foo... | Bar... |
|-------|----------|------|-------|--------|--------|
| 3     | 10       | 1    | 42    | ...    | ...    |
| 5     | 20       | 1    | 69    | ...    |        |
```

One complication that I have is that I have a set of users (call them "ClientId 0") who will perform actions for the other clients (like "Client 10", "Client 20").  And the actions that "Client 0" perform need to be visible / usable by the client they performed the action for.  So "Client 0" has to create things that will receive a different ClientId.2

Does this approach appear to be sound, or what should I be examining to understand if it will hold up as I explained above?

Alternatively, is there a superior data structure in order to represent multi-tenant, hierarchical information?

1Also see this slidedeck starting at slide 53 for additional details of implementing a nested set tree structure.

2My working approach to resolve this is to pull the ClientId from the node that Client 0 is working on, and assign the node's ClientId to the newly created node.

答:
Adding a "tenant" column (ClientId in this case), and using that to filter by tenant, is logically identical to having a separate database for each tenant. The only difference is, you have to remember to use that everywhere. This is true for everything in the database, not simply your nested sets. You are safe, assuming you are careful in writing the stored procedures.

Whatever advantages or disadvantages you found in the chosen hierarchy representation, will still be present even after making it a multi-tenant database. The only difference is, now you have a bit more work to do on each query.

Title: Multitenancy and nested set trees


问:
Design caching system
I am thinking about making my own caching system, mainly for C#, but this is more a design question, so the language doesn't really matter.

Let me start at the beginning, I have a basic web api, which uses the ETag-header to check if something changed.

This is the ICachingProvider, it is the base interface for all caching providers.

```<code>public interface ICachingProvider {
    // Writes a caching entry
    void WriteCachingEntry<T>(string uri, string md5, T data);
    // Reads a caching entry sync
    Tuple<string, T> ReadCachingEntry<T>(string uri);
    // Reads a caching entry async
    Task<Tuple<string, T>> ReadCachingEntryAsync<T>(string uri);
    // Clears the cache
    void ClearCache();
    // The current storage size
    long Size { get; }
    // How much can be stored maximum
    long MaxSize { get; }
}
</code>```

Currently, I have build a MemoryCahingProvider and a FileSystemCachingProvider where the last one is just storing JSON files in the isolated storage.

Now my plan is, to also implement a database caching system, I hope for better speed and less space use, maybe I am wrong with that.

To get to the point of the question, I am unsure which type of database I should use. First I thought of just a SQLlite database, but that would be not really efficient, cause I have more than one type of element, similar to the Facebook API. The next shot would be CouchDB or MongoDB, but I don't want to install more software on the client system than needed.

Does anybody have experience with that kind of cache or has someone an idea how to build something like it and with which database type?

答:
You might want to look around the System.Web.Caching documentation on MSDN.  There are a few good abstract ideas about Caching data that are relevant to any system including outside of a Website.

For example, the CacheDependency class:

Establishes a dependency relationship between an item stored in an ASP.NET application's Cache object and a file, cache key, an array of either, or another CacheDependency object. The CacheDependency class monitors the dependency relationships so that when any of them changes, the cached item will be automatically removed.

And although your cache sounds like it won't have anything to do with ASP.Net's cache, following this example in creating your own should be beneficial.

As for your ICachingProvider the OutputCacheProvider states:

Notes to Inheritors
  When you inherit from OutputCacheProvider, you must override the following members:

• Add

• Get

• Remove

• Set

I find it's good practice to see what Microsoft's created and the naming conventions they use for similiar object and then re-use those ideas for my own classes.  It's beneficial because other programmers who might use your classes and have any experience with similar classes from Microsoft should have an easier time implementing your solution.

The next shot would be CouchDB or MongoDB, but I don't want to install more software on the client system than needed.

If you're building a Database Cache, it shouldn't care what the backend is (unless that's where you are storing the cache, but then I'd asked how that would be any faster then a database in general).

Title: Design caching system


问:
C++ Templates where the type is a shared_ptr
When creating template classes in C++, if the type upon which the template will be specialized is intended to be a shared_ptr type, is it better to make T a shared_ptr type or make T a non-pointer type?

For example, which of the below classes is better, FirstExample or SecondExample?  Or alternatively, is this entirely context-dependent?  If so, can you an example of a context where FirstExample is better and a context where SecondExample is better?

In my case, clarity of usage is a higher priority than performance.

```<code>#include <iostream>
#include <memory>

using IntPtr = std::shared_ptr<int>;

template <class T>
class FirstExample
{

public:
    FirstExample( std::shared_ptr<T> value )
    :myData( value ) {}
    std::shared_ptr<T> getData() const { return myData; }

private:
    std::shared_ptr<T> myData;
};

template <class T>
class SecondExample
{

public:
    SecondExample( const T& value )
    :myData( value ) {}

    T getData() const { return myData; }

private:
    T myData;
};

int main(int argc, const char * argv[])
{
    FirstExample<int> firstInstance( std::make_shared<int>( 10 ) );
    IntPtr copyOfFirstInstanceData = firstInstance.getData();

    SecondExample<IntPtr> secondInstance( std::make_shared<int>(10) );
    IntPtr copyOfSecondInstanceData = secondInstance.getData();

    return 0;
}
</code>```

答:
Let's first adjust the user-defined ctors for equivalence:

Use pass-by-value and move-construction:

```<code>FirstExample( std::shared_ptr<T> value ) : myData(std::move(value)) {}
SecondExample( T value ) : myData(std::move(value)) {}
</code>```

Use pass-by-reference and copy-construction:

```<code>FirstExample( const std::shared_ptr<T>& value ) : myData(value) {}
SecondExample( const T& value ) : myData(value) {}
</code>```

Use pass-by-reference and move-/copy-construction:

```<code>FirstExample( const std::shared_ptr<T>& value ) : myData(value) {}
SecondExample( const T& value ) : myData(value) {}
FirstExample( std::shared_ptr<T>&& value ) : myData(std::move(value)) {}
SecondExample( T&& value ) : myData(std::move(value)) {}
</code>```

As an aside, I would change getData too, in order to avoid copyng where not needed:

```<code>const T& getData() const { return myData; }
const std::shared_ptr<T>& getData() const { return myData; }
</code>```

Now, ThirdExample behaves the same as FirstExample:

```<code>template<class T> using ThirdExample = SecondExample<std::shared_ptr<T>>;
</code>```

The advantage to using the first one is less typing and possibly better error-messages.

The advantage to using the second is more flexibility, as you can use another smart-pointer, as long as it offers the proper interface.

But why should you have to decide between either?

As ThirdExample demonstrates, you can implement the second example, and provide the first as a simple alias-template for convenience, just like the standard-library does for std::integer_sequence and std::index_sequence.

Body:
Let's first adjust the user-defined ctors for equivalence:

Use pass-by-value and move-construction:

FirstExample( std::shared_ptr<T> value ) : myData(std::move(value)) {}
SecondExample( T value ) : myData(std::move(value)) {}

Use pass-by-reference and copy-construction:

FirstExample( const std::shared_ptr<T>& value ) : myData(value) {}
SecondExample( const T& value ) : myData(value) {}

Use pass-by-reference and move-/copy-construction:

FirstExample( const std::shared_ptr<T>& value ) : myData(value) {}
SecondExample( const T& value ) : myData(value) {}
FirstExample( std::shared_ptr<T>&& value ) : myData(std::move(value)) {}
SecondExample( T&& value ) : myData(std::move(value)) {}

As an aside, I would change getData too, in order to avoid copyng where not needed:

const T& getData() const { return myData; }
const std::shared_ptr<T>& getData() const { return myData; }

Now, ThirdExample behaves the same as FirstExample:

template<class T> using ThirdExample = SecondExample<std::shared_ptr<T>>;

The advantage to using the first one is less typing and possibly better error-messages.

The advantage to using the second is more flexibility, as you can use another smart-pointer, as long as it offers the proper interface.

But why should you have to decide between either?

As ThirdExample demonstrates, you can implement the second example, and provide the first as a simple alias-template for convenience, just like the standard-library does for std::integer_sequence and std::index_sequence.


问:
Writing Unit Tests in the Middle
Is unit testing a 100% or not at all kind of deal?

I was browsing through my old projects and started adding features, this time with unit testing.  However, is this ultimately worthless if I'm going to be re-using past components that do not have unit tests?

Do I need to write unit tests for all the previous classes and not bother at all, or is it OK to only write unit tests for the new stuff I'm adding?

答: (for the lack of commenting ability) I think it is better than not to test at all. Not every snippet needs to be tested, but only what will be used by the programmer eventually. Testing the utilitiy functions that you use internally is good, but not required if you access everything through a clean API afterwards.</p>

Title: Writing Unit Tests in the Middle


问:
Is it bad form to break out of Range-based for
I have a range-based for that operates over a std::vector of objects (items), like so:

```<code>for(auto item : itemCollection)
{
   if(!item.some_check(...))
   {
      // do 5% stuff
      break;
   }

   // do the 95% stuff
}
</code>```

Most of the time (>95%) the loop operates on the entire container, however, if some_check() returns a false (about 5% of the time), it'll break out of the loop early.

Is it considered bad form to break out of a range-based for? Should I instead use a while or a C++03-style for(...;...;...) here?

答:
I'd at least be tempted to do something like:

```<code>auto pos = std::find_if(items.begin(), items.end(),
      [](item const &i) { return i.check(); });

std::foreach(items.begin(), pos, [](item const &i) { /* 95% stuff */ });

if (pos != items.end())
    pos->rare_stuff();
</code>```

This may (or may not) be marginally longer, but to me the intent seems more clear.

Body:
I'd at least be tempted to do something like:

auto pos = std::find_if(items.begin(), items.end(),
      [](item const &i) { return i.check(); });

std::foreach(items.begin(), pos, [](item const &i) { /* 95% stuff */ });

if (pos != items.end())
    pos->rare_stuff();

This may (or may not) be marginally longer, but to me the intent seems more clear.


问:
How can I use the "Non-Member Functions Improve Encapsulation" pattern from C#?
In 2000, Scott Meyers argued that non-member functions improve encapsulation.  In C++, a non-member function is a C-style global function:

http://www.drdobbs.com/cpp/how-non-member-functions-improve-encapsu/184401197

If you subscribe to his theory, how would you implement this in C#?

If you don't subscribe to his theory, and wish to debate the topic, please do it outside of this post (I suggest her: Feel free the comment here with regard to the actual pattern: --- https://plus.google.com/105106301233937725736/posts/QvhKbB3y7F2 .)

This question is about how to best implement that pattern in C#.  Acceptable answer describes a set of rules to use this pattern from C#

答:
Use the following rules:

For any non-static method of a class which a) does not access private fields directly and b) only calls public methods, move the method to a static helper class and turn it into an extension method.

Any public static method can also be moved to a helper class.

By convention, a helper is a new static class with the same name as the original class but with the word Helper appended to it.

Example:

ORIGINAL CODE:

```<code>public class Foo
{
    int _intField;

    public void Method1()
    {
        doing something with _intField;
    }

    public void Method2()
    {
        Method1();
    }

    public static void Method3()
    {
        ....
    }

    private void Method4()
    {
        ....
    }

    public void Method5()
    {
        Method4();
    }
}
</code>```

PROPOSED TRANSOFORMATION:

```<code>public class Foo
{
    int _intField;

    public void Method1()
    {
        doing something with _intField;
    }

    private void Method4()
    {
        ....
    }

    public void Method5()
    {
        Method4();
    }
}

public static class FooHelper
{
    public static void Method2(this Foo foo)
    {
        foo.Method1();
    }

    public static void Method3()
    {
        ....
    }
}
</code>```

Method 1 cannot move because it's using a private field

Method 2 can move because it's not using a private field and it's calling a public method.  It can be turned into an extension method.

Method 3 can be moved because it's a static public method

Method 4 cannot move because it's a private method

Method 5 cannot move because it's calling a private method.

Body:
Use the following rules:

For any non-static method of a class which a) does not access private fields directly and b) only calls public methods, move the method to a static helper class and turn it into an extension method.

Any public static method can also be moved to a helper class.

By convention, a helper is a new static class with the same name as the original class but with the word Helper appended to it.

Example:

ORIGINAL CODE:

public class Foo
{
    int _intField;

    public void Method1()
    {
        doing something with _intField;
    }

    public void Method2()
    {
        Method1();
    }

    public static void Method3()
    {
        ....
    }

    private void Method4()
    {
        ....
    }

    public void Method5()
    {
        Method4();
    }
}

PROPOSED TRANSOFORMATION:

public class Foo
{
    int _intField;

    public void Method1()
    {
        doing something with _intField;
    }

    private void Method4()
    {
        ....
    }

    public void Method5()
    {
        Method4();
    }
}

public static class FooHelper
{
    public static void Method2(this Foo foo)
    {
        foo.Method1();
    }

    public static void Method3()
    {
        ....
    }
}

Method 1 cannot move because it's using a private field

Method 2 can move because it's not using a private field and it's calling a public method.  It can be turned into an extension method.

Method 3 can be moved because it's a static public method

Method 4 cannot move because it's a private method

Method 5 cannot move because it's calling a private method.


问:
Is it better to call a function multiple times, or to assign a variable multiple times and call the function once?
Is it better to write

```<code>if (condition) {
    do_something(0);
} else if (other_condition) {
    do_something(1);
} else {
    do_something(2);
}
</code>```

or

```<code>int variable;
if (condition) {
    variable = 0;
} else if (other_condition) {
    variable = 1;
} else {
    variable = 2;
}
do_something(variable);
</code>```

?

I prefer the latter, because you only <s>call the function</s> write the function once, but it takes up more lines of code and will grow rapidly for functions with large numbers of arguments.

答:
I recommend separating the concerns of the do_something and the conditions.  There is some business logic that should be well named and encapsulated to determine what the do_something will operate on (ConditionType in my example below).  Use the resulting ConditionType as a parameter to do_something.

I read a lot of code (more than I write) and I'm not a big fan of putting as much code into one line as possible.  It's too easy to miss some crucial step when scanning the code.

Also, use constants, enums or your favorite stuc to avoid magic numers (0,1,2 in this case).

Here's a code sample:

```<code>   const int ConditionTypeZero = 0;
   const int ConditionTypeOne = 1;
   const int ConditionTypeDefault = 2;
</code>```

...

```<code>        var conditionType = GetConditionType(condition, otherCondition);
        do_something(conditionType);
</code>```

...

```<code>    private int GetConditionType(bool condition, bool otherCondition )
    {
        if (condition)
        {
            return ConditionTypeZero;
        }
        if (otherCondition)
        {
            return ConditionTypeOne;
        }
        return ConditionTypeDefault;
    }
</code>```

Body:
I recommend separating the concerns of the do_something and the conditions.  There is some business logic that should be well named and encapsulated to determine what the do_something will operate on (ConditionType in my example below).  Use the resulting ConditionType as a parameter to do_something.

I read a lot of code (more than I write) and I'm not a big fan of putting as much code into one line as possible.  It's too easy to miss some crucial step when scanning the code.

Also, use constants, enums or your favorite stuc to avoid magic numers (0,1,2 in this case).

Here's a code sample:

   const int ConditionTypeZero = 0;
   const int ConditionTypeOne = 1;
   const int ConditionTypeDefault = 2;

...

        var conditionType = GetConditionType(condition, otherCondition);
        do_something(conditionType);

...

    private int GetConditionType(bool condition, bool otherCondition )
    {
        if (condition)
        {
            return ConditionTypeZero;
        }
        if (otherCondition)
        {
            return ConditionTypeOne;
        }
        return ConditionTypeDefault;
    }


问:
Is it good practice to shorten functions?
Is it good practice to shorten functions? Like this code for instance:

```<code>      using System;
      namespace Hello
      {
          class Program
          {
                public static void Main(String[] args)
                {
                     Write("Hello, World!");
                     Stagger();
                }

                public static void Write(string s)
                {
                    Console.WriteLine(s);
                }

                public static void Stagger()
                {
                     Console.ReadLine();
                }
           }
      }
</code>```

I get lazy and do stuff like that a lot. But is it proper?

答:
If you do it to improve code readability, this is a good idea.

If you do it to type less, you probably do it wrong.

In your example, your methods don't improve readability; I would even assert that they add complexity and decrease readability:

What is Write? Is it writing something to a file? A log? Right, you should read how the method is implemented (since you don't even have XML comments) in order to know what is the purpose of the method.

What do Stagger means? Unless this term is used a lot in the application domain, I would avoid it: Console.ReadLine is much more explicit.

They might help you to type less, if you're writing your apps in Notepad. If you use Visual Studio, Intellisense will make those methods irrelevant. Not counting the fact that if you count using the official C# style, you would be writing Program.Write instead of just Write, which means that the number of keys pressed while using Intellisense will be exactly the same.


答:
Building on Franck's comment, you can use a DataSet to save to XML.

Writing DataSet Contents as XML Data

```<code>System.IO.StreamWriter xmlSW = new System.IO.StreamWriter("Customers.xml");

// This example assumes you have a custom dataset called ds holding customer data
ds.WriteXml(xmlSW, XmlWriteMode.WriteSchema);

xmlSW.Close();
</code>```

This will produce an XML file called Customers.xml in your current directory that is a serialized version of your DataSet.

You will also need to know how to load that data back into your objects...
Loading a DataSet from XML

```<code>// Load the dataset from a file called input.xml
DataSet ds = new DataSet();
ds.ReadXml("Customers.xml", XmlReadMode.ReadSchema);
</code>```

Title: How to save complex data to file/disk?

Body:
Building on Franck's comment, you can use a DataSet to save to XML.

Writing DataSet Contents as XML Data

System.IO.StreamWriter xmlSW = new System.IO.StreamWriter("Customers.xml");

// This example assumes you have a custom dataset called ds holding customer data
ds.WriteXml(xmlSW, XmlWriteMode.WriteSchema);

xmlSW.Close();

This will produce an XML file called Customers.xml in your current directory that is a serialized version of your DataSet.

You will also need to know how to load that data back into your objects...
Loading a DataSet from XML

// Load the dataset from a file called input.xml
DataSet ds = new DataSet();
ds.ReadXml("Customers.xml", XmlReadMode.ReadSchema);


问:
Should naming conventions be consistent or not across multiple programming languages in one project?
I saw this question on SO: here; the question is kind of old and not many viewed, so I want to ask it again.

I currently work on a project using C++, C# and Actionscript-3, the conventions of C++ and C# are similar, but AS3 is not.

For example, the function name conventions differ a lot, it's common to use GetFoo() in C++ but getFoo() in AS3.

According to the SO question's answer, it suggests that using different convention for different languages is more appropriate, which is also my opinion; I'd like to just accept it, but it's kind of too brief; to persuade my team members, I'd like to look for more detailed pros and cons.

答:
Coding standards should be specific to the particulars of each language and platform. An idiom that is considered essential in C++ might be superfluous for another language such as C#. For example, you might insist of always placing a constant in an 'if' expression on the left (5 = x) to reduce the chances of accidentally having a single equals accepted as good code by the C++ compiler. In C# this is not relevant because the expression must return a boolean and so you can never accidentally have a single equals.

Adding extra conventions just to make the languages consistent means each language will end up with meaningless rules that are not relevant and therefore will annoy the developers.

It is also important to use standards that are likely to be familiar to other programmers in the same community but not working at your company. New recruits should not be completely surprised by the coding standard because it is so different from everyone else in industry.


问:
DDD Validation best practice: entity containing data doesn't have the context to validate before data is set. Where should set function be?
I am pretty new to DDD, so bear with me.

I have a Chat room and people in the Chat room. The people in the chat room need to propose a date they would like a webinar to be on.
we have a bussiness rule, where we need to have at least 3 people in the webinar for the webinar to take place and each person interested in the webinar needs to say that they are ready for someone to propose a date

I have 2 solutions in mind

solution 1:

Making Chat room the aggregate root

Have a function propose date on the Chat room, ProposeDate(Person, Date).

In this solution I would validate the bussiness rules on the Chat room and set the date on person entity.

An issue With this solution is that we have a setter on the person entity that can be called from anywhere in the domain without
the reference to the validation in the chat room, should the proposed date for each person actually be removed from the person entity and be set on the chat room itself so it cannot be set wihout calling chat room entity?

solution 2

Make person the aggregate root

On Person have ProposeDate(Date).and person would ask Chat room can i propose this date.
The problem i have with this solution is I believe it invalidates the tell don't ask principle.

If i have it all wrong and there is another solution please let me know.

The people have to propose/accept/reject that date dates to each other.

答:
When you have a situation like this where two objects seem interdependent, sometimes the solution is to refactor a dependency into third object. In this case why don't you have WebinarVote object with propose(ChatRoom, Person) method?

After the voting period is over, your Webinar (or whatever it is called now in your architecture) service object will iterate WebinarVotes to determine which webinars take place and which are not.


答:
Please, use the standard conventions for each language (or somewhat very close to the standard).

More consistency in each piece of software itself (because it will have the conventions that really apply)

More ease for additional (external) fellow developers. They must not learn "inconsistent" styles.

Easier comparison/review/transfer of code out of / into examples in the "world outside"

No regrets later, see below

Here's what I experienced:

I am currently working in a shop where they first did Java-only projects. Then, they started to do C#/.NET and applied the already-well-known Java styles/Naming conventions to those C#/.NET projects.

When I (and others) joined the team, after doing C# for years, I was really annoyed with how the code looked like and had a hard time to continue with the perceivedly awkward Java-style in the C#/.NET projects.

Later they figured, that having a "wrong" style is bad, and started to adopt the C#/.NET conventions in new C#/.NET projects. Now we have really mixed up styles - sometimes different within a platform, sometimes different in the projects. Hopefully, over time, we will write less Java-styled C# code, but in my perspective, it would have been better to never start out doing that at all.

Bottom line:

What you could do

Consistent brace rules (Opening one on new line / same line)

Consistent commenting rules (e.g. XML only)

Rules about code quality (length of methods, unit testing frequency etc..

Use a tool to help applying the above

What you really should keep per language / platform

Namespaces

Casing of identifiers

Naming styles for identifiers

Title: Should naming conventions be consistent or not across multiple programming languages in one project?

Body:
I saw this question on SO: here; the question is kind of old and not many viewed, so I want to ask it again.

I currently work on a project using C++, C# and Actionscript-3, the conventions of C++ and C# are similar, but AS3 is not.

For example, the function name conventions differ a lot, it's common to use GetFoo() in C++ but getFoo() in AS3.

According to the SO question's answer, it suggests that using different convention for different languages is more appropriate, which is also my opinion; I'd like to just accept it, but it's kind of too brief; to persuade my team members, I'd like to look for more detailed pros and cons.


问:
Design pattern where decorated object may ask for things like "status updates" from the decorator to perform it's role?
Here's an situation that has haunted one of my open-source projects for quite some time.

Imagine if you could represent every front-end input form with a single backend object? An object that contains properties and anonymous functions (for bits of logic...such as perhaps a call to a database repository to query and bring in all the values for your drop-down, etc). The class would be very basic, but would be decorated by a decorator that would add all the features you need. The decorator will take each decorated client (the class that represents your form) and provide features that make things like "input cascading", "input ID prefixing to avoid name collisions", "XSS protection", etc. a breeze! Giving your HTML input some backend server-side functionality will now be easy!

However, what concerns me is that in some cases the decorated class needs to know some status from its decorator (ack! Code Smell? Coupling?). What I did to get around that is to provide extra properties in a Factory class for providing status. See simple (but long enough to be "real world") example below:

Decorator

```<code>Class InputDecorator{
    private $client;

    // Inject the client object to decorate
    public function __construct(InputInterface $client){
         $this->client = $client;
         $this->client->input = $this->client->setupInput();
    }

   // Perform an Input cascade
   public function cascade($inputName){
         foreach($this->client->input[$inputName]['children'] as $child){
              $this->client->input[$child]->cascadeStatus = true;   // <-- Set status here
              $this->outputAJAX($child);
         }
   }
   // Return HTML content
   public function outputHTML(){
        foreach($this->client->input as $input){
               $dropDownValues = $input['drop-down-values'];
               // take the dropdown set of values and render <select><option...
        }
    }
    // Return Script(JQuery) content
    public function outputAJAX($inputName){
        $dropDownValues = $this->client->input[$inputName]['drop-down-values'];
        // take the dropdown values and render $(input-name-here).html('<option...
    }

    // overloading magic methods here (typical of decorator pattern)
}
</code>```

Decorated class (there will be lots of these, each representing its own input form)

```<code>Class InputExample implements InputInterface{

    public $input = array();

    public function setupInput(){
        $_this = $this; (for php 5.3 reasons)

        $this->input['states']                     = InputFactory::make("select");
        $this->input['states']->class              = "some CSS class";
        $this->input['states']->style              = "Inline styles are bad!";
        $this->input['states']->children           = array("cities");
        $this->input['states']->values             = function() use ($_this){

              // ORM->query for list of states
              // return array of states
        }

        $this->input['cities']                     = InputFactory::make("select");
        $this->input['cities']->class              = "";
        $this->input['cities']->style              = "";
        $this->input['cities']->values             = function() use ($_this){

              if($_this->input['cities']->cascadeStatus == true)  // <-- status check
                   // return array of cities in the selected state
              else
                   // return array()
        }
    }
</code>```

}

Factory class

```<code>class InputFactory implements InputFactoryInterface{

    public static function make($type)
    {
        if ($type == "select") {
            return new InputTypes\Select($id);  //<-- cascadeStatus property is defined in here
        } elseif ($type == "text") {
            return new InputTypes\Text($id);
        } elseif ($type == "checkbox") {
            return new InputTypes\CheckBox($id, "hidden");
        }
        // Room to grow...
    }
}
</code>```

Investigate my use of cascadeStatus, which is the status the Decorator sets to true. The decorated client class needs to know about this status to perform a decision in its anonymous function. This whole solution acts as sort of a Decorator + Mediator/Factory pattern. Is this the correct approach and/or use of these patterns?

答:
I believe what you are trying to do is to reinvent Symfony Forms and add some bunch of ORM to them ;)

They indeed contain all the data from the form and all the form HTML structure described in a very declarative way with a possibility of extension.

But I think you are wrong when you want forms to be able to query backend data. This is incorrect.

Form should not know how to actually query data. You violate SRP here. Form is only for handling input from the user and, may be, validating it (but actual validation should be performed by means of a separate validator class)

What you should do is to query data using Services or Repositories (depending on your architecture complexity) in Controllers and then create Forms using that data. When user submits form, you validate user data there and then again call Service (or Repository) to store that data into the backend.


问:
Why is the "period of a (pseudo)random number generator" important?
I've been trying to understand (pseudo)random number generator code from various sources and the concept of the period continues to elude me.  To satisfy the minimum level of understanding, I've tried to think of it in terms of "sample size" from statistics.

However, I believe my understanding may be overly simplistic.  The documentation I've found for functions/calls that allow (or require) a period indicate the value is important but fall flat in explaining why...

Where it is possible for a programmer to set the period for a (pseudo)random number generator, what defines a good (optimal or desirable) period and why?

Additionally (for focused understanding):
 - Is it important (or helpful) to define the period using a prime number?
 - Does a large period or a small period produce a more desirable output?

答:
Take your favorite program using some PRNG.

Then imagine what would happen if the period of the PRNG becomes very small (e.g. 100).

Of course it depends upon the program, but if you use the weak PRNG for encryption, an attacker could easily notice the periodicity, and eventually be able to decrypt secret messages. If you use the weak PRNG for Monte Carlo simulation, running your simulation for a long time won't teach you anything more than running it for a short time, etc.

I am not a PRNG expert, but AFAIK the period is usually not some tunable parameter. However, you can choose your PRNG, and some PRNG give (in their documentation) some promises (or expectations) about their period. Usually a PRNG with a longer period has a bigger state, so is slower. So you have a tradeoff to make.


问:
How can i get around of infinite circle of technology change
I have been in web developement for 6 years. I am facing one big problem and i want to know how can i get rid of it.

There are many personal projects like web application which i want o build.

As an example 4 years ago

I was working in joomla and i started my web application also in joomla on shared hoisting

Then i chnaged my job where i need to build site in Symfony. After few months i thought symfony is better and then it started by application to be done in symfony.

I was in midway and then my company got project in python django on own server. After working on django i thought that to be cool idea because after working on django for few months i forgot about symfony stuff. i got new VPS and started web application there.

Then i chnaged my job where there were big teams and proper procedure for django applications . what i did was very bad as compared to new companies. so thought to redo that way

5.At that time i reached 70% doing that way. Then my contract finished and i joined new company where there were proper coding standards, design patterns and TDD. so i again started it from scratch to be like that.

Now i joined new company where everyone is using angular and REST and again i am thinking to redo in REST way.

I am totally confused what to do. There is endless loop there.
I find that applying newly learn techniques greatly help in learning those things but my personal project is never  going to complete this way.

The otehr thing is if we know that good of way of writing code then its very hard to keep a maintain the old code and adding new features loks nighmare when you know what u r doing is not maintainable.

I want to know what to do.

Suppose if i start one project then should i finish it that way even though i did find new things or i need to refactor.

答:
Yes, and its a hindrance to the industry. This continual churn of technologies only serves to help people who create them and those who sell training courses. So Microsoft is happy as it gets to sell new tools every few years and we all have to buy them because we've been indoctrinated to "stay current".

Whilst some change is necessary, IT seems to be the only industry where its so continual.

What you can do to mitigate this is to learn fundamental skills. Understand how computers, networks and databases work and then treat all implementations, whether it be using Sympony, PHP, Joomla or javascript or whatever as just different wrappers to the same underlying things. Forget technology as a thing in itself, and focus on using these different technologies as just another tool to produce products. IMHO once you start to concentrate on what you're building and not the bricks you're using to build then you get a lot more satisfaction from the job.

This attitude also helps you build better products. Once you know the tools don't really matter you start choosing the right one for each job. A lot of people who learn some technology today think that's the only one to use regardless of the task in hand.


答: You can start covering your current code and, if you have some time to spend, start covering core functionality of old code. Also you can ask your PM for some extra time for that=)</p>

Title: Writing Unit Tests in the Middle

Body:
Is unit testing a 100% or not at all kind of deal?

I was browsing through my old projects and started adding features, this time with unit testing.  However, is this ultimately worthless if I'm going to be re-using past components that do not have unit tests?

Do I need to write unit tests for all the previous classes and not bother at all, or is it OK to only write unit tests for the new stuff I'm adding?


答:
It's not you. It is something that a lot of developers struggle with and it is something you learn to live with as a developer. As soon as your code is typed it will probably be outdated.

Here's a few tips on how to deal with that...

Building something in a new framework/library/language is the best way to learn

A lot of developers have projects 'on the side', either serious projects that are done in their spare time and published or throwaways that just serve to learn a particular framework or library or language. I can't count the times I've started a very simple application just to get the feel for a specific language or framework. If you want to get up to speed with some language or framework, build an application that you plan to throw away first to get a feel for it before committing to it. There is nothing wrong with rebuilding the same application over and over if you want to learn a new thing. Ever wondered why there are so many 'lets build a blogging application' tutorials out there for frameworks?

Picking a framework/library/language means picking and sticking with its paradigms

If you pick an OO language, you know you will need to develop using OO techniques. If you pick Joomla, you know you will need to follow the development guidelines for Joomla. There will always be new techniques and new frameworks, but unless there is a VERY good reason you should not constantly rework your application in these new languages or frameworks. You simply can't win because there will always be new things going on. 'Good enough' beats 'perfect' in software development any day as 'good enough' software is out there being used (and potentially making money) and 'perfect' software is constantly being reworked but never used.

There are no bad frameworks/libraries/languages

Every framework has its advantages and disadvantages. In that sense there are no bad frameworks, only frameworks that are not to your liking. I'm a big fan of TDD/BDD/DDD and I much prefer the tools and the structure that Symfony gives me which allows me to write the kind of code I like the way I like it. Your tastes will grow and evolve and there is nothing wrong with rewriting an old application to a new framework, but only if you can afford to stop developing new features for a while.

Ultimately, it's up to you

Personally, I have a number of projects where, if I were to start over, I would probably pick a different framework. Even things that are only two years old sometimes. But they are actively being used, there are no problems that are related to the framework or language I picked, I can still add features pretty quickly and I've got enough other things to do so it's not something I think about.

Title: How can i get around of infinite circle of technology change

Body:
I have been in web developement for 6 years. I am facing one big problem and i want to know how can i get rid of it.

There are many personal projects like web application which i want o build.

As an example 4 years ago

I was working in joomla and i started my web application also in joomla on shared hoisting

Then i chnaged my job where i need to build site in Symfony. After few months i thought symfony is better and then it started by application to be done in symfony.

I was in midway and then my company got project in python django on own server. After working on django i thought that to be cool idea because after working on django for few months i forgot about symfony stuff. i got new VPS and started web application there.

Then i chnaged my job where there were big teams and proper procedure for django applications . what i did was very bad as compared to new companies. so thought to redo that way

5.At that time i reached 70% doing that way. Then my contract finished and i joined new company where there were proper coding standards, design patterns and TDD. so i again started it from scratch to be like that.

Now i joined new company where everyone is using angular and REST and again i am thinking to redo in REST way.

I am totally confused what to do. There is endless loop there.
I find that applying newly learn techniques greatly help in learning those things but my personal project is never  going to complete this way.

The otehr thing is if we know that good of way of writing code then its very hard to keep a maintain the old code and adding new features loks nighmare when you know what u r doing is not maintainable.

I want to know what to do.

Suppose if i start one project then should i finish it that way even though i did find new things or i need to refactor.


问:
Is set of libraries called API only if it talks to a system/server?
I have read few articles about API. Can't find the reference now.

In some of them, they define API as set of tools, routines and protocols which are helping programmer to build a software with use of pre-programmed methods.

In others they say the same thing, but they also say that API is only when these methods are communicating with some external systems/services.

Which one is right? I just want to know, that if I use library to draw in Java, is that called API because it contains pre-programmed methods or that is just library/component?

答:
An API stands for "Application Programming Interface". It means any interface between the system, library or device and your development.

So, no. An API is any programmatic interface, whether its a library with exposed C functions or a webserver with exposed REST verbs or anything in between.


问:
How can I ask my boss (in a polite way) to comment his code?
I am being taught by my boss (I just finished school and he wanted someone with a little programming experience, so he chose me to train me on what that company specializes in) and started working with ASP.NET MVC applications, some HTML and CSS. I'm fine with the web design stuff he gives me (it is pretty simple to understand without clarification).

But for instance, he gives me a task to do with ASP.NET MVC, he explains it really well. But he doesn't explain anything in the code he has just given me. (We use source control in Visual Studio 2013), so it's literally hundreds of lines of code, without any background on what it is supposed to do. The kind of code that I'm seeing is code I've never seen before, so it is really difficult to try and figure out.

I would try and ask him more questions, but he is always working (it's his own business), and I feel as though he might get annoyed with all these questions I have on my hands.

So just something that will help my out until I get a grip on things, how can I ask my boss to put comments into his code that he gives me, but politely?

答:
Here are my $0.02 on the matter. I'm not suggesting an exclusive answer, a lot of what has been said here is quite relevant.

I would try a bit of social engineering to arrange things so that your boss finds it easier/less time-consuming to comment some of his code than not to.

Now, this can be pretty easy if you were willing to take a big risk and annoy him - but we don't want to do that. (side note: You could just fail to be able to do anything without him writing down or dictating comments to yo, you insisting and pestering him about it endlessly etc.)

What's the alternative, then? A few ideas, depending on the circumstance.

Option 1

Take the time to understand a piece of his code as doing X.

Now think of a reasonable way Y to misunderstand it.

Tell boss (via email or say over breakfast or what-not) that you're currently trying to figure it out.

Add a comment saying it's not clear what the code means , but you understand it as Y; try to make this comment visible to him - but don't try too hard!

Act assuming Y - and make sure your boss notices your action (so you don't waste your time work for a long time on a false assumption).

Boss should take the initiative to correct you. At this point, tell him something like "I really wish this code had a couple of comments to prevent me making the wrong assumption. I'll correct the comment I added for myself. Do you suppose you could help me out with some general description of this-and-that pieces of code? I'm not experienced enough to figure out the exact intention, and I'm just a couple of sentences would do the trick." Or something..

Option 2

You're in training. Try to arrange for an (additional?) fixed-frequency weekly meeting with him. At this meeting go over some code - but you need to come prepared enough for him not to have to explain every single line. At some point - hopefully - he'll realize he can skip the meeting if he just added the comments.

Option 3

Get another co-worker to fail to understand the same piece of code as you. Both of you approach the boss at different times asking the same questions. That's a sure-fire way to make him realize he's failing to do something... but not everyone has the luxury of helpful coworkers on the same project.


问:
Is throwing an exception an anti-pattern here?
I just had a discussion over a design choice after a code review. I wonder what your opinions are.

There's this Preferences class, which is a bucket for key-value pairs. Null values are legal (that's important). We expect that certain values may not be saved yet, and we want to handle these cases automatically by initializing them with predefined default value when requested.

The discussed solution used following pattern (NOTE: this is not the actual code, obviously - it's simplified for illustrative purposes):

```<code>public class Preferences {
    // null values are legal
    private Map<String, String> valuesFromDatabase;

    private static Map<String, String> defaultValues;

    class KeyNotFoundException extends Exception {
    }

    public String getByKey(String key) {
        try {
            return getValueByKey(key);
        } catch (KeyNotFoundException e) {
            String defaultValue = defaultValues.get(key);
            valuesFromDatabase.put(key, defaultvalue);
            return defaultValue;
        }
    }

    private String getValueByKey(String key) throws KeyNotFoundException {
        if (valuesFromDatabase.containsKey(key)) {
            return valuesFromDatabase.get(key);
        } else {
            throw new KeyNotFoundException();
        }
    }
}
</code>```

It was criticized as an anti-pattern - abusing exceptions to control the flow. KeyNotFoundException - brought to life for that one use case only - is never going to be seen out of the scope of this class.

It's essentially two methods playing fetch with it merely to communicate something to each other.

The key not being present in the database isn't something alarming, or exceptional - we expect this to occur whenever a new preference setting is added, hence the mechanism that gracefully initializes it with a default value if needed.

The counterargument was that getValueByKey - the private method - as defined right now has no natural way of informing the public method about both the value, and whether the key was there. (If it wasn't, it has to be added so that the value can be updated).

Returning null would be ambiguous, since null is a perfectly legal value, so there's no telling whether it meant that the key wasn't there, or if there was a null.

getValueByKey would have to return some sort of a Tuple<Boolean, String>, the bool set to true if the key is already there, so that we can distinguish between (true, null) and (false, null). (An out parameter could be used in C#, but that's Java).

Is this a nicer alternative? Yes, you'd have to define some single-use class to the effect of Tuple<Boolean, String>, but then we're getting rid of KeyNotFoundException, so that kind of balances out. We're also avoiding the overhead of handling an exception, although it's not significant in practical terms - there are no performance considerations to speak of, it's a client app and it's not like user preferences will be retrieved millions of times per second.

A variation of this approach could use Guava's Optional<String> (Guava is already used throughout the project) instead of some custom Tuple<Boolean, String>, and then we can differentiate between Optional.<String>absent() and "proper" null. It still feels hackish, though, for reasons that are easy to see - introducing two levels of "nullness" seem to abuse the concept that stood behind creating Optionals in the first place.

Another option would be to explicitly check whether the key exists (add a boolean containsKey(String key) method and call getValueByKey only if we have already asserted that it exists).

Finally, one could also inline the private method, but the actual getByKey is somewhat more complex than my code sample, thus inlining would make it look quite nasty.

I may be splitting hairs here, but I'm curious what you would bet on to be closest to best practice in this case. I didn't find an answer in Oracle's or Google's style guides.

Is using exceptions like in the code sample an anti-pattern, or is it acceptable given that alternatives aren't very clean, either? If it is, under what circumstances would it be fine? And vice versa?

答:
I know I am late to the party, but anyways your use case resembles how Java's Properties lets one define a set of default properties too, which will be checked if there is no corresponding key loaded by the instance.

Looking at how the implementation is done for Properties.getProperty(String) (from Java 7):

```<code>Object oval = super.get(key);
String sval = (oval instanceof String) ? (String)oval : null;
return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;
</code>```

There really isn't a need to "abuse exceptions to control the flow", as you have quoted.

A similar, but slightly more terse, approach to @BЈовић's answer can also be:

```<code>public String getByKey(String key) {
    if (!valuesFromDatabase.containsKey(key)) {
        valuesFromDatabase.put(key, defaultValues.get(key));
    }
    return valuesFromDatabase.get(key);
}
</code>```

Body:
I know I am late to the party, but anyways your use case resembles how Java's Properties lets one define a set of default properties too, which will be checked if there is no corresponding key loaded by the instance.

Looking at how the implementation is done for Properties.getProperty(String) (from Java 7):

Object oval = super.get(key);
String sval = (oval instanceof String) ? (String)oval : null;
return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;

There really isn't a need to "abuse exceptions to control the flow", as you have quoted.

A similar, but slightly more terse, approach to @BЈовић's answer can also be:

public String getByKey(String key) {
    if (!valuesFromDatabase.containsKey(key)) {
        valuesFromDatabase.put(key, defaultValues.get(key));
    }
    return valuesFromDatabase.get(key);
}


答:
Computers are not Turing Machines. They are Deterministic Finite State Machines.

Turing Machines have infinite memory, computers have finite memory. Turing Machines have arbitrarily many (though finite) states, computers can't have arbitrarily many states, the number of different states that a computer can be in is bounded by its memory (a computer with 1 KiByte of memory and 4 8-bit registers can only be in 8*21024 + 4*28 =~ 10300 distinct states).

The program itself is also stored in memory, therefore it is itself part of the state. Any program which runs long enough must at some point end up in a state in which it was once before. And since the program itself is part of the state and computers are deterministic, this means that it will do the exact same thing it did the last time it was in this state, thus it will end up in the same followup-state it ended up the last time, which means that it will now do the same thing it did … and so on. Any programs which runs long enough will at some point end up in a state it was in before and from that point on run in exactly the same way it did the last time.

This length of steps when the program starts to repeat itself is called the period. Every program has one, not just Pseudo-Random Number Generators.

Note that mostly, those periods are theoretical. Even if the 1 KiByte computer I mentioned above had a 5 THz CPU, it would still take 10270 times the current age of the universe to cycle through 10300 states.

However, most programs, and certainly most PRNGs don't use the entire memory of the computer just for their own state. (PRNGs in particular are usually embedded into much larger programs and thus need to "steal" their memory from them.) Plus, programs don't cycle through all possible states perfectly, typically, they can only be in a much smaller number of states. The period length can never be longer than the number of internal states, and is typically shorter. For a badly designed PRNG, it is way shorter.

The RANDU PRNG that was used on the IBM System/360 in the Scientific Subroutine Library and ported to other mainframes was used for decades in research, simulations, statistics and so on, and it has (among other major flaws, e.g. it can only ever generate odd integers, never an even one) a period of just 229. Which means that after about only 10 billion steps you know exactly what number is going to come next. (It takes only about 40 GiByte to store the entire sequence!) If you play online black jack, you know all the cards that are coming, if you play online roulette, you know what number is going to come next, and so on.

Where it is possible for a programmer to set the period for a (pseudo)random number generator

Generally, it isn't. The period length is a property of the algorithm. There are some algorithms where you can choose the size of the internal state, which influences the length of the period.

what defines a good (optimal or desirable) period

Long. The longer the better.

How long? Well, cynically speaking: long enough that buying enough harddisks to store it will be more expensive than attacking some other part of the system (e.g. bribing the casino's datacenters janitor to let you in and install a backdoor on the server which sends you the player's cards).

and why?

I hope that's obvious now. A PRNG with a small period (say 5) will give the same 5 numbers over and over again. That's not very "random".

Title: Why is the "period of a (pseudo)random number generator" important?

Body:
I've been trying to understand (pseudo)random number generator code from various sources and the concept of the period continues to elude me.  To satisfy the minimum level of understanding, I've tried to think of it in terms of "sample size" from statistics.

However, I believe my understanding may be overly simplistic.  The documentation I've found for functions/calls that allow (or require) a period indicate the value is important but fall flat in explaining why...

Where it is possible for a programmer to set the period for a (pseudo)random number generator, what defines a good (optimal or desirable) period and why?

Additionally (for focused understanding):
 - Is it important (or helpful) to define the period using a prime number?
 - Does a large period or a small period produce a more desirable output?


答: If the old stuff has been working fine for years, creating the unit tests now is not mandatory unless you change something in the old stuff. Anyway, writing unit tests for the new parts is not at all pointless. The new parts are the ones most likely to contain bugs, and they are also the parts most likely to be changed or refactored.</p>

Title: Writing Unit Tests in the Middle


问:
Separation of logic from presentation results in repetition of code
Here's what I've been thinking.

Say you're making a multiplayer game. A good way to structure it is to have all your game logic on the server and have the clients only be responsible for transmitting input to the server, drawing the current state of the game and UI.
This usually leads to clean code and it also prevents client-side cheating.

Now say you have a unit in your game that fires a cannon with a certain cooldown. The reason it can't fire continuously is because it has to reload. This is represented by an animation. But the server also needs to be aware of the cooldown so you end up having to sync up the animation duration and the cooldown that the server understands. In this simple example you can just speed up or slow down the animation if you decide to change the cooldown, but there are definitely processes where it would be more reasonable to animate them and then have the game values depend on the actual animation.

Is there a smart way to avoid this? It seems that it puts a significant load on the programmer because he has to manually make sure that two decoupled pieces of code, that would probably be better of coupled, are in sync. If you didn't separate the presentation from logic you would just hook directly into the animation and have it trigger actions in the game.

答:
If you are doing multiplayer games, the timing of various actions must be well-thought out. Take a look at World of Warcraft, Diablo 3, League Of Legends... People create spreadsheets with rows and rows of data and calculations to determine the most effective skill combination for their purpose. And game designers balance the game accordingly.

In some cases games will switch the order in which effects are applied on a skill in the game because it is completely overpowered. For example, think of a skill that lowers armor and deals damage. The order in which this happens can make a HUGE difference as first lowering the armor would cause more damage to be dealt.

Big multiplayer games have people who deal with balancing issues and specifically look into timings. I would think that the timings are programmed into the game and animations need to match the timing. The "Feel" of the designer for animations is important, but ultimately balance is a matter of numbers and therefore the realm of the programmer.


问:
What are the benefits of using database abstraction by ORM?
I'm starting to use the ORM recommended by the framework I choose, and though I like the idea of the added layer of abstraction the ORM provides, I'm starting to realize what this really means. It means I'm no longer working with my database (mysql) and any mysql-specific features are gone, out the window, like they don't exist.

The idea the ORM has is that it's trying to help me by making everything database agnostic. This sounds great, but often there is a reason why I choose a specific database system. But by going the database agnostic route, the ORM takes the lowest common denominator, which means I end up with the smallest set of features (the ones supported by all databases).

What if I know that for the long term I won't be switching the underlying database? Why not access the database-specific features, too?

答:
My (simple) two cents:

1:
There are ORMS and there are ORMS. Some ORMS are based on Active Record, others based on Data Mapper - that in itself is a relevant consideration, but one that requires some investigation to understand the implications. In general - my experience in PHP is that ORMS support the former, few support the latter.
Active Record based ORMs seem to have one of two effects - they either de-normalise the database, or invoke a plethora of classes to support object interactions.

2:
The advantage of ORMs diminish in direct correlation to the complexity of the query you need to run. When you have a nice simple relationship - i.e. Users -> Posts, they can work very well. This (IMO) is why most ORMs/frameworks use examples like this. When you need to run complex queries, however, the amount of ORMQL you need to generate is comparable to the string length of a regular SQL query - but less performant owing to the object graph running the query invokes, which kind of defeats the point of abstraction. So, in a nutshell - ORM's are brilliant for the first 30-50% of your database tasks - but terrible for the last. I think this is another reason why AR is so prevalent.

3:
Why hide from the database? Would you use a server side language to abstract javascript?

Personally I mix those approaches:

use ORM for the basics, loading "users"

use a DAO containing several pre-baked SQL queries (i.e. selectLike, selectWhere).

extend the DAO where necessary to add more specific but re-usable custom queries

Provide simple database access through the DAO - i.e. $data = Dao->query(your sql string) to execute one off queries.

Having said all that, Doctrine 2 is coming out soon, which looks really interesting.


问:
How static functions are better than Class Methods in Objective-C
I was going to through this post by Mattt Thompson in which he mentions that

Static functions are nicer than shoe-horned class methods

I just don't see any flaw with Class methods in Objective-C and I could not relate it to C based static functions. Is it something to do with the fact that Objective-C classes are itself objects of metaclasses?

答:
Some languages such as Java have “static methods” that live in a class's namespace, but do not use dynamic dispatch. Instead, they are dispatched statically at compile time (hence their name). Dynamic dispatch is less efficient than static dispatch, since we have to search for the method in all classes of the object we're calling a method on. A central point of OOP is that we do not know the class of the object we'll be calling methods on a priori.

In Java, the properties of static methods make them a good candidate in two use cases:

“Class methods” (which Java doesn't have), e.g. some constructor with a better interface. These are problematic in Java since static methods can't be inherited, and classes can't be passed around as objects. The result is often an extra Factory object.

Objective-C can use real class methods in these cases, which is welcome flexibility. After all, Objective-C has a metaobject protocol, and classes are objects.

Low-cost method calls. Since calling a statically dispatched method is much cheaper than invoking a dynamically dispatched method on an object, static methods can be used to optimize code.

Since Objective-C builds upon the C language, we can always use plain old C functions. C does not have dynamic dispatch, and function calls are very efficient. However, C has no concepts of classes and namespaces, so great care has to be taken to avoid name clashes.

The object-oriented paradigm turns out to be very good at encapsulating complexity. While this makes it easier to design systems, it also makes it more difficult to reason about performance.

The point of the post you mentioned seems to be that using the C subset of Objective-C (i.e. the procedural features) can be a very good idea, since it contains a couple of sensible features the object-oriented parts of Objective-C won't give you. The sentence “Static functions are nicer than shoe-horned class methods” is likely to mean that if you have some function that does not have to be an instance method, using a C function is probably a good – and certainly more performant – solution than putting it into a class method (which would also miss the point of classes-as-objects).


问:
Best strategy for storing static data : json file vs DB (mongo) vs redis
I'm currently designing a set of webservices related to travel industry. one of this service need to be take in input a airport code and a locale, and return the localized city name.

```<code>fn(cityCode) => localizedCityName
fn(airportCode) => cityCode, localizedCityName, localizedAirportName
</code>```

I have currently in the mongodb a collection of cities which contains, among other stuff, for each city, localized infos, and airports list :

```<code>{cityCode:'lon',
  localized:{
            'en':{name:'london'},
            'fr':{name:'londres'}
            },
 airports:[{code:'GTW', name:'Gatwick'}]
  }
</code>```

I'm wondering what would be the best performant and scalable since :

The pivotal format is json (so moving data from solution to another is easy)

There are about 200 cities, but each city can contain a significant amount of data not related to our use)

The data we need is rarely or never changing (gatwick airport will always be in london ...)

There is need to localize a few cities at a time (20 to 100).

the api is only on one server (with redis installed) but we hope to get one day to the point where clustering would be required.

I was considering :

Leave the data in MongoDB, and query as needed, and use index to optimise.
Pros : data consistency, unique source for the queries.
Cons : not performant

Re-arrange my json, and store it to many files, by rearrange i mean having a assoc json array with cityCode as the key , and airportCode for the other files
pros: Uber faster
cons: no consistency, not scalable ??

Re-arrange my json and store to a redis same as before except it's on a redis cache
pros: Same as before except more scalable maybe
cons: need to read and parse the json (i couldn't really see anything else)

NOw the questions :

Do you have some other strategies to recommend ?

Which one should i go for and why ?

答:
The most performant solution is in-memory, after that.. YMMV. For example, reading the data from file can be slower than reading from the DB. I wouldn't like to say which is better, so you'll have to test it.

However, if redis is caching the file, then this will be faster (effectively in-memory!) so I'd go for that, but automated consistency is something worth having. So, I would generate the files from the data held in the DB. If you do this at startup (or reload of the application), you can ignore the file completely and apart from a delay between updating in the DB and writing to file (which I assume you'd schedule for a known release time) you're good to go. Obviously you could add a method to your administrative system to force a rewrite which might be even better.