答:
It doesn't seem like too many answers consider programming in teams.  I'm a senior developer and I tend to write comments aimed at explaining what is otherwise simple for me to understand.

I see it as a form of posthumous team communication or education. I encourage the team to look through code they are using, but maybe haven't written to understand it better.

A couple of examples just from this week (PHP code):

```<code>//Pattern for finding jpeg photos
//Case insensitive pattern for jpg and jpeg
const PATTERN_PHOTO = "*.{[jJ][pP][gG],[jJ][pP][eE][gG]}";
</code>```

I'd hope the name PATTERN_PHOTO would be helpful later in code to explain what it does, but without the comments how clear would it be to a junior developer what this specific pattern does?

Same set of code:

```<code>//Ignore . and .. directories in Linux
if($file != "." && $file != "..")
</code>```

There's an expectation that our developers know PHP, but not that they understand the Linux OS we are using for hosting.

So, I find these comments to actually increase the overall efficiency of our team for the very little time it takes to write them.

There's less cases of people rewriting code simply because they don't understand how it works. "I didn't understand how it did what it was supposed to, so I fixed it." Seriously, I've had to deal with this before.

There are less questions asked about individual pieces of code.  Answering the questions just once, usually requires looking up the code and the time for me to re-familiarize myself with it. And sometimes I'll get the same question from more than one person weeks apart. (Yes, it would be on things as simple as the examples above)

Other developers are encouraged and guided to learn on their own.  I'd expect that if they came across //Ignore . and .. directories in Linux they'd likely hop on Google and would suddenly understand Linux a little bit better.

Title: "Comments are a code smell"

Body: A coworker of mine believes that any use of in-code comments (ie, not javadoc style method or class comments) is a code smell.  What do you think?

Body:
It doesn't seem like too many answers consider programming in teams.  I'm a senior developer and I tend to write comments aimed at explaining what is otherwise simple for me to understand.

I see it as a form of posthumous team communication or education. I encourage the team to look through code they are using, but maybe haven't written to understand it better.

A couple of examples just from this week (PHP code):

//Pattern for finding jpeg photos
//Case insensitive pattern for jpg and jpeg
const PATTERN_PHOTO = "*.{[jJ][pP][gG],[jJ][pP][eE][gG]}";

I'd hope the name PATTERN_PHOTO would be helpful later in code to explain what it does, but without the comments how clear would it be to a junior developer what this specific pattern does?

Same set of code:

//Ignore . and .. directories in Linux
if($file != "." && $file != "..")

There's an expectation that our developers know PHP, but not that they understand the Linux OS we are using for hosting.

So, I find these comments to actually increase the overall efficiency of our team for the very little time it takes to write them.

There's less cases of people rewriting code simply because they don't understand how it works. "I didn't understand how it did what it was supposed to, so I fixed it." Seriously, I've had to deal with this before.

There are less questions asked about individual pieces of code.  Answering the questions just once, usually requires looking up the code and the time for me to re-familiarize myself with it. And sometimes I'll get the same question from more than one person weeks apart. (Yes, it would be on things as simple as the examples above)

Other developers are encouraged and guided to learn on their own.  I'd expect that if they came across //Ignore . and .. directories in Linux they'd likely hop on Google and would suddenly understand Linux a little bit better.


问:
How can I combine C/Fortran with JavaScript?
I'm working on a project where I need heavy numerical calculations to be real-time visualized with something flexible like D3.js. Are there frameworks out there that would let me painlessly achieve this? Or do I need to use some sort of "glue" code written in, say, Python?</p>

答:
What you're asking for is interoperation between C/Fortran and Javascript, interopation can be done in a great many ways, the most common two that come to mind however are:

Foreign Function Interface (FFI) bindings and Inter-Process Communication (IPC).

FFI is where a standard agreed upon protocol is built into the assembly at compile time which allows access to the assemblies facilities by foreign languages. There are a variety of FFI standard interfaces, but I am familiar with none that allows an interpreted language to interface with a compiled language or vice versa; the concept doesn't fit with using interpreters as their scripts are plain text with no assemblies to interface.

That said, the interpreter may have facilities for FFI access. Node.JS comes to mind as a javascript interpreter that likely has facilities for FFI access. Alternatively you could use your C program to host a javascript interpreter like Chrome's V8 engine to have your C execute the javascript directly if this would help you, though I don't suspect it would.

Inter-Process Communication (IPC) is where you run the process in one language, and the other process in the other language, and then use a form of IPC to communicate between the two. Commonly this is done via shared memory space where both processes can put data into that memory space and read from it to send messages back and forth. The same technique is used with files sometimes though this is a high-overhead technique due to the IO costs, it does however afford a durability in the event of either or both processes crashing, or even the machine losing power.

The shared memory technique for IPC often  comes in a couple flavors:

A parent process calling a child process and using "pipes" to send messages directly to the child process. I believe Node.JS suggests this technique for it to interoperate with other languages

Named pipes which are system-wide shared memory spaces registered with the kernel and accessible by an identifier

Other than shared memory or disk, the other largely used IPC technique is network communication. Whenever your browser accesses a website it is communicating with another process, so this is IPC, and in this way web services have become a hugely common way of accomplishing IPC. Direct TCP techniques are still used as well as even UDP for certain tasks, but the point is the network may act as a communication layer between processes just as a memory space or disk space may.

All of this said, my suggestion is you use web services for IPC as your language interoperation technique, because you wish for visualization on a browser.

That's all. I hope you learned something, carry on :)

ContentLicense: CC BY-SA 3.0


问:
How old is "too old"?
I've been told that to be taken seriously as a job applicant, I should drop years of relevant experience off my résumé, remove the year I got my degree, or both. Or not even bother applying, because no one wants to hire programmers older than them.1

Or that I should found a company, not because I want to, or because I have a product I care about, but because that way I can get a job if/when my company is acquired.

Or that I should focus more on management jobs (which I've successfully done in the past) because… well, they couldn't really explain this one, except the implication was that over a certain age you're a loser if you're still writing code. But I like writing code.

Have you seen this? Is this only a local (Northern California) issue?

If you've ever hired programmers:2

Of the résumés you've received, how old was the eldest applicant?

What was the age of the oldest person you've interviewed?

How old (when hired) was the oldest person you hired?

How old is "too old" to employed as a programmer?

1 I'm assuming all applicants have equivalent applicable experience. This isn't about someone with three decades of COBOL applying for a Java guru job.

2 Yes, I know that (at least in the US) you aren't supposed to ask how old an applicant is. In my experience, though, you can get a general idea from a résumé.

答:
Personally, I wouldn't want a job that I had to get by hiding how old I was (Full disclosure:  I'm 27, so that isn't an issue for me).

Or that I should focus more on management jobs (which I've successfully done in the past) because… well, they couldn't really explain this one, except the implication was that over a certain age you're a loser if you're still writing code. But I like writing code.

I think you have 2 things working against you:

Older programmers have more experience and cost more money.

I think that software shops in Silicon Valley (if you'd consider that Northern California) have adapted an "up or out" mentality.  If you've reached a certain age and you haven't started your own company or at least taken on a leadership role in one, then you must either really not be passionate about the software business or you must not be that good at it.

I'm not saying these lines of reasoning are correct or that I agree with them.  I'm just saying that's the way it is.  These two issues are going to be a big deal if you want a startup job, but they will probably be less of an issue if you are finding a job for a more established company.

Lastly, have you considered applying for a Tech Lead or Architect job?  They're "more advanced" positions that aren't necessarily management gigs.

ContentLicense: CC BY-SA 2.5

Title: How old is "too old"?


问:
Help me understand a part of Java Language Specification
I'm reading part 17.2.1 of Java language specification: http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.2.1

I won't copy a text, it's too long, but I would like to know, why for third step of sequence they're saying that

If thread t was removed from m's wait set in step 2 due to an interrupt

Thread couldn't get to step 2 it wasn't removed from wait set, because it written for the step 1:

Thread t does not execute any further instructions until it has been removed from m's wait set

Thus thread can't be removed from wait set in step 2 whatever it's due to, because it was already removed.

Please help me understand this.

答:
That is probably just a typo and is meant to be step 1.

Typos happen even in large projects and where a lot of people work on it.


问:
Render Ruby object to interactive html
I am developing a tool that discovers network services enabled on host and writes short summary on them like this:
```
init,1
└── login,1560 --
    └── bash,1629
        └── nc,12137 -lup 50505
{
    :net => [
        [0] "*:50505 IPv4 UDP "
    ],
    :fds => [
        [0] "/root (cwd)",
        [1] "/",
        [2] "/bin/nc.traditional",
        [3] "/xochikit/ld_poison.so (stat: No such file or directory)",
        [4] "/dev/tty2",
        [5] "*:50505"
    ]
}
```

It proved to be very nice formatted and useful for quick discovery thanks to colors provided by the awesome_print gem. However, its output is just a text. One issue is that if I want to share it, I lose colors. I'd also like to fold and unfold parts of objects, quickly jump to specific processes and what not? Adding comments, for example. Thus I want something web-based.

What is the best approach to implement features like these? I haven't worked with web interfaces before and I don't have much experience with Ruby.

Update:

Maybe I didn't write my willings clear enough.

I think I want something that behaves like Chromium's chrome://chrome/settings/ (or better, of course). Is there maybe an erb template or manual showing how to implement that? I thought this is a thing needed often enough.

答:
Sinatra would probably be the lightest approach.

Rails brings more overhead, but is also very easy to use and deploy.  If your app will be expanding and be incorporating things like users and logins, it might be better to tackle Rails.

Both should work fine, and there are lots of coding examples and tutorials for basic applications using each.

Title: Render Ruby object to interactive html


问:
Relationship between "Task Parallel Library" and "Task-based Asynchronous Pattern"?
In the context of C#, .NET 4/4.5 used for an application running on a web-server, what is the relationship between "Task Parallel Library" and "Task-based Asynchronous Pattern"?

I understand one is a library and the other is a pattern. But to dig deeper, is it like "The library is used by the pattern to enforce good practices". I'm also not clear if both are supported in .NET 4.0 (with await and async keywords)

Edit: Seems that await and async are only in .NET 4.5 ...

答:
The TPL is a new(ish) framework that provides a simplified API for concurrent programming. The Task-Based Asynchronous Pattern is a framework design guideline that leverages the TPL to deliver consistently designed concurrent operations.

The async/await keywords are syntactic sugar that allow you to consume TAP APIs without diving into the details of continuation.


问:
What programming languages exist for mechatronics purposes?
Is there any specific language which is designed for mechatronics programming? I know about LabView, which is a data flow language, but not sure about its main platform.

Could you recommend to me, some languages apart from c/c++? Any language which is used in the topic of mechatronics( robotics, sensor programming, etc ).

答:
I am using the Wikipedia definition of mechatronics, which would encompass things as common as a Dishwashers, ABS brakes in cars etc. </p>

<p>@Scott gives a very good answer for products focused on field programmable and bespoke systems. Another class of products - specific products developed with production runs of 1000's or millions of units in mind (Toys/cameras, automotive systems such as ABS, remote door opening etc.....), Software in these automation systems are indistinguishable from traditional embedded systems, using identical software tools and development practices. Most of these are programmed in general purpose languages focused on reliability, efficiency and ability to run on resource constrained systems (obviously tradeoffs have to be made). These include as C/C++ (most common so must be mentioned despite the question) Modulo2, Ada, D.  More recently Erlang was designed with principles more aligned to mechatronics than most general purpose languages. </p>

Body:
I am using the Wikipedia definition of mechatronics, which would encompass things as common as a Dishwashers, ABS brakes in cars etc.

@Scott gives a very good answer for products focused on field programmable and bespoke systems. Another class of products - specific products developed with production runs of 1000's or millions of units in mind (Toys/cameras, automotive systems such as ABS, remote door opening etc.....), Software in these automation systems are indistinguishable from traditional embedded systems, using identical software tools and development practices. Most of these are programmed in general purpose languages focused on reliability, efficiency and ability to run on resource constrained systems (obviously tradeoffs have to be made). These include as C/C++ (most common so must be mentioned despite the question) Modulo2, Ada, D.  More recently Erlang was designed with principles more aligned to mechatronics than most general purpose languages.


问:
Tension between the dependency inversion principle and avoiding "new" in C++?
I have seen a lot of advice that it is better to do Type object; than

```<code>Type* object = new Type();
</code>```

in C++ whenever possible—i.e., minimize your use of new. I understand the rational behind this and appreciate it.

But according to my understanding, to practice dependency inversion requires pointers, e.g.:

```<code>Type* object = new Implementation();
</code>```

where Type is abstract (i.e. contains at least one pure virtual method) and Implementation is concrete. It is not possible to do

```<code>Type object = Implementation();
</code>```

because what that means is

```<code>Type object;
object = Implementation();
</code>```

which requires constructing object as a Type initially—but that cannot be done, since Type is abstract.

Is there an inherent tension between the dependency inversion principle and avoiding new when using C++? If so, what patterns/principles/practices can be used to mitigate this tension?

答:
No, dependency inversion does not necessitate pointers.

The decision to use pointers or not is a question of data sharing and memory optimization.

Dependency inversion has to do with class hierarchies and inhertiance.

All the same places where you expect to pass Type* that was allocated with new Type() can be changed to accept a Type& that is created with type = Type().


问:
How to implement a safe password history
Passwords shouldn't be stored in plain text for obvious security reasons: you have to store hashes, and you should also generate the hash carefully to avoid rainbow table attacks.

However, usually you have the requirement to store the last n passwords and to enforce minimal complexity and minimal change between the different passwords (to prevent the user from using a sequence like Password_1, Password_2, ..., Password_n). This would be trivial with plain text passwords, but how can you do that by storing only hashes?

In other words: how it is possible to implement a safe password history mechanism?

答:
First, you could store the hashes for the last "n" previous passwords, so you can check if their new password duplicates a previous password.  You also have the plain text of their current password (because they have logged in or provided it to you to authenticate their password change request), and their new password, so you can check for minimal changes between these two passwords.

If (for you) it is very important to directly compare these two passwords against "n" previous passwords, then you have to store these passwords (encrypted) to be able to retrieve them later.

While it could be seen as a security flaw to do this, encryption methods could be implemented to provide sufficient security.

Store each password (encrypted), for the last "n" passwords.

Store the date and time that the most recent password was created.

Store the hash of the most recent password.

Encrypt all the passwords using the hash (salted) of the current password, and the password creation timestamp, and perhaps something like an account number or email address.

Un-encrypt and re-encrypt all the passwords each time a new password is created.

Then, anytime the password is changed, you can un-encrypt all the old passwords, and do all your minimal-change tests.

Now, if someone had this person's password, and knew all the other necessary details they could un-encrypt this information for this one person. But if they already have this person's password, they can already login as this person and access this person's account.

Also, for the old passwords, they may not have to be stored in strictly plain text. They could be stored in some obfuscated way. Or stored as an alphabetical list of the characters in the password.

I'm not saying this is a recommended thing to do in the general case, but assuming the task you have described is required in your case, then this is one way to accomplish it with some security measures.


问:
how do early stage startups hire ninja programmers
I am programmer who just started working on a startup idea. At the moment I want to bring onboard at least one programmer. This programmer should be a ninja - a 10x engineer.

Since early days are probably the most risky for a startup, I want to make sure I approach this problem the best I can.

How do I find these people? and How do I convince them to come onboard?

I would love to hear from people who started their own companies and what their thoughts are about hiring

Update: I would like to get the ninja as a co-founder so besides being a ninja (ie. great programmer with computer science background) he/she has to have a healthy appetite for risk (for great programmers this is not a big deal because they can be hired anytime into mainstream jobs if the startup doesn't work)

答:
According to "Rapid Development", the following factors are the top 10 motivators for programmers.

Achievement

Possibility for growth

Work itself

Personal life

Technical-supervision opportunity

Advancement

Interpersonal relations, peers

Recognition

Salary

Responsibility

If you want to hire a top-tier developer, you need to consider what's most important to software developers and offer your prospective developer what he wants: a challenging, satisfying project that won't take over his life.


问:
Using machine learning to aim mirrors in a solar array?
I've been thinking about solar collectors where several independent mirrors to focus the light on a solar collector, similar to the following design from Energy Innovations.

Because there will be flaws in the assembly of this solar array, I am proceeding with the following assumptions (or lack thereof):

The software knows the "position" of each mirror, but doesn't know how this position relates to the real world or to other mirrors.  This will account for poor mirror calibration or other environmental factors which may effect one mirror but not the others.

If a mirror moves 10 units in one direction, and then 10 units in the opposite direction, it will end up where it originally started.

I would like to use machine learning to position the mirrors correctly and focus the light on the collector.  I expect I would approach this as an optimization problem, optimizing the mirror positions to maximize the heat inside the collector and the power output.

The problem is finding a small target in a noisy high-dimensional space (considering each mirror has 2 axis of rotation).  Some of the problems I anticipate are:

cloudy days, even if you stumble upon the perfect mirror alignment, it might be cloudy at the time

noisy sensor data

the sun is a moving target, it moves along a path, and follows a different path every day - although you could calculate the exact position of the sun at any time, you wouldn't know how that position relates to your mirrors

My question isn't about the solar array, but possible machine learning techniques that would help in this "small target in a noisy high dimensional-space" problem.  I mentioned the solar array because it was the catalyst for this question and a good example.

What machine learning techniques can find such a small target in a noisy high-dimensional space?

EDIT:

A few additional thoughts:

Yes, you can calculate the suns position in the real world, but you don't know how the mirrors position is related to the real world (unless you've learned it somehow).  You might know the suns azimuth is 220 degrees, and the suns elevation is 60 degrees, and you might know a mirror is at position (-20, 42); now tell me, is that mirror correctly aligned with the sun?  You don't know.

Lets assume you have some very sophisticated heat measurements, and you know "with this heat level, there must be 2 mirrors correctly aligned".  Now the question is, which two mirrors (out of 25 or more) are correctly aligned?

One solution I considered was to approximate the correct "alignment function" using a neural network which would take the suns azimuth and elevation as input and output a large array with 2 values for each mirror which correspond to the 2 axis of each mirror.  I'm not sure what the best training method is though.

More thoughts:

The mirrors do have a coordinate system which the software has access to, but the software doesn't know how this coordinate system relates the the real world.  Let's say a mirror is at position (4, 42); what does that mean?  I don't know and neither does the software.  But I do know that if I move the mirror around and then move it back to (4, 42) the mirror will be in the same position it was previously.  Additionally, two mirrors may be at position (4, 42) but be pointing in opposite directions in the real world.

Yes, with a lot of quality sensors the problem is easy to solve.  Energy Innovations is out of business as best I can tell, probably because they used a bunch of really awesome sensors and people said "I'll just buy solar panels, they're cheaper."

The only sensors in the system are in the collector head.

答:
Sun paths can be predicted, so I imagine you can get the mirror aligned pretty closely already if you know the time of day, the day of the year, and the latitude and longitude.

You don't need machine learning for this.

If you have mirrors that don't know which way they're pointed (i.e. you can't correlate their position with elevation and azimuth measurements), you could try using a camera with a wide field of view, sweeping the sky until a bright white spot appears in the camera's view.  You can then move the mirror towards that spot (using some simple x/y calculations), until the bright spot is centered in the camera's view.  Put a dark filter on the camera so that all it sees is the sun.

However, your question states that you do know where the mirrors are pointed.  If you have sensors on the mirrors that tell you how they are positioned, then you have the capability to correlate those position measurements to actual azimuth and elevation numbers.

I would imagine it would be pretty simple to detect cloudy days with a single solar cell, or the absence of any heat on the mirror.


问:
What are the advantages of Maven when it comes to single man, educational projects
I've spend a few hours playing around with Maven + reading some stuff on the apache official site and also a few random googled articles. By this I mean that I really tried to find the answers myself - both by reading and by doing things on my own. Also maybe worth to mention that I installed the m2e plugin so most of the time I've tried things out from Eclipse and not using the command line too much.

However aside from the generated project that for example prevent me from using the default package I didn't see that much of a difference with the standard way I've created my projects before try Maven. In fact I've almost decided to skip Maven for now and move on to the other technology I wanted to learn more in-depth - Hibernate, but when I start with opening the official page the first thing I've read was the recommendation to use Hibernate with Maven.

That get me confused and made me taking a step back and trying once more to find what I'm obviously missing right now.

As it's said in the maven.apache.. site, the true strength of Maven is shown when you work on large projects with other people, but I lack the option to see how Maven is really used in this scenario, still i think that there are maybe advantages even when it comes to working with small projects alone, but I really have difficulties to point them out.

So what do you think are the advantages of Maven when it's used for small projects writing from a single person. What are the things that I should be aware of and try to exploit (I mean features offered by Maven) that can come in handy in this situations?

答:
I find the management of dependencies a big win even for solo projects. Adding some XML to the pom.xml and letting Maven worry about downloading the JAR, and putting it in the right place is really convenient.

Using the command line for building and deploying is usually much quicker. Once you have some commands in the history, six keystrokes can build, run all tests, and deploy to Tomcat. Building with Eclipse and deploying through the Tomcat Manager is about 12 careful clicks.

Even if your project stays solo, you may want to use a different machine at some point. With Maven, there is no need to have JARs in version control, just clone your GitHub repo, and mvn package will get everything you need.

Finally, you wouldn't learn Hibernate if you didn't plan on working on big projects. These days, most of those big products are built with Maven, so your time won't be wasted.

One tip: Make sure to install the Eclipse m2 plugin, otherwise Eclipse won't understand your code.


问:
Synchronize Azure SQL (cloud) with Azure SQL Emulator (local)?
We have an Azure service (web role) that heavily depends on the database. For offline development/testing, we'd like to have the app+db run offline within the emulators. Running the webrole itself within the emulator is straightforward but doing so for the Azure SQL isn't so.

What is the simplest way to ensure that the cloud Azure SQL database and the emulator/local Azure SQL database are in sync? We can afford some level of staleness for simplicity of sync operation (meaning it's ok for the local copy to be a few hours stale versus mirroring every write as soon as it happens)

Thanks

答:
Ran into the solution a while back, though I'd update it here:

Azure SQL cannot be run inside the emulators. You have to use SQL Server (Express/Standard/whatever) and modify the connection string in the web.config. Azure SQL and SQL server are close enough that it's doable most of the time. But since Azure SQL is a noticable sub-set of SQL server (feature wise), you will most likely run into some situations where that difference shows up and you have to spend many hours figuring out what the unsupported SQL statement is and what the fix it.

Azure SQL needs to have emulator and synchronization support.


问:
Is this an acceptable approach to undo/redo in Python?
I'm making an application (wxPython) to process some data from Excel documents. I want the user to be able to undo and redo actions, even gigantic actions like processing the contents of 10 000 cells simultaneously. I Googled the topic, and all the solutions I could find involves a lot of black magic or is overly complicated.

Here is how I imagine my simple undo/redo scheme. I write two classes - one called ActionStack and an abstract one called Action. Every "undoable" operation must be a subclass of Action and define the methods do and undo. The Action subclass is passed the instance of the "document", or data model, and is responsible for committing the operation and remembering how to undo the change.

Now, every document is associated with an instance of the ActionStack. The ActionStack maintains a stack of actions (surprise!). Every time actions are undone and new actions are performed, all undone actions are removed for ever. The ActionStack will also automatically remove the oldest Action when the stack reaches the configurable maximum amount.

I imagine the workflow would produce code looking something like this:

```<code>class TableDocument(object):
    def __init__(self, table):
        self.table = table
        self.action_stack = ActionStack(history_limit=50)

    # ...

    def delete_cells(self, cells):
        self.action_stack.push(
            DeleteAction(self, cells)
        )

    def add_column(self, index, name=''):
        self.action_stack.push(
            AddColumnAction(self, index, name)
        )

    # ...

    def undo(self, count=1):
        self.action_stack.undo(count)

    def redo(self, count=1):
        self.action_stack.redo(count)
</code>```

Given that none of the methods I've found are this simple, I thought I'd get the experts' opinion before I go ahead with this plan. More specifically, what I'm wondering about is - are there any glaring holes in this plan that I'm not seeing?

答:
Your approach is just an application of the classical command pattern, not specific for Python, and well known for implementing undo functionality. Google for "command pattern undo" to find examples, even small ones which are not overly complicated.

And yes, if you do it right, you can implement undo functionality that way. If it will be easy or hard to implement, depends much on the kind of Actions you are going to have, and the amount of data to process. In theory, before applying an action, you could save the whole state of your data, and reset to that state whenever you want to "undo". That will make it possible to implement your undo in a simple, generic way, without having the need of individual "undo" operations. However, if you have a huge amount of data to process, and you want a big undo stack, then this approach is often not feasible due to memory constraints. If that is your situation, a specific saving of the modified data and a corresponding "undo" implementation can be much more efficient, but only if you have lots of "small" actions to process which will allow such an optimization.

Further key points:

do you expect parallel Actions to be applied to the data, by multiple threads or processes or users? That can make the implementation of "undo" very hard.

do you expect Actions where you cannot foresee which part of your data will be changed? In this case, you will need a "save all" approach for that actions, which may or may not be a problem, see above.


问:
Programming a trading strategy
Excuse me if I'm not descriptive enough, as I do not have much of a background when it comes to these things:

How would I go about coding a primitive trading strategy and link it to some sort of artificial trading environment? Where do I start, and what are some other essential questions I should be asking?

I am interested more in doing this because it interests me than making returns. Ideally it utilizes random/historical market data and doesn't actually execute any real trades.

My background: I'm almost done my undergrad degree in computer science, and have had intro finance and economic courses. Familiar mostly with C and Java.

答:
Check out the class Computational Investing, in Coursera.  Its currently in week 5 of a planned 8 weeks, but you may be able to catch up.

The class uses Python, and a tool kit written by the professor.  https://github.com/tucker777/QSTK

The tool kit pulls data from Yahoo (free) and has tools to generate and evaluate portfolios of stocks.

Title: Programming a trading strategy


答: If you want live up to date information you will have to pay for it through a vendor such as Telvent. If a 15 to 20 minute delay is acceptable then you can scrape information from Yahoo or Google finance.</p>

Title: Programming a trading strategy

Body:
Excuse me if I'm not descriptive enough, as I do not have much of a background when it comes to these things:

How would I go about coding a primitive trading strategy and link it to some sort of artificial trading environment? Where do I start, and what are some other essential questions I should be asking?

I am interested more in doing this because it interests me than making returns. Ideally it utilizes random/historical market data and doesn't actually execute any real trades.

My background: I'm almost done my undergrad degree in computer science, and have had intro finance and economic courses. Familiar mostly with C and Java.


问:
Can I use a project code which has New BSD license but uses a GPL license library?
I want to use the ICSOpenVpn project source code in my commercial application.

If we see the ICSOpenVpn project, it states that its license is New BSD but the libopenvpn.so library it uses is under GNU GPLv2 license.

As per FAQ for version 2 of GNU GPL "If a library is released under the GPL (not the LGPL), does that mean that any program which uses it has to be under the GPL?" The answer says: "Yes, because the program as it is actually run includes the library."

Also, how could ICSOpenVpn change the license to New BSD?

答: You missed the part where ICS-OpenVPN says "Please [note] that openvpn used by this project is under GPLv2. Only the GUI is BSD licensed."  Presumably the GUI is not bound tightly enough to OpenVPN's libopenvpn to require it to be GPL.</p>


问:
Time allocated to code reviews
If you are doing code reviews

How much time do you spend on code reviews, compared to implementation?

How much of the changes undergo code review?

you think it's to much / should be more?

Are there any studies about effectiveness?

[edit] thank you all for the answers, it's hard to pick a "winner" for such a question, there is lots of valuable info in the other replies, too.

答:
At my work we have the following procedure for code reviews.  It has worked well for us so far, and we found it to be very time-efficient, especially in terms of man-hours.  We do not really have any specific time allocated to the reviews.  Every commit or merge to the trunk must be reviewed, and it takes as long as it takes for the reviewer to ok it.

Edit:
The time it takes, of course, depends on the magnitude of the change.  Small features and bug fixes take minutes.  Large new features, refactorings, or changes that affect many parts of the system can take half a day to review and another day to address all the issues that come up as a result.

To make this system work it is crucial to commit to the trunk often, so that the changes are of manageable size.  You do not want to have the situation when you have to review a year's worth of somebody's code.

Title: Time allocated to code reviews


答:
DIP means instead of instantiating objects of class Foo directly in class Bar, you have an abstract interface IFoo, and pass already constructed objects of type IFoo into Bar (for example, through the constructor of Bar). That allows you easily to replace Foo objects in Bar by MockFoo objects for example, for testing purposes. If those objects are constructed on the stack like

```<code> Foo foo;
 Bar bar(&foo);
</code>```

or dynamically like

```<code> IFoo *foo = new Foo();
 Bar bar(foo);
</code>```

where Bar constructor has the signature

```<code>  Bar(IFoo *foo)
  {
       //...
  }
</code>```

does not matter in terms of DIP, that decision does only depend on the intended life time of your object foo (which in both cases should be at least as long as the lifetime of bar).

The pointer variant is technically different on one aspect: if you like, you can transfer the ownership of the foo object to bar and let bar do the memory cleanup by calling delete on fooin the destructor. However, it is questionable if this is good programming style, and I would not recommend that technique to you. If you want to have automatic cleanup of foo when bar does not need it any more, I suggest that you better make use of smart pointers.

To your edit: I guess you have a misconception here about pointers and "new". This code

```<code> Implementation object;
 Type *ptrObject = &object;
</code>```

gives you a pointer to an object of type Type without using new. Or in short:

```<code>"Avoiding the usage of new" != "You cannot have pointers"
</code>```

(and both is irrelevant to the dependency inversion principle).

Title: Tension between the dependency inversion principle and avoiding "new" in C++?

Body:
I have seen a lot of advice that it is better to do Type object; than

```<code>Type* object = new Type();
</code>```

in C++ whenever possible—i.e., minimize your use of new. I understand the rational behind this and appreciate it.

But according to my understanding, to practice dependency inversion requires pointers, e.g.:

```<code>Type* object = new Implementation();
</code>```

where Type is abstract (i.e. contains at least one pure virtual method) and Implementation is concrete. It is not possible to do

```<code>Type object = Implementation();
</code>```

because what that means is

```<code>Type object;
object = Implementation();
</code>```

which requires constructing object as a Type initially—but that cannot be done, since Type is abstract.

Is there an inherent tension between the dependency inversion principle and avoiding new when using C++? If so, what patterns/principles/practices can be used to mitigate this tension?

Body:
DIP means instead of instantiating objects of class Foo directly in class Bar, you have an abstract interface IFoo, and pass already constructed objects of type IFoo into Bar (for example, through the constructor of Bar). That allows you easily to replace Foo objects in Bar by MockFoo objects for example, for testing purposes. If those objects are constructed on the stack like

 Foo foo;
 Bar bar(&foo);

or dynamically like

 IFoo *foo = new Foo();
 Bar bar(foo);

where Bar constructor has the signature

  Bar(IFoo *foo)
  {
       //...
  }

does not matter in terms of DIP, that decision does only depend on the intended life time of your object foo (which in both cases should be at least as long as the lifetime of bar).

The pointer variant is technically different on one aspect: if you like, you can transfer the ownership of the foo object to bar and let bar do the memory cleanup by calling delete on fooin the destructor. However, it is questionable if this is good programming style, and I would not recommend that technique to you. If you want to have automatic cleanup of foo when bar does not need it any more, I suggest that you better make use of smart pointers.

To your edit: I guess you have a misconception here about pointers and "new". This code

 Implementation object;
 Type *ptrObject = &object;

gives you a pointer to an object of type Type without using new. Or in short:

"Avoiding the usage of new" != "You cannot have pointers"

(and both is irrelevant to the dependency inversion principle).


答:
For this sort of application, a field of mirrors trying to point at a solar collector, you can very much calculate where you think the sun should be, where the mirrors should be, what angle they should be at, and how to position them so they point towards your collector. You know, a mathematical model. It'll be close. Probably close enough.

As for calibrating the mirrors to deal with imperfections and deviations from your model:

Wiggle one mirror at a time. If your output increases, keep the change. Store the alteration as calOffset. Call it done.

I agree with Harvey, machine learning is overkill for this.

But hey, let's say you want an mobile autonomous system that can wake up after a long nap and go find the sun. And we can't afford a $0.05 battery to keep time. And since it's mobile, the sun could be in god-knows what direction. And all the humans are dead. And our robotic solar-cell overlords had a serious binger and they don't know what part of the world they woke up on. And their GPS can't pick up a signal. And none of their buddies know what happened.

1) Sweep the area with one mirror and note any spikes in power output. Repeat that a few times to make sure it wasn't a cloud or something.

2) You now know the position of the sun. Go git it.

3) Wait an hour.

4) Sweep the whole area again with a mirror. Spikes. Clouds. Yada yada.

5) You now know the path of the sun. Follow it till you reach the limit of your servos or until the power drops off

6) Rotate 180 degrees and wait 12 hours.

7) Do the sweep thing.

8) From the difference in the sun's setting position and the rising position, you now roughly know your latitude/season*. (At least, your offset from the equator. Still don't know north from south). Adjust accordingly.

9) Wait a day. Note the difference in the sunrise location. You now know what side of the solstice you're on.

10) Wait upwards towards 6 months. Note where the direction of sunrise peaks.  You now know if you're in winter or summer, and can safely figure out the path of the sun for the next EON.

If any of the steps with "now you know" aren't clear, the answer is MATH (and the orbital mechanics of Earth**). Mr. Math is your friend. He can tell you things. And unless the axiom of equality or some such turns out to be false, you can even trust him.

*Offer not valid in the Arctic or Antarctic circles.

**Offer also not valid on Mars, Venus, Titan, Io, and other select locations.

Title: Using machine learning to aim mirrors in a solar array?

Body:
I've been thinking about solar collectors where several independent mirrors to focus the light on a solar collector, similar to the following design from Energy Innovations.

Because there will be flaws in the assembly of this solar array, I am proceeding with the following assumptions (or lack thereof):

The software knows the "position" of each mirror, but doesn't know how this position relates to the real world or to other mirrors.  This will account for poor mirror calibration or other environmental factors which may effect one mirror but not the others.

If a mirror moves 10 units in one direction, and then 10 units in the opposite direction, it will end up where it originally started.

I would like to use machine learning to position the mirrors correctly and focus the light on the collector.  I expect I would approach this as an optimization problem, optimizing the mirror positions to maximize the heat inside the collector and the power output.

The problem is finding a small target in a noisy high-dimensional space (considering each mirror has 2 axis of rotation).  Some of the problems I anticipate are:

cloudy days, even if you stumble upon the perfect mirror alignment, it might be cloudy at the time

noisy sensor data

the sun is a moving target, it moves along a path, and follows a different path every day - although you could calculate the exact position of the sun at any time, you wouldn't know how that position relates to your mirrors

My question isn't about the solar array, but possible machine learning techniques that would help in this "small target in a noisy high dimensional-space" problem.  I mentioned the solar array because it was the catalyst for this question and a good example.

What machine learning techniques can find such a small target in a noisy high-dimensional space?

EDIT:

A few additional thoughts:

Yes, you can calculate the suns position in the real world, but you don't know how the mirrors position is related to the real world (unless you've learned it somehow).  You might know the suns azimuth is 220 degrees, and the suns elevation is 60 degrees, and you might know a mirror is at position (-20, 42); now tell me, is that mirror correctly aligned with the sun?  You don't know.

Lets assume you have some very sophisticated heat measurements, and you know "with this heat level, there must be 2 mirrors correctly aligned".  Now the question is, which two mirrors (out of 25 or more) are correctly aligned?

One solution I considered was to approximate the correct "alignment function" using a neural network which would take the suns azimuth and elevation as input and output a large array with 2 values for each mirror which correspond to the 2 axis of each mirror.  I'm not sure what the best training method is though.

More thoughts:

The mirrors do have a coordinate system which the software has access to, but the software doesn't know how this coordinate system relates the the real world.  Let's say a mirror is at position (4, 42); what does that mean?  I don't know and neither does the software.  But I do know that if I move the mirror around and then move it back to (4, 42) the mirror will be in the same position it was previously.  Additionally, two mirrors may be at position (4, 42) but be pointing in opposite directions in the real world.

Yes, with a lot of quality sensors the problem is easy to solve.  Energy Innovations is out of business as best I can tell, probably because they used a bunch of really awesome sensors and people said "I'll just buy solar panels, they're cheaper."

The only sensors in the system are in the collector head.


问:
Allowing client to select data to return via REST interface
I have a rest service that is essentially a proxy to a variety of other services. So if I call

GET /users/{id}

It will get their user profile, as well as order history, and contact info, etc... all from various services, and aggregates them into one nice object. My problem is that each call to a different service has the potential to add time to the original request, so we would rather not get ALL the data ALL of the time if a particular client does not care about all of the pieces.

A solution I have arrived at is to do something like this:

GET /users/{id}?includeOrders=true&includeX=true&includeY=true...

That works, and it allow me to do only what I need to, but it is cumbersome. We have added enough different data sources that there are too many parameters for that style to be useful. I could do something similar with a single integer and a bitmask or something, but that only makes it harder to read, and it does not feel very Restful.

I could break it down into multiple calls so they would need to call /users/{id}/orders and /users/{id}/profile separately, but that sort of defeats the purpose of an aggregating proxy, who's purpose is to make clients jobs easier.

Are there any good patterns that can help me return just enough data for each client, without making it too difficult for them to filter and select what they want?

答:
I would suggest returning your minimum subset of data with links to the other related data.

If you currently have something like the following:

```<code><user>
    <id>1</id>
    <defects>
        <defect id="100" ... />
        <defect id="101" ... />
        <defect id="102" ... />
    </defects>
    <stories>
        <story id="10" ... />
        <story id="11" ... />
    </stories>
</user>
</code>```

You could turn that into:

```<code><user>
    <id>1</id>
    <defects>
        <link rel="defects" href="http://example.org/users/1/defects">
    </defects>
    <stories>
        <link rel="stories" href="http://example.org/users/1/stories">
    </stories>
</user>
</code>```

Then your client could follow the links to get the data that it cares about.

Alternatively, you could treat the options as a single sub-resource of users, and depending on how you're able to respond to certain requests, it could be done in a single method.

To continue my example, you might accept the following URIs:

```<code>GET /users/{id}/defects
GET /users/{id}/stories
GET /users/{id}/defects_stories
GET /users/{id}/stories_defects
</code>```

In Spring we could accomplish this with something like:

```<code>@RequestMapping(value = "/users/{id}/{options}", method = RequestMethod.GET)
public @ResponseBody String get(@PathVariable Long addressId, @PathVariable String options) {
    // parse options, retrieve data, etc
    return "your xml";
}
</code>```

Based on your additional comments, this might be the best approach, if you can handle it without adding too much complexity. I would avoid Robert Harvey's suggestion of using true/false parameters separated by slashes, because it abuses the hierarchical meaning of URIs; plus it's even less readable than your current solution.

Ultimately, there's nothing in REST against using URL parameters (since REST isn't specific to HTTP); see this related SO question about URL parameters & REST.

Body:
I would suggest returning your minimum subset of data with links to the other related data.

If you currently have something like the following:

<user>
    <id>1</id>
    <defects>
        <defect id="100" ... />
        <defect id="101" ... />
        <defect id="102" ... />
    </defects>
    <stories>
        <story id="10" ... />
        <story id="11" ... />
    </stories>
</user>

You could turn that into:

<user>
    <id>1</id>
    <defects>
        <link rel="defects" href="http://example.org/users/1/defects">
    </defects>
    <stories>
        <link rel="stories" href="http://example.org/users/1/stories">
    </stories>
</user>

Then your client could follow the links to get the data that it cares about.

Alternatively, you could treat the options as a single sub-resource of users, and depending on how you're able to respond to certain requests, it could be done in a single method.

To continue my example, you might accept the following URIs:

GET /users/{id}/defects
GET /users/{id}/stories
GET /users/{id}/defects_stories
GET /users/{id}/stories_defects

In Spring we could accomplish this with something like:

@RequestMapping(value = "/users/{id}/{options}", method = RequestMethod.GET)
public @ResponseBody String get(@PathVariable Long addressId, @PathVariable String options) {
    // parse options, retrieve data, etc
    return "your xml";
}

Based on your additional comments, this might be the best approach, if you can handle it without adding too much complexity. I would avoid Robert Harvey's suggestion of using true/false parameters separated by slashes, because it abuses the hierarchical meaning of URIs; plus it's even less readable than your current solution.

Ultimately, there's nothing in REST against using URL parameters (since REST isn't specific to HTTP); see this related SO question about URL parameters & REST.


问:
Can HTML injection be a security issue?
I recently came across a website that generates a random adjective, surrounded by a prefix and suffix entered by the user. For example, if the user enters "123" for prefix, and "789" for suffix, it might generate "123Productive789". I've been screwing around with it, and I thought I might try something out. I entered this into the prefix field:

```<code>Click<hr />
</code>```

And, sure enough, I was given the link, then an
, then a random adjective. What I'm wondering is, could this be dangerous? There must be many more websites out there that have this issue, are all of them vulnerable to some sort of php injection?

答:
It would appear that this site is vulnerable to a cross-site scripting attack (XSS).  XSS is a reasonably common and potentially significant security vulnerability.

Of course, what an attacker can accomplish by using an XSS attack will depend heavily on the nature of the site itself.  An XSS vulnerability on a banking site is likely to lead to many more attacks than an XSS vulnerability on a toy web app that has no logins and has no user data.

Title: Can HTML injection be a security issue?

Body:
I recently came across a website that generates a random adjective, surrounded by a prefix and suffix entered by the user. For example, if the user enters "123" for prefix, and "789" for suffix, it might generate "123Productive789". I've been screwing around with it, and I thought I might try something out. I entered this into the prefix field:

```<code><a href="javascript:window.close();">Click</a><hr />
</code>```

And, sure enough, I was given the link, then an
, then a random adjective. What I'm wondering is, could this be dangerous? There must be many more websites out there that have this issue, are all of them vulnerable to some sort of php injection?


问:
SOA in .NET 4.5 with MSMQ Durability
I have been doing some investigation regarding ASP.NET Web API. I have enjoyed using the WCF REST template to create new RESTful web services, and Web API seems to be a cleaner iteration on that.

However, I like the flexibility of WCF in allowing http, TCP, MSMQ. This aspect seems to facilitate a distributed and scalable architecture. Is it correct for me to conclude that WCF would continue to be my best bet when it comes to fire-and-forget durable message queueing?

答:
If what you want is fire-and-forget then go with MSMQ directly, or use something like NServiceBus or MassTransit. The problem with WCF is that in attempting to abstract transport details, it ends up hiding aspects that should be explicit. The cost of abstraction is too high in my opinion.

You can use WebAPI in conjunction with MSMQ+NServiceBus. They solve very different problems. In a stereotypical architecture, you'd have a web service implemented with WebAPI which sends messages, in a fire-and-forget manner, to a messaging technology. It can also serve queries which can return processing status and results, for instance.

Whether you use WebAPI or WCF depends on your requirements. If there is a need to use TCP then go with WCF. If you can use HTTP, I'd go with WebAPI. It makes HTTP explicit and easier to work with. After all, HTTP is already a rich application protocol, why hide it?

Title: SOA in .NET 4.5 with MSMQ Durability


问:
As a programmer, are you required to do timesheets?
Timesheets are something that I've never been fond of, but none the less something that is a requirement within my company. They don't bother me so much, but they seem to really grind some other people's gears. I suppose I have a few questions, and feedback would be great.

Are you required to do timesheets, assuming you aren't a contractor? (That is understandable to me).

What is the granularity of timesheets that you would be comfortable with or that you use? (ex: all entries must be under two hours).

Would timesheets ever factor into your reasons for not accepting a job or leaving a current one?

How has management within your organization justified timesheets if you aren't billing to a client?

答:
I'll fill out timesheets if necessary (like we have billable hours that a client needs to know about), but in general I dislike the whole idea for a few reasons:

I forget them.  Nine times out of ten, I end up forgetting for a couple of days and then go back and fill it in with mostly inaccurate info.

I'm an adult who is very capable of allocating my own time.  If I'm spending too much time on something, I'll let my manager know.  I don't need them to tell me when I'm spending too much time on something.


问:
Basic use of Business Rules
I have a query on whether the following requirements would need to be designed via Business Rules - this is for a JEE based application where currently this is coded as part of the Business logic.

System will create a tax account for every city, county and district
  combination that imposes tax for only certain cities, counties or
  districts depending on the taxpayer's business.

When the user establishes an account which exists in all subdivisions
  (i.e. at city or county level), the application must use his tax code
  and automatically populate all the locations without requiring the
  user to data enter every location.

I assume this would mean a data lookup table from a master table (of tax accounts) and fetch and display all locations. Is there some way in which a Rules Engine can be used to manage these combinations?

答:
You can create your own simple Business Rule Engine with language like Groovy and concept of Domain specific Language (DSL). Business rule written in dynamic language like Groovy (for your J2EE) allow you to change rule/business logic of the application without redeploying.
Groovy also have specific features, like metaprogramming, closures, AST transformations, builders that make this task doable. It will be very painful, if not impossible to create decent DSL in java.

Creating domain specific Language (DSL) allow your business users if not code the rule themselves but at least easily read them and understand. And all you need for authoring business rules is simple text editor or Eclipse.

Groovy very well integrated into J2EE application, so you can write only rule portion in the language.

Another question do you really need power and flexibility of business rules and DSL for your application? In my case business want to change rules themselves quite often.

There are resources available if you want to dig deeper: book GroovyInAction Second edition from Manning, book - Groovy for DSL - the later one has a chapter on creating groovy Rule engine - good starting point. There is also several publications online - just goodle for groovy and dsl keywords.  http://docs.codehaus.org/display/GROOVY/Writing+Domain-Specific+Languages

Drool is complex piece of software, maybe overkill for task at hand.

Title: Basic use of Business Rules


问:
How to tell whether your programmers are under-performing?
I am a team lead with 5+ developers. I have a developer (let's call him A) who is a good programmer, who writes good clean, easy to understand code. However he is somewhat difficult to manage, and sometimes I wonder whether he is really under-performing or not.

Our company requires the developers to indicate the work progress in the bug tracker we use, not so much as to monitor the programmers but to keep the stakeholders apprised of the progress. The thing is, A only updates a task progress when it is done ( maybe 3 weeks after it is first worked on) and this leaves everyone wondering what is going on in the middle of the development week. He wouldn't change his habit despite repeated probing. ( It's OK, developers hate paperwork, I do, too)

Recent 2-3 months he on leave quite often due to various events-- either he is sick, or have to attend a lot of personal events etc. ( It's OK, bad things happen in a string. It's just a coincidence)

We define sprints, or roadmaps for each month. And in the beginning of the sprint, we will  discuss the amount of work each of the developers have to do in a sprint and the developers get to set the amount of time they need for each task. He usually won't be able to complete all of them. (It's OK, the developers are regularly missing deadlines not due to their fault).

I am based in Singapore. Not sure if that matters. Yeah, Asians are known to be reticent, but does that matter?

If only one or two of the above events happen, I won't feel that A is under-performing, but they all happen together. So I have the feeling that A is under-performing and maybe-- God forbid--- slacking off.

This is just a feeling based on my years of experience as programmer. But I could be wrong.

It is notoriously hard to measure the work of a programmer, given that not all two tasks are alike, and there lacks a standard objective to measure the commitment of a programmer to your company. It is downright impossible to tell whether the programmer is doing his job or slacking off. All you can do, is to trust them-- yeah, trusting and giving them autonomy is the best way for programmers to work, I know that, so don't start a lecture on why you need to trust your programmers, thank you every much-- but if they abuse your trust, can you know?

Outcome:

I've a straight talk with him regarding my perception on his performance. He was indignant when I suggested that I had the feeling that he wasn't performing at his best level. He felt that this was a completely unfair feeling. I then replied that this was my feeling and I didn't know whether my feeling was right or not. He would have none of this and ended the discussion immediately.

Before he left he said that he "would try to give more to the company" in a very cold tone. I was taken aback by his reaction. I am sure that I offended him in some ways. Not too sure whether that was the right thing to do for me to be so frank with him, though.

My question is: How can you tell whether your programmers are under-performing? Surely there are experience team leads who know better than me on this?

Extra notes:

I hate micromanaging. So all that we have for our software process is Sprint ( where tasks get prioritized and assigned, and at the end of the month, a review of the amount of work done). Developers would require to update the tasks as they go along everyday.

There is no standup meeting, or anything of the sort. Mainly because we have the freedom to work from home and everyone cherishes this freedom.

Although I am the one who sets the deadline, but the developers will provide the estimate for each tasks and I will decide-- based on the estimate-- the tasks that go into a particular sprint. If they can't finish the tasks at the end of the sprint, I will push them to the next. So theoretically one can just do only 1 or 2 tasks during the whole sprint and then push the remaining 99 tasks to the next sprint and still he will be fine as long as justifies this-- in the form of daily work progress updates

答:
Daily standups even if over Skype or in a chat room are the way around this but not if you treat it as an update for the stakeholders. When once a day the whole team just checks in to say what they've been working on, what challenges they're running into and what they plan on doing next, you get several wins:

Whether you're wasting time for good or bad reasons, that something is taking too long is going to be more transparent, encouraging your devs to ask for help when they need it and not go overboard on research that probably didn't need to happen or solving a problem for the challenge of it when input from the rest of the team would help them knock it out a lot faster.

You, as a manager can see where people are encountering roadblocks most frequently, which helps you estimate better and possibly resolve fundamental issues that are wasting time/money.

IMO, it really helps the team learn to underpromise on estimates better when they can see how long it typically takes for everybody to get even relatively straightforward things done sometimes.

You'll often be reminded of things that need to be communicated in terms of re-prioritizing when your team members tell you what they plan on doing next.

So forget '% of complete.' Just make the process about getting everybody honest with themselves as much as with everybody else, making better/more-reliable estimates as they gain more experience at it, and giving people a little more motivation to have progress to report without it turning into a mind-numbing exercise of putting a number on something that you really can't.

It sounds like upper management understands that deadlines don't always get hit. Doing daily standups will give you more ammo on that front because you'll have much more specific ideas about why they aren't getting hit.

And don't do them first thing. Always a mistake IMO. People need time to sink their teeth back into the problem before they can more reliably report on what all the issues are, IMO.

Quick standups that are more about communication than accountability, and simply setting goals, more than anything are what make agile work in my opinion. The rest I could take or leave, especially Scrum, which I've come to view as executive/stakeholder snake oil.


问:
Sets Data Structure in Golang
I really like google golang but could some one explain what the rationale is for the implementors having left out a basic data structure such as sets from the standard library?</p>

答:
One potential reason for this omission is that it's really easy to model sets with a map.

To be honest I think it's a bit of an oversight too, however looking at Perl, the story's exactly the same. In Perl you get lists and hashtables, in Go you get arrays, slices, and maps. In Perl you'd generally use a hashtable for any and all problems relating to a set, the same is applicable to Go.

Example

to imitate a set ints in Go, we define a map:

```<code>set := make(map[int]bool)
</code>```

To add something is as easy as:

```<code>i := valueToAdd()
set[i] = true
</code>```

Deleting something is just

```<code>delete(set, i)
</code>```

And the potential awkwardness of this construct is easily abstracted away:

```<code>type IntSet struct {
    set map[int]bool
}

func (set *IntSet) Add(i int) bool {
    _, found := set.set[i]
    set.set[i] = true
    return !found   //False if it existed already
}
</code>```

And delete and get can be defined similarly, I have the complete implementation here . The major disatvantage here is the fact that go doesn't have generics. However it is possible to do this with interface{} in which case you'd have cast the results of get.

Title: Sets Data Structure in Golang

Body:
One potential reason for this omission is that it's really easy to model sets with a map.

To be honest I think it's a bit of an oversight too, however looking at Perl, the story's exactly the same. In Perl you get lists and hashtables, in Go you get arrays, slices, and maps. In Perl you'd generally use a hashtable for any and all problems relating to a set, the same is applicable to Go.

Example

to imitate a set ints in Go, we define a map:

set := make(map[int]bool)

To add something is as easy as:

i := valueToAdd()
set[i] = true

Deleting something is just

delete(set, i)

And the potential awkwardness of this construct is easily abstracted away:

type IntSet struct {
    set map[int]bool
}

func (set *IntSet) Add(i int) bool {
    _, found := set.set[i]
    set.set[i] = true
    return !found   //False if it existed already
}

And delete and get can be defined similarly, I have the complete implementation here . The major disatvantage here is the fact that go doesn't have generics. However it is possible to do this with interface{} in which case you'd have cast the results of get.


答:
Start-ups are risky business. if I'm an expert in what you need, I'm going to want a piece of the profits (large piece) and a look at the business plan to make sure it looks as if this thing has a reasonable chance of success and a lot of perks and a top drawer salary (I doubt what you want to do is so interesting that these people will accept a pay cut.)

In all honestly can you offer this?

Title: how do early stage startups hire ninja programmers

Body:
I am programmer who just started working on a startup idea. At the moment I want to bring onboard at least one programmer. This programmer should be a ninja - a 10x engineer.

Since early days are probably the most risky for a startup, I want to make sure I approach this problem the best I can.

How do I find these people? and How do I convince them to come onboard?

I would love to hear from people who started their own companies and what their thoughts are about hiring

Update: I would like to get the ninja as a co-founder so besides being a ninja (ie. great programmer with computer science background) he/she has to have a healthy appetite for risk (for great programmers this is not a big deal because they can be hired anytime into mainstream jobs if the startup doesn't work)


问:
Are monolith and microkernel design patterns or strategies?
What's the difference between a design pattern and a strategy?
Is the monolith actually an anti-pattern?</p>

答:
I would love to say the monolith is an anti-pattern because I absolutely hate it. The difference between monolithic and micro in my mind are simply levels of abstraction, monolithic relies on one level of abstraction, horizontal segmentation, where micro has another layer, vertical as well as horizontal segmentation. Unfortunately it is not an anti-pattern, and I wouldn't say it's a "design pattern" to begin with.

It's an architecture, and the modern form of the micro architecture is known as "Service Oriented Architecture" where service boundaries give you that vertical segmentation, though the actor model is another architecture which gives it to you and has been gaining steam lately.

We all know what modern monoliths look like though, they're the N-tiers we've all been working on for years. Or at least some of us have unfortunately.


答:
They are architectures for operating systems.

monolithic == highly integrated OS with all core functions tightly coupled in a single executable.

microkernal == small OS kernel which implements only the most basic OS functionality (usually only process scheduling and some basic memory management) all other functions supplied by separate modules.

Microkernals sounds really good as an architecture/concept unfortunately it creates performance and security problems. The only microkernal OS you are likely to come accross in real life is Apples Darwin which still contains some vestages of the "mach" kernel although much of this has been optimized away.

Title: Are monolith and microkernel design patterns or strategies?

Body:
What's the difference between a design pattern and a strategy?
Is the monolith actually an anti-pattern?</p>


答:
The start-ups that get the best programmers do tend to be the ones started by really great programmers. So, I guess, Mission A is to become a really great programmer.

One thing I will say- when I worked freelance there were a lot of people who "had this really great idea" but they wouldn't say what it was unless I was ready to sign a 200 page nda and really commit to definitely working on that idea.

It's been said before and it will be said again but success is rarely determined by the quality of your ideas. Really programmers don't care about your ideas they're more about execution.


问:
Address search from large text file
Basically I want to develop a Address lookup(part of my project) using C# (and I can use SQL if necessary). I have a very large text file which have all the UK address and postcodes. Addresses needs to be looked up from this text file.

For example, as soon as a  user type in the first letter of the  postcode it should list all the postcodes starting with that letter and as they type in further, the list should narrow down.

Could you guys please how I can do this search very fast and list the results in a efficient way. If you guys know any methods and technologies pls do advice.

答:
For performance issues the first thing to do would be, importing the text files into a database.

On typing you can query the affected fields with "where Field LIKE 'input%' Order by xxx" and display the result.

Depending on the amount of Data you also could load all data to your local machine and apply filters on the displayed data.

Title: Address search from large text file


问:
Can someone provide a short code example of compiler bootstrapping?
This Turing award lecture by Ken Thompson on topic "Reflections on Trusting Trust" gives good insight about how C compiler was made in C itself.

Though I understand the crux, it still hasn't sunk in. So ultimately, once the compiler is written to do lexical analysis, parse trees, syntax analysis, byte code generation etc, a separate machine code is again written to do all that on compiler?

Can anyone please explain with a small example of the procedure? Bootstrapping on wiki gives good insights, but only a rough view on it.

PS: I am aware of the duplicates on the site, but found them to be an overview which I am already aware

答:
Step 1.  Write your compiler in a different language.

Step 2.  Compile the code from Step 1.

Step 3.  Write your compiler in the same language.

Step 4.  Compile the code from step 3.

Repeat Steps 3-4 for any further updates to your compiler.

Note that steps 1 and 3 may happen simultaneously or in a different order.

I admit that this is very much an oversimplification.


问:
Tension between the dependency inversion principle and avoiding "new" in C++?
I have seen a lot of advice that it is better to do Type object; than

```<code>Type* object = new Type();
</code>```

in C++ whenever possible—i.e., minimize your use of new. I understand the rational behind this and appreciate it.

But according to my understanding, to practice dependency inversion requires pointers, e.g.:

```<code>Type* object = new Implementation();
</code>```

where Type is abstract (i.e. contains at least one pure virtual method) and Implementation is concrete. It is not possible to do

```<code>Type object = Implementation();
</code>```

because what that means is

```<code>Type object;
object = Implementation();
</code>```

which requires constructing object as a Type initially—but that cannot be done, since Type is abstract.

Is there an inherent tension between the dependency inversion principle and avoiding new when using C++? If so, what patterns/principles/practices can be used to mitigate this tension?

答:
You can absolutely use Dependency Injection without ever using new().  Polymorphism, which is what you're talking about here, is realized in C++ using pointers and/or references.  Doc Brown already addressed references, let's talk about pointers.

As an example (assuming that Implementation is derived from Type, and Bar's constructor takes a pointer to a Type):

```<code>Implementation imp;
Bar bar(&imp);
</code>```

The "address of" operator (&) takes the address of an object, the result is a pointer to the object.  This is a different way of getting a pointer to an object (different compared to your use of new, that is.)

In C++, any time I have a pointer to a Derived, I can also use it as a pointer to a Base.  That's the whole idea of polymorphism, a Derived is a Base, right?

If my 2-step example above made a leap that you couldn't follow, I'll repeat it here, but with one more (unnecessary) step:

```<code>Implementation imp;
Type *t = &imp;
Bar bar(t);
</code>```

t is a pointer to a Type, so it can point to any class derived from Type, including Implementation.  No use of new.  No tension.

Body:
You can absolutely use Dependency Injection without ever using new().  Polymorphism, which is what you're talking about here, is realized in C++ using pointers and/or references.  Doc Brown already addressed references, let's talk about pointers.

As an example (assuming that Implementation is derived from Type, and Bar's constructor takes a pointer to a Type):

Implementation imp;
Bar bar(&imp);

The "address of" operator (&) takes the address of an object, the result is a pointer to the object.  This is a different way of getting a pointer to an object (different compared to your use of new, that is.)

In C++, any time I have a pointer to a Derived, I can also use it as a pointer to a Base.  That's the whole idea of polymorphism, a Derived is a Base, right?

If my 2-step example above made a leap that you couldn't follow, I'll repeat it here, but with one more (unnecessary) step:

Implementation imp;
Type *t = &imp;
Bar bar(t);

t is a pointer to a Type, so it can point to any class derived from Type, including Implementation.  No use of new.  No tension.


答:
from another answer:

```<code>Step 1. Write your compiler in a different language.
Step 2. Compile the code from Step 1.
Step 3. Write your compiler in the same language.
Step 4. Compile the code from step 3.
</code>```

Steps 1 and 2 are only needed for the "first ever" compiler for a new language.  There are lots of variations on steps 3 and 4, so for example you can write the code generator for a new architecture, cross compile, then run the same compiler on a new machine.  Or you can extend your compiler to handle new syntax and constructs without using any of the extensions, then rewrite the compiler to use the extensions and compile itself.

Title: Can someone provide a short code example of compiler bootstrapping?

Body:
This Turing award lecture by Ken Thompson on topic "Reflections on Trusting Trust" gives good insight about how C compiler was made in C itself.

Though I understand the crux, it still hasn't sunk in. So ultimately, once the compiler is written to do lexical analysis, parse trees, syntax analysis, byte code generation etc, a separate machine code is again written to do all that on compiler?

Can anyone please explain with a small example of the procedure? Bootstrapping on wiki gives good insights, but only a rough view on it.

PS: I am aware of the duplicates on the site, but found them to be an overview which I am already aware

Body:
from another answer:

Step 1. Write your compiler in a different language.
Step 2. Compile the code from Step 1.
Step 3. Write your compiler in the same language.
Step 4. Compile the code from step 3.

Steps 1 and 2 are only needed for the "first ever" compiler for a new language.  There are lots of variations on steps 3 and 4, so for example you can write the code generator for a new architecture, cross compile, then run the same compiler on a new machine.  Or you can extend your compiler to handle new syntax and constructs without using any of the extensions, then rewrite the compiler to use the extensions and compile itself.


问:
Designing web-based plugin systems correctly so they don't waste as many resources?
Many CMS systems which rely on third parties for much of their code often build "plugin" or "hooks" systems to make it easy for developers to modify the codebase's actions without editing the core files.

This usually means an Observer or Event design pattern.

However, when you look at systems like wordpress you see that on every page they load some kind of bootstrap file from each of the plugin's folders to see if that plugin will need to run that request. Its this poor design that causes systems like wordpress to spend many extra MB's of memory loading and parsing unneeded items each page.

Are there alternative ways to do this? I'm looking for ideas in building my own.

For example, Is there a way to load all this once and then cache the results so that your system knows how to lazy-load plugins? In other words, the system loads a configuration file that specifies all the events that plugin wishes to tie into and then saves it for future requests?

If that also performs poorly, then perhaps there is a special file-structure that could be used to make educated guesses about when certain plugins are unneeded to fullfil the request.

Any ideas?

If anyone wants an example of the "plugin" concept you can find one here.

答:
A really good question!

Especially when it comes to PHP since we dont really have a shared memory and we are bound to the HTTP REQ<=>RESP "flow".

... if I am not wrong, please don't hesitate to correct me if ...

With PHP 5.3+, you can take advantage of namespaces to optimze the performance "load"/"overhead" of plugin-classes(files).

During each PHP request, the "use" keyword in combination with correct "namespace" declarations will make sure that only those classes are "read/loaded/parsed" which are required for the current request.

This is sadly not done (to my knowledge) with any kind of PHP-internal-optimization. (correct me if I am wrong)

Therefore it depends on your OODesign skill not to produce monolithic classes, cause that would defy any class-loading-optimization using namespaces.

Additionally you can take advantage of .phar packages, which fit very well into the namespaces/use-keyword concept.

Ref's:
PHP Namespaces


问:
scons and python unit tests best practices
I am using scons to build a large project containing a mix of C++ and Python.  I would like scons to run Python unit tests either using nose or not.  Currently, we have a long list of tests files and run a test builder on each one.  This causes each test file to be run as a separate script which feel inelegant and inefficient. </p>

<p>Is there a better way of doing this? </p>

答:
In the end, I created a custom builder which called an external test runner.  Just save the following to site_scons/site_tools/run_py_test.py, import it in the main SConstruct file, and use it as you would any other builder.

```<code>import sys
import os
import subprocess
try:
    from fabulous.color import red, green, fg256
    utest_fail = lambda x: sys.stderr.write(red(x).as_utf8+'\n')
    utest_pass = lambda x: sys.stdout.write(green(x).as_utf8+'\n')
    utest_directory = lambda x: sys.stdout.write(fg256(63, x).as_utf8+'\n')
except ImportError:
    sys.stdout.write('Fabulous module not found: '
            'To install it, run sudo pip-python install fabulous\n')
    utest_fail = lambda x: sys.stderr.write(x)
    utest_pass = lambda x: sys.stdout.write(x)
    utest_directory = lambda x: sys.stdout.write(x+'\n')

def which (program):
    """Reimplementation of the shell command 'which'.

    @see: http://stackoverflow.com/a/377028/232794
    @type program: String
    @param program: The program name.
    @return The full path to the program or None.
    """
    is_exe = lambda x: os.path.isfile(x) and os.access(x, os.X_OK)
    fpath, fname = os.path.split(program)
    if fpath:
        if is_exe(program):
            return program
    else:
        for path in os.environ["PATH"].split(os.pathsep):
            exe_file = os.path.join(path, program)
            if is_exe(exe_file):
                return exe_file
    return None

def runTests (target = None, source = None, env = None):
    """Run a test aggregation tool (by default py.test) in directories
    provided in TEST_DIR_LIST.

    @type target: String
    @param target: A target.
    @type source: String
    @param source: A source.
    @type env: SCONS ENV.
    @param env: This should contain TEST_RUNNER or TEST_DIR_LIST otherwise
                defaults values are used.
    """
    if not 'TEST_RUNNER' in env:
        env.Append(TEST_RUNNER='py.test')
    if not 'TEST_DIR_LIST' in env:
        sys.stderr.write("+++ TEST_DIR_LIST is not set, using project root.\n")
        env.Replace(TEST_DIR_LIST=['./'])
    cmd = which(env['TEST_RUNNER'])
    if cmd:
        retCode = 0
        for path in env['TEST_DIR_LIST']:
            utest_directory('\n+++ Running unit tests in directory %s:' % (path))
            retCode += subprocess.call([cmd], cwd=path)
        line = "="*80 + "\n"
        if retCode:
            try:
                utest_fail(line + u" ✘  Unit tests failed!\n" + line)
            except UnicodeEncodeError:
                utest_fail(line + " -  Unit tests failed!\n" + line)
        else:
            try:
                utest_pass(line + u" ✓  Unit tests passed.\n" + line)
            except UnicodeEncodeError:
                utest_pass(line + " +  Unit tests passed.\n" + line)
        env.Exit(retCode)
    else:
        sys.stderr.write("*** Could not find %s command\n" % (
            env['TEST_RUNNER']))
        env.Exit(-1)
</code>```

If you are going to down vote, at least leave a comment as to why this is a bad answer so it can be improved!!!

Body:
In the end, I created a custom builder which called an external test runner.  Just save the following to site_scons/site_tools/run_py_test.py, import it in the main SConstruct file, and use it as you would any other builder.

import sys
import os
import subprocess
try:
    from fabulous.color import red, green, fg256
    utest_fail = lambda x: sys.stderr.write(red(x).as_utf8+'\n')
    utest_pass = lambda x: sys.stdout.write(green(x).as_utf8+'\n')
    utest_directory = lambda x: sys.stdout.write(fg256(63, x).as_utf8+'\n')
except ImportError:
    sys.stdout.write('Fabulous module not found: '
            'To install it, run sudo pip-python install fabulous\n')
    utest_fail = lambda x: sys.stderr.write(x)
    utest_pass = lambda x: sys.stdout.write(x)
    utest_directory = lambda x: sys.stdout.write(x+'\n')

def which (program):
    """Reimplementation of the shell command 'which'.

    @see: http://stackoverflow.com/a/377028/232794
    @type program: String
    @param program: The program name.
    @return The full path to the program or None.
    """
    is_exe = lambda x: os.path.isfile(x) and os.access(x, os.X_OK)
    fpath, fname = os.path.split(program)
    if fpath:
        if is_exe(program):
            return program
    else:
        for path in os.environ["PATH"].split(os.pathsep):
            exe_file = os.path.join(path, program)
            if is_exe(exe_file):
                return exe_file
    return None

def runTests (target = None, source = None, env = None):
    """Run a test aggregation tool (by default py.test) in directories
    provided in TEST_DIR_LIST.

    @type target: String
    @param target: A target.
    @type source: String
    @param source: A source.
    @type env: SCONS ENV.
    @param env: This should contain TEST_RUNNER or TEST_DIR_LIST otherwise
                defaults values are used.
    """
    if not 'TEST_RUNNER' in env:
        env.Append(TEST_RUNNER='py.test')
    if not 'TEST_DIR_LIST' in env:
        sys.stderr.write("+++ TEST_DIR_LIST is not set, using project root.\n")
        env.Replace(TEST_DIR_LIST=['./'])
    cmd = which(env['TEST_RUNNER'])
    if cmd:
        retCode = 0
        for path in env['TEST_DIR_LIST']:
            utest_directory('\n+++ Running unit tests in directory %s:' % (path))
            retCode += subprocess.call([cmd], cwd=path)
        line = "="*80 + "\n"
        if retCode:
            try:
                utest_fail(line + u" ✘  Unit tests failed!\n" + line)
            except UnicodeEncodeError:
                utest_fail(line + " -  Unit tests failed!\n" + line)
        else:
            try:
                utest_pass(line + u" ✓  Unit tests passed.\n" + line)
            except UnicodeEncodeError:
                utest_pass(line + " +  Unit tests passed.\n" + line)
        env.Exit(retCode)
    else:
        sys.stderr.write("*** Could not find %s command\n" % (
            env['TEST_RUNNER']))
        env.Exit(-1)

If you are going to down vote, at least leave a comment as to why this is a bad answer so it can be improved!!!


答:
As Aaron McIver has already noted, a RESTful architecture is mainly intended to leave the clients ask for they want (service segregation). It should be your client (a traditional web page, an AJAX page, a GUI-less WS client) to decide which information sources it wants to use and call the corresponding servers. It should be your client to collect data, organize it and display it to the end user.

There is nothing wrong in having one or more convenience proxy server that aggregate data, when this data is commonly used in this aggregated way, but this should be considered just this: a convenience method, not the standard way to go. The reason for this is what you are experimenting now: the external interface (the side to the end user) suffers a combinatory complexity explosion and rapidly became unmaneagable.

So you should leave your client decide if it wants to query one or more aggregated-data server or the original raw data sources. You should leave the client to assemble the data and display it. Please note that you can have more than one proxy server, if this makes sense in your case.

Put aside this, consider as well that you can make a good use of cache and temporary tables (database "views") to supply the proxy server with ready-to-use, partially-digested data.

Title: Allowing client to select data to return via REST interface

Body:
I have a rest service that is essentially a proxy to a variety of other services. So if I call

GET /users/{id}

It will get their user profile, as well as order history, and contact info, etc... all from various services, and aggregates them into one nice object. My problem is that each call to a different service has the potential to add time to the original request, so we would rather not get ALL the data ALL of the time if a particular client does not care about all of the pieces.

A solution I have arrived at is to do something like this:

GET /users/{id}?includeOrders=true&includeX=true&includeY=true...

That works, and it allow me to do only what I need to, but it is cumbersome. We have added enough different data sources that there are too many parameters for that style to be useful. I could do something similar with a single integer and a bitmask or something, but that only makes it harder to read, and it does not feel very Restful.

I could break it down into multiple calls so they would need to call /users/{id}/orders and /users/{id}/profile separately, but that sort of defeats the purpose of an aggregating proxy, who's purpose is to make clients jobs easier.

Are there any good patterns that can help me return just enough data for each client, without making it too difficult for them to filter and select what they want?


问:
What are some famous one-liner or two-liner programs and equations?
I'm experimenting with a new platform and I'm trying to write a program that deals with strings that are no longer than 60 characters and I'd like to populate the data store with some famous or well-known small chunks of code and equations, since programming and math goes along with the theme of my software. The code can be in any language and the equations from any discipline of mathematics, just so long as they're less than a total of 60 characters in length. I suspect people are gonna break out some brainfuck for this one.

For example,

```<code>#include<stdio.h>
int main(){printf ("Hi World\n");return 0;}
</code>```

60 characters exactly!

Thanks so much for your wisdom!

答:
This Quine from the Jargon File in C:

```
char*f="char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c";main(){printf(f,34,f,34,10);}
```

There is also a LISP version there, too, but you can find many others floating around, in pretty much any language you could imaging...

Body:
This Quine from the Jargon File in C:

char*f="char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c";main(){printf(f,34,f,34,10);}

There is also a LISP version there, too, but you can find many others floating around, in pretty much any language you could imaging...


问:
What programming languages exist for mechatronics purposes?
Is there any specific language which is designed for mechatronics programming? I know about LabView, which is a data flow language, but not sure about its main platform.

Could you recommend to me, some languages apart from c/c++? Any language which is used in the topic of mechatronics( robotics, sensor programming, etc ).

答:
As an addition to Scotts answer, there is also another strong force in the field of industrial automation: Siemens. Their PLC, the S7, is very common and comes with some - non-IEC-compatible - languages:

graph (like SFC)

kop

fup

scl (like ST): a little bit comparable to an programming language like C. It's actually readable

awl (like IL): Assembler-like programming.

Which language you like is totally based on your own decision. For easy I/O Mapping and simple decision-making i would stick with il/awl, if i have to programm complexer things, i rather stick with st/scl.