答:
is this database design fully normalised?

There are many normal forms.  This design is mostly 3NF, except for the three ReferenceCount columns.  As such it accords with common practice in the software development industry.

is it sound?

Yes, given your comments to the other answer, I wouldn't be upset if one of my developers suggested this as a schema.

I'd question if there should be another table between Word and Rhyme.  Do combat and wombat rhyme on the "at" or the "om" or both?  Would SyllableCount vary with pronunciation?


问:
Is this database design sound and fully normalised?
I'm creating a database to store words. The intended usage is to query the database to find a word matching a set of filters. E.g. if I wanted to find a word with < 10 letters, < 3 syllables, rhymes with 'at' and matches the tag 'animal', it would return 'cat', 'bat', 'wombat'...

Unnormalised, the table would look like this: (primary key is marked by PK, contextual information is given in square brackets)

```<code>Word(
    WordID PK,
    Word,
    AltSpelling, [another accepted variant e.g. "color"/"colour"]
    Rhyme, [the rhyme part of the word, e.g. for "wombat" the rhyme is "at"]
    Pronunciation, [how the word is pronounced in IPA]
    SyllableCount,
    Tags, [tags relate to meaning of the word, e.g. "apple" would have the tags "round", "fruit", "plant" etc.]
    Categories, [categories are how the word would be hierarchically categorised, e.g. cat comes under the category "animal"]
    WordClasses [the word class that a word can be used as, e.g. "rest" can be Noun and Verb]
)
</code>```

This is the Entity-Relationship diagram to describe how the database should work:

And finally, this is the solution I've come up with for a normalised database design. FK indicates foreign key, CPK is Composite Primary Key.

```<code>Word(WordID PK, Word, OtherSpelling, RhymeID FK, SyllableCount)
Rhyme(RhymeID PK, Rhyme)
Pronunciation(PronID PK, WordID FK, Pron, DialectApplicable [the dialect in which the pronunciation is used, e.g. RP, SE, BrE, AmE])
Tag(TagID PK, TagName, TagReferentCount [the number of words to which the tag applies])
WordTag(WordID CPK FK, TagID CPK FK)
Category(CategoryID PK, CategoryName, CategoryReferentCount [cf. TagReferentCount for categories])
WordCategory(WordID CPK FK, CategoryID CPK FK)
Class(ClassID PK, ClassName, ClassReferentCount [cf. TagReferentCount for word classes])
WordClass(WordID CPK FK, ClassID CPK FK)
</code>```

The question is twofold: is this database design fully normalised? And more importantly, is it sound? I have no practical experience with databases, so I'd like to know if I've made any mistakes, or if the design can be improved/optimised.

答:
Model is not normal

OtherSpelling should be a separate entity named Spelling

DialectApplicable should be a separate entity named Dialect

TagReferentCount should be calculated

CategoryReferentCount should calculated

In WordTag, WordID and TagID should be separate FK besides being CPK

In WordCategory, WordID and CategoryID should be separate FK besides being CPK

In WordClass, WordID and ClassID should be separate FK besides being CPK

ClassReferentCount should be calculated

In the chart, cardinality is inverted for the WordTag, WordCategory and WordClass tables, (trident should be on the N:N table)

ContentLicense: CC BY-SA 3.0


问:
Implement RPC via SIP
Look at the following use case.

I have a client (Java) application, which wants to get/set the state of another, remote application (C). The communication between them is done via SIP, which is run in another thread.

The SIP interface can do the following:

sendMessage
onRequest

I have two ideas for the architecture:

RPC (JSON-RPC)

Define a class which does the marshalling/unmarshalling for JSONRPCRequests and JSONRPCResponse (http://software.dzhuvinov.com/json-rpc-2.0-base.html)

Define a Invoker class, which has something like a call(server, name, arguments) method.

In the Invoker class, the name and arguments are put into a JSONRPCRequest and sent via the SIP layer sendMessage

Now comes my problem. How do i actually get the right back to the caller? The control flow is now:

The onRequest method is called, but I do now know whether it is the answer to my previous call. What i do is putting all responses reaching my server into a Map, and just poll that list in the Invoker.

A rough sketch might be;

Invoker (provides API to client)

```<code>class Invoker {

    private Channel channel;

    public Invoker(Channel channel) { this.channel = channel; }

    public Object call(String server, String name, Object .. args) {
        JSONRPCRequest req = ...;
        channel.sendMessage(server, req.toString());
        while( ! channel.hasResponse(req.id()) {
            Thread.sleep(42);
        }
        return channel.getResponse(req.id()).result();
    }

}
</code>```

Channel (interface to messenger):

```<code>class Channel {

    private Map<Object, JSONRPCResponse> responses = new //;

    private Sip sip = new Sip() {
        public void onRequest(String msg) {
            JSONRPCResponse response = JSONRPCResponse.parse(msg);
            responses.put(msg.id(), response);
        }
    };

    public void sendMessage(String server, String message) {
        sip.sendMessage();
    }

    public boolean hasResponse onRequest(Object id) {
        responses.hasKey(id);
    }

    public JSONRPCResponse getResponse(Object id) {
        responses.get(id);
        responses.delete(id);
    }

}
</code>```

SIP (messenger itself):

```<code>abstract class Sip {

    public void sendMessage(String msg) {
        // SIP magic
    }

    public abstract void onRequest(String msg);
}
</code>```

Is there a better way to do that? My biggest problems/code smells are:

the blocking in Invoker

the protocol is in Invoker, maybe I want to switch marshalling to something else

the map as mean to get the correct response for a request

the SIP abstract method looks strange

No error handling

No timeout

Message Passing

Is there an easy way to get rid of RPC, and implement something like RPC with just message passing? Any hints for pattern are welcome. I do not need the code itself, I am totally fine with just architecture. I tried to google for message passing implementations, and how they actually change state with it, but I did not find anything useful. How to implement timeout/ error handling?

Any good books/literature on that topic is also welcome, as I never programmed such distributed stuff.

Any other ideas on which protocol to use inside SIP to change state is welcome, too, as RPC was my initial thought, and I did not find anything other useful.

The code will not compile, I guess, it was just to visualize my idea.

答:
the blocking in Invoker

the protocol is in Invoker, maybe I want to

switch marshalling to something else the map as mean to get the

correct response for a request the SIP abstract method looks strange

No error handling No timeout

Your channel by design is NON-BLOCKING.  Your implementation with the while() loop makes it not.  Employing callbacks could make this much prettier.  In C the callbacks could be method pointers or Java an interface-bound handler.

```<code>interface AsyncResponder
void result( Object o );
void error( Object o );
</code>```

Create an ID/SEQUENCE NUMBER for every message and store in a map the ID and the reference to the callback handler.  Execute the callback handler when you get a response.

Blocking timeouts are much easier. hasResponse(id) could be very easily adapted to occur on an interval in conjunction with the timeout mechanism.

Non-Blocking timeouts have the duty of removing the request callback from the callback map.  If/when a response occurs there should be no callback to execute.

Error handling should be done in the request handler only.  Not globally.

Nothing wrong with having the protocol in the invoker unless you expect the invoker to change all the time.

RPC calls must have a SEQUENCE NUMBER, COMMAND NAME, MESSAGE LENGTH, MESSAGE BODY.  For responses the COMMAND NAME could be "_result" / "_error". (as done in RTMP)

Title: Implement RPC via SIP

Body:
the blocking in Invoker

the protocol is in Invoker, maybe I want to

switch marshalling to something else the map as mean to get the

correct response for a request the SIP abstract method looks strange

No error handling No timeout

Your channel by design is NON-BLOCKING.  Your implementation with the while() loop makes it not.  Employing callbacks could make this much prettier.  In C the callbacks could be method pointers or Java an interface-bound handler.

interface AsyncResponder
void result( Object o );
void error( Object o );

Create an ID/SEQUENCE NUMBER for every message and store in a map the ID and the reference to the callback handler.  Execute the callback handler when you get a response.

Blocking timeouts are much easier. hasResponse(id) could be very easily adapted to occur on an interval in conjunction with the timeout mechanism.

Non-Blocking timeouts have the duty of removing the request callback from the callback map.  If/when a response occurs there should be no callback to execute.

Error handling should be done in the request handler only.  Not globally.

Nothing wrong with having the protocol in the invoker unless you expect the invoker to change all the time.

RPC calls must have a SEQUENCE NUMBER, COMMAND NAME, MESSAGE LENGTH, MESSAGE BODY.  For responses the COMMAND NAME could be "_result" / "_error". (as done in RTMP)


问:
Cloud Newbie ... what should I know while creating my App
I never developed for the Cloud, and to tell you the truth I am not fully 100% sure of what Cloud is when coming down to servers and services.

I am starting to develop an App where I will need to be able to scale as needed.  The App has a lot of User entries as well as file uploads.  Think is as a "forum" (this is the closest model that I can think that applies to my App).

User will be able to register, create "threads", create "polls", answer, and vote.  They can also upload images, videos, and audio files.

The person that previously created the website used 'Auto-increment" fields to keep track of the user ids.  while rebuilding the page I was thinking to drop that part and use UUID() for all the users entries, this, I believe might solve the concurrency of entries at the same millisecond or weird locks or duplicate ids or possible other problems... but... other than that, I do not have a clue of what the "cloud" would do.

Those are my questions:

I will use MySQL as DB, will I have to connect to different DBs to write to alleviate the stress or I should not even worry about that because the "cloud" will take care of that? (I will have replication for the reads)

When accessing files, will I have to know on what server the files where uploaded to be able to serve them, or will the "cloud" magically do that ?

If I increase the "cloud" space by adding a new server, what should I know about it? simply deploy it and everything is up and running or will I need to do something to my code to tell "hey, there is another instance, now do this and that differently"

Thank you and I hope you understood what I meant.

答:
Cloud, put plainly, is a server for rent available online.  As such, generally most of the types of hassles regarding setting up a server and making it accessible online while not putting the rest of your network at risk, is already handled for you.  There are several types of clouds depending on what the cloud offers you:

Storage clouds - They offer a means to upload and download files.  As such, they generally charge by storage usage or by overall capacity.  Most of these storage clouds will offer free file hosting up to a certain amount, which is excellent for development (and less so for production).

Web application clouds - These allow you to create a backend and (potentially) a frontend for your website.  The technologies vary greatly, and some also have database support built-in.  When you use this you generally get charged by CPU rate.

Database clouds - These allow you to save data.  This may or may not also allow you to save files, though strictly speaking, it is not optimal to do so.  Among these, the ones offering NoSQL type databases are generally free, though you should be able to find MySQL services as well.  These tend to charge you by amount of data stored.

Virtual machine clouds - These are generally the most flexible and allow you to do anything you could do with a server connected to the internet, including the above three cloud types.  These are however also the most difficult to setup, since what you gain in flexibility, you lose in automatic handling.  In other words, you have to do everything yourself.  You generally get charged by CPU rate and/or disk capacity.

I will use MySQL as DB, will I have to connect to different DBs to
  write to alleviate the stress or I should not even worry about that
  because the "cloud" will take care of that? (I will have replication
  for the reads)

If the cloud is providing MySQL service, then clustering and stress management is entirely expected to be handled by the server(s).  If you're given a virtual machine cloud to install MySQL on, obviously that won't be the case, since you yourself are handling that aspect.

Keep in mind that you could use one cloud to hold your application and another cloud to provide MySQL services.  You'd see a slightly larger delay, but it greatly simplifies your task if you need both web application hosting and MySQL hosting.

When accessing files, will I have to know on what server the files
  where uploaded to be able to serve them, or will the "cloud" magically
  do that ?

It depends entirely on how you're handling files.  If you're using a cloud storage service, then again, you won't have to worry about any of that.  Presumably, they will give you an API or interface by which you can use to download files.  Supposing you wanted to offer your client a link to a file they uploaded, you'd likely not want to download to the web application cloud only to send it to the client, but rather simply offer them a direct link.  In this sense, again, you're simplifying things greatly.  You do not handle the files, the cloud service does.  Instead you handle the URL that the client can use to download that file.

If you're not using a cloud storage service, then it is no different from handling it on your own server.  You would presumably then need to download the file to your server, find a folder to put it in, and within MySQL save the file's position on the disk (or network as the case may be).  When the client asks for that file, you have to find it and send it yourself.

If you handle it yourself on a virtual machine cloud, you have the added complication that if users could exceed the disk capacity (either through malice or negligence), then there could be supplementary charges and/or the entire virtual machine cloud will no longer respond (you have to hope they had a backup of some sort or you would simply have to reset the machine, neither of which is desirable).

If I increase the "cloud" space by adding a new server, what should I
  know about it? simply deploy it and everything is up and running or
  will I need to do something to my code to tell "hey, there is another
  instance, now do this and that differently"

Assuming each cloud instance is its own virtual machine, there is nothing magical about it.  If you have two, then there is going to be no automatic instance handling, and you will therefore need to handle it yourself.

If you use web application clouds, then it depends on what they offer.  Some may only give you a single instance, while others will offer to increase that amount by demand for an additional cost.

Conclusion

If you're looking into using clouds to assist with development, my advice to you is to warm your boss to the idea of going the whole 9 yards and using whatever services you require.  The point is to simplify production times by cutting out all the excess s*** err stuff you have to go through.  There is no point doing this half-way.  Speaking from experience, there is really nothing worse than winning the battle to use cloud technology, yet compromising by being given only a single virtual machine instance.  The only thing you've gained is that you already have a computer ready to go and connected to the internet.  You still have to do the rest of it on your own, with all that that entails regarding security and the rest.

Know that the primary things you have to worry about are the things you yourself introduce to the cloud.  For instance, if you're using a web application cloud, you need not worry about security problems other than the ones you create yourself in your web application.  If you're using a virtual machine cloud, you will likely have to configure the firewall yourself, and hence any attacks your virtual machine cloud will receive will be on the ports you enabled on your firewall.  If you are using a database cloud, ensure that you're verifying the size of the files being uploaded as to prevent all of your storage capacity being occupied by a single user with nothing better to do than to upload 1 TB of porn videos.  For the most part, these are common sense things.

I hope that answers your question.


问:
When to stop reviewing code?
I am working in a team of 4 members. we have started reviewing code 2 months ago. we are using Gerrit as code review tool. while reviewing code initially we have found lot's of problem in the code i.e. no proper code comments. method are too big. no null pointer checking. no proper exception handling etc.

but now a days it looks code has improved. so when i am reviewing code i am not getting that much problem in the code.

so my question is should we continue code review even there is not that much flaws in the code? or will we stop?

答:
To put it clear, code review is not really for spotting bugs (even if it might do it), that is testing. Code review's purpose is mainly to keep project patterns, clean code, avoid local solutions.

To use a tool is useful, but it always worths the time and effort for a four eyes check to keep up good practices in the project. Even more senior programmers should let their code being subjected to a code review. The practices sometimes getting outdated or rethinked.
In Java I can recommend Adam Bien's works on rethinking old patterns. If the team is heterogenous (various workplace past + age), the regular code review is vital to avoid chaotic coding.

Just to mention an obvious reason why to spend time on code review: if the code is reviewed and becomes homogenous in practices, anybody can go on vacation without constant bugging about their solutions. Since the code is similar, others can actually deal with even the unfamiliar modules of the program in case of bugfixing. Also if somebody leaves from the project, there will be no modules with horrible solutions which nobody understands.

So when to stop? Never. The regular code review pays back in long run. Makes the code easier to maintain.

Title: When to stop reviewing code?


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

答:
It is surely decreasing the code readability. I would say, if you have chance, then avoid nesting try-catches.

If you have to nest try-catches, always stop for a minute and think:

do I have the chance to combine them?

```<code>try {
  ... code
} catch (FirstKindOfException e) {
  ... do something
} catch (SecondKindOfException e) {
  ... do something else
}
</code>```

should I just simply extract the nested part into a new method? The code will be much more clean.

```<code>...
try {
  ... code
} catch (FirstKindOfException e) {
   panicMethod();
}
...

private void panicMethod(){
try{
... do the nested things
catch (SecondKindOfException e) {
  ... do something else
  }
}
</code>```

It is obvious if you have to nest three or more levels of try-catches, in a single method, that is a sure sign of time for refactor.

Body:
It is surely decreasing the code readability. I would say, if you have chance, then avoid nesting try-catches.

If you have to nest try-catches, always stop for a minute and think:

do I have the chance to combine them?

try {
  ... code
} catch (FirstKindOfException e) {
  ... do something
} catch (SecondKindOfException e) {
  ... do something else
}

should I just simply extract the nested part into a new method? The code will be much more clean.

...
try {
  ... code
} catch (FirstKindOfException e) {
   panicMethod();
}
...

private void panicMethod(){
try{
... do the nested things
catch (SecondKindOfException e) {
  ... do something else
  }
}

It is obvious if you have to nest three or more levels of try-catches, in a single method, that is a sure sign of time for refactor.


问:
Why is the finalize method included in Java?
According to this post, we should never rely on the finalize method to be called. So why did Java include it in the programming language at all?

It seems like a terrible decision to include in any programming language a function that might get called.

答:
Purpose of this method is explained in API documentation as follows:

it is invoked if and when the Java virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized...

the usual purpose of finalize... is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded...

If you're additionally interested in reasons why language designers have chosen that "object is irrevocably discarded" (garbage collected) the way that is beyond application programmer control ("we should never rely"), this has been explained in an answer to related question:

automatic garbage collection... eliminates entire classes of programming errors that bedevil C and C++ programmers. You can develop Java code with confidence that the system will find many errors quickly and that major problems won't lay dormant until after your production code has shipped..

Above quote, in turn, was taken from official documentation about Java design goals, that is it can be considered authoritative reference explaining why Java language designers decided this way.

For a more detailed and language agnostic discussion of this preference, refer OOSC section 9.6 Automatic memory management (actually, not only this section but the whole chapter 9 is very worth reading if you're interested in stuff like that). This section opens with an unambiguous statement:

A good O-O environment should offer an automatic memory management mechanism
  which will detect and reclaim unreachable objects, allowing application developers to
  concentrate on their job — application development.

The preceding discussion should suffice to show how important it is to have such a
  facility available. In the words of Michael Schweitzer and Lambert Strether:

An object-oriented program without automatic memory management is
    roughly the same as a pressure cooker without a safety valve: sooner or later
    the thing is sure to blow up!


答:
Finalizers are important for the management of native resources. For example, your object might need to allocate a WidgetHandle from the operating system using a non-Java API. If you don't release that WidgetHandle when your object is GC'd, you're going to be leaking WidgetHandles.

What's important is that the "finalizer is never called" cases break down rather simply:

The program shuts down quickly

The object "lives forever" during the lifetime of the program

The computer turns off / your process is killed by the OS / etc

In all three of these cases, you either don't have a native leak (by virtue of the fact that your program is not running anymore), or you already have a non-native leak (if you keep allocating managed objects without them being GC'd).

The "don't rely on the finalizer being called" warning is really about not using finalizers for program logic. For example, you don't want to keep track of how many of your objects exist across all instances of your program by incrementing a counter in a file somewhere during construction and decrementing it in a finalizer -- because there's no guarantee that your objects will be finalized, this file counter will probably not ever go back to 0. This is really a special case of the more general principle that you shouldn't depend on your program terminating normally (power failures, etc).

For management of native resources, though, the cases where the finalizer doesn't run correspond to cases where you don't care if it doesn't run.

Title: Why is the finalize method included in Java?

Body:
According to this post, we should never rely on the finalize method to be called. So why did Java include it in the programming language at all?

It seems like a terrible decision to include in any programming language a function that might get called.


问:
Using GPL library in our software
Currently we're starting to work on a project for which we're going to need image processing libraries. The problem is some of the libraries are GPL licensed.

I'm reading through GPL licence and quite a lot of other ones (FreeImage public licence etc..) and I'm a bit confused of all the long text. So I'll ask bluntly:

Is it possible to use a GPL licenced library in our commercial product (not modifying the code, just building the library and using it in our product (as a DLL)) without releasing the source code of our product?

答:
Don't distribute

The GPL is primarily about distribution of your software (what they call "conveyance").  If you only use your app and the GPL library in-house, the GPL does not consider this conveyance, and the copyleft provision is not triggered.  You therefore don't need to make your program open-source.

Communicate at arms length

If your program does not depend on the GPL library or application for its proper functioning, and you communicate at  arms length with it through spawning a separate process, pipes or something similar rather than linking the library in your application directly, then the GPL considers your program a separate application (instead of a derived work), and you don't need to make your program open-source.

Title: Using GPL library in our software


问:
longest list of words with matching start and end letters
My friend gave me a problem that he says is easy, but I can't figure out a good algorithm to use to do it.

You are given an input of 100 random English words.  You have to find the longest string of words where the last letter in one word matches the first letter in the next word.  You can only use each word once.

For example, if you were given the words "cat", "dog", "that", the longest string you could make would be "cat -> that".  If you were given the words "mouse", "moose", "unicorn", the longest string you could make would just be one word (since none of those words link).  If you were given the words "bird", "dish", "harb", the longest string you could make would be "harb -> bird -> dish" (or "dish -> harb -> bird" or "bird -> dish -> harb").

I came up with the idea of modeling this as a directed cyclic graph.  Each node would just be a word, with vertices going to each word/node that started with the letter this word ended with.

```<code>+-------+         \ +------+
|  cat  |-----------| that |
+-------+         / +------+
    |                  |
   \|/                 |
+-------+ /            |
|  the  |--------------+
+-------+ \
</code>```

This problem appears to be a longest path search, which is NP-Hard.

Is there a better way to do it?  Or even some sort of approximation algorithm that could be used?  Or some way to exploit qualities of English to reduce the search space?

答:
If you make 26X26 matrix to represent directed graph of vertex as each alphabet and words as edge.
For example word - APPLE connect vertex A and E with edge directed from A to E.
Now the problem reduces to finding largest Eulerian trail (path which includes maximum number of edges, visiting each edge once wit possible repetition of vertices) in the graph.
One of the O(E) algorithm would be to start randomly from a pair of vertices.
Find a path between them. Than keep relaxing the path till it is possible.

update
@GlenH7 I solved a similar question on www.hackerearth/jda recently, there was relative marks with respect to best solution and I scored the highest marks with the following approch-

Given list of words. Find the longest chain that can be formed by them. A chain is valid if every word begin with a letter *ending at the ending of last word.

Approch =

1)make the graph of alphabets as vertices and words as edges.
  In place of using multiple edges use one with weight equal to
  number of edges.

2)find the strongly connected component of graph with
  maximum edges. Temporarily discard other edges.

3)For each vertex make its indegree equal to its outdegree.

4)Now their exists eulerian circuit in graph. Find it.

5)Now in remaining graph(w.r.t orignal graph find the longest
  trail with first vertex in chosen strongly connected
  component. I think this is NP hard.

6)Include the above trail in Elerian circuit converting eulerian
  circuit into trail.

Why - I accept that this question is most probably NP hard(guess, not mathematically speaking). But the above approach works best when there is a long list(1000+) of uniformly distributed words(i.e. not intended to be w.c. for above approach).
Let us assume that after converting given list to graph mentioned above, it luckily turns out to be a eulerian graph(see http://en.wikipedia.org/wiki/Eulerian_path for conditions), then without any doubt we can say that answer to above question is P and is actually the eulerian path in the graph(see http://www.graph-magics.com/articles/euler.php for a very simple approch to do so and see this to verify that your graph has single http://www.geeksforgeeks.org/strongly-connected-components/ and if not temporarily clean other small scc because eulerian path exists for single scc). Thus for not lucky cases(which are almost all cases) I try to convert them to lucky cases(i.e eulerian trail condition are fulfilled). How to do this? I tried do increasing depth search for irrelevant edges(the set of edges in a path staring from vertex with outdegree greater than indegree and ending at vertex with indegree greater than outdegree). Increasing depth search means that first I searched for all such set of one edge in path than two edges in path and so on. It may seem at first look that ith depth search would take O(nodes^i) thus total time complexity of O(nodes + nodes^2 + nodes^3 + ....) till it is a lucky case. But amortized analysis will revel it is O(edges). Once it is reduced lucky case find eulerian circuit.

Till here it was all polynomial time. This would give almost the best solution. But to further increase your solution(perfect solution is NP hard) try some greedy approach in remaining graph to find a long trail staring with one of vertices in chosen scc. Now add this to above found eulerian trail to further increase it.


问:
How to execute batch jobs via webservices or rest api
I have an AppHarbor site where I need to do weekly updates to some of the data. I don't want to go the route of deploying an exe and adding an additional "webworker" to my site because those cost money. My thought is to add a web service/REST api service to the site so I can just call it, it will execute the "batch job stuff", then when it's complete, return a custom status code, like success or failure. Behind the scenes, it would update a "BatchLog" table or something like that, then I could create a page/view where I could access the log details and see which batch processes did or did not run.

So, that's how I'm thinking I want to implement, but I'm a little skeptical of the security around this. First of all, obviously I don't want ANYONE to be able to kick off these batch jobs just by going to my web service/rest api.

To fix that, I'm thinking there are 2 different ways to do this, or a combination of both.

1) Require some credentials and maybe an additional secret code in order to get them to actually kick off.
2) Configure in a table when each batch job can actually run, and the frequency. That way, if a hacker does call my service/rest api. It will only be able to execute 1 time per hour/day/week/month/etc. So they could hammer the service, but each successive call would just return a failure, or something like that.

One thing to note, I read someplace a couple months ago that there are cloud services out there that will "schedule" batch jobs like this for you. And the free one that I read about will do 1 service. Any more than 1 and you have to start paying for it. So for my example, I'd just create one service, and have it called multiple times per day/week, and let my BatchJob configuration table determine whether it actually needs to process or not.

So, how horrible of an idea is this? What are some other approaches to accomplishing batch jobs in a cloud environment where they don't offer batch services.

答:
This is quite normal and in many environments just the easiest way to do this. I have various batch jobs running that often enough don't do more than calling a REST API controller with curl (on the same machine as my web server).

For the protection part you have many options. Simple user authentication is easy enough and should be safe if coded properly (and you can use a super complex password and totally weird user name if you like, same for the URL of the task), in addition you could limit requests to certain IP addresses, if the batch is running on the same machine then limit to localhost is as secure as you can get. If you can change settings of the web server there would be even more options, if not your code should be able to do most important things anyway.

Also as you write you can limit the batch processing by querying the time and date. I do this anyway, since some jobs should not run every hour. So in my case there is only one controller called hourly and that decides what to do dependent on time. For example some heavy load image processing is done only once at night, one hour later some other heavy worker is running and during the day some simple data import runs every hour.


答:
According to Joshua Bloch's Effective Java (Second Edition), there are two scenarios when finalize() is useful:

One is to act as a “safety net” in case the owner of an object forgets
  to call its explicit termination method. While there’s no guarantee
  that the finalizer will be invoked promptly, it may be better to free
  the resource late than never, in those (hopefully rare) cases when the
  client fails to call the explicit termination method. But the
  finalizer should log a warning if it finds that the resource has not
  been terminated

A second legitimate use of finalizers concerns objects with native
  peers. A native peer is a native object to which a normal object
  delegates via native methods. Because a native peer is not a normal
  object, the garbage collector doesn’t know about it and can’t reclaim
  it when its Java peer is reclaimed. A finalizer is an appropriate
  vehicle for performing this task, assuming the native peer holds no
  critical resources. If the native peer holds resources that must be
  terminated promptly, the class should have an explicit termination
  method, as described above. The termination method should do whatever
  is required to free the critical resource.

For further reading, refer to Item 7, page 27.


问:
Use of AGPL software on server with closed-source software
I need to use some software under the AGPL to populate a database with some data. This software has a web interface, which I shall disable.

Then the data will be used "at arms length" with closed-source software by reading the database. I may also use some IPC, like datagram sockets to provide a bridge between the AGPL software and the other software. This closed sourced software will provide a web interface to the user.

Am I right that it is not breaking the AGPL to have this software as closed source? If I modify the AGPL software, am I able to hide these modifications, so long that the software doesn't provide any network interface to other users?

答:
That's not an "arm's length" connection, but let's say for the sake of this discussion that it is.

In order to not be considered a derived work, your software must not depend on this "arms length" relationship in order to function properly.  Is that true of your software?  If it is not true, then you've created a derived work, and your software falls under the AGPL's copyleft provisions.

You may also sidestep the AGPL by not distributing.  The copyleft provisions do not apply if the software is "for internal use only," and is not "conveyed" to others outside your organization.


答:
CAVEAT: I may be out of date, but this is my understanding as of a few years ago:

In general, there is no guarantee of when a finalizer runs -- or even that it runs at all, though some JVMs will let you request a complete GC and finalization before the program exits (which, of course, means that the program takes longer to exit, and which isn't the default mode of operation).

And some GCs were known to explicitly delay or avoid GC'ing objects that had finalizers, in the hope that this would produce better performance on benchmarks.

These behaviors unfortunately conflict with the original reasons finalizers were recommended, and have encouraged the use of explicitly-called shutdown methods instead.

If you have an object that really has to be cleaned up before being discarded, and if you really can't trust the users to do so, a finalizer may still be worth considering. But in general, there are Good Reasons that you don't see them as often in modern Java code as you did in some of the early examples.


问:
Is OAuth suitable for this scenario?
I need to create a simple web application for track expenses, with some basic actions (user must be able to create an account and log in, list expenses, edit them, etc) with a REST API for each one, and the trick is that I need to be able to pass credentials to both the webpage and the API.

So, after some research I've found some examples using Digest Authentication and HMAC Authentication but lot of posts also mentioned OAuth as an alternative approach, so my question is, given this scenario, would be proper to use OAuth? I mean, as far as I understand OAuth is suitable when you want to share resources with other application, which I'm not doing for this project; besides that, when you try to access the shared resource it appears a page requesting permission for the foreign application, would that page appear at some point in my application? (maybe after the login?)

In case OAuth wouldn't be suitable, is there any other option besides Digest authentication and HMAC authentication?

答:
OAuth 2 is a token-passing scheme. There are several different ways you can get an OAuth server to grant you a token. Once you have the token, you pass it into the service (over SSL, to protect it), and if the token is good -- and not expired -- then you can get the page or information you want.

The token-passing scheme is very easy to work with. It's very similar to a "session ID" that you send to the service, except that it comes as an Authorization header instead of a cookie or query parameter.

The part that might make OAuth harder for you is setting up the authorization on the server side; there are several server-side implementations of OAuth out there, but configuring them for what resources are protected and what aren't is a little more complicated than just having a list of usernames & passwords. There is also the "scope" allowed a client, what grant types are available to a client, and what resources that client can access. It might require a little more learning curve than something simple like Digest or HMAC.

Also, while I've protected a lot of REST services with OAuth, I've never protected web pages. So I'm not sure how to do that. With a template-driven framework like Spring MVC, their OAuth solution would fit in very nicely. Short of that, though, I'm not sure how you'd go about it (getting the browser to attach the right Authorization header to pull a page).

OAuth is more versatile than the other schemes, though, so if you're putting together a business site (as opposed to your own project) the extra learning curve and setup time could be worth it.

Title: Is OAuth suitable for this scenario?


答:
The Google Java Style Guide has some sage advice on the subject:

It is extremely rare to override Object.finalize.

Tip: Don't do it. If you absolutely must, first read and understand Effective Java Item 7, "Avoid Finalizers," very carefully, and then don't do it.


问:
Third-party repositories in Subversion; local mods?
I'm building a small app as an interface to support functions, monitoring, web-based running of unit tests, etc. This will be checked into Subversion.

I want to include a third-party app which is also available via Subversion. Thus far I prefer to reference the remote repository to ease upgrades, particularly when I have made local modifications.

Now, I want the third-party app to be distributed alongside the parent app. Subversion externals are out, since I can't commit my local mods. git svn seems like it has potential, but I'm not sure on the details. (Seems dirty to have a .git directory with svn metadata stored in an svn repository.)

So how do you handle modifications to Subversion projects you can't commit to? I'm pretty open to any combination of git and svn, preferably allowing the code to live in our existing Subversion repository.

Update: I'm currently exploring vendor branches, which is more convention than feature. There is a tool called svn_load_dirs.pl to assist with the process.

答:
I handle the situation of external repositories by creating a copy on our local repository, and by periodically merging the changes from the external repository to the local repository (i.e. svn merge from external into my working copy, then commit). Thus, I can keep reasonably up-to-date on the external code, but I'm able to make (and keep) the local changes I need.

Obviously, you need to make sure that you have permission to distribute a modified version of their code alongside your app.

ContentLicense: CC BY-SA 2.5


问:
What's the best way to store options with multiple/boolean choices in an Android app?
I'm working on an Android social app which connects to a Postgresql database for up-to-date user data and is going to use lookups for dropdown menus, and these lookups will either have multiple options (e.g. for eye color, the menu would show 'blue', 'brown', 'green', 'hazel', 'red', 'white') or boolean options (e.g. for smoking, the menu would show 'non-smoker' or 'smoker').

Should I store these within the app? They're not really high sensitivity/confidentiality. I was originally thinking I would store them in the Postgresql database but in hindsight that seems overkill and would possibly start to affect performance and cause unnecessary hit on the database server.

What would be the best way to store these within the app/on the device? For arguments sake, I'm using the current latest Android version (4.4.4, though obviously not only targeting that one).

答:
Firstly your question is against the sites guidelines as it is something which will lead to discussion.
Still to answer your question, you can create a sqlite db and inside it a table with all this static values to be displayed in drop downs, put the sqlite db in assets folder, on app first launch copy it to applications default db location and read it from there and display in drop downs. At the same time you can always integrate a mechanism of downloading values from server and updating local sqlite table, in case you add any new values to a drop down field on your postgresql database side.


问:
Trivially Sortable Encoding for Arbitrary-Precision Decimals
I'm looking for a reasonably(*) space-efficient way to encode arbitrary-precision decimals (e.g. BigDecimal), such that when sorting the bit-pattern of the encodings lexicographically, the numbers will be sorted numerically. This is related to this Stackoverflow question.

One simple/naive approach to this would be to convert the number to some IEEE-like representation with some 8-byte exponent (which is what RR in the NTL does?), but obviously this is quite an inefficient(*) representation.

Is there perhaps some other approach to this? Are there any optimizations possible for arbitrary-precision Integers? The encoding need not support any other operations than sorting (and, if possible, decoding the number again).

(*) By reasonably speace-efficient I don't mean cramming bits together into bytes like IEEE-754 does. I just don't want to have any unnecessarily large fields that will remain mostly empty in a large number of situations (as would be the case with an 8-byte exponent). The motive for this is that the (encoded) number will later on be processed by this scheme, which takes a few milliseconds per byte to run.

The runtime-efficiency of the encoding/decoding on the other hand isn't much of an issue (anything less than 10 ms for 128 bytes of data will still be overshadowed by the runtime of the OPE scheme liked above).

(Also note, this should be a decimal representation, so there are to precision-issues due to "binary stuff".)

答:
An interesting computer science problem. An encoding strategy that achieved this would necessarily have more or less the following features.

The sign is represented in such a way that the high order bit is 1 for positive values, and all following bits are complemented for negative.

The exponent is encoded as its magnitude, preceded by its length (which must be fixed). A positive exponent has a length preceded by a 1 bit, negative has 0 and the length and magnitude complemented.

The mantissa is encoded as a normalised fraction, occupying as many bits as required. The compare function should assume that trailing bits are zero.

The layout something looks like this.

S 1L EEEE MMMMMMMMMMMMMMM

  S 0L EEEEE MMMM

It's much simpler if the arbitrary precision requirement is dropped or relaxed somewhat.

Note that this depends on there being a canonical representation, and there is no room for NaN or similar values. Zero is not zero.


答:
The Gnu AGPLv3 differs from the GPL only in how it applies to a situation where (a) you modify the software and (b) network users can "interact" with the software. In that case you are required to distribute the source code.

[If this is not the licence version you had in mind, then please amend your question.]

This part of the licence does not have "distribute" or "merge" wording, and does not seek to include "covered" software under the ambit of source code distribution. Your closed source code is safe.

Am I right that it is not breaking the AGPL to have this software as closed source? If I modify the AGPL software, am I able to hide these modifications, so long that the software doesn't provide any network interface to other users?

Yes, and nearly. You must ensure the modified software does not provide any public network interaction to avoid this term. That's broader than "interface".

Title: Use of AGPL software on server with closed-source software

Body:
I need to use some software under the AGPL to populate a database with some data. This software has a web interface, which I shall disable.

Then the data will be used "at arms length" with closed-source software by reading the database. I may also use some IPC, like datagram sockets to provide a bridge between the AGPL software and the other software. This closed sourced software will provide a web interface to the user.

Am I right that it is not breaking the AGPL to have this software as closed source? If I modify the AGPL software, am I able to hide these modifications, so long that the software doesn't provide any network interface to other users?


问:
What to consider when choosing a framework for a development team?
As I'm finishing a web development course with a few friends with whom I have worked in many projects we're aiming to begin a professional team of developers. Functions are well defined between programmers, analysts and designers, and the team shouldn't get to a much larger number than six members.

To quicken development and keep each developer's work from overlapping with others', an MVC framework has been considered by the group. The front-end developers shouldn't mind which one is chosen since it's all based around web standards for them, but me and the other programmers have been considering a few options.

Since the question is getting really verbose, I'll just summarize what I would like more experient developers' opinions on:

What should be considered when choosing a web framework? For small projects, performance shouldn't be a killer factor, but would it still be reasonable to remove Ruby/Python systems from the competition?

Is being able to work on more than one programming language, or more than one framework in a given language something important?

Should the learning of one or more frameworks be done in group, or on each developer's own, and then gather opinions on them?

答:
I guess most important thing while choosing a framework/platform is the technological strength of your team members / developers.

And yeah MVC for maintenance/re-useability point of view.

But for a small, quick development - I would suggest PhP is best ! Even team doesnt know or have other web framework knowledge, they can adopt it very quickly with very simple learning curve.

What should be considered when choosing a web framework? For small projects, performance shouldn't be a killer factor, but would it still be reasonable to remove Ruby/Python systems from the competition?

As mentioned team's strength and available technical know-how.

Is being able to work on more than one programming language, or more than one framework in a given language something important?

It depends on what you wish to achieve, yes it's always good to have a heterogeneous resource pool. But that might be very complex to maintain, motivate and drive.

Should the learning of one or more frameworks be done in group, or on each developer's own, and then gather opinions on them?

Any learning in group is good if it's target oriented, measured and involvement. Sometimes lazy guy keep quite in group and the purpose is not served. XP methods like pair programming can be helpful as well.

Finally, is there a more recommended project architecture/organisation method for team projects than MVC for the Web? What are the viable options?

Again framework/technology depends upon the team's strength. But use some agile aproach to measure the goals and steer accordingly.

Good luck !


问:
Is Microsoft Solver capable of solving such complex scheduling problems
I'm writing a application that will decide the scheduling plan for a few dozen people according to their occupation (role) and availability (week day and hours).

I've used a excel spreadsheet to help solve this problem, I was then able to devise a logical and decision based approach to assign these people to shifts and roles, following a few set of constraints.

When I was about to write this into code, I was suggested to try Microsoft Solver. I've read a while about it's capabilities but I'm unfamiliar with some terminology encountered.

Well, suffice it to say that I never used Microsoft Solver and I do not even know if the following scheduling problem can be solved with it or not. So far I've found very few examples about scheduling problems and even those are simple by comparison.

So my question is, can Microsoft Solver solve the following scheduling problem?

Scheduling problem

Assign people to shifts for an entire week based on their availability and what role they are qualified to fill following a few set of constraints and goals.

Structure

Weeks are composed of seven days.

Days are composed of shifts (dawn, morning, afternoon and night sifts are the most common). Shifts are specified in this way:

Dawn: 00:00 AM -> 08:00 AM

Morning: 08:00 AM -> 12:00 AM

Afternoon: 01:00 PM -> 07:00 PM

Night: 07:00 PM -> 00:00 AM

There are some weekdays which might not have any shifts while others can have one, two or more.

Each person has a weekly availability following the same week day / shift structure.

Example: Peter is only available on Monday night's (Monday 07:00 PM -> 00:00 AM).

Sometimes people have two or more days on which they are available and would like their schedule to rotate through these.

Example: Kyle is only available on Wednesday night's and Thursday morning's, also he would like to rotate them. One week Wednesday another week Thursday.

Each person has a set of roles he or she can fill (one or more).

Driver

Medic

Doctor

Goals

Each shift must have at least: two drivers, one doctor and three medics.

Every person with availability must be assigned to a shift, it does not matter if one shift has three drivers while another only has two.

Constraints

A person can only fill one role per shift.

A person can only be assigned to a shift if he or she has availability to perform it.

A person can only be assigned to a role if he or she is qualified to perform it.

A person can only be assigned to one shift per week, unless when the organization is understaffed (by understaffed means we need more people to fill shifts to meet goal #1). If thats the case one additional condition applies when assigning people to shifts after the first one.

A person assigned to a second shift in the same week must be the person that hasn't performed a second shift for the longest time, while still meeting the person's availability and role capability.

Moderator note: Could not find the appropriate tags.

答:
If Solver is appropriate depends primarily on how you build your model for the given problem, and if the problem size stays within the boundaries of the solver. According to this site, the limit for Excel Solver is 200 decision variables and 100 constraints (they are also promoting their premium solver product for bigger problem sizes). But when you use Microsoft Solver Foundation directly from C#, and not the Excel add-in, the limits are much higher, depending on the Solver variant at least 5000 constraint terms.

Some ideas how to build a model:

you may create a model involving a 0-1-matrix with one row per "person" and 28 columns "shifts of the week" (assumed there are 4 shifts per day). The roles could be added by extending the matrix value interval from 0-1 to 0-3, (0=not assigned, 1-3 assigned for a specific role) so without additional decision variables.

or you assume that each person takes at maximum 3 shifts per week (even in the "understaffed" case), you may build your model in a different way, using the columns for indexes from 0 to 28, pointing to "first shift of the week, second shift, third shift" (0="not assigned"). Adding the roles to this second model may involve 3 additional matrix columns for the roles of the first/second/third shift.

Then you have to build your constraints accordingly. For example the constraint "minimum needed roles per shift" will result in at least 3 x 28 solver constraints (3 per shift, summing up the number of "roles" assigned for that shift and telling the solver how many persons are expected at minimum for that role). Each of the four constraints you listed above for the persons will need at least one corresponding solver constraint,

I hope you got the idea of how to estimate the problem size from the above. On that estimation, decide for yourself if these limits are sufficient to solve your problem, and which solver variant you need.


问:
Best Mechanism for Generating License Keys
What methods / libraries / tools would people suggest for generating license keys (those lovely AAAAA-AAAAA-AAAAA-AAAAA-AAAAA things you put in when you register software)?

Any gotchas to look out for when implementing them?

(At the moment I'm interested in this as a general thing rather than language specific so just state what language you're using if your solution is language specific).

答:
This same question was asked on SO and the accepted answer is pretty good.  The general gist is:

Take the user's name

Concatentate the users name and the secret key and hash with (for example) SHA1

Unpack the SHA1 hash as an alphanumeric string. This is the individual user's "Product Key"

Within the program, do the same hash, and compare with the product key. If equal, OK.


问:
DDD with entities with no relations
Disclaimer: I'm not 100% sure I understand some of the practices and concepts in DDD, so this question may actually be answered by clarification of the ideas

I'm working in a legacy system (written in PHP) in which the database tool provides no object relational mapping, and thus objects coming out of the database access layer tend to be, more or less, mirrors of the rows returned by the query.

For newer modules, I'm trying to apply DDD. However, I'm struggling with how to build a rich domain model when my entities have no relations. Take for example the following requirement: users can see the messages they have previously accepted, where a message being accepted is, internally, recorded by a single new row in a database table.

Were I using an ORM and did my entities have relations, I may write something akin to $user->getAcceptedMessages(), which would return a collection of message entities. However, since I don't have that, I'm torn between two approaches:

Populate the relations as part of the building of the entity in the repository. The queries could get big, unwieldy and hard to maintain as the number of relations grows and the schema changes.

Create services for actions such as this, which depend on the repository and a take a user, eg UserMessageAcceptanceService. However, I suspect this might lead to an anemic domain model.

Is either approach sensible in this scenario? Or is some form of ORM a requirement for applying DDD effectively?

答:
DDD is about modeling the business cases. It has nothing to do with how the entities and relations are persisted. DDD was actually conceived in time when ORM was not much used and when you read the original DDD book, you will notice they talk a lot about using pure SQL for persistence. That is why Repositories in DDD exist. Just because you don't persist the relations in DB doesn't mean your entities don't have them. They do.

In your case, both solutions are possible. Actually using both in single project is best way, because both have it's advantages and disadvantages. In second case, it would actually be better if such method was part of the repository itself. There is no need to create specific service. Third option would be to use lazy-loading like behavior in getAcceptedMessages method and load the data only on request. But you should be careful with that.

Title: DDD with entities with no relations


问:
Does it make sense to use tag without version numbers?
In our working group we had a discussion on using git tags in our projects. Some people argumented pro tags and some argumented against using tags.
The crux is that we are talking about Magento projects which aren't really using version numbers, hence the tags only consist of 20140624 date references.

The pro side was that it is easier to revert to a tag than a normal commit hash and it is part of gitflow, the contra side was that it only creates overhead.

What do you think on using git tags only for reverting history?

答:
My place uses tags for releases and hotfixes, where hotfixes are a letter subset of the current release.  Ie

```<code>release/1.0.1
hotfix/1.0.1a
hotfix/1.0.1b

release/1.0.2
</code>```

and so on.  It helps keep merge points grouped together by a common value so you can be sure what was put in where.  Certainly, it creates overhead because you're reverting multiple commits, but wouldn't that happen anyway?

Body:
My place uses tags for releases and hotfixes, where hotfixes are a letter subset of the current release.  Ie

release/1.0.1
hotfix/1.0.1a
hotfix/1.0.1b

release/1.0.2

and so on.  It helps keep merge points grouped together by a common value so you can be sure what was put in where.  Certainly, it creates overhead because you're reverting multiple commits, but wouldn't that happen anyway?


问:
Open source license that requires visible attribution in derivative works?
My company has a client-side web application that we would like to open-source. We don't want to impose any restrictions on usage, except that any modifications and derivative works should contain a visible attribution, e.g. show our name and/or logo - we want it to be visible that our code is being used in the application.

As I understand, while licenses like BSD 3-clause contain an attribution requirement, it can be satisfied with something like a HTML comment only. How to license to ensure that derivative works are visibly labeled?

答:
I shall assume that your main purpose is to tell potential users of your source code what you would like them to do, rather than actually suing anybody. For the latter, please see your friendly lawyer.

I would suggest you need three things.

Start with a base licence in a form that covers most of the bases, but does not have the specific clause you want. The advantage is (a) the language sounds right and (b) it probably has the right words just in case some lawyer does read it.

Draft one single clause that clearly specifies exactly what you want them to do. Specify exactly your name and logo, how prominent it must be, where it must be shown and so on. Read other licences to see the kind of language to use. The Gnu licences are quite good at that, but any will do. Add this clause to the licence and renumber as needed.

Write a plain English preamble which explains what you want users of the software to do in the simplest possible language. Put that preamble on the download page in prominent text so they couldn't possibly miss it.

Sounds like you can start with BSD and just add your own clause. Not that hard really, unless you plan to sue someone. Which you don't.


问:
Is there a benefit in compiling your code as you go along?
I recently had a job interview in which they gave me an hour to write some real code.  It wasn't a huge amount, probably less than 100 lines.  After about 45 minutes, I compiled, ran it, and got it to work.  I may have spent 5-10 minutes working out compile errors and a couple minor bugs, but overall it was very smooth.  (Incidentally, I did get an offer from them.)

However, what puzzled me was that after I handed over the completed code, the interviewer told me that the only thing I did wrong was "not compiling as I go along".  I asked him what the difference is, and he said "what would you have done if you finished the code and it didn't compile in time".

In my understanding that's an invalid argument, because "getting code to compile" for a given length of code generally involves fixing a constant number of compile errors and takes a fairly constant amount of time, which should be the same whether you do it after you finish writing the code, or if you interleave it with your coding time.  If anything, interrupting your coding to search for missing semicolons would probably be detrimental to your efficiency.  Except in extreme circumstances when I'm experimenting with obscurities around edge-cases on things like virtual functions in derived classes, etc. it seems reasonable to expect that code written by a an experienced developer will compile, minus the occasional typing error, and even if it doesn't, it's not as if I would have to rewrite a portion of the code in order to fix the compile error.

In another similar incident, I was given an incomplete codebase in an interview, and asked to finish it and make necessary modifications to get it running.  I started by reading through the existing code, and then after a few minutes (even before I had finished looking at the code), the interviewer told me that's enough.  When I asked him what he would have done (i.e. "what did I do wrong"), he told me that he would have started by immediately getting the code to compile.

Why is that even relevant?  In my opinion and in my experience, whether or not a piece of code compiles is essentially random, involving things like whether or not semicolons are missing, and has little to do with the correctness of the underlying program.  (To me, focusing on compiling is like running an article through a spell-check without proofreading to check the grammar.)

If you give me a piece of incomplete code, the first thing I do will be to read it.  I won't even try to compile it until I know what the code is doing and I know the algorithm is correct.

Anyway, these have been just a couple recent incidents, but in general I've heard many developers talk about compiling their code as they go along, and yet nobody has been able to tell me the benefit of doing so.  I understand the benefits of testing your code as you go along, but why compiling?

So my question is this: Is there something I missed?  Is there actually a benefit to compiling as you go along?  Or is this some sort of myth propagated by the software community that you must compile your code frequently?

答:
In my opinion and in my experience, whether or not a piece of code compiles is essentially random, involving things like whether or not semicolons are missing, and has little to do with the correctness of the underlying program. (To me, focusing on compiling is like running an article through a spell-check without proofreading to check the grammar.)

My experience is very different: less than 5% of compile errors I get are about syntax. I know the language well, when I get errors it's mostly type errors, telling me that the semantics are not correct.

This is why I am happy to benefit as quickly as possible from the feedback of my compiler.
Have you ever experienced using an IDE which underlines compile errors in real-time? Having a shorter feedback loop can be very valuable.

If you give me a piece of incomplete code, the first thing I do will be to read it. I won't even try to compile it until I know what the code is doing and I know the algorithm is correct.

If you are expected to work on code written by someone else, you don't always have time to read everything. The good news is: well-written code has low coupling, and should enable you to reason independently about just the part of the code that you need.

In those cases you have to assume that the code you have not yet read is correct, and investigate lazily when there is a problem.

"getting code to compile" for a given length of code generally involves fixing a constant number of compile errors and takes a fairly constant amount of time, which should be the same whether you do it after you finish writing the code, or if you interleave it with your coding time.

Context-switching is costly for your brain, therefore fixing small errors as soon as you write them can be more efficient.

EDIT: I can also make an analogy with source control, when a whole team is working on the same sources. Compiling as you go along is like making frequent commits, it helps to avoid having a lot of pain at the end when you have to merge and sort out everything.

You say that you disable things like red lines under your text. Do you also do that when typing an email or writing a technical document? Then you have to proof-read again all the pages instead of fixing mistakes as you go.

Another advantage is, when working on your code, if you keep it compiling or near-compiling at all times, you can benefit from a lot of semantic-based IDE features (safe renaming, refactoring, finding uses of a symbol...).

If you want to have a better understanding of how these features help, you could try enabling them and practicing, to experience yourself their benefits.
You could also try to do some pair-programming with anyone that is used to them, and see how they benefit from them.


问:
Could taking design lightly be a pitfall of agile methodologies causing constant rework or is it a misunderstanding of the methodology?
I've been working for a year now in an agile project to redesign an insurance application.
I really like working in an agile environment, although management and analysts I work with still have waterfall thinking ingrained and makes it a little complicated to embrace the new paradigm shift to agile.

From a developer standpoint sometimes I feel like since we're doing "agile" we don't over-think or think enough our designs and just go with the flow and refactor later. Many times that ends up in rework because we never put enough though to certain pieces of the application. As far as I understand, agile is like having small waterfalls and deliver small pieces at a time (iterations), but sometimes we lose the big picture and end up paying for it later with rework.

Are agile methodologies encouraging or tolerating procrastination more than traditional waterfall? Is this constant rework for pieces of software with defined requirements acceptable?

Is anyone else experiencing something similar in their projects?

答:
In the agile setup I am working with,It is what the team wants.The definition of team here does not include managers and business folks.I agree this may not be possible if you are working in a place where business people have a more say at technical issues.

In our case if the team feels that some feature needs a design discussion, we do come up with design documents and nice block diagrams.But this is sometimes an overkill for certain features and makes more sense to get it over with as quickly as possible.

It might help to have a tech person experienced with agile workflows to steer demos and retrospective where the respective business person can also participate and understand what working in agile actually means.

Every team is different so It is up to your team to figure out what is best for you,And I feel agile lets you do that more easily than other models open communication opportunities with demos,retrospectives which sparks discussions and improvements.


答:
It's about the same as when storing passwords. You should have a unique secret key known only to the generator and your program. Use this key to manipulate the details (user name, password, organization, etc) and then hash it. You can then do something trivial transfer encoding in Base32 on the hash or simply move it to a hex string if you don't care about a format.

Any gotchas to look out for when implementing them?

Keep secrets secret and separate.  Make your implementation improvable. If someone breaks it can you easily change the implementation? One common implementation on desktop applications is to use a remote server to validate the license. This removes the possibility that someone could reverse engineer a hash or the algorithm by inspecting the application itself.

Title: Best Mechanism for Generating License Keys

Body:
What methods / libraries / tools would people suggest for generating license keys (those lovely AAAAA-AAAAA-AAAAA-AAAAA-AAAAA things you put in when you register software)?

Any gotchas to look out for when implementing them?

(At the moment I'm interested in this as a general thing rather than language specific so just state what language you're using if your solution is language specific).


答:
Part of this is expected and good, part of this is not, and we can't really tell you which is which.

The main focus of agile is delivering what is important now, based on what you know today. Take a little bit of time, implement this important bit and ship it so that you can get feedback on it sooner rather than later. Some of that feedback will be "ugh, this actually kinda sucks in practice!". This is the good sort of rework that agile promotes. Since you caught this early, you can fix it early.

The other side of this is that what is important to business people is rarely important to developers. As developers, you need someone to advocate for technically important things like infrastructure, training, and technical debt. This can help eliminate some of the "gaps" missed by most agile environments. But in the end, the discipline to do a good job rather than just slapping together a solution will fall on you the developer - agile or not.

Title: Could taking design lightly be a pitfall of agile methodologies causing constant rework or is it a misunderstanding of the methodology?

Body:
I've been working for a year now in an agile project to redesign an insurance application.
I really like working in an agile environment, although management and analysts I work with still have waterfall thinking ingrained and makes it a little complicated to embrace the new paradigm shift to agile.

From a developer standpoint sometimes I feel like since we're doing "agile" we don't over-think or think enough our designs and just go with the flow and refactor later. Many times that ends up in rework because we never put enough though to certain pieces of the application. As far as I understand, agile is like having small waterfalls and deliver small pieces at a time (iterations), but sometimes we lose the big picture and end up paying for it later with rework.

Are agile methodologies encouraging or tolerating procrastination more than traditional waterfall? Is this constant rework for pieces of software with defined requirements acceptable?

Is anyone else experiencing something similar in their projects?


答:
When choosing any framework, consider its popularity, in terms of blog posts, StackOverflow questions, resources and documentation.  A framework that's relatively new isn't guaranteed to blossom and may make you experts in a niche framework.  Ideally you will want something that is easy for any new developer to get into.

Ensure that it is a fit for your core competency.  If you are a Microsoft shop, you will not want a PHP as there is a learning curve involved and a .NET developer would not normally be very skilled at PHP.  At the same time, there may be a resonable expectation for an ASP.NET developer to pick up Javascript MV* frameworks.

Regarding performance, if it's not an actual requirement or NFR to have lightning fast pages, then I don't think that ruling out Python/Ruby is reasonable.  You should be OK with those languages if this is an internal system with a moderate amount of concurrent users.  There are many frameworks in Python and Ruby which offer a lot of functionality that could meet several requirements, and they ought to be ruled out only if there are other barriers to development, such as testing, development, learning curve, etc.

Regarding learning - it helps a lot if there is a 'champion' of a technology.  This champion can do a workshop and teach the other developers the basics.  Having the basics goes a long way to more learning and letting the developers go through more tutorials, videos or assignments.  The success of this approach will depend on the team culture.

Regarding your last question about architecture/organisation, one interesting area would be to use a Javascript MV* framework (AngularJS, Knockout) with a RESTful API as the backend (and this can still be a regular MVC framework such as Django or ASP.NET WebAPI).  This allows you a rich, functional front end and you can focus on business logic in the backend without worrying about generating the markup.

Title: What to consider when choosing a framework for a development team?

Body:
As I'm finishing a web development course with a few friends with whom I have worked in many projects we're aiming to begin a professional team of developers. Functions are well defined between programmers, analysts and designers, and the team shouldn't get to a much larger number than six members.

To quicken development and keep each developer's work from overlapping with others', an MVC framework has been considered by the group. The front-end developers shouldn't mind which one is chosen since it's all based around web standards for them, but me and the other programmers have been considering a few options.

Since the question is getting really verbose, I'll just summarize what I would like more experient developers' opinions on:

What should be considered when choosing a web framework? For small projects, performance shouldn't be a killer factor, but would it still be reasonable to remove Ruby/Python systems from the competition?

Is being able to work on more than one programming language, or more than one framework in a given language something important?

Should the learning of one or more frameworks be done in group, or on each developer's own, and then gather opinions on them?


答:
Although agile is based on a minimum viable product and only delivering what you need, it is also based on making the best decisions you can with the information you currently have.

This means not only considering the user story you are working on, but what you know is coming, or likely to come.  The correct time for design is whenever you need it - considering dedicating time to design when you think something you are doing might cause a problem in the future.

To me agile does not encourage procrastination or delay of design decisions, it allows you to remove the huge overhead of system design at the start (which is never right anyway), and integrate it into the project as and when it is necessary.

There is always going to be a degree of re-work in software and there will always be unexpected things cropping up, but if as you say the requirements are well defined and truly unlikely to change, you can consider them in design relatively early on and may still spend a reasonable amount of time on design work at the start of the project. This can be considered as part of 'sprint zero' as well - basically planning a framework for the project.


答:
Choosing framework depending on many factors:

Time/Resources: If you have time, then you can be more adventurous to try new technologies. If you have less time, keep it simple, if you have decent amount of time, do the research and find something which fits well to your dreams. On resources: if you have scarse resources (no income, spare time per day) then make it tight and simple.

Competence: If the team has a good base on a technology which is ok, but might not be the best, it is a rational choice still, since most of you don't have to learn the basics, and the others can catch up quickly, since they have many people to ask from. Write all the competence you all have with a scale 1-5 and think through your strength (programming languages, APIs, tools etc).

Project size: If you have a small project, keep the choice as simple as it can be. Always ask: do we really need this/that feature the framework provides? The "keep it simple" pays back on the second or third projects when you think about what to do differently, and it won't be too numerous list.

Popularity: Don't pick some exotic technology, check out how many people are using it. Search forums about questions made of that technology, if it is numerous then you can judge how well it works, and also you get plenty of hits if you want to look after problematic cases.

Once we made a project we inherited a bad base, but we had the time to finish it. The project was pretty simple, so we chose GWT, and looked for some extension to work with (we chose smartGWT). We had the time to make experiments on the technology, and it was successful. If we had less time, we probably sticked to Vanilla GWT with less features. It took a month to get familiar with smartGWT (finding out what works and how, and what do we need to add up), if you don't have the time/resource, don't waste that amount of time on experimenting.


问:
Good architecture for passing data to multiple APIs

I have a hypothetical scenario where I need to pass data to APIs from multiple different providers, depending on the 'vendor identity' provided by one of the pieces of data.

If I have the data as objects within an enumerable parent object, what would be a good way to pass the objects to the appropriate API in a reasonably loosely coupled way?

Even if the API definition was more tightly hardcoded somehow, what would be a good way to actually pass the objects through to the API?

I'm writing the solution in C# although it's the general architecture that matters not the specific implementation

答:
I think it would help if you clarified how much difference in APIs there are. eg. Could ytou be using 2 location services where one takes lat/long co-ordinates, and the other XY co-ords; or are they more different like a location service and a pizza-ordering service?

Either way, your best bet is a message passing architecture. You have a broker  or message bus that your main code sends messages to, and the broker decides which API to pass the message on to. Often you will have a facade (or gateway) that sits between the broker and the APIs if there is a fair bit of translation required from the program's data that is sent to the broker and what the API expects to receive.

One benefit of a message broker like this is you can add new APIs dynamically, once they register to receive messages, they will suddenly start to receive them, no code changes required in your client program, assuming you can send the new relevant data.


问:
Coding and testing in the same sprint
How is testing handled within the same sprint as coding, if all or most of the coding is not done until the end of the sprint? (I'm referring to the "soup-to-nuts" development and testing of a single PBI within a sprint.)

Most of the answers I've seen online involve QA automation, but even that isn't really possible since you generally need a functional UI to record or create automated tests from. I only have storyboards that continue to evolve as I develop features and discover new requirements.

In my case, I am developing a new desktop application. Desktop apps don't generally lend themselves to automated testing very well. I have some automated unit tests, but they are not the manual functional/integration tests that a QA professional would perform.

So, where I'm at now is that my sprint ends tomorrow, I still have coding to finish, and my QA folks have nothing to test yet, and no idea how to test whatever I'd give them without me holding their hands.

I'm sure I'm not the first person to have this dilemma.

In the past, I've done a pipeline: in the current sprint the test team tests the features that have been implemented during the previous sprint. At my current job, the PM refers to this approach as "waterfall", and as such, unacceptable.

答: Testing and coding go hand in hand. You could schedule it module by module. Once the module is finished, you could supply it to testers. This whole scenario also depends on which phase of testing you are working on. Spiral SDLC model looks good. In that, simultaneous testing and coding is convenient. Another approach could be V model.</p>

Title: Coding and testing in the same sprint


问:
Is Visual Studio just an IDE?
For Windows Development I mean.

Looking over other questions there are alternatives to VS, but they seem to be web based ones, which is fine, or you could program an entire .net website in notepad, should the urge drive you to.

But is there more to it than just an IDE for Windows Development? I.E. Is it possible for me to create an application in just notepad, is the compiler part of Visual Studio, or is it separate, which could be called via command line or something?

I don't want to not use VS, I'm happy with it, does what I need etc etc, just more a facet I'm curious about.

答:
Compilers are available separately.

For C# it would be the csc.exe. You could call it from the command line any time. Pass along the name of the source files to compile, the libraries to reference, compilation option and here you go.

I believe Visual Studio itself calls the compiler over the command line when you ask it to build your project. The build output messages you see is what the command-line compiler returns.

Apart from this Visual Studio is more than just a GUI for a compiler. It has a nice text editor, debugger, designer tools, SQL browser, also integrates with test tools, versioning control and other instrumentary (it's extendable through plug-ins). You'd be striving hard to find an equivalent product (for the Microsoft stack) with a comparable level of consolidation.

Title: Is Visual Studio just an IDE?


问:
usage of double pointers and n pointers?
I am familiar with basic C pointers. Just wanted to ask what is the actual use of double pointers or for that matter n pointer?

```<code>#include<stdio.h>
int main()
{
    int n = 10 , *ptr , **ptr_ptr ;
    ptr = &n;
    ptr_ptr = &ptr;
    printf("%d", **ptr_ptr);
    return(0);
}
</code>```

**ptr_ptr is printing 10 but how?

答:
ptr_ptr is a pointer to a pointer to an int, and it points at ptr.
ptr is a pointer to an int, and it points at n.
n is an int, which has been set to 10.

A prefix '*' is the de-reference operator, meaning "the thing pointed to by".
So **ptr_ptr evaluates to *ptr which evaluates to n, which is 10.  If it helps, consider **ptr_ptr as equivalent to *(*ptr_ptr).

What's it for?  Two possible uses are

Passing a pointer as a parameter to a function, where you want the function to be able to change the pointer to a different one.

Passing an array of pointers to a function, as in the classic int main(int argc, char **argv).

I have never encountered any need for an int ***ptr_ptr_ptr, but it's valid C.

ContentLicense: CC BY-SA 4.0


问:
GUI code implementation for specific Window/Form class
Terminology

Element/Form Element -> Fields such as text list, combo_boxes, push buttons, etc.

Problem

I have a program that is basically a fat client that interfaces into a system that keeps track of traffic lights.

In this program exists a form. The form contains several subforms. As it is right now, some of the subforms have their own classes, and some of the subforms are just controlled completely from the class of the main form.

The main form class has a validate method which is called when the form is submitted and calls validation methods within the subforms, as well as performing validation on some of the form elements that don't have their own classes.

I'm not sold on this architecture, as I feel that it should be consistent (every subform should have its own class, or no subform should have it's own class.

My problem lies in the implementation of handling warnings. Right now, when an element is validated against, the validation results in an error or a warning. If it is an error, the user must correct the box to meet the forms validation requirements. If it is a warning, the user must be notified of that warning, and then submit the form a second time.

If a warning occurs, I have created a flag that will be set. Before the information within the form elements can be accepted, the form first checks if any errors were thrown. In the event there are no errors, it checks for warnings. If the warning flag is set, it displays the warning. The next time this method is called, the warning flag is toggled within the validation method.

I don't really like this implementation, as it basically means you have this rogue flag that should be cleared based on the requirements. I've thought about implementing it as a state machine, however you run into the same problem where the state of the form would be set in various spots within the class based on the requirements (One of the fields changed, reset to the starting state, etc).

The other problem that I ran into is that the warning flag is owned by the main forms class. I can create a pointer to the parent form from the subform, and then set the flag from there.

This all feels very clunky, and I feel like it could create a lot of confusion. To me there has to be a better way to architect this.

EDIT: Adding some code for context

```<code>bool MainForm::callback_pushbutton_submit_clicked( const FormEvent&)
{
    if ( validate_all_forms() )
    {
        //display error
    }
    else if ( get_warning_displayed_flag() )
    {
        //display warning
    }
    else
    {
        //submit form
    }

}

bool MainForm::validate_all_forms()
{
    bool error = false;
    bool display_warning = false;
    error |= subform1.validate();
    error |= subform2.validate();
    //...

    //perform some validation on subforms that are controlled within the main form
    if ( !(my_date_field.validate() )
    {
        display_warning = true;
    }
    //....

    if( error )
    {
        //format error message
        //clear warning flag
    }
    else if ( display_warning )
     if ( !get_display_warning_flag() )
     {
        //get warning message
     }
     invert_warning_display_flag()
}
return error;
</code>```

答:
I'm going to quote your question and add some code to it (it's easier to speak about it afterwards):

In this program exists a form. The form contains several subforms. As it is right now, some of the subforms have their own classes, and some of the subforms are just controlled completely from the class of the main form.

```<code>struct subform { virtual bool validate() const = 0; };

struct warning { element& f; string message; }
struct error { /* subform or element reference list, and a message here */ };
</code>```

The main form class has a validate method which is called when the form is submitted and calls validation methods within the subforms, as well as performing validation on some of the form elements that don't have their own classes.

```<code>class form {
    vector<unique_ptr<subform>> subforms;

    bool is_valid() const
    {
        auto sf_valid = [](const unique_ptr<subform>& sf){
            return sf->validate();
        });
        if(all_of(begin(subforms), end(subforms), sf_valid))
        {
            // custom *this validation code
            return true;
        }
        return false;
    }
    void update_data()
    {
        // update all data here, based on the content of the
        // forms and fields in *this
    }
public:
    void submit()
    {
        if(is_valid())
        {
             update_data();
             // submit form here (whatever that means)
        }
    }

};
</code>```

This architecture is written for the positive case only. It is up to you if you want to treat warning and error as exceptions, or not.

If you treat them as exceptions, throw instances of them in subform::validate and in form::is_valid, and catch and handle all that, in form::submit()`.

If you treat them as non-exceptional cases (i.e. part of normal application flow and not actual errors), then instead of returning bool from subform::validate and form::is_valid, return a user-defined type that converts to bool, and has accessors for any error(s) and warning(s) issued.

In submit, on the else branch or in a catch block, you should reset the fields and subforms referenced in the received error or warning instances.

Edit (explanation):

You mentioned getting rid of the flag for error/success. This implementation splits the validation from the commit phase (updating the data and submitting), and the validation is const (the functions do not alter state, and propagate information by return value/throwning).

You can then (if needed), refactor the custom validation code in form into new subforms.

Body:
I'm going to quote your question and add some code to it (it's easier to speak about it afterwards):

In this program exists a form. The form contains several subforms. As it is right now, some of the subforms have their own classes, and some of the subforms are just controlled completely from the class of the main form.

struct subform { virtual bool validate() const = 0; };

struct warning { element& f; string message; }
struct error { /* subform or element reference list, and a message here */ };

The main form class has a validate method which is called when the form is submitted and calls validation methods within the subforms, as well as performing validation on some of the form elements that don't have their own classes.

class form {
    vector<unique_ptr<subform>> subforms;

    bool is_valid() const
    {
        auto sf_valid = [](const unique_ptr<subform>& sf){
            return sf->validate();
        });
        if(all_of(begin(subforms), end(subforms), sf_valid))
        {
            // custom *this validation code
            return true;
        }
        return false;
    }
    void update_data()
    {
        // update all data here, based on the content of the
        // forms and fields in *this
    }
public:
    void submit()
    {
        if(is_valid())
        {
             update_data();
             // submit form here (whatever that means)
        }
    }

};

This architecture is written for the positive case only. It is up to you if you want to treat warning and error as exceptions, or not.

If you treat them as exceptions, throw instances of them in subform::validate and in form::is_valid, and catch and handle all that, in form::submit()`.

If you treat them as non-exceptional cases (i.e. part of normal application flow and not actual errors), then instead of returning bool from subform::validate and form::is_valid, return a user-defined type that converts to bool, and has accessors for any error(s) and warning(s) issued.

In submit, on the else branch or in a catch block, you should reset the fields and subforms referenced in the received error or warning instances.

Edit (explanation):

You mentioned getting rid of the flag for error/success. This implementation splits the validation from the commit phase (updating the data and submitting), and the validation is const (the functions do not alter state, and propagate information by return value/throwning).

You can then (if needed), refactor the custom validation code in form into new subforms.


答:
Seems like a good opportunity for message queues.

Queues are good for retrying errors and for keeping things loosely coupled.  For example, the data you're passing could be pushed onto an incoming queue.  Some process would grab it and decide which APIs should receive it, writing it to several outgoing queues.  The outgoing queues when processed pass the data along to the appropriate receiver.  This models the human workflow of workers having in bins and out bins on their desk and delivery workers who get the outgoing paperwork to the right place.

Title: Good architecture for passing data to multiple APIs

Body:
I have a hypothetical scenario where I need to pass data to APIs from multiple different providers, depending on the 'vendor identity' provided by one of the pieces of data.

If I have the data as objects within an enumerable parent object, what would be a good way to pass the objects to the appropriate API in a reasonably loosely coupled way?

Even if the API definition was more tightly hardcoded somehow, what would be a good way to actually pass the objects through to the API?

I'm writing the solution in C# although it's the general architecture that matters not the specific implementation


答:
The actual role of your QA is close to acceptance testing. I would imagine this to be done by a separate team, which acts more as product owner rather than a part of the development team.

Example: during a sprint, you need to add a feature which enables to filter search results by different criteria. You already have your search mechanism implemented, but the results are ordered alphabetically.

During the sprint:

The team drafts the design of the new feature and the impact on the actual code base.

Developers write unit tests which ensure that the ordering is working as expected, and at the same time writes the actual code.

The new feature is thoughtfully tested to ensure that it doesn't break anything (regression testing) and that it is working as expected (unit tests).

If possible and appropriate, which is not the case in most projects, a product owner (and so your QA team) can constantly evaluate the new feature in order to prevent the team going in the wrong direction. This requires continuous integration with dozens of builds every day.

After the sprint, the product owner evaluates the new feature to check that it corresponds to the needs of the customer. Your QA team is actually here, after the sprint ended.

I believe your actual issues are the following:

Scope. A sprint concerns your team, and your team only, not your actual QA which acts more as a product owner.

Testing. The fact that you have a QA team doesn't mean that all you need to do is to write code. The job of your team is to deliver a feature which works as expected, not to throw out code for the others to test. If you rely on QA team to do the testing for you, this will increase the overall cost, since bugs will be fixed one to two weeks later instead of being fixed nearly instantly.

Title: Coding and testing in the same sprint

Body:
How is testing handled within the same sprint as coding, if all or most of the coding is not done until the end of the sprint? (I'm referring to the "soup-to-nuts" development and testing of a single PBI within a sprint.)

Most of the answers I've seen online involve QA automation, but even that isn't really possible since you generally need a functional UI to record or create automated tests from. I only have storyboards that continue to evolve as I develop features and discover new requirements.

In my case, I am developing a new desktop application. Desktop apps don't generally lend themselves to automated testing very well. I have some automated unit tests, but they are not the manual functional/integration tests that a QA professional would perform.

So, where I'm at now is that my sprint ends tomorrow, I still have coding to finish, and my QA folks have nothing to test yet, and no idea how to test whatever I'd give them without me holding their hands.

I'm sure I'm not the first person to have this dilemma.

In the past, I've done a pipeline: in the current sprint the test team tests the features that have been implemented during the previous sprint. At my current job, the PM refers to this approach as "waterfall", and as such, unacceptable.