问:
Debugging memory corruption
First off, I do realize this is not a perfect Q&A style question with an absolute answer, but I can't think of any wording to make it work better. I don't think there is an absolute solution to this and this is one of the reasons why I'm posting it here instead of Stack Overflow.

Over the last month I have been rewriting a fairly old piece of server code (mmorpg) to be more modern and easier to extend/mod.
I started with the network portion and implemented a 3rd party library (libevent) to handle stuff for me.
With all the re-factoring and code changes I introduced memory corruption somewhere and I have been struggling to find out where it happens.

I can't seem to reliably reproduce it on my dev/test environment, even when implementing primitive bots to simulate some load I don't get crashes any more (I fixed a libevent issue which caused some stuff)

I have tried so far:

Valgrinding the hell out of it - No invalid writes until the thing crashes (which may take 1+ day in production.. or just an hour) which is really baffling me, surely at some point it would access invalid memory and not overwrite stuff by chance? (Is there a way to "spread out" the address range?)

Code-Analysis tools, namely coverity and cppcheck. While they did point out some.. nastiness and edge cases in the code there was nothing serious.

Recording the process until it crashes with gdb (via undodb) and then working my way backwards. This /sounds/ like it should be doable, but I either end up crashing gdb by using the auto-complete feature or I end up in some internal libevent structure where I get lost since there's too many possible branches (one corruption causing another and so on).
I guess it would be nice if I could see what a pointer originally belongs to/where it was allocated, that would eliminate most of the branching-issues. I cant run valgrind with undodb though, and I the normal gdb record is unusably slow (if that even works in combination with valgrind).

Code review! By myself (thoroughly) and having some friends look over my code, though I doubt it was thorough enough. I was thinking about maybe hiring a dev to do some code review/debugging with me, but I cant afford to put too much money in it and I wouldn't know where to look for someone who'd be willing to work for little-to-no money if he doesn't find the issue or anyone qualified at all.

I should also note: I usually get consistent backtraces.
There are a few places where the crash happens, mostly related to the socket class becoming corrupted somehow. Be it an invalid pointer pointing to something which isn't a socket or the socket class itself becoming overwritten (partially?) with gibberish. Although I suspect it's crashing there the most since that's one of the mostly used parts, so it's the first corrupted memory which gets used.

All in all this issue has had me busy for nearly 2 month (on and off, more of a hobby project) and is really frustrating me to the point where I become grumpy IRL and think about just giving up.
I just can't think about what else I am supposed to do to find the issue.

Are there any useful techniques I missed?
How do you  deal with that? (It can't be that common since there isn't much information about this.. or I'm just really blind?)

Edit:

Some specs in case it matters:

Using c++(11) via gcc 4.7 (version supplied by debian wheezy)

The codebase is around 150k lines

Edit in response to david.pfx post: (sorry for the slow response)

Are you keeping careful records of crashes, to look for patterns?

Yes, I still have dumps of the recent crashes lying around

Are the few places really similar? In what way?

Well, in the most recent version (they seem to change whenever I add/remove code or change related structures) it would always get caught in an item timer method. Basically an item has a specific time after which it expires and it sends updated info to the client.
The invalid socket pointer would be in the (still valid as far as I can tell) Player class, mostly related to that.
I am also experiencing loads of crashes in the cleanup phase, after the normal shutdown where it's destroying all the static classes that haven't been explicitly destroyed (__run_exit_handlers in the backtrace). Mostly involving std::map of one class, guessing that's just the first thing that comes up though.

What does the corrupt data look like? Zeros? Ascii? Patterns?

I haven't found any patterns yet, seems somewhat random to me. It's hard to tell since I don't know where the corruption started.

Is it heap-related?

It's entirely heap-related (I enabled gcc's stack guard and that didn't catch anything).

Does the corruption happen after a free()?

You're going to have to elaborate a bit on that one.
Do you mean having pointers of already free'd objects lying around?
I'm setting every reference to null once the object gets destroyed, so unless I missed something somewhere, no. That should show up in valgrind though which it didn't.

Is there something distinctive about the network traffic (buffer size, recovery cycle)?

The network traffic consists of raw data. So char arrays, (u)intX_t or packed (to remove padding) structs for more complex things, each packet has a header consisting of an id and the packet size itself which is validated against the expected size.
They are around 10-60bytes with the biggest (internal 'bootup' packet, fired once at startup) having a size of a few Mb.

Lots and lots of production asserts. Crash early and predictably before the damage propagates.

I once had a crash related to std::map corruption, each entity has a map of it's "view", each entity that can see it and vice versa is in that.
I added a 200byte buffer in front and after, filled it with 0x33 and checked it before each access. The corruption just magically vanished, I must've moved something around which made it corrupt something else.

Strategic logging, so you know accurately what was happening just before. Add to the logging as you get closer to an answer.

It works.. to an extend.

In desperation, can you save state and auto-restart? I can think of a few pieces of production software that do that.

I somewhat do that. The software consists of a main "cache" process and some other worker ones which all access the cache to get and save stuff. So per crash I don't lose much progress, it still disconnects all the users and so on, it's definitely not a solution.

Concurrency: threading, race conditions, etc

There's a mysql thread to do "async" queries, that's all untouched though and only shares information to the database class via functions with all lock.

Interrupts

There's an interrupt timer to prevent it from locking up that just aborts if it didn't complete a cycle for 30 seconds, that code should be safe though:

```<code>if (!tics) {
    abort();
} else
    tics = 0;
</code>```

tics is volatile int tics = 0; which is increased each time a cycle is completed. Old code too.

events/callbacks/exceptions: corrupting state or the stack unpredictably

Lots of callbacks are being used (async network I/O, timers), but they shouldn't do anything bad.

Unusual data: unusual input data/timing/state

I've had a few edge cases related to that. Disconnecting a socket while packets are still being processed resulted in accessing a nullptr and such, but those have been easy to spot so far since every reference gets cleaned up right after telling the class itself it's done. (Destruction itself is handled by a loop deleting all the destroyed objects each cycle)

Dependency on an asynchronous external process.

Care to elaborate? This is somewhat the case, the cache process mentioned above.
Only thing I could imagine off the top of my head would be it not finishing quick enough and using garbage data, but that's not the case since that's using network too. Same packet model.

答:
To paraphrase what you say in your question, it is not possible to give you a definitive answer. The best we can do is make suggestions of things to look for and tools and techniques.

Some suggestions will appear naive, other may seam more applicable, but hopefully one triggers a thought you can follow up. I must say that the answer by david.pfx has sound advice and suggestions.

From the symptoms

to me it sounds like a buffer overrun.

a related problem is using unvalidated socket data as a subscript or key, etc.

is it possible that you are using a global variable someplace, or have a global and local with the same name, or somehow one player's data interferes with another?

As with many bugs, you are probably making an invalid assumption somewhere. Or possibly more than one. Multiple interacting errors are hard to detect.

Does every variable have a description? And can you define a validity assertion?

If not add those, scan through the code to see that each variable appears to used correctly. Add that assertion wherever it makes sense.

The suggestion to add lots assertion is a good one: the first place to put them is on every function entry point. Validate the arguments and any relevant global state.

I use lots of logging for debugging long-running / asynchronous / real-time codes.

Again, insert a log write on every function call.

If the log files get too large, the logging functions can wrap / switch files / etc.

It's most useful if the log messages indent with the function call depth.

Log file can show how a bug propagates. Useful when one piece of code does something not-quite-right that acts as a delayed action bomb.

Many people have their own home grown logging code. I have an old C macro log system somewhere, and maybe a C++ version ...

Title: Debugging memory corruption


问:
Sense of unit tests without TDD
We have new (quite big) project starting, that we planned to develop using TDD.

The idea of TDD failed (many business and non-business reasons), but right now we have a conversation - should we write unit tests anyway, or not.
My friend says that there is no (or close to zero) sense in writing unit tests without TDD, we should focus only on integration tests. I believe the opposite, that there is still some sense in writing plain unit tests, just to make code more futureproof. What do you think?

Added:
I think that this is not a duplicate of >>this question<< - I understand difference between UT and TDD. My question is not about differences, but about sense of writing Unit Tests without TDD.

答:
Often the quality of tests depends on their provenance. I'm regularly guilty of not doing 'real' TDD - I write some code to prove that the strategy I'd like to use actually works, then cover each case that code is meant to support with tests afterward. Usually the unit of code is a class, to give you a general idea of how much work I'll happily do without test coverage - that is to say, not a large amount. What this means is that the semantic meaning of the tests matches up nicely with the system under test in its 'finished' state - because I wrote them knowing what cases the SUT fulfils and how it fulfils them.

Conversely, TDD with its policy of aggressive refactoring tends to obsolete tests at least as fast as you can write them as the public interface of the system under test changes. I personally find the mental workload of both designing the functional units of the application and keeping the semantics of the tests that cover it in sync to be too high to maintain my concentration and the test maintenance frequently slips. The codebase ends up with tests hanging around that don't test anything of value or are just plain wrong. If you have the discipline and mental capacity to keep the test suite up to date, by all means, practice TDD as rigorously as you'd like. I don't, so I've found it less successful for that reason.

ContentLicense: CC BY-SA 3.0

Title: Sense of unit tests without TDD


答:
Actually Uncle Bob mentioned a very interesting point in one of his Clean Coders videos. He said that the Red-Green-Refactor cycle can be applied in 2 ways.

1st is the conventional TDD way. Write a failing test then make the test pass and finally refactor.

2nd way is to write a very small piece of production code and immediately follow that by its unit test then refactor.

The idea is to go in very small steps. Of course you lose the verification from the production code that your test went from red to green, but in some cases where I was working mostly with junior developers who refused even to try to understand TDD it proved to be somewhat effective.

Again I repeat (and this was emphasized by Uncle Bob) the idea is to go in very small steps and to immediately test the piece of production code that was just added.

Title: Sense of unit tests without TDD

Body:
We have new (quite big) project starting, that we planned to develop using TDD.

The idea of TDD failed (many business and non-business reasons), but right now we have a conversation - should we write unit tests anyway, or not.
My friend says that there is no (or close to zero) sense in writing unit tests without TDD, we should focus only on integration tests. I believe the opposite, that there is still some sense in writing plain unit tests, just to make code more futureproof. What do you think?

Added:
I think that this is not a duplicate of >>this question<< - I understand difference between UT and TDD. My question is not about differences, but about sense of writing Unit Tests without TDD.


问:
Distributing GPL licensed software that optionally links to non-GPL licensed libraries
I can find lots of information regarding the restrictions imposed on linking GPL licensed libraries from non-GPL licensed software. However, I'd like to do the opposite - I'd like to distribute some software under a GPLv3 license that has the option to dynamically link to a proprietary shared library.

The proprietary library would not constitute an essential component of the functioning software, since there are other open source (BSD licensed) libraries that could be linked at runtime instead (specifically, I'm thinking about linking MKL BLAS versus ATLAS, OpenBLAS etc.).

I found this relevant section in the GPL FAQ.

Does this mean that I would need to explicitly grant an exception for every possible non-GPL licensed shared library the user might choose to link to at run-time?

Would I need to write exceptions for libraries that are open source, but not GPL licensed (e.g. BSD)?

If the library that is being linked to is not covered by an exception (i.e. if it is GPL or LGPL), what would be my obligation to distribute the source code for that library?

答:
As BLAS is effectively an API with multiple (both free and non-free) implementations, it is possible that it falls under the GPL definition of a "Standard Interface". In that case, the GPL requirements don't extend to the BLAS library that you happen to distribute with your application.

It might be worth the effort to ask the FSF if they consider the BLAS API to fall under that definition.

If that route doesn't work out, you can give explicit permission to use a particular library (or set of libraries) as indicated by the GPL FAQ section you linked to:

[...] If you're using GPLv3, you can accomplish this goal by granting an additional permission under section 7. The following license notice will do that. You must replace all the text in brackets with text that is appropriate for your program. If not everybody can distribute source for the libraries you intend to link with, you should remove the text in braces; otherwise, just remove the braces themselves.

Copyright (C) [years] [name of copyright holder]

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, see http://www.gnu.org/licenses.

Additional permission under GNU GPL version 3 section 7

If you modify this Program, or any covered work, by linking or combining it with [name of library] (or a modified version of that library), containing parts covered by the terms of [name of library's license], the licensors of this Program grant you additional permission to convey the resulting work. {Corresponding Source for a non-source form of such a combination shall include the source code for the parts of [name of library] used as well as that of the covered work.}

This explicit permission is only needed for libraries whose license is otherwise incompatible with the GPL. Libraries licensed under a permissive license, like BSD or MIT are compatible with GPL programs, so you don't need to exempt them.

If your program is linked to a library that has an incompatible license and isn't exempted, then you are not allowed to distribute that combination. Neither in source, nor in binary form.


问:
How should I design the class for a database entity?
I'm studying OOP PHP and have watched two tutorials that implement user login\registration system as an example. But implementation varies. Which way will be more correct one to work with data such as this?

Load all data retrieved from database as array into a property called something like _data on class creation and further methods operate with this property

Create separate properties for each field retrieved from database, on class creation load all data fields into respective properties and operate with that properties separately?

Then let's say I want to create a method that returns a list of all users with their data. Which way is better?

Method that returns just an array of userdata like this:

```<code>Array([0]=>array([id] => 1, [username] => 'John', ...),
      [1]=>array([id] => 2, [username] => 'Jack', ...),
      ...)
</code>```

Method that creates a new instance of it's class for each user and returns an array of objects

答:
"Then let's say I want to create a method that returns a list of all users with their data. Which way is better?"

Arrays:
These will be fast, easy, and simple. If you have a few elements in each array, things will be easy to track and focus on. e.g. You'll know that index 0 has the user id, index 1 has the name etc. There is a reason why zend framework 2 uses arrays for configuration rather than large objects. For basic needs this is the way to go.

Objects:
These will consume more memory and ultimately be (a little-bit) slower. However, type hinting if you need it, and fancy names given to each piece of data is your boon.

The "Best" method depends on your needs. I'd probably go with option 1 most of the time as PHP is a slow language and I like things to be fast. For more complicated systems, I'd use doctrine.

ContentLicense: CC BY-SA 4.0


问:
Why is cleverness considered harmful in programming by some people?
I've noticed a lot of questions lately relating to different abstraction techniques, and answers saying basically that the techniques in question are "too clever." I would think that part of our jobs as programmers is to determine the best solutions to the problems we are given to solve, and cleverness is helpful in doing that.

So my question is: are the people who think certain abstraction techniques are too clever opposed to cleverness per se, or is there some other reason for the objection?

EDIT: This parser combinator is an example of what I would consider to be clever code. I downloaded this and looked it over for about half an hour. Then I stepped through the macro expansion on paper and saw the light. Now that I understand it, it seems much more elegant than the Haskell parser combinator.

答:
Fools ignore complexity; pragmatists suffer it; experts avoid it; geniuses remove it.
-- Alan Perlis</p>

ContentLicense: CC BY-SA 2.5


答:
Whether it be unit, component, integration or acceptance tests, the important part is that it must be automated. Not having automated tests is a fatal mistake for any kind of software, from the simple CRUDs to the most complex calculations.
The reasoning is that writing the automated tests will always cost less than the continuous need for running all tests manually when you don't, by orders of magnitude. After you have them written, you just have to push a button to see if they pass or fail. Manual tests will always take a long time to run, and depend on humans (living creatures who get bored, may lack attention and so on) to be able to check if the tests pass or fail.
In short, always write automated tests.

Now, about the reason why your colleague might be against doing any kind of unit testing without TDD: It's probably because it is harder to trust tests written after the production code. And if you can't trust your automated tests, they are worth nothing.
Following the TDD cycle, you must first make a test fail (for the right reason) to be allowed to write the production code to make it pass (and no more). This process is essentially testing your tests, so you can trust them. Not to mention the fact that writing tests before the actual code pushes you to design your units and components to be more easily testable (high levels of decoupling, SRP applied, etc...). Although, of course, doing TDD takes discipline.

Instead, if you write all the production code first, when you write the tests for it, you'll expect them to pass at first run. This is very problematic, because you may have created a test that covers 100% of your production code, without asserting the correct behavior (may not even perform any assertions! I've seen this happen), since you can't see it failing first to check if it is failing for the right reason.
Thus, you may have false-positives. False-positives will eventually break the trust in your test suite, essentially forcing people to resort to manual testing again, so you have the cost of both processes (writing tests + manual tests).

This means you must find another way to test your tests, like TDD does. So you resort to debugging, commenting parts of the production code, etc., in order to be able to trust the tests. The problem is that the process of "testing your tests" is much slower this way. Adding this time to the time that you'll spend running ad-hoc tests manually (because you don't have automatic tests while you are coding the production code), in my experience, results in an overall process that is much slower than practicing TDD "by the book" (Kent Beck - TDD by Example). Also, I'm willing to make a bet here and say that truly "testing your tests" after they were written takes much more discipline than TDD.

So maybe your team can reconsider the "business and non-business reasons" for not doing TDD. In my experience, people tend to think TDD is slower compared to just writing unit tests after the code is done. This assumption is flawed, as you have read above.

Title: Sense of unit tests without TDD


问:
On restricting commercial use for Mathematica / Matlab projects while requiring attribution
I want to share the code of a research project, for academic purposes, in Bitbucket and Matlab's FileExchange.

The code is in both Matlab and Mathematica, and I want to restrict its use for commercial purposes. I don't know if I can use Affero GPL for this, as both Matlab and Mathematica are commercial software.

This is the most direct response from the terms of GPLv3 that I've found:

If a programming language interpreter has a license that is incompatible with the GPL, can I run GPL-covered programs on it? (#InterpreterIncompat)

When the interpreter just interprets a language, the answer is yes. The interpreted program, to the interpreter, is just data; the GPL doesn't restrict what tools you process the program with.

However, when the interpreter is extended to provide “bindings” to other facilities (often, but not necessarily, libraries), the interpreted program is effectively linked to the facilities it uses through these bindings. The JNI or Java Native Interface is an example of such a facility; libraries that are accessed in this way are linked dynamically with the Java programs that call them.

So if these facilities are released under a GPL-incompatible license, the situation is like linking in any other way with a GPL-incompatible library. Which implies that:

If you are writing code and releasing it under the GPL, you can state an explicit exception giving permission to link it with those GPL-incompatible facilities.

If you wrote and released the program under the GPL, and you designed it specifically to work with those facilities, people can take that as an implicit exception permitting them to link it with those facilities. But if that is what you intend, it is better to say so explicitly.

You can't take someone else's GPL-covered code and use it that way, or add such exceptions to it. Only the copyright holders of that code can add the exception...

What license is compatible with these requirements in Matlab / Mathematica?

Allowing academic usage

Requiring attribution for academic usage

Restricting commercial usage (the program's output -processed images- should not be sold)

答:
First off, there is no open source license that allows you to restrict the area it is used in.  Thus, the desire to use the GPL or AGPL and also have it restricting commercial usage is completely contrary to the definition of open source.

6. No Discrimination Against Fields of Endeavor

The license must not restrict anyone from making use of the program in a specific field of endeavor. For example, it may not restrict the program from being used in a business, or from being used for genetic research.

Rationale: The major intention of this clause is to prohibit license traps that prevent open source from being used commercially. We want commercial users to join our community, not feel excluded from it.

So...

The code is in both Matlab and Mathematica, and I want to restrict its use for commercial purposes. I don't know if I can use Affero GPL for this, as both Matlab and Mathematica are commercial software.

As both the GPL and AGPL are open source licenses, they are incompatible with the "restrict it to academic use."

At this point, you are left to writing your own license.  You may wish to look at How can I create my own software license? and possibly head over to the License Generator which generates a boiler plate license based on various selections that you could then modify.

It should also be pointed out that this license will be incompatible with other open and free software licenses.


问:
Proper Query Object Pattern - NHibernate
I was looking to an alternative to Repository Pattern and I've read about "Query object pattern" (http://lostechies.com/jimmybogard/2012/10/08/favor-query-objects-over-repositories/). However the implementation provided in the link above is not exactly what I was looking for - I look for something that can be easily injected, easily tested and easily extended/changed (only last point holds in the link-implementation). I thought a bit and I think "I have invented" solution. Suppose I have controller BlogEntriesController (i'm talking about mvc asp application with ORM) and I inject to constructor class:

```<code>public class BlogEntriesQueries
{
    IRetrieveQuery<BlogEntryEntity> PageBlogEntries { get; set; }
    IRetrieveQuery<BlogEntryEntity> ProposedBlogEntries { get; set; }
    IAddQuery<BlogEntryEntity> CreateNewBlogEntry { get; set; }
    ... etc..
}
</code>```

where IRetrieveQuery<T> is smth like:

```<code>public interface IRetrieveQuery<TEntity> where TEntity : EntityBase
     IQueryable<TEntity> CurrentQuery { get; }
     IRetrieveQuery<TEntity> InvokeQuery(IRetrieveQuery<TEntity> subQuery);
     IEnumerable<TEntity> GetQueryResults();
}
</code>```

Then all the work I'd have to do is proper configuration of IoC container to inject queries into controller and proper calls in controller like: _blogQueries.ProposedBlogEntries.GetQueryResults() The question is - it is good solution - are there any drawbacks that I don't see? I think i'd get testability (don't even have to mock ISession in case of NHibernate) and it'd be easily extensible but I'm not an expert (especially at database applications) by any means.

答:
This is a correct implementation , you are going in right direction and will have no side effects. You might want to write create a integration test to ensure that query api is designed as per you need. </p>

<p>Keep it simple as much you can. Dont bloat it with pattern util you need one. </p>

Body:
This is a correct implementation , you are going in right direction and will have no side effects. You might want to write create a integration test to ensure that query api is designed as per you need.

Keep it simple as much you can. Dont bloat it with pattern util you need one.


答:
Simple solutions are better for long-term maintenance. And it's not always just about language familiarity. A complex line (or lines) takes time to figure out even if you're an expert in the given language. You open up a file and start reading: "ok, simple, simple, got it, yep, WTF?!" Your brain comes to a screeching halt and you now have to stop and decipher a complicated line. Unless there was a measurable, concrete reason for that implementation, it is "too clever".

Figuring out what's going on gets progressively harder as complexity grows from a clever method to a clever class to a clever pattern. Aside from well-known approaches, you have to figure out the thought process that went into creating a "clever" solution, which can be quite difficult.

That said, I hate avoiding a pattern (when its use is justified) just because someone might not understand it. It's up to us as developers to keep learning and if we don't understand something, it's a reason to learn it, not to avoid it.

Title: Why is cleverness considered harmful in programming by some people?

Body:
I've noticed a lot of questions lately relating to different abstraction techniques, and answers saying basically that the techniques in question are "too clever." I would think that part of our jobs as programmers is to determine the best solutions to the problems we are given to solve, and cleverness is helpful in doing that.

So my question is: are the people who think certain abstraction techniques are too clever opposed to cleverness per se, or is there some other reason for the objection?

EDIT: This parser combinator is an example of what I would consider to be clever code. I downloaded this and looked it over for about half an hour. Then I stepped through the macro expansion on paper and saw the light. Now that I understand it, it seems much more elegant than the Haskell parser combinator.


答:
Query testing can be handled easily without need to mock them. You just need to approach it from slightly different view. Example is here.

Second thing is: Do you really need ability to inject queries? Queries are usually closely tied to business logic they are used to. Are you really going to gain something by complicating your life by abstracting away each query? Every time you create some design, make sure you actually write significant amount of code, which uses this design, so you can evaluate how complex it is to actually use the design.

Final practical note : Are you sure your queries always work on single entity? Or return said entity? Queries are usually quite complex. You can just use LINQ directly if you don't need complex queries.

One more thing I noticed : Are you reusing single query across all calls? Is that really a query? Looks like different form of repository. How are you going to pass parameters for query?

Title: Proper Query Object Pattern - NHibernate

Body:
I was looking to an alternative to Repository Pattern and I've read about "Query object pattern" (http://lostechies.com/jimmybogard/2012/10/08/favor-query-objects-over-repositories/). However the implementation provided in the link above is not exactly what I was looking for - I look for something that can be easily injected, easily tested and easily extended/changed (only last point holds in the link-implementation). I thought a bit and I think "I have invented" solution. Suppose I have controller BlogEntriesController (i'm talking about mvc asp application with ORM) and I inject to constructor class:

```<code>public class BlogEntriesQueries
{
    IRetrieveQuery<BlogEntryEntity> PageBlogEntries { get; set; }
    IRetrieveQuery<BlogEntryEntity> ProposedBlogEntries { get; set; }
    IAddQuery<BlogEntryEntity> CreateNewBlogEntry { get; set; }
    ... etc..
}
</code>```

where IRetrieveQuery<T> is smth like:

```<code>public interface IRetrieveQuery<TEntity> where TEntity : EntityBase
     IQueryable<TEntity> CurrentQuery { get; }
     IRetrieveQuery<TEntity> InvokeQuery(IRetrieveQuery<TEntity> subQuery);
     IEnumerable<TEntity> GetQueryResults();
}
</code>```

Then all the work I'd have to do is proper configuration of IoC container to inject queries into controller and proper calls in controller like: _blogQueries.ProposedBlogEntries.GetQueryResults() The question is - it is good solution - are there any drawbacks that I don't see? I think i'd get testability (don't even have to mock ISession in case of NHibernate) and it'd be easily extensible but I'm not an expert (especially at database applications) by any means.


问:
Best practices for geolocation
We are writing a flight search engine. We want to pre-fill the departure airport for mobile users with the closest one to their location. To do that, our plan is to

Find a list of airports with their latitude/longitude.

Find the user geolocation using something like HTML5 Geolocation (which asks the user for permission).

Calculate the distance between the user's location and every airport to find the closest one.

Fill the departure form.

Is this a standard way of proceeding? I am a junior programmer and I am not used to this kind of problem. Is there any obstacle I should bear in mind while developing my solution? I have the feeling the algorithm to calculate the distance between one point and 300 locations might get a bit heavy.

答:
We are writing a flight search engine.

Use a search engine!

You can use Apache Solr for example, and your #3 step is done. No need to know the details of the calculation. The basic steps needed is to store the geocodes to be searched on (the airports') then do a search using user's geocode maybe a maximum distance for results to be included.

Don't worry about being a junior, this is how seniors do it, i.e. look for existing solutions so they don't have to solve it themselves. It might take some time to configure it, but the end result is production quality.

Title: Best practices for geolocation


问:
Creating fast, realtime dialogue search for television scripts
We have a database of television scripts and we'd like to search it, getting results as we type.

We often remember words or snippets of dialogue but can't remember exactly what was said or what episode it was said in:  "What was the episode where George goes against his instincts and walks up to the girl at the diner and tells her he's unemployed?"

With this tool you'd type "George unemployed" and the result with context (and possibly other hits) would pop up:

```<code>EPISODE: "The Opposite"

George : Excuse me, I couldn't help but notice that you were looking in my direction.
Victoria : Oh, yes I was, you just ordered the same exact lunch as me.
( G takes a deep breath )
George : My name is George. I'm unemployed and I live with my parents.
Victoria : I'm Victoria. Hi.
</code>```

My question is about the best way to design a data structure to implement this kind of search?

My idea so far is store a lookup table in an in-memory data store such as redis.  To create the table, first I'd give each script (episode) an id, and then i'd parse the script into lines based on the person speaking.  So a particular piece of dialogue could be located with an episode id paired with a line id.

Next I would process each line, creating a data structure indexed by all words appearing in any script, and storing the episode/line location of every instance the word was used.

```<code>apple -> [
           { scriptid: 9, lines: [11,99] },
           { scriptid: 21, lines: [103,211,214] }
         ]
orange -> [
            { scriptid: 2, lines: [101] }
          ]
</code>```

We could pair this data structure with a simple scoring algorithm, so that when a user searched for multiple words, we'd only show matches where both words occured in the same script, and give higher rank to locations where the words appear closer to one another.

There are other details that, ideally, would be accounted for, such as matching plurals and mispelled words.

Is this a reasonable approach?  How could it be improved?  What other details should I consider?

答:
What type of database are you using? and what programming language?
I personally, using a PHP and MySQL setup, would contain the scripts as an array by first splitting them and inserting into the table.
I would then split the user's search into an array too by spaces.
Then, I would use the query
        SELECT *
        FROM 'table'
        WHERE 'column'.contains('array[0], array[1]')

Hopefully this makes sense and provides a viable answer, and if not I hope it gives you a 'starting point' as such :)


答: I would use a search engine, e.g. ElasticSearch, which already has everything you need. It takes care data structure, storage, etc. so you can focus on what filtering the search and returning the results, i.e. drop some words (propositions, etc.), fix misspelled words, give boost to some results, etc. Each of those problem would need a separate discussion on how to solve it. But the default configuration should be enough to get you started.</p>

Title: Creating fast, realtime dialogue search for television scripts

Body:
We have a database of television scripts and we'd like to search it, getting results as we type.

We often remember words or snippets of dialogue but can't remember exactly what was said or what episode it was said in:  "What was the episode where George goes against his instincts and walks up to the girl at the diner and tells her he's unemployed?"

With this tool you'd type "George unemployed" and the result with context (and possibly other hits) would pop up:

```<code>EPISODE: "The Opposite"

George : Excuse me, I couldn't help but notice that you were looking in my direction.
Victoria : Oh, yes I was, you just ordered the same exact lunch as me.
( G takes a deep breath )
George : My name is George. I'm unemployed and I live with my parents.
Victoria : I'm Victoria. Hi.
</code>```

My question is about the best way to design a data structure to implement this kind of search?

My idea so far is store a lookup table in an in-memory data store such as redis.  To create the table, first I'd give each script (episode) an id, and then i'd parse the script into lines based on the person speaking.  So a particular piece of dialogue could be located with an episode id paired with a line id.

Next I would process each line, creating a data structure indexed by all words appearing in any script, and storing the episode/line location of every instance the word was used.

```<code>apple -> [
           { scriptid: 9, lines: [11,99] },
           { scriptid: 21, lines: [103,211,214] }
         ]
orange -> [
            { scriptid: 2, lines: [101] }
          ]
</code>```

We could pair this data structure with a simple scoring algorithm, so that when a user searched for multiple words, we'd only show matches where both words occured in the same script, and give higher rank to locations where the words appear closer to one another.

There are other details that, ideally, would be accounted for, such as matching plurals and mispelled words.

Is this a reasonable approach?  How could it be improved?  What other details should I consider?


问:
C/C++: Which conversion warnings make sense in practice?
C/C++ implicit conversions can cause bugs. Here's one example:

```<code>int foo, bar;
scanf("%d", &foo);
scanf("%d", &bar);
float foobar = foo / bar;
</code>```

If I input 7 and 2, it's not 3.5 as expected - it's 3 -> bug (let's ignore the buffer overflow).

gcc's -Wconversion warns about this kind of stuff, so I turned it on. But it just seems to me like it's going over the top with the warnings. For instance, this:

```<code>int foo;
float bar;
scanf("%d", &foo);
scanf("%f", &bar);
float foobar = foo / bar;
</code>```

Causes a warning:

```<code>warning: conversion to 'float' from 'int' may alter its value [-Wconversion]
</code>```

Even though it works as intended, returning 3.5 when I input 7 and 2. I know that float cannot represent all possible int values precisely, but I'm questioning if adding a cast here does anything to help - other than adding more code. In the example above, casting one of the int values in the expression to float actually changed the result, replacing an implicit cast by an explicit one in this example doesn't.

So much for float to int conversion warnings, but that's not all. There's also sign conversion warnings. Traditional example:

```<code>std::vector<char> chars = get_chars();
for (int i = 0; i < chars.size(); i++)
    std::cout << chars[i] << std::endl;
</code>```

Causes this warning:

```<code>warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
</code>```

I could use the more cumbersome unsigned int for the index variable instead - which will require further casting when I do calculations with it. All for the unusual situation that I have between 2^31 + 1 and 2^32 elements in my vector and don't do any calculations with the index variable that cast it to int.

So, my question is: Which conversion warnings make sense in practice? Does adding explicit casts to fix them really improve anything?

答: With GCC you'll find alot of the important warnings are already grouped together under -Wall and -Wextra, its also useful to use -Werror to turn warnings into errors.


答:
There is no open source licence that can do that. What you are describing is:

a commercial licence

that happens to be free for academic (and personal?) use, but with attribution obligations

that includes source code

with redistribution rights, which may be of binaries, source and/or modified versions

with sub-licensing, to cover redistribution, which may or may not include 'copyleft'.

That's a pretty complicated licence. Are you sure you're up for it?

You might do better to pick a GPL (no exceptions) licence to start with, relying on the general distaste for such licences in the commercial sector. You can add an academic attribution clause quite easily without intruding on the other terms.

Then down the track you can dual licence it with whatever commercial terms you want and/or stop releasing updates for the GPL version. If you plan to do that, you need to keep tight control over the copyright of the original and any modified versions.

Title: On restricting commercial use for Mathematica / Matlab projects while requiring attribution

Body:
I want to share the code of a research project, for academic purposes, in Bitbucket and Matlab's FileExchange.

The code is in both Matlab and Mathematica, and I want to restrict its use for commercial purposes. I don't know if I can use Affero GPL for this, as both Matlab and Mathematica are commercial software.

This is the most direct response from the terms of GPLv3 that I've found:

If a programming language interpreter has a license that is incompatible with the GPL, can I run GPL-covered programs on it? (#InterpreterIncompat)

When the interpreter just interprets a language, the answer is yes. The interpreted program, to the interpreter, is just data; the GPL doesn't restrict what tools you process the program with.

However, when the interpreter is extended to provide “bindings” to other facilities (often, but not necessarily, libraries), the interpreted program is effectively linked to the facilities it uses through these bindings. The JNI or Java Native Interface is an example of such a facility; libraries that are accessed in this way are linked dynamically with the Java programs that call them.

So if these facilities are released under a GPL-incompatible license, the situation is like linking in any other way with a GPL-incompatible library. Which implies that:

If you are writing code and releasing it under the GPL, you can state an explicit exception giving permission to link it with those GPL-incompatible facilities.

If you wrote and released the program under the GPL, and you designed it specifically to work with those facilities, people can take that as an implicit exception permitting them to link it with those facilities. But if that is what you intend, it is better to say so explicitly.

You can't take someone else's GPL-covered code and use it that way, or add such exceptions to it. Only the copyright holders of that code can add the exception...

What license is compatible with these requirements in Matlab / Mathematica?

Allowing academic usage

Requiring attribution for academic usage

Restricting commercial usage (the program's output -processed images- should not be sold)


答:
It goes kind of like this.

The ability to use unsigned values and variables and real numbers of different precisions
are not particularly common features in programming languages, but are critical in order for C/C++ to do its job.

Mixing arithmetic types like these is a major cause of bugs in real, commercial C/C++ software. Trust me, I've seen plenty.

So the C/C++ standards nail down conversion behaviour with great care. There are pages and pages on this stuff.

Therefore we ask the compiler to do as much as it possibly can to help us find and avoid bugs in the conversion world. Some things are errors (contravene the standard); others are warnings (default is associated with possible or likely bugs).

Now it is up to us to identify programming strategies that minimise the risk of bugs, and in particular that do not trigger warnings. That's our job.

Casts are bad. Every cast potentially papers over a bug or a warning or a design issue. We can't live without them, but the fewer the better. Most are avoidable, with proper design.

Your two examples all look like wrong code to me. In the first two examples you need to tell the compiler whether to use int, float or double arithmetic, as the defaults could easily be incorrect. The third example must use an unsigned loop variable. C/C++ is a very sharp knife and you need more care to avoid cutting yourself.

Title: C/C++: Which conversion warnings make sense in practice?

Body:
C/C++ implicit conversions can cause bugs. Here's one example:

```<code>int foo, bar;
scanf("%d", &foo);
scanf("%d", &bar);
float foobar = foo / bar;
</code>```

If I input 7 and 2, it's not 3.5 as expected - it's 3 -> bug (let's ignore the buffer overflow).

gcc's -Wconversion warns about this kind of stuff, so I turned it on. But it just seems to me like it's going over the top with the warnings. For instance, this:

```<code>int foo;
float bar;
scanf("%d", &foo);
scanf("%f", &bar);
float foobar = foo / bar;
</code>```

Causes a warning:

```<code>warning: conversion to 'float' from 'int' may alter its value [-Wconversion]
</code>```

Even though it works as intended, returning 3.5 when I input 7 and 2. I know that float cannot represent all possible int values precisely, but I'm questioning if adding a cast here does anything to help - other than adding more code. In the example above, casting one of the int values in the expression to float actually changed the result, replacing an implicit cast by an explicit one in this example doesn't.

So much for float to int conversion warnings, but that's not all. There's also sign conversion warnings. Traditional example:

```<code>std::vector<char> chars = get_chars();
for (int i = 0; i < chars.size(); i++)
    std::cout << chars[i] << std::endl;
</code>```

Causes this warning:

```<code>warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
</code>```

I could use the more cumbersome unsigned int for the index variable instead - which will require further casting when I do calculations with it. All for the unusual situation that I have between 2^31 + 1 and 2^32 elements in my vector and don't do any calculations with the index variable that cast it to int.

So, my question is: Which conversion warnings make sense in practice? Does adding explicit casts to fix them really improve anything?


答: cleverness is a tool; by itself it is not harmful. It only becomes harmful in context where it is not necessary.</p>


问:
The nature of callbacks in Node.js
In the plain javaScript world I've learned that nested functions can be problematic because the inner function is "re-created" every time the outer function is called:

```<code>function outer(a) {
    function inner(arg1,arg2){
        return arg1 + arg2;
    }
    return inner(a, 6);
}
myVar = outer(4);
</code>```

Apparently the inner function is garbage collected and the constant re-creation of it has a cost on performance. This could be re-factored as:

```<code>function inner(arg1, arg2) {
    return arg1 + arg2;
}

function outer(a) {
    return inner(a, 6);
}

myVar = outer(4);
</code>```

If I am correct it seems that the tradeoff lies in the changing scope of inner; the fact that function inner no longer has access to outer's own variables. If I am correct so far, lets move to Node.JS. Node is heavy on callbacks, and my question is, are asynchronous callback functions typical in Node also "re-created" every time they are fired? Take the following common Node construct:

```<code>http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
</code>```

In reality the function can get pretty large; it can encompass a lot of functionality. Is the above callback "re-created" every time a client GETs or POSTs the server? If so, would it be better to always de-couple the callbacks as in the plain javaScript example:

```<code>function handleGET(req, res) {
   res.writeHead(200, {'Content-Type': 'text/plain'});
   res.end('Hello World\n');
}

http.createServer(handleGET).listen(1337, '127.0.0.1');
</code>```

as a related bonus question, are event functions re-created too? Would they benefit from prior  declaration?

```<code>myObj.on('someEvent', function someFunction() {...});
</code>```

vs.

```<code>function someFunction() {...}
myObj.on('someEvent', someFunction);
</code>```

答:
Node.JS doesn't fundamentally change the way Javascript works, and the issue you're describing is a Javascript issue.

More specifically, because the inner function is inside the scope of an outer function, it is recreated every time the inner function enters and leaves the outer scope.  It's exactly the same thing that happens when you declare a variable inside of a function, as opposed to outside of it.  Node.JS has nothing to say about all that.

Fundamentally, callback functions are just functions; they have no special property that changes the way they behave in local scope.

Related

JSlint error 'Don't make functions within a loop.'

Title: The nature of callbacks in Node.js


答:
The -Wconversion flag frequently gives false positives, so it may be a good idea to turn these warnings off unless you really need the rigor.

In most cases, I find -Wsign-conversion to be more useful as unintentional signed-to-unsigned conversions can often cause serious bugs.

It would be nice if GCC provided a separate flag that warns only about non-subtype conversions though (e.g. int to char).


问:
Keep user and user profile in different tables?
I have seen in a couple of projects that developers prefer to keep essential user info in one table (email/login, password hash, screen name) and rest of the non essential user profile in another (creation date, country, etc). By non-essential I mean that this data is needed only occasionally. Obvious benefit is that if you are using ORM querying less fields is obviously good. But then you can have two entities mapped to same table and this will save you from querying stuff you don't need (while being more convenient). Does anybody know any other advantage of keeping these things in two tables?</p>

答:
There are, at least, three cases when having a person table for basic attributes and a second table for other attributes with a one-to-one relationship, is desirable:

BLOB data like a picture. A separate table allows data to be stored separately for performance reasons, for example in a separate tablespace.

Data that doesn't apply to everybody or that only apply to a person playing a certain role. Think of it as columns that would be null in many rows if they were part of the main table. In the database of a clinic, you can have a person table, a patient table and a medic table, in the first one you would have basic attributes, in the second one only attributes pertinent when the person is a patient like insurance coverage, in the third table (when the person is a medic) you could have the medical-specialty and other data that only applies to medical personel. Obviously a medic can be a patient.

A table that materializes a relationship with an entity in a remote system. In this case the table establishes an equivalence between unique identifiers in separate database for interoperability reasons.

I think the case you expose fits within the second case.


问:
Confusion in understanding the theorem on Amortization
I was reading a book on Algorithm Analysis by Micheal T Goodrich. I came across amortization technique and I got struck in understanding the proof of theorem. I am putting the theorem and the proof and the part that I need some more explanation. Please help me:

Theorem: A series of n operations on an initially empty clearable table implemented with an array takes O(n) time

Definition of clearable table: This Abstract data type stores table of elements, which can be accessed by their index in the table. In addition this ADT supports the following two methods:

add(e): Add the element e to the next available cell in the table.

  clear : Empty the table by removing all its elements.

Proof: Let M0....,Mn-1 be the series of operations performed on S, and let Mi0,....,Mik-1 be the clear operations within the series. We then have,

0<= i0 < .... < i k-1 <= n-1.

Let us also define i-1 = -1. The running time of operation Mij (a clear operation) is O(ij - ij-1), because at most ij - ij-1 -1 elements could have been added into the table (using the add operation) since the previous clear operation Mij-1 or since the beginning of the series.

Question:

Why and how i-1 = -1 is defined?

I did not understand "because at most ij - ij-1 -1 elements could have been added into the table (using the add operation) since the previous clear operation Mij-1 or since the beginning of the series". Please explain me more on this statement on why -1 came into the picture.

答:
I'll start with your second question. That -1 comes from the fact, that both ij and ij-1 are clear operations, you want to count only the add operations in between, so you do not have to count the left and right values. (For example there are 3 values between 3 and 7: 4, 5, 6.)

The answer for your first question: because it is before the first element and it works well with the quoted text. (For example assume the first clear is at index 2; between i-1=-1 and i0=2, there are 2 integers: 0 and 1, so the formula of ij-ij-1-1 = 2-(-1)-1=2 still works well.) You could use a smaller number as well, but that would be more confusing.


问:
Updating settings on embedded systems device with non-reliable comms
I am writing a JavaFX GUI program for user configuration of an embedded systems device. The device will be connected by a wired or wireless serial connection which is not 100% reliable. Therefore when the user makes settings changes in the GUI, the program sends a set message to the device, then must verify they are set.

My question is what program structures are useful for this purpose. My current approach is

A class called Sync which contains two variables related to the particular setting. One is bound to the setting as it appears in the GUI and another to the setting as it has been received from the device.

A property in Sync that describes if the two settings are the same, the device is setting is newer or the GUI setting is newer. Field decorations then bind to this setting.

A button called set that sends a set message to the device

A loop that continually polls the device for its settings (which are stored in the Sync class). The decorators then give feedback to the user whether the settings have synced.

I'd like to do something a bit more elegant where the Sync class takes care of sending messages and checking if the setting were set.

Some specific questions:

Should I remove the 'set' button and have the Sync class update the device whenever it has valid settings?

Is a decoration enough to indicate whether or not the setting has been set? Would a dialog box be better?

Some general questions:

Is there a best practice approach to this problem?

Are there common classes that would be useful?

Any other alternative designs?

Edit:

A constraint: I'd like to keep the device as dumb as possible. I've implemented custom protocols with CRC, ack/nack, retries, message buffers, etc. before, albeit between two embedded systems. The software will be modified for different beginner Arduino projects and I want to keep most of the logic (even validation) on the GUI side.

答:
The usual approach for communicating over a protocol that can introduce errors into the messages or drop messages altogether involves

Adding checksums to the messages to allow the received to verify message integrity

The sending of positive or negative acknowledge messages to inform the sender that a message has respectively been successfully received or has been missed. Positive acknowledgement is usually easier to implement.

A retry mechanism to re-send messages that have not been acknowledges within a reasonable time.

With these facilities in the communication protocol, the higher level components in both sides can regard the communication as reliable and don't need to take special measures to ensure that the message was received.


问:
Should we encapsulate everything in a try{} block in a Try object?
Why can't I make a class for a Try including what I try and then run that in the try {} block? Why is it impractical?

```<code>class DBConnectTry extends Try {
  TryResponse response[] attempt(TryObject o[]...){
     //try to connect to database
  }
}
</code>```

Then in a try ... catch I could invoke only the DBConnectTry.attempt. Would that improve readability / clarity / modularization or is it not a good idea?

答:
I would say this is not a good idea. This kind of encapsulation is not a standard practice for exceptions, because exceptions should be... exceptionally rare to begin with. Code of this variety looks good on paper, but in practice it will be a nightmare to maintain. Write only enough code to get the problem solved. See the KISS principle, and don't repeat yourself.

Redundant code does not help code clarity, or readability, and adding extra classes and methods for small tasks will only make your application source bloat.

Also, consider that it will be more difficult to remove such classes later (if need arises) than it is to implement them in the first place.

Consider using the throws keyword to propagate exceptions to a calling class, and then handle them there. Instead of an interface Try, with implementing class DBConnectTry, create a method in your impl database connection class that has one try catch block for each connection.

```<code>public class DBConnectionManager{
    public Connection getConnection() throws SQLException {
        Connection conn = null;
        conn = //Get connection obj from DriverManager or whatever
        return conn;
    }
}
</code>```

Every call to DBConnectionManager#getConnection should handle SQLExceptions, or pass them up to the next calling class.

Also, look into logging frameworks like log4j and slf4j. You do not need a class to encapsulate Exception messages (your TryResponse class). You can bin multiple messages into simple arrays/lists of String objects.

Body:
I would say this is not a good idea. This kind of encapsulation is not a standard practice for exceptions, because exceptions should be... exceptionally rare to begin with. Code of this variety looks good on paper, but in practice it will be a nightmare to maintain. Write only enough code to get the problem solved. See the KISS principle, and don't repeat yourself.

Redundant code does not help code clarity, or readability, and adding extra classes and methods for small tasks will only make your application source bloat.

Also, consider that it will be more difficult to remove such classes later (if need arises) than it is to implement them in the first place.

Consider using the throws keyword to propagate exceptions to a calling class, and then handle them there. Instead of an interface Try, with implementing class DBConnectTry, create a method in your impl database connection class that has one try catch block for each connection.

public class DBConnectionManager{
    public Connection getConnection() throws SQLException {
        Connection conn = null;
        conn = //Get connection obj from DriverManager or whatever
        return conn;
    }
}

Every call to DBConnectionManager#getConnection should handle SQLExceptions, or pass them up to the next calling class.

Also, look into logging frameworks like log4j and slf4j. You do not need a class to encapsulate Exception messages (your TryResponse class). You can bin multiple messages into simple arrays/lists of String objects.


答: Performant, maintainable, timely and cheap are the ways I measure a solution.  I guess clever could also be part of a solution as long as it doesn't negatively affect those qualities.</p>


问:
Refactoring Tomcat webapp deployments
Background

We have several webapps running on Tomcat 7.0.x (on Linux). For historical reasons, we have populated the "Common" lib (i.e. $CATALINA_HOME/lib) with many application jars. These jars include Spring, Jackson, and our own persistence-layer jars.

As we deploy new persistence-layer jars, we are embroiled in a classpath hell: some jars are OK in ~/webapps/foo/WEB-INF/lib, but some must be excluded and placed in Common. Recent adventures show that some may have to be in both places.

Our local (Windows) build is similarly messy: we have one folder filled with jars. We build with Eclipse (not Gradle, Ant, etc) and do not have an explicit list of jars used by the web applications. The Linux build does use Ant, but semi-selectively globs jars into the WAR files.

Question

I would like to white-board a strategy on how to extricate ourselves from this predicament. One approach is listed below, but I would appreciate any refinements, or alternative approaches.

The goal is to correct the deploy process, not the local build.

Strategy

On local machine, use Gradle to compile each web application:

add compile-time dependencies until compilation is OK

locate Gradle's local cache (or observe output) to find transitive run-time dependencies

add all jars to ~/WEB-INF/lib for the web app, with explicit references in the formal Ant build

deploy into a fresh, pristine Tomcat install

observe startup logs for any "ClassNotFound" exceptions, both for 3rd-party libraries and our application libraries

add these libraries to the formal Ant build

perform regression QA tests on application and monitor for "ClassNotFound"

答:
In my experience building applications using Maven for Tomcat 6 and Tomcat 7, I never needed to deploy jars explicitly to Tomcat's common lib directory.

Take a look at this post: https://stackoverflow.com/questions/267953/managing-libraries-in-tomcat

Answers there indicate that conflicts may arise between classes in the shared common lib folder, and those in each web application, as each web application may have dependencies of differing versions.

Also, according to the Tomcat page you linked to for Common class loader libraries: "Normally, application classes should NOT be placed here".

For example, if you had log4j-1.2.15.jar in the commons/lib directory, and log4j-1.2.17.jar in your application lib dir, Tomcat may try to load 1.2.15, while your application was written to use 1.2.17, which can lead to ClassNotFoundExceptions, and potentially other exceptions.

I'd recommend only placing libraries that Tomcat needs for itself into the common lib directory, and let each application context load its own dependencies.

If you use Maven (and configure the project correctly), you should not need to manually move any jars around. Gradle may have similar functionality.

Title: Refactoring Tomcat webapp deployments


问:
Pulling results from postgresql with cursors
I'm writing an application in C++ using libpq as the interface to postgresql. I'm currently putting together a small library of helper functions to help deal with the conversion from C++ to C for an application to be used on either a PC or an ARM device (2GHz 2GB ram).

With regards to pulling data from the database, I'm concerned as to whether I should pull all the results from the database at once, or use a cursor to read one or a few at a time.

The returned result set could probably be in the hundreds and I'm assuming:

if I pull all results, it'll be a memory hit for the application

if I pull one tuple at a time via a cursor, it could be a heavy burden on the database server (currently localhost for development)

I don't understand how I could obtain some suitable metrics to determine which would be best, so my questions are:

Is there a best practice when it comes to retrieving results from the database in the real world (all at once / a row at a time / a few rows at a time)

How would I determine some form of metrics to test efficiency? I'm guessing I'd have to take both application memory usage and database load into account along with response time.

If it is best to pull a few tuples at a time, how do I determine the optimal amount to pull at once?

答:
From our experience, cursors cause more trouble than they're worth. SQL is about result sets, and that's what you should be dealing with. The ideal is to decide on some result set size that optimises network traffic and application memory usage, and see if you can deliver within that limit. Scrolling list views have been doing this since forever.

For queries on an index you can WHERE and LIMIT.

For many (simpler) queries you can use OFFSET and LIMIT.

Test the optimiser -- this may work for complex queries too, but you don't want to execute the query twice.

Depending on your API, it may be possible to create a stored procedure and a temporary table.


问:
Writing a superset of a programming language as a transcompiler
My idea is to write a superset of C# (but question is not language-specific), so that it source-to-source compiles (transcompiles) to C# itself (fall-through switch clauses, default method parameters etc., nothing impossible in C#).

First idea was to parse it, make syntax trees, abstract trees etc. but it seems as a bit of an overkill to me, mostly because large portions of code will remain the same.

My question: Is there a simpler way to do this?

One of my ideas was to search for tokens that need modifying (e.g. switch in case of fall-through) and then rewrite the code (add goto case NEXT_CASE where needed) but is there a better and cleaner way to do this?

答:
If you want this to maintainable then not really. I've seen a compiler that was literally an overgrown sed script. It worked of course, but then we decided we wanted to add something to the language..

However, if you take the more or less standard route of

Lex

Parse

Compile superset to vanilla C# AST

Pretty print AST

you can almost certainly use an existing library for 4, and if you decide to grow your compiler then you'll have a far easier time. If you want to do anything vaguely serious with this compiler than the initial overhead is well worth it.

It might be worth your time to look into some nicer tools for parsing/lexing. I don't think it'd be impossible to find/modify an existing C# grammar to deal with 1 and 2.


问:
Efficiency of self-education
Do you think being self-educated in software development is good? Please give an example of what you have learnt successfully by yourself.</p>

答:
I learned most of my C/C++ knowledge in class, but all of my other programming languages, all of my theory, all of my project management, and tons of other knowledge that makes me the hacker I am were self-taught.

As others have said, self-education in our industry is not just good, it's necessary.  This isn't like being a plumber or baking bread: technology is always evolving, and we have to move with it or become irrelevant.

It's worth asking: what are the implications of this?  The best hackers aren't just code monkeys spitting out code -- we take the time to learn the big-picture stuff.  Algorithms, complexity, language design, etc. will stay with us and serve us well no matter what language we find ourselves using, or what we find ourselves using it for.  I've seen this type of hacker jump from one part of the tech industry to a completely different one, effortlessly.

Too many people think the "big picture" concepts are above the heads of all but academics, and I strongly disagree.  When you take the time to learn them, you'll find that some are already practiced intuitively by good coders, and the rest make sense once you've become familiar with them.

Title: Efficiency of self-education


问:
Reconstructing a huffman tree using minimal information in the header
I'm writing a huffman encoding program in C. I'm trying to include the least amount of information in the header as possible, I know the simplest way to decompress the file in the header would be to store the frequencies of each character in the file, but for a large file with 256 characters it would take 2304 bytes ((1 byte for character + 8 bytes for long frequency) * 256), which I don't think is optimal.

 I know I can reconstruct a tree from a preorder scan and an inorder scan of it, but that requires having no duplicate values. That is bad because I now have to store each node in the tree (in a huffman tree: n*2 - 1 with n being the number of unique characters), twice, having each node be a long value (which could take ((256*2 - 1) * 2) * 8 = 8176 bytes.

Is there a way I'm missing here, or are those my only options?

Thanks.

答:
First, as discussed in comments, you should get rid of the frequencies since you only need them to create the tree, not to reproduce the codes for decoding. In your program, but not on disk, the tree structure would might look like this (note the absence of frequencies):

```<code>struct Node {
  char value; // only used for leaf nodes
  // leaf nodes have BOTH child pointers NULL
  struct Node *left, *right;
}
</code>```

I think the following scheme should allow reproducing the tree (though not the frequencies) using at most 2n * k bits for alphabets where each character takes k bits (so k <= log2 n <= k + 1):

Assign arbitrary consecutive indices to all interior nodes of the Huffman tree.

For each character, write out the index of the parent node.

Order the interior nodes by their indices. For each node except the root, write out the index of its parent node. For the root node, make its "parent" index equal to itself.

Since there are at most n-1 interior nodes, node indices fit into k bits each. So the interior node records plus the n character records, we arrive at slightly less than 2n*k bits. Decoding is relatively easy: First read the k character records, create the corresponding interior nodes, and iteratively add the newly discovered nodes (those referenced by other interior nodes but not yet created). You can recognize the root node by its self-reference.

Note that this would require a different tree structure, one with parent references instead of child references and a flag to distinguish leaf nodes (in memory, you can use NULL for the root's parent) If this makes it easier to generate the codes, you can invert the parent pointers, i.e. turn this representation into the nice top-down structure mentioned above.

Caveat: I assumed k is known to both parties (if not, a single extra byte should suffice for any practical application). I also assumes an alphabet of fixed-size bit vectors, but I think that's the case in virtually all applications (and if it's not true, you can add that metadata and still get away rather well).

Body:
First, as discussed in comments, you should get rid of the frequencies since you only need them to create the tree, not to reproduce the codes for decoding. In your program, but not on disk, the tree structure would might look like this (note the absence of frequencies):

struct Node {
  char value; // only used for leaf nodes
  // leaf nodes have BOTH child pointers NULL
  struct Node *left, *right;
}

I think the following scheme should allow reproducing the tree (though not the frequencies) using at most 2n * k bits for alphabets where each character takes k bits (so k <= log2 n <= k + 1):

Assign arbitrary consecutive indices to all interior nodes of the Huffman tree.

For each character, write out the index of the parent node.

Order the interior nodes by their indices. For each node except the root, write out the index of its parent node. For the root node, make its "parent" index equal to itself.

Since there are at most n-1 interior nodes, node indices fit into k bits each. So the interior node records plus the n character records, we arrive at slightly less than 2n*k bits. Decoding is relatively easy: First read the k character records, create the corresponding interior nodes, and iteratively add the newly discovered nodes (those referenced by other interior nodes but not yet created). You can recognize the root node by its self-reference.

Note that this would require a different tree structure, one with parent references instead of child references and a flag to distinguish leaf nodes (in memory, you can use NULL for the root's parent) If this makes it easier to generate the codes, you can invert the parent pointers, i.e. turn this representation into the nice top-down structure mentioned above.

Caveat: I assumed k is known to both parties (if not, a single extra byte should suffice for any practical application). I also assumes an alphabet of fixed-size bit vectors, but I think that's the case in virtually all applications (and if it's not true, you can add that metadata and still get away rather well).


答:
There are 2 separate problems, store the topography and assign the leaf nodes

Assigning the leaf nodes can be done by storing the characters in in a predefined order so it can be extracted as needed.

Storing topography can be done by having a bit vector with 2 bits per parent node in the previous layer where 1 represents a compound node and 0 represents a leaf node

so first there is 1 bit for the root which is 1 and the next 2 bits will represent the next level down

to build the tree using the node{char value; node* left, right;} setup will be:

```<code>char[] chars;//prefill with the other array
int charIndex = 0;

node root;
vector<node*> toBuild(root);

while(!toBuild.empty()){
    node n = toBuild.popFront();
    bool bit = grabBit();
    if(bit){
        n.left = new node;
        toBuild.pushBack(n.left);
    }else
        n.value = chars[charIndex++];
    bit = grabBit();
    if(bit){
        n.right = new node;
        toBuild.pushBack(n.left);
    }else
        n.value = chars[charIndex++];
}
return root;
</code>```

This is 2*n bits in the topography plus the permutation which is O(log n!) at the minimum.

Another option is to store the length of each encoded token. Using just that you can build a huffman tree deterministically. You start with the shortest token and assign it all 0 bits. The next token you add with carry 1 to the encoding and add 0 bits as needed. To store the length of each token you can use a fixed huffman encoding.

This is the method used in DEFLATE.

Title: Reconstructing a huffman tree using minimal information in the header

Body:
I'm writing a huffman encoding program in C. I'm trying to include the least amount of information in the header as possible, I know the simplest way to decompress the file in the header would be to store the frequencies of each character in the file, but for a large file with 256 characters it would take 2304 bytes ((1 byte for character + 8 bytes for long frequency) * 256), which I don't think is optimal.

 I know I can reconstruct a tree from a preorder scan and an inorder scan of it, but that requires having no duplicate values. That is bad because I now have to store each node in the tree (in a huffman tree: n*2 - 1 with n being the number of unique characters), twice, having each node be a long value (which could take ((256*2 - 1) * 2) * 8 = 8176 bytes.

Is there a way I'm missing here, or are those my only options?

Thanks.

Body:
There are 2 separate problems, store the topography and assign the leaf nodes

Assigning the leaf nodes can be done by storing the characters in in a predefined order so it can be extracted as needed.

Storing topography can be done by having a bit vector with 2 bits per parent node in the previous layer where 1 represents a compound node and 0 represents a leaf node

so first there is 1 bit for the root which is 1 and the next 2 bits will represent the next level down

to build the tree using the node{char value; node* left, right;} setup will be:

char[] chars;//prefill with the other array
int charIndex = 0;

node root;
vector<node*> toBuild(root);

while(!toBuild.empty()){
    node n = toBuild.popFront();
    bool bit = grabBit();
    if(bit){
        n.left = new node;
        toBuild.pushBack(n.left);
    }else
        n.value = chars[charIndex++];
    bit = grabBit();
    if(bit){
        n.right = new node;
        toBuild.pushBack(n.left);
    }else
        n.value = chars[charIndex++];
}
return root;

This is 2*n bits in the topography plus the permutation which is O(log n!) at the minimum.

Another option is to store the length of each encoded token. Using just that you can build a huffman tree deterministically. You start with the shortest token and assign it all 0 bits. The next token you add with carry 1 to the encoding and add 0 bits as needed. To store the length of each token you can use a fixed huffman encoding.

This is the method used in DEFLATE.


问:
When running PHPUnit tests - is it common to use a separate database for testing?
I'm running tests from the same database as I use in development. I recall using Rails and I used a separate database for testing (mainly coz the tutorial I was going by said so). Made sense though. I was wondering it this was the common way to do things or whether there was an alternative. Can anyone shed a little light on this?</p>

答:
Quite common, but… it is a result of misusing PHPUnit

It is important to distinguish unit-tests from integration-tests and acceptance-tests.

The main aim of PHPUnit is writing unit-tests.

Unit test is supposed to test isolated "units" (functions, public methods). Everything else, including database, is emulated via Test Doubles (Mocks). So, if you need to test method of your controller you "mock" model classes so that they return pre-defined (or randomly generated) answers. If you need to test the model you mock db-access classes and, taking this to the extreme, if you need to test db-driver you mock the socket-layer to return specific byte-sequences.

On the other hand it is possible to use PHPUnit for acceptance-tests (though, there are better tools for that). Acceptance tests in web-applications are supposed to fake web-requests and check that application returns proper responses. It is very desirable to check the full-stack, so, requests should hit database eventually. In this case it is a good idea to use a separate database, so that tests do not harm production data.


问:
Zend framework 2 authentication - only storing the user ID in session
I've just put together my first app using Zend_Authenticate. According to some tutorials I've read they store the user's "email" column. Is this recommended? I've read to store the ID only, then on each request fetch the user by ID. That way, only the ID is stored in the session. Is this recommended with Zend too?

Anyway here is my handling of user login, I'd appreciate any feedback. Basically I'm authenticating. If that passed, I then store the ID in auth storage and a custom flash message. The script then redirects to the homepage (upon which the login form will be replaced with a user panel - e.g. "Welcome back John" - and my flash message will display.

Below is my loginAction method:

```<code>public function loginAction()
{
  $form = new LoginForm();

  $form->get('submit')->setValue('Login');

  $request = $this->getRequest();

  if ($request->isPost()) {

      // check the users credentials and authenticate
      $this->getAuthService()
        ->getAdapter()
        ->setIdentity($request->getPost('email'))
        ->setCredential($request->getPost('password'));

      $result = $this->getAuthService()->authenticate();

      if ($result->isValid()) {

        // set the user id in storage
        $resultRow = $this->getAuthService()->getAdapter()->getResultRowObject();
        $this->getAuthService()->getStorage()->write($resultRow->id);

        // set flash messenger
        $this->flashMessenger()->addMessage('You are now logged in');

        // redirect to the home page
        return $this->redirect()->toUrl('/');
      }

  }

    return new ViewModel(array(
      'form' => $form,
    ));
  }

}
</code>```

答:
There isn't any "golden" rule here, just common sense:

storing user-id is a minimum. If it works for you (see below) — great, no need to complicate the matters.

storing additional information inside of session is nothing else but denormalization: you trade automatic consistency for speed. if you store enough information in session (display name, email, …) for generating some of the pages you won't need to query the whole user-object in a separate DB-query (speed!) but you will have to be sure to sync updated fields of profile between DB and the session (no more automatic consistency)

so, if speed benefit is important for you — give it a chance. otherwise — just store user-id


答:
It's not necessary to store the actual frequencies of each symbol, or the exact topology of the Huffman tree.  You only need to store enough information to encode the level on the tree at which each symbol resides.

You can modify a Huffman tree by shuffling symbols  and internal branch nodes around on the same level without changing the coding efficiency of the tree.  So it makes sense to map your particular huffman tree to its canonical version then you only need to specify which of the canonical trees you are using.  I suggest, starting at the top then going down, shove all the symbols to the left as far as they will go, then sort them in ascending order.

Once you've made you tree canonical you need to actually encode it.

If you limit your tree depth to 32 levels then you can just encode a 256 by 5-bit array (160 bytes) giving the huffman tree level of each symbol.

You can approach the information-theoretic minimum of the encoding size of you arithmetically encode the subset of available symbols at each level, but I figure since you're using Huffman codes you're not ready for arithmetic encoding yet.


问:
Assigning responsibility for cancelling an Order
During a conversation with our Domain Expert we can across this feature:

A Customer service agent can cancel an Order by decreasing its quantity. To cancel an Order we decrease its quantity by the specified quantity, change its status to either partially cancelled or fully cancelled and log the CS agent who issued the cancellation.

We are currently split between several solutions (assuming we have the agentId, orderId and cancelledQty from the request):

Solution 1:

```<code>CSAgent agent= (CSAgent) EmployeeRepository.getById(agentId);
Order order= OrderRepository.getById(orderId);
agent.cancel(order,cancelledQty);
</code>```

vs

Solution 2:

```<code>CSAgent agent= (CSAgent) EmployeeRepository.getById(agentId);
Order order= OrderRepository.getById(orderId);
order.cancel(cancelledQty,agent);
</code>```

Solution 1 really captures what the requirements is conveying and reads really well; however, solution 2 seems more natural when coding the implementation of the cancel() method as I'll mostly update fields in the Order class itself which I hope to keep private with no public setters.

The only thing I need from the CSAgnet class is a snapshot of its data to be stored with the Order.

So how should I determine the best course of action from a DDD perspective?

答:
Solution 1 violates Single Responsibility Principle

CSAgent.cancel sounds like we're cancelling an agent!

The method parameters hints that the agent knows order implimentation enough to set the order's state correctly. CSAgent certainly has to know the difference between "partial" and "complete" order processing.

Even if CSAgent.cancel(order, quantity) just calls order.cancel(quantity), CSAgent simply has no business doing the order cancel process for another entity.

Solution 2 violates Single Responsibility Principle

Just switch CSAgent with Order above. It's the same thing.

We need a God Object

Well, not exactly; but a class that executes the order cancellation process. That logging is missing from all our solutions tells me this.

"OrderCancelProcessor" (just to give it a name for the time being)

Both CSAgent and Order may have CancelOrder() methods but they will not take references to each other. They only update their own state.

May process differently depending on "complete" vs "partial" cancellation.

Formalize Cancellation - including Partial and Complete

I think a OrderCancellation entity would naturally, eventually come out of the DDD process. This might represent an immutable order cancellation request (state) - a DDD thing.

I suspect this would be passed to agent.cancel() and order.cancel().

Likewise, It's clear there are different aspects of "cancellation". I suspect that the OrderCancellation may encapsulate the "complete" and "partial" concepts. As such it communicates to the players in the cancellation workflow that this cancellation is partial or complete.

Even if the difference (partial vs. complete cancellation) is literally only a matter of Order.Quantity being zero or not; it is a mistake to ignore domain concepts just because underlying code may be trivial.

If you don't encapsulate these concepts I expect classes will be infested with this:

if(thisOrder.Quantity > 0) { ... } else { ...}

I guarantee some/many/most/all of the cancer-code above will not check for negative numbers.

This cancer-code becomes a poster child for DRY and SRP

Each class involved in the order cancel workflow does its own thing in terms of a order cancellation vice an order quantity.

Getting Process Players Together

The Command Design Pattern comes to mind. This design will keep the CSAgent and Order decoupled and help us adhere to SRP, DRY, etc.

NOTE: Command Pattern may be overkill. It all depends on the domain model specifics which I am blissfully ignorant of.


问:
Which Continuous Integration framework do you use and why?
There are quite a few different Continuous Integration (CI) frameworks out there and I'm wondering which is the most popular.  Which frameworks have you used at firms where you work?

Is there any reason one CI framework is more popular than another - perhaps this is to do with the features it offers, things that integrate into it or maybe its just marketing?

It seems like continuous integration is used more in the Java and .net worlds than say ruby or python.  Why is this?

答:
Hudson or Jenkins (the latter is a fork of the former). Reason: It is simple (simple to install and to use) and has great flexibility. The plugins add nearly every functionality I can think of.

Some years ago I used damagecontrol. It was also simple to use, but hadn't the plugins. But the author decided that he would give up on the simple solution and started development of a new version, that consisted of different servers communicating with each other (what the hell?). That didn't worked well and the project was given up. I was on the search some time, but neither cruisecontrol (too complicated) or continuum really got me. Until hudson, that worked from the first moment for me.


答:
I used to (not in PHP admittedly, but this applies to every DB) test stored procedures on the dev DB.

DBs have transactions. So start one, truncate tables, insert test data, run tests and then rollback. Your DB will be back to its old state. Simple.

Title: When running PHPUnit tests - is it common to use a separate database for testing?

Body: I'm running tests from the same database as I use in development. I recall using Rails and I used a separate database for testing (mainly coz the tutorial I was going by said so). Made sense though. I was wondering it this was the common way to do things or whether there was an alternative. Can anyone shed a little light on this?</p>


答:
```<code>http.createServer(function (req, res)
{
    // do stuff
});
</code>```

is almost exactly the same as

```<code>function handler (req, res)
{
    // do stuff
}

http.createServer(handler);
</code>```

The arguments passed to a called function are evaluated in the same scope as the call. Now, if you had done something like:

```<code>http.createServer(function (req, res)
{
  function handler (req, res)
  {
    // do stuff
  }
  return handler(req, res);
});
</code>```

Then yes, you'd be creating a new function for every request, and that would be inefficient. In terms of something like a web server this is usually not a problem since the request handlers are often declared in a single place.

The only time you really want to nest functions (which is quite different from passing a function as an argument, as you are doing in your examples), is when you need references to the enclosing scope's variables (ie: a closure)

In answer to your second question, no. There's nothing different about event handler functions, and both your examples are functionally equivalent, and should essentially compile to the same machine code.

Title: The nature of callbacks in Node.js

Body:
In the plain javaScript world I've learned that nested functions can be problematic because the inner function is "re-created" every time the outer function is called:

```<code>function outer(a) {
    function inner(arg1,arg2){
        return arg1 + arg2;
    }
    return inner(a, 6);
}
myVar = outer(4);
</code>```

Apparently the inner function is garbage collected and the constant re-creation of it has a cost on performance. This could be re-factored as:

```<code>function inner(arg1, arg2) {
    return arg1 + arg2;
}

function outer(a) {
    return inner(a, 6);
}

myVar = outer(4);
</code>```

If I am correct it seems that the tradeoff lies in the changing scope of inner; the fact that function inner no longer has access to outer's own variables. If I am correct so far, lets move to Node.JS. Node is heavy on callbacks, and my question is, are asynchronous callback functions typical in Node also "re-created" every time they are fired? Take the following common Node construct:

```<code>http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
</code>```

In reality the function can get pretty large; it can encompass a lot of functionality. Is the above callback "re-created" every time a client GETs or POSTs the server? If so, would it be better to always de-couple the callbacks as in the plain javaScript example:

```<code>function handleGET(req, res) {
   res.writeHead(200, {'Content-Type': 'text/plain'});
   res.end('Hello World\n');
}

http.createServer(handleGET).listen(1337, '127.0.0.1');
</code>```

as a related bonus question, are event functions re-created too? Would they benefit from prior  declaration?

```<code>myObj.on('someEvent', function someFunction() {...});
</code>```

vs.

```<code>function someFunction() {...}
myObj.on('someEvent', someFunction);
</code>```

Body:
http.createServer(function (req, res)
{
    // do stuff
});

is almost exactly the same as

function handler (req, res)
{
    // do stuff
}

http.createServer(handler);

The arguments passed to a called function are evaluated in the same scope as the call. Now, if you had done something like:

http.createServer(function (req, res)
{
  function handler (req, res)
  {
    // do stuff
  }
  return handler(req, res);
});

Then yes, you'd be creating a new function for every request, and that would be inefficient. In terms of something like a web server this is usually not a problem since the request handlers are often declared in a single place.

The only time you really want to nest functions (which is quite different from passing a function as an argument, as you are doing in your examples), is when you need references to the enclosing scope's variables (ie: a closure)

In answer to your second question, no. There's nothing different about event handler functions, and both your examples are functionally equivalent, and should essentially compile to the same machine code.


问:
Make Return Type an Interface - Problem with Initialization
I would like to make the return type of my method an interface rather than a class for similar reasons stated in  c# List or IList, however I am having trouble figuring out how to initialize the interface to return it.  I cannot use new IA() and (IA) new A() does not work as I cannot cast the result to B.

```<code>interface IA{}
class A: IA{}
class B: IA{}

class UseIA
{
  public IA DesiredMethod()
  {
    return ???;// new IA()
  }
  public A UndesiredMethod()
  {
    return new A();
  }
}
</code>```

答:
An implementation of an interface is the realization of that interface. Realization is being implemented within the class. Just return an instance of the class (A or B) which realizes the return-type interface.</p>

```<code>interface IA{}
class A: IA{}

class UseIA
{
  public IA DesiredMethod()
  {
    return new A();
  }
}
</code>```

Body:
An implementation of an interface is the realization of that interface. Realization is being implemented within the class. Just return an instance of the class (A or B) which realizes the return-type interface.

interface IA{}
class A: IA{}

class UseIA
{
  public IA DesiredMethod()
  {
    return new A();
  }
}


问:
Where to place 3rd party software license information?
After a user installs my application, where in the app's install directory should I place information regarding 3rd party software (e.g. FOSS) licenses? Should I just append it to my EULA? Is there some sort of convention or standard that I should following to make it easily scan-able by license detection scripts?</p>

答: If you look at other software that makes use of FOSS you'll usually find the information available through the about option in the menu. For an example have a look at the Chrome or Firefox about screen/page. </p>


问:
In what situation do Entity Framework enums become useful?
I am working on a project where there will be plenty of static options being stored in the database. I looked at using Enums for this, but do not see how they could be useful.

They do not create any kind of look-up table, just reference a number in the table which can be used in code as an enum option. The number is meaningless to anyone creating SSRS reports and if you need to add an extra option, you need to recompile.

Is there a situation where these have a genuine purpose and are a better fit than an entity. Or are they generally a bad practice for the above reasons?

答: For reporting scenarios, the database will not be able to report any meaningful information about the enum. This makes Entity Framework enums much less useful unless you use the enum to supply the name or text value, and graft that onto a class that represents the lookup table (which is not an EF enum, I realize).</p>


问:
Fullstack programmers and web design
I have been looking into full stack developments and I am confused as I am getting different answers from people.

I know that full stack developers are responsible for front end and back end programming, but some people say that they are also responsible for designing websites as well.  Can someone clarify this? If this is true, it seems impossible that someone would be able to keep up with all of the latest design/art trends while keeping up with an insane amount of languages and frameworks which constantly change (considering they would technically be doing 3 jobs in 1)

EDIT: By designing websites, I mean, are they responsible for making the template in Photoshop and then implementing it? Or will they be presented with a photoshop template which they will have to implement?  It was incorrectly worded by me, so I thought I would clarify.

答:
A web developer develops, but does not necessarily design, web sites. Graphic artists design, but do not necessarily develop, web sites. You can do both but not many can do both well.

Therefore, a "full stack web developer" is not expected to also do design.

Title: Fullstack programmers and web design


答:
If, by design, you mean the actual artwork or template in photoshop, then, no, full-stack developers do not typically do this.

However, there's been many a time when I've been presented with a single template and expected to extrapolate it into a functional site - this means making design choices beyond the original template. And then, typically, I've had to implement UI design that was never included in the original brief. So a full-stack developer needs to have some design skills, but not to be the designer.

Title: Fullstack programmers and web design

Body:
I have been looking into full stack developments and I am confused as I am getting different answers from people.

I know that full stack developers are responsible for front end and back end programming, but some people say that they are also responsible for designing websites as well.  Can someone clarify this? If this is true, it seems impossible that someone would be able to keep up with all of the latest design/art trends while keeping up with an insane amount of languages and frameworks which constantly change (considering they would technically be doing 3 jobs in 1)

EDIT: By designing websites, I mean, are they responsible for making the template in Photoshop and then implementing it? Or will they be presented with a photoshop template which they will have to implement?  It was incorrectly worded by me, so I thought I would clarify.