问:
Dealing with data, when your database is under version control
Currently my database is not under some kind of vcs, we can get deltas but that's about it. I would like to try and make product deployments more automated, and less time consuming. I understand that placing a db's schema files under source control allow you to manage versions, and that these files are basically for dropping the old tables/indexes/etc and then adding the new versions.

My question is, what about the data that's already there? By dropping everything we'd lose all of the data. So, we would have to do a data dump before updating the database, and then re-load the data back after the update has been done. Problem is, some of our largest databases have 80+ GB of data, and we probably have a total of 20 sets of databases (6 DBs per set).

I'm sure that this would work, but given the size of everything, is there a simpler solution that would cut out the need to dump and reload everything each time a schema update took place? And, if not, wouldn't we have to dump the data such that reloading it took in to account the new schema?

答:
What we used in a company I used to work at is Liquibase. It is a solution in Java, but since you only ever need to run it once per DB update it will likely work for other languages as well. I'm pretty sure there are other solutions that work in a similar way, so I'm just going to describe how it functions in general so you can search for something that works for you:

You have a file (or set of files) with changelogs - commands that need to be run on a DB to update it from version a to version b. The DB itself then has a special table that tracks which of these commands have been executed against it along with checksums, timestamps and other misc info.

Then when you need to make an update to the schema - add the commands that need to be run to update the DB without dropping the data (likely UPDATE statements). Then when you actually run the update, the updater will find the version of the DB schema, find all updates that need to be run to get from its current version to the latest version and run only those in correct order.

That way you do not recreate the schema every time and don't need to reload the data. It's also automated and fairly quick compared to doing it manually or recreating the DB completely


问:
Why can't we use IP address instead of cookies in identifying the client in servlets?
I know we have some extra advantages in using the cookies over IP address, but my question is Why can't the container just remember the IP address of the client in identifying the client when he visit his site again? Is it possible for the container to remember the client with the help of IP address?</p>

答:
A client is identified by a cookie as well as the IP address. However, the IP address cannot be used exclusively:

What if two clients are located behind the same NAT firewall or proxy? They will have the same external IP address to the server.

What if a user has two different browsers open on the same machine, and wants two separate sessions (maybe for testing?)

A user may have a dynamic IP address which conceivably could change during a session.

An attacker may be able to spoof an IP address and take over a session if it relied on IP address alone.

This means an IP address does not uniquely identify a client in all cases.

ContentLicense: CC BY-SA 3.0


答:
Sometimes you can use IP address.

If you're on a LAN or are otherwise dealing exclusively with users that have IP's statically distributed to single clients, using that address is perfectly fine -- sometimes preferable and necessary.

But, usually you can't.

If you're running a public site, most of the IP addresses that hit your server aren't static or dedicated. Most of them represent multiple clients: Your desktop, laptop, and cellphone all go out over the same IP address when you're on your home network. And that IP can change -- even mid-session.

Title: Why can't we use IP address instead of cookies in identifying the client in servlets?

Body: I know we have some extra advantages in using the cookies over IP address, but my question is Why can't the container just remember the IP address of the client in identifying the client when he visit his site again? Is it possible for the container to remember the client with the help of IP address?</p>


问:
What to do about "stopping point syndrome"?
I have noticed a behavior in myself that I call "stopping point syndrome"... it means I cannot stop working until I reach a "stopping point" (or I become exhausted).  In other words, if I am in the zone working on getting a feature completed, I won't stop until I get it done.   If it's a bug I'm working on, this effect is even more pronounced... I won't stop until it's fixed, or at the very least, understood.  Even though I work from a laptop and have VPN and could easily go home and pick up on the work later that evening, that fact does not help me leave work... it's like I'm afraid I will die before I find the bug.

I have a very hard time explaining this to family members who can't figure out why I never come home from work at the same time and sometimes come home at 11 pm.  Even managers have been mystified by this, and I've noticed that most of my colleagues have no trouble working the same hours every day.

Does anyone else have this problem?  Should I be worried about this and/or try to change it?  If so how?

答: It's great that you enjoy your work. But it's also very sad how so many people here seem to lead lives that revolve around work.</p>

ContentLicense: CC BY-SA 2.5


问:
Self-Evaluation: How do I know if I actually have a "good grasp" of OOP?
If I skip the back story and any thoughts I have on this topic, there's really only one question left to ask:

How can I find out if I have a "good grasp" on OOP?

(I am specifically using PHP, but it probably won't matter...)

Right now I kind of think of classes as a collection of functions with some global-ish variables accessible by all those functions. This helps to reuse code, keep files short and the namespace clean.

Some of you mentioned inheritance: To me that again just means that I can extend an existing class with more functions and more global-ish variables. It's like an add on to my existing class. And the same benefits come into place: reuse code, keep files short.

I have a ominous feeling that I'll be disillusioned here in a minute...

答:
When I first forayed into the realm of truly understanding JavaScript, I subscribed to newsletters, read code for new libraries and frameworks, and read books and every interesting blog post I stumbled across.  My appetite was voracious.  But over time as I worked on JavaScript projects -- actual experience is essential -- and kept up my reading, I eventually noticed fewer fresh ideas.  My JavaScript worldview shrunk.  Much is familiar.

My more recent foray has been Functional Programming.  I've grokked high-order functions, composition, currying, referential transparency, and most of the fundamentals, but I still tread into unfamiliar territory when I find posts related to Monads, Functors, and parts of Haskell.  When you start to notice that your appetite has been satiated -- namely, it's harder to find something fresh and exciting -- that's when you've got a "good grasp."


问:
How to update last accessed with Forms Authentication cookie
I've got an MVC 4 site where I'm using Forms Authentication. I'm setting a cookie when a user logs in. The majority of my site does NOT require a user to be logged in though.

What I'd like to do, whenever a user FIRST accesses my site, I'd like to update my "LastLogon" that I have stored in my database table to the corresponding value (if Request.IsAuthenticated). But I don't want it to fire on EVERY page that the user accesses.

So, where do I track when the user first accesses my site? Is that in the Global.asax? Is Application_Start method for all first requests to a site, or is that when the website first loads after an iisreset?

答:
I realized/found yesterday that in the Global.asax that there's a method available called "Session_Start". I believe that will work perfectly.

I could also use the Application_Request method, but I don't want to hit my SQL Server that often.

(I'm also thinking of adding it to the "Session_End" method in case they are on the site for along time...

Here's a link to the Global.asax methods available:

http://sandblogaspnet.blogspot.com/2008/03/methods-in-globalasax.html

Edit/Update:

I just stumbled across this article on CodeProject that appears to be the time that the cookie is ready:

http://www.codeproject.com/Articles/288631/Secure-ASP-NET-MVC-applications


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

答:
Self-education is necessary unless you have a generous benefactor willing to support formal training time and time again.  I've had to learn a lot of things on my own outside of a class room, to name some of the bigger ones:

ASP and ASP.Net would be things I had to learn on the job as where I worked wanted to make the new site re-write use these and thus I had to pick this up on my own rather than in a classroom.

IIS and MS-SQL Server would be another couple of things that I learned about as I needed and thus I'd say I was self-taught here.  While I did have co-workers that could help with questions if I got really stuck just getting my head around these was something else back in the early days of my career.

AJAX would something a that a handful of years ago now I learned on the job in researching frameworks and trying to bring it into where I worked just before the Microsoft ASP.Net AJAX launched.

At the same time, only doing self-education can be dangerous as not everyone wants to do everything this way.  For some people they may want more structure and not be so independent in driving their way through learning something.

Title: Efficiency of self-education


问:
How do I / what is the best way to interact with a desktop application from a browser web application?
Situation:

We have a desktop application.
We also have a web application.

We need the two to interact with each other. So when a button is pushed in the web application, the desktop application needs to start running a process (pulling data from the cloud using an API we've created). With two local applications, this can be done with ports/listening, editing a flag somewhere locally, and the other application reading this flag, etc.

Can this be done from web -> desktop?

Ideally the web application would have direct communication with the desktop application and notify it to begin pulling data down from the cloud using our API. But if this is not possible, having the desktop app run a process that loops and checks for a flag in a file or something would be a good start.

After minimal research, I've found some stuff like HTML5 File Api which interacts with "Sandbox" directory on the local machine. I've also read that it is impossible to access any thing on the local machine for security reasons.

Also, I dont want the user to be prompted to request a download file everytime, we need things to happen behind the scenes. (Even if the user has to initally set up some lower security / permissions just for this situation)

Any input is appreciated!

答:
Generally speaking, its a good thing that web pages cannot interact with your operating system.  You wouldn't want any random site to be able to access your files and programs.

If you need to work around this, a couple of options come to mind.  Each will require some effort.

turn your web application into a desktop application using a tool/framework such as node-webkit.  Then you'll have access to the same local resources a desktop application would have.

Host the web application inside a desktop application with an embedded browser.  Extend the javascript system to allow the web application to intereact with the host application.

Have the/a desktop app act as a web server, listening on port 80.  Then within the web application, make calls to localhost:80.

Create an ActiveX that runs inside the web application that will handle the link.

Create a desktop version of your web application


问:
1 class model or 3 class models? 1 for each - UI, Hardware API Library, and Database
I've always used one class model in all 3 area's of my projects. User Interface, Hardware API (for data collection), Database (entity database context). Every new project only seems to grow in size, which means it gets hard to change the class model because everything uses it.

It was suggested to me by seasoned programmer to split up my class model and use a different one for each part of my project. Then convert the data between models with passing data back and forth between the different parts. Which means if one of the models change that you only have to change the code in that section and change the "converters".

Is this a general practice? It's definitely more work initially but I am wondering if it will save me time in the future.

EDIT: I changed data model to class model as suggested in the comments. Though what I am specifically talking about is data modeled in classes in my application.

答: Yes, this is a general/good practice. See modular programming, separation of concerns and single responsibility principle. These concepts are interrelated but the gist of it is that you should break up your code into independent, single-purpose pieces. This allows you to make changes to the pieces individually without affecting the rest, swap them with similar pieces and reuse them elsewhere. Currently you wouldn't be able to reuse, say, your hardware code in another project that uses a different UI; the parts are said to be tightly coupled.</p>


答:
So it sounds like your projects typically have three areas: data collection, storage, and presentation. There are tradeoffs between the single model and multiple models approaches.

With a single model, is it very easy to ensure consistency across all the areas of your project. With multiple models, unless you build in mechanisms for automatic updates (and cope with the subtle bugs that that can lead to), it is possible for them to become inconsistent with respect to each other.

With a single model, as the demands of access to the data increase as your project grows, its interface can get bigger and its internals can get more complex and unwieldy. With multiple models, you must define and maintain interfaces for each model and write a lot of code to pack and unpack data for transfer in and out of them.

It sounds like you already have good modularity in your project, with separate UI, collection, and storage modules. A single data model becomes a fourth module. I'm not convinced that moving to six modules (a separate model for each module) will help things.

What I recommend is that you keep the single model, but give it three interfaces, one for each module that interacts with it: UI, collection, and storage. The UI module would see only the UI interface for the data model, and would not know or care how the data is collected or stored. Likewise for the collection and storage modules. In general, programming in terms of interfaces is very good practice, as it can limit complexity and keeps coupling to a manageable level.

Title: 1 class model or 3 class models? 1 for each - UI, Hardware API Library, and Database

Body:
I've always used one class model in all 3 area's of my projects. User Interface, Hardware API (for data collection), Database (entity database context). Every new project only seems to grow in size, which means it gets hard to change the class model because everything uses it.

It was suggested to me by seasoned programmer to split up my class model and use a different one for each part of my project. Then convert the data between models with passing data back and forth between the different parts. Which means if one of the models change that you only have to change the code in that section and change the "converters".

Is this a general practice? It's definitely more work initially but I am wondering if it will save me time in the future.

EDIT: I changed data model to class model as suggested in the comments. Though what I am specifically talking about is data modeled in classes in my application.


问:
JS design pattern/algorithm for avoiding duplicate redraws in a fairly coupled system
Given:

some sort of widget based web app

lots of JS functionality

high coupling (communication/callbacks between widgets)

widgets draw themselves

certain widgets need to do a complete and fairly expensive redraw every time one of their dependencies is changed.

Problem: there can be complicated dependency chains and it becomes difficult to make sure redraws only happen once per user action.

For example:

A needs to be redrawn if B or C change

B is dependent on C's state

So if C changes, so does B and both may end up causing A to redraw.

If this only happens a few times, it's easy to solve. But when it happens all over, it becomes a nightmare to engineer and makes maintenance even worse.

I assume this is a somewhat common problem, so there must be some method of dealing with. That or my designs are flawed in some way.

Any suggestions? Either some design pattern or an algorithm to deal with limiting redraws would be very much appreciated.

Edit

Alright, based on Randall's suggestions, here's what I'm thinking:

Semaphore like global variable but without any multi-threading stuff so basically just a global count. Let's name this S. I would have one per widget that requires expensive redraws.

S has a signal and a wait function. Signal increments S and wait decrements it.

S is initialized with a callback function to the draw of the widget.

Every time something is triggered (user event, ajax return, etc) S.signal() is called.

Once the event is finished changing things within an object (after callbacks are made), S.wait() is called.

S.wait(), after decrementing S check if count is 0. If it is, it redraws.

So if C changes:

C runs S.signal() (S==1)

C makes its changes, callback to B

B runs S.signal() (S==2)

B makes its changes

B runs S.wait() (S==1, no redraw)

B finishes, C starts again.

C runs S.wait() (S==0, redraw)

Like the dirty flag but more of a dirty-count and I think this is easier to implement. It can be extended for additional functionality fairly easy. If, for some reason, I want to optimize it so not everything is redrawn in certain cases, I can add some flags that represent things to be redrawn and reset them every draw.

Also, it could be replaced with a stack and do basically the same thing but with some better extension capabilities.

Edit 2

Izkata's solution turned out to be trivial to implement and only slightly less flexible. Only 2 changes are needed:

Add a timerID property to the widget that needs redrawn.

Wrap redraw function in a timer:

JS code:

```<code>A.draw = function(){
    var that = this;
    clearTimeout(this.drawTimer);
    this.drawTimer = setTimeout(function(){that._draw();},0);
}
A._draw = function(){
    //old draw code goes here
    //could be made "private" or nested in A.draw
}
</code>```

No other changes are needed so your interface stays the same and it works unless you need it to work with Async code like ajax calls.

答:
Good question. I think you need to separate capture of the need for a redraw from the act of redrawing. Basically, as dependent things change, they notify their parent. The parent then marks itself as being "dirty" which means it needs a redraw. At a later time, the redraw occurs and the parent clears its "dirty" flag. This is common in event-driven windowing and UI systems (sorry, I can't remember a google-able name for this right now).

In your case, if the dirty flag is going from clear to set, you might want to schedule a redraw operation for a short time in the future. Then, other events can occur which will trigger the dirty flag to be set multiple times. After time has passed, you can redraw what needs it. Hopefully the wait time can be short enough to not be noticeable to the user, but long enough to collect all the likely changes that need to be redrawn.

An alternative to the time-based approach proposed above is to use logic to somehow detect that the system has stabilized and defer redraws until that point. I'm not sure what triggers the changes that necessitate the redraws, but perhaps there is an obvious point where the changes have all been made.

A third approach might be to have your system use an event queue to drive its work. The queue could be prioritized, such that data modification events come before redraw events, and that there need be only a single redraw event per widget (or a single event for the entire page).

Since SO sometimes collapses comments, people should read Izkata's suggestion below for using a zero-delay timeout and JavaScript's single-threaded nature to create what is effectively an ad hoc queue to ensure that the redraws get done after the main processing. Clever.


答:
I would begin by transitioning your data access to a protected service layer. If both the web application and desktop application are modified to pull from the same exact data source then it becomes trivial to establish a sort of "polling" of the service to determine if the action has been engaged and new work needs to begin.

Alternatively, you could retool your desktop application into a windows service that continuously watches either your data store or possibly use a filesystemwatcher to detect changes. If the web application has access to make changes to a file, then the service simply needs to watch for changes in that file to begin its actions.

Even beyond that, you could extend your windows service to accept snmp calls. Basically you would be building your own "server" that would be able to receive simple remote requests from your webserver. In essence, it would be a direct communication between web application and desktop application. I will agree here with @GrandmasterB that this is fairly dangerous if you are unfamiliar with this option as this creates a very dangerous intrusion vector on a public system. The connection will need to be secured to a specific port that is monitored and disallowed for all but the relevant systems, and it would be highly recommended to add some kind of encrypted authentication mechanism to it.

My preference, though, would be to make it a service based polling mechanism. It is easier to maintain, chews up fewer resources on either system, provides fewer intrusion vectors and is very scalable should your desktop application be rebuilt or rewritten for some reason.

Title: How do I / what is the best way to interact with a desktop application from a browser web application?

Body:
Situation:

We have a desktop application.
We also have a web application.

We need the two to interact with each other. So when a button is pushed in the web application, the desktop application needs to start running a process (pulling data from the cloud using an API we've created). With two local applications, this can be done with ports/listening, editing a flag somewhere locally, and the other application reading this flag, etc.

Can this be done from web -> desktop?

Ideally the web application would have direct communication with the desktop application and notify it to begin pulling data down from the cloud using our API. But if this is not possible, having the desktop app run a process that loops and checks for a flag in a file or something would be a good start.

After minimal research, I've found some stuff like HTML5 File Api which interacts with "Sandbox" directory on the local machine. I've also read that it is impossible to access any thing on the local machine for security reasons.

Also, I dont want the user to be prompted to request a download file everytime, we need things to happen behind the scenes. (Even if the user has to initally set up some lower security / permissions just for this situation)

Any input is appreciated!


问:
Logging asynchronously - how should it be done?
In many of the services I work on there are a lot of logging being done.
The services are WCF services (mostly) which use the .NET EventLogger class.

I'm in the process of improving these services' performance, and I got to think that logging asynchronously would benefit performance.

I'm unaware of what happens when multiple threads ask to log, and if it creates a bottleneck, but even if it doesn't I still think that it shouldn't interfere with the actual process being executed.

My thoughts are that I should invoke the same log method I call now but do so using a new thread, while continuing with the actual process.

Some questions about that:

Is it ok?

Are there any downsides?

Should it be done in a different way?

Maybe its so fast that it doesn't even worth the effort?

答:
Logging asynchronously is the only way to go if you need low-latency in the logging threads. The way this is done for maximum performance is through the disruptor pattern for lock-free and garbage-free thread communication. Now if you want to allow multiple threads to log concurrently to the same file you must either synchronize the log calls and pay the price in  lock contention OR use a lock-free multiplexor. For example, CoralQueue provides a simple multiplexing queue as described below:

You can take a look on CoralLog which uses these strategies for asynchronous logging.

Disclaimer: I am one of the developers of CoralQueue and CoralLog.


问:
Why don't mobile platforms support generational garbage collection?
Both Windows Phone/Xbox and Android lack support for generational garbage collection. This is frustrating for a lot of programmers. There seems to be a legitimate engineering reason to it, but I can't figure it out.

Current phones have more memory and probably better CPUs than the desktops/laptops running .NET 1.1 with generational GC back in 2001, and I can't think of any reasons that ARM processors would be worse at generational GC than x86 would. There's also less need for multitasking on phones and consoles, so there's relatively more free heap space.

So what gives?

Edit: A few points to clarify:

These platforms use garbage collection for apps exclusively, so my question isn't about why GC isn't supported; my question is about why generational garbage collection isn't.

The reason that people are frustrated about the lack of generational GC is that non-genrational GC is extremely inefficient. (That means that battery life isn't the reason.)

I do believe that there's an honest technical reason for the lack of generational GC support. This isn't a rhetorical question.

答:
My best guess would be that it's not a technical reason, but that the developers for those phone platforms simply haven't put in the effort yet. I know it's not the answer you're hoping for, but if you've done any mobile development you've probably noticed there is a lot of low hanging fruit. They probably simply haven't had the time to write multiple GC implementations, fine tune them all, and select the one that does best. Why would anyone spend time reinventing the wheel when they could instead release new, flashy, poorly-thought-out APIs? ;)

FWIW, and as confirmation of my theory, Android's new runtime (ART) does have a generational GC and they're planning to have a compacting GC in the future.


问:
What are the warning signs of impending doom to watch out for on a project?
Having worked on a failed project is one of the few things that most programmers have in common, regardless of language used, industry or experience.

These projects can be great learning experiences, soul-crushing disasters (or both!), and can occur for a multitude of reasons:

upper management change of heart

under-skilled / under-resourced team

emergence of superior competitor during dev cycle

over/under management

Once you've worked on a couple of such projects, is it possible to recognise at an early stage exactly when a project is doomed to fail?

For me, a big sign is having a hard & fast external deadline combined with feature creep. I've seen projects which were well planned out and proceeding right on schedule go horribly off the rails once the late feature requests started to roll in and get added to the final "deliverable". The proposers of these requests earned the nickname of Columbo, due to rarely leaving the room without asking for "just one more thing".

What are the warning signs you look out for that set off the alarm bells of impending doom in your head?

答:
I've been in three death marches in the last five years. Here are a few things that contributed to my gut feel of impending doom.

The company decides to have junior developers design and develop new features, and assigns more expensive senior developers to fix their bugs.

The company outsources critical software components to third-world companies that don't have the required domain expertise.

The crunch-time cycles come so close together that peoples' health is breaking down.

The pills your team lead takes to drug himself to sleep every night quit working.

The client sends change orders faster than you can analyze them.

You're supposed to deliver several years' work in a few weeks, but management refuses to do a feature freeze.

Your hardware suppliers are clearly having trouble delivering a workable product on schedule, and the decision-makers in your company won't consider any alternatives.

The prototype devices developers need so they have a ghost of a chance of meeting the probably-unrealistic schedule are taken away and given to top execs to make them feel good.

Week one: "Oh, crap, the code is buggy. Everybody quit doing new features and fix bugs." Week two: "Oh, crap, we're not going to meet the feature schedule. Everybody quit fixing bugs and write new features." Repeat indefinitely.

The development is done in one country, and QA is all done in another country half-way around the world, so a round-trip communication about a bug always requires 24 hours, and at least one of the people involved is discussing complicated technical problems in a language they're not proficient in.


答:
Using IP address as identifier is generally not recommended, as it is not what IP address is meant for - functionally it is a plain address for routing from a to b, and it tells nothing what is before a or after b.

In example, same IP address may be shared by a number of natted devices, most common cases being

a) a provider dynamically assigning a pool of addresses to its customers, that is quite common as buying the same quantity of public adresses you can serve more customers (you need just enough addresses for simultaneous users, not total users)

b) a private network accessing the web from a single address, internally redirecting packets to hundreds or thousands if machines


问:
Should we only catch in exceptional circumstances?
Whether error handling by throwing exceptions is good or bad is contentious.

Are exceptions as control flow considered a serious antipattern? If so, Why?

The common line is that exceptions are for "exceptional circumstances". But what if a library author has decided to throw an exception? The library author cannot predict what might be an "exceptional circumstance" for us.

Of course, if we cannot rule out an exception being thrown from a function that we must use as-is and permitting the exception to bubbling up to the top is unacceptable then we must catch the exception.

Often however, we can take preventative measures. With sufficient effort we might even be able to guarantee that the conditions under which the library function would throw an exception will never occur. The throw will still be there (it's not our choice) but we can eliminate the need for try and catch.

An example is std::map.at(). (Pretend there's no std::map.find().)

What is the general wisdom? Is it in good taste to write code with the expectation that a library function will occasionally throw? Is client code obligated to minimize the use of exceptions?

Edit
To try and distinguish this more from past questions and be more concrete, suppose we have some library function that might throw a file not found exception. Some client code wants to use the library to access a file. The client code is being programmed with the expectation that the file sometimes will not exist, even under "unexceptional circumstances". The client code can be made able to check if the library function will throw an exception before being called but it's non-trivial to do so. Should client code pile additional error checking on top of the error checking within the library or should the client code use the library generated exception as control flow?

In such a case adding error handling code on top of the library will almost certainly be less efficient than catching an error, will require less coding, and it could be argued that you must have a try/catch anyway. On the other hand it is a decidedly unexceptional control flow mechanism.

答:
The common line is that exceptions are for "exceptional
  circumstances".

Well, it's wrong.

Catching is certainly something you might want to do rarely, but exception throwing is not for exceptional circumstances at all. You should throw whenever a run-time condition means that you cannot fulfill your contract.

There's a big middle ground between "Using exceptions to replace if" and "Exceptions are only for exceptional circumstances".

Oh, and contracts that mean "Every caller must check for error every time", like return codes, are vastly worse than "Using exceptions to replace if".


答:
Handle exceptions you can handle and let others bubble to the top.

When a library-function throws a FileNotFound exception and you have a way to gently handle that exception. Do so, but the same function might also throw a IAmGoingToCorruptYourMemoryInEvilWays exception which you can't handle then you let it bubble to the top.

The common line is that exceptions are for "exceptional
  circumstances". But what if a library author has decided to throw an
  exception? The library author cannot predict what might be an
  "exceptional circumstance" for us.

In my opinion you should read it as exceptional circumstances within a system. And for the library function the system is small and the act of trying to read a file that isn't there is exceptional.
In the larger system where the library function is used, the file not being there may very well be something that's to be expected, so you deal with the FileNotFound exception in that system.

Title: Should we only catch in exceptional circumstances?

Body:
Whether error handling by throwing exceptions is good or bad is contentious.

Are exceptions as control flow considered a serious antipattern? If so, Why?

The common line is that exceptions are for "exceptional circumstances". But what if a library author has decided to throw an exception? The library author cannot predict what might be an "exceptional circumstance" for us.

Of course, if we cannot rule out an exception being thrown from a function that we must use as-is and permitting the exception to bubbling up to the top is unacceptable then we must catch the exception.

Often however, we can take preventative measures. With sufficient effort we might even be able to guarantee that the conditions under which the library function would throw an exception will never occur. The throw will still be there (it's not our choice) but we can eliminate the need for try and catch.

An example is std::map.at(). (Pretend there's no std::map.find().)

What is the general wisdom? Is it in good taste to write code with the expectation that a library function will occasionally throw? Is client code obligated to minimize the use of exceptions?

Edit
To try and distinguish this more from past questions and be more concrete, suppose we have some library function that might throw a file not found exception. Some client code wants to use the library to access a file. The client code is being programmed with the expectation that the file sometimes will not exist, even under "unexceptional circumstances". The client code can be made able to check if the library function will throw an exception before being called but it's non-trivial to do so. Should client code pile additional error checking on top of the error checking within the library or should the client code use the library generated exception as control flow?

In such a case adding error handling code on top of the library will almost certainly be less efficient than catching an error, will require less coding, and it could be argued that you must have a try/catch anyway. On the other hand it is a decidedly unexceptional control flow mechanism.


问:
There's a most performant way to check that a collection has exactly 1 element?
I came up with this solution :

```<code>if (Take(2).Count() == 1)
</code>```

is there any more performance solution (or better syntactical sugar) to do this check ?
I want a performance way because this will be an extension used on Linq To Entites and Linq to Objects.

I'm not using SingleOrDefault because that will throw and exception if it has more than 1 element.

Based on @Telastyn answer I came up with the following:

```<code>public static bool HasOne<T>(this IEnumerable<T> enumerable) {
    var enumerator = enumerable.GetEnumerator();
    return enumerator.MoveNext() && !enumerator.MoveNext();
}
</code>```

another implementation (slighly slower but 100% sure will work effectivly on Linq to Entities) would be :

```<code>public static bool HasOne<T>(this IEnumerable<T> enumerable) {
    return !enumerable.FirstOrDefault().Equals(default(T)) && !enumerable.Skip(1).Any();
}
</code>```

I'm not sure if the MoveNext one works with IQueryable on Linq to Entites. (any takers? I don't know how to test that)

After some test, Take(2).Count() == 1; is the fastest. :S

答: Yes, making your own extension that returns false on the second successful MoveNext (or none) should be the most performant implementation for IEnumerable. Anything with a Count property should just check if it is equal to one.


问:
Open Source Service Discovery
I'm trying to wrap my head around this article on OSS service discovery and I'm having a tough time seeing the forest through the trees.

In that article, the author poses the main problem for service discovery:

The problem seems simple at first: How do clients determine the IP and port for a service that exist on multiple hosts?

But I'm not even sure what this means/implies. When talking about service discovery, what exactly are we talking about here? Is this like, if we want to connect to a database, we might need a host and port number defined somewhere, like:

```<code>host=mydatabase01.example.com
port=9300
</code>```

??? Is this what the author is talking about, and is what is implied by "service discovery"?

If so, wouldn't the (obvious) solution be to just put a web service in front of everything? That way, clients don't care about what specific host/port they're connecting to, they just make RESTful calls to, say, http://my-data-service.example.com.

It can't be this simple, as that article goes on to talk about things like ZooKeeper and Eureka, which seem to be very complex beasts. I'm clearly missing something; if so, can someone provide a specific, concrete use case of what is meant when we talk about "service discovery"?

答:
This article is discussing distributed services. A popular example is a peer-to-peer (P2P) file sharing network. Nodes connect to a swarm to share data and may drop out at any time.

The article discusses several open-source "registries" which allow clients to connect to the network and announce their presence so they may use the services on the network. Potentially, depending on the application, the client itself may become a network resource as well. In this context, "network" means a finite number of client systems that are communicating. They may be on different physical networks: this is similar to a P2P distributed system. They may on the same physical network: this is similar to a corporate intranet.

How do these clients know about each other? If I sit at my desk and say "I would really like to print to printer X, or share files with computer Y" how do I get from staring at my cup of coffee to actually performing those actions? In a traditional corporate environment there will be network domain servers that handle this. Maybe MIS tells me an IP address. But what if those systems are not on a corporate or home network? Somewhere out there, in the ephemeral Internet, is a computer to which I want to connect. This is the problem the author is trying to solve: how do I find and connect to those clients?

Service discovery may occur in one of several ways, and this is by no means an exhaustive list. Each protocol is different, and new protocols are invented on a regular basis (check the ACM Digital Library if it is available to you, there is a lot of info on this in there)

Rely on a central server to manage connections and disconnections. This is similar to a P2P tracker file or Kerberos, and is close to the example you provided in the question.

Broadcast on a subnet looking at a specific port. If you use SMB or CIFS for home file sharing, it is similar to this.

Preconfigure other client IP addresses to talk to. Those clients may give you other clients that they are attached to.

Rely on another protocol such as DNS to provide IP addresses.

Once connected to such a network, there is a wealth of follow-on questions that are beyond the scope of this question, but if you find this interesting, you might want to dig into. Check out distributed computing.

References

Here are some of the papers I read that might help understand this topic a bit more. Note that you will need access to the ACM Digital Library:

Managing update conflicts in Bayou, a weakly connected replicated storage system

Disconnected Operation in the Coda File System

Flexible, Wide-Area Storage for Distributed Systems with WheelFS

Title: Open Source Service Discovery


答:
Three more reasons to add:

Multiuser workstations and terminal servers exist.  Many users could be running completely independent browser processes in separate sessions.

IP addresses aren't persistent.  It could be reassigned when a DHCP lease expires.

The application should support roaming.  For example, a user on a phone might drop out of WiFi range and get handed off to a 3G connection.  The IP address would change, but it would be nice to let the web application keep working.


问:
City exploration algorithm
The purpose of the algorithm is to create n routes on a geographical map, where n is being given, whereas all routes take no longer than t time units by foot and end where they start, while trying to have as little overlap as possible, meaning the user walking the routes walks on as few streets twice or more often as possible.

The software is to calculate closed routes (starting point = ending point) while the length of the routes should equal the time t. Another restriction is that each street should be walked along as few times as possible to always explore new areas while walking for the same time period.

Example:

You are in New York City (google maps for an example map)

You want to walk for 40 minutes.

You want to be on new roads or paths as often as possible.

Picture having a thread. The thread has a length of velocity_walking * t in a smaller scale. Now you glue the end and start to a starting point on your map, lat and lon. Then you move the thread so it is only on streets/walking paths, without being on the same street twice.

Now you take another thread and glue the end and start to a starting point on your map, lat and lon. Now you do the same thing as above, trying to not put it over the other thread.

You repeat this n times, all in all, so given the first two threads, you repeat it n-2 times.

So the steps are

Enter n

Enter t

Enter the starting point in lat and lon

Create n routes that take t time units to walk by foot, start and end in lat,lon and if possible don't contain the same streets or parts of streets twice.

My idea:

Having a circle of radius equaling walking_velocity * t / 2. Plan routes from start to point on circle to start.

Disadvantages:

Same streets to the circle's circumference and back.

Many streets are omitted.

What is the better algorithm to do this? Language does not matter, algorithm matters.

答:
This might not be the most efficient, but it would be a good starting point. I think any better algorithm would be an optimization or simplification of this one.

Set up the problem

Convert the geographical map into a graph. Intersections are nodes, streets or other footpaths are edges.

Each edge has a cost which is the time required to traverse that edge.

Each edge tracks the number of times it has been traversed.

Pick a starting node.

Choose a desired time (cost) at which a route terminates.

The algorithm will have a running total of the cost accrued from traversing the previous nodes. This cost starts at zero.

Traversing the graph

At any given node, evaluate the edges connected to it. If the cost of an edge plus the current accrued cost is greater than the terminating cost, that edge is removed from consideration. If the algorithm backtracked from an edge, remove that edge from consideration.

If there are no edges to consider, backtrack and remove the cost of the backtracked edge. If the total cost is zero (i.e. at the start node with nothing traversed), the algorithm terminates.

Of the remaining edges, pick one to traverse and repeat the algorithm.

Analysis

This is essentially a basic search tree algorithm which includes costs for each edge. Note that it also does not forbid traversing forward backwards, meaning going from node A to B and back to A without backtracking. That might actually increase the total amount of distance covered if B is a dead end and the cost is low compared to other edges.

Title: City exploration algorithm


问:
How do you make decorators as powerful as macros?
Quick background: I am designing a Pythonic language that I want to be as powerful as Lisp while remaining easy to use. And by "powerful", I mean "flexible and expressive".

I've just been introduced to Python function decorators, and I like them. I found this question that suggests decorators are not as powerful as Lisp macros. The third answer to this question says that the reason they're not is because they can only operate on functions.

But what if you could use decorators on arbitrary expressions? (In my language, expressions are first-class, just like functions and everything else, so doing that would be fairly natural.) Would that make decorators as powerful as Lisp macros? Or would I have to use syntax rules, like Racket and Honu, or some other technique?

EDIT

I was asked to provide an example of the syntactic construct. For an "expression decorator", it would simply be a decorator on an expression (where "$(...)" creates an expression object):

```<code>@Decorator("Params")
$(if (true):
    printf("Was true")
else:
    printf("Wasn't true"))
</code>```

Also, if I do need to use syntax rules, as mentioned above, it would use a pattern matching syntax:

```<code># Match a bool expression, a comma, and a string.
macro assert(expr:bool lit string) { e , s }:
    if (!e):
        error s
    else:
        printf("All good")
</code>```

The above would match this:

```<code>assert i == 0, "i should have been 0"
</code>```

That's a rough sketch, but it should give you an idea.

答:
Short answer: You can't.

Not in Python or anything close to what it is today, at any rate. The reason is straightforward: Functions are objects. Expressions are not. Expressions describe activities--the activity of operating on objects and producing a result. While the input and output of expressions are objects, the expressions themselves are not. Because they are not objects, they cannot be decorated.

This limitation on Python metaprogramming is pretty baked into the language. It's not impossible to change Python semantics. Modules like forbiddenfruit let you monkeypatch built in objects in ways that are officially "impossible." Modules like MacroPy, karnickel, and the now apparently defunct MetaPython do provide some macro facilities through AST rewriting and related techniques. None of these is certified for production use, however, and most of them pop up, support one or two language iterations, and then evaporate. The double-back-flip-with-twist moves necessary to make them ever work at all just are not sustainable over any period of time, in any generality, or in the context of "code you can depend on."

There are modules that successfully use introspection and reflection to inspect what is currently happening in a program. My say module, for example, works well across many Python 2 and Python 3 versions, and even alternate Python implementations like PyPy. But few Python meta-modules can actually change program semantics. GvR has been pretty resistant to metaprogramming facilities. The with statement semantics, for example, were constrained for the particular purpose of limiting how much metaprogramming it can support.

But, that's Python as it exists today. You might imagine your Python++ language having arbitrary code blocks (the mythical multi-line lambda, often mentioned but never actually seen in Python) that can be decorated. Lambdas are objects, and code blocks are very object-ify-able. You probably would not want the overhead of making every expression into an object, but it's not hard to imagine an environment in which any expression that was interesting to encode in a block could be done so, then decorated.

Two standard (i.e. non-Lispy) and quite Pythonic languages that have worked to integrate metaprogramming and advanced macro facilities are Julia and Nim (formerly known as Nimrod). The way they address program representation and transformation are very powerful, and would be good study points for your design.


问:
Are there examples of non CRUD approaches?
I'm a programmer but also have worked as an archivist. As archivist it's a lot about keeping data.

I often get into arguments with colleagues when it comes to operations on data. I don't like the U and the D in CRUD too much. Rather then update a record I prefer to add a new one and have a reference to the old record. That way you build a history of changes.
I also don't like deleting records but rather mark them as inactive.

Is there a term for this? Basically only creating and reading data? Are there examples of this approach?

答:
Yes its quite common in enterprise systems there are basically two approaches:-

"bi - temporal" in which every record has a valid from and valid to time stamp (the "current" record having a valid to date of "forever" - null, "9999-12-31" or some such high value).
Records are never deleted instead the "valid to" date is set to the current time and in the case of an update a new record is inserted with a valid from of the current time and a forever valid to date.

"history table" -- every time a record is altered a copy of the old record is dumped in a history/log table with a timestamp for the event.

There are great variations in granularity for both approaches. e.g. If the quantity of widgets on an order item is changed do you maintain history for the entire order or just that one item?

Generally speaking "bi-temporal" is a lot of extra work and only worth it if you have a lot of use cases like "auditor gets order status as of 31st December".


问:
Phone Number based Login
I'm trying to implement a phone number based login and not sure if I should ask the users for full number, i.e country code + number or just the number.

What I have in mind so far:

Attempt optimistic login using whatever number user gives.

Most likely, it'll come back with a single user match.

In case there is a collision, throw an error (and show some helpful clue to the user asking them to enter full number)

I guess this is a simplistic approach with minimum friction and complexity (as opposed to IP detection, geography based resolution etc.).

答:
on sign up have a country drop down with the dial code +1 US, +44 UK, +55 Portugal...

Validate the number to the user by sending an SMS with a validation code, expensive but important.

Allow login by just the CNS - local number as the DB can match the password, if you have 2 numbers both different country codes the password will match.

If you wanted to do extensions use a / slash . point or # to specify.

Don't store the number in any format, all local format a cns or number in its full entirety is 441233661666 but the login will accept just 01233661666 as any trailing 0 is ignored.

Title: Phone Number based Login


问:
How does code produced on an open-source framework is affected by it's original license (MIT in this case)
I'm looking into building a new PHP commercial app for a client that will be hosted as a paid service and possibly resold to third parties with a non-compete clause.

I've tested a few frameworks and chose Laravel which is licensed under the MIT License. The framework itself will not be modified but the app will be heavily dependent on Laravel functions and architecture.

How does the original license affects code produced with the software it's covering? MIT being a non-viral license, my humble guess is that my code can be distributed under a more commercial-friendly license that prevent third-parties to do anything else than using the app as a final product as long as the original licensed is kept for the framework part.

Am I wrong? How should I do this?

答:
As Wikipedia explains:

It is a permissive free software license, meaning that it permits reuse within proprietary software provided all copies of the licensed software include a copy of the MIT License terms and the copyright notice. Such proprietary software retains its proprietary nature even though it incorporates software under the MIT License.


问:
Difference between reverse lookup tables and rainbow tables
Using Reverse Lookup Tables, you create a lookup table consisting of the password hash of user accounts. Then you use another table which consists of hashes with guessed passwords. Then you compare the two to see if the hashed password of compromised user account matches hashed password in lookup table.

Using Rainbow Tables, it appears to be the same technique except the lookup table is smaller so you can search through them quicker.

What is the real difference between reverse lookup tables and rainbow tables?

答: Wikipedia explicitly calls out the difference between a rainbow table and a "simple lookup table" as being multiple entries per hash vs. one entry per hash. See the opening paragraph.</p>


问:
How to implement a no-login authentication system
I am looking to build a very loose authentication system that can track a user and link submitted data/comments to a specific user.  The submissions are essentially anonymous, but a user may want to edit his submission/comment at a later date.

I want the experience to be as smooth as possible so do not want to ask users to sign up for an account and then login each time.  There is no point as their submissions are not in their name and to another user browsing the site, there is no way of linking a submission to a specific user (think anonymous comments on a blog post or pastie).  However, the user should have the ability to edit (at least in the short term) the content they have posted.

The way I imagine doing this would be to place a unique identifier in a cookie on the users machine.  This would enable me to link a submission to a user, and while that cookie remained on the users machine, I would allow them to edit their content.  Of course, if the cookie is lost, or the user accesses the site from a different browser, then they would not be able to edit their content, but this is not really an issue, they can always resubmit a new piece of content.

Is there a better way of doing this?  How can I implement this so that the user can edit their data for the longest possible amount of time.

答:
You may want to look a SQRL. This is a rather new login mechanism which provides exactly
what you look for: An anonymous, repeatable login for a user, using a stable (per server) user ID.

You may want to associate a user name, password, Address etc.., to this user ID but this is all optional.

SQRL is under active development by security expert Steve Gibson at grc.com.


问:
Using DBUS on Android
I would like to know if it is possible to use DBUS on Android using Java. From what I have been searching till now it appears like there isn't any DBUS programming support on Android. However, Bluetooth on Android uses DBUS. I would like to confirm if it is actually the case. If so, is there a possibility that DBUS would be supported in the future?</p>

答:
As  dbus is written in C, and Android supports (mostly) the Dalvik subset of Java its not surprising that dbus is not supported.

You can write C code for Android but unless you are a phone company (i.e. able to install OS level updates) there is no practical way to distribute your software.

Title: Using DBUS on Android


答:
To me it's all a matter of abstraction layer consistency: if you throw something you catch yourself in the same context you are probably abusing. If you are throwing because of something you -at your actual level- don't know how to handle, throwing is the right way to go.

Who called you probably known WHY did he called, and hence knows also what to do if something goes wrong.

Now, about the catch: the correct place to catch is the one at with the reason for the error can be understood and the problem can gently be solved.

Suppose you are writing a function to manipulate a file: if the file is not there or if you find it is not in the expected format, since it was not you who supplied the file, you cannot know why it happened. So, instead to attempt a workaround yourself (that can be not what your caller intended) just throw reporting the problem.

Suppose you are he one that calls a function that has to manipulate a file: if that was you who gets the name of the file from the user, and the file is found as being not there or not being as expected, you should catch and ask again and eventually admitting an answer like "do nothing and let me go".  If the name of the file was instead already given to you from somewhere upside, you should let the exception go up.

Another typical case is a function requiring a container to grow. If the memory allocation fails, the allocator will throw. The container- at that point- has to react to put itself in consistent state (no dangling pointers or half-made internal structures), but has to re-throw since it cannot solve the problem of the missing memory.

The catch should probably happen in a place when some other memory can be dismissed (an the operation re-attempted), or the user be informed of the problem to give it a chance to gain more memory for the actual task (for example by closing some other task or application) and reattempt the failed command.


问:
Dual pivot quicksort in face of expensive swaps
I was told this is better place to ask this

TLDR

Has anyone tested dual pivot quicksort performance with expensive-to-swap elements? It seems that in this case, it should massively underperform compared to standard quicksort.

And yes, I know about Cycle Sort (if it is only the original array that is expensive to modify) and that I could use indices/pointers inside the array, sort them and then swap them into their correct place.

However, the first is completely out of the question (Quadratic average case is just not good enough) and the second is unsuitable for implementation of general case sort. (It imposes both performance and memory consumption overhead even in cases where it is better and faster to work with the original array).

Backstory

Inspired by recent "question" on stack overflow, I decided to go and implement non trivial versions of given sorts (introsort, quicksort with 3-way partition, median of 3 pivot selection, small block insertion sort etc).

During some research I also came upon dual pivot quicksort, which is the current implementation of quicksort in Java standard library. Generally it claims that it is always at least as good as standard quicksort, and empirical testing seemed to support it. (Which is the reason it is the current implementation.)

However, it seems that no STL implementation uses dual pivot quicksort for the quicksort phase of introsort, which made me wonder why. After more research I found this paper. It says that while dual pivot quicksort performs on average 5% less comparisons, it performs significantly more swaps. (Approximately 80% more) Obviously, since Java has only primitives and reference types, swapping is always cheap. (Even so, it uses this sort only for primitives, because it is not stable)

It also seems that at least part of the advantage of dual pivot quicksort is in its improved cache behaviour (Because it divides into smaller subarray that can fit into cache faster).

So I wanted to see whether someone already tested standard quicksort vs dual pivot quicksort when elements are expensive to swap and has the numbers (and possibly source) lying around, or whether I will have to test this myself.

答:
The answer is probably not, because it's reasonably obvious that it wouldn't matter except in rare cases.

Assume that the reason that swaps are expensive is that you are sorting objects that are large, contained in a database or accessed through an API. Regardless, those objects have keys and (by your statement) keys carry enough information that they can be compared cheaply.

Simply ensure that each key contains a reference to the underlying object. If necessary attach a pointer or index to each key. Then sort by any available method -- it obviously doesn't matter because the swaps are the expensive part.

Now perform a chase-your-tail swap on the sorted data. Start with the first key, swap it into the first position, swap the object that was there into its position and so on. You will have performed exactly the minimum number of swaps required to sort the data.

In other words, for expensive swaps there is a simple algorithm that ensures making the minimum swaps, that works regardless of sort algorithm. The only sort algorithms we care about to analyse are the ones where comparisons are expensive.


问:
The Observer pattern using the pulling mechanism
I was wondering about the following implementation of

```<code>public void update(Observable obs, Object arg)
</code>```

while I want to send to all of my observer and update using notifyObserver() I and passing a references to this the observer can use the getters from the subject to pull the information he wants.

What is the arg argument on the update method meant for?

答:
When implementing the Observer pattern, there are two main approaches to consider: the 'push' model and the 'pull' model.

In the 'push' model, the subject (i.e. the Observable) sends the observer on notification all the data it will need. The observer doesn't need to query the subject for information. In the 'pull' model, the subject merely notifies the observer that something happaned, and the observer queries the subject based to get the information it needs.

Let's discuss the pros and cons of both approaches:

Push

The main advantage of the 'push' model is lower coupling between the observer and the subject. The observer doesn't need to know anything about the subject in order to query it. If it needed to, we'd need to do one of the following: A- do downcasting on the side of the observer in order to invoke class-specific get methods on the subject. This is bad. B- make the Observable interface more class-specific, offering specific get methods, thus making the relationship between the observer and subject less general and making things more copupled.

By implementing the 'push' model, we avoid all of this.

However the disadvantage is less flexibility: the subject may not always know what exact information the observers need in order to send it to them. This will often mean more specific Observer intefaces, such as AgeObserver that are notified when the 'age' of the subject is changed, and HeightObserver which are sent the current height of the subject on notification.

This is one option. The other is the subject sending a whole lot of information encapsulated in an Info object of some sort and have the observers query it from there. But again, we can't be sure we're sending the correct info. So it's either this, or forcing the observers to implement more specific Observer interfaces, which tightens the coupling on the observer's side.

Pull

I already noted the disadvantages of the 'pull' model. The observers would have to know things about the subject in order to query the right information, which leads A- to downcasting (ugly), or B- favorably to more specific Observable interfaces, that offer more specific accessor methods. For example AgeObservable offers a getAge() method.

The advantage of this is more flexibility. Each observer can decide for itself what to query, without relying on the subject to send the correct information.

You should choose the strategy that is better for the specific project you're working on.

In reality, you'll always have specific Observer and Observable intefaces, so you have some coupling between the sides anyway.

So choose either 'pull' or 'push' by what fits you best.

Do all AgeObservers need simply the age of the subject? Implement the 'push' model. Less coupling and simpler.

Do all HeightObservers need varying information about the subject - aka one needs to query the age too, and some other object needs to query the weight in addition to the height? Implement the 'pull' model. This would force you to add accessors (getters) to the Observable inteface (either this or pass the actual object as a parameter in it's explicit type, but doing this through the interface allows you to deny the observers access to things that don't matter to them). This soultion creates higher coupling, but it's more flexible.

Choose what fits your situation best.

ContentLicense: CC BY-SA 4.0


问:
What's the most absurd myth about programming issues?
To put it another way... What is the most commonly held and frustrating misunderstanding about programming, you have encountered?

Which widespread and longstanding myths/misconceptions do you find hard for programmers to dispel/correct.

Please, explain why this is a myth.

答: Porting a program to C++ will automatically make it run faster.</p>


问:
Separating roles authorization and database
Something really bothers me about the way authorization tends to be done with roles in ASP.NET MVC.

The way it is normally done is that you have a Users table and a Roles table. A User can have many Roles.

Then you decorate your controllers/actions with an authorize attribute saying which roles you want to allow access to the controller/action:

```<code>[Authorize(Roles = "Admin")]
public ActionResult AdminOnlyAction()
{
}
</code>```

The problem with this is that your code is reliant on data within the database.

Obviously you could, at application start, seed your database with the roles needed if they're not present but there's still a fragile dependency on the data in the database (e.g. it could be deleted, renamed, etc. etc.).

Having an enum or similar with the Role levels in instead of storing them in the database seems reasonable but still leaves me with a sour feeling.

Is there an elegant alternative which I've not hit upon?

答:
```<code>Obviously you could, at application start, seed your database with the roles needed ...
</code>```

I would suggest that you should be doing this at installation time, not application start-up.  A "user-level" connection probably shouldn't have permission to make that sort of change anyway.

```<code>... there's still a fragile dependency on the data in the database ...
</code>```

What's "fragile" about it?  It's no different to [pretty much] every other application ever written that uses a database.  If the table structures aren't set up the way that the application expects, it crashes and burns.

Surely the important issue here is that the relationships between Users and Roles live outside of your code.

The access decorators that you add to your code are static.  You mark a method as "Administrators only" and that's that.

The association of a User with a Role is dynamic.  Different people will hold different roles over time; that's all captured in the database and your code doesn't change.

Body:
Obviously you could, at application start, seed your database with the roles needed ...

I would suggest that you should be doing this at installation time, not application start-up.  A "user-level" connection probably shouldn't have permission to make that sort of change anyway.

... there's still a fragile dependency on the data in the database ...

What's "fragile" about it?  It's no different to [pretty much] every other application ever written that uses a database.  If the table structures aren't set up the way that the application expects, it crashes and burns.

Surely the important issue here is that the relationships between Users and Roles live outside of your code.

The access decorators that you add to your code are static.  You mark a method as "Administrators only" and that's that.

The association of a User with a Role is dynamic.  Different people will hold different roles over time; that's all captured in the database and your code doesn't change.


答:
Java went much further and made Exceptions part of the method contract and, IMHO, that's good.  It focuses your thinking about exceptions.  When you call a method, you know exactly which exception(s) it might throw, because the method tells you what they are and the Java compiler insists that you deal with them - catch them and handle them, or change your method contract, declaring that "you" might throw these exceptions to your callers.

Because it doesn't explicitly do this, .Net has encouraged a lot of lazy, woolly thinking about exceptions, leading to all of the confusion already mentioned by others.

A method should throw an exception when it's asked to do something it can't.

Defensive programming can only give you so much protection - you can "pre-test" conditions as much as you like, but eventually you just have to call "that method" and you can still get "strange things" happening (system exceptions, race conditions, etc., etc.) between your last check and actually trying to do the work.

You should only catch an exception when your code can do something useful about it.
Far too many Developers "finish" their programs and then ask about "Global" exception handlers - catching and logging the [whole!] exception to a file just before the application curls up and dies.  OK, it's a backstop, but it's only "useful" up to a point.  Catching that FileNotFoundException and trying to read a different file, all without the user knowing it's happened, is perhaps a little more "useful".


答:
What is the general wisdom? Is it in good taste to write code with the expectation that a library function will occasionally throw?

I think you do not understand how the exceptions are used.

What is important to understand is : you can not ignore exceptions. In a normal program execution, an exception must not occur.

If it must not occur, then why use exceptions?

to catch a bug - someone implemented something, that collides with other part of the software. Maybe set a constant wrongly.

to catch invalid parameters - if the user enters wrong value, which is not checked, what do you do? Maybe something out of range, or string instead of a number.

In such a case adding error handling code on top of the library will almost certainly be less efficient than catching an error, will require less coding, and it could be argued that you must have a try/catch anyway.

Again, proves that it comes from a person not knowing what exceptions are.

Exceptions are thrown at places where the error occurs, and caught at places where you know how to deal with the error. This separates the program execution into two :

normal execution path

error propagation path - when an error happens

This way you can concentrate on the normal execution of the program, ignoring the error path. When an error happens, throw an exception, and move on.

Since, you do not need to check error code, your program will be smaller, more efficient, and easier to maintain and unit test. Last two items are specially good for developers.

Performance wise, program with the use of exceptions will be better or same then the program without exceptions, because of zero-cost mechanism. Only thing that is paid, is a bit increase in the program/library size increase.

Of course, throwing an exception is expensive, but it doesn't really matter. If the program can not continue, then it doesn't matter how long it takes to resolve the error. On the other hand, with error codes, you pay for every check.


问:
GIT workflow for web development
Long ago the small team of web developer I work with started using git for web development. Back then we just committed to staging or master directly and then merged frequently between the two. It was better than nothing, but it was also a mess.

Not too long ago we adopted the gitflow work flow.  While it's certainly better than the chaos that came before it seems somewhat cumbersome and excessively release/milestone oriented. My fellow devs are frequently ask me to clarify how it's supposed to work and what should merge and shouldn't.  In general it seems ill suited for web development work where we're deploying code frequently and without tracking specific milestones for release.

On a friends recent suggestion I've begun looking at GitHub Flow.  Reading Scott Chacon's post here hits the pain point perfectly with this:

So, why don’t we use git-flow at GitHub? Well, the main issue is that we deploy all the time. The git-flow process is designed largely around the “release”. We don’t really have “releases” because we deploy to production every day – often several times a day.

FWIW, I've also looked at this nice round up of workflows on Atlassian's site:
https://www.atlassian.com/git/workflows#!workflow-feature-branch

However they ALL look like poor choices for web development in a small team and again geared towards major application releases not frequent/daily releases.

The is a question over on SE asking to compare git-flow to github-flow
https://stackoverflow.com/questions/18188492/what-are-the-pros-and-cons-of-git-flow-vs-github-flow

That's a good answer in general, but as I mentioned in my comment below meta.programmers.SE seems to indicate that questions about general best workflow practices belong here and I was hoping for a broader list of possible answers than just git-flow and github-flow, while being specific to web development.  Hence I think it warrants a new question here.

With that, what do you find is the best/prefered git based workflow for a small web development team working on projects with fairly continuous deployment?  Is it github-flow or something else?

答:
First I'd like to make a little summary of the different workflows that you've looked into and you think are not suitable for the kind of development you're working on:

Centralized (Source): Pretty much like SVN workflow but now on a distributed environment. Every developer works on a personal copy of master and pushes changes to origin/master directly or via pull request.

Feature branch (Source): Well, that. Every developer working on a particular feature should work on a specific branch dedicated to that feature only. This feature branch should be created from master or from another feature branch. Eventually everything gets merged back to master.

Gitflow (Source): Two main branches track a project history, develop and master. Another 3 branches called hotfix, release and feature hold changes made directly to master for fixing critical production bugs, change version number and other details prior to a release or work on a particular feature just like Feature branch, respectively.

GitHub flow (Source): Developers create a feature branch off of master. Changes are pushed via pull request. Changes accepted into master get deployed immediately by GitHub bot Hubot.

For the development part of your question, the answer depends on the background of your team. Do they come from an SVN environment? Then you should go with the centralized approach since it's the one that resembles SVN most. Do they feel comfortable working with Git? Then perhaps you shouldn't try to adapt your team's workflow to any of those but implement your own, crafted to suit your needs which if I understood well are development flexibility and fast deployment.

I also think you should focus on improving the latter. How is your deployment pipeline composed of? In "Continuous Delivery: Reliable Software Releases through Build, Test, and Deployment Automation" the authors identify possible causes for infrequent deployments, some of which are:

The deployment process is not automated.

Testing takes a long time.

People don't understand the build/test/deploy process.

Developers are not being disciplined about keeping the application working by making small, incremental changes, and so frequently break existing functionality

Do any of those sound like something you could improve? Perhaps you could tell us a little bit more about how you and your team handle this part of the project.

Title: GIT workflow for web development


问:
How to allocate cross-cutting concerns in agile software development planning?
My employer has been moving from traditional waterfall projects to a modified version of agile.  Work is broken into stories, which are estimated and sequenced into two-week long iterations.  Each iteration includes points for building, testing, and accepting features.

Stories are focused on testable business features.  There is no way to plan and account for cross cutting concerns such as design, architecture, infrastructure, or (ironically) project management.

Should agile teams explicitly include stories or tasks for these cross-cutting concerns?  How does your team handle tasks that never go away?  Are they "off book"?

答:
I've seen two basic approaches:

Add recurring, timeboxed tasks to each sprint for these kinds of issues. This increases the visibility of the tasks and allows checking whether a specific step or task is Done. The downside is more overhead cloning or recreating these tasks from sprint to sprint.

Just set aside a collective timebox for these tasks each sprint, reducing the available time for implementing stories in the sprint accordingly. This is simpler to manage, but makes it more difficult to manage the individual tasks included in the box.


问:
Best Practices For Temporary Scripts (Python)
I am running two separate programs which are similar enough that they share a lot of code. I run these programs often and after I evaluate the output. This is a very fluid process and everytime is different from the last time.

Currently I write a script named something like: "temp_evaluate_program_A" and then I will copy it to "temp_evaluate_program_B" and change a few lines, along with writing a lot of code on the fly. Often the code isn't very readable and only parts of it will ever be reused. This seems suboptimal as I get a lot of duplication.

What do other people do in these fluid situations where there is scope for sharing code between these related programs and reusing the code over time but the scope is limited.

答:
I have this problem when writing data manipulation scripts. Each does its own thing--but I'd like there to be as much commonality as possible, and I'd like each script to be as simple as possible.

A few suggestions:

Modules. Learn to package your reusable code into modules. Even among a collection of single-use, temporary programs there will tend to be
common tasks. You'll often need to read and write data from similar sources,
for example, and often in common formats. Great! We already have some functions that
can be coded into shared modules. In the simplest case, this can be just a
file shared.py (or whatever you'd like to call it) in the
same directory as your scripts. Then use from shared import * in each of your
scripts. As your modules become more sophisticated, you can learn to package them
so they can be installed as standard modules on your system, or even shared with
others. That is a bit of a learning curve; I recommend starting small.

External modules. Gradually learn to use code from other modules. If you're not
familiar with the Python Package Index, it's a great
source of pre-packaged functionality to do many things. Installing most code there
is a simple pip or easy_install command away. Over time you will find a handful
of modules from either the Python standard library or PyPI that greatly simplify your
work. Learn them and rely on them often. If you're working with Web pages, for
example, requests is a wonderful tool.
For dates and times, I find arrow very helpful.
And I'd (not so) humbly add my own say, show, and textdata modules
as other examples of modules that try to not just provide functions, but make
otherwise messy tasks simple, clean, and self-contained.

Common idioms. Even if you cannot reuse code directly, using common patterns
of coding will reduce your cognitive load
as you work. You will see the same patterns of usage time and again, becoming
a master of those idioms, and quickly comprehending the intent and effect of each
idiom.
Python is an especially good language for idiomatic
development, as has a strong community
ethos of using shared design principles (e.g. "the Zen of Python") and common programming styles (e.g. PEP-8).

Source Code Control. If you're using a lot of individual scripts, and especially if you're building many
of them ad hoc, you would benefit from source code control.
You might think for one-off coding, source repositories wouldn't have that much value,
but I've found quite the opposite. The more fluid your programming situation, the more
having some elements that provide discipline and structure becomes.
It lets you make mistakes,
screw up your code, then find out "what did I change?" And if you like, revert to what
you had previously.
I like Mercurial. It's easy to get going, and excellent tutorials are available.
Git is also popular and effective. Commit early; commit often.

If your individual scripts are idiosyncratic and do quite different things, then your opportunities for pure reuse are, if not minimal, certainly constrained. That doesn't mean there aren't opportunity for leverage. It turns out that leverage--the ability to get a disproportionate reward for the effort expended--is the real goal. Reusing your own code is one way of getting it. Using others' code and expertise (external modules), using high-productivity idioms, and providing some discipline and order in what is otherwise a messy situation--those are other useful mechanisms to get more out of each working hour.


问:
Has whitespace in identifiers ever been idiomatic?
C# style suggests using CamelCase in identifiers to delimit words. Lisp tradition suggests using-dashes-instead.

Has there ever existed a programming language where using spaces in identifiers was not only allowed, but a commonly used idiom when employing multi-word identifiers?

It's possible to have identifiers with spaces in some Scheme implementations, but it's not a widely seen practice. Here's an example:

```<code>Petite Chez Scheme Version 8.4
Copyright (c) 1985-2011 Cadence Research Systems

> (define |hey there| 100)
> (define |x y z| 200)
> (list |hey there| |x y z|)
(100 200)
</code>```

答:
Power Query uses a lot of automatically generated code. I'd guess more than half of generated identifiers use white space:

```<code>let
    Source = Sql.Database(".", "Test"),
    dbo_pvt = Source{[Schema="dbo",Item="pvt"]}[Data],
    #"Filtered Rows" = Table.SelectRows(dbo_pvt, each [VendorID] <= 4),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Emp1", "Emp2"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns", {"Emp3", "Emp4"}, {{"Count", each List.Sum([Emp5]), type number}})
in
    #"Grouped Rows"
</code>```

As you can see, like in many languages there's extra syntax to disambiguate what the identifier is.

But in places where it's unambiguous, no extra syntax is needed:

```<code>let
    spaceRecord = [with space = 42, recursive record = @spaceRecord],
    drilldown = spaceRecord[recursive record][recursive record][recursive record][with space]
in
    drilldown   // 42
</code>```

Body:
Power Query uses a lot of automatically generated code. I'd guess more than half of generated identifiers use white space:

let
    Source = Sql.Database(".", "Test"),
    dbo_pvt = Source{[Schema="dbo",Item="pvt"]}[Data],
    #"Filtered Rows" = Table.SelectRows(dbo_pvt, each [VendorID] <= 4),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Emp1", "Emp2"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns", {"Emp3", "Emp4"}, {{"Count", each List.Sum([Emp5]), type number}})
in
    #"Grouped Rows"

As you can see, like in many languages there's extra syntax to disambiguate what the identifier is.

But in places where it's unambiguous, no extra syntax is needed:

let
    spaceRecord = [with space = 42, recursive record = @spaceRecord],
    drilldown = spaceRecord[recursive record][recursive record][recursive record][with space]
in
    drilldown   // 42


问:
How to get a database on my filesystem that I can use in my application
I'm making a .NET application where I'll have a lot of data entries. This will not go to any server though, but needs to be stored and updated, frequently on the local machine.

I would like something like an SQL database, that simply exists in a file, in my application's folder (or in %appdata% or whatever).

But how do I go about this? What can I use? I'm a big fan of Linq2Sql, so if it supports that, it would be awesome.

I've tried to use MSSQL Compact, but I couldn't really get it to work very well. It basically required me to install MSSQL as well.

What options do I have, other than MSSQL Compact?

答:
The concept you're talking about is generally referred to as an embedded database (or sometimes, a serverless database).

There are many out there; I don't think lists are a good format for this site, but you could try SQLite, for example.  If you need a more fully-fledged RDBMS, something like Firebird embedded might be appropriate.


答:
First, if you are ensuring they are created -- and I'll disagree with Phill W. here and note that having an "ensure roles" task on startup is a great approach -- then they aren't fragile.

If end users are doing something like manually editing your database then that gets into warranty voiding tampering so things not working would be expected in my book.

Finally, you can build custom attributes that allow you to do more than hardcode Role=Admin. One app we did which was tied to windows had a pretty nifty custom mapping scheme where it had internal groups mapped to external NT groups so the admins weren't stuck with specifically named groups. This app would also create it's own NT groups if you'd let it.

Title: Separating roles authorization and database

Body:
Something really bothers me about the way authorization tends to be done with roles in ASP.NET MVC.

The way it is normally done is that you have a Users table and a Roles table. A User can have many Roles.

Then you decorate your controllers/actions with an authorize attribute saying which roles you want to allow access to the controller/action:

```<code>[Authorize(Roles = "Admin")]
public ActionResult AdminOnlyAction()
{
}
</code>```

The problem with this is that your code is reliant on data within the database.

Obviously you could, at application start, seed your database with the roles needed if they're not present but there's still a fragile dependency on the data in the database (e.g. it could be deleted, renamed, etc. etc.).

Having an enum or similar with the Role levels in instead of storing them in the database seems reasonable but still leaves me with a sour feeling.

Is there an elegant alternative which I've not hit upon?


问:
Running time of simple for-loops
I'm reading algorithms and I understand most of it, one thing that I can still struggle with a lot is something as simple as running times on different for-loops. Everyone seems to have easy with that, except for me and therefore I search help here.

I am currently doing some excercises from my book and I need help completing them in order to figure out the different running times.

The title of the exercise is: "Give the order of growth(as a function of N) of the running times of each of the following code fragments"

a:

```<code>int sum = 0;
for (int n = N; n > 0; n /= 2)
  for(int i = 0; i < n; i++)
    sum++;
</code>```

b:

```<code>int sum = 0;
for (int i = 1; i < N; i *= 2)
  for(int j = 0; j < i; j++)
    sum++;
</code>```

c:

```<code>int sum = 0;
for (int i = 1; i < N; i *= 2)
  for(int j = 0; j < N; j++)
    sum++;
</code>```

We have learned different kinds of running times/order of growth like n, n^2, n^3, Log N, N Log N etc. But I have hard understanding which to choose when the for loop differs like it does. the "n, n^2, n^3" is not a problem though, but I can't tell what these for-loops running time is.

Here's an attempt of something.. the y-axis represents "N" value and the x axis represents times the outer loop has been run. The drawings in the left is: arrow to the right = outer-loop, circle = inner loop and the current N value. I have then drawn some graphs just to look at it but I'm not sure this is right though.. Especially the last one where N remains 16 all the time. Thanks.

答:
The actual running time is less important than the growth of the running time as a function of the algorithm's inputs from the theoretical perspective of studying algorithms. Real world is typically different.

Regardless, the book is probably discussing running times as a way to illustrate different code structures.

A simple Java example for timing code looks like the following. Most popular languages have a similar way of accomplishing the same thing.

```<code>long begin = System.currentTimeMillis();
// Do some stuff
long interval = System.currentTimeMillis() - begin;
System.out.println("The algorithm took " + interval + "ms to process");
</code>```

Use this programming idiom for each code structure you listed and compare the results. That sounds like what the book is telling you.

Given how fast modern CPUs are and how effective modern compilers are at optimization, I would recommend looping millions of times to get an accurate comparison. That means the variable N in your code should be quite large. I would also run each algorithm several times and take the average.

The reason for that is there is overhead associated with any code. To have an accurate comparison you want to make sure factors such as cache misses are minimized.

Title: Running time of simple for-loops

Body:
The actual running time is less important than the growth of the running time as a function of the algorithm's inputs from the theoretical perspective of studying algorithms. Real world is typically different.

Regardless, the book is probably discussing running times as a way to illustrate different code structures.

A simple Java example for timing code looks like the following. Most popular languages have a similar way of accomplishing the same thing.

long begin = System.currentTimeMillis();
// Do some stuff
long interval = System.currentTimeMillis() - begin;
System.out.println("The algorithm took " + interval + "ms to process");

Use this programming idiom for each code structure you listed and compare the results. That sounds like what the book is telling you.

Given how fast modern CPUs are and how effective modern compilers are at optimization, I would recommend looping millions of times to get an accurate comparison. That means the variable N in your code should be quite large. I would also run each algorithm several times and take the average.

The reason for that is there is overhead associated with any code. To have an accurate comparison you want to make sure factors such as cache misses are minimized.


答:
Yes create stories for design, architecture, infrastructure and project management.

You may wish to create an additional board for them, at least initially.

Ultimately they involve people doing work so as they get developed you should be able to either assign people to them or move them into a team's project board.

One thing we do for more intangible stories sometimes is creating spikjes that are time-bound, e.g. "2 days" to do such tasks.

Title: How to allocate cross-cutting concerns in agile software development planning?

Body:
My employer has been moving from traditional waterfall projects to a modified version of agile.  Work is broken into stories, which are estimated and sequenced into two-week long iterations.  Each iteration includes points for building, testing, and accepting features.

Stories are focused on testable business features.  There is no way to plan and account for cross cutting concerns such as design, architecture, infrastructure, or (ironically) project management.

Should agile teams explicitly include stories or tasks for these cross-cutting concerns?  How does your team handle tasks that never go away?  Are they "off book"?


问:
Running Python script in background on Mac
I have a Python script that counts the number of file in a folder, and if the number of files exceeds 10 then it creates a popup window to ask the user to clean up the folder (I use tkMessageBox).

How can I have the program run in the background so that it consumes only minimal resources? I tried running it as a daemon and as an agent but I couldn't see anything happen (no popup window).

答:
Daemons, by definition, have no user interfaces. So that won't work.

What you probably want to do is simply run the program normally. Have it check if there are more than N (for you N=10, but let's keep it generic) files in a folder. If so, it should pop up a tkMessageBox warning the user. If not, it should sleep for CHECK_INTERVAL seconds. Anything from 60 seconds to 10 minutes would be a reasonable default, which would not consume many system resources.

You do not mention what platform you're running on, and you're using Tk, so you probably wish to keep this generic. If you had specific platform choices you could use a more platform-native notifier, which most GUI platforms now support (e.g. Windows, Mac, Ubuntu Unity).


问:
What should be in a coding standard?
What should be in a good (read:useful) coding standard?

Things the code should have.

Things the code shouldn't have.

Should the coding standard include definitions of things the language, compiler, or code formatter enforces?

What about metrics like cyclomatic complexity, lines per file, etc?

答:
I think there are actually two things to address, and I would in fact consider them separately because they cannot be approached in the same manner, though I find both important.

The technical aspect: which aims at avoiding risky or ill-formed code (even if accepted by the compiler/interpreter)

The presentation aspect: which concerns itself with making the program clear to readers

The technical aspect I qualify of Coding Standard, as do Herb Sutter and Andrei Alexandrescu with their C++ Coding Standards. The presentation I qualify of Coding Style, which includes naming convention, indentation, etc...

Coding Standard

Because it is purely technical, a Coding Standard can be mostly objective. As such every rule should be supported by a reason. In the book I referred to each item has:

A title, simple and to the point

A summary, which explains the title

A discussion, which illustrate the issue of doing otherwise and thus states the rationale

optional Some examples, because a good example is worth a thousand words

optional A list of exceptions for which this rule cannot be applied, sometimes with work arounds

A list of references (other books, websites) that have discussed this point

Rationale and Exceptions are very important, as they summarize the why and when.

The title shall be explicit enough that during reviews one only need to have a list of titles (cheat sheet) to work with. And obviously, group the items by category to make it easier to look out for one.

Sutter and Alexandrescu managed to have a list of only a hundred items, even though C++ is deemed hairy ;)

Coding Style

This part is generally less objective (and can be downright subjective). The intent here is to guarantee consistency, because this helps maintainers and new comers.

You do not want to enter a holy-war about which indentation or brace style is better here, there are forums for this: so in this category you do things by consensus > majority vote > arbitrary decision by leader(s).

For an example of formatting, see the list of options of Artistic Style. Ideally, the rules should be clear and complete enough that a program could rewrite the code (though it's unlikely you'll ever code one ;) )

For the naming convention, I would try to make class/types easily distinguished from variables/attributes.

It is also in this category that I classify the "measures" like:

prefer short to long methods: it's usually difficult to agree on what long is

prefer early return/continue/break to reduce indentation

Misc ?

And as a final word, there is one item that is rarely, if ever, discussed in Coding Standards, perhaps because it's particular to each application: code organization. The architectural issue is perhaps the most outstanding issue, screw up on the initial design and you'll be plagued by it years from now. You should perhaps add a section for basic file handling: public/private headers, dependency management, separation of concern, interfacing with other systems or libraries...

But those are nothing if they are not actually applied and enforced.

Any violation should be brought up during code reviews, and no code review should be okay if a violation is outstanding:

fix the code to match the rule

fix the rule so that the code doesn't stand out any longer

Obviously, changing a rule means getting the "go ahead" from the leaders.

Title: What should be in a coding standard?