问:
How do you unit-test code using graph structures?
I am writing (recursive) code that is navigating a dependency graph looks for cycles or contradictions in the dependencies. However, I am not sure how to approach unit testing this. The problem is that one of our main concerns is will the code handle on all the interesting graph structures that can arise and making sure that all nodes will be handled appropriately.

While usually 100% line or branch coverage is sufficient to be confident that some code works, it feels like even with 100% path coverage you'd still have doubts.

So, how does one go about selecting graph structures for test cases to have confidence that their code could handle all the conceivable permutations you'll find in real world data.

PS- If it matters, all edges in my graph are labelled "must have" xor "cannot have" and there are no trivial cycles, and there is only one edge between any two nodes.

PPS- This additional problem statement was originally posted by the question's author in a comment below:

For all vertices N in forest F, for all vertices M, in F, such that if there are any walks between N and M they all must either use only edges labelled 'conflict' or 'requires'.

答:
My understanding of the problem, as originally stated and then updated by comments under Macke's reply, includes the following: 1) both edge types (dependencies and conflicts) are directed; 2) if two nodes are connected by one edge, they must not be connected by another, even if it is of the other type or in reverse; 3) if a path between two nodes can be constructed by mixing edges of different types, then that is an error, rather than a circumstance that is ignored; 4) If there is a path between two nodes using edges of one type, then there may not be another path between them using edges of the other type; 5) cycles of either a single edge type or mixed edge types are not permitted (from a guess at the application, I am not sure that conflict-only cycles are an error, but this condition can be removed, if not.)

Furthermore, I will assume the data structure used does not prevent violations of these requirements being expressed (for example, a graph violating condition 2 could not be expressed in a map from node-pair to (type, direction) if the node-pair always has the least-numbered node first.) If certain errors cannot be expressed, it reduces the number of cases to be considered.

There are actually three graphs that can be considered here: the two of exclusively one edge type, and the mixed graph formed by the union of one of each of the two types.  You can use this to systematically generate all graphs up to some number of nodes. First generate all possible graphs of N nodes having no more than one edge between any two ordered pairs of nodes (ordered pairs because these are directed graphs.) Now take all possible pairs of these graphs, one representing dependencies and the other representing conflicts, and form the union of each pair.

If your data structure cannot express violations of condition 2, you can significantly reduce the cases to be considered by only constructing all possible conflict graphs that fit within the spaces of the dependency graphs, or vice-versa. Otherwise, you can detect violations of condition 2 while forming the union.

On a breadth-first traversal of the combined graph from the first node, you can build the set of all paths to every reachable node, and as you do so, you can check for violations of all the conditions (for cycle detection, you could use Tarjan's algorithm.)

You only have to consider paths from the first node, even if the graph is disconnected, because paths from any other node will appear as paths from the first node in some other case.

If mixed-edge paths can simply be ignored, rather than being errors (condition 3), it is sufficient to consider the dependency and conflict graphs independently, and check that if a node is reachable in one, it is not in the other.

If you remember the paths found in examining graphs of N-1 nodes, you can use them as the starting point for generating and evaluating graphs of N nodes.

This does not generate multiple edges of the same type between nodes, but it could be extended to do so. This would greatly increase the number of cases however, so it would be better if the code being tested made this impossible to represent, or failing that, filtered out all such cases beforehand.

The key to writing an oracle like this is to keep it as simple as possible, even if that means being inefficient, so that you can establish trust in it (ideally through arguments for its correctness, backed up by testing.)

Once you have the means to generate test cases, and you trust the oracle you have created to accurately separate the good from the bad, you might use this to drive the automated testing of the target code. If that is not feasible, your next best option is to comb through the results for distinctive cases. The oracle can classify the errors it finds, and give you some information about  the accepted cases, such as the number and length of paths of each type, and whether there are any nodes that are at the start of both types of path, and this could help you look for cases you have not seen before.


问:
How to save many changes to database data?
I'm working on a feature which will pull many (say, 1000-5000) rows from a database, display them to the user and allow the user to edit them.  This editing may involve changing text; adding rows at the beginning, middle or end of the dataset; and/or deleting rows.  The order of the rows is important (i.e., I can't just shift a newly-added row to the end of the dataset.)

At any point in the editing, the user can hit a save button, at which point the changes are recorded in the database.

Here's where my question comes in: is there an accepted pattern for how to do this type of thing?  Thus far, I've just been deleting all the originally-pulled rows and then reinserting the newly-saved rows.  However, as each row has an auto-incremented ID, I'm starting to get worried (you can see how a single user could easily blow through 100,000 IDs in no time at all).  At some point in the future, I may also have foreign keys which depend on these rows IDs and of course this method will wreak havoc on them.

On the other hand, modifying each individual changed row--including keeping track of deletions and additions--seems like a recipe for mistakes.

答:
Some options I would consider (not exclusive choices):

use code that actually does an UPDATE when the record already exists.  The user is updating the record so an UPDATE statement would match that better than manually deleting and re-insert which is fraught with danger and error-prone.

use a temp table (with the same table structure) to store the transactions that are being presented to the user, plus any changes they make while editing.  When they finally save, perform all the validations on the records in this table and then apply them to the base table.

using an audit table to store deleted records before deleting them.

mark records as pending delete now and then have a background job that runs at an appropriate interval (could be hourly, could be monthly, depends on the need) to actually delete (or archive) them.

Make sure, if your database allows it, to create and enforce foreign key constraints, so you don't leave 'dangling' records as you mention.  Keeping a relational database in good shape when you delete records is just one of the practices you need to learn and apply when using databases.

Also, particularly in the case of delete and re-insert, if you use a large enough GUID, millions of records a second would not be an issue.  Getting unique id's shouldn't be an issue that drives your architectural design.

OwnerDisplayName: Swiftheart

ContentLicense: CC BY-SA 3.0


问:
Many different classes that need similar functionality. Best approach?
I have many (at the moment around 30) different message classes in an application I am creating. Each of these messages need to be serialized and deserialized. However, the process of serializing and deserializing is mostly unique to each class (the format of the serialized message is specified in a document, and says which bits in the message goes where), but some are pretty straight forward and use the same process.

I thought about having each of them implement an interface, with Serialize() and Deserialize() methods. However, it seems cumbersome to implement this for each message, especially when many of the messages do in fact use the same code to do this serializing and deserializing.

I thought about having a class that contains a Dictionary<T, Func<IMessage, byte[]>, and a function to map a function to serialize to a specific type. Then use this dictionary to get the appropriate serializer.

Any thoughts? How would you go about this problem?

答:
Sounds like  you can cover this using straight-forward inheritance. Create a base class with the common implementation, and create subclasses that override that implementation with their own, unique behaviour.

You can read up on MSDN here.

EDIT: I figured I should elaborate with an example.
Define a base class that implements the default behavior of your (de)serialization.

```<code>abstract class Message {
   public virtual void Serialize() {
      // Default logic here
   }

   public virtual void Deserialize() {
      // Default logic here
   }
}
</code>```

Your derived class can then override these methods and specifiy their own behaviour if necessary.

```<code>class SpecialMessage : Message {
   public override void Serialize() {
      // Specific logic here
   }

   public override void Deserialize() {
      // Specific logic here
   }
}
</code>```

Any classes that inherit from the Message class that do not override these methods, will use the default methods defined in the base class.

Body:
Sounds like  you can cover this using straight-forward inheritance. Create a base class with the common implementation, and create subclasses that override that implementation with their own, unique behaviour.

You can read up on MSDN here.

EDIT: I figured I should elaborate with an example.
Define a base class that implements the default behavior of your (de)serialization.

abstract class Message {
   public virtual void Serialize() {
      // Default logic here
   }

   public virtual void Deserialize() {
      // Default logic here
   }
}

Your derived class can then override these methods and specifiy their own behaviour if necessary.

class SpecialMessage : Message {
   public override void Serialize() {
      // Specific logic here
   }

   public override void Deserialize() {
      // Specific logic here
   }
}

Any classes that inherit from the Message class that do not override these methods, will use the default methods defined in the base class.


答:
I would recommend putting the serialization and deserialization code in separate classes rather than the data model classes like it sounds like you are thinking.

I'm solving a similar problem now and I have three distinct groups of classes. The first is the model for the messages that contain the various message fields. There's no validation or logic in them, they are really DTOs. All of the messages inherit from a marker interface.

The next group of classes is the serialization and deserialization class. I have two interfaces - one for serialization and one for deserialization. They each have one method that accepts a Message and returns a generic time (for serialization) and one method that returns a Message and accepts a generic type. I implement this interface in serializers and deserializers for XML Strings (the generic is of type String), binary (the generic is of type byte[]), and Properties (the generic is of type Properties).

The final group of classes is my validators. These are more of a logical grouping that ensure the messages are in accordance with the spec. They return List of validation issues, returning an empty List if validation is successful.

The advantage of this approach is that it completely decouples your message data from your message rules from your transformation processes and makes it trivial to establish new sets of rules or serialization/deserialization formats.

Title: Many different classes that need similar functionality. Best approach?

Body:
I have many (at the moment around 30) different message classes in an application I am creating. Each of these messages need to be serialized and deserialized. However, the process of serializing and deserializing is mostly unique to each class (the format of the serialized message is specified in a document, and says which bits in the message goes where), but some are pretty straight forward and use the same process.

I thought about having each of them implement an interface, with Serialize() and Deserialize() methods. However, it seems cumbersome to implement this for each message, especially when many of the messages do in fact use the same code to do this serializing and deserializing.

I thought about having a class that contains a Dictionary<T, Func<IMessage, byte[]>, and a function to map a function to serialize to a specific type. Then use this dictionary to get the appropriate serializer.

Any thoughts? How would you go about this problem?


问:
Trying to learn about core concepts in Javascript by making a web app, how can I do that without jumping to MVC frameworks?
For some reason I don't want to jump to a javascript MVC framework to make a web app for my portfolio. My main motive behind that is to learn in & outs of Javascript. What are my options if I want to make a web app without using MVC? Keep in mind, I still know basic concepts but I want to dig deeper into Javascript. </p>

答:
Most of the big JavaScript frameworks tend to include the following features:

Ajax API

Event manager/listeners

Promises

Class system

DOM widgets

Templating

My recommendation is to study and implement each of these concepts. As you work through them, you'll find that most of them tie together on some fundamental level. Ajax and Promises work well together, so do templating and DOM widgets, DOM widgets and event managers, etc. Once you understand these concepts and how they work together, you'll see that there's nothing special about MVC/MVVM frameworks. It's all just JavaScript.

You'll also learn quite a bit about JavaScript in general as certain components tend to emphasize certain parts of the language. Implementing a class system will give you a deep understanding of JavaScript's prototypes, Promises will open your mind to new model of asynchronous programming, templates will test your string manipulation and regular expression skills, and so forth.

You're already on the right track. Making a web app using an MVC framework only proves that you can use that one framework. By understanding the core components, you should be able to pick up and adapt to any framework future employers are using.


问:
Calling RESTful service from browser and handling 4xx class responses
I have a resource called Logins, and I invoke a GET via AJAX from the browser.  The current behavior is to return a 404 when the specific login cannot be found.

I'm using this to check whether a username is in use or not.  (when creating a new user, the username must be unique)

```<code>http://localhost/Logins/Matthew
</code>```

The AJAX code works fine and can read the response's status code, however, the browser's (firefox) console flags it as an error (all 404s are).  This leads me to believe that it may not be the best idea to give non-successful type responses to browsers.

I'd rather not have the browser treat this as an exceptional case and flag it as an error in the console, mainly because it's alarming when the developer tools light up.

Should I:

Ignore the console, 404s are perfectly valid in this case.

Avoid using 4xx status code responses and return a less 'restful' response.

Change the resource as Logins doesn't make sense for the use-case.

Something else...

Browsers are the only consumer of this API, so I can change it anyway that best suits them.

答:
404 is perfectly fine for this use case. 4xx status codes are client error codes, so browser treats them as such, and that is perfectly fine too.

Another kind of APIs (e.g. JSON-RPC) use different approaches, but since you're going RESTful, do not change response code just to make console output look pretty - it is not a use case, users are not supposed to inspect console output.


问:
Mutable with logic inside or immutable with logic outside?
I know immutable objects are preferred to mutable objects for reasoning and maintenance. But in occasions making a class immutable have some costs, let me explain it with a simple example:

```<code>class Mutable
{
    private int i;
    public Mutable(int _i) { i = _i}
    public void Print() // impure function
    {
         print(i);
         i++;   // logic inside
    }
}

... caller

Immutable m = new Mutable(1);
while (true)
{
    m.Print();
}
</code>```

I call it mutable because its internal state changes and Print varies on each call.

Versus.

```<code>class Immutable
{
    private int i;
    public Mutable(int _i) { i = _i}
    public int Print() // pure function
    {
         print(i);
         return i;  // returns its state
    }
}

... caller
int i =1;
while (true)
{
    Immutable im = new Immutable(i);
    i = im.Print(); // get the previous obeject's state
    i++; // logic outside
}
</code>```

Preferring immutable solution make me to do a lot of work outside of an object which was supposed to encapsulate that logic.

My actual problem (you may skip the example below)

I have a class which draws a text in a box:

```<code>class TextDrawer
{
   private string text;
   private Rectangle box;
   public TextDrawer(_text, _box) {...}
   public void DrawTextInBox()
   {
   }
}
</code>```

It is probable that the Text overflows from the box and part of it wasn't written. I should know the remaining unwritten part to draw it in a new box.(in fact I take an snapshot of the old box as a video frame and need to make a new frame for the remaining text to make a video of the text)

Now I have two options:

1- Make the TextDrawer mutable (its internal state is mutable) and DrawTextInBox a non-pure function and on each call it draws the remaining part of text in the box.

```<code>class TextDrawer
{

   private string text;
   private Rectangle box;
   private string remaining;

   public TextDrawer(_text, _box)
   {
      remaining = text = _text;
      box = _box;
   }
   public void DrawTextInBox()
   {
      draw as you can from remaining
      update remaining part;
   }
}
</code>```

2- Keep the TextDrawer immutable and DrawTextInBox a pure function which returns the remaining part, then the caller should keep track of the remaining and pass it again to the TextDrawer or instantiate a new TextDrawer by the remaining text until there is nothing to write.

```<code>class TextDrawer
{
   private string text;
   private Rectangle box;
   public TextDrawer(_text, _box) { ... }
   public string DrawTextInBox()
   {
       draw as you can from text;
       return remaining part;
   }
}
</code>```

The second approach is immutable but actually shifts the responsibility of tracking the remaining part outside of the class. The first approach is mutable, but it encapsulates the logic and just need a call to DrawTextInBox to draw the remaining part.

Question: The logic of the above examples was simple but this logic could be more complex, then I don't know whether to extract this logic from a class in order to make it immutable or let it be mutable and have the logic inside?

答:
In your example:

```<code>int i =1;
while (true)
{
    Immutable im = new Immutable(i);
    i = im.Print(); // get the previous obeject's state
    i++; // logic outside
}
</code>```

Immutable should return the new state that the mutable solution would've stored, i.e. i + 1. You already have the old state. Returning the old state and forcing the caller to update the state himself is pointless. You did this correctly in your real problem, where you return the remaining part of the string instead of the original string.

You should prefer an immutable solution unless you have good reasons otherwise. A good reason may be that the immutable solution is too slow, but first you have to clearly define what's "fast enough" and profile the application to show that the bottleneck is your immutable class and that the mutable alternative will solve the problem.

Body:
In your example:

int i =1;
while (true)
{
    Immutable im = new Immutable(i);
    i = im.Print(); // get the previous obeject's state
    i++; // logic outside
}

Immutable should return the new state that the mutable solution would've stored, i.e. i + 1. You already have the old state. Returning the old state and forcing the caller to update the state himself is pointless. You did this correctly in your real problem, where you return the remaining part of the string instead of the original string.

You should prefer an immutable solution unless you have good reasons otherwise. A good reason may be that the immutable solution is too slow, but first you have to clearly define what's "fast enough" and profile the application to show that the bottleneck is your immutable class and that the mutable alternative will solve the problem.


问:
What should be kept in mind when writing a garbage collector?
I would like to learn the important concepts behind garbage collector design. My priority is conceptual clarity, not runtime efficiency. In particular, I would like to know:

What data structures are used in a garbage collector?

How much overhead per object is acceptable?

What triggers the garbage collection process?

How "stopping the world" is actually implemented?

答:
Read the garbage collection wikipage then the GC handbook. Perhaps also the old P.Wilson uniprocessor GC survey

I'm not sure that your question makes a lot of sense. In practice, a GC is tightly related to the language implementation (or the bytecode virtual machine) it is targeting. Real world GC for compiled languages require deep cooperation between compiler and GC.

Some few GCs are conservative, they suppose that everything which looks like a pointer may be a GC-ed pointer. A typical example is Boehm conservative garbage collector, but its implementation is tricky. I believe that coding conservative GCs is practically quite tricky (you need to deeply understand how the address space is managed on your computer).

Most GC are precise, they need to know exactly all the pointers (to GC-ed values) on the call stack. This is often language specific. You could have C++ classes or C macros for that, like in my qish GC (which is a generational copying GC).

I would study several toy Scheme implementations, they usually have a GC.

your questions

What data structures are used in a garbage collector?

I'm not sure it is relevant. You just need to be able to traverse the pointers for local variables (on some call stack, either the machine call stack or more frequently your interpreter's call stack), and you need to scan every pointer in your values (or GC-ed objects). Of course, your GC-ed object may have some (language specific) data structure. And some GCs have complex data structures for efficiency reasons (answering quickly to the two main questions: what are the local pointers in this call frame? what are the pointers in this object or GC-ed value).

How much overhead per object is acceptable?

Define what you mean by overhead. Some GC-s don't have per object overhead (especially because the language may bring its own overhead -i.e. metadata, class pointer, vtable- which can partly be reused by the GC). They can have a system-wide overhead (e.g. naive copying GC are using only half of the memory).

What triggers the garbage collection process?

It depends. Usually the object allocator could trigger GC, e.g. get some memory and if none is available, do a GC.

How "stopping the world" is actually implemented.

"Stop the world" is a property of GCs (the simplest ones, which complete the GC while application code -a.k.a. mutator- is stopped).

missing questions

You forgot several questions.

What is a closure, and why functional languages want a GC? Read also about continuations....

How to make a multi-threaded friendly GC?

These questions are documented.

Notice that GC are conceptually easy (they are just scanning entirely a graph of references). A naive mark-and-sweep GC for a language whose implementation you know intimately (e.g. that you are implementing right now) is very easy to code (a few dozens or hundreds of lines). What is hard is to debug them and to implement efficient GCs.

A mark&sweep GC is conceptually very easy:

keep a mark flag or bit in every GC-ed value; allocate values with the mark bit cleared

be able to iterate on every values (even the dead ones). You could e.g. allocate fixed-size values in a big array, or manage a linked list or a vector of (pointers, internal to the GC, to) them.

code a recursive marking routine (which avoid marking twice by skipping already marked values)

mark all the local and global variables, and recur the marking routine (mark phase)

delete every non-marked value (sweep phase)

Copying (notably generational copying) or compacting GC are a bit more complex. You'll probably need to understand what is a write barrier and you'll need to implement it.

Debugging a GC could be painful. With a recent gdb, you could use watchpoints and script the debugger in Python.

practical advice

Code a tiny interpreter (e.g. of some tiny Lisp, or some language with closures or objects) and code the GC at the same time, while reading some GC books.


问:
How much work should I place inside a lock statement?
I am a junior developer working on writing an update for software that receives data from a third-party solution, stores it in a database, and then conditions the data for use by another third-party solution. Our software runs as a Windows service.

Looking at the code from a previous version, I see this:

```<code>        static Object _workerLocker = new object();
        static int _runningWorkers = 0;
        int MaxSimultaneousThreads = 5;

        foreach(int SomeObject in ListOfObjects)
        {
            lock (_workerLocker)
            {
                while (_runningWorkers >= MaxSimultaneousThreads)
                {
                    Monitor.Wait(_workerLocker);
                }
            }

            // check to see if the service has been stopped. If yes, then exit
            if (this.IsRunning() == false)
            {
                break;
            }

            lock (_workerLocker)
            {
                _runningWorkers++;
            }

            ThreadPool.QueueUserWorkItem(SomeMethod, SomeObject);

        }
</code>```

The logic seems clear: Wait for room in the thread pool, make sure the service hasn't been stopped, then increment the thread counter and queue the work. _runningWorkers is decremented inside SomeMethod() inside a lock statement that then calls Monitor.Pulse(_workerLocker).

My question is:
Is there any benefit in grouping all the code inside a single lock, like this:

```<code>        static Object _workerLocker = new object();
        static int _runningWorkers = 0;
        int MaxSimultaneousThreads = 5;

        foreach (int SomeObject in ListOfObjects)
        {
            // Is doing all the work inside a single lock better?
            lock (_workerLocker)
            {
                // wait for room in ThreadPool
                while (_runningWorkers >= MaxSimultaneousThreads)
                {
                    Monitor.Wait(_workerLocker);
                }
                // check to see if the service has been stopped.
                if (this.IsRunning())
                {
                    ThreadPool.QueueUserWorkItem(SomeMethod, SomeObject);
                    _runningWorkers++;
                }
                else
                {
                    break;
                }
            }
        }
</code>```

It seems like, it may cause a little bit more waiting for other threads, but then it seems like locking repeatedly in a single logical block would also be somewhat time-consuming. However, I'm new to multi-threading, so I'm assuming that there are other concerns here that I'm unaware of.

The only other places where _workerLocker gets locked is in SomeMethod(), and only for the purpose of decrementing _runningWorkers, and then outside the foreach to wait for the number of _runningWorkers to go to zero before logging and returning.

Thanks for any help.

EDIT 4/8/15

Thanks to @delnan for the recommendation to use a semaphore. The code becomes:

```<code>        static int MaxSimultaneousThreads = 5;
        static Semaphore WorkerSem = new Semaphore(MaxSimultaneousThreads, MaxSimultaneousThreads);

        foreach (int SomeObject in ListOfObjects)
        {
            // wait for an available thread
            WorkerSem.WaitOne();

            // check if the service has stopped
            if (this.IsRunning())
            {
                ThreadPool.QueueUserWorkItem(SomeMethod, SomeObject);
            }
            else
            {
                break;
            }
        }
</code>```

WorkerSem.Release() is called inside SomeMethod().

答:
The other answers are quite good and clearly address the correctness concerns. Let me address your more general question:

How much work should I place inside a lock statement?

Let's start with the standard advice, that you allude to and delnan alludes to in the final paragraph of the accepted answer:

Do as little work as possible while locking a particular object. Locks that are held for a long time are subject to contention, and contention is slow.  Note that this implies that the total amount of code in a particular lock and the total amount of code in all lock statements that lock on the same object are both relevant.

Have as few locks as possible, to make the likelihood of deadlocks (or livelocks) lower.

The clever reader will note that these are opposites. The first point suggests breaking up big locks into many smaller, finer-grained locks to avoid contention. The second suggests consolidating distinct locks into the same lock object to avoid deadlocks.

What can we conclude from the fact that the best standard advice is thoroughly contradictory?  We get to actually good advice:

Don't go there in the first place. If you are sharing memory between threads, you're opening yourself up for a world of pain.

My advice is, if you want concurrency, use processes as your unit of concurrency. If you cannot use processes then use application domains. If you cannot use application domains, then have your threads managed by the Task Parallel Library and write your code in terms of high-level tasks (jobs) rather than low-level threads (workers).

If you absolutely positively must use low-level concurrency primitives like threads or semaphores, then use them to build a higher-level abstraction that captures what you really need. You will likely find that the higher level abstraction is something like "perform a task asynchronously that can be cancelled by the user", and hey, the TPL already supports that, so you don't need to roll your own.  You will likely find that you need something like thread safe lazy initialization; don't roll your own, use Lazy<T>, which was written by experts. Use threadsafe collections (immutable or otherwise) written by experts. Move the level of abstraction up as high as possible.


问:
Recording many data items and retrieving time-based snapshots
I have a .Net Windows application that continually receives data from a hardware device. Additionally, the user can change various settings and parameters from within the software. I would like to record all of this data to some form of database - both the data arriving from the h/w, and settings that the user changes. The intention is to allow the user to select a point in time and see a "snapshot" of the h/w data values and software settings present at that moment in time.

Numbers:- There will be approx 200 values to record, and it should be possible to get a snapshot at a resolution of 100ms (which is roughly the interval that data is received from the h/w). Additionally users should be able to get a snapshot from up to 60 hours in the past.

One solution I thought of was to record values only as they change (e.g. as data is received from the h/w, or when the user changes a setting). However using this approach, I can see it being quite tricky and/or slow to recreate a snapshot from the stored data. I would have to find the last recorded value for each of the 200 data items, prior to the requested snapshot time. I suspect this would also require the data tier needing knowledge of what data points constitute a snapshot (ideally the "service" should be pretty dumb, and just persist any values thrown at it, and return snapshots as requested).

I also thought of maintaining a cache of the 200 data values in the application, which I update whenever some action occurs (h/w data is received, user changes a setting). A separate timer could then push the entire collection to the data tier every 100ms. By storing each of these as a timestamped record then that's my snapshot, making subsequent retrieval trivial. The downside is that it's persisting an awful lot more data, especially as the vast majority of these 200+ values will rarely change.

Finally, another consideration is how to "truncate" old data, i.e. deleting data that is older than 60 hours as new data comes in.

Okay so it's not "big data", and writing to a database every 100ms isn't particularly intensive, but it's not something that I've had to do before (database experience has generally been limited to CRUD data entry applications!). I would be interested in opinions on the proposed approaches, thoughts on alternative solutions, and any frameworks or databases that might be of use.

答:
Option 3) Snapshots on the Hour (or some interval) + the change log.

Best of both worlds.
Quick build Process a max of 1 hour Changes
Low(er) storage


问:
Clean code principles to jquery
A jquery code could get very long in lines and become an spaghetti code. It could bring problems like chaining functions or many lines for getting data from HTML elements. Also it may create a clutter in your codebase and refactoring it could be a huge pain as well.

In that scenario, I don't know how to keep basic principles of clean code in jquery - like having many few-lines functions as possible and titled with proper names; separate functions in layers such as business layers, data layers, entity layers, etc.

I was advised to look at angular.js as a framework that could help in organizing jquery code. What do you think?

答:
I know what you are talking about and have faced the same problem where chaining functions, getting data from HTML elements etc creates a clutter in your codebase and refactoring it could be a huge pain as well.

Even if you use MVC frameworks out there but you don't organize your code correctly you will still end up with a messy codebase.

What I have been using for myself that worked in practice already is to write your jQuery code in plugin format. This way making it reusable for other projects if needed, cutting down on code duplication, forcing you to encapsulate change and most of all it becomes open for extension but closed for modification - so all of those great practices that make up a clean code base.

```<code>(function($){
    var PluginName = function(element)
    {
        var elem = $(element);
        var obj = this;

        var settings = $.extend({
            someSetting: "someValue"
        });

        this.func1 = function(someArgument)
        {
            some code
        };

        this.func2 = function()
        {
            some more code
        };
    };

    $.fn.pluginName= function()
    {
        return this.each(function()
        {
            var element = $(this);
            if (element.data('pluginName')) return;
            var pluginName = new PluginName(this);
            element.data('pluginName', pluginName);
        });
    };
})(jQuery);
</code>```

Then once you have all of your code in one place that represents one responsibility (SRP) you can go ahead and use it like so:

```<code>       jQuery(this).pluginName();
       var pluginName = jQuery(this).data('pluginName');

       pluginName.func1(someArgument);
</code>```

Title: Clean code principles to jquery

Body:
I know what you are talking about and have faced the same problem where chaining functions, getting data from HTML elements etc creates a clutter in your codebase and refactoring it could be a huge pain as well.

Even if you use MVC frameworks out there but you don't organize your code correctly you will still end up with a messy codebase.

What I have been using for myself that worked in practice already is to write your jQuery code in plugin format. This way making it reusable for other projects if needed, cutting down on code duplication, forcing you to encapsulate change and most of all it becomes open for extension but closed for modification - so all of those great practices that make up a clean code base.

(function($){
    var PluginName = function(element)
    {
        var elem = $(element);
        var obj = this;

        var settings = $.extend({
            someSetting: "someValue"
        });

        this.func1 = function(someArgument)
        {
            some code
        };

        this.func2 = function()
        {
            some more code
        };
    };

    $.fn.pluginName= function()
    {
        return this.each(function()
        {
            var element = $(this);
            if (element.data('pluginName')) return;
            var pluginName = new PluginName(this);
            element.data('pluginName', pluginName);
        });
    };
})(jQuery);

Then once you have all of your code in one place that represents one responsibility (SRP) you can go ahead and use it like so:

       jQuery(this).pluginName();
       var pluginName = jQuery(this).data('pluginName');

       pluginName.func1(someArgument);


问:
should I help QA team in finding bugs?
I Just started my career as Consultant Developer and Joined a small company. When I go through the code for an already developed application, I can see that there are some bugs. I inform the QA team about these and ask them to open defects so that I can fix those bugs but QA team is not taking it in a sportive way..

Is this something unprofessional?

EDIT:
About work environment:
Product Manager wants every small thing to be entered in bugzilla that is the reason I ask QA team to open defects.
Also I don't have access to the QA environment so I ask them to check if a particular branch of code is working fine in their environment..
I can open bugs in the system but I thought this would be more offending than asking them to open..
I just want the product to be bug free..when it leaves the company but no other intentions..

答:
When I go through the code for an already developed application, I can see that there are some bugs

These small bugs should be found during dev testing and after dev testing developer should also run the application to make sure it works as expected before passing it to QA so right there already something is not going very well.

I inform the QA team about these and ask them to open defects so that I can fix those bugs

If the code being deployed to the QA environment is buggy and then you are asking them to pass it back to you so that you can fix it I can totally see why they would be weirded out a bit. It's really about expectations and in this case they were expecting that code being deployed is going to work, if it wasn't working then why was it deployed in the first place?

Communication is key - so if for some reason there is a separation between you and the actual dev team hence this problem then I would recommend talking to them about it and explaining to them the actual mental process that you went through yourself.

If there is no separation between you and the dev team and you were responsible for that code in any way then yes it is unprofessional because it has been released without being thoroughly dev tested.

However, I assume that is not the case because you have found the bugs half way and were just letting them know about it so talking to them and explaining the situation would be the best thing in this case.

Title: should I help QA team in finding bugs?


答:
I worked for a number of high-profile corporations. It was always gladly accepted if a person opens a bug when [s]he discovers a bug. No matter who that person is, and whose responsibility that bug might be.

If somebody could be offended by having a bug filed against their code, these people act utterly unprofessionally. A developer should be as egoless as possible, and a bug report should be as blameless as possible. This applies to post-mortems, too.

There is no blame, and no guilt; only defects that need to be discovered and fixed. I suspect you don't practice code reviews either; you should.

Title: should I help QA team in finding bugs?

Body:
I Just started my career as Consultant Developer and Joined a small company. When I go through the code for an already developed application, I can see that there are some bugs. I inform the QA team about these and ask them to open defects so that I can fix those bugs but QA team is not taking it in a sportive way..

Is this something unprofessional?

EDIT:
About work environment:
Product Manager wants every small thing to be entered in bugzilla that is the reason I ask QA team to open defects.
Also I don't have access to the QA environment so I ask them to check if a particular branch of code is working fine in their environment..
I can open bugs in the system but I thought this would be more offending than asking them to open..
I just want the product to be bug free..when it leaves the company but no other intentions..


问:
Database Design for Inventory
I am starting to design a typical product based MySQL database but I keep running in circles on how to design the tables.

I have many types of products

Jeans

Tshirts

Dresses

Which have many of the same attributes

Size

Price

Colour

But some of the products have specific attributes, like length or logo or package.

Do I make a separate table for every product even though many of the columns are the same, or do I make a general product table and then have some sub-tables for specific products?

答:
One approach would be to design your database similar to how you would define a class hierarchy, where you define base table(s) (classes) that provide the common attributes, and then you add additional tables that provide specific attributes to extend the table to the specific products.

Define a base product table

```<code>create table Products_base as
    id
    item
    brand
    model
    description
    size
    color
    price #more about this later
</code>```

Define additional tables for products that differ substantially

```<code>create table pants as
    base #foreign key to base product
    waist
    length
    material
    style

create table dress as
    base #foreign key to base product table
    waist
    hemlength
    bust
    material
    belt
</code>```

Another approach would be to define the base product table, and then define an attributes table, and provide the attributes for each additional product,

```<code>create table Products_base as
    id autoincrement
    item
    brand
    model
    description
    size
    color
    price #more about this later

create table attributes as
    id autoincrement
    name
    description

create table product_+attributes as
    product_id #product.id
    attribute_id #attributes.id
    value
</code>```

Using pants as an example, you would want to add attributes to pants (add to the attributes table)

```<code>"length", "length of item"
"inseam", "length of inseam"
"style", "style of article" #example, "jeans"
#etc
</code>```

And then you would add values for the attributes

```<code>product.id("pants"), attribute.id("inseam"), "32in"
product.id("pants"), attribute.id("waist"), "34in"
product.id("pants"), attribute.id("style"), "jeans"
product.id("dress"), attribute.id("style"), "sundress"
#etc
</code>```

Title: Database Design for Inventory

Body:
One approach would be to design your database similar to how you would define a class hierarchy, where you define base table(s) (classes) that provide the common attributes, and then you add additional tables that provide specific attributes to extend the table to the specific products.

Define a base product table

create table Products_base as
    id
    item
    brand
    model
    description
    size
    color
    price #more about this later

Define additional tables for products that differ substantially

create table pants as
    base #foreign key to base product
    waist
    length
    material
    style

create table dress as
    base #foreign key to base product table
    waist
    hemlength
    bust
    material
    belt

Another approach would be to define the base product table, and then define an attributes table, and provide the attributes for each additional product,

create table Products_base as
    id autoincrement
    item
    brand
    model
    description
    size
    color
    price #more about this later

create table attributes as
    id autoincrement
    name
    description

create table product_+attributes as
    product_id #product.id
    attribute_id #attributes.id
    value

Using pants as an example, you would want to add attributes to pants (add to the attributes table)

"length", "length of item"
"inseam", "length of inseam"
"style", "style of article" #example, "jeans"
#etc

And then you would add values for the attributes

product.id("pants"), attribute.id("inseam"), "32in"
product.id("pants"), attribute.id("waist"), "34in"
product.id("pants"), attribute.id("style"), "jeans"
product.id("dress"), attribute.id("style"), "sundress"
#etc


问:
How to properly license open source software
I have a project I've been working on which I would like to push into a new, public Github repository as open source. After searching on StackExchange for the right license to use I've settled on the BSD license.

My question is: now what? Do I just need to copy the BSD license text into a file called LICENSE in the root of my repository and call it good? Is that the general rule for how all open source licenses work? Or do I need to notify some other organization that I'm using this license so I can 'enforce' it if necessary?

答:
It is sufficient to attach the licence file in your work. However it is normally preferable to add a small comment stating who owns the copyright and where to find the full licence text at the top of each source file.

You do not need to notify any organisation to enforce your licence if someone breaks the terms, although it will require you to take them to court.

ContentLicense: CC BY-SA 2.5


问:
How does a program talk to a graphics card?
I have heard that GPU's are better at performing certain tasks than a CPU. My question is, how does a program tell a graphics card to process something instead of the CPU? Does the program talk to the GPU directly? Or does the CPU determine what to give the graphics card? It seems like the benefits of processing with a GPU would be negated somewhat if the CPU had to tell the GPU each instruction to process.</p>

答:
My question is, how does a program tell a graphics card to process something instead of the CPU?

The short answer is that every graphics card must come with a driver implementing a standard graphics API such as OpenGL or DirectX, so that the code running on the CPU can call standard functions from that API. When writing your code, this is largely indistinguishable from coding against any other library.

However, a huge part of graphics programming involves passing special programs called "shaders" over to the graphics card, so they can be executed there. These are generally written in a different language specifically designed for shaders.

Does the program talk to the GPU directly? Or does the CPU determine what to give the graphics card?

"The program" is already running on the CPU (and any shaders it provided are running on the GPU) so I'm not sure what this is trying to ask.

It seems like the benefits of processing with a GPU would be negated somewhat if the CPU had to tell the GPU each instruction to process.

Indeed, this can happen. Moving data from the CPU to the GPU is a common bottleneck in graphically intensive programs. In fact, some features in more recent OpenGL versions are specifically designed to generate data on the GPU, so that you don't have to pass as much from the CPU.

Of course, whether the bottleneck is in the CPU, the GPU, or the communication between the two depends entirely on the individual program.


问:
Why do many exception messages not contain useful details?
It seems there is a certain amount of agreement that exception messages should contain useful details.

Why is it that many common exceptions from system components do not contain useful details?

A few examples:

.NET List index access ArgumentOutOfRangeException does not tell me the index value that was tried and was invalid, nor does it tell me the allowed range.

Basically all exception messages from the MSVC C++ standard library are utterly useless (in the same vein as above).

Oracle exceptions in .NET, telling you (paraphrased) "TABLE OR VIEW not found", but not which one.

So, to me it seems that for the most part exception messages do not contain sufficient details to be useful. Are my expectations out of line? Am I using exceptions wrong that I even notice this? Or maybe my impression is wrong: a majority of exceptions do actually provide useful details?

答:
To give a slightly different answer: The offending code has probably been done to spec:

The function receives X and returns Y

If X is invalid, throw exception Z

Add the pressure to deliver exactly to spec (for fear of being rejected in review/testing) in minimum time and with minimum fuss, then you have your recipe for an entirely compliant and unhelpful library exception.

ContentLicense: CC BY-SA 4.0


问:
Two way communication between Form and plugins
I have started to develop a support tool for an old legacy system that is no longer maintained but still needed for the foreseeable future. The legacy system offers few, clunky and error prone methods for basic tasks such as handling users, rules, data imports and settings. The Tool I am developing will be used by the inhouse support team.

I'm using panels to group related tasks together. I intend to load each panel into a TabControl on user request. The main form have a few user settings that affects/ all panels. A panel might perform a task that other panels need to aware of.

Question TL:DR

How can the MainForm be made aware that Panel A have made a significant change that other panels might be interested in?

How can a panel receive information from MainForm?

My thoughts so far:

The MainForm could get information from each Panel by subscribing to custom events (such as a UsersUpdatedEvent). But how to do the other way around since a Panel does not "know" the parent form?

Use example:
Homer wants to add a user and a new rule for company "Duff Beer". He opens the application and selects "Duff beer" from a dropdown list. He then clicks on a "User" toolstrip icon and the user panel opens in a tab. Homer creates user "Lisa" and then switches over to "Rules" panel and creates a new rule that is owned by Lisa.

Bonus question:
I would also like each panel to be able to add menu items to the MainForm Menustrip. Now that I think about it, the application is a bit like a very very lightweight Visual studio. With tabs and menus and toolbars that change depending on whtat the user is watching.

答:
First off, don't use Panels to group related tasks.  Use UserControls.  I used panels to group controls on my first WinForms application, and it turned into a horrible monstrosity, worthy of appearing on TDWTF.  Just...don't do it.

Instead, use UserControls.  These are kind of like Windows, but are hosted inside of a container, similar to a Button or ComboBox.  Normal function calls are used for MainForm to UserControl communication.  Events are used for UserControl to MainForm communication.

For example, you might have a UserControl called CreateNewUser.  It would contain a text box for the new user's name and a button to create a new user's name.  It would also need a way of notifying the MainForm that the button was clicked.

The individual UserControls do not need to know about the MainForm.  All they need to do is provide public (usually custom) events that their parent can then use.  CreateNewUser does not need to care what created it.  All CreateNewUser needs to do is let its parent know when the user has created a new user.

To answer your question..

How can a panel [UserControl] receive information from MainForm?

The panel [UserControl] will provide public functions / methods that the MainForm can call.  The code behind the UI in WinForms is very similar to plain old C# classes.  In fact, they are.  They can have public methods and properties that anyone can call.

Here's the MSDN for WinForms UserControls: https://msdn.microsoft.com/en-us/library/aa302342.aspx


问:
Handling Business Logic/Validation Race Conditions
I'm working on a Training Enrollment web application with the following (simplified) features:

Maintenance (Add, Edit, Deactivation) of Trainings

Student sign-up

Student enrollment to Active Trainings

I've employed Onion Architecture. All my business logic/validation is in the Domain Layer and the Data Access Layer consists of ADO.NET components and stored procedures (no business logic whatsoever). The problem is that since the business logic/validation is in the domain, there may be a race condition in the following scenarios:

Scenario 1

Training A allows a maximum of 50 students only

Training A currently has 49 students enrolled

In the web application, two users are attempting to enroll simultaneously. At this point two threads spawned from two
concurrent requests to the web application are able to query 49
users, so both validations succeed in the Domain layer and both
users are able to enroll.

Scenario 2

Training A is created

Student A is attempting to enroll to Training A

While Student A is attempting to enroll to Training A, a Training Admin decides to deactivate Training A. A race condition occurs when
in the Domain layer, when the following happens:  In thread 1 of
User 1, validating whether Training A is active succeeds (returns
true) and at the same time;

In thread 2, the thread on which Training A is being deactived validates that there are no enrollments yet; so that both
validations succeed.

An obvious solution is to add business logic (checking for enrollment count, checking whether a Training is active) in my stored procedures but I'm hesitant to do this as it breaks the design.

My validation logic has a single point of entry and I can add a lock to such that the validation logic becomes a critical section so only one operation (validation and DB persistence) happens at a time, but this reduces concurrency.

Any alternative approach or how has this solved in the systems you've worked on?

答:
An alternative approach, which may or may not be appropriate for your situation, would be to implement a queue and have a worker thread or process which processes items in the queue. There would always be only a single instance of the worker. The worker processes requests in a FIFO order.

This way you eliminate race conditions - if two users both sign up for the last slot at the same time, only one of them will get it, and you can send a notification to the other one that their request was denied. There are various ways you could do that, one would be to have users redirected to a page which says "request pending" which is updated once their request is processed by the worker thread.


问:
Is directly using Make considered obsolete?
So I have been coming across many comments/posts/etc regarding creating makefiles directly, and how it is a silly thing to do in 2015. I am aware of tools such as CMake, and I actually use CMake quite often. The thing is, CMake is just creating the Makefile for you and helping to remove the tedium of doing it yourself. Of course it adds a lot of other great features... but its still a Makefile in the end.

So my question is, is the 'obsolete' talk regarding make referring to the entire Make utility, or just the idea of manually writing your own Makefiles? I do not use an IDE for C/C++ development at all (just emacs), so I have always written Makefiles.

If Make is considered outdated, what should a C/C++ dev be using to build small, personal projects?

答:
Makefiles are not obsolete, in the same way that text files are not obsolete. Storing all data in plain text is not always the right way of doing things, but if all you want is a Todo List then a plain text file is fine. For something more complicated you might want a more complicated format like Markdown or XML or a custom binary format or anything in between, but for simple cases plain text works fine.

Similarly, if all you want is a way to avoid writing out g++ -g src/*.c -o blah -W -Wall -Werror && ./blah all the time, a hand-written Makefile is perfect!

If you want to build something that is highly portable, where you don't have to manage that portability yourself, you probably want something like Autotools to generate the right Makefile for you. Autotools will detect the features supported by various platforms. A program written in standard C89 built with Autotools will compile virtually everywhere.


问:
What features would you like to have in PHP?
Since it's the holiday season now and everybody's making wishes, I wonder - which language features you would wish PHP would have added? I am interested in some practical suggestions/wishes for the language. By practical I mean:

Something that can be practically done (not: "I wish PHP would guess what my code means and fix bugs for me" or "I wish any code would execute under 5ms")

Something that doesn't require changing PHP into another language (not: "I wish they'd drop $ signs and use space instead of braces" or "I wish PHP were compiled, statically typed and had # in it's name")

Something that would not require breaking all the existing code (not: "Let's rename 500 functions and change parameter order for them")

Something that does change the language or some interesting aspect of it (not: "I wish there was extension to support for XYZ protocol" or "I wish bug #12345 were finally fixed")

Something that is more than a rant (not: "I wish PHP wouldn't suck so badly")

Anybody has any good wishes?

Mod edit: Stanislav Malyshev is a core PHP developer.

答:
1) I would love for newly instantiated objects to return "$this" so I can method chain,
$user = new User('john')->setLastName('Doe')->save();

2) If you've ever used ruby, and most recently node, they have a great interactive shell (IRB).  I would love for PHP to have one that was actually useful.

3) Traits/Mixins, but I hear those are on the way.

4) I want to second the short array $myArray = ['my', 'array'];

5) Consistent naming/order (i.e. needle haystack)

OwnerDisplayName: Jakefolio


答:
I can only tell you what I do. First, if you see a bug then it's "your fault" and you should enter it into the tracker, write a test case proving the bug, fix it, and submit the fix. If the QA teams sees a but then it's "their fault" and should be entered in the tracker with steps to reproduce.

Now it's not really about fault, it's more about long term responsibility. When you do a meeting to talk about progress, who is going to be "boss of that bug". The best answer is usually the one that finds it. Meaning that the team putting in the bug is the one that needs to be going "did you fix X yet".

Even more important is the fact that not every bug is going to be fixed. In the real world you have to manage bugs v.s. time constraints and other costs. It's "ok" to have a bug that's not going to get fixed in the next version, as long as everyone knows where it is, and there's a clear expectation of when it will be fixed.

So, first if you see a bug enter it into the tracker. Handle accordingly. A documented bug is 1000 times better then an undocumented one.

Now as for telling the QA team to do stuff. I personally think it's none of my business what the QA team does. As long as they find bugs and get them into the tracker in a agreed upon format, then I'm good. By having that separation I feel that I maximize their usefulness (finding "bugs" that arise, not because code faults, but because of mis-understandings). If I am giving work direction then I feel like I am not letting them do what they need to do. As have even gone as far as refusing to help them do something in the app so that we could see if the stuff they needed to do to accomplish their task was intuitive enough.

Finally, depending on company structure, you shouldn't be telling QA to do anything. Do they report to you? Do you sign their pay check. More often then not QA is equal to Dev in the corporate ladder. Telling someone to do more work is definitely not your job (unless it is). That does not mean you don't work closely with QA, you should be. At the same time, there not part of "your team" so hands off.

Most importantly from all this is:

If you find a bug, enter it, and handle it. Don't ask others to

Don't give work direction to other teams, it's not your place

Remember, this is very different company to company. This closely matches "what I do". But different companies will do things differently. You should either use their SOP or work with them to develop a SOP document.

I also stayed away from management, organization, or planing techniques. There are a lot of questions answered if you adopt one technique or another.

Title: should I help QA team in finding bugs?


答:
There is no reason you can't apply clean code principals to Javascript. You just need to do it. There are code metrics you can use like https://codeclimate.com/github/jquery/jquery but there a double edged sword.

Structure your code base, DRY your code, apply SRP. Javascript is no different then any other language.

Standard rules of thumb apply like "If you have to copy and paste your doing something wrong."

The biggest thing with Javascript is anonymous functions. Just make sure your DRY your JS code base and the rest will mostly take care of it's self.

Title: Clean code principles to jquery

Body:
A jquery code could get very long in lines and become an spaghetti code. It could bring problems like chaining functions or many lines for getting data from HTML elements. Also it may create a clutter in your codebase and refactoring it could be a huge pain as well.

In that scenario, I don't know how to keep basic principles of clean code in jquery - like having many few-lines functions as possible and titled with proper names; separate functions in layers such as business layers, data layers, entity layers, etc.

I was advised to look at angular.js as a framework that could help in organizing jquery code. What do you think?


问:
Is using nested try-catch blocks an anti-pattern?
Is this an antipattern? It is an acceptable practice?</p>

```<code>    try {
        //do something
    } catch (Exception e) {
        try {
            //do something in the same line, but being less ambitious
        } catch (Exception ex) {
            try {
                //Do the minimum acceptable
            } catch (Exception e1) {
                //More try catches?
            }
        }
    }
</code>```

答: In some cases a nested Try-Catch is unavoidable. For instance when the error recovery code itself can throw and exception. But in order to improve the readability of the code you can always extract the nested block into a method of its own. Check out this blog post for more examples on nested Try-Catch-Finally blocks.</p>


答:
Yes, you should.  Software quality is the responsibility of everyone on the project.  Ok, let me backpedal immediately... you should actually open the bug yourself, if that's what your manager says you should do (after checking to make sure it isn't already reported).

Having a wall between the two groups is counter-productive, in my opinion, because it leads to blame, accountability problems, and poor communication.  If anyone is defensive or aloof, the problem is them, their team leadership, or the leadership of the company.  Recognizing that as a contractor is important, I suppose, so that you don't step on toes and compromise your status.

All bugs should be tracked because, except in some rare cases, all software of a certain size carries a backlog of bugs.  Understanding the backlog is important when choosing what to work on, whether or not to release an iteration, and planning for the future.

Title: should I help QA team in finding bugs?


答:
Looking at the title "should I help QA team in finding bugs?"

My answer is No, or at least only incidentally.

You should focus on helping out the users of the product and thus the future of the company by having products that are bug free.

The bugs you see will exist for a wide variety of root reasons which may be that they've not been noticed or complained about but may also still exist because there's other more important projects, not enough revenue from the product, no good ways to distribute patches the product, documentation that actually includes the bug, etc, etc.

Make a case for the item to be recorded as a representative of the user.

Getting access to the bug tracking system to enter bugs can be good, however don't expect to suddenly inject a bunch of work into existing workflows without comment.  What you really need to do is get more senior management on-board with this and have them notify folks about these tickets and allocating time and effort for them.

Do not try and slip-stream them into current tickets because you will generally get the feedback from the developer that "that has nothing to do with this ticket and I didn't touch that area", plus "it was like that before my ticket".  So that approach tends to be a non-starter.

Basically you need to get your input working along with everyone else towards making a good product.

Title: should I help QA team in finding bugs?


答:
I don't think that human written Makefile-s are obsolete, especially when:

using POSIX make, which gives you a portable Makefile

or using GNU make 4, which gives you many very interesting features, in particular GUILE scriptability, which enables to code efficiently the fancy features provided by Makefile generators (I believe that the features of autotools or Cmake could be easily written by Guile customization of GNU make). Of course, the price to pay is to require GNU make 4 (not a big deal, IMHO).

Title: Is directly using Make considered obsolete?

Body:
So I have been coming across many comments/posts/etc regarding creating makefiles directly, and how it is a silly thing to do in 2015. I am aware of tools such as CMake, and I actually use CMake quite often. The thing is, CMake is just creating the Makefile for you and helping to remove the tedium of doing it yourself. Of course it adds a lot of other great features... but its still a Makefile in the end.

So my question is, is the 'obsolete' talk regarding make referring to the entire Make utility, or just the idea of manually writing your own Makefiles? I do not use an IDE for C/C++ development at all (just emacs), so I have always written Makefiles.

If Make is considered outdated, what should a C/C++ dev be using to build small, personal projects?


问:
How much Code Coverage is "enough"?
We are starting a push for code coverage here at my work, and it has got me to thinking.... How much code coverage is enough?

When do you get to the point of diminishing returns on code coverage?  What is the sweet spot between good coverage and not enough?  Does it vary by the type of project your are making (ie WPF, WCF, Mobile, ASP.NET)  (These are C# classes we are writing.)

答: Code coverage approaches 100% asymptotically.  Consequently, that last 5% is probably more effort than it is worth, as you begin to achieve vanishingly small returns for the effort expended.</p>

Title: How much Code Coverage is "enough"?


问:
eBook editions of programming books
(I'll get my justification for why this is on topic in early: programming books tend to have fairly specific formatting needs - code samples, tables, images and graphs - which are not common to all book types and are not necessarily well handled by eBook readers.  Similarly they're used in different ways - you often dip in and out rather than read cover to cover.)

I've just noticed that Don't Make Me Think by Steve Krug is available as an eBook edition for the Kindle (and presumably also for other readers) which set me thinking.

There are certain advantages to eBook readers for tech books - primarily that you can carry a massive library of what would be heavy physical books around very easily.  The downside is that certain eBook readers allegedly aren't particularly well equipped to cope with tables, code samples and so on and a book like Don't Make Me Think presumably makes extensive use of these sorts of things.

So, the question, what are your experiences of reading and using programming books on an eBook reader and would you recommend it? I'm specifically interested in the latest generation Kindle but happy to hear about all devices - might be useful to state which one you use in the answer.

答:
I have a lot of programming books in both physical and electronic formats and I also have a Kindle (the latest 'small' version). Oh and I have a Safari subscription as well...

Overall I much prefer a physical book to reading on the computer screen as you have more freedom in where you can read it and also you can browse and flick through to random sections in a way you can't really with an electronic version. Of course this is all assuming you're where the book is.

For electronic books you can basically divide the formats into ones that preserve page layout (e.g. PDFs) and the ones that support free reflowing (e.g. mobi). The formats that preserve page layout generally are better for books where layout is critical or where there are a lot of diagrams - programming books come in this category.

I'm afraid the smaller Kindle is very poor for reading technical/programming books unless they're predominantly text. PDFs on the Kindle are difficult to navigate and zooming is very clunky. There are converters to the native format but I find these generally lose most of the formatting. I suspect a good OCR program that supports PDFs as input such as ABBYY Finereader would do a better job but I've not tried it. Technical books published in mobi format are fine unless they have diagrams - often these are just as hard to zoom in on as PDFs and worse can be very low resolution bitmap versions and hence unreadable. On the other hand the Kindle is extremely good when it comes to reading books that are mostly text (such as a typical novel or something like Mythical Man Month or whatever). An added bonus is that you can read it in daylight.

I don't personally have an iPad but these seem to be very good for reading PDFs (Safari also has an iPad app which lets you read their books provided you have an active internet connection). On the other hand you can't really use it outdoors and I don't think the screen is as clear as the Kindle's for reading a lot of text - and so I don't see much benefit over using my laptop (I have a Vaio Z so the screen is high quality and the laptop is extremely portable).

It's only a matter of time before an A4 colour eBook reader comes out with electronic ink (the type of screen that the Kindle and most other eBook readers have), until then you'll need to decide where you want to compromise if you want to read ebooks away from your computer/laptop.

Title: eBook editions of programming books


问:
Multi-tenancy - single database vs multiple database
We have a number of clients, whose systems share some functionality, but also have quite a degree of diversity. The number of clients is growing - always a healthy thing! - and the diversity between their businesses is also increasing.

At present there is a single ASP.Net (Web Forms) Web Site (as opposed to web project), which has sub-folders for each tenant, with that tenant's non-standard pages. There is a separate model project, which deals with database access and business logic.

Which is preferable - and most importantly, why - between having (a) 1 database per client, with only the features associated with that client; or (b) a single database shared by all clients, where only a subset of tables are used by any one client.

The main concerns within the business are over:

maintenance of multiple assets - backups, version control and the like

promoting re-use as much as possible

How would you ensure these concerns are addressed, which solution is preferable, and why? (I have been also compiling responses to similar questions)

答:
Here's the highlights from research from other sources (originally from revision 2 of the question):

Should I use one database per application or share a single database amongst multiple applications

Supporting multitenancy

MSDN Multi-Tenant Data Architecture

SO How to create a multi-tenant database with shared table structures?

CloudExpo SaaS - Multi-Tenant Database Design Options

MSDN blogs Moving to Multi-Tenant Database Model Made Easy with SQL Azure Federations

Ask SQL Server Central One database or multiple?

SQL Server Central Mixing tables from different databases/applications into one database

Ayende@Rahien blog Multi Tenancy - The Physical Data Model

Code Project Multi Tenants Database Architecture

Joel on Software Multi-tenant database design

SO Multiple application using one database?

SO Single or multiple databases

Code Rant - Multi-tenancy Part 1, Strategy and Part 2, Components and Context


问:
Can I ditch the persistence.xml file on JBoss?
I have a Spring based app where another programmer is using JPA's persistence.xml file to define the setup of hibernate and to define the entities that are being persisted.

The app is using Spring's java configs so there is almost no xml in it. I already have Spring generating a LocalContainerEntityManagerFactoryBean for hibernate. Reading the LocalContainerEntityManagerFactoryBean doco it sounds like I can simply migrate the persistence.xml file's setup into the java methods that creates the it and everything should just work.

The app is running on JBoss and my question is whether JBoss actually requires the persistence.xml file and what it does with it?

Reading (A lot) of web pages and the JPA specification it appears that the JEE container has to use it, however we are Spring based even though we are running on JBoss 8. SO I'm thinking I can run without persistence.xml and simply use Spring Java config whilst still being fully JPA complient.

Thoughts?

答:
Yes, you can go without persistence.xml, since it is not required to use JPA at all to run your application in Java EE container. Some applications don't even need persistence at all, and JBoss can run them as well.

As for compliance with JPA: as I understand, an implementation of a standard must comply the specification, not an application using this implementation. So, for example, Spring may or may not be JPA-compliant, not your application using Spring. Your application just may or may not use JPA features.

The specification (JSR 220) explains persistence.xml file usage like so, not mentioning any alternatives to using it:

6.2.1 persistence.xml file

A persistence.xml file defines a persistence unit. It may be used to specify managed persistence
  classes included in the persistence unit, object/relational mapping information for those classes, and
  other configuration information for the persistence unit and for the entity manager(s) and entity manager
  factory for the persistence unit.

I believe LocalContainerEntityManagerFactoryBean is only Spring's feature: Spring 3.1 M2: Configuration Enhancements


问:
A good workflow to start with Continuous Delivery?
My team is facing a couple of new challanges in the near future, as we will start developing a couple of (micro)services which will run in a cloud environment. Therefore we want to establish a continuous delivery workflow (and maybe move on to continuous deployment someday).

Currently we are developing an application based on Eclipse RCP together with some web applications (Java servlets). After we provide the released artifacts, the customer is responsible for operation. We release 3-4 times a year with additional maintenance releases when needed. We use git with a trunk-based (all-on-master) workflow and additional maintenance branches. The developers commit at least once a day, which triggers a build on our CI server. The master may get unstable once in a while and contains a lot of incomplete (and not hidden!) features.

As we will be responsible for operation of our services, our main goal is a always release-ready mainline in order to react fast if something is broken.Thus we thought about extending our git workflow with feature branches. But we soon dropped that idea because of several reasons (e.g. merge conflicts with features/stories that take 1-2 weeks, no satisfying CI feedback, etc.)[1-2]. Hence we decided to keep our all-on-master workflow (without maintenance branches) and work with patterns like feature-toggles, branch-by-abstraction, etc. [3-4].
However, we don’t have much experience with those patterns and also think that it’s non trivial to always keep the mainline 100% clean and release-ready. Thus we decided to have an additional development branch which is merged back when needed or at least at the end of a sprint. This ensures that the master branch really stays release-ready, while the development branch don’t has to be (but of course should be).

After this workflow has established, we want to move a bit further and introduce some kind of short living feature branches. In my opinion the main problems with feature branches arise from their rather long life time. Imagine you have a user story which is composed of several subtasks. A task takes about 1 day of work. When the task is done, the changes get pushed to the mainline which triggers a full build on the build server. This is CI as we know it, right? However, my commit can still lead to an unstable mainline. Of course I can run all unit tests locally for example, but the build server might run additional tests or check other quality metrics which I don’t want to run locally. Thus it would be nice to have a dedicated branch and build job for my task. This enables me to verify and check my changes before I push them to the mainline. So I grab a subtask to work on, and automatically get a feature branch and build job. When the task is finished it gets pushed to the mainline and the branch+build job is cleaned up.

I know this workflow is similar to 'gitflow' for example, however the main difference is the lifetime of branches as mentioned above.

What do you think about this workflow? Which workflows do you use?

(1) „Continuous Delivery“, Jez Humble and David Farley,
(2) martinfowler.com, FeatureBranch,
(3) martinfowler.com, FeatureToggle,
(4) martinfowler.com, BranchByAbstraction,

答:
If I understand your question correctly, the only reason you want a "clean master" at all times is so that you can "react fast if something is broken". I believe this is best served by doing hotfixes on release branches, rather than moving all non-hotfix tasks to non-master branches.

Basically, you do all development work on master as usual, then when the sprint ends and you do a release, you "cut" a branch from master and call it something like "releases/20150411". Then, if you ever find a bug on production, you figure out which release branches are affected by it, apply a patch to those release branches, then rerelease/redeploy/etc the patched versions to the users that had the broken releases. This is actually more flexible than the "clean master" approach since you can easily have multiple release branches, which (assuming you have several deployment stages like we do) minimizes the risk of accidentally adding new bugs/features to the "farthest out" user group as a side effect of fixing the current bug.


问:
Using $timeout to normalize response time of a webservice call (Best practice question)
Consider you have a simple, standard $http request to a REST api:

```<code>...
function makeCall() {
    var restURL = "http://my-rest-api/endpoint";
    return $http.get(restURL);
}
...
</code>```

If the execution time of this call varies for whatever reason (sometimes returns immediately, other times takes around a second to e), what is the best practice for normalizing the response time to the user?

In the past, I've done things like this:

```<code>...
function makeCall() {
    var restURL = "http://my-rest-api/endpoint";
    return $http.get(restURL);
}

...

function myController($scope) {
    var promise = makeCall(); //make rest call
    $scope.waitMessage = true;
    $timeout(function() {
        //before resolving the promise, wait a certain number of ms, then
        //resolve and display data to user
        promise.then(function(response) {
            $scope.output = response.data;
            $scope.waitMessage = false;
        });
    }, 1000);
}
</code>```

This is an attempt to make the request take a fixed amount of time, regardless of if it returned immediately or not. During the delay time, I display an interstitial of some kind saying "Loading, please wait..." or the like, controlled by a variable like $scope.waitMessage.

Any best practices for this sort of use case?

答:
I'm not sure if it's a "best practice", but my way is to create a directive that throttles variable changes, using the lodash/underscore throttle function:

```<code>app.directive('throttle', function($window) {
  var WAIT_TIME = 750;

  return {
    scope: true,
    link: function(scope, element, attrs) {
      var name = attrs.throttle;

      function setValue(value) {
        // Shadow the value in the child scope
        scope[name] = value;

        // The leading edge of the _.throttle callback
        // is called within a digest, but later ones are not
        scope.$$phase || scope.$apply();
      }

      scope.$parent.$watch(name, $window._.throttle(setValue, WAIT_TIME));
    }
  };
});
</code>```

That can be used for a simple text state variable to determine what to show to the user:

```<code><div throttle="state">
  <div ng-if="state == 'loading'">Loading...</div>
  <div ng-if="state == 'loaded'">[Loaded template]</div>
</div>
</code>```

So referring to the code in your question, in the controller you could have something like:

```<code>$scope.state = 'loading';
makeCall().then(function(response) {
  $scope.output = response.data;
  $scope.state = 'loaded';
});
</code>```

The benefits of doing this rather than via $timeouts in your controller or services:

Better separation of responsibilities. The controller/services don't need to know about exactly how/when the data will be shown to the user. This is just in the template/directive.

Throttling means you've set a minimum time any given state will be shown to the user. If the user has been in that state already for at least that time, then there will be no extra time waiting.

I originally posted this on my blog at http://charemza.name/blog/posts/angularjs/templates/angularjs-throttled-variable-changes/

Body:
I'm not sure if it's a "best practice", but my way is to create a directive that throttles variable changes, using the lodash/underscore throttle function:

app.directive('throttle', function($window) {
  var WAIT_TIME = 750;

  return {
    scope: true,
    link: function(scope, element, attrs) {
      var name = attrs.throttle;

      function setValue(value) {
        // Shadow the value in the child scope
        scope[name] = value;

        // The leading edge of the _.throttle callback
        // is called within a digest, but later ones are not
        scope.$$phase || scope.$apply();
      }

      scope.$parent.$watch(name, $window._.throttle(setValue, WAIT_TIME));
    }
  };
});

That can be used for a simple text state variable to determine what to show to the user:

<div throttle="state">
  <div ng-if="state == 'loading'">Loading...</div>
  <div ng-if="state == 'loaded'">[Loaded template]</div>
</div>

So referring to the code in your question, in the controller you could have something like:

$scope.state = 'loading';
makeCall().then(function(response) {
  $scope.output = response.data;
  $scope.state = 'loaded';
});

The benefits of doing this rather than via $timeouts in your controller or services:

Better separation of responsibilities. The controller/services don't need to know about exactly how/when the data will be shown to the user. This is just in the template/directive.

Throttling means you've set a minimum time any given state will be shown to the user. If the user has been in that state already for at least that time, then there will be no extra time waiting.

I originally posted this on my blog at http://charemza.name/blog/posts/angularjs/templates/angularjs-throttled-variable-changes/


问:
how to paginate and combine results that come from different sources sorted
I have the following scenario:

I have a user that needs to see some news feeds from different sites and I want to paginate them and sort them by date. The problem is I don't understand how to do that schemantically.

Fetch top-N from each site and then limit the results? That could result in inconsistent results if for example:

I ask the first 100 results from each site and then select in total the newest 100 from all sites. The second time I do this if site A had all of its first 100 results newer than the other sites , it is a problem. I will ask from the sites results from 100-200 where site B's results should come from 0-100 because they were not displayed the last time.

How to do this kind of aggreggation without fetching all available (or an obsene amount of results) from each site?

答:
There can be no general rule.

If all of A's articles are newer than B why would you expect any B articles in your list?

You need to nail down your expected behaviour and make a decision based on that. Also think about what happens if one resource is unavailable at query time etc.


问:
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?

答:
The fact that your data-gathering methods are complex enough to merit tests and separate enough from your primary goal to be methods of their own rather than part of some loop points to the solution: make these methods not private, but members of some other class that provides gathering/filtering/tabulating functionality.

Then you write tests for the dumb data-munging aspects of the helper class (e.g. "distinguishing numbers from characters") in one place, and tests for your primary goal (e.g. "getting the sales figures") in another place, and you don't have to repeat basic filtering tests in the tests for your normal business logic.

Quite generally, if your class that does one thing contains extensive code for doing another thing that is required for, but separate from, its primary purpose, that code should live in another class and be called via public methods. It shouldn't be hidden in private corners of a class that only accidentally contains that code. This improves testability and understandability at the same time.


问:
Design pattern for multiple companies in AngularJS
We are on the verge of launching v3 of our in-house product. v2 was written completely in AngularJS and V3 will be the same, but written completely in Typescript.

We have a few customers that have very specific needs for the product and each have their own version (hosted on our own platform), and have their own deployment. There are common shared parts which is the same for everybody.

At the moment everything is in 1 solution (Visual Studio) which we prefer due to ease of maintaining it, but it made a big mess of mixing of settings, company specific options etc. There are a lot of if-else statements in the controllers, directives and even views (still, all angularJS) to accompany for multiple companies.

Now in V3 we are rethinking this setup and want to become more flexible, every company will have its own controllers, directives and views, with the option of some parts being shared and using a service/data layer to handle common shared tasks.

Now I am thinking how can I make this manageable? At the moment we have 6 customers but this is expected to grow with 10-15 each year. I am afraid we will again end up with a big pile of files scattered through the project which makes it hard to maintain and test.

I want to avoid this and keep the project flexible and manageable.

Should I figure out a way to split every company in separate solutions with the common Javascript/Typescript files for app startup and routing in a common project? Maybe just have a single folder for every company with custom controllers, directives and views, but how would the routing be handled?

How do we approach managing enterprise apps with multiple companies?

PS. Our web application is angular because almost everything is client side, it has the ability to work 100% offline for longer periods of time, the backend side is not important for this problem.

答:
If you realy, realy, realy need so many client-specific features, which I do not recommend at all, you should make your application highly modular. Every time a client-specific change is needed just create a new module instead of adding an if-else mess to existing modules. Make these modules as small as possible and use interfaces and patterns like dependency injection to try and keep this organized. Load the client version of a module where possible or fall back to the default.

```<code>NewsModule/NewsModule.js
NewsModule/ArticleHeader/ArticleHeader.js
NewsModule/ArticleHeader/ArticleHeader.ClientA.js
NewsModule/ArticleHeader/ArticleHeader.ClientC.js
</code>```

An alternative method could be (and this is just a though, I'm not sure if it could work, and might anger the gods) to create a branch for each client. Always push from master (or dev, or whatever) to client, and never the other way around.

```<code>        / ClientA
master -- ClientB
        \ ClientC
</code>```

Please keep in mind that, whatever solution you choose, this will never be easy to maintain. Bugfixes will be a pain. Continuous delivery will be a pain. Testing will be a pain. Etc.

Body:
If you realy, realy, realy need so many client-specific features, which I do not recommend at all, you should make your application highly modular. Every time a client-specific change is needed just create a new module instead of adding an if-else mess to existing modules. Make these modules as small as possible and use interfaces and patterns like dependency injection to try and keep this organized. Load the client version of a module where possible or fall back to the default.

NewsModule/NewsModule.js
NewsModule/ArticleHeader/ArticleHeader.js
NewsModule/ArticleHeader/ArticleHeader.ClientA.js
NewsModule/ArticleHeader/ArticleHeader.ClientC.js

An alternative method could be (and this is just a though, I'm not sure if it could work, and might anger the gods) to create a branch for each client. Always push from master (or dev, or whatever) to client, and never the other way around.

        / ClientA
master -- ClientB
        \ ClientC

Please keep in mind that, whatever solution you choose, this will never be easy to maintain. Bugfixes will be a pain. Continuous delivery will be a pain. Testing will be a pain. Etc.


答:
You have to remember the start point when paginating, so if you retrieve the latest results for all sites, you have to be able to repeat the query for the same result set - that will depend on the result data you get back, for example, I will use an ID value for my SQL data results, but it could be based on date for other systems.

Once you have this, you can repeat the query asking for next (or previous) pages of data. If you ever want to start again, you'll have to include a reload or refresh option that resets the start point. You only need the start point data for paginated queries, never the first one (though it might help if you have data that doesn't return something you can use, you might have to store the datetime you requested the data before calling, and pass that through to prevent a new entry appearing between the time requested and results returned).

Title: how to paginate and combine results that come from different sources sorted

Body:
I have the following scenario:

I have a user that needs to see some news feeds from different sites and I want to paginate them and sort them by date. The problem is I don't understand how to do that schemantically.

Fetch top-N from each site and then limit the results? That could result in inconsistent results if for example:

I ask the first 100 results from each site and then select in total the newest 100 from all sites. The second time I do this if site A had all of its first 100 results newer than the other sites , it is a problem. I will ask from the sites results from 100-200 where site B's results should come from 0-100 because they were not displayed the last time.

How to do this kind of aggreggation without fetching all available (or an obsene amount of results) from each site?


问:
Many different classes that need similar functionality. Best approach?
I have many (at the moment around 30) different message classes in an application I am creating. Each of these messages need to be serialized and deserialized. However, the process of serializing and deserializing is mostly unique to each class (the format of the serialized message is specified in a document, and says which bits in the message goes where), but some are pretty straight forward and use the same process.

I thought about having each of them implement an interface, with Serialize() and Deserialize() methods. However, it seems cumbersome to implement this for each message, especially when many of the messages do in fact use the same code to do this serializing and deserializing.

I thought about having a class that contains a Dictionary<T, Func<IMessage, byte[]>, and a function to map a function to serialize to a specific type. Then use this dictionary to get the appropriate serializer.

Any thoughts? How would you go about this problem?

答: Generic answer: My gut feeling says if you have 30 different but very similar things, you should go one metalevel up, have one class for all of them and push the difference completely down to the data. Even if you end up with a nano VM / nano interpreter. Maybe during implementing it, you find out you really only have two or three kinds of them, parametrizable by different kind of data.</p>