问:
Elegant way to count the number of files in Zip archive
I need to count the number of files in several zip archives for error checking.  I've found several answers on SO but nothing elegant.  I currently have the following code which works but it's not pretty:

```<code>File[] fileListing = new File("ZipFiles").listFiles();
int numberOfFilesInAllZipArchives = 0;
for(File file : fileListing)
{
    ZipFile zf = new ZipFile(file.getName());
    numberOfFilesInAllZipArchives += zf.size();
    zf.close();
}
</code>```

Is there any way to get around creating an object for each zip file or making this process more streamlined?

Thanks!

答:
The only thing I see inelegant about your implementation is intertwining the file listing work, the zip file opening/closing work, and the counting work. (Do you have another issue with it?)

Solution 1: Use Java's try-with-resources block to automate the file closing work thanks to ZipFile being AutoCloseable:

```<code>...
try (ZipFile zf = new ZipFile(file) {
  numberOfFilesInAllZipArchives += zf.size();
}
...
</code>```

Solution 2: In Groovy, separate out the file listing work (and add filename filtering):

```<code>import java.util.zip.ZipFile

def dir = new File('Downloads')
def num = 0
dir.eachFileMatch(~/.*\.zip/) { f ->
    def z = new ZipFile(f)
    num += z.size()
    z.close()
}
println num
</code>```

Solution 3: Also separate ZipFile open/closing work using Groovy's "with" idiom, adding a zipFileWith(closure) method to the class File:

```<code>import java.util.zip.ZipFile

File.metaClass.zipFileWith = { closure ->
    def zf = new ZipFile(delegate)
    try {
        closure(zf)
    } finally {
        zf?.close()
    }
}

def dir = new File('Downloads')
def num = 0
dir.eachFileMatch(~/.*\.zip/) { file ->
    file.zipFileWith { zf -> num += zf.size() }
}
println num
</code>```

Solution 4: Add an eachZipFile(closure) method to the class File:

```<code>import java.util.zip.ZipFile

File.metaClass.eachZipFile = { closure ->
    delegate.eachFileMatch(~/.*\.zip/) { file ->
        def zf = new ZipFile(file)
        try {
            closure(zf)
        } finally {
            zf?.close()
        }
    }
}

def dir = new File('Downloads')
def num = 0
dir.eachZipFile { zf -> num += zf.size() }
println num
</code>```

Body:
The only thing I see inelegant about your implementation is intertwining the file listing work, the zip file opening/closing work, and the counting work. (Do you have another issue with it?)

Solution 1: Use Java's try-with-resources block to automate the file closing work thanks to ZipFile being AutoCloseable:

...
try (ZipFile zf = new ZipFile(file) {
  numberOfFilesInAllZipArchives += zf.size();
}
...

Solution 2: In Groovy, separate out the file listing work (and add filename filtering):

import java.util.zip.ZipFile

def dir = new File('Downloads')
def num = 0
dir.eachFileMatch(~/.*\.zip/) { f ->
    def z = new ZipFile(f)
    num += z.size()
    z.close()
}
println num

Solution 3: Also separate ZipFile open/closing work using Groovy's "with" idiom, adding a zipFileWith(closure) method to the class File:

import java.util.zip.ZipFile

File.metaClass.zipFileWith = { closure ->
    def zf = new ZipFile(delegate)
    try {
        closure(zf)
    } finally {
        zf?.close()
    }
}

def dir = new File('Downloads')
def num = 0
dir.eachFileMatch(~/.*\.zip/) { file ->
    file.zipFileWith { zf -> num += zf.size() }
}
println num

Solution 4: Add an eachZipFile(closure) method to the class File:

import java.util.zip.ZipFile

File.metaClass.eachZipFile = { closure ->
    delegate.eachFileMatch(~/.*\.zip/) { file ->
        def zf = new ZipFile(file)
        try {
            closure(zf)
        } finally {
            zf?.close()
        }
    }
}

def dir = new File('Downloads')
def num = 0
dir.eachZipFile { zf -> num += zf.size() }
println num


问:
In C is it possible to change http requests to https
Long story short I've got this package that currently has a security risk where credentials are sent over http and I need to convert them to https but I currently lack the knowledge to do so. Could anyone point me in the right direction on how to go about this?

Just for the record I did not create this program but became its proxy maintainer for Gentoo as to keep it available for the community and since its kinda my first package I don't want to lose it.

答:
I would not touch the C program. I suggest that you put apache in front of it. You can easily configure apache for https (you will need to get a cert).
You will need to google how to set up a "reverse proxy".
Apache will then act as the front door and forward the http requests to your other process on an internal port. </p>

ContentLicense: CC BY-SA 3.0


问:
Defining acceptance criteria for a user story
I have been given a user story / epic which is display a web-page giving details about a retail store for a company.

Simply put, if the url was mywebsite.com/newyork it would display the new york store details
mywebsite.com/athens would display the athens details etc.
The things that would be displayed are things like opening hours, location, phone number

From my reading it looks like user story acceptance criteria are very high level. As such
I have 2 questions about acceptance criteria for my current project.

Questions

1) The product owner has put display Welcome to myRetailStore yyy where yyy is the name of the store page being displayed as a h1 tag at the top of the page underneath the main navigation bar

is that a relevant acceptance criteria? should the part about h1 tag be in the detail?(as it seems not high level enough to me)

2) assuming that there were 400 stores does checking the fact that Welcome to myRetailStore New York is displayed for New York mean that story has passed its acceptance criteria or would it need to be checked for all 400 stores?

答:
It depends.

The level of details in acceptance criteria may very depending on the type of story. Consider creating a user story for a web page or for a new programming language? I suppose different level of details will be needed to accept the story (meaning it is done correctly).

I find that the more detailed the AC are, the less are the chances of misinterpretation of requirements by developer.

In case it matters how the greeting message is displayed, specify it, if not it can be omitted.

For 400 stores, if pages are templated - and store names are populated in, then the story should sound like "when I open a page for a specific store (newyork) correct fields are populated in the template (hours, name, etc)" - no need to repeat it for each store. As there would be another story about creating the data records for all stores with correct data.

I would recommend to take a look at tooling for ATDD. We've used SpecFlow and it was very useful in providing just the right amount of details.


问:
Remote server development
We are working on a django project which is hosted on an amazon ec2 instance. It is on very early stage, so changes are made constantly and the webapp should be updated more than once a day. Right now, the development cycle looks like:

The code is modified/added/deleted.

It is pushed to a bitbucket repository.

I login to ec2 server with ssh.

Pull the new code from the repo and merge.

Restart the server.

Although, it does the job, I don't feel like it is the best way to working with remote servers.

What are the well known techniques of development on remote servers?

答:
I think what you need is a Continuous Integration (CI) server. You can host your own, for example Jenkins which has BitBucket integration or use a hosted service like CodeShip which has a free plan.

You could try and set up your own set of scripts to do a similar job but the industry has already provided a solution in this area.

You didn't mention tests in your question but You can also get these CI tools to run your tests for you as well, so bad builds don't go into production.

Title: Remote server development


问:
What data type is this?
I have a list of items I am adding to, however this special list will delete anything past a given capacity. Note the order is maintained.

For the life of me, I can't think of the name of such a construct.

Consider

```<code>data = new SpecialListType(3); // set capacity to 3

data.Add("A");
data.Add("B");
data.Add("C");
data.Add("D");

data.Dump(); // returns {"B", "C", "D"}
</code>```

What is this? Some form of a Set or Buffer? And is there a framework implementation of this in Java and .NET?

答:
It's a bounded queue (a first-in-first-out queue with a fixed capacity).

This particular queue always allows addition of elements and silently remove head element for newly added element (when full).

In Java there is the CircularFifoQueue that works exactly that way (see also Size-limited queue that holds last N elements in Java).

For .NET you'd take a look at:

Limit size of Queue in .NET?

Fixed size queue which automatically dequeues old values upon new enques

Title: What data type is this?


问:
Representing category splits in a personal finance database
I'm working on developing a personal finance applications (e.g. Quicken/Mint.com/etc..). I plan to store the data in a SQLite database. I'm stuck trying to determine how I want to represent splits in the database. Each transaction will be assigned a category such as "Food and Dinning" or "Rent". However, a single transaction may be split across multiple categories. For example a transaction at a grocery store may be split between "Groceries" and "Household".

Here's one version of my Transactions table. Transactions that are split would have a Catagory of "Split".

```<code>---Transactions Table (Option 1)---
INTEGER Id
DATE Date
REAL Amount
TEXT Description
TEXT Comment
TEXT Tags
TEXT Category
</code>```

And here's my Splits table which identifies how much of each transaction is assigned to specific categories.

```<code>---Splits Table (Option 1)---
INTERGER Id
INTEGTER TransactionId
REAL SplitAmount
TEXT Category
</code>```

The problem I have with this setup is the two tables could potentially get out of sync with one another. If a split is added or removed then the code has to know to switch the transaction category to and from "Split". Also the total amount of the splits associated with a transaction need to match the actual transaction amount.

Alternatively I was thinking of just treating everything as a split or "TransactionCategory". So rather then assigning an amount to the transaction the amount is just assumed to be whatever the sum of the "TransactionsCategories" are for each transactions. Transactions that are not split would just end up having a single associated "TransactionCategory" row to identify the amount and category of the transaction. If for some reason a Transaction did not have an associated "TransactionCategory" entry it could just be assumed to be an uncategorized transaction of 0 amount.

```<code> ---Transactions Table (Option 2)---
INTEGER Id
DATE Date
TEXT Description
TEXT Comment
TEXT Tags

---TransactionCategories Table (Option 2)---
INTERGER Id
INTEGTER TransactionId
REAL Amount
TEXT Category
</code>```

I'm leaning towards this option as I do not have to keep the two tables in sync, but I'm worried about the overhead of having to join the two tables and take the sum of the TransactionCategories table anytime I need to show a list of transactions and their total amounts.

答:
Double entry handles split transactions well.

Credit checking 200
and
debit grocery 150
debit household 50

Debits = Credits.

The double entry means recording both debit(s) and credit(s).


问:
Dependency injection vs dependency lookup in a layered architecture
Given a layered architecture, where the business logic is isolated in a package from all other layers, like persistent storage, user interface, interfaces for various (vendor) remote services, etc, I am thinking to manage dependencies in the following way:

Within the library, all dependencies are injected manually via the constructor. This has the advantage (which in my opinion is big) that it makes tests more explicit, increasing their value as a documentation source.

For the other layers and plugins, I am thinking to inject manually only one object used for dependency lookup, which would make mocking easier. I doubt there would be only one object for all subsystems, I am merely thinking about really specific, small interfaces, and factory instances which implement one or more of those interfaces. How many interfaces such a factory implements is an implementation detail, I guess I would start with only one factory implementing all interfaces, and as the system evolves, I would pull out other factories and refactor accordingly (YAGNI). The interfaces (i.e. the signature of the constructors) would stay as they were before refactoring, thanks to interface segregation.

The business library gets its commands from outside via commands (think DDD).

What do you think about this? Other advantages and disadvantages are welcome!

Which other strategies for dependency management do you prefer in which situations and why?

答:
Your dependency lookup looks very similar to service locator pattern, except that often, service locator is static.

The benefits of such dependency lookup when compared to ordinary dependency injection are that:

You won't find constructors which take dozens of (often mandatory) arguments if they need many dependencies. This makes your code shorter and easier to read and write.

You don't need to change the definition of the constructor when the object needs to rely on an additional dependency (or don't need a previously used dependency). This means less code changes over time.

There is however a drawback:

The dependencies are out of control. When dependencies are injected through the constructor, by looking at the constructor, you immediately know which dependencies the class needs. If the only thing there is a lookup (or a service locator), you only know that the class relies on one to N dependencies, with no additional information.

This also makes it difficult to use those classes. Imagine you're in a context of a unit test. Which objects, in the lookup, should be mocked? The problem exists already for dependency injection (since the tested method don't necessarily have to use every dependency requested by the class), but with a lookup, it becomes larger, with more dependencies which can be potentially used.

Pass-through (that is when a class A requests a dependency in order to pass it to another class B it calls) can make this task even worse, since you have no visibility on which dependencies are used by the class B if you pass the lookup to it.


问:
Choosing a software license that requires written permission to use commercially
I'm looking for a license that I haven't seen before. I would like a license that allows other people to use my code non-commercially, but unless they have written permission they should not be able to sell derivatives.

Pull requests by other people should be possible, but their code would also become owned by me.

Forking should be possible, in which case mutual permission would be required from me and the fork maintainers in order for either of us to use the software commercially.

Does something like this exist?

答:
Actually, there are products available with a licensing scheme which fulfills very similar requirements. It typically works by picking a dual license model. You publish your software, for example, under a GPL license (which theoretically allows selling of the software or derivatives, but makes it practically impossible). But you tell people on your website that you are willing to offer them your software under a different license, under whatever terms you like, assumed they ask you first and you agree to their usage model.

As a well known example, you might look into the license terms of MySql.


答:
You're going for something akin to (but not similar) Spring's ApplicationContextAware, where you pass ApplicationContext (which is a BeanFactory) via a setter, so it later can get beans it needs.

Generally it is a bad idea, if you only need to inject dependencies:

It is not explicit about dependencies it uses. So, it is actually harder to mock, not easier, because you're not sure what needs to be mocked unless you look into the whole code of a class and probably the whole code of its' super- and subclasses, if inheritance happens.

It is not type safe if you query dependencies by string keys, with all negative consequences of that. However, if you say you'll have specific interfaces to deal with that (like UserRepositoryFactory which has a method like getUserRepository, I guess), it will be type safe, but I don't see the benefit in this solution, because it seems much easier to just inject dependencies without medium getter/wrapper/injector-objects. I think it would be just duplication of DI functionality.

If I would make a plugin system, I would go with "stupid" plugins, which just implement some SomethingPlugin interface, and expect it's dependencies to be injected (via constructor or setters, which doesn't really matter) by the system, without any prior knowledge of anything outside.

Example:

public class UserRatingPlugin implements GenericPlugin,
        UserRepositoryAwarePlugin, PostResositoryAwarePlugin {

    // here are dependencies
    private UserRepository userRepository;
    private PostRepository postRepository

    @Override // from UserRepositoryAwarePlugin
    public void setUserRepository(UserRepository ur) {
        userRepository = ur;
    }

    @Override // from PostResositoryAwarePlugin
    public void setPostRepository(PostRepository pr) {
        postRepository = pr;
    }

    @Override // from GenericPlugin
    // this should be required to make this class a plugin
    public void doPluginMagic() { /* ... */ }
}
```

Then you register this plugin (via config file, I imagine), then somewhere in a "system level" code you instantiate this class, set dependencies (this part can be automated by Spring's or similar DI-container) and invoke doPluginMagic().

This way you still have small specific interfaces, but now as parts of a plugin API, which I think simplifies the whole thing. You can see what plugin can access by just looking on its' type declaration, and no additional "factories" have to be crafted.

Title: Dependency injection vs dependency lookup in a layered architecture

Body:
Given a layered architecture, where the business logic is isolated in a package from all other layers, like persistent storage, user interface, interfaces for various (vendor) remote services, etc, I am thinking to manage dependencies in the following way:

Within the library, all dependencies are injected manually via the constructor. This has the advantage (which in my opinion is big) that it makes tests more explicit, increasing their value as a documentation source.

For the other layers and plugins, I am thinking to inject manually only one object used for dependency lookup, which would make mocking easier. I doubt there would be only one object for all subsystems, I am merely thinking about really specific, small interfaces, and factory instances which implement one or more of those interfaces. How many interfaces such a factory implements is an implementation detail, I guess I would start with only one factory implementing all interfaces, and as the system evolves, I would pull out other factories and refactor accordingly (YAGNI). The interfaces (i.e. the signature of the constructors) would stay as they were before refactoring, thanks to interface segregation.

The business library gets its commands from outside via commands (think DDD).

What do you think about this? Other advantages and disadvantages are welcome!

Which other strategies for dependency management do you prefer in which situations and why?

Body:
You're going for something akin to (but not similar) Spring's ApplicationContextAware, where you pass ApplicationContext (which is a BeanFactory) via a setter, so it later can get beans it needs.

Generally it is a bad idea, if you only need to inject dependencies:

It is not explicit about dependencies it uses. So, it is actually harder to mock, not easier, because you're not sure what needs to be mocked unless you look into the whole code of a class and probably the whole code of its' super- and subclasses, if inheritance happens.

It is not type safe if you query dependencies by string keys, with all negative consequences of that. However, if you say you'll have specific interfaces to deal with that (like UserRepositoryFactory which has a method like getUserRepository, I guess), it will be type safe, but I don't see the benefit in this solution, because it seems much easier to just inject dependencies without medium getter/wrapper/injector-objects. I think it would be just duplication of DI functionality.

If I would make a plugin system, I would go with "stupid" plugins, which just implement some SomethingPlugin interface, and expect it's dependencies to be injected (via constructor or setters, which doesn't really matter) by the system, without any prior knowledge of anything outside.

Example:

public class UserRatingPlugin implements GenericPlugin,
        UserRepositoryAwarePlugin, PostResositoryAwarePlugin {

    // here are dependencies
    private UserRepository userRepository;
    private PostRepository postRepository

    @Override // from UserRepositoryAwarePlugin
    public void setUserRepository(UserRepository ur) {
        userRepository = ur;
    }

    @Override // from PostResositoryAwarePlugin
    public void setPostRepository(PostRepository pr) {
        postRepository = pr;
    }

    @Override // from GenericPlugin
    // this should be required to make this class a plugin
    public void doPluginMagic() { /* ... */ }
}

Then you register this plugin (via config file, I imagine), then somewhere in a "system level" code you instantiate this class, set dependencies (this part can be automated by Spring's or similar DI-container) and invoke doPluginMagic().

This way you still have small specific interfaces, but now as parts of a plugin API, which I think simplifies the whole thing. You can see what plugin can access by just looking on its' type declaration, and no additional "factories" have to be crafted.


问:
Synchronization of resource that has moved out of requests filter range
I have a RESTful API with corresponding clients.

If a client request fetches a set of resources (Events) based on a filter, say events taking place between 2015-04-13 - 2015-04-19. If an event which has previously been synced to the client changes time on the server so that it will be outside of the above time range it, the next request will not include the updated event and the client will not know about the change until it makes a request which includes the updated time.

The solutions I'm thinking of are

Adding simplistic version management by creating a new events with the updated time which refers to the original event whenever an event is updated. When a request is made the server would have to return the latest version of all the events within the time range.

Adding a new resource (say EventUpdate) which only tracks time changes of an event leaving sort of a "gravestone" where the Event used to be. Whenever a request is made EventUpdates would have to be checked to see if there previously was an event in the requested time range, if so, these Events should also be included in the response.

Right now I'm leaning towards solution 2. since it seems less overkill for what I need right now.

Is there any standard way of solving this kind of synchronization problem?

答:
Is there any standard way of solving this kind of synchronization
  problem?

I believe not, but I can propose a system redesign.

Make two API methods instead of one:

Method returning event ids for events currently scheduled for the specified date interval

Method returning event details together with updated_at timestamp for a given event ids

If a client wants to check or recheck the specified time range, he uses method #1. If he wants to fetch event details, he uses method #2. #2 will return an event by id even if it has shifted its date.

To minimize traffic for method #2 you can use scheme used by HTTP protocol (If-Modified-Since header).

To make a sync on a client, you can make one by one API calls first to recheck specified interval and then to update all events (those you have ids from the previous calls and those, which were returned by method #1 as new).

Does that solve your case?


问:
DI container with multiple instances of same object
I'm looking for idea to make a DI container that can store multiple instances of the same object. Usually, a DI container should store either a single instance of the object, or create new object each time it gets requested. Here's example I'm working on:

```<code>class Container {
    protected $store;

    public function get($alias) {
         if (isset($this -> store[$alias])) {
             return $this -> store[$alias]($this);
         }
    }

    public function bind($alias, Closure $obj) {
         $this -> store[$alias] = $obj;
    }
}
</code>```

This all works well, for this case:

```<code>$c = new Container();
$c -> bind('book', function() {
    return new Book();
});

$book = $c -> get('book');
</code>```

Now imagine I have class "Catalog", which behavior is dictated by parameters passed to it's constructor:

```<code>class Catalog {
    protected $category;

    public function __construct($category) {
        $this -> category = $category;
    }
}
</code>```

I want only single instance of each category stored in container. Yes i can do this easily by

```<code>$c -> bind('catalog.magazines', function() {
    return new Catalog('magazines');
});

$c -> bind('catalog.books', function() {
    return new Catalog('books');
});
</code>```

But categories can be dynamically created at runtime, and this seems like a lot of code. Is there a convenient solution for this? Only one instance of each object and can handle them dynamically. Or maybe it's not DI at all?

答:
But categories can be dynamically created at runtime, and this seems
  like a lot of code. Is there a convenient solution for this? Only one
  instance of each object and can handle them dynamically. Or maybe it's
  not DI at all?

Well, if categories can be dynamically created, how are you going to identify and get from DI container?

I think you should not put categories /catalogs / whatever into container if they are really mass-created at runtime dynamically. Instead you might want to put CategoryCollection into container and add / query / remove collections from it using any criteria you want.

Isn't that what you want?


问:
What default features is Windows missing that are required for programmers?
What features is Windows missing that can improve the productivity of a programmer. I know a lot of people would want some features from Linux and OSX ported or being created for Windows as well. What is missing for you ?

An example would be the option to have multiple workspaces. When not having the possibility to use dual monitors that can come in handy.

答:
I agree with the other posts so to add:

I miss ssh with x forwarding vs windows remote desktop.  For those that don't know you can run a desktop program across the network but it acts like a local window.

ContentLicense: CC BY-SA 2.5


问:
Verifying absence of an event in multithreaded unit test
I've got a legacy application that had a fragile unit test (which is really more of an integration test) that used a lot of Thread.Sleep calls to wait for events to happen.

While trying to introduce some determinism into the tests with some configurable callbacks I came across a test that is attempting to validate that given a certain input, another event doesn't occur.

It is also using Thread.Sleep to wait for 100ms before checking to see if the callback was called, but I'm thinking that a delay in execution could result in a false positive.

I'm trying to keep the test suite snappy without introducing longer delays by increasing the value of the wait.

For the positive tests I've created some code barriers that get resolved through the course of the test, but I don't see a pattern to deterministically determine that an event is not going to occur.

答: If the executor is a singleton then you should replace the executor returned by its getInstance() (or stored in the static INSTANCE field) with your mocking framework until you can put DI in place. That way you can monitor the task queue and be know when no more tasks are pending completion.


问:
TDD Red-Green-Refactor and if/how to test methods that become private
as far as I understand it, most people seem to agree that private methods should not be tested directly, but rather through whatever public methods call them. I can see their point, but I have some problems with this when I try to follow the "Three Laws of TDD", and use the "Red - green - refactor" cycle. I think it's best explained by an example:

Right now, I need a program that can read a file (containing tab-separated data) and filter out all columns that contain non-numerical data. I guess there's probably some simple tools available already to do this, but I decided to implement it from scratch myself, mostly because I figured it could be a nice and clean project for me to get some practice with TDD.

So, first, I "put the red hat on", that is, I need a test that fails. I figured, I'll need a method that finds all the non-numerical fields in a line. So I write a simple test, of course it fails to compile immediately, so I start writing the function itself, and after a couple of cycles back and forth (red/green) I have a working function and a complete test.

Next, I continue with a function, "gatherNonNumericColumns" that reads the file, one line at a time, and calls my "findNonNumericFields"-function on each line to gather up all the columns that eventually must be removed. A couple of red-green-cycles, and I'm done, having again, a working function and a complete test.

Now, I figure I should refactor. Since my method "findNonNumericFields" was designed only because I figured I would need it when implementing "gatherNonNumericColumns", it seems to me that it would be reasonable to let "findNonNumericFields" become private. However, that would break my first tests, since they would no longer have access to the method they were testing.

So, I end up with a private methods, and a suite of test that test it. Since so many people advice that private methods should not be tested, it feels like I've painted myself into a corner here. But where exactly did I fail?

I gather I could have started out at a higher level, writing a test that tests what will eventually become my public method (that is, findAndFilterOutAllNonNumericalColumns), but that feels somewhat counter to the whole point of TDD (at least according to Uncle Bob): That you should switch constantly between writing tests and production code, and that at any point in time, all your tests worked within the last minute or so. Because if I start out by writing a test for a public method, there will be several minutes (or hours, or even days in very complex cases) before I get all the details in the private methods to work so that the test testing the public method passes.

So, what to do? Is TDD (with the rapid red-green-refactor cycle) simply not compatible with private methods? Or is there a fault in my design?

答:
Does the sun revolve around the earth or the earth around the sun? According to Einstein the answer is yes, or both as both models differ only by the point of view, likewise encapsulation and test driven development are only in conflict because we think they are. We sit here like Galileo and the pope, hurling insults at one another: fool, don't you see that private methods need testing too; heretic, don't break encapsulation! Likewise when we recognize that truth is grander than we thought can we try something like encapsulate the tests for the private interfaces so that the tests for the public interfaces don't break encapsulation.

Try this: add two methods, one that has no input but justs returns the number of private tests and one that takes a test number as a parameter and returns pass/fail.


答:
There is quite a lot of possible solutions to your problem.  You can easily write scripts to automate the steps of the process or use an existing ready-to-use continuous integration solution.  I will shortly describe the first solution, where you write scripts to do this yourself.

On the server where your webapp mywebapp is deployed, write a small script shell mywebapp_deployment_tool which deploys the latest version of the application. I suggest that you keep a dedicated branch live or release corresponding to the latest deployed version, and which is distinct from the development branch. The job of the script would then be:

Checkout the latest live branch.

Restart the server (maybe kill -HUP would do).

Now create a git owned by your deployment infrastructure¹ repository on your server and configure a git hook that will trigger your mywebapp_deployment_tool each time a commit is pushed to the live branch.

On your development repository, set up a git hook that will automatically push merges to the live branch to the repository on the server. This will, in turn, trigger the deployment script.

Also note that VirtualBox or FreeBSD jails can be used to set up an environment where you can test your webapp before merging changes to live.

¹ This just means that this repository should not be used for development.

Title: Remote server development

Body:
We are working on a django project which is hosted on an amazon ec2 instance. It is on very early stage, so changes are made constantly and the webapp should be updated more than once a day. Right now, the development cycle looks like:

The code is modified/added/deleted.

It is pushed to a bitbucket repository.

I login to ec2 server with ssh.

Pull the new code from the repo and merge.

Restart the server.

Although, it does the job, I don't feel like it is the best way to working with remote servers.

What are the well known techniques of development on remote servers?


问:
API design for modular apps
Let's assume a web app made of independent modules:

```<code>example.com/accounting
example.com/employees
example.com/admin
</code>```

Next step is adding an API which normally might end like this:

```<code>example.com/api/v1/accounting
example.com/api/v1/employees
example.com/api/v1/admin
</code>```

I worked before like this and it was the most natural thing, but now that each sub-app is a whole unique beast that even have independent deployment operations, this pattern came up as an idea:

```<code>example.com/accounting/api/v1
example.com/employees/api/v1
example.com/admin/api/v1/
</code>```

Are there any downsides between both ways of organizing my API across modules that I should be aware of?

I am trying to think about deployment costs, caching, security... but cannot come up with any clear disadvantage. The only thing that bothers me is my habit of having all api operations under a common root path.

答:
As mentioned by Frank, it depends on how independent the independent modules are. In the example above, and in real life, there is probably a common dependency for these three endpoints, namely a persistent datastore.

Good API naming communicates with the user of the API. If there is truly no dependency, you should use the second pattern since that implies that the API:s are separate and you could e.g. move one one of the API:s to a new server and URL without affecting the semantics of the API.

But if you have any dependency, you should use the first one. Otherwise you could have the following scenario:

Management discovers the virtue of email and decides that all the fax numbers in the db should be replaced by email addresses.

You update /employees/api/v1 and bump the version number, /employees/api/v2

But the change in the db also forces you to update /admin/api/v1/ to /admin/api/v2/

Now you have to communicate to all your users that they have to use v2 for admin and employees and v1 for accounting. I.e. you have to communicate that there is a dependency between your apis. In that situation it's more clear to have one version number for the entire API.

It's good practice to modularize stuff, but in reality there's usually the giant global dependency called the database to think of.

Title: API design for modular apps


问:
Web applications and homogeneity across users
My boss is asking me to implement certain features in a web application (rails) that are relevant for only a couple users, sometimes just one user (the reason is this is targeted for businesses and a user has a high life-time value for him).

This is not simply "show different content to different users" though, the web has become pretty good at handling that. It's about actual functionality, like different form fields, ajax requests only for a single user etc..

While i can imagine ways to hack this up, all sorts of alarm bells go off: bloat and unmaintainability being the primary ones. Also, i can't find any web applications doing this very much (at least with the custom functionality growing as a function of the number of users).

I couldn't convince my boss yet that this is a bad idea, am i wrong here? I feel like a web app is not the right tool for this job. And if this is indeed a bad idea, what would the best way be for me to clearly explain him that?

Thanks!

答:
I couldn't convince my boss yet that this is a bad idea, am i wrong
  here? I feel like a web app is not the right tool for this job. And if
  this is indeed a bad idea, what would the best way be for me to
  clearly explain him that?

I am really not sure this is a bad idea. To me it sounds like a good idea because your customers will get what they want from you, not from someone with more agile processes.

It seems it's time for you to think about some plugin system in your application architecture.

This will allow you to make custom feature development isolated as a plugin without bloating the whole thing.


问:
How to optimize the application with huge number of database requests per minute
I have to provide free demo of some service to end users in my application. Free demo could be of 30 mins, 1 hours, 5 hours etc. (predefined time) for a new user for one time only.

User can also consume that time in parts. like in 30 mins of free demo, they can use like 10 mins today, 15 mins tomorrow and rest of the time on next day etc.
Now If a user opt the free demo of 30 mins and logged in & using the service. I can restrict the user for 30 mins via his start time & end time. I can send them to payment page if sum of start & end-time is equals to 30 min.

Now problem arises with some uncertain conditions like what if user closes the browser or their internet stopped working or anything else at their end during their active session. In this, I can't calculate their consumed time because of lack of endtime.

Scenario could be like below (for 30 min demo).

```<code>UserID  StartTime           EndTime             Consumed(mins)
10      09-04-2015 10:00    09-04-2015 10:10        10
10      10-04-2015 05:00    10-04-2015 05:04        4
10      11-04-2015 07:46    11-04-2015 07:56        10
10      11-04-2015 10:00    // Browser closed or any uncertain condition
10      11-04-2015 11:00    // How to restrict user to use actual 30 mins because I do not have EndTime in above row to calculate Consumed mins.
</code>```

I may have more than 100000 users at the time same to use our services, So I am finding an efficient solution for this.

As per my understanding, I can create a Separate Job to check user's LastActiviteTime and based on that I can update their Consumed(mins) in database. That Job would be executed every minute and also on the other hand, browser of each session user would update the LastActiveTime in database.

This can solve my problem but I'm not very sure about the performance of my application becuase of huge number of database request per minute.

Please advice.

答:
I do not think making decisions about if trial time was exhausted on client is a good idea. This can be easily fooled and can't be calculated with some precision.

Since you have a web application, I guess, it would be much better to limit a number of API calls a trial user can make without payment. You can make some tests and map an average number of API calls to a time, an average user spends at your software.


问:
Builder design patterns: passing parameters from client to the builder
Does the builder design pattern allow passing parameters?
If yes, what is right way to achieve it?

Overview

The builder pattern is about hiding the details of object constructon, it places two layers of abstraction between the product and the client: builder and director

builder is the interface for product construction, the idea is
polymorphism: same methods are used to set different products

director separates client from the building process

Builder pattern is meant for complex products, so the builder interface could be large: setPart1(), setPart2(), ... setPart100()

director has small and simple interface: constructProduct(), getProduct()

same director could use different concrete builders for different concrete products

but also same builder interface could be used by different directors

What if some properties of the product cannot be hidden from the client.

Example problem

Assume we have the complex product such as computer set.

```<code>class ComputerSet {
private:
    string procName;
    string hddName;
    int hddCapacity;
public:
    ComputerSet(string p) : procName(p) {}
    void setHdd(string name, int capacity);
};

void ComputerSet::setHdd(string name, int capacity) {
  hddName = name;
  hddCapacity = capacity;
}
</code>```

Actually, it is not so complex. The product has only 3 parts to set: procName, hddName, hddCapacity. The reason is to make example small. Let's say the computer set could have many components.

Dividing HDD part into two parts (name and capacity) has been done for the simplicity of the example. They have different nature: hddName is builder-constant, hddCapacity is builder-variant.

Let's assume there is the concrete product: computer set with procName="Amdel" and hddName="Orange". These details could be hidden from the client. The client:

creates the instance of the Director

creates the instance of the AmdelBuilder

passes AmbelBuilder to the Director

uses Director's inteface to get the product

does not know about "Amdel" and "Orange" strings

But there could be many HDD capacities to choose: 250, 500, 750, 1000, 1500, 2000.

One way is to create the builder for each capacity: AmdelBuilder250, AmdelBuilder500, AmdelBuilder750, AmdelBuilder1000, AmdelBuilder1500, AmdelBuilder2000.

What about designing only one AmbelBuilder for each capacities and passing hddCapacity parameter just before the product is created?
How to pass the parameter:

contain it inside the builder as its property (data member);
two kinds of settings both from the client level

set with constructor (once per instance)

set with the setter (setting multiple values per single instance)

make it the argument of the builder method, it has to be passed from the director level

make it the argument of the director construction method

contain it inside the director as its property (data member)

First Approach

The Builder stores the hddCapacity as own data field

```<code>class Builder {
protected:
    ComputerSet *cs;
    int hddCapacity;
public:
    Builder(int capacity = 250) : hddCapacity(capacity) {  }
    virtual void createComputerSet() = 0;
    virtual void setHdd() = 0;
    ComputerSet* returnComputerSet() { return cs; }
};
</code>```

Concrete builder's setHdd() method does not need parameters

```<code>class AmdelBuilder : public Builder {
public:
    void createComputerSet();
    void setHdd();
    AmdelBuilder(int capacity = 500) : Builder(capacity) { }
};

void AmdelBuilder::createComputerSet() {
    cs = new ComputerSet("Amdel");
}
void AmdelBuilder::setHdd() {
    cs->setHdd("Orange", hddCapacity);
}
</code>```

The director does not know about the hddCapacity parameter

```<code>class Director {
private:
    Builder *builder;
public:
    void setBuilder(Builder *b) { builder = b; }
    void constructComputerSet();
    ComputerSet* getComputerSet() { return builder->returnComputerSet(); }
};

void Director::constructComputerSet() {
    builder->createComputerSet();
    builder->setHdd();
}
</code>```

because the client sets AmdelBuilder.hddCapacity when the client creates the Builder instance

```<code>Director* d = new Director();
Builder* ba = new AmdelBuilder(750); // Amdel + Orange HDD 750
d->setBuilder(ba);
d->constructComputerSet();
ComputerSet* csa750 = d->getComputerSet();
</code>```

Other solution

Builder does not store hddCapacity, but the setHdd() method requires hddCapacity parameter.

```<code>class Builder {
protected:
    ComputerSet *cs;
public:
    Builder() {  }
    virtual void createComputerSet() = 0;
    virtual void setHdd(int hddCapacity) = 0;
    ComputerSet* returnComputerSet() { return cs; }
};
</code>```

The director is the direct user of the Builder's API. So the director must know the hddCapacity value.

Question

What should is the best practice that does not violate the OO principles and the encapsulation idea of the builder pattern?

答:
I believe neither of your approaches violate anything and both can be used just fine.

Passing parameters to the builder can be done either using constructor or setter methods. I do not see any problem with it.

I tend to pass parameters via constructor if there are not so many of them. If I have more than 3-5 configuration parameters I switch to using methods to configure Builder.


问:
Use model for “everything” in MVC?
This question is a result out of the discussion HERE and was moved from HERE.

Is it really good practice to supply EVERY value you display in any view via a model?

Especially variables like the current username. Even Microsoft odes the following in their default template for the _LoginPartial in ASP.NET MVC 3+:

```<code>"Hello " + User.Identity.GetUserName() + "!"
</code>```

I think this is OK to not supply this via a model as it is much better in light of maintainability. Or should you obay the "MVC rules" and add a model/sub-model to every page/model to supply such stuff and pass it around in your views and partials?

How do you solve such "problems" or what do you think is the best way of doing such stuff?

Inheritance would be an option:

```<code>public class BaseUserModel
{
    public string UserName { get; set; }
}
</code>```

Require every model to inherit from a base model like above containing such values.

But could that cause problems if you need/want to inherit another class for some model?

答:
Inheritance

I have used inheritance before as a solution for passing common data to many of the same pages (user, role, event tracking/logging, profile information, and other data).  This is convenient in some cases, but not always ideal.  For instance, you may add profile data to the base model for pages where they can add their email address, or display their profile picture, and this may be used on many pages, but many pages this model may not be used at all which would be a waste.

Container Model

A better idea in my opinion is to create models specifically for your view(s).  This way you can add several different models to a single container model and each view can have their custom set of models without carrying extra models that are not needed for that view (unlike the inheritance idea).  You then could include the profile model on views that require it, and exclude it on views that don't.  One feature I particularly like about this idea, is that you can include sub-models for partial views that can narrowly focus on just that model and be re-used on many other views.

Model + Web API Calls

Another strategy I have used is a mix of models and Web API calls.  A main model for the page, but certain parts of the page could get/set their data via an API call, this I have found is useful for smaller parts of the page or commonly re-used parts of the page.  This may not be the best from a purest point of view, but I have found it useful in practical situations.  The most common examples for me are usually grids or custom controls.  I will have a model for the page, but some grids or controls on the page will get their data from API calls that typically return XML or Json and be manipulated via Javascript/jQuery.

Title: Use model for “everything” in MVC?


问:
Front-end structure of large scale Django project
Few days ago, I started to work in new company. Before me, all front-end and backend code was written by one man.

As you know, Django app contains two main directories for front-end: /static - for static(public) files and /templates - for django templates

Now, we have large application with more than 10 different modules like: home, admin, spanel, mobile etc.

This is current structure of files and directories:

FIRST - /static directory. As you can see, it is mixed directories with some named like modules, some contains global libs.

one more:

SECOND - /templates directory. Some directories named like module with mixed templates, some
depends on new version =), some used only in module, but placed globally.

and more:

I think, that this is ugly, non-maintainable, put-in-stress structure!

After some time spend, I suggest to use this scheme, that based on module-structure.

At first, we have version directories, used for save full project backup, includes: /DEPRECATED directory - for old, unused files and /CURRENT (Active) directory, that contains

production version of project.

I think it's right, because we can access to older or newer version files fast and easy.
Also, we are saved from broken or wrong dependencies between different versions.

Second, in every version we have standalone modules and global module.

Every module contains own /static and /templates directories. This structure used to avoid broken or wrong dependencies between different modules, because every module has own js app,  css tables and local images.

Global module contains all libraries, main style-sheets and images like logos or favicon.

I think, this structure is much better to maintain, update, refactoring etc.

My question is:

How do you think, is this scheme better than current? Can this scheme live, or it is not possible to implement this in Django app?

答:
Yes. It is much better then how it was done previously. Moreover Django is also the right tool to do it. The reason for the same is, when we look at the base of the framework i.e. Django is this case is written in Python. Python believes more in Readability rather then writing the code itself.

So, yes the things you have done is much better in simple terms


问:
Python - Service, how to get a code review
I am usually a PHP and Delphi developer and have been getting into Python lately.

I am developing an app and would love to get some feedback on the code, there are few things which I would love to tidy up myself but I am sure there are other things.

Where / how is a good way to get in touch with an experienced Python dev? Paying is no problem.

答: I would check to see if there is a local Python Users Group.  People on those lists are usually interested in helping out other Python developers. </p>


问:
Why is it bad to have one mechanism for inheritance and subtyping?
It is quite well-known that (class) inheritance and subtyping (sometimes called interface inheritance) are different things: inheritance is a mechanism for sharing code, while subtyping is a relationship that allows an object of a subtype to be substituted where an object of a supertype is required, which is a kind of polymorphism.

I have read in several places that having one programming language mechanism that conflates both is a bad thing. However, many OO languages do just this (Java being the prime example). I can see some justification for this: After all, if we have the subtype relationship "B subtype of A", then we can expect that an object of type B will share some behaviour with objects of type A, therefore why not kill two birds with one stone and let B inherit behaviour from A?

So why is this a bad thing? I am interested in examples as well as in a more general/theoretical justification.

答:
I've never heard this exact claim before, but from the way you describe it, it sounds very closely related to two mantras I am familiar with: "favor composition over inheritance" and "separate interface from implementation".

The definition of subtyping that you appear to be using (I won't get into the question of whether it's correct or not) is that, if A is a subtype of B, that means A has the same public interface as B--possibly with some extra stuff--and that interface fulfills the same contract for A as it does for B. Look up "Liskov Substitution Principle" for more on that.

That is very different from saying that A shares some of B's code. There are many ways to let classes share each other's code, and (in most OOP languages) one the simplest and safest is for A to have a private member variable of type B. In other words, composition.

The problem with a concrete class A inheriting from another concrete class B is that, in addition to telling the compiler that A shares B's interface, you're effectively forcing A to use B's code as well, and there's no easy way to separate those things if you feel A should use a different implementation from B in the future. Thus, we would say that A and B are tightly coupled. It's often said that inheritance is one of the tightest forms of coupling there is in modern programming languages.

If you instead create an abstract class/interface/whatever-your-language-calls-it, and make A and B inherit from that, then the compiler understands that they have the same interface but their implementations remain decoupled forever. You may still have A use an instance of B internally, but you're free to change that at any time without breaking any client code. You can even switch it around so that B is implemented using an A if you want.

Now, personally I would not say that it's a mistake for programming languages to use the same inheritance mechanism for both "interface inheritance" and "implementation inheritance", mainly because none of them force you to tightly couple your classes using that mechanism. You can inherit from abstract classes if you only want only "interface inheritance". You can use composition if you want only "implementation inheritance". Plus, the fewer features/mechanisms a programming language has, the easier it is to learn and use correctly. But I assume that anyone claiming it is a mistake is taking the arguments I summarized above and simply going one step farther with them.


问:
Split one big class on sub-classes
I have a generic question and I don't understand how create what I want.
I writing a basic program they can access to one database an do some SQL request, etc. But at the moment I have one big class with all SQL for all the tables from the database. And I want slipt this class in different classes for make the code more easy to read and implement stuff. I think the best solution is create one subclass for each table on the database and on for this class put all the methods related on this class. But later I don't understand how I can call this methods from the class. Because for access to the database I create an object DataBase on the main application and if I want call a method just I do a database.methodIWant().

At the moment I have:

DataBase (this class have all the methods for the databases) extends DataBaseUtil

DataBaseUtil (this class contain all methods and information for connect to the database)

and I crate the database object with DataBase db = new DataBase();

And I want something like:

DataBase (this class have all the methods for the databases) extends DataBaseUtil

DataBaseUtil (this class contain all methods and information for connect to the database)

and classes for example:

DogSQL (all the methods for the table dog on the database)

CatSQL (all the methods for the table cat on the database)

and I create the database object with DataBase db = new DataBase(); and call methods like db.methodFromDogSQL();

I don't understand how do this. I don't understand if a need use abstract, interface, inheritance for this or which is the best implementation for this. I'm starting and my knowledge in java is a bit basic, so I want learn how do this type of stuff.

答: I suggest you study Inversion of Control and the dependency injection pattern. Take a look at this Martin Fowler article on dependency injection. Using an IoC container will help you break up your class into more manageable components and make your solution less brittle than it is now.</p>

Title: Split one big class on sub-classes


答:
There is no need for using subclasses here, Dog and Cat should not be derived from Database (in fact, this would result in a typical example of how to not to use inheritance).

Instead, you could have the Create/Read/Update/Delete methods of the classes Dog or Cat just have a parameter of type DataBase (or IDataBase, an interface, if you want to be able to mock the database object for testing). For example, there should not be a method in DataBase like db.SelectDog(int Id), only a (maybe static) method in the class Dog like Dog.Select(IDatabase db, int Id). That method could return a Dog object, which corresponds to a row in your Dog table.

The "Dog.Select" method may contain the SQL code needed and call some general methods of IDataBase for selecting data, but in no way should your database class have knowledge about the SQLs needed for specific business objects.

Title: Split one big class on sub-classes

Body:
I have a generic question and I don't understand how create what I want.
I writing a basic program they can access to one database an do some SQL request, etc. But at the moment I have one big class with all SQL for all the tables from the database. And I want slipt this class in different classes for make the code more easy to read and implement stuff. I think the best solution is create one subclass for each table on the database and on for this class put all the methods related on this class. But later I don't understand how I can call this methods from the class. Because for access to the database I create an object DataBase on the main application and if I want call a method just I do a database.methodIWant().

At the moment I have:

DataBase (this class have all the methods for the databases) extends DataBaseUtil

DataBaseUtil (this class contain all methods and information for connect to the database)

and I crate the database object with DataBase db = new DataBase();

And I want something like:

DataBase (this class have all the methods for the databases) extends DataBaseUtil

DataBaseUtil (this class contain all methods and information for connect to the database)

and classes for example:

DogSQL (all the methods for the table dog on the database)

CatSQL (all the methods for the table cat on the database)

and I create the database object with DataBase db = new DataBase(); and call methods like db.methodFromDogSQL();

I don't understand how do this. I don't understand if a need use abstract, interface, inheritance for this or which is the best implementation for this. I'm starting and my knowledge in java is a bit basic, so I want learn how do this type of stuff.


问:
Is the Entity Component System architecture object oriented by definition?
Is the Entity Component System architecture object oriented, by definition?  It seems more procedural or functional to me.  My opinion is that it doesn't prevent you from implementing it in an OO language, but it would not be idiomatic to do so in a staunchly OO way.

It seems like ECS separates data (E & C) from behavior (S). As evidence:

The idea is to have no game methods embedded in the entity.

And:

The component consists of a minimal set of data needed for a specific purpose

Systems are single purpose functions that take a set of entities which have a specific component

I think this is not object oriented because a big part of being object oriented is combining your data and behavior together.  As evidence:

In contrast, the object-oriented approach encourages the programmer to place data where it is not directly accessible by the rest of the program. Instead, the data is accessed by calling specially written functions, commonly called methods, which are bundled in with the data.

ECS, on the other hand, seems to be all about separating your data from your behavior.

答:
NO. And I'm surprised how many people voted otherwise!

Paradigm

It's Data-Oriented a.k.a. Data-Driven because we are talking about the architecture and not the language it's written in. Architectures are realizations of programming styles or paradigms, which can usually be unadvisably worked around in a given language.

Functional?

Your comparison to Functional/Procedural programming is a relevant and meaningful comparison. Note, however, that a "Functional" language is different from the "Procedural" paradigm. And you can implement an ECS in a Functional language like Haskell, which people have done.

Where cohesion happens

Your observation is relevant and spot-on:

"...[ECS] doesn't prevent you from implementing it in an OO language, but it would not be idiomatic to do so in a staunchly OO way"

ECS/ES is not EC/CE

There is a difference between the component-based architectures, "Entity-Component" and "Entity-Component-System". Since this is an evolving design pattern, I have seen these definitions used interchangeably. "EC" or or "CE" or "Entity-Component" architectures put behavior in components, whereas "ES" or "ECS" architectures put behavior in systems. Here are some ECS articles, both of which use misleading nomenclature, but get the general idea across:

http://www.richardlord.net/blog/what-is-an-entity-framework  (Ash framework)

http://en.wikipedia.org/wiki/Entity_component_system

If you're trying to understand these terms in 2015, make sure someone's reference to "Entity Component System" doesn't mean "Entity-Component architecture".

ContentLicense: CC BY-SA 4.0


问:
Project has become so complex and big that I am lost

Hello this has never happened with me before . I am making an online shopping android app which has become huge and complex . While developing the code I have followed all the clean naming conventions and made appropriate packages with proper suitable meaningful names . But still I find that this project has become so huge and complex that I am not able to work on it anymore . Any ways to simplify things here =D . What shall be done so that this code doesnt look like a Matrix to me =D

答:
What you apparently failed to do is what any large project needs: project management, documentation, planning, etc. etc.

Yes, it's paperwork, it's not fun like coding, but it needs to be done.

Make sub projects, modules, split things up according to functionality, define interfaces between those sub projects, document everything, have some diagrams (UML anyone?), things like that.

That's the only way any large project remains on track, and people know what it actually does...


问:
How can I transition to a job using the Microsoft stack?
I graduated from university with a computer science degree (BSc), having done some C, C++, Java, Python, and Oracle along the way. I did some freelancing while I was at university and carried this into a full time job when I graduated, and I've been working full time for a year now.

PHP isn't really what I want to do, and I think it's limiting my opportunities the longer I stay along this path. It's not a language I like to use on a daily basis, and I would like to work in a larger organization, where it doesn't seem PHP has much use.

I want to stay in web development, and there seems to be plenty of jobs for the Microsoft stack: C#, SQL Server, and ASP.NET MVC. But beyond a half semester writing common-line C++ in Visual Studio, I have no experience with Microsoft technology.

Where do I start to make the transition to landing a job in a Microsoft shop? Are there any specific certifications I should be focusing on, or university courses I should be taking? What are employers or recruiters looking for?

What can I do besides creating a pet project in my spare time?

答:
Keyo, I think you should not be concentrating on what headhunters want. Frankly, if they want PHP or some language that in your opinion is even more poorly designed then will you be able to stick with it for a life time?

My suggestion is not to fall for the technology trap by just learning programming languages and techniques without any understanding of a domain. Look around a bit -- see what interests you. Could be game programming, high performance computing, device drivers, kernel development, compilers... the list goes on.

Once you have chosen a field or 2, look into what's there in the open source domain on these areas. Contribute heavily, you would be noticed believe me.


问:
JSON schema design for variable response
I am drafting a JSON schema for an API. One of the responses aims to show all the updates to different properties of an given entity. The number of updates may vary from one response to another. For example, it can be that the values of property_a and property_d change in one response, and those of property_a, property_b, and property_c in the next.

I want to design a schema that is both friendly to human interpretation and programmatic execution.

A concrete example of such an entity can be appointment bookings. Each appointment may consist of a place, a date, a time and a list of attendees, and all of them may change after an appointment is created. Now we want to have an API endpoint that would return any update to an existing appointment. The response contains the updated properties only.

So far I come up something like below:

```<code>{
    "event": "update",
    "appointment_id": 123,
    "updated_properties": ["place", "time"],
    "place": "some new location",
    "time": "some new time"
}
</code>```

In essence, this schema tells which properties are updated and simply returns these properties only with their new values.

Is this a sensible design? Any alternative schema for improvements?

答:
You could consider changing the value for updated_properties to an object instead of an array and list the new value right in the list of updated properties. This will reduce the overall size of the message.

```<code>{
    "event": "update",
    "appointment_id": 123,
    "updated_properties: {
        "place": "some new location",
        "time": "some new time"
    },
    "added_properties": {
        "attendees": [{"name": "Some Name", "role": "organizer"}]
    }
}
</code>```

The response you're describing sounds to be from a POST or PUT request, so you could also simply list the updated/added properties in the object, stating in the documentation that all object properties are not returned, only the updated properties. The user could perform a GET request to get all the properties if needed.

```<code>{
    "event": "update",
    "appointment_id": 123,
    "place": "some new location",
    "time": "some new time",
    "attendees": [{"name": "Some Name", "role": "organizer"}]
}
</code>```

Either approach would be acceptable.

Body:
You could consider changing the value for updated_properties to an object instead of an array and list the new value right in the list of updated properties. This will reduce the overall size of the message.

{
    "event": "update",
    "appointment_id": 123,
    "updated_properties: {
        "place": "some new location",
        "time": "some new time"
    },
    "added_properties": {
        "attendees": [{"name": "Some Name", "role": "organizer"}]
    }
}

The response you're describing sounds to be from a POST or PUT request, so you could also simply list the updated/added properties in the object, stating in the documentation that all object properties are not returned, only the updated properties. The user could perform a GET request to get all the properties if needed.

{
    "event": "update",
    "appointment_id": 123,
    "place": "some new location",
    "time": "some new time",
    "attendees": [{"name": "Some Name", "role": "organizer"}]
}

Either approach would be acceptable.


答:
I have read in several places that having one programming language mechanism that conflates both is a bad thing.  [...] So why is this a bad thing?

Because in situations where one wants one of them and not the other, one is left without an option (a recast of the "wanted a banana but what you got was a gorilla holding the banana and the entire jungle" problem, to import a famous quote). "subclass" as presented in many languages is an operation that "means a lot", that has "too much operations" within, with an "all or nothing" requirement.

Example #1: Suppose a type A is introduced by a declaration of a "fat" class A -- a class whose instances take a large amount of memory.

Now suppose in this system a new subtype of A is required (call it B), which "extends" A with extra operations used in a new area of the system, but whose values should be also used in old areas (where values of type A are expected). The final requirement for B is that even though values of this type strictly conforms to A's "contract", these values do not need to -- or even must not -- take much memory.

In a language where both operations are conflated in a "subclass" operation, there is no option: to have a subtype of A used where A is expected, one has to subclass A, and by subclassing A, one brings to the subclass a bunch of things that are not only irrelevant to the subclass, but explicitly undesired. You wanted just the type, but got the structure as well.

Example #2: There's the other way around too. That is, a class A exists in a system, and one wants to create a new structure B based on A but whose semantics, for some reason, will depart from A's "contract" in some sensible way. Because of that violation, for safety's sake (or even semantics sake) it is expected that values of type B should not be allowed to be used where values of type A are expected (and vice-versa). Again, with only a generic "subclass" operation, this option is not available. You want just the structure, but will get the type as well.

While this is commonly worked around using an encapsulated value of type A in B (otherwise known as  "composition"), one is forced to write all the artificial glue code that wouldn't be necessary otherwise.

Title: Why is it bad to have one mechanism for inheritance and subtyping?

Body:
It is quite well-known that (class) inheritance and subtyping (sometimes called interface inheritance) are different things: inheritance is a mechanism for sharing code, while subtyping is a relationship that allows an object of a subtype to be substituted where an object of a supertype is required, which is a kind of polymorphism.

I have read in several places that having one programming language mechanism that conflates both is a bad thing. However, many OO languages do just this (Java being the prime example). I can see some justification for this: After all, if we have the subtype relationship "B subtype of A", then we can expect that an object of type B will share some behaviour with objects of type A, therefore why not kill two birds with one stone and let B inherit behaviour from A?

So why is this a bad thing? I am interested in examples as well as in a more general/theoretical justification.


问:
Which design pattern for a single class accessing an object representing a data structure?
Which design pattern should I use when I have a class representing a data structure (aka C-style struct) and I wish to have only a single class to be able to access it.

I was thinking of simply declaring the class that represents a data structure as an inner class but wanted to know if there is a better way to design this.

eg:

```<code>class XXManager{
getXX();
setXXAtrtribute();
}

class XX{
  String name;
  String email;
  String job;
}
</code>```

Here I want to design a class XXManager which will handle the creation and managment of 'XX' objects and no one should be able to access XX other than through the XXManager.

答:
I make an assumption that your implementation language is Java, due to the syntax in the description.

Your methods getXX and setXX  of XXManager indicates that another class is responsible for both creating and using your XX class. Thus it cannot be a private inner class of XXManager.

STW has a point in that Manager is a bad word for a class. What does it really do? To me it seems to be holding just an instance of XX and nothing more, in which case it doesnt really make sense for the class to exist.


问:
Are there any impediment in the Apache License 2.0 to sell software?
If my project uses a part of the other person protected by the Apache License 2.0 code, I can sell it and keep as open source or there is any impediment to sell? The license itself I think very confused, especially when only option in English.

答: The Apache License has nothing to say about buying or selling software.  It neither endorses nor prohibits the sale of software so licensed.</p>


问:
TDD'ing an external interface
In order to create a very testable codebase, I abstracted away a certain external dependency. To be precise, this dependency is actually implemented by a vendor SDK and it communicates directly with a hardware. As such, this is the interface:

```<code>public interface IHardware
{
    void Send(IMessage message);

    event EventHandler<MessageEventArgs> OnMessage;
}
</code>```

(The communication with the hardware is fully assyncronous)

As soon as this is initialized, events may be raised by the hardware (through the the OnMessage event). Client code can send messages any time, and possible responses will be received in the form of events.

Now, this very simple interface needs to be implemented; The implementation is just a delegation to the actual SDK class. The problem is this: how I write tests to provoke the proper implementation?

答:
Abstracting away the 3rd party dependency like you did does not only benefit testing, as it also makes your code more robust against changes in the dependency (by effectively wrapping it/creating your own adapter).

Testing that the real 3rd party product does what you think it does when receiving commands from your code might not be possible in the context of unit testing. However, what you can do is to verify that your Adapter works as you expect it to. Sending commands when you expect it to and acts accordingly when messages are received.

Example:

```<code>    [TestMethod]
    public void shutdown_on_receiving_off_signal() {
        HardwareMock mock = new HardwareMock(); //implements IHardware, and replaces the 3PP in your tests.
        ThirdPartyAdapter adapter = new ThirdPartyAdapter(mock);
        adapter.shutdown(); //send shutdown command  to the IHardware impl.
        AssertMockReceived(OFF_SIGNAL);
    }
</code>```

Title: TDD'ing an external interface

Body:
Abstracting away the 3rd party dependency like you did does not only benefit testing, as it also makes your code more robust against changes in the dependency (by effectively wrapping it/creating your own adapter).

Testing that the real 3rd party product does what you think it does when receiving commands from your code might not be possible in the context of unit testing. However, what you can do is to verify that your Adapter works as you expect it to. Sending commands when you expect it to and acts accordingly when messages are received.

Example:

    [TestMethod]
    public void shutdown_on_receiving_off_signal() {
        HardwareMock mock = new HardwareMock(); //implements IHardware, and replaces the 3PP in your tests.
        ThirdPartyAdapter adapter = new ThirdPartyAdapter(mock);
        adapter.shutdown(); //send shutdown command  to the IHardware impl.
        AssertMockReceived(OFF_SIGNAL);
    }


答:
As you noticed, the TDD mantra of only writing code in response to a failing (unit) test starts to break down when you approach a system boundary where you need to integrate with hardware or third-party code.

You started out right by defining an abstraction layer over the SDK (in the form of the IHardware interface) and writing your code against that interface.

As it is unlikely that the IHardware implementation can be subjected to automatic unit tests in a meaningful way, you may have to let the ideal of TDD go for that part of the project. However, not all is lost:

With a bit of luck, the adaptation layer is simple enough that it can be verified by reviewing the code

The SDK you are wrapping is likely to be well tested already

You can still test the combination of SDK and adaptation layer with higher-level tests, such as integration tests and acceptance tests. These tests you will need anyway to prove the entire system works correctly.

Title: TDD'ing an external interface

Body:
In order to create a very testable codebase, I abstracted away a certain external dependency. To be precise, this dependency is actually implemented by a vendor SDK and it communicates directly with a hardware. As such, this is the interface:

```<code>public interface IHardware
{
    void Send(IMessage message);

    event EventHandler<MessageEventArgs> OnMessage;
}
</code>```

(The communication with the hardware is fully assyncronous)

As soon as this is initialized, events may be raised by the hardware (through the the OnMessage event). Client code can send messages any time, and possible responses will be received in the form of events.

Now, this very simple interface needs to be implemented; The implementation is just a delegation to the actual SDK class. The problem is this: how I write tests to provoke the proper implementation?


问:
Moving from C++ to NodeJS - Working with XML and JSON
I need to make a program to compare several large +100MB XML files, find the difference, and spit out JSON. I have written the comparison code in C++ and it performs great but now when I am getting to the bit about working with the XML data and converting it to JSON I find I'm hitting a lot of walls.

Would it be logical to port the XML comparison code to NodeJS, which may perform slightly slower, but make things a lot easier when it comes to converting XML to JSON and modifying the contents?

Any input on this would be appreciated.

James

EDIT: Or alternatively, should I keep the computational side of things as a nodejs module in C++?

答:
IT depends on the frequency of the task in your hand. If it is going to be one time job, or let's say once in a year job, do not worry about the speed, reduce the complexity of your code (independent from the language you are planning to use).

Otherwise, if such XML files are created every day / hour and require JSON conversion, I will even prefer C since standard libraries might be troublesome.

NodeJS is good for network applications, on the other hand, your problem seems like a good old CLI application. For Interpreter languages, you might want to try Ruby or Python.

A quick search might even give you good libraries or open source implementations.

Do not forget to sample your data for testing.


问:
TDD Red-Green-Refactor and if/how to test methods that become private
as far as I understand it, most people seem to agree that private methods should not be tested directly, but rather through whatever public methods call them. I can see their point, but I have some problems with this when I try to follow the "Three Laws of TDD", and use the "Red - green - refactor" cycle. I think it's best explained by an example:

Right now, I need a program that can read a file (containing tab-separated data) and filter out all columns that contain non-numerical data. I guess there's probably some simple tools available already to do this, but I decided to implement it from scratch myself, mostly because I figured it could be a nice and clean project for me to get some practice with TDD.

So, first, I "put the red hat on", that is, I need a test that fails. I figured, I'll need a method that finds all the non-numerical fields in a line. So I write a simple test, of course it fails to compile immediately, so I start writing the function itself, and after a couple of cycles back and forth (red/green) I have a working function and a complete test.

Next, I continue with a function, "gatherNonNumericColumns" that reads the file, one line at a time, and calls my "findNonNumericFields"-function on each line to gather up all the columns that eventually must be removed. A couple of red-green-cycles, and I'm done, having again, a working function and a complete test.

Now, I figure I should refactor. Since my method "findNonNumericFields" was designed only because I figured I would need it when implementing "gatherNonNumericColumns", it seems to me that it would be reasonable to let "findNonNumericFields" become private. However, that would break my first tests, since they would no longer have access to the method they were testing.

So, I end up with a private methods, and a suite of test that test it. Since so many people advice that private methods should not be tested, it feels like I've painted myself into a corner here. But where exactly did I fail?

I gather I could have started out at a higher level, writing a test that tests what will eventually become my public method (that is, findAndFilterOutAllNonNumericalColumns), but that feels somewhat counter to the whole point of TDD (at least according to Uncle Bob): That you should switch constantly between writing tests and production code, and that at any point in time, all your tests worked within the last minute or so. Because if I start out by writing a test for a public method, there will be several minutes (or hours, or even days in very complex cases) before I get all the details in the private methods to work so that the test testing the public method passes.

So, what to do? Is TDD (with the rapid red-green-refactor cycle) simply not compatible with private methods? Or is there a fault in my design?

答:
I've experienced this and felt your pain.

My solution was to:

stop treating tests like building a monolith.

Remember that when you have written a set of tests, lets say 5, to drive out some functionality, you don't have to keep all of these tests around, especially when this becomes part of something else.

For example I often have:

low level test 1

code to meet it

low level test 2

code to meet it

low level test 3

code to meet it

low level test 4

code to meet it

low level test 5

code to meet it

so then I have

low level test 1

low level test 2

low level test 3

low level test 4

low level test 5

However if I now add higher level function(s) that call it, that have a lot of tests, I might be able to now reduce those low level tests to just be:

low level test 1

low level test 5

The devil is in the details and the ability to do it will depend on the circumstances.


问:
What is VLA and how it is implemented?
Most languages does support variable length arrays on the heap but I've heard that those can be stored on the stack too. I'm curios what languages support this construct, some examples and how are they implemented?

I would prefer examples from modern languages as I don't want to code in legacy (like 'C', etc.).

答:
The term "variable-length array" is actually specific to C, but it sounds like you're mostly interested in dynamic stack allocation. To review, VLAs are a feature added in C99 (then made optional in C11) which allow an array to be declared with a size not known until runtime. This means that the language must allocate space at runtime once it knows what the size is. It is not specified whether this runtime allocation is done on the heap or the stack. GCC happens to use the stack, but that's not required.

So from a language-lawyer point of view, the correct answer is probably "no language does" because I'm not aware of any standard feature in any language that must be implemented via stack allocation. From a practical point of view, I'm under the impression this is a very C-specific feature (many people see this as a standard replacement for the alloca() function), and dynamic stack allocation isn't generally viewed as a very appealing or desirable feature for other languages, partly because it's very easy to blow out the stack with it, it adds yet another complication to memory management, and there's not much of a need to have it in the first place.

If you're primarily interested in "modern" languages, a lot of those are garbage collected nowadays, so whether heap/stack/static/cloud/aether allocation is being used is an implementation detail you should (almost) never have to think about. I don't believe any of Rust, Go or Swift have explicit stack allocation either (even the ones that allow an array's size to be unknown until runtime also seem to treat that array more like a std::vector than a "raw" array).

Title: What is VLA and how it is implemented?


问:
What default features is Windows missing that are required for programmers?
What features is Windows missing that can improve the productivity of a programmer. I know a lot of people would want some features from Linux and OSX ported or being created for Windows as well. What is missing for you ?

An example would be the option to have multiple workspaces. When not having the possibility to use dual monitors that can come in handy.

答: BASH, sed, grep, awk, find and ssh</p>

Body: BASH, sed, grep, awk, find and ssh


问:
How does Polyglot Persistence handle relational data?
I have recently been studying up on microservices, and an associated idea that I've seen is that of polyglot persistence and microservices working with their own databases, or whatever storage they may be using. My question is how does this model handle relational data that may span more than one service?

For example, say you have a microservice for dealing with Customer information, and one dealing with Orders. I would assume an Order would need a way to reference a given Customer in its storage model, which I believe a relational database handles well, but if these microservices truly have independent databases, how is this handled?

As a side note, I've worked with one large relational database in my organization for the last three years, so I could understand if my thinking has been clouded by repetition, but I am very eager to understand this concept. Thanks!

答:
If you're designing the services to be truly independent, then they're going to have to serve identity functions...ways to uniquely identify entities, so that they might be externally referred to.  After all, the service is the boundary and you're not supposed to see through it to the underlying store.  That identity really, really shouldn't change in the face of entity mutation - so it shouldn't be made up of mutable data - such as a customer's name or an employee name.

A particular application that uses the identity may map it or may use it as provided.  Each application developed would be free to map-or-use the identity in a manner consistent with it's purpose.  There is no reason to assume some kind of globalized/centralized mapping.  If a centralized mapping feels right, then a centralized, relational store for all the microservices has got to feel even more right.

It gets complicated if the services need to support guarantees...like "I won't destroy this entity until you release it" - which could be a long- or short-lived promise.  This is kinda like declaratively externalizing the idea of a foreign key.  Applications would have to be able to release the protection on entities when all references are removed; and that can get complicated.

For example, imagine service s serving entity e, and application a wants to refer to e and wants a guarantee that e won't perish, then a needs an unique way to refer to the contextual protection of e.  This way, a can inform s when e is no longer needed.  Reference counting (in s or in a)isn't good enough.

Possibly, s can simply deprecate e and not destroy it - maybe mark it as logically deleted.  This obviates the complexity of externalizing the foreign key relationship but makes data live longer than it might have to otherwise.

Another strategy a might employ is just getting over the fact that a referred-to entity is no longer there.  That's not so simple either.

In short - applications relying on a multiplicity of microservices have their work cut out for them - the services may or may not play well in such an environment.  One wonders if the joy of picking one's own data store when building a service is outweighed by the complexity visited upon the service's consumers.  The strategies chosen are going to be dictated one's ability to tailor the service's behavior to the needs of applications.  If we're talking about microservices from "out there", then the applications are going to have to be that much more robust, and may even decide to replicate substantial portions of the data provided by the service.


问:
How to make absolute jumps relative?
Given I have x86 assembly code disassembled into a list of structures that fully describe it (opcode, regs, imm, etc.), how can I programmatically turn absolute jumps to relative jumps?

Basically what I try to achieve is that I can insert or delete bytes between jump and destination and afterwards fix the jump simply by adding the delta to it.

答:
Lets start out by making a distinction between assembly and machine code.  These are often seen as something very similar, and they are, but there's a step between the assembly language and the machine language - that of the assembler.

People don't work in machine code anymore (they did long ago) its too much of a pain. Working with an assembler gives you the ability to use labels and in some cases, higher level opcodes. The assembler can then translate the labels and higher level opcodes into machine code.

When you disassemble a binary, you are getting the translation of the machine code back into assembly. But not all of the information for assembly is there. In particular, the labels are not there.  Without the labels, you have jump and read addresses that are fixed.  That was what the assembler did converting the initial assembly into machine code.

If you want to get the labels back, you need to add them. Go through and label every address that is used as a jump or branching target. Then convert every use of that address to its label and go forward from there.  Remember that you need to add all of the labels and do this because you will be shifting thing around.  Adding an instruction or two near the top will push everything after it to a new address.

Title: How to make absolute jumps relative?


答:
(...) microservices working with their own databases

As always with architecture decisions, there is trade-off between various objectives. One of the key objectives of microservices is to have independently deployable components. At one extreme, this means separating not only the code bases, but also the respective data persistence.

Although viable in some scenarios, I would contest this approach if the microservices pertain to the same backend system. In such a case it seems perfectly valid that several microservices share their data in the same database, as long as they have well-defined scopes and responsibilities - and still achieve the objective of independent deployability.

I would assume an Order would need a way to reference a given Customer in its storage model, which I believe a relational database handles well, but if these microservices truly have independent databases, how is this handled?

Each microservice needs to offer a way for related object identity -- some reference key, e.g. as a external_ref field. The reference key should be independent from the actual model or primary key of the (foreign) microservice's database as to keep the services decoupled from internal changes, data migrations etc. Typically, the keys employed are either surrogate keys or natural/business keys.

Example

Order microservice references a Customer object

the /order/<key> resource will contain a customer_ref, e.g. of the form <foreign service>:<foreign key>, specifically customer:123456

the /customer/<key> resource may contain a collection of orders it knows of, say as a relation customerdb.Orders(customer_id, order_ref), where order_ref is of the same form as above, order:abc3566 (I'm using abc3566 to indicate the key schemes need not be the same).

Using this approach, a service broker in your architecture can resolve any such _ref into an actual service call and return the respective foreign object. Note that I'm blatantly assuming a REST-style set of microservices here, but the principle applies to any other kind of services.

Issues

Typically there are several issues that arise from this:

How to join data efficiently?

In a relational model, joins are done by the database, and thus are relatively efficient. To achieve the same here, you need to subset both service's result to a minimum subset and the join the data client side. Typical strategies also involve caching to avoid querying the same (potentially rather static) data over and over again.

How to keep a set of microservices data free of redundancies?

Essentially, the same concept as with any relational database design apply. However, due to caching strategies, data redundancies are sometimes introduced and need to be dealt with. One approach is to introduce webhooks (i.e. callbacks) so that a service can notify a dependent service of changes.

How to limit the impact of such an architecture on complexity? (in particular dependency management, deployment, testing)

As always with any architecture decisions, there are trade-offs in microservices. It is up to the architect(s) to evaluate the pro & cons of such an approach. Over time, we will find abstractions and frameworks that deal with the most common complexities in a way that will essentially remove the downsides while keeping the advantages.

Title: How does Polyglot Persistence handle relational data?

Body:
I have recently been studying up on microservices, and an associated idea that I've seen is that of polyglot persistence and microservices working with their own databases, or whatever storage they may be using. My question is how does this model handle relational data that may span more than one service?

For example, say you have a microservice for dealing with Customer information, and one dealing with Orders. I would assume an Order would need a way to reference a given Customer in its storage model, which I believe a relational database handles well, but if these microservices truly have independent databases, how is this handled?

As a side note, I've worked with one large relational database in my organization for the last three years, so I could understand if my thinking has been clouded by repetition, but I am very eager to understand this concept. Thanks!


问:
How do you respond to a request for 'emergency contact information' for when you are on vacation?
I want to go on vacation and not think about work at all but they want me to provide them a contact number in the event of an emergency (server goes down, web service malfunctions, etc). I am afraid that it will be abused (they will contact me before trying everything for example) but I also think that if I am on vacation I should not be bothered even if there is an outage. Does anyone have experience with situations like this? What's the tactful approach? Any creative solutions?</p>

答:
If you are really the only person who can fix their problem then they really need to rethink a lot of things. What if you quit? What if you died?

The should not need to call you.