问:
What makes the difference between "Hire" and an honest "almost" for final on-site interviews?
So, I have recently had on-site interviews with Google and Amazon and received polite rejection letters letting me know I was close, but not quite right for the skills they were looking for.

I've made it to the final round for all the interviews I've done (except for some offers from small uninteresting positions that I interviewed with for practice), but so far having 5-8 interviews in a day gives me enough time to have my mistakes add up just enough to put me out of the running.

I know I did well there at least on the coding questions and other general technical questions, apparently I'm bad at designing OOP things like card games or parking garages though (I dove too deep into one object and used up all my time, instead of being broader) and my coding answers although they work overall didn't quite had a few bugs/edge cases I missed (like a case where an input node could actually could be the answer rather than needing to be distinct). And I have no problem saying "I don't know", but maybe I'm rambling a bit and need to say it for questions I think I can answer, but can't give a crisp answer to...

So, what are the things that push you over the top from being good, but not quite to "Hire"?

Any advise on what you look for or something you know that gave you that little extra boost?

答:
Ok, just to throw in some practical experience here.

I work for one of these elite software firms, and I do not find our hiring policies to be geared to "not missing" great talent but to "not hiring" mediocre talent. I have seen that some of these companies really want to hire great people, but they do so by interviewing lots of really good looking (on paper) developers and then culling out the ones that they don't want. Once someone is hired, it is very difficult to get rid of them so it pays to turn down a candidate that you believe may actually be a great fit, but that one of the interviewers saw some red flags on.

At the company I currently work for, I was turned down because one and only one of the interviewers (the most important one) gave me a thumbs down. This interviewer asked me a very domain specific question and did not speak fluent english. They did not hire me, but the team thought that the company would be missing out on a potentially good hire. They sent me to another set of interviews with a different team the next week and I got the job (with "strong hire" marks I might add).

My advice is that if you really believe that you have what it takes, keep interviewing with this company and learn from each experience until you land the job. Most of these companies keep a registry of everyone they interview and they black-list the poor candidates (so they never get another shot). However, the candidates that were good candidates but just didn't perform well that day, or didn't fit well with the team will remain in the hiring pool. You will immediately know if you have been black-listed when the recruiter phone calls just stop one day and every future contact seems to hit deaf ears. If you receive future inquiries from the company, you know that you are fine. There is absolutely no harm in setting up more interviews after your first rejection as long as you weren't black listed. In fact, I would highly recommend interviewing with multiple teams all at once. The interviewers are going to reject you at the first perceived sign of trouble, whether or not it is a real trouble. They are cautious and don't want to make bad hires much more than they want to make good hires.

A few more thoughts:

--None of these companies are going to give you feedback. It is a legal liability. It sucks that this is the way it is, but I can promise you that it isn't going to happen.

--I personally talked to a brilliant engineer when I interviewed with Microsoft who told me it took him 5+ tries before he was finally hired. This guy was a senior level SDE, so MSFT obviously validated that he was a good hire by promoting him.

Some tips:

Know your data structures and algorithms backwards and forwards. You need to know everything all of the way up to graph traversals.

Know architecture, especially distributed systems and problems of scale

Have a list of projects you have led memorized. Have a list with examples of leadership principles you have exhibited in your job memorized. These are the most challenging questions to answer in the interview (behavioral interviews). You can be perfect in the tech side and if you don't survive the behavioral interview you will not be hired.

Don't worry about which programming languages they are looking for. Know one object-oriented language backwards and forwards and code in that. The interviewer doesn't usually care which language you code in and doesn't judge you based on it.

Finally, please email me your resume. ;=)


问:
Is the Adapter the correct design pattern for this situation?
I have an instance of a class UntouchableClass of which I need to access the member variables to use in my DotLiquid template. Problem is, UntouchableClass has to inherit from Drop if I want to use it in my template, but I can't change it since it is from an existing library.

I've read about the Adapter pattern, but UntouchableClass does not have an interface from which to inherit the Adapter.

Also, I can't use multiple inheritance (I would create a class which inherits from both UntouchableClass and Drop but this is C# and from what I know this isn't possible).

Is there a design pattern that would suit this situation? Or is there a particular way to do this using DotLiquid?

答:
+1 @Ben Aaronson, BTW. Good answer.

I view inheritance as as a form of implied composition, where the compiler is doing the busywork of composition for you. There are differences, and much different consequences, but as a result I don't worry so much when I switch to one or the other.

One of the main differences, though, is that when you use composition to build a class on top of another, you aren't limited by what your base class is. In this case that's good -- it's exactly what you need.

In this case, the central problem is polymorphism. You need to implement a subclass of Drop. To the outside world, your class must look like a Drop. If your implementation is really just a wrapper -- i.e. adaptor -- that delegates down to an instance of UntouchableClass, that's fine.

IMHO the real value of design patterns is understanding at a really detailed level why they work.

OwnerDisplayName: Deinonychus

ContentLicense: CC BY-SA 3.0


问:
What is the name for this variation to Adapter Pattern?

Introduction

An Adapter normally wraps another object, so that it can be used in an interface it wasn't designed for, e.g., when you want to use

```<code>interface Node {
    Node parent();
    Iterable<Node> children();
}
</code>```

together with

```<code>class TreeModel {
    private Node root;
    // example method (stupid)
    Node grandparent(Node node) {
        return node.parent().parent();
    }
}
</code>```

and you're given a class like

```<code>class File {
    File getParent() {...}
    File[] listFiles() {...}
}
</code>```

you need to write some FileToNodeAdapter.

Unfortunately, it means that you need to wrap each single object and you also need both a way to get from FileToNodeAdapter to File (which is trivial, since it's embedded), but also from File to FileToNodeAdapter, which leads either to creating a new object each time or to using some Map, which must be either globally accessible or referenced in each FileToNodeAdapter.

The Pattern

Replace the interface Node by

```<code>interface NodeWorker<T> {
    T parentOf(T node);
    Iterable<T> childrenOf(T node);
}
</code>```

and modify the TreeModel like

```<code>class TreeModel<T> {
    private NodeWorker<T> nodeWorker;
    private T root;
    // example method (stupid)
    T grandparent(T node) {
        return nodeWorker.parentOf(nodeWorker.parentOf(node));
    }
    ...
}
</code>```

Does this pattern have a name?

Are there any disadvantages, besides the fact that it is little bit more verbose and only applicable when you are in charge of the TreeModel code?

答:
I don't think there's a specific name for this; I think it's kind of a glorified adaptor using composition, but made very generic. Which is really powerful.

With NodeWorker and TreeModel, you're trading extension for composition, using generics. You're using generics to parameterize the underlying type of the nodes. So that any object type can just drop into the tree without modification.

There's one step you need, though, and that's the concrete implementations of NodeWorker, which might be based on concrete types of T. (Or you could just make NodeWorker a class, provide a get() method, and carry T object along without knowing or caring what it really is.)

FWIW In Haskell, there's this idea of "lifting" functions into a monadic space, and you're moving in that direction. You're putting objects of type T into the TreeModel space. TreeModel is basically on the road to becoming a monad. But that topic gets pretty deep and I always get it wrong.

EDIT: For example (pseudocode)

```<code>*class* NodeWorker<T> {
    T object;
    T get() { return object; }
    Nodeworker(T object) { this.object = object; }
    ... other parent/child information ...
    T parentOf(T node) { .... }
    Iterable<T> childrenOf(T node) { ... }
}
</code>```

The advantage is that you just have one nodeworker that carries an object of arbitrary type. The disadvantage is that you don't know anything about object, and can't invoke its methods (except for the basic Object stuff).

Body:
I don't think there's a specific name for this; I think it's kind of a glorified adaptor using composition, but made very generic. Which is really powerful.

With NodeWorker and TreeModel, you're trading extension for composition, using generics. You're using generics to parameterize the underlying type of the nodes. So that any object type can just drop into the tree without modification.

There's one step you need, though, and that's the concrete implementations of NodeWorker, which might be based on concrete types of T. (Or you could just make NodeWorker a class, provide a get() method, and carry T object along without knowing or caring what it really is.)

FWIW In Haskell, there's this idea of "lifting" functions into a monadic space, and you're moving in that direction. You're putting objects of type T into the TreeModel space. TreeModel is basically on the road to becoming a monad. But that topic gets pretty deep and I always get it wrong.

EDIT: For example (pseudocode)

*class* NodeWorker<T> {
    T object;
    T get() { return object; }
    Nodeworker(T object) { this.object = object; }
    ... other parent/child information ...
    T parentOf(T node) { .... }
    Iterable<T> childrenOf(T node) { ... }
}

The advantage is that you just have one nodeworker that carries an object of arbitrary type. The disadvantage is that you don't know anything about object, and can't invoke its methods (except for the basic Object stuff).


问:
Can a system be 100% Data Driven?
My new boss has been working on this project for many years.  I've only been here a few weeks, but I am not sure it's possible.  He would like to design a system that is "100% data driven".

So if we put in enough data, we can define and generate any application.  I've managed to at least get him to concede some things like users, or apps should have predefined values, but he likes the concept of the structure of the system, the user interface and the logic all being stored as data.

There are some demos of simple things and he's basically rediscovered some simple ideas of object oriented programming and your basic template systems, but I think overall that this goal might be actually impossible.

I don't know how you can define logic using data without the system becoming so complex that you are doing actual programming anyway.

I think theoretically it isn't because the thing that interprets the data ends up needing to become turing complete to describe the application so you've just shifted the problem one level higher to no net benefit.

Is such a 100% Data Driven Application possible?

答:
Your boss should read this piece: Bad Carma: The "Vision" project, a cautionary tale about inner platform effect or second system effect.

Abstract

Those of us who work in Information Technology (IT) have all been on a project where something important is just not right. We know it, most everyone knows it, but nobody is quite able to put his or her finger on the problem in a convincing way.

This story is about such an IT project, the most spectacular failure I
  have ever experienced. It resulted in the complete dismissal of a
  medium-sized IT department, and eventually led to the destruction of a
  growing company in a growing industry.  The company, which we'll call "Upstart," was a successful and profitable subscription television business.

The project occurred in the early 1990s, and it was a custom-built
  order-entry and customer-service application, closely resembling what
  is now referred to as Customer-Relationship Management or CRM. The
  core functionality of the system included:

Order entry and inventory

Customer service, help desk

General ledger, accounts receivable, billing, and accounts payable

The application was
  called "Vision" and the name was both its officially stated promise
  for Upstart as well as a self-aggrandizing nod to its architect. The
  application was innovative, in that it was built to be flexible enough
  to accommodate any future changes to the business. Not just any
  foreseeable future changes to the business, but absolutely any changes
  to the business, in any form. It was quite a remarkable claim, but
  Vision was intended to be the last application ever built. It achieved
  this utter flexibility by being completely data-driven, providing
  limitless abstraction, and using object-oriented programming
  techniques that were cutting-edge at the time.

Like many such projects that set out to create a mission-critical
  application, the development effort spanned two years, about a year
  longer than originally projected. But that was acceptable, because
  this was the application that would last forever, adapting to any
  future requirements, providing unlimited Return On Investment (ROI).
  When the application finally went "live," almost everybody in the
  company had invested so much in it that literally the fate of the
  company hinged on its success.

However, in the event of total project malfunction, mission-critical
  applications running the core business of multinational corporations
  are not permitted the luxury of the type of fast flameout demonstrated
  by thousands of "dot-com" companies in the era of the Internet bubble. Within a month of Vision going "live," it was apparent to
  all but those most heavily vested in its construction that it was a
  failure.

See Also

http://en.wikipedia.org/wiki/Inner-platform_effect

Title: Can a system be 100% Data Driven?


问:
What do you call a tree that grows in both directions?
What would be the technical term for a tree that grows in both the directions, starting from the root in the middle?

The central idea being that new elements can be added to the tree (Now, I am not sure if it is even okay to call it one) in both the directions. But, when used for traversal purposes, you need to follow the directed links.

答:
I don't believe there is a special name for this, it's simply a tree. "Growing in both directions" is purely an artifact of how you drew the tree.

Also, it's a little hard to tell from your ASCII art, but if the top and bottom are connected and you have cycles, then this is technically not a tree at all, but a graph (of which trees are a more specific case).

Based on the updated picture, this is not a tree at all but a directed graph. What is labeled as "root" cannot be a root because it has multiple edges leading into it. A tree is a very specific sort of Directed Acyclic Graph, which this is, but has the additional constraint that it must have only a single node without any edges leading into it. That node is the root. As you can see, none of the nodes in your graph can satisfy this definition.


答: A tree is a special kind of graph. More specifically, it's "any connected graph without simple cycles". So, your graph is a tree. It doesn't matter if it grows or not. Yours is a directed graph so it doesn't really matter anyways. It's not a binary tree, if that's what you were asking =)</p>

Title: What do you call a tree that grows in both directions?

Body:
What would be the technical term for a tree that grows in both the directions, starting from the root in the middle?

The central idea being that new elements can be added to the tree (Now, I am not sure if it is even okay to call it one) in both the directions. But, when used for traversal purposes, you need to follow the directed links.


问:
Capture portions of a number using mathematical operations
Is there a mathematical way to get the following:

For the the number 777,888,999 so 777 million and etc etc

getMillions(777888999) should return 777 only

getThousands(777888999) should return 888 only

getHundreds(777888999) should return 999 only

This should be done without using regex and without text processing but should be achieved using only mathematical operations.

I'm trying to solve a problem that may be familiar to some which is converting a number expressed in digits to words so 1 -> one, 10 -> ten, 100 -> one hundred etc

My thinking is that if I can convert a number up to 999 then the problem is pretty much solved as the rest will only append the words millions and/or thousands so

777888999 will covert to the words:

777 million and 888 thousands and 999 hundred

of course the above numbers will expressed in words.

This originally was given to me as part of a job interview process about a year ago. I am absolutely not using the solution here for a job application. But the above question is about number manipulation which I'm very interested in.

答:
You could do something like this:

```<code>print (777888999/1000000)%1000
print (777888999/1000)%1000
print (777888999)%1000
</code>```

So, divide by the 1th unit of the position you are looking for, and then just mod it by 1000 since all of these positions can have three places.

Body:
You could do something like this:

print (777888999/1000000)%1000
print (777888999/1000)%1000
print (777888999)%1000

So, divide by the 1th unit of the position you are looking for, and then just mod it by 1000 since all of these positions can have three places.


问:
Visual Design implementation timing in Agile
We work in Scrum with the caveat that we don't have a potentially shippable product at the end of each Sprint, instead requiring several hardening/stabilization Sprints before release.

One of the reasons for this is that our chief UX designer prefers that we'll complete all the visual work (such as colors, fonts, exact layout and controls) at the later stages of the project. On one hand it makes sense because a feature with decent UI based on a wireframe is usually good enough to receive feedback from the users, so working too hard on the final visuals and styles is not necessary for risk reduction. It might even be wasteful if the feature fails at usability tests or if after further user feedback we'll decide to change the visual design for the entire system, including features that were already implemented. On the other hand with this approach we will have a list of features that are not completely developed, and with the final polishes being left out until the final Sprints.

What do you think we should do? Should we go with the recommendation of the chief UX designer or should we go for a potentially shippable product?

Thank you.

答:
Firstly, consider yourself lucky for actually having a UX designer :)

I'm not sure I agree with your definition of "shippable," though.  Your definition isn't a bad one, it just might be a tad strict.

If you have all the functionality coded in with a decent (but still sub-optimal) user experience you're already on par with most production-grade software.  I would consider this shippable.

Leaving the UX polishing/hardening sprint until later seems like a wise choice.  It means the definition of the feature, as requested by the PO and users, is pretty stable.  As such, its implementation is unlikely to change and the risk of having to redo the UX is acceptably low.

I see nothing wrong with your UX guys suggested approach.  That is to say, as long as it doesn't have any negative side effects on other roles or processes.  You haven't mentioned any so I'll assume there aren't.


答:
There's a couple simple categories you can fit graphs into that makes it easier to classify them.

Directed: This is a graph wherein you have parent-child relations that are one way, that is the child may not directly reference the parent

Undirected: This is a graph wherein you have node-node relations, they are not parent-child and may go either way

Acyclic: This is a graph that does not have any cycles in it, this means none of the nodes may reference a node which directly or indirectly references it, that is to say it may not reference one of it's ancestors

Cyclic: This is a graph that may have cycles in it, this means any node may reference any node

Balanced: This is a directed graph with any path being up to the length of N+-1, when drawn it's recognizable because it looks symmetric and the childless nodes all live within 1 height level of eachother.

Binary: This is a directed graph where every node has no more than 2 children.

The graph you have drawn, is a Directed Acyclic Graph, often called a DAG for short. Most things we refer to as "trees" fall into this category, people don't usually call graphs that are either undirected or cyclic "trees" because when you draw one, it doesn't give off the appearance of having a root which is grown from as trees do. People usually resort to the term "graph" in the case of non-DAGs

Above are the commonly seen and well known classifiers, digging around Wikipedia(https://en.wikipedia.org/wiki/Directed_graph) you may find others which are less known such as:

Rooted: This is a graph with a single root node (your specific example may be called unrooted, though very few people are likely to recognize this terminology)

Symmetric: Genuinely; I don't know off hand.

Complete: A complete graph is a graph where all nodes have direct references to all other nodes.


答:
I agree that deferring final UI decisions until later is the way to go in this case. Disclaimer: I am assuming, based on the question, that "final UI design" is a story in and of itself. Also, the fact that you have a dedicated UX designer lends credence to this since that person will be dedicated to working on these features.

If the user story states "Feature X shall do Y and Z" and is ugly, then fine, you can meet the requirements with some rough edges (placeholder icons, temporary color scheme, etc) as long as there is a separate story later on to fill in the UI gaps.

The most important thing is not to lose sight of requirements. As long as they are entered into your requirements tracking system, scheduled, and delivered, it really does not matter if you defer them until later. But if the UI is a risk factor, why work on it now and throw away hours of productivity later?

Title: Visual Design implementation timing in Agile

Body:
We work in Scrum with the caveat that we don't have a potentially shippable product at the end of each Sprint, instead requiring several hardening/stabilization Sprints before release.

One of the reasons for this is that our chief UX designer prefers that we'll complete all the visual work (such as colors, fonts, exact layout and controls) at the later stages of the project. On one hand it makes sense because a feature with decent UI based on a wireframe is usually good enough to receive feedback from the users, so working too hard on the final visuals and styles is not necessary for risk reduction. It might even be wasteful if the feature fails at usability tests or if after further user feedback we'll decide to change the visual design for the entire system, including features that were already implemented. On the other hand with this approach we will have a list of features that are not completely developed, and with the final polishes being left out until the final Sprints.

What do you think we should do? Should we go with the recommendation of the chief UX designer or should we go for a potentially shippable product?

Thank you.


问:
Do frameworks put too much abstraction?
I've been programming for a little under a year and have some experience writing systems applications, web apps, and scripts for businesses/organizations. However, one thing I've never really done is working with a framework like Django, Rails or Zend.

Looking over the Django framework, I'm a little frustrated with how much is abstracted away in frameworks. I understand the core goals of DRY and minimal code, but some of this over-reliance on different modules and heavy abstraction of core functions feels like it:

Makes programs get dated really fast because of the ever-changing nature of modules/frameworks,

Makes code hard to understand because of the plethora of frameworks and modules available and all of their idiosyncrasies,

Makes code less logical unless you've read all of the documentation; i.e., I can read through some list comprehensions and conditional logic and figure out what a program is doing, but when you see functions that require passing in arbitrary strings and dictionaries, things get a little hard to understand unless you're already a guru in a given module; and:

Makes it difficult and tedious to switch between frameworks. Switching between languages is already a challenge, but it's manageable if you have a strong enough understanding of their core functionality/philosophy. Switching between frameworks seems to be more a matter of rote memorization, which in some ways seems to encourage the very inefficiency these frameworks were designed to eliminate.

Do we really need to put like 50 layers of abstraction on top of something as simple as a MySQL query? Why not use something like PHP's PDO interface, where prepared statements/input testing is handled but the universally understandable SQL query is still a part of the function?

Are those abstractions really useful? Isn't feature bloat making them useless, making applications more difficult compared to similar applications written without using a framework?

答:
I agree that "You use libraries, but frameworks use you". That's a very neat way to put it. I don't agree that as soon as you start re-using code you are beginning to build a framework, as you usually don't need to do much more than a quick copy and paste to make use of your code again - that's a library you are building up; no need to get weird about how you bring the code into your site or app.

For me, the crucial point is that it I have to get more out of a resource than it requires me to invest. Or,from another angle, I would say that as soon as a framework's needs are greater than the reward(s) available, all advantage is absent. So it's 'Yes! Please! And thank you!' to all who make assets that will drop straight and function as needed within my own html/CSS/PHP and SQL.

One thing I find incomprehensible is that frameworks are said to make maintenance easier? If the complex mesh of interworking parts isn't performing as intended, you'd better know everything there is to know about the framework in question. Or be ready for a huge input of new syntax.

Title: Do frameworks put too much abstraction?


问:
Keeping documentation / manuals up-to-date in a rapidly changing project

Background

I work on a relatively large project (~570 KLOC) in a relatively small team (formerly 5 developers, now 2). Large amounts of code can appear and then vanish in months; for example, a few years ago an investment bank launched a new stock exchange, then shut it down 7 months later—thousands of LOC were written to support that exchange's interface, then deleted.

To help operations and support teams as well as new developers, we have an internal wiki (a modified DokuWiki, if it matters) with things like retrospective specs (we're writing specs as we go, unfortunately) and what various settings, flags, and status codes mean and do.

Problem

Due to the fast pace of development, however, much of our wiki content becomes obsolete in months, if not weeks. And it's not as simple as updating a single wiki section about whatever component is being changed; with numerous interlocking components, changing one thing, like a setting in the algorithmic trading component, might require looking through a hundred references to this setting and/or component in the wiki, many whose sections may need updating.

The main issue, I believe, is a motivational one. Developers know that whatever they write has a good chance of becoming obsolete soon, so it's rarely worth it (nor is it practical) to go searching throuh the wiki to update all relevant pieces, especially when 2 or 3 days later there might be a request to change, say, the format of a setting, or even its functionality. This leads to distrust of the wiki—even amongst ourselves, we're never sure whether what we're reading indeed describes the latest functionality. And that, I feel, ultimately feeds a vicious cycle akin to the broken windows theory: the perception of disorder demotivates any action to bring back order.

Idea

My idea was to migrate the wiki to something more like Stack Exchange—for example customizing Question2Answer. For two reasons:

I hope that putting all information in Q&A form encourages documentation to be written with the thought in mind, "What question am I trying to answer? Who am I trying to help?" This is unrelated to this post, though.

More importantly, my idea is to use tags—a tag for each component, each setting, each business term, etc.—so that it's easier to find posts that need updating, rather than going through every instance of a keyword—and keyword searches are context-less already, on their own.

Question

Before I go through with this idea, though, I thought I might benefit from advice if anyone else has worked in such a context. I'm open to all ideas and alternatives!

How do you keep highly cross-referenced documentation up-to-date in a rapidly changing project?

答:
Whenever possible, reduce or eliminate the need for documentation by writing clearly understandable code instead of writing documentation to explain obtuse code.

Keep the documentation as close as possible to the actual code it describes.  In most cases, that means putting the documentation in the code itself. Some development platforms (e.g. Visual Studio) have external tools available that can be used to convert code comments to other readable forms, such as help files and web pages.

Limit the documentation to integration concerns and overall architecture.  Let the actual code speak for itself.


答: Robert Harvey's answer is already a good one, but I like to add a remark about your current plan. The same way your code changes from release to release, the same way your documentation has to change. But if your docs are in a Wiki or a "Stack Exchange"-like system, you cannot integrate this well into your source code control system (SCCS). So I strongly suggest not to separate the docs from the rest of the source tree - the docs should be versioned and tagged together with the corresponding code version. A tool like doxygen can be used to write your docs (including those for end users) in text form (suitable for any SCCS) and generate nicely formatted documents.</p>

Title: Keeping documentation / manuals up-to-date in a rapidly changing project

Body:
Background

I work on a relatively large project (~570 KLOC) in a relatively small team (formerly 5 developers, now 2). Large amounts of code can appear and then vanish in months; for example, a few years ago an investment bank launched a new stock exchange, then shut it down 7 months later—thousands of LOC were written to support that exchange's interface, then deleted.

To help operations and support teams as well as new developers, we have an internal wiki (a modified DokuWiki, if it matters) with things like retrospective specs (we're writing specs as we go, unfortunately) and what various settings, flags, and status codes mean and do.

Problem

Due to the fast pace of development, however, much of our wiki content becomes obsolete in months, if not weeks. And it's not as simple as updating a single wiki section about whatever component is being changed; with numerous interlocking components, changing one thing, like a setting in the algorithmic trading component, might require looking through a hundred references to this setting and/or component in the wiki, many whose sections may need updating.

The main issue, I believe, is a motivational one. Developers know that whatever they write has a good chance of becoming obsolete soon, so it's rarely worth it (nor is it practical) to go searching throuh the wiki to update all relevant pieces, especially when 2 or 3 days later there might be a request to change, say, the format of a setting, or even its functionality. This leads to distrust of the wiki—even amongst ourselves, we're never sure whether what we're reading indeed describes the latest functionality. And that, I feel, ultimately feeds a vicious cycle akin to the broken windows theory: the perception of disorder demotivates any action to bring back order.

Idea

My idea was to migrate the wiki to something more like Stack Exchange—for example customizing Question2Answer. For two reasons:

I hope that putting all information in Q&A form encourages documentation to be written with the thought in mind, "What question am I trying to answer? Who am I trying to help?" This is unrelated to this post, though.

More importantly, my idea is to use tags—a tag for each component, each setting, each business term, etc.—so that it's easier to find posts that need updating, rather than going through every instance of a keyword—and keyword searches are context-less already, on their own.

Question

Before I go through with this idea, though, I thought I might benefit from advice if anyone else has worked in such a context. I'm open to all ideas and alternatives!

How do you keep highly cross-referenced documentation up-to-date in a rapidly changing project?

ContentLicense: CC BY-SA 4.0


问:
Citations for inadvisability of globally unique password
I am having a disagreement with a client about the user authentication process for a system. The nub of it is that they want each user to have a globally unique password (i.e. no two users can have the same password).

I have wheeled out all the obvious arguments against this:

it's a security vulnerability,

it confuses identification with authentication,

it's pointless, etc.,

but they are insisting that there is nothing wrong with this approach.

I have done various gGoogle searches looking for authoritative (or semi-authoritative , or even just independent) opinions on this, but can't find any (mainly it's just such an obvious faux pas that it doesn't seem worth warning against, as far as I can tell).  Can anybody point me towards any such independent opinion, please?

EDIT: Thanks for all your answers, but I already understand the problems with this proposed approach/requirement, and can even explain them to the client, but the client wont't accept them, hence my request for independent and/or authoritative sources.

I'd also found the Daily WTF article, but it suffers from the problem that Jon Hopkins has pointed out - that this is such a self-evident WTF that it doesn't seem worth explaining why.

And yes, the passwords are going to be salted and hashed. In which case global uniqueness might well be difficult to ensure, but that doesn't solve my problem - it just means that I have a requirement that the client won't budge on, that's not only ill-advised, but is also difficult to implement. And if I was in a position to say "I'm not budging on salting and hashing", then I'd be in a position to say "I'm not implementing globally unique passwords".

Any pointers to independent and/or authoritative sources for why this is a bad idea still gratefully received...

答:
Best practice gets in the way of meeting the requirement:

You can't do this because you don't know what the passwords are so you can't compare them because all that you store is the hash of the password and a salt (which you can store next to the hashed password). This is best practice, so that's what you do. Done.

Another edit: Doh! Hindsight is easy - you could still check for uniqueness because (of course) you have the salt... just shoot me now... of course you don't have to mention this detail to the client.

FWIW, its not quite as dumb as you think it is - the aim is to prevent groups of users agreeing and sharing the same password, which people will do in the belief that it will make their lives easier.

If applied with a requirement to change the password regularly and a constraint that prevents people from re-using a current or previously used password (at all, ever) and sensible "strength" requirements (or at least a pre-loaded dictionary of "blocked" passwords) you're going to get to a point, fairly rapidly, where the odds are not in the hacker's favour anyway.

Ok, my answer originally started with:

There's notionally very little wrong
  with this given a couple of provisos -
  most of which are that I'm being
  thick...

Inappropriate humour apparently - point was that if you don't have the globally unique constraint you're creating different opportunities, perhaps less dangerous - I don't know.

ContentLicense: CC BY-SA 2.5


答:
Intent is an experimental product attempting to address precisely this issue, currently in an Eclipse/Java envoronment. The goal seems to be to have strongly typed, or perhaps unit tested, documentation. Any part of the documentation becoming inconsistent with the corresponding source code will get flagged up as an error, just like code that calls a function that no longer exists.

This addresses the heart of the problem with documentation; being unable to trust it because it might be older, or perhaps newer, than the system. And unlike simple document generation (javadoc/doxygen), the documentation is structured around readability, not the source code.

Promising as it looks, I'd be interested if anyone has any real-world experience with it...


答:
You could argue convincingly, I think, that any web browser application could be considered 100% data driven1.

Of course, that doesn't make it simpler or easier to build applications on the web, in fact it makes them much harder.

Tell your boss that he's reinventing the web browser, and he will eventually have to reinvent JavaScript to build anything reasonably complex.

1 Well, if you ignore plugins, JavaScript and HTML5.

Title: Can a system be 100% Data Driven?

Body:
My new boss has been working on this project for many years.  I've only been here a few weeks, but I am not sure it's possible.  He would like to design a system that is "100% data driven".

So if we put in enough data, we can define and generate any application.  I've managed to at least get him to concede some things like users, or apps should have predefined values, but he likes the concept of the structure of the system, the user interface and the logic all being stored as data.

There are some demos of simple things and he's basically rediscovered some simple ideas of object oriented programming and your basic template systems, but I think overall that this goal might be actually impossible.

I don't know how you can define logic using data without the system becoming so complex that you are doing actual programming anyway.

I think theoretically it isn't because the thing that interprets the data ends up needing to become turing complete to describe the application so you've just shifted the problem one level higher to no net benefit.

Is such a 100% Data Driven Application possible?


问:
Virtual machine for a high level programming language vs an operating system
It struck me recently that a virtual machine for a high level programming language is very much like an OS. It manages resources, e.g. stack, heap, etc. similar to how an operating system manages resources and if the programming language supports threads then the virtual machine also does time-sharing and context switching similar to how an operating system does it.

How far can this analogy be taken? Why is that we don't have a bare-metal implementation of the JVM? It seems like all the pieces are there so what exactly is the reason such things are not more prevalent?

答:
There are "bare metal" machines based on virtual machines.  Most recently, there have been various attempts at a Java Processor which would run a JVM (well, JM since its not virtual) in hardware.  They do exist - though not in general purpose machines that we use, they more harken back to the original embedded idea that Java was born from.

This harkens back to the Lisp machine which approached CPU design instead with high level instructions in the CPU itself.

The reason is... you don't really get that much out of the specialization at a hardware level... and the direction of research has been for smaller instruction sets with multiple cores rather than trying to make complex instructions.  That's where the money has been chased and thats why people are exploring that path more.

The question to consider - is narrowing down your available programs that the system can run worth the investment cost of creating such a system.  Furthermore, as the general purpose processors get faster, can you keep up with a viable alternative?

... cheaper desktop PCs soon were able to run Lisp programs even faster than Lisp machines, without the use of special purpose hardware. Their high profit margin hardware business eliminated, most Lisp Machine manufacturers went out of business by the early 90s ...

From Wikipedia on the Lisp Machine.

Note that last section on the other language-optimized computers which includes the above mentioned Java Processors.  They just haven't been commercially viable in most situations (and none of them for a general purpose machine).


答: Hardware implementations of the JVM do exist, and there are a number to choose from. However, they aren't without some drawbacks. For one, you can't easily upgrade the machine if it's written in silicon. </p>

Title: Virtual machine for a high level programming language vs an operating system

Body:
It struck me recently that a virtual machine for a high level programming language is very much like an OS. It manages resources, e.g. stack, heap, etc. similar to how an operating system manages resources and if the programming language supports threads then the virtual machine also does time-sharing and context switching similar to how an operating system does it.

How far can this analogy be taken? Why is that we don't have a bare-metal implementation of the JVM? It seems like all the pieces are there so what exactly is the reason such things are not more prevalent?


问:
Is there a download limit to ios apps?
With reference to iOS 7.
I couldn't find any Apple documents with regards to this.

Are we able to download any size of documents and manage them ourselves (within our app say < 100MB)?

Example, we roll out new content every month. Thus after downloading contents for this month, we should remove last month's content to save space. Content doesn't have to be backed up and will be stored in Caches folder.

答: The limit is relative to the available storage on the device.  There exist (or did exist) Navigation apps that could download well over 1GB of data.</p>

Title: Is there a download limit to ios apps?


问:
How does browsing speed differ in native apps, web apps, and websites?
I read an article online which I give the reference to below which compare between Native apps and web apps, in addition to standard website. It seems from that article that native apps are faster than web apps and websites. However, that was not clear for me.

Could a native app be built to be used exactly as a website? meaning could the functions in a social website, for example, be programmed as a native app? In this case isn't that going to use a server-side language? Then what make it faster if even the website and the web app use the same thing?

http://www.mobi-cart.com/native-vs-web.jsp

答: What makes one faster than the other is by not doing the same thing in both.  Each platform or development tool path allows different sets of optimizations.  Sometimes these optimizations are closer to the hardware, or goes through less layers of software which can eat CPU cycles.  If a developer takes advantage of available optimizations (often by native coding or using platform native libraries instead of portable ones), then the result can be faster.  If not, then maybe not.  Even if faster, the difference may or may not be perceivable to the user, or important to the app provider.</p>


问:
LLVM case: is the success due to the algorithms?
I'm not sure if this is a subjective question, I hope not but I'm not proficient with LLVM so I'm just wondering: how come it's become so popular and so widespread used in the industry?

Were the optimization algorithms used in gcc/icc/msvc just worse than the LLVM ones? Is it possible that a university project managed to outperform such known and long-standing compilers? I know that LLVM is modular, but can then a design be the cause of that much success?

I can't understand the real reasons behind that, this is also probably related to a subjective question like "is the success of a software always related to a new or semi-new algorithm that speeds something up?" but I'd like to get objective reasons to my first question if possible

答:
The license of LLVM is more acceptable to some companies, thus creating a greater current critical mass of corporate sponsored/paid development of the tool suite for certain architectures/languages.

The division between compiler components/front/back/ends has a cleaner (resulting from being newer) specification in LLVM, and thus presents a lower hurdle for new developers.

The effect of the new algorithms was that research implementers needed a place to put them to experiment with them without digging through as much legacy compiler architecture, which may be another reason for the newer specified modularity.


问:
What exactly is “computer systems”?
My professor made a comment today - "...They've been having trouble with filesystem performance, and since they're more graphics guys, they asked us systems guys to help out...".

What exactly is "computer systems"? What does it mean to be a "systems guy"? And, what is a "systems guy" passionate about? For contrast, what is not computer systems? What would possibly bore a "systems guy"?

答:
He means a specialist in Operating Systems. Typically covering things like file systems, device I/O, memory management, concurrency control, and security.

File systems would be things like "Do I have a system where the file structure can be accessed quickly and efficiently?".

Device I/O would be along the lines of "Is there a way to more efficiently support some particular type of network controller so that I can send messages with fewer errors?"

Memory management would cover "How can I context switch more rapidly?" This means fully changing from one application to another for instance. It's an expensive operation to do because it involves total copies of memory.

Concurrency Control covers topics like "How do I ensure that all of my CPUs (if I have more than one) are being evenly utilized?"

Security is a whole sub-field of Computer Science and Engineering but is very relevant to operating systems. An example might be "How can I comprehensively protect mobile devices from viruses without draining the battery too much or at all?"

Graphics would be more abstractly mathematical and while potentially as low-level in terms of distance from the machine code, tends to be somewhat less so. He could also be using "Graphics" in terms of people who are more design or human factors oriented.

Title: What exactly is “computer systems”?


答:
Bare metal JVMs do (did?) exist, but never scaled enough in volume to fund enough development R&D to keep up with super high-volume mainstream processor implementations.  An x86 (in a small nanometer process) or ARM ends up running a JVM faster and cheaper and using less power than a multi-Moores-law-generations-behind (thus, in big nanometer or micron process) custom JVM.  And it currently takes tens of millions to billions to keep up.

The same thing happened to some non-consumer-driven RISC architectures and some less-specialized DSPs.


答:
Yes. As far as I know, a system like Mathematica, which is a so-called powerful programming language but essentially is a shell, is built upon the similar idea that your boss has expected. Wolfram Mathematica is now becoming complex enough such that many computational tasks can be easily done by it.

Data driven is a concept. If we programmers are going to manipulate data in a simple way, we need a shell that is easy for us to play with data. Try to understand that once we start talking about learning a programming language based on syntax, we are actually learning the application interface or simply its shell. If we understand the shell, we can drive the programs.

As for 100 % data driven, if the compiler or the interpreter can understand the shell, computation is driven. If the data has the same underlying structure as the shell or interface that has, the data can be driven by compiler or interpreter too. I think Mathematica is a good explanation for why I respond you with a yes.

Title: Can a system be 100% Data Driven?


答: A system is a lot more than a CPU running code.  How all the boxes are connected, reliably powered, cooled, fault-monitored, fail-over provisioned, who feeds the card-punches and moves the tape-reels (just kidding), etc. can be affected by software that a systems guy might be involved with.</p>

Title: What exactly is “computer systems”?

Body:
My professor made a comment today - "...They've been having trouble with filesystem performance, and since they're more graphics guys, they asked us systems guys to help out...".

What exactly is "computer systems"? What does it mean to be a "systems guy"? And, what is a "systems guy" passionate about? For contrast, what is not computer systems? What would possibly bore a "systems guy"?


问:
Performance: recursion vs. iteration in Javascript
I have read recently some articles (e.g. http://dailyjs.com/2012/09/14/functional-programming/) about the functional aspects of Javascript and the relationship between Scheme and Javascript (the latter was influenced by the first, which is a functional language, while the O-O aspects are inherited from Self which is a prototyping-based language).

However my question is more specific: I was wondering if there are metrics about the performance of recursion vs. iteration in Javascript.

I know that in some languages (where by design iteration performs better) the difference is minimal because the interpreter / compiler converts recursion into iteration, however I guess that probably this is not the case of Javascript since it is, at least partially, a functional language.

答:
I was pretty curious about this performance in javascript as well, so I did some experiments (albeit on an older version of node). I wrote a factorial calculator recursively vs iterations and ran it a few times locally. The result seemed pretty heavily skewed toward recursion having a tax (expected).

Code: https://github.com/j03m/trickyQuestions/blob/master/factorial.js

```<code>Result:
j03m-MacBook-Air:trickyQuestions j03m$ node factorial.js
Time:557
Time:126
j03m-MacBook-Air:trickyQuestions j03m$ node factorial.js
Time:519
Time:120
j03m-MacBook-Air:trickyQuestions j03m$ node factorial.js
Time:541
Time:123
j03m-MacBook-Air:trickyQuestions j03m$ node --version
v0.8.22
</code>```

Body:
I was pretty curious about this performance in javascript as well, so I did some experiments (albeit on an older version of node). I wrote a factorial calculator recursively vs iterations and ran it a few times locally. The result seemed pretty heavily skewed toward recursion having a tax (expected).

Code: https://github.com/j03m/trickyQuestions/blob/master/factorial.js

Result:
j03m-MacBook-Air:trickyQuestions j03m$ node factorial.js
Time:557
Time:126
j03m-MacBook-Air:trickyQuestions j03m$ node factorial.js
Time:519
Time:120
j03m-MacBook-Air:trickyQuestions j03m$ node factorial.js
Time:541
Time:123
j03m-MacBook-Air:trickyQuestions j03m$ node --version
v0.8.22


答:
How far can this be taken?

Pretty far. You might want to check out jnode (Java New Operating system Design Effort) (github repo) which is a full bare metal implementation of the JVM. Only about 500k of assembly, as a "nano-kernel," used in the boot process. Once the OS is running, all running code is actually java.

Why is that we don't have a bare-metal implementation of the JVM?

A lot of things that JVM developers are used to thinking of as "just there" are actually supported by the underlying OS, and the JVM just uses the OS resource. Example:
Device drivers. If you look at the jnode code above, you will find that there is ongoing effort in building device drivers. They need to be written in java, which you will never see anywhere else.


问:
Correct Seam for Stub

In Chapter 3 of his book The Art of Unit Testing: with Examples in C#, Roy Osherove illustrates the issue of undesirable external dependencies in code under test.

He shows this with a method named IsValidLogFileName which takes in a filename string as an argument, reads in a config file from the local filesystem and checks whether the given filename extension exists in the config file. Here's what such a class would look like:

```
   public class LogAnalyzer
    {
        public bool IsValidLogFileName(string fileName)
        {

            string [] extensions = System.IO.File.ReadAllLines(@"C:\Users\Public\ext.config");

            foreach (string ext in extensions)
            {
                if (fileName.EndsWith(ext))
                {
                    return true;
                }
            }
            return false;
        }
    }
```

The issue here is that testing IsValidLogFileName has a dependency on the local filesystem.

The author introduces the concept of stubs as a means of removing this dependency. In particular,
he describes the following design:

Create an interface:

```
    interface IExtensionManager
    {
        bool IsValid(string fileName);
    }
```

an implementing production class:

```
    class FileExtensionManager : IExtensionManager
    {
        bool IsValid(string fileName)
        {
            string [] extensions = System.IO.File.ReadAllLines(@"C:\Users\Public\ext.config");

            foreach (string ext in extensions)
            {
                if (fileName.EndsWith(ext))
                {
                    return true;
                }
            }
            return false;
        }
    }
```

a stub:

```
    class FakeExtensionManager : IExtensionManager
    {
        bool IsValid(string fileName)
        {
            string [] extensions = { "txt", "c", "cpp", "cs"};

            foreach (string ext in extensions)
            {
                if (fileName.EndsWith(ext))
                {
                    return true;
                }
            }
            return false;
        }
    }
```

refactor LogAnalyzer as follows:

```
   public class LogAnalyzer
    {
        IExtensionManager em;

        public LogAnalyzer(IExtensionManager extensionManager)
        {
            em = extensionManager;
        }

        public bool IsValidLogFileName(string fileName)
        {
            return em.IsValid(fileName);
        }
    }
```

run production code like this:

```
    LogAnalyzer la = new LogAnalyzer(new FileExtensionManager);
```

and test code like this:

```
    LogAnalyzer la = new LogAnalyzer(new FakeExtensionManager);
```

The part I don't understand is why we have to extract the validation logic
from the original IsValidLogFileName by making an ExtensionManager interface.
Wouldn't it make more sense to create something like a ConfigManager interface
and refactor as follows:

interface:

```
    interface IConfigManager
    {
        string [] GetFileExtensionConfig();
    }
```

an implementing production class:

```
    class LocalConfigManager : IConfigManager
    {
        string [] GetFileExtensionConfig( )
        {
            return System.IO.File.ReadAllLines(@"C:\Users\Public\ext.config");
        }
    }
```

a stub:

```
    class FakeConfigManager : IConfigManager
    {
        string [] GetFileExtensionConfig( )
        {
            string [] extensions = { "txt", "c", "cpp", "cs"};
            return extensions;
        }
    }
```

refactor LogAnalyzer as follows:

```
    interface IExtensionManager
    {
        bool IsValid(string fileName);
    }
```0

run production code like this:

```
    interface IExtensionManager
    {
        bool IsValid(string fileName);
    }
```1

and test code like this:

```
    interface IExtensionManager
    {
        bool IsValid(string fileName);
    }
```2

答:
Roy here (book author). I like your implementation of solving this problem. For one, it only abstracts away the IO part, and not the logic, leaving the logic in a testable location. Second, in your implementation the LogAnalyzer class still does something. In my implementation in the book, after refactoring, LogAnalyzer does nothing except being a forwarding mechanism toward the Extension manager.

One thing to notice in your original explanation of what I show in the book:

I don't recommend that the fake extension manager do any logic at all, but instead take its queues from the test to know what to return. So logic is not implemented twice (in prod and fake code).

In essence, you got the idea. Fake out the dependency code. It's a good implementation of the solution for such a small and simple piece of code and I would have no problem with it if I saw you do it in real world production code. I'd probably say "Good job!".

Then I would go on to say that the fake extension config should not have hard coded extensions, but should be configured by the test so that :

the test is more readable

the fake class is more reusable in other tests where you might want to simulate other configurations

```<code>FakeConfigMgr fakeConfig  = new FakeConfigMgr();
fakeConfig.ValidExtensionsWillBe = new []("txt","ext");
LogAnalyzer la = new LogAnalyzer(fakeConfig);
</code>```

Title: Correct Seam for Stub

Body:
Roy here (book author). I like your implementation of solving this problem. For one, it only abstracts away the IO part, and not the logic, leaving the logic in a testable location. Second, in your implementation the LogAnalyzer class still does something. In my implementation in the book, after refactoring, LogAnalyzer does nothing except being a forwarding mechanism toward the Extension manager.

One thing to notice in your original explanation of what I show in the book:

I don't recommend that the fake extension manager do any logic at all, but instead take its queues from the test to know what to return. So logic is not implemented twice (in prod and fake code).

In essence, you got the idea. Fake out the dependency code. It's a good implementation of the solution for such a small and simple piece of code and I would have no problem with it if I saw you do it in real world production code. I'd probably say "Good job!".

Then I would go on to say that the fake extension config should not have hard coded extensions, but should be configured by the test so that :

the test is more readable

the fake class is more reusable in other tests where you might want to simulate other configurations

FakeConfigMgr fakeConfig  = new FakeConfigMgr();
fakeConfig.ValidExtensionsWillBe = new []("txt","ext");
LogAnalyzer la = new LogAnalyzer(fakeConfig);


问:
Testing the Consumers of Subclassed Data Structures
PHP's SplQueue does not include a clear() or reset() function to wipe data out of the data structure. My application requires that functionality. This leaves two options:

A) Create a subclass of SplQueue. Example:

```<code>class UserQueue extends SplQueue
{
  public function clear() {
    //dequeue everything
  }
}
</code>```

The problem with this approach is that I can't see a way to test consumers of this subclass without relying on clear() as defined in the subclass. In essence I can't think of a way to stub that functionality in PHPUnit short of just depending on UserQueue's own tests.

B) Implement the clear() function in every consumer of SplQueue that requires it (most consumers in my application), which seems obviously terrible but would appear to be the only testable method.

I am hoping someone out there can tell me there is an obvious way to test option A that I've missed. I also considered wrapping SplQueue but I don't see how this improves matters. Or I could monkey patch it with runkit, but a nearly-allergic aversion to runkit has been beaten into me.

答:
After batting this around with other people on my team for a few days, the best (only) solution for a purist TDD environment that we could come up with is to inject the data structure as a mock object, stub the clear/reset function, and provide it with a closure to run as a result. Fortunately PHP and PHPUnit support this. It's not an incredible solution, but it works.

PHPUnit Example:

```<code>$userQueueMock = $this->getMockBuilder('ProjectNamespace\UserQueue')
->setMethods(array('clear'))->getMock();

$userQueueMock->expects($this->once())->method('clear')
->return($this->returnCallback(function() { foreach($this->queue as $entry){}});
</code>```

Hope this helps someone else!

Body:
After batting this around with other people on my team for a few days, the best (only) solution for a purist TDD environment that we could come up with is to inject the data structure as a mock object, stub the clear/reset function, and provide it with a closure to run as a result. Fortunately PHP and PHPUnit support this. It's not an incredible solution, but it works.

PHPUnit Example:

$userQueueMock = $this->getMockBuilder('ProjectNamespace\UserQueue')
->setMethods(array('clear'))->getMock();

$userQueueMock->expects($this->once())->method('clear')
->return($this->returnCallback(function() { foreach($this->queue as $entry){}});

Hope this helps someone else!


答:
By not maintaining the documentation, your source code becomes a mess, and over time, the cost of changing it escalates till its too expensive to make minor changes and maintain. At this point you have turned the current value of your code (500K SLOC is worth how much at the moment) into something worth nothing.

Say you code is worth $5Million now, and over 10 years you let if degrade to have no value - that is $500k / year lost. Spending maybe $100K to maintain its value is cheap.

Its really nothing more than simple business economics. Spend some money now on preventative maintenance to reduce costs and loss of value in the future. No difference than painting a steel bridge, only to start back at the beginning as soon as you have finished.


问:
How do you store "fuzzy dates" into a database?
This is a problem I've run into a few times. Imagine you have a record that you want to store into a database table. This table has a DateTime column called "date_created". This one particular record was created a long time ago, and you're not really sure about the exact date, but you know the year and month. Other records you know just the year. Other records you know the day, month and year.

You can't use a DateTime field, because "May 1978" isn't a valid date. If you split it up into multiple columns, you lose the ability to query. Has anyone else run into this, if so how did you handle it?

To clarify the system I'm building, it is a system that tracks archives. Some content was produced a long time ago, and all that we know is "May 1978". I could store it as May 1 1978, but only with some way to denote that this date is only accurate to the month. That way some years later when I'm retrieving that archive, I'm not confused when the dates don't match up.

For my purposes, it is important to differentiate "unknown day in May 1978" with "May 1st, 1978". Also, I would not want to store the unknowns as 0, like "May 0, 1978" because most database systems will reject that as an invalid date value.

答:
Another option would be to store the dates as integers of the form YYYYMMDD.

You only know the year is 1951: Store as 19510000

You know the month and year is March 1951: Store as 19510300

You know the full date is March 14, 1951: Store as 19510314

A completely unknown date: Store as 0

Benefits

You can store your fuzzy date in one field instead of two date fields or a date and an accuracy as many of the other answers suggest.

Queries are still easy:

all records for the year 1951 - SELECT * FROM table WHERE thedate>=19510000 and thedate<19520000

all records for March 1951 - SELECT * FROM table where thedate>=19510300 and thedate<19510400

all records for March 14, 1951 - SELECT * FROM table where thedate=19510314

NOTES

Your GUI would need a GetDateString(int fuzzyDate) which is pretty easy to implement.

Sorting is easy with the int format. You should know that unknown dates will come first. You could reverse this by using 99 for the 'padding' instead of 00 for the month or day.


答:
A tree is any acyclic graph.

In an undirected tree, any node can be the root, since all edges are undirected and therefore there's no 'parent / child' relationship.  Most of these aren't typically thought as trees and aren't drawn as such, but all the mathematical definitions apply.

In a directed tree, usually there's a single root, if you define if the direction of edges are 'child->parent' or 'parent->child'.  Which one is usually an implementation detail: in-memory structures typically have pointers from parent to children, relational databases use foreign keys from children to parent.

Note that a directed tree can have no root if the direction of the arcs don't represent a parent/child relationship.  This is still a three, but most of the implementation simplifications typical of a directed three are less obvious.  In that case, an implementation could either treat the three as undirected, or add an underlying rooted tree by labeling the parent/child relationships separately.

In this case the edge direction is drawn inconsistently, but I guess any implementation would pick one and flip the arrows of one half, so you end with a pretty standard rooted tree.  Of course, to keep the 'prefix' subtree separate from the 'suffix' one, i would either use two independent trees, or initialize the root with two fixed nodes ('prefix' and 'suffix') and grow each one independently.


答:
The answer is yes, it is possible to create a fully data-driven system and yes, it's usually a really bad idea.

A fully data driven program is one in which all logic and configuration is handled by values stored in such a way that in another context they would be regarded as data. There were many 4GL products produced in the 1980s that provided the capability to generate reports, forms, tables and logic using data items entered into a multiplicity of forms, stored in tables and accessible through reports. I used to refer to such systems as "paint by numbers" but I see it has now come to be known as the "inner system" effect. Good name.

People who create these systems are trying (whether they know it or not) to create a new programming language. Since they do not have the skills, they do it badly. From the viewpoint of the JVM/CLR, a compiled Java/C# program is simply data. In this case it has been done well. In either case, programmers are needed to use the language, whatever it is.

There is one specific way to make this work, that I know of. You build the skeleton of each of the components you need: form, report, table, etc. You provide a mechanism to configure various parts of these components by setting data items. For a chosen set of features you make the decisions and freeze them into the system, and specifically deny the ability to configure those features.

You also implement a language that has the ability to code logical operations. My recommendation is to use an existing language such as lua or maybe Python. You embed pieces of this code wherever logical operations are needed.

By doing this you substantially reduce the amount of writing required to implement each form, report, table and so on. The system seems to be data-driven, but only to a point.

At this point you have just implemented a new 4GL. If you happen do do this successfully, please let me know. Most people fail dismally. I will be the first to congratulate you on your achievement.

Title: Can a system be 100% Data Driven?


问:
Converting event-based asynchrony to C#5 async
We currently have an interface for an asynchronous video decoder that runs on its own thread. Basically you feed it some data and it'll eventually call you back on its thread through some events, like so (not real code):

```<code>interface IDecoder {
    void Decode(parameters);
    event FrameReady;
    event FrameDropped;
    // lots of other possibilities
}
</code>```

It's a bit messy and I'd like to convert it to a C#5, async-based version, something like:

```<code>interface IDecoder {
    async Task<DecodeResult> DecodeAsync(parameters);
}
</code>```

Where DecodeResult will have all the information that would have otherwise been transmitted via events, and the client code can be written cleanly with async/await rather than a mess of event handlers and thread dispatching.

It is my understanding that Tasks are scheduled on the ThreadPool by default. We want all Decode operations to happen sequentially on a single, decoder-specific thread. Can this be achieved using Tasks, if so, how? Are Tasks even appropriate for this use or is there a more obvious pattern I'm missing?

答:
You could implement a custom TaskScheduler for the purpose of only feeding one thread with the work of tasks as you can see at MSDN - How to: Create a Task Scheduler That Limits Concurrency

In rare scenarios, you might be able to achieve a performance speedup by creating a custom task scheduler that is derived from the System.Threading.Tasks.TaskScheduler class. You can then specify this scheduler by supplying a ParallelOptions object to an overload of the For or ForEach method. When you use Task objects directly, you can specify the custom scheduler by using the TaskFactory constructor that takes a TaskScheduler as an input parameter, or by some other means such as TaskFactory.StartNew.

You can also use a custom scheduler to achieve functionality that the default scheduler does not provide, such as strict first-in, first-out (FIFO) execution order...


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

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

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

This is current structure of files and directories:

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

one more:

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

and more:

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

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

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

production version of project.

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

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

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

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

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

My question is:

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

答:
I think that's a good structure but I'm gonna suggest you that don't use django template or any server side template technology.
Imagine that in the future you decide (or not you, your boss) to implement some services in other server technology than python/django (ruby/rails, symphony2, J2EE, .NET), that way all you client side html templates are not usable.
I think is a good idea to use client side templating like mustache.js and others.

Check this article: "https://engineering.linkedin.com/frontend/client-side-templating-throwdown-mustache-handlebars-dustjs-and-more"


问:
Can a system be 100% Data Driven?
My new boss has been working on this project for many years.  I've only been here a few weeks, but I am not sure it's possible.  He would like to design a system that is "100% data driven".

So if we put in enough data, we can define and generate any application.  I've managed to at least get him to concede some things like users, or apps should have predefined values, but he likes the concept of the structure of the system, the user interface and the logic all being stored as data.

There are some demos of simple things and he's basically rediscovered some simple ideas of object oriented programming and your basic template systems, but I think overall that this goal might be actually impossible.

I don't know how you can define logic using data without the system becoming so complex that you are doing actual programming anyway.

I think theoretically it isn't because the thing that interprets the data ends up needing to become turing complete to describe the application so you've just shifted the problem one level higher to no net benefit.

Is such a 100% Data Driven Application possible?

答:
I think you're basically correct. A language runtime is already a fully flexible data-driven system. It takes one piece of data (the program) and uses it to determine how it should act on other data. It might even have a multi-user scheme to store code for re-use by other programs (ranging from an include path to proper install management).

A "scripting language", roughly speaking, is a language runtime where this code input is human-readable. A compiler places an extra step between the user and the runtime. "Joke" languages like Malbolge <s>and APL</s> needn't be human-readable in any form. But it's all the same thing at one level, and anyway human-readable does not mean that all potential users have the skills to read or write it, or can be expected to develop them.

There are good reasons why you don't normally expose a language runtime directly to end-users. The main one being that removing flexibility increases convenience.

If I want to type a SO post, I just want to type it. I'm perfectly capable of instead writing a C++ program to output it, but I would not use a web browser that exposed a C++ program editor instead of a regular text box. People who don't know C++ not only wouldn't use the browser, they couldn't.

If I want to configure certain business parameters then I don't necessarily want to do that using a Turing-complete specification language, and even if I did this is probably not distinguishable from "hard-coding" those same business parameters in any other programming language. You still need to consider whether what you're writing means what you want it to mean. You still need to test that changes are correct. That is, you still need programming skills for any tasks that are non-trivial and not anticipated by someone who does have programming skills who prepared a specialised sub-system ("application") for you to configure ("use").

So if you're about to embark on a 100% data-driven system, that can do anything given the right data, you have two questions to ask yourself:

Are we in the business of inventing programming languages, or should we be?

Will our new programming language be better (for our purposes) than the ones we already have and will we support and develop it as it needs?

Sometimes the answers are yes, and you write a domain-specific language of some kind. Or even a real general-purpose programming language if you're Sun/Microsoft/Stroustrup/van Rossum/many others. Sometimes the answers are no and you have the "inner platform" effect -- after much effort and trial and error you end up with something. If you're lucky it's only slightly inferior to the programming language you wrote it in, and no easier to use.

Some languages are harder or easier to use than others, in particular if they are specialised to a purpose like R then some users will find them much easier. What you probably won't do, is make general applications programming fundamentally easier. At any one time there are probably several people/organizations in the world with the potential to do that, but your boss/company has to honestly consider whether or not that includes him/you.

There's a trick often used for games, which is to expose Lua bindings to the game engine. This allows designers to program in a relatively easy language, but still engage a "real" programmer where necessary for performance or to access particular functionality of the engine or the platform. The resulting Lua scripts are "data" as far as the engine is concerned. They don't all need to include much of what you'd call "logic" as opposed to config data, and often they pretty much define all the plot and environment, but not the whole gameplay. This is not 100% data-driven and it certainly isn't 100% error-free, but it's an interesting practical compromise.

Title: Can a system be 100% Data Driven?


问:
Is it a bad idea to have many protected variables, events and procedures in a unit?
I am writing a full fledged p2p networking library on top of Indy's UDP server with an intention of publishing it online. The design I have implemented has a P2P manager ( TIdUDPServer ) which can act as a server or a peer. Each connected peer is represented by a separate object which contains additional features and information about them. The classes are both contained in a single unit.

For the purpose of simplification, I have declared those members used by either, under protected. Somehow, my instincts are giving me a bad feeling about this. The P2P manager has very few members but the peer object has nearly everything under protected. Going the other way will most certainly introduce a whole lot of code and method calls.

I need to know;

Is it a good practice for networking libraries to have protected members?

Should I replace the variables with properties under protected? With getters and setters or direct access would be fine?

答:
I can't see what the type of library should have to do with the architecture of your code in general.

As a general rule of thumb, it is better to start with high restrictions, you can lower them all the time. Encapsulation is a valuable good, it can help a great deal to keep things decoupled. It's much harder going the other way, moving something from published down the road to private. For example, for interfaced classes (especially refcounted ones), I would recommend to put all interface methods under protected to enforce this way for such classes and just leave the CTOR/DTORs public.

Regarding getters/setters: If in doubt, make it a property. Delphi is smart enough to replace acesses to simple properties like

```<code>property foo : string read ffoo write ffoo;
</code>```

with direct variable access, just check the generated CPU code.

Aside from that, I would prefer strict protected/private rather than just protected/private wherever possible.

Body:
I can't see what the type of library should have to do with the architecture of your code in general.

As a general rule of thumb, it is better to start with high restrictions, you can lower them all the time. Encapsulation is a valuable good, it can help a great deal to keep things decoupled. It's much harder going the other way, moving something from published down the road to private. For example, for interfaced classes (especially refcounted ones), I would recommend to put all interface methods under protected to enforce this way for such classes and just leave the CTOR/DTORs public.

Regarding getters/setters: If in doubt, make it a property. Delphi is smart enough to replace acesses to simple properties like

property foo : string read ffoo write ffoo;

with direct variable access, just check the generated CPU code.

Aside from that, I would prefer strict protected/private rather than just protected/private wherever possible.


问:
Are there any problems with implementing custom HTTP methods?
We have a URL in the following format

/instance/{instanceType}/{instanceId}

You can call it with the standard HTTP methods: POST, GET, DELETE, PUT. However, there are a few more actions that we take on it such as "Save as draft" or "Curate"

We thought we could just use custom HTTP methods like: DRAFT, VALIDATE, CURATE

I think this is acceptable since the standards say

"The set of common methods for HTTP/1.1 is defined below. Although this set can be expanded, additional methods cannot be assumed to share the same semantics for separately extended clients and servers."

And tools like WebDav create some of their own extensions.

Are there problems someone has run into with custom methods? I'm thinking of proxy servers and firewalls but any other areas of concern are welcome. Should I stay on the safe side and just have a URL parameter like action=validate|curate|draft?

答:
I think custom HTTP method is the very best way to implement entity actions. Adding the action to the entity body (POST) doesn't seems right, it's not part of your entity (although the result might be saved in it). Also, using the custom HTTP methods proxies could determine their actions without the need of parsing the entity body.

It's like CRUD, you'd always want to implement those, but also you have your own specific set of actions (per enitity). I really don't see what would be the problem extending those.

Also @Rein Henrichs "You don't DRAFT a post, you transform its draft state to true or you create a draft resource" seems false to me. A drafts property would be used for persistent saving the state, not for making the transformation. Actions don't even necessarily result in a 'state', or be saved in a property. Creating a seperate entity for each state/transformation seems even more fuzzy.. try to maintain the same reference (URI) to the enity.


答:
You are correct that there is a close relationship between VMs and Operating Systems. There is also a close relationship between those two and Programming Languages. In fact, one might argue that they are more or less the same thing!

There is a famous quote by Daniel H. H. Ingalls from the Design Principles of Smalltalk: "an Operating System is a collection of routines missing from the language; there shouldn't be one."

The original Smalltalk machines didn't have an OS, or rather the Smalltalk system was the OS. Even the video drivers were written in Smalltalk. You might also want to look into the design of the AS/400, which is closely related to what you are asking about.


问:
Requirement refinement between two levels of specification
I am currently working on the definition of the documentation architecture of a system, from customers needs to software/hardware requirements.
I encounter a big problem with the level of refinement of requirements.

The classic architecture is : PTS --> SSS --> SSDD --> SRS/HRS
with PTS : Purshaser Technical Specification
SSS : Supplier System Specification
SSDD : System Segment Design Description
SRS / HRS : Software / Hardware Requirement Specification.

Requirements from PTS are reworked in SSS, this document only expressed the needs (no design requirements are defined at this level). Then, the system design is described in SSDD : we allocate requirements from the SSS to functions from the design and functions are then allocated to component (Software or hardware) (we are still at the SSDD level). Finally, for each component, we write one SRS or one HRS. Requirements in SRS or HRS are refinement of requirements from SSS (and traceability matrix are made between these two levels).

My problem is the following one :

Our system is a complex one, and some of the requirements in the SSS needs to be refined twice to be at the right level in the SRS (means that software people can understand the requirement to make their coding). But, with this document architecture, I can only refine once the requirements from the SSS. The second problem is that only a part of the requirements from the SSS needs to be refined twice. The other part only need one refinement.

On the picture below, the green boxes are requirements at the right level for SRS or HRS. And purple boxes are intermediate requirements which can not be included in SSS since they are design requirements. Where can I put these purple requirements ??

Is there someone who has already encountered this problem ? Should I write two documents at SRS level ? Should I include intermediate requirements in SSDD ? Should I includes the two refinement levels (purple and green) in the same SRS document (not sure that's possible since a SRS is only for one component) ???

Thanks for your help and expertise ;-)

答:
If you had a spec that said 'we need a an aircraft carrier with a flag' then it would obviously be insane to split the system into those two parts, and then specify the carrier and the flag to the same level of detail.

Most such formal development standards recognise this, and have some way of tailoring the level of decomposition and traceability to something that makes more sense contextually. The problem is that this tends to get treated as an advanced special case you would learn how to do properly sometime after you have specified and delivered your third aircraft carrier.

Which is a lot to ask of a flag supplier.

I suspect that the issue is that the elements listed in the SSS should be ones that are appropriately sized for being specified in the standard way. If that split is wrong, it should be changed so that it is right. Hopefully that doesn't have contractual implications.


问:
What is the best way to keep both html document structure and php control structure strongly visible in complex view file
I create a lot of views in my work as part of the common MVC pattern applied in web frameworks like Code Igniter and Cake.

I also adhere to the following maxim as best I can: fat models and library objects doing the logic; skinny controllers responding to user actions server side; and views presenting request results and providing UI.

This leads me to a situation where a lot of views are just html, variable echos and for loops, with the accompanying mark up for css and javascript application. Nice and clear.

However, in some cases views need to be more complex. We might need to do different things based on the state of each looped item for example and sometimes this gets quite complex.

The problem with this is that the identing applied to both html and php soon gets out of sync.

A very simple example:

```<code><ul>
   <li> ... </li>
   <li> ... </li>
   <?foreach>
       <li> ... </li>
   <?endforeach>
</ul>
</code>```

We now have li elements that are at the same depth in the output document but not at the same level in the code. If we start using other control structures this can get worse:

```<code><ul>
   <li> ... </li>
   <li> ... </li>
   <?foreach>
       <li> ... </li>
       <?if>
           <li> ... </li>
       <?endif>
   <?endforeach>
</ul>
</code>```

And so on ...

Is there a well used pattern I can follow that minimises this problem even with complex views ?

答:
Conditional compilation in C and C++ suffers from a similar problem: the interleaving of preprocessor directives and C/C++ code.

The usual trick there is to indent them independently of each other. A complex bit of code might look like this:

```<code>{
    ...
    {
        ...
#if A
# if B
        ...
# else
        ...
# endif
#else
        ...
#endif
    }
}
</code>```

As you can see, the preprocessor directives (#if/#else/#endif) use a completely separate indentation than the code they are interleaved with (represented by ...) and even a different number of spaces per indentation level.

For your complex views, you could employ a similar strategy and indent the PHP and HTML independently of each other. You will have to decide for yourself if it improves or hinders the readability.

Body:
Conditional compilation in C and C++ suffers from a similar problem: the interleaving of preprocessor directives and C/C++ code.

The usual trick there is to indent them independently of each other. A complex bit of code might look like this:

{
    ...
    {
        ...
#if A
# if B
        ...
# else
        ...
# endif
#else
        ...
#endif
    }
}

As you can see, the preprocessor directives (#if/#else/#endif) use a completely separate indentation than the code they are interleaved with (represented by ...) and even a different number of spaces per indentation level.

For your complex views, you could employ a similar strategy and indent the PHP and HTML independently of each other. You will have to decide for yourself if it improves or hinders the readability.


问:
Application settings via constructor injection or ambient context
I am developing an application containing a class which loads application settings from a configuration file.

Settings class:

public sealed class MyAppSettings : IMyAppSettings
{
    private IMyAppSettings DefaultSettingsInstance;

    public MyAppSettings()
    {
        DefaultLanguage = "en";

        var config = ConfigurationManager.GetSection("MyAppSettings") as NameValueCollection;

        if (config != null)
        {
            if (!string.IsNullOrEmpty(config["DefaultLanguage"]))
                DefaultLanguage = config["DefaultLanguage"];
        }
    }

    public static IMyAppSettings Default
    {
        get
        {
            return DefaultSettingsInstance ?? (DefaultSettingsInstance = new MyAppSettings());
        }
        set
        {
            if (value == null)
                throw new ArgumentNullException("value");

            DefaultSettingsInstance = value;
        }
    }

    public string DefaultLanguage { get; private set; }

}
```

<p>Class which is using setting (version 1):</p>

<pre class="lang-cs prettyprint-override"><code>public sealed class SomeClass
{
    public SomeFunction()
    {
        // Using language setting here
        if(MyAppSettings.Default.DefaultLanguage == "en")
        {
            ...
        }
    }
}
</code>```

or version 2:

public sealed class SomeClass
{
    private readonly string _defaultLanguage;

    public SomeClass(string defaultLanguage)
    {
        if(string.IsNullOrEmpty(defaultLanguage))
            throw new ArgumentNullException("defaultLanguage");

        _defaultLanguage = defaultLanguage;
    }

    public SomeFunction()
    {
        // Using language setting here
        if(_defaultLanguage == "en")
        {
            ...
        }
    }
}
```

Both classes and interface are located in the same assembly.

What approach is better for a global application settings, pass it via construction injection or use an ambient context or something else?

答:
In your Version 2, the requirement of a default language is explicit, which I think is a good thing.

In Version 1, if you don't look at the source code it will be not obvious where it takes the default language from, and appear like "magic", which is not a good thing.

There's definitely no one-size-fits-all solution for choosing between constructor injection / setter injection / ambient context, but in this particular example I think you'll have a cleaner API and more robust code with constructor injection.


答:
I worked at a company where this was the goal. Snippets of SQL were stored in database tables, read at runtime, and executed. Performance was terrible, as you can imagine, and bugs were frequent.  It was also impossible to debug, with no stack traces or anything else that makes life easy.

"Data-driven programming" results from a fundamental lack of understanding of what we're doing, as programmers; any data that is capable of making an algorithm happen is actually "programming," even if you've somehow managed to mingle (mangle?) the two ideas in the user interface.  Now, this doesn't mean that you can't combine the two ideas from the other direction, so that all code is data; that's the premise behind lisp, which is enabled by its homoiconicity and exploited by its macro system.  Yes, these concepts sound similar, but their implications and applications are very different in practice.

Also, this may be editorializing, but the places I've encountered that want "completely data-driven" programming really don't value their programmers.  They think of the code as a cost center, something to be outsourced, or ignored.

Title: Can a system be 100% Data Driven?


答:
In version 1 it looks like you're using a static reference to look up the setting, which is generally more cumbersome to control in unit-testing (you're writing tests, right?) than a constructor-injected value, as in version 2.

Also with version 2, as Janos says in his answer, the explicit requirement of a default language is a good thing, because consumers will be aware of it at the time code is written, rather than possibly missing it at runtime.

Title: Application settings via constructor injection or ambient context

Body:
I am developing an application containing a class which loads application settings from a configuration file.

Settings class:

public sealed class MyAppSettings : IMyAppSettings
{
    private IMyAppSettings DefaultSettingsInstance;

    public MyAppSettings()
    {
        DefaultLanguage = "en";

        var config = ConfigurationManager.GetSection("MyAppSettings") as NameValueCollection;

        if (config != null)
        {
            if (!string.IsNullOrEmpty(config["DefaultLanguage"]))
                DefaultLanguage = config["DefaultLanguage"];
        }
    }

    public static IMyAppSettings Default
    {
        get
        {
            return DefaultSettingsInstance ?? (DefaultSettingsInstance = new MyAppSettings());
        }
        set
        {
            if (value == null)
                throw new ArgumentNullException("value");

            DefaultSettingsInstance = value;
        }
    }

    public string DefaultLanguage { get; private set; }

}
```

<p>Class which is using setting (version 1):</p>

<pre class="lang-cs prettyprint-override"><code>public sealed class SomeClass
{
    public SomeFunction()
    {
        // Using language setting here
        if(MyAppSettings.Default.DefaultLanguage == "en")
        {
            ...
        }
    }
}
</code>```

or version 2:

public sealed class SomeClass
{
    private readonly string _defaultLanguage;

    public SomeClass(string defaultLanguage)
    {
        if(string.IsNullOrEmpty(defaultLanguage))
            throw new ArgumentNullException("defaultLanguage");

        _defaultLanguage = defaultLanguage;
    }

    public SomeFunction()
    {
        // Using language setting here
        if(_defaultLanguage == "en")
        {
            ...
        }
    }
}
```

Both classes and interface are located in the same assembly.

What approach is better for a global application settings, pass it via construction injection or use an ambient context or something else?