问:
Dealing with the node.js callback pyramid
I've just started using node, and one thing I've quickly noticed is how quickly callbacks can build up to a silly level of indentation:

```<code>doStuff(arg1, arg2, function(err, result) {
    doMoreStuff(arg3, arg4, function(err, result) {
        doEvenMoreStuff(arg5, arg6, function(err, result) {
            omgHowDidIGetHere();
        });
    });
});
</code>```

The official style guide says to put each callback in a separate function, but that seems overly restrictive on the use of closures, and making a single object declared in the top level available several layers down, as the object has to be passed through all the intermediate callbacks.

Is it ok to use function scope to help here? Put all the callback functions that need access to a global-ish object inside a function that declares that object, so it goes into a closure?

```<code>function topLevelFunction(globalishObject, callback) {

    function doMoreStuffImpl(err, result) {
        doMoreStuff(arg5, arg6, function(err, result) {
            callback(null, globalishObject);
        });
    }

    doStuff(arg1, arg2, doMoreStuffImpl);
}
</code>```

and so on for several more layers...

Or are there frameworks etc to help reduce the levels of indentation without declaring a named function for every single callback? How do you deal with the callback pyramid?

答:
Promises provide a clean separation of concerns between asynchronous
  behavior and the interface so asynchronous functions can be called
  without callbacks, and callback interaction can be done on the generic
  promise interface.

There is multiple implementations of "promises":

node-promise

promised-io

or the most popular Q

For example you can rewrite this nested callbacks

```<code>http.get(url.parse("http://test.com/"), function(res) {
    console.log(res.statusCode);
    http.get(url.parse(res.headers["location"]), function(res) {
        console.log(res.statusCode);
    });
});
</code>```

like

```<code>httpGet(url.parse("http://test.com/")).then(function (res) {
    console.log(res.statusCode);
    return httpGet(url.parse(res.headers["location"]));
}).then(function (res) {
    console.log(res.statusCode);
});
</code>```

Instead of callback in callback a(b(c())) you chain the ".then" a().then(b()).then(c()).

An introduction here: http://howtonode.org/promises

Body:
Promises provide a clean separation of concerns between asynchronous
  behavior and the interface so asynchronous functions can be called
  without callbacks, and callback interaction can be done on the generic
  promise interface.

There is multiple implementations of "promises":

node-promise

promised-io

or the most popular Q

For example you can rewrite this nested callbacks

http.get(url.parse("http://test.com/"), function(res) {
    console.log(res.statusCode);
    http.get(url.parse(res.headers["location"]), function(res) {
        console.log(res.statusCode);
    });
});

like

httpGet(url.parse("http://test.com/")).then(function (res) {
    console.log(res.statusCode);
    return httpGet(url.parse(res.headers["location"]));
}).then(function (res) {
    console.log(res.statusCode);
});

Instead of callback in callback a(b(c())) you chain the ".then" a().then(b()).then(c()).

An introduction here: http://howtonode.org/promises


问:
Reference vs dereference pointers in arguments C++/C
OK, I'm facing this all the time in many functions I write, which should I use?

```<code>void sth(int* a)
void sth(int& a)
</code>```

Which one is faster, regarding two separate occasions: a is a small variable or a is a large data struct.

I would like a deep answers with pertinence to the actual hardware and stack process.

答:
Reference and Pointers are two implementation of a same concept: indirection (that is "talk about something through a pronoun")

At machine level they are the same thing (index of a memory cell), so there in no performance distinction.
At language level the main difference is mostly in being "explicit" and "mutable":

pointer dereferencing is explicit: given pa = &a; a.x is the same of pa->x

reference dereferencing is implicit: given ra = a; a.x is the same as ra.x

The identical syntax inside expressions makes reference more suitable in generic functions, since the way they will be expressed won't change whether the access to the variable is direct or indirect.

pointer are mutable: pa = &a1; ...; pa = &a2; or ++pa or pa[x] are all possible

reference are unmutable: ra = a1; ... ; ra= a2; in fact assign the a2 value to a1 (thus playing a different game)

The mutable nature of pointers make them more suitable implementing generic iterators.

It is like talking about fixed versus adaptive wrenches. Their different shape makes their usability to change respect to certain context. But for the screw standpoint, they are just wrenches.

ContentLicense: CC BY-SA 3.0


问:
How to keep your third party libraries up to date?
Let's say that I have a project that depends on 10 libraries, and within my project's trunk I'm free to use any versions of those libraries. So I start with the most recent versions. Then,  each of those libraries gets an update once a month (on average). Now, keeping my trunk completely up to date would require updating a library reference every three days.

This is obviously too much. Even though usually version 1.2.3 is a drop-in replacement for version 1.2.2, you never know without testing. Unit tests aren't enough; if it's a DB / file engine, you have to ensure that it works properly with files that were created with older versions, and maybe vice versa. If it has something to do with GUI, you have to visually inspect everything. And so on.

How do you handle this? Some possible approaches:

If it ain't broke, don't fix it. Stay with your current version of the library as long as you don't notice anything wrong with it when used in your application, no matter how often the library vendor publishes updates. Small incremental changes are just waste.

Update frequently in order to keep change small. Since you'll have to update some day in any case, it's better to update often so that you notice any problems early when they're easy to fix, instead of jumping over several versions and letting potential problems to accumulate.

Something in between. Is there a sweet spot?

答:
Subversion Externals

What is great in that feature is that you can specify the revision you want.

Please note that updates will be slower if you have many externals.

ContentLicense: CC BY-SA 2.5


问:
Are trivial protected getters blatant overkill?
Something I really have not thought about before (AS3 syntax):

```<code>private var m_obj:Object;
protected function get obj():Object
{
    return m_obj;
}

private var m_str:String;
protected function get str():String
{
    return m_str;
}
</code>```

At least subclasses won't be able to set m_obj or m_str (though they could still modify m_obj).

My question: Is this just blatant overkill?

This Programmer's question:
When or why should one use getters/setters for class properties instead of simply making them public properties?
has been suggested as a duplicate.

That question is different because it only addresses public vs. private properties and whether a property should be wrapped with getters & setters.  For my question, I'm focusing on protected variables and how inheriting classes would interact with those variables.

So the alternative implementation would be:

```<code>protected var m_obj:Object; //more accessible than a private variable with a protected getter
protected var m_str:String; //more accessible than a private variable with a protected getter
</code>```

My question is similar because I am talking about using trivial protected getters to block subclasses from writing to the variables, but to allow them all other access.

In languages like AS3, this will even allow them to make any changes to mutable object variables, as long as the references themselves are not modified.

答:
Is this just blatant overkill?

Often times it is, sometimes it's not.

Keeping m_obj in some known good state helps protect the Liskov Substitution Principle, keeping your code more resilient and higher quality. Sometimes you can trust inheritors to respect that behavior, sometimes you can't (either due to usage pattern or the subtlety of the contract it implements). Though this code/question also stumbles towards some of the reasons for "Why is Clean Code suggesting avoiding protected variables?"


问:
PHP: A config file in .ini or .php format?
I'm working on a huge CMS system, and I asked myself what configuration format I should use.

There are two common formats for configuration files. The first one is an INI file, containg all the configuration properties. Then you can simply parse this INI file using build in PHP functions. A second option is to use a PHP file containing a regular PHP array with these configuration properties.

Now, it's easier to edit an INI file, but a PHP file give's you more options, for example it allows you to add a function which retrieves one of the configuration options while reading the configuration file.

Note: The PHP configuration file would only contain an array of configuration, no initialization functions or anything. (This is possible of course, but it's not implemented by default)

Now, what is recommend for me to use as configuration file? What is the most common format for a configuration file? Should I go for simplicity with the INI files, or with a more dynamic one using PHP.

One thing to note, this is not for personal usage. I'm planning to release the CMS system soon, and a lot of websites are scheduled already to change to my CMS system.

答:
INI files have the disadvantage that they can't elegantly represent complex data structures like arrays. Therefore they are only useful for very simple configuration. Using a PHP file for configuration can be viewed as a security problem: Any code might be entered there. Worse, a simple syntax error like forgetting a closing quote could render the whole configuration unusable.

There are already common human-readable data formats. XML is extremely powerful, but overly verbose, and not every data maps cleanly to XML. JSON is another excellent format, but has a very strict syntax.

I would urge you to consider YAML. It is extremely easy to read, and includes JSON as a subset of its syntax.

```<code>this is a key: some value here
another key:
  - this is the first item in an array
  - another one
  - { inline: dictionary, has: values }
  - [ inline array, has, more, values]
  - "Quote this, if you like"
</code>```

There are many more advanced features beyond simple dictionaries and lists, making this a very flexible format.

Body:
INI files have the disadvantage that they can't elegantly represent complex data structures like arrays. Therefore they are only useful for very simple configuration. Using a PHP file for configuration can be viewed as a security problem: Any code might be entered there. Worse, a simple syntax error like forgetting a closing quote could render the whole configuration unusable.

There are already common human-readable data formats. XML is extremely powerful, but overly verbose, and not every data maps cleanly to XML. JSON is another excellent format, but has a very strict syntax.

I would urge you to consider YAML. It is extremely easy to read, and includes JSON as a subset of its syntax.

this is a key: some value here
another key:
  - this is the first item in an array
  - another one
  - { inline: dictionary, has: values }
  - [ inline array, has, more, values]
  - "Quote this, if you like"

There are many more advanced features beyond simple dictionaries and lists, making this a very flexible format.


问:
Best supporting argument for refactoring
Currently I am working on a code best described as C code living in C++ body. However I haven't been able to convince power that be to re-factor on ground of ease of maintenance. </p>

<p>What in your opinion is the best argument for refactoring the code. </p>

答: Just do it as you go. In my experience, very few projects warrant a wholesale re-factoring of the code as a separate project. If there are pieces of the code you would never get to because they don't need modification, then the value of refactoring them is probably dubious anyway.</p>


答:
Trivial protected getters have at least two practical advantages over an exposed variable:

For debugging, they are a perfect place to put a conditional breakpoint which triggers when “that funny value gets into your program”.  It is much harder to get the same control if you access the value directly.

For mocking an object, if you are using this method to test descendants.

The cost of using getters is virtually zero, so there is no real reason not to use them. Is there?

Title: Are trivial protected getters blatant overkill?

Body:
Something I really have not thought about before (AS3 syntax):

```<code>private var m_obj:Object;
protected function get obj():Object
{
    return m_obj;
}

private var m_str:String;
protected function get str():String
{
    return m_str;
}
</code>```

At least subclasses won't be able to set m_obj or m_str (though they could still modify m_obj).

My question: Is this just blatant overkill?

This Programmer's question:
When or why should one use getters/setters for class properties instead of simply making them public properties?
has been suggested as a duplicate.

That question is different because it only addresses public vs. private properties and whether a property should be wrapped with getters & setters.  For my question, I'm focusing on protected variables and how inheriting classes would interact with those variables.

So the alternative implementation would be:

```<code>protected var m_obj:Object; //more accessible than a private variable with a protected getter
protected var m_str:String; //more accessible than a private variable with a protected getter
</code>```

My question is similar because I am talking about using trivial protected getters to block subclasses from writing to the variables, but to allow them all other access.

In languages like AS3, this will even allow them to make any changes to mutable object variables, as long as the references themselves are not modified.


问:
Is ROA a specific form of doing SOA?
I have read somewhere that ROA (Resource Oriented Architecture) is SOA (Service Oriented Architecture) with specific constraints added. SOA is the abstract concept of combining discrete pieces of software and ROA is an implementation of SOA with all of the constraints of RESTful services applied to it:

SOA = the concept

ROA = the concept + specific implementation details

I also had my share of posts saying that ROA is different than SOA, then simply fallback to statements like "ROA is REST" and "SOA is SOAP" and presenting the same more or less pertinent comparisons and differences between SOAP and REST.

So just to clear up my confusion: Is ROA a specific form of doing SOA?

答:
SOA is by definition independent of any technology. SOA can be implemented using any service-based technology, it obviously encompasses all REST/HTTP applications.

Based on this, ROA might be considered as a subpart of SOA that respects a specific set of guidelines of an implementation of the REST-style architecture.

Title: Is ROA a specific form of doing SOA?


答:
The most important difference between references and pointers is that you cannot free an object through a reference while it is possible to do it through a pointer.

Thus, selecting the reference type instead of the pointer type for an argument a in a method of an object b advertises that ownership of a is not transferred to b.

(The common belief, that it is not possible to pass a dereferenced NULL as a reference to a method without cheating is wrong. Most methods creating an object —e.g. clone or factories—will return a pointer, NULL or not. If the method you want to call with the freshly created object uses references, you have to dereference the pointer.)

Title: Reference vs dereference pointers in arguments C++/C

Body:
OK, I'm facing this all the time in many functions I write, which should I use?

```<code>void sth(int* a)
void sth(int& a)
</code>```

Which one is faster, regarding two separate occasions: a is a small variable or a is a large data struct.

I would like a deep answers with pertinence to the actual hardware and stack process.


答:
I think you need to consider the size and complexity of the configuration needed,

Do you need to read it often? always?

Do you need to be able to override the data on certain circumstances?

In most cases I would go for a php file, specially if you can put that file outside of the web server root and as a read-only file.

YAML is nice, but I guess can be expensive to process the config file on every request, you would need some sort of cache, and that of course would add complexity for something that could be really simple.

Title: PHP: A config file in .ini or .php format?

Body:
I'm working on a huge CMS system, and I asked myself what configuration format I should use.

There are two common formats for configuration files. The first one is an INI file, containg all the configuration properties. Then you can simply parse this INI file using build in PHP functions. A second option is to use a PHP file containing a regular PHP array with these configuration properties.

Now, it's easier to edit an INI file, but a PHP file give's you more options, for example it allows you to add a function which retrieves one of the configuration options while reading the configuration file.

Note: The PHP configuration file would only contain an array of configuration, no initialization functions or anything. (This is possible of course, but it's not implemented by default)

Now, what is recommend for me to use as configuration file? What is the most common format for a configuration file? Should I go for simplicity with the INI files, or with a more dynamic one using PHP.

One thing to note, this is not for personal usage. I'm planning to release the CMS system soon, and a lot of websites are scheduled already to change to my CMS system.


问:
Is it bad practice to call a controller action from a view that was rendered by another controller?
Let's say I have an OrderController which handles orders. The user adds products to it through the view, and then the final price gets calculated through an AJAX call to a controller action.

The price calculation logic is implemented in a seperate class and used in a controller action. What happens is that I have many views from different controllers that need to use that particular action. I'd like to have some kind of a PriceController that I could call an action on. But then the view would have to know about that PriceController and call an action on it.

Is it bad practice for a view to call an action on a different controller from which it was rendered?

答:
This seems like a pretty reasonable solution to me.

A view that has to request information should, and can, be able to request that information from any appropriate controller - I don't think there needs to be a 1:1 mapping if that's what your implying there should be?


答:
You don't tell the taxi driver how to drive his car.

You don't tell the hairdresser how to cut your hairs.

You don't tell a coder how to work with his code.

If you think your code needs refactoring. Refactor it.

Title: Best supporting argument for refactoring

Body:
Currently I am working on a code best described as C code living in C++ body. However I haven't been able to convince power that be to re-factor on ground of ease of maintenance. </p>

<p>What in your opinion is the best argument for refactoring the code. </p>


答:
I would go with a .php file.  While its very minimal, an ini file is still adding overhead to each page load as it gets opened and processed every time a value is read.  Even if you load the entire file into memory, you still are doing extra work looking thigns up.  You'd have the same issue with .xml or other format as well.  A PHP file would be loaded once, and then as a bonus, cached by any PHP caching system you might have.

And really, is there much difference between this:

```<code>somesetting = 1
</code>```

and this?

```<code>$somesetting = 1;
</code>```

Both are just as easy to manage.

Body:
I would go with a .php file.  While its very minimal, an ini file is still adding overhead to each page load as it gets opened and processed every time a value is read.  Even if you load the entire file into memory, you still are doing extra work looking thigns up.  You'd have the same issue with .xml or other format as well.  A PHP file would be loaded once, and then as a bonus, cached by any PHP caching system you might have.

And really, is there much difference between this:

somesetting = 1

and this?

$somesetting = 1;

Both are just as easy to manage.


问:
Safest way (i.e. HTTPS, POST, PGP) to send decryption keys through the web?
I am in the final stages of development for my Revit plugin. This plugin is programmed in C#, and distributed via a DLL. One of the DLLs is an encrypted SQLite database (with proprietary data) that is in the form of a DLL. Currently, in development stages, the decryption key for the SQLite database is hardcoded in my main DLL (the program's DLL). For distribution, since DLLs are easily decompilable, I am in need of a new method to decrypt the DLL. My solution is to send our decryption keys from our servers securely to the host's computer.

I was looking in POST, thinking it was more secure than GET, but upon research, it appears it's similarly insecure, only more "obscure" than GET. I also looked into HTTPS, but Hostgator requires extra money for HTTPS use.

I am in need of some advice - are there any custom solutions I can do to implement this?

答:
This is not a direct answer but I am afraid a comment is too short to finish my point.

You are trying to keep the key out of the hands of your users. HTTPS in this case does not help too much. Here is why: additional to HTTPS, you will need some authentication information passed from the your application. Without authentication, a user just use a browser and hit the link, voila, the decryption/encryption key. So it comes down to protect this authentication information, which is embed again in one of your binaries and you are back to your original problem. To prevent De-compilation, try these obfuscation products. They often do a good enough job to protect your from reverse engineering.

To avoid key being intercepted during transmission, you do need HTTPS but if you cannot use it because of budget concern, you can roll your own asymmetric encryption over HTTP. All you need to do is to encrypt the key on the server with a private key and decrypt the key in your program with a public key.


答:
You can't secure a DB that is physically delivered to and then decrypted on a Client's computer from the Client itself.

It simple can't be done... It's like saying you want to store a Safe in someone house and send someone in with a key, when you need access to it. You want to insure that no one grabs that key out if his hand. You are asking "Where can i hide the Key?" But anyway you cut it, he as to take the key out to open the safe. The the home owner will see it, he can grab it, he can wait for the safe to be opened then stop it from closing... There is just too many holes in this system because the environment is not safe. You simply can't secure one file in an unsafe environment and at the same time keep it accessible.

The solution is simply never give all the data to the client in the first place. You need to created a service that delivers the data on an as need basis.

Title: Safest way (i.e. HTTPS, POST, PGP) to send decryption keys through the web?

Body:
I am in the final stages of development for my Revit plugin. This plugin is programmed in C#, and distributed via a DLL. One of the DLLs is an encrypted SQLite database (with proprietary data) that is in the form of a DLL. Currently, in development stages, the decryption key for the SQLite database is hardcoded in my main DLL (the program's DLL). For distribution, since DLLs are easily decompilable, I am in need of a new method to decrypt the DLL. My solution is to send our decryption keys from our servers securely to the host's computer.

I was looking in POST, thinking it was more secure than GET, but upon research, it appears it's similarly insecure, only more "obscure" than GET. I also looked into HTTPS, but Hostgator requires extra money for HTTPS use.

I am in need of some advice - are there any custom solutions I can do to implement this?


问:
How can I correct html for missing closing tags in c# ?
I have html stored in database received from text editors and because of some mistakes in the code the html is not valid, it is missing some closing tags. and due to this I am not able to process this html for some other operations like generating pdfs. So I need to write some code in c# that can check for tags that are missing closing tags and insert a closing tag at the correct place. </p>

答:
You are never going to know the intent of the author, don't waste your time trying.

Validate the HTML on insert, and kick back a message to the user on Error.

Or

When i did this i used IE automation to interpret the HTML, so the UI where the user enters it can show the user how it will look (using the Local IE or browser they are currently using as the interpreter). IE is also very forgiving when interpreting HTML, so it doesn't have to be perfect. The key is that the user sees what it looks like before hand, and I can interpret the HTML in a way that matches what the user saw.


答:
Most reasons are dealing with probabilities which makes them difficult to count, especially if the willingness to take risks is high. Better maintenance? If nothing goes wrong, you won't have to touch it. Better portability? If we never change the running system, we won't need it. Guaranteed future? Who knows if we will need the functionality of this code in a few years time at all? Code's a dead end? Who knows if we will ever build code on top of that crap. Security issues? We don't believe in evil hackers. Programmers having fun to make something bright and shiny? We don't pay them to have fun. Easier to manage? So we would need less qualified programmers, paying them less money? Ahh, now you've got a point...!

The last argument was a joke, but I think you will need to tackle your request with real benefits and depending on what's high on the agenda of your bosses you will need different arguments. My list above is maybe useful as a starting point...


问:
What is lightweight lock in distributed shared memory systems?
I started reading Tanenbaum's Distributed Systems book a while ago. I read about two phase locking and timestamp reordering in transactions chapter. While having a deeper look from google I heard of lightweight transactions/lightweight transactional memory. But I couldn't find any good explanation and implementation. So what is lightweight memory? What are the benefits of lightweight locks? And how can I implement them?</p>

答:
The term "lightweight," when it is applied to locking and transactions, is generally used to indicate a locking or transaction scheme that has low overhead.  Low overhead means that your computer is chewing up less CPU time on housekeeping tasks, and spending more time doing actual work.

The term "lightweight," when applied to memory locking and transactions, is probably referring to Software Transactional Memory (STM).  Some languages, like Clojure, have STM built in.

Unlike the locking techniques used in most modern multithreaded
  applications, STM is very optimistic: a thread completes modifications
  to shared memory without regard for what other threads might be doing,
  recording every read and write that it is performing in a log. Instead
  of placing the onus on the writer to make sure it does not adversely
  affect other operations in progress, it is placed on the reader, who
  after completing an entire transaction verifies that other threads
  have not concurrently made changes to memory that it accessed in the
  past. This final operation, in which the changes of a transaction are
  validated and, if validation is successful, made permanent, is called
  a commit. A transaction may also abort at any time, causing all of its
  prior changes to be rolled back or undone.

The benefit of this optimistic approach is increased concurrency: no thread needs to wait for access to a resource, and different threads can safely and simultaneously modify disjoint parts of a data structure that would normally be protected under the same lock.


问:
What are the safety benefits of a type system?
In JavaScript: The Good Parts by Douglas Crockford, he mentions in his inheritance chapter,

The other benefit of classical inheritance is that it includes the specification of a system of types. This mostly frees the programmer from having to write explicit casting operations, which is a very good thing because when casting, the safety benefits of a type system are lost.

So first of all, what actually is safety? protection against data corruption, or hackers, or system malfunctions, etc.?

What are the safety benefits of a type system? What makes a type system different that allows it to provide these safety benefits?

答:
Type systems prevent errors

Type systems eliminates illegal programs. Consider the following Python code.

```<code> a = 'foo'
 b = True
 c = a / b
</code>```

In Python, this program fails; it throws an exception. In a language like Java, C#, Haskell, whatever, this isn't even a legal program. You entirely avoid these errors because they simply aren't possible in the set of input programs.

Similarly, a better type system rules out more errors. If we jump up to super advanced type systems we can say things like this:

```<code> Definition divide x (y : {x : integer | x /= 0}) = x / y
</code>```

Now the type system guarantees that there aren't any divide-by-0 errors.

What sort of errors

Here's a brief list of what errors type systems can prevent

Out-of-range errors

SQL injection

Generalizing 2, many safety issues (what taint checking is for in Perl)

Out-of-sequence errors (forgetting to call init)

Forcing a subset of values to be used (for example, only integers greater than 0)

Nefarious kittens (Yes, it was a joke)

Loss-of-precision errors

Software transactional memory (STM) errors (this needs purity, which also requires types)

Generalizing 8, controlling side effects

Invariants over data structures (is a binary tree balanced?)

Forgetting an exception or throwing the wrong one

And remember, this is also at compile time. No need to write tests with 100% code coverage to simply check for type errors, the compiler just does it for you :)

Case study: Typed lambda calculus

Alright, let's examine the simplest of all type systems, simply typed lambda calculus.

Basically there are two types,

```<code>Type = Unit | Type -> Type
</code>```

And all terms are either variables, lambdas, or application. Based on this, we can prove that any well typed program terminates. There is never a situation where the program will get stuck or loop forever. This isn't provable in normal lambda calculus because well, it isn't true.

Think about this, we can use type systems to guarentee that our program doesn't loop forever, rather cool right?

Detour into dynamic types

Dynamic type systems can offer identical guarantees as static type systems, but at runtime rather than compile time. Actually, since it's runtime, you can actually offer more information. You lose some guarantees however, particularly about static properties like termination.

So dynamic types don't rule out certain programs, but rather route malformed programs to well-defined actions, like throwing exceptions.

TLDR

So the long and the short of it, is that type systems rule out certain programs. Many of the programs are broken in some way, therefore, with type systems we avoid these broken programs.

Body:
Type systems prevent errors

Type systems eliminates illegal programs. Consider the following Python code.

 a = 'foo'
 b = True
 c = a / b

In Python, this program fails; it throws an exception. In a language like Java, C#, Haskell, whatever, this isn't even a legal program. You entirely avoid these errors because they simply aren't possible in the set of input programs.

Similarly, a better type system rules out more errors. If we jump up to super advanced type systems we can say things like this:

 Definition divide x (y : {x : integer | x /= 0}) = x / y

Now the type system guarantees that there aren't any divide-by-0 errors.

What sort of errors

Here's a brief list of what errors type systems can prevent

Out-of-range errors

SQL injection

Generalizing 2, many safety issues (what taint checking is for in Perl)

Out-of-sequence errors (forgetting to call init)

Forcing a subset of values to be used (for example, only integers greater than 0)

Nefarious kittens (Yes, it was a joke)

Loss-of-precision errors

Software transactional memory (STM) errors (this needs purity, which also requires types)

Generalizing 8, controlling side effects

Invariants over data structures (is a binary tree balanced?)

Forgetting an exception or throwing the wrong one

And remember, this is also at compile time. No need to write tests with 100% code coverage to simply check for type errors, the compiler just does it for you :)

Case study: Typed lambda calculus

Alright, let's examine the simplest of all type systems, simply typed lambda calculus.

Basically there are two types,

Type = Unit | Type -> Type

And all terms are either variables, lambdas, or application. Based on this, we can prove that any well typed program terminates. There is never a situation where the program will get stuck or loop forever. This isn't provable in normal lambda calculus because well, it isn't true.

Think about this, we can use type systems to guarentee that our program doesn't loop forever, rather cool right?

Detour into dynamic types

Dynamic type systems can offer identical guarantees as static type systems, but at runtime rather than compile time. Actually, since it's runtime, you can actually offer more information. You lose some guarantees however, particularly about static properties like termination.

So dynamic types don't rule out certain programs, but rather route malformed programs to well-defined actions, like throwing exceptions.

TLDR

So the long and the short of it, is that type systems rule out certain programs. Many of the programs are broken in some way, therefore, with type systems we avoid these broken programs.


答:
A type system helps you avoid simple coding errors, or rather allows the compiler catch those errors for you.

For example, in JavaScript and Python, the following problem will often only be caught at runtime - and depending on testing quality/rarity of the condition may actually make it to production:

```<code>if (someRareCondition)
     a = 1
else
     a = {1, 2, 3}

// 10 lines below
k = a.length
</code>```

While a strongly-typed language will force you to explicitly state that a is an array and will not let you assign an integer. In this way, there isn't any chance a won't have length - even in the rarest cases.

Body:
In JavaScript: The Good Parts by Douglas Crockford, he mentions in his inheritance chapter,

The other benefit of classical inheritance is that it includes the specification of a system of types. This mostly frees the programmer from having to write explicit casting operations, which is a very good thing because when casting, the safety benefits of a type system are lost.

So first of all, what actually is safety? protection against data corruption, or hackers, or system malfunctions, etc.?

What are the safety benefits of a type system? What makes a type system different that allows it to provide these safety benefits?

Body:
A type system helps you avoid simple coding errors, or rather allows the compiler catch those errors for you.

For example, in JavaScript and Python, the following problem will often only be caught at runtime - and depending on testing quality/rarity of the condition may actually make it to production:

if (someRareCondition)
     a = 1
else
     a = {1, 2, 3}

// 10 lines below
k = a.length

While a strongly-typed language will force you to explicitly state that a is an array and will not let you assign an integer. In this way, there isn't any chance a won't have length - even in the rarest cases.


答: I agree with Morons that making the authors correct it is the best course. But if you need a technical solution you could punch it through htmltidy -- an old utility that, well, tidys up HTML code. There is a well maintained managed version on github.</p>

Title: How can I correct html for missing closing tags in c# ?

Body: I have html stored in database received from text editors and because of some mistakes in the code the html is not valid, it is missing some closing tags. and due to this I am not able to process this html for some other operations like generating pdfs. So I need to write some code in c# that can check for tags that are missing closing tags and insert a closing tag at the correct place. </p>


答:
Introduction

Type safety can be achieved with either statically-typed (compiled, static type checking) and/or runtime (evaluated, dynamic type checking) languages.  According to Wikipedia a '... strong type system is described as one in which there is no possibility of an unchecked runtime type error (ed Luca Cardelli). In other writing, the absence of unchecked run-time errors is referred to as safety or type safety ...'

Safety - Static Type Checking

Classically, type safety has been synonymous with static typing, in languages such as C, C++ and Haskell, that are designed to detect type mis-matches when they are compiled.  This has the benefit of avoiding potentially undefined or error-prone conditions when the programme is executed.  This can be invaluable where there is a risk that pointer types may be mis-matched, for example, a situation that could lead to catastrophic consequences if not detected.  In this sense static typing is considered synonymous with memory safety.

Static typing is not completely safe but enhances safety, however.  Even statically-typed systems can have catastrophic consequences.  Many experts consider that statically-typed can be used to write more robust and less error-prone (mission critical) systems.

Statically-typed languages can help to reduce the risk of loss of data or loss of accuracy in numerical work, that can occur due to mis-matching or truncating double to float or mis-matching integral and float types.

There is an advantage in using statically-typed languages for efficiency and speed of execution. The runtime benefits from not having to determine the types during execution.

Safety - Runtime Type Checking

Erlang, for example, is a type declarative, dynamically type checked language that runs on a virtual machine.  Erlang code can be byte compiled.  Erlang is considered perhaps the most important mission-critical, fault tolerant language available, and it is reported that Erlang has a reliability of nine 9's (99.9999999% or not more than 31.5 msecs per year).

Certain languages, such as Common Lisp, are not statically-typed but types can be declared if desired which can help improve speed and efficiency.  It is also to be noted that many of the more widely used interpreted languages, such as Python, are, underneath the evaluation loop, written in statically-typed languages such as C or C++.  Both Commom Lisp and Python are considered type safe by the definition above.

Title: What are the safety benefits of a type system?


答:
In the case of an immutable class implementation, making your internal variables protected would break the contract you have set, in which case the protected getters would not be overkill andnecessary (though you would have to return copies of your variable, not a pointer to it).

In mutable classes, I still prefer getters/setters to granting direct access to my internal variables. This makes sure that you can change how you handle your internal variables as you wish, without fear of breaking any client code. For instance, imagine you have a Java class as follows:

public abstract class SomeClass {
    protected int[] someVariable;

    // Rest of implementation
}
```

<p>Client class extending <code>SomeClass</code> would then directly access <code>someVariable</code>, manipulating it how they see fit. Now, this can create all sorts of problems if these classes do not leave <code>someVariable</code> in an acceptable state. Moreover, if you decide in a subsequent release to change the definition of <code>SomeClass</code> to use a <code>List<Integers></code> instead of an array, you will then break compatibility with the existing code base, which expect <code>SomeClass</code> to have an internal array of <code>int</code>s, not a list of <code>int</code>s.</p>

<p>Using protected getters in this case would allow you to make this change without breaking any pre-established contract.</p>

<pre class="lang-java prettyprint-override"><code>public abstract class SomeClass {
    List<Integer> someVariable;

    protected int[] getSomeVariable () {
        // Return int[] version of someVariable
    }

    // Rest of implementation
}
</code>```

Body:
In the case of an immutable class implementation, making your internal variables protected would break the contract you have set, in which case the protected getters would not be overkill andnecessary (though you would have to return copies of your variable, not a pointer to it).

In mutable classes, I still prefer getters/setters to granting direct access to my internal variables. This makes sure that you can change how you handle your internal variables as you wish, without fear of breaking any client code. For instance, imagine you have a Java class as follows:

public abstract class SomeClass {
    protected int[] someVariable;

    // Rest of implementation
}

Client class extending SomeClass would then directly access someVariable, manipulating it how they see fit. Now, this can create all sorts of problems if these classes do not leave someVariable in an acceptable state. Moreover, if you decide in a subsequent release to change the definition of SomeClass to use a List<Integers> instead of an array, you will then break compatibility with the existing code base, which expect SomeClass to have an internal array of ints, not a list of ints.

Using protected getters in this case would allow you to make this change without breaking any pre-established contract.

public abstract class SomeClass {
    List<Integer> someVariable;

    protected int[] getSomeVariable () {
        // Return int[] version of someVariable
    }

    // Rest of implementation
}


答:
The earlier in the software development cycle you can catch an error, the less expensive it is to fix.  Consider an error that causes your biggest client, or all your clients to lose data.  Such an error could be the end of your company if it is only caught after real customers have lost data!  It is clearly less expensive to find and fix this bug before moving it to production.

Even for less costly errors, more time and energy is spent if testers are involved than if programmers can find and fix it.  It's cheaper if it does not get checked into source control where other programmers can build software that relies on it.  Type safety prevents certain classes of errors from even compiling, thus eliminating almost the entire potential cost of those errors.

But that isn't the whole story.  As anyone who programs in a dynamic language will tell you, some times it's nice if your program just compiles so you can try out part of it without getting every little detail to work out.  There is a trade-off between safety and convenience.  Unit tests can mitigate some of the risk of using a dynamic language, but writing and maintaining good unit tests has its own cost which may be higher than that of using a type-safe language.

If you are experimenting, if your code will only be used once (such as a one-time report), of if you are in a situation where you wouldn't bother to write a unit test anyway, then a dynamic language is probably perfect for you.  If you have a large application and want to change one part without breaking the rest of it, then type safety is a life saver.  The types of errors type safety catches are exactly the kind of errors that humans tend to overlook or get wrong when refactoring.


答: One other big reason to go with PHP -- security. If you expose that .ini file to the webroot, most typical web server configurations will serve that file as a plain text file exposing whatever configuration secrets your app has. A php file containing purely code will just return a blank page, if you don't bother to take any security steps which are also an option because you are now in a programming language that gives you options.</p>


答:
If your software is security-critical, you must update as soon as possible, no excuses. You don't want a little bug in a graphics library to make your whole program vulnerable.

Otherwise, when the lib is mature, it's "If it ain't broke, don't fix it." for me. Sooner or later, I might need a feature of a later version and have no choice but to update, but til then, the effort is hard to justify. On the other hand, when I work with a relatively new lib or framework, like Grails or ExtJS, I stay up-to-date with the latest version because these products don't feel completely mature yet, so updating is likely to save me from running into one of those bugs the later version fixed.

Title: How to keep your third party libraries up to date?

Body:
Let's say that I have a project that depends on 10 libraries, and within my project's trunk I'm free to use any versions of those libraries. So I start with the most recent versions. Then,  each of those libraries gets an update once a month (on average). Now, keeping my trunk completely up to date would require updating a library reference every three days.

This is obviously too much. Even though usually version 1.2.3 is a drop-in replacement for version 1.2.2, you never know without testing. Unit tests aren't enough; if it's a DB / file engine, you have to ensure that it works properly with files that were created with older versions, and maybe vice versa. If it has something to do with GUI, you have to visually inspect everything. And so on.

How do you handle this? Some possible approaches:

If it ain't broke, don't fix it. Stay with your current version of the library as long as you don't notice anything wrong with it when used in your application, no matter how often the library vendor publishes updates. Small incremental changes are just waste.

Update frequently in order to keep change small. Since you'll have to update some day in any case, it's better to update often so that you notice any problems early when they're easy to fix, instead of jumping over several versions and letting potential problems to accumulate.

Something in between. Is there a sweet spot?


问:
Why isn't there a typeclass for functions?
In a learning problem I've been messing around with, I realised I needed a typeclass for functions with operations for applying, composing etc. Reasons...

It can be convenient to treat a representation of a function as if it were the function itself, so that applying the function implicitly uses an interpreter, and composing functions derives a new description.

Once you have a typeclass for functions, you can have derived typeclasses for special kinds of functions - in my case, I want invertible functions.

For example, functions that apply integer offsets could be represented by an ADT containing an integer. Applying those functions just means adding the integer. Composition is implemented by adding the wrapped integers. The inverse function has the integer negated. The identity function wraps zero. The constant function cannot be provided because there's no suitable representation for it.

Of course it doesn't need to spell things as if it the values were genuine Haskell functions, but once I had the idea, I thought a library like that must already exist and maybe even using the standard spellings. But I can't find such a typeclass in the Haskell library.

I found the Data.Function module, but there's no typeclass - just some common functions that are also available from the Prelude.

So - why isn't there a typeclass for functions? Is it "just because there isn't" or "because it's not so useful as you think"? Or maybe there's a fundamental problem with the idea?

The biggest possible problem I've thought of so far is that function application on actual functions would probably have to be special-cased by the compiler to avoid a looping problem - in order to apply this function I need to apply the function application function, and to do that I need to call the function application function, and to do that...

More Clues

Example code to show what I'm aiming for...

```<code>{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}

--  In my first version, Doable only had the one argument f. This version
--  seemed to be needed to support the UndoableOffset type.
--
--  It seems to work, but it also seems strange. In particular,
--  the composition function - a and b are in the class, but c isn't,
--  yet there's nothing special about c compared with a and b.
class Doable f a b where
  fwdApply :: f a b -> a -> b
  compDoable :: f b c -> f a b -> f a c

--  In the first version, I only needed a constraint for
--  Doable f a b, but either version makes sense.
class (Doable f a b, Doable f b a) => Undoable f a b where
  bwd      :: f a b -> f b a

  bwdApply :: f a b -> b -> a
  bwdApply f b = fwdApply (bwd f) b

--  Original ADT - just making sure I could wrap a pair of functions
--  and there were no really daft mistakes.
data UndoableFn a b = UFN { getFwd :: a -> b, getBwd :: b -> a }

instance Doable UndoableFn a b where
  fwdApply = getFwd
  compDoable f g = UFN ((getFwd f) . (getFwd g)) ((getBwd g) . (getBwd f))

instance Undoable UndoableFn a b where
  bwd f    = UFN (getBwd f) (getFwd f)
  bwdApply = getBwd

--  Making this one work led to all the extensions. This representation
--  can only represent certain functions. I seem to need the typeclass
--  arguments, but also to need to restrict which cases can happen, hence
--  the GADT. A GADT with only one constructor still seems odd. Perhaps
--  surprisingly, this type isn't just a toy (except that the whole thing's
--  a toy really) - it's one real case I need for the exercise. Still a
--  simple special case though.
data UndoableOffset a b where
  UOFF :: Int -> UndoableOffset Int Int

instance Doable UndoableOffset Int Int where
  fwdApply (UOFF x) y = y+x
  compDoable (UOFF x) (UOFF y) = UOFF (x+y)

instance Undoable UndoableOffset Int Int where
  bwdApply (UOFF x) y = y-x
  bwd (UOFF x) = UOFF (-x)

--  Some value-constructing functions
--  (-x) isn't shorthand for subtraction - whoops.
undoableAdd :: Int -> UndoableFn Int Int
undoableAdd x = UFN (+x) (\y -> y-x)

undoableMul :: Int -> UndoableFn Int Int
undoableMul x = UFN (*x) (`div` x)

--  With UndoableFn, it's possible to define an invertible function
--  that isn't invertible - to break the laws. To prevent that, need
--  the UFN constructor to be private (and all public ops to preserve
--  the laws). undoableMul is already not always invertible.
validate :: Undoable f a b => Eq a => f a b -> a -> Bool
validate f x = (bwdApply f (fwdApply f x)) == x

--  Validating a multiply-by-zero invertible function shows the flaw
--  in the validate-function plan. Must try harder.
main = do putStrLn . show $ validate (undoableAdd 3) 5
          putStrLn . show $ validate (undoableMul 3) 5
          --putStrLn . show $ validate (undoableMul 0) 5
          fb1 <- return $ UOFF 5
          fb2 <- return $ UOFF 7
          fb3 <- return $ compDoable fb1 fb2
          putStrLn $ "fwdApply fb1  3 = " ++ (show $ fwdApply fb1  3)
          putStrLn $ "bwdApply fb1  8 = " ++ (show $ bwdApply fb1  8)
          putStrLn $ "fwdApply fb3  2 = " ++ (show $ fwdApply fb3  2)
          putStrLn $ "bwdApply fb3 14 = " ++ (show $ bwdApply fb3 14)
</code>```

The application involves a kind of unification where unified values aren't equal, but are related via those invertible functions - Prolog-style logic but with a = f(b) constraints rather than a = b. Most of the composition will result from optimizing a union-find structure. The need for inverses should be obvious.

If no item in a unified set has an exact value, then a particular item can only be quantified relative to another item in that unified set. That's why I don't want to use "real" functions - computing those relative values. I could drop the whole function aspect and just have absolute and relative quantities - I probably only need numbers/vectors and (+) - but my inner architecture astronaut wants his fun.

The only way I break the links apart again is via backtracking, and everything is pure - union-find will be done using keys into an IntMap as "pointers". I have simple union-find working, but as I haven't added the invertible functions yet, there's no point listing it here.

Reasons I can't use Applicative, Monad, Arrow etc

The main operations I need the function abstraction class to provide are application and composition. That sounds familiar - e.g. the Applicative (<*>), Monad (>>=) and Arrow (>>>) are all composition functions. However, the types that implement the function abstraction in my case will contain some data structure that represents a function, but which isn't (and can't contain) a function, and which can only represent some limited set of functions.

As I mentioned in the explanation of the code, sometimes I can only quantify one item relative to another because no item in a "unified" cluster has an exact value. I want to be able to derive a representation of that function, which in general will be the composition of several provided functions (walking up to a common ancestor in the union/find tree) and of several inverse functions (walking back down to the other item).

Simple case - where the original "functions" are limited to integer-offset "functions", I want the composed result as an integer-offset "function" - add the component offsets. That's a big part of why the composition function needs to be in the class as well as the application function.

This means I cannot provide the operations pure, return or arr for my types, so I can't use Applicative, Monad or Arrow.

This isn't a failure of those types - it's a mismatch of abstractions. The abstraction I want is of a simple pure function. There's no side-effecting, for example, and no need to build a convenient notation for sequencing and composing the functions other than an equivalent of the standard (.) that applies to all functions.

I could instance Category. I'm confident that all my functiony-things will be able to provide an identity, though I probably don't need it. But as Category doesn't support application, I'd still need a derived class anyway to add that operation.

答:
Well, I don't know of any baked in ideas that market themselves as representing "function-y" things. But there are several that come close

Categories

If you have a simple function concept that has identities and composition, than you have a category.

```<code>class Category c where
  id :: c a a
  (.) :: c b c -> c a b -> c a c
</code>```

The disadvantage is that you cannot create a nice category instance with a set of objects (a, b, and c). You can create a custom category class I suppose.

Arrows

If your functions have a notion of products and can inject arbitrary functions, than arrows are for you

```<code> class Arrow a where
   arr :: (b -> c) -> a b c
   first :: a b c -> a (b, d) (c, d)
   second :: a b c -> a (d, b) (d, c)
</code>```

ArrowApply has a notion of application which looks important for what you want.

Applicatives

Applicatives have your notion of application, I've used them in an AST to represent function application.

```<code>class Functor f => Applicative f where
  pure :: a -> f a
  (<*>) :: f (a -> b) -> f b -> f c
</code>```

There are many other ideas. But a common theme is to build up some data structure representing your function, and than pass it to an interpretation function.

This also how many free monads work. I'd suggest poking at these if you're feeling brave, they're a powerful tool for the stuff that you're suggesting and essentially let you build up a datastructure using do notation and then collapse it into a side effecting computation with different functions. But the beauty is that these functions just operation on the datastructure, and aren't really aware of how you made it all. This is what'd I'd suggest for your example of an interpreter.

Body:
Well, I don't know of any baked in ideas that market themselves as representing "function-y" things. But there are several that come close

Categories

If you have a simple function concept that has identities and composition, than you have a category.

class Category c where
  id :: c a a
  (.) :: c b c -> c a b -> c a c

The disadvantage is that you cannot create a nice category instance with a set of objects (a, b, and c). You can create a custom category class I suppose.

Arrows

If your functions have a notion of products and can inject arbitrary functions, than arrows are for you

 class Arrow a where
   arr :: (b -> c) -> a b c
   first :: a b c -> a (b, d) (c, d)
   second :: a b c -> a (d, b) (d, c)

ArrowApply has a notion of application which looks important for what you want.

Applicatives

Applicatives have your notion of application, I've used them in an AST to represent function application.

class Functor f => Applicative f where
  pure :: a -> f a
  (<*>) :: f (a -> b) -> f b -> f c

There are many other ideas. But a common theme is to build up some data structure representing your function, and than pass it to an interpretation function.

This also how many free monads work. I'd suggest poking at these if you're feeling brave, they're a powerful tool for the stuff that you're suggesting and essentially let you build up a datastructure using do notation and then collapse it into a side effecting computation with different functions. But the beauty is that these functions just operation on the datastructure, and aren't really aware of how you made it all. This is what'd I'd suggest for your example of an interpreter.


问:
Data Structure for Small Number of Agents in a Relatively Big 2D World
I'm working on a project where we will implement a kind of world simulation where there is a square 2D world. Agents live on this world and make decisions like moving or replicating themselves based on their neighbor cells(world=grid) and some extra parameters(which are not based on the state of the world). I'm looking for a data structure to implement such a project.

My concerns are :
I will implement this 3 times: sequential, using OpenMP, using MPI. So if I can use the same structure that will be quite good.

The first thing comes up is keeping a 2D array for the world and storing agent references in it. And simulate the world for each time slice by checking every cell in each iteration and further processing if an agents is found in the cell. The downside is what if I have 1000x1000 world and only 5 agents in it. It will be an overkill for both sequential and parallel versions to check each cell and look for possible agents in them. I can use quadtree and store agents in it, but then how can I get the information about neighbor cells then?

Please let me know if I should elaborate more.

答:
To start with, just keep a list of all the agents with their locations.  When working with one agent, scan the list to find all the other agents close enough to be of interest.  Skip creating an actual world grid array; it limits you too much and wastes too much memory.

Next, as your agent list gets real big and you waste too much time looking at agents too far away, impose a grid on your world.  With a 1000x1000 world, maybe a 100x100 grid would work.  Each element will hold a list of agents that are within its 100 world-cells.  Now to check for neighbors, look in the agent's grid cell and take all the occupants who are also in adjacent world-cells.  In some cases, you may need to check certain adjacent grid cells also.  This grid lets you handle 100,000 items as if they were only 100 or so regarding which agents are in adjacent, or even near-by, world-cells.

A quadtree might work better if your agents tend to cluster together, but that gets tricky even without "parallelism".  My guess is the combination of a list of all agents, each agent with its world-grid cell location, and the mid-sized grid with lists of agents within each cell boundary, should work okay.

(The mid-sized grid's size needs to based on a combination of your world size and the total number of agents.  Getting the perfect size is tricky, but just about any size is going to be vastly superior to the extremes of not using a grid and using a world-sized grid.)

(Note that with this technique your world-grid could become truly vast--trillions of cells on a side--without causing any difficulty.  Mind, your agents would need to be able to see across quite a log of cells in order to find each other.)


问:
Why "Fork me on GitHub"?
I understand how GitHub works, but one thing I've been confused about is, why almost every OSS project lately has a "Fork me on GitHub" link on their homepage. For example,

http://jqtjs.com/, http://www.daviddurman.com/flexi-color-picker/, and others.

Why is this so common? Is it that they want/need code validation, checking for security/performance improvements that they may not know how to do?

Is it meant to show that this is a collaborative project - you're welcome to add improvements?

Do they work for GitHub, or want to promote their service? Oddly enough, I don't think I've seen a "Fork project on Bitbucket" logo recently.

My first reaction to that logo was that the project probably needs to be modified (forked) in order to integrate it with anything useful - or that they are encouraging fragmented codebase, encouraging everyone to make their own fork of the project. But I don't think that is the intent.

答:
Github is trying to widen their userbase to include any collaborative work, not only source code hosting; and thus do they push the fork and send a pull request method.  Making patches and sending them via email wouldn't be attractive to most people other than programmers and alike.  Push permissions are not really important here, as eventually most of the git (or mercurial, darcs, or whatever) users with considerable amount of knowledge of the tool and development workflow would be able to send an email with a patch attached somehow.

Also, Github is more about social networking than code hosting: your profile, repos and changes make up your identity as a software developer, just like your tweets and '+1's.  All those ribbons and buttons and alike are used as a means of self-canvassing, a path to the profile of the person whom the project belongs to.  Nowadays, having a decent Github profile is the first thing expected from a developer.  Even though I use mercurial for anything, I push to Github via hg-git for these reasons.

Eventually, that "Fork me on Github" ribbon differs just slightly in purpose from the "Tweet this Article" or the "Share this Thing on G+" button.  (Also having a fork of a repository on your list of repos means "I am a user of this thing", there are thousands of forks of Linux on github, even though Linus Torvalds does not accept pull requests.)

Title: Why "Fork me on GitHub"?


问:
How to read thousands lines of code without any documentation?
Previously I was searching for a good TimeLine control for a WPF project. I found an answer in Here which direct me to this CodePlex project.

Now I want to change code to feed my culture needs. But there are some mismatches!

My question is:

How you interact with such thousands lines of code?

EDIT:

Any shortcut will be great!

答:
I would start by using the Leo Editor in @shadow mode with active use of cloned nodes. This allows one to add notes and comments for each section of code under study without changing the code, and the annotations will always be in context, next to the code it's talking about. Here's an example workflow the docs:

For example, when I fix a bug in Leo,
  I create an ordinary node to represent
  the bug. This bug node is my view of
  all the data in Leo’s source code that
  relates to the bug. As I discover code
  related to the bug, I clone their
  nodes and move them under the bug
  node. I’ll also add ordinary nodes as
  children of the bug node. These nodes
  contain the original bug report,
  descriptions of how I fixed the bug,
  test data, or any other notes I might
  want to keep.

Once I have created the bug node, I
  concentrate only on that node and its
  children. I can examine the bug node
  and its children without having to
  jump around the outline. Everything I
  need is in one place. When I get
  around to actually fixing the bug I
  can do so by changing the clones.
  Again, I do not have to jump around
  the outline. It doesn’t matter how big
  or complex the entire outline is: I am
  only dealing with the bug node and its
  children. This extremely narrow focus
  makes it much easier to fix bugs.


问:
code review with git-flow and github
With regular git and github I can do a code review by simply creating a pull request of the feature branch I'm working on to the master branch. How would I do code reviews with git-flow? With workflow like "git flow feature finish` I'm confused as to where the code review actually happens and how git-flow or git can facilitate that review.</p>

答:
The process the team I work with uses for this is as follows:

Create a feature branch: git flow feature start module_1

The code is updated on the feature branch

As changes are committed they are pushed to GitHub (or once at the end if preferred)

When the feature is completed a pull request is opened in GitHub comparing develop and the feature branch module_1

The team reviews the pull request and makes comments

Any changes from the pull request are made to the feature branch

Once all changes are incorporated on the feature branch the feature branch is finished: git flow feature finish module_1

The develop branch is pushed to GitHub (GitHub will automatically mark the pull request as closed/merged when this happens)

Normally all of this process is done by the original author but that is not required.  Anyone on our team can step in and pick up this process at any point.  All they have to do is checkout the feature branch and continue on with the process.  Who ever runs git flow feature finish module_1 will have the luxury of their local feature branch being deleted but anyone else who checked out the branch with have to do this manually if they want to using something like git branch -D feature/module_1.

For hotfixes we use a similiar approach and create the pull request in GitHub before we finish the hotfix.

Title: code review with git-flow and github


问:
Are we queueing and serializing properly?
We process messages through a variety of services (one message will touch probably 9 services before it's done, each doing a specific IO-related function).  Right now we have a combination of the worst-case (XML data contract serialization) and best-case (in-memory MSMQ) for performance.

The nature of the message means that our serialized data ends up about 12-15 kilobytes, and we process about 4 million messages per week.  Persistent messages in MSMQ were too slow for us, and as the data grows we are feeling the pressure from MSMQ's memory-mapped files.  The server is at 16GB of memory usage and growing, just for queueing.  Performance also suffers when the memory usage is high, as the machine starts swapping.  We're already doing the MSMQ self-cleanup behavior.

I feel like there's a part we're doing wrong here.  I tried using RavenDB to persist the messages and just queueing an identifier, but the performance there was very slow (1000 messages per minute, at best).  I'm not sure if that's a result of using the development version or what, but we definitely need a higher throughput[1].  The concept worked very well in theory but performance was not up to the task.

The usage pattern has one service acting as a router, which does all reads.  The other services will attach information based on their 3rd party hook, and forward back to the router.  Most objects are touched 9-12 times, although about 10% are forced to loop around in this system for awhile until the 3rd parties respond appropriately.  The services right now account for this and have appropriate sleeping behaviors, as we utilize the priority field of the message for this reason.

So, my question, is what is an ideal stack for message passing between discrete-but-LAN'ed machines in a C#/Windows environment?  I would normally start with BinaryFormatter instead of XML serialization, but that's a rabbit hole if a better way is to offload serialization to a document store.  Hence, my question.

[1]: The nature of our business means the sooner we process messages, the more money we make.  We've empirically proven that processing a message later in the week means we are less likely to make that money.  While performance of "1000 per minute" sounds plenty fast, we really need that number upwards of 10k/minute.  Just because I'm giving numbers in messages per week doesn't mean we have a whole week to process those messages.

=============== edit:

Additional information

Based on the comments, I'll add some clarification:

I'm not sure serialization is our bottleneck.  I've benchmarked the application, and while serialization does show up in the heat graph, it's only responsible for maybe 2.5-3% of the service's CPU utilization.

I'm mostly concerned about the permanence of our messages and potential misuse of MSMQ.  We are using non-transactional, non-persistent messages so we can keep queueing performance up, and I would really like to have at least persistent messages so they survive a reboot.

Adding more RAM is a stopgap measure.  The machine has already gone from 4GB -> 16GB of RAM and it's getting harder and harder to take it down to continue adding more.

Because of the star routing pattern of the application, half the time an object is popped then pushed to a queue it doesn't change at all.  This lends itself again (IMO) to storing it in some kind of key-value store elsewhere and simply passing message identifiers.

The star routing pattern is integral to the application and will not change.  We can't application-centipede it because every piece along the way operates asynchronously (in a polling fashion) and we want to centralize the retry behavior in one place.

The application logic is written in C#, the objects are immutable POCOs, the target deployment environment is Windows Server 2012, and we are allowed to stand up additional machines if a particular piece of software is only supported in Linux.

My goals are to maintain current throughput while reducing memory footprint and increasing fault tolerance with a minimum outlay of capital.

答:
We had a somewhat similar situation several years ago, with a queued message system (audio fingerprints in our case). We strongly valued persistency of the enqueued data packets, but we found out that enqueuing everything to disk and consuming the queue from disk was very expensive.

If we switched to memory-based queues, performance was exceptional, but we had a major problem. Every once in a while the consumers of the queues became unavailable for a considerable amount of time (the consumer and producer elements in our case are connected via WAN), so the producer's  queue would grow to a point it became unmanageable and like your case, once memory consumption was very high, excessive memory thrashing during swapping brought the system to a complete crawl.

We designed a queue we christened VMQueue  (for Virtual Memory Queue, a very bad name in retrospective). The idea of this queue is that if the consumer process is running up to par, in other words, processing fast enough to be able to keep the number of enqueued elements below a certain level, then it has basically the same performance of a memory-based queue. However, when the consumer slows down or becomes unavailable and the producer queue grows to a certain size, then the queue will begin automatically paging elements to-and-from disk (using BinaryFormatter serialization by the way).   This process keeps memory usage completely controlled, and the paging process is fast, or at least much faster than the virtual memory swapping occurring during heavy-memory load. Once the consumer manages to drain the queue below the threshold, it resumes working as a pure memory-based queue

If the system crashes or reboots, then the queue is able to recover all paged elements that were stored to disk, it will only lose the elements that were still kept in memory before the crash.  If you can afford losing a limited number of packets during a crash or reboot, this queue might be helpful.

If you are interested, I can share the VMQueue class source code so you can play around with it. The queue will accept any class that is marked as Serializable.  Upon creation of the queue you establish the size of the page in number of elements.  The class interface is virtually the same of a standard Queue class. The code is very old however, (.net 1.1) so no generic interface exists unfortunately.

I know that moving from the proven MSMQ technology is a huge bet, however this queue has been working reliably for almost 6 years and has allowed us to survive and recover from scenarios where the producer machine has been offline for several weeks! Please let me know if you are interested.   :)


问:
documentation of typescript code
my question is rather short:

How do I document typescript code properly?
I found out that for projects becoming bigger and bigger, it is important to look at a function and immediately know parameters, what it returns and side-effects etc. It is tiring to have just a bunch of comments before a function, most of the time these 'blocks' even look differently in style. What I am looking for is a documentation tool like javadoc or doxygen for typescript. Is there anything out there? Or is it possible to 'abuse' a documentation tool and get it to work with typescript?

答:
If you are looking for something like JavaDoc, you might find JsDoc useful.

I have written a small tool for converting JsDoc comments in TypeScript definition files into JavaDoc-style HTML pages with links between types.

You can find it here, but right now it needs be built from source:

typescript-docs

Title: documentation of typescript code


答:
So first of all, what actually is safety? Protection against data corruption, or hackers, or system malfunctions, etc.?

All of the other answers and more. In general, "type safety" simply means that none of the programs a compiler successfully compiles will contain type errors.

Now, what is a type error? In principle, you can specify any undesirable property as a type error, and some type systems will be able to statically ensure no program has such an error.

By "property" above, I mean some type of logical proposition that applies to your program, for instance, "all indices are within array bounds". Other types of properties include, "all deferenced pointers are valid", "this program doesn't perform any I/O", or "this program performs I/O only to /dev/null", etc. Just about any sort of property can be specified and type checked in this way, depending on the expressiveness of your type system.

Dependent type systems are among the most general of type systems, via which you can enforce pretty much any property you like. It's not necessarily easy to do so though, as sophisticated properties are subject to incompleteness courtesy of Gödel.


问:
What is the right level of site blocking for you as a developer?
Almost all developers who work for a large corporation find themselves on the wrong side of site blocking software. It can be both frustrating ("Just let me read that damn blog!") and helpful ("Woah! Dodged a bullet with that site").

In your opinion, what is the right level of blocking to apply to developers and why?

答:
Honestly I'm generally ok with site blocking as long as I can actually get to the site's that I need to without a wasting additional time and/or have to jump through a bunch of hoops.

The various corporations I've worked at have blocked sites but they were generally the sites you shouldn't be visiting at work (warez, porn, etc.).  If you had a need to get to these blocked sites there was a process to get approval if necessary.  I never ran across a site that I needed at work that was actually blocked.  Usually I would get a warning page that it hadn't been categorized but I could continue to the site with an extra click.  I can deal with that, since I understand what the site blocking was setup as a corporate policy.

Note that they didn't block facebook, etc but they did monitor the amount of time spent on those sites.


问:
Why can't the IT industry deliver large, faultless projects quickly as in other industries?
After watching National Geographic's MegaStructures series, I was surprised how fast large projects are completed. Once the preliminary work (design, specifications, etc.) is done on paper, the realization itself of huge projects takes just a few years or sometimes a few months.

For example, Airbus A380 "formally launched on Dec. 19, 2000", and "in the Early March, 2005", the aircraft was already tested. The same goes for huge oil tankers, skyscrapers, etc.

Comparing this to the delays in the software industry, I can't help wondering why most IT projects are so slow, or more precisely, why they cannot be as fast and faultless, at the same scale, given enough people?

Projects such as the Airbus A380 present both:

Major unforeseen risks: while this is not the first aircraft built, it still pushes the limits of the technology and things which worked well for smaller airliners may not work for the larger one due to physical constraints; in the same way, new technologies are used which were not used before, because for example they were not available in 1969 when the Boeing 747 was certified.

Risks related to human resources and management in general: people quitting in the middle of the project, inability to reach a person because she's on vacation, ordinary human errors, etc.

With those risks, people still achieve projects like those large airliners in a very short period of time, and despite the delivery delays, those projects are still hugely successful and of a high quality.

When it comes to software development, the projects are hardly as large and complicated as an airliner (both technically and in terms of management), and have slightly less unforeseen risks from the real world.

Still, most IT projects are slow and late, and adding more developers to the project is not a solution (going from a team of ten developers to two thousand will sometimes make it possible to deliver the project faster, sometimes not, and sometimes will only harm the project and increase the risk of not finishing it at all).

Those which are still delivered may often contain a lot of bugs, requiring consecutive service packs and regular updates (imagine "installing updates" on every Airbus A380 twice a week to patch the bugs in the original product to prevent the aircraft from crashing).

How can such differences be explained? Is it due exclusively to the fact that the software development industry is too young to be able to manage thousands of people on a single project in order to deliver large scale, nearly faultless products very fast?

答:
For me the main problem that software engineering face is use cases, user and cross platforms.

Use cases

How many use cases does an airplane have? Most of it is just to fly from one place to other. Maybe there are more such as radar, traffic control, etc., but the use case is clear and not much. In software engineering, we are faced with unclear requirements and user who do not know what they want. Different user need different configuration / flow, can an airplane be customized as user want (I want to have tv and refrigerator!)?

User

Who operates an airplane? A pilot, a copilot, some stewards (if counted) and tower operators. They are all pros and has their job descriptions clear. Software are used by noobs and dummies, not to mention evil hackers and crackers, while still need to be integrateable with other modules (such as OpenID or Google AdSense). If an airplane can be operated by dummies while still surviving from missiles or ninja robbers, then you can say that the airplane has the same quality with software.

Cross platforms

An airplane fly only in the earth's sky. I'm unsure about how they handle the foggy or windy or hot, cold and humid climate, but an airplane is not designed to fly at different gravitation level (I will be amazed if it can fly to Mars). Sometimes, an application must survive different platforms, such as Internet Explorer, Google Chrome, Firefox and Safari for browser (sorry Opera), or Windows XP / 7 / 8, or Linux for OS level. Not to mention mobile devices and different resolution and orientations.

ContentLicense: CC BY-SA 4.0


问:
Why would Java app make RPC call to itself?
I am working with a multithreaded homegrown multi-module app in my new job.  We use the the Thrift protocol to communicate RPC calls between different stand-alone applications in a distributed system.  One of them listens on multiple ports and I just noticed that it actually makes an RPC call to itself from one thread invoked from one socket it listens to (web service call) to another port within the same app.  I verified that it could accomplish the same thing if it just went and directly called the method that the remote procedure ultimately invokes as it is all within the same application, same JVM.  To make it even more mysterious, the call is completely synchronous, i.e. no callbacks involved.  The first thread totally sits and waits until it makes a call across the wire to itself and comes back.

Now, I am perplexed why anybody would do it this way.  It seems like calling somebody on the phone that sits in the same room as you do.  Can anybody provide an explanation why the developer before me would come up with the above mentioned model?  Maybe there is a reason and I am missing something.

答: At a guess they thought that perhaps, someday, they might like to put that server into another process. And so, they used the RPC mechanism in preparation for a future that never came.</p>


问:
Just being hired as a senior developer, never even been a junior developer, what should I expect?
I've been a freelancer and a coder by night for a while, and recently, I've been hired after several levels of interviews in a nice NY company, even though I've some lacks in specific fields. Is this common for companies to hire seniors with less experience? Will they wait some weeks to respect a certain learning curve?

I don't know anything about working in a company, so that's why I worry. After one week, I'm still checking and exploring sources, but after one week of work, it seems that some coworkers are considering that I'm slow. I'm good in maths, physics, algorithms, but still I need to learn about all the templates used in this company.

Anyone here already received a less-experienced senior member in his team? Is this acceptable?

I'm planing on having a meeting with my boss to stop worrying about that. Sounds like a good idea?

[EDIT]

Thanks for these answers. I'm definitely a -new - senior developer. I returned to the office with more confidence on Monday. I guess that it's normal to feel a bit incompetent in front of unknown templates/sources during the first weeks when you receive a good pay.

答:
There is no commonly accepted definition of "senior developer". Definitions may exist within organizations but a senior developer usually represents someone:

With software development experience (3-5 years minimum),

Can work without constant supervision (often with no supervision),

Familiar with the development environment and tools,

Capable of supervising or teaching junior developers,

Capable of designing and implementing small to medium sized projects.

It is hard to talk about your specific situation but there is usually is a learning curve when joining a new team.

No matter how standard the tools and processes they use, each team has a history of decisions that lead them to their current state. If the organization uses custom libraries or environments, my first question would be to ask about documentation and training. Big companies may have formal training for new employees, even senior ones. Read any existing designs, the build environment documentation, processes and so on. If these do not exist, offer to document them.

I would then ask to pair with an existing senior developer. This is usually the fastest way to learn what is expected and how things work. How did they solve that problem? How much effort did they spend on unit tests and reviews? Why did they do it this way and not that way? Ensure the other developer helps you setup your development environment and walks you through the release process, too.

Make it clear to them you know the language and tools, just not their techniques. For example, if you did things a different way previously and think it is better than their way, tentatively and respectfully suggest it.

Hopefully, pairing with them will not slow them down. They may even appreciate another set of eyes to catch typos and issues before they are committed to source control.

Lastly, realize you are not going to fully understand a large project within a week so start fixing small bugs or features. Make sure your buddy reviews them and you get any and all feedback. You will miss things. You will make mistakes. That's OK. Learn from them, do not repeat them and work hard. If you are good at what you do, you will get there.


问:
Is an event loop just a for/while loop with optimized polling?
I'm trying to understand what an event loop is. Often the explanation is that in an event loop, you do something until you're notified that an event has occurred. You then handle the event and continue doing what you were doing before.

To map the above definition with an example. I have a server which 'listens' in a event loop, and when a socket connection is detected, the data from it gets read and displayed, after which the server resumes/starts listening as it did before.

However, this event happening and us getting notified 'just like that' are to much for me to handle. You can say: "It's not 'just like that' you have to register an event listener". But what's an event listener but a function which for some reason isn't returning. Is it in it's own loop, waiting to be notified when an event happens? Should the event listener also register an event listener? Where does it end?

Events are a nice abstraction to work with, however just an abstraction. I believe that in the end, polling is unavoidable. Perhaps we are not doing it in our code, but the lower levels (the programming language implementation or the OS) are doing it for us.

It basically comes down to the following pseudo code which is running somewhere low enough so it doesn't result in busy waiting:

```<code>while(True):
    do stuff
    check if event has happened (poll)
    do other stuff
</code>```

This is my understanding of the whole idea, and I would like to hear if this is correct. I'm open in accepting that the whole idea is fundamentally wrong, in which case I would like the correct explanation.

答:
No. It is not "optimized polling." An event-loop uses interrupt-driven I/O instead of polling.

While, Until, For, etc. loops are polling loops.

"Polling" is the process of repeatedly checking something. Since the loop code executes continuously, and because it is a small, "tight" loop, there is little time for the processor to switch tasks and do anything else. Almost all "hangs," "freezes," "lockups" or whatever you want to call it when the computer becomes unresponsive, are the manifestation of code being stuck in an unintended polling loop. Instrumentation will show 100% CPU usage.

Interrupt-driven event loops are far more efficient than polling loops. Polling is an extremely wasteful use of CPU cycles so every effort is made to eliminate or minimize it.

However, to optimize code quality, most languages try to use the polling loop paradigm as closely as possible for event handing commands since they serve functionally similar purposes within a program. Thus, with polling being the more familiar way to wait for a keypress or something, it is easy for the inexperienced to use it and wind up with a program that may run fine by itself, but nothing else works while it's running. It has "taken over" the machine.

As explained in other answers, in interrupt-driven event handing, essentially a "flag" is set within the CPU and the process is "suspended" (not allowed to run) until that flag is changed by some other process (such as the keyboard driver changing it when the user has pressed a key). If the flag is an actual hardware condition such as a line being "pulled high," it's called an "interrupt" or "hardware interrupt." Most however, are implemented as just a memory address on the CPU or in main memory (RAM) and are called "semaphores."

Semaphores can be changed under software control and so can provide a very fast, simple signalling mechanism between software processes.

Interrupts, however, can only be changed by hardware. The most ubiquitous use of interrupts is the one triggered at regular intervals by the internal clock chip. One of the countless kinds of software actions activated by clock interrupts, is the changing of semaphores.

I've left out a lot but had to stop somewhere. Please ask if you need more details.


答:
Something that Joel Spolsky wrote way back when on his blog (can't find the article now) really stuck with me regarding this:

He said how code is not natural human language, but as programmers, we are easily lulled into thinking that it is, and that we should be able to read it as such. Consequently, a lot of us look at new code and expect to be able to just "read it" and understand it immediately, as if it was a block of text in English.

So I think the key is to basically just be slow, methodical, and scientific. And as others have said - comment it (and even refactor) as you go. Don't fall into the mindset of "I should just look at it and understand immediately".

Oh, and yes, I still fall into this trap myself sometimes. "Do as I say, not as I do", and all that. :)

Title: How to read thousands lines of code without any documentation?

Body:
Previously I was searching for a good TimeLine control for a WPF project. I found an answer in Here which direct me to this CodePlex project.

Now I want to change code to feed my culture needs. But there are some mismatches!

My question is:

How you interact with such thousands lines of code?

EDIT:

Any shortcut will be great!


答: It is possible that the program that runs on that process was designed to be deployed to multiple nodes; in that case you have the same process on two nodes, and the process from node 1 calls the process of node 2 even though they run the same program.</p>

Title: Why would Java app make RPC call to itself?

Body:
I am working with a multithreaded homegrown multi-module app in my new job.  We use the the Thrift protocol to communicate RPC calls between different stand-alone applications in a distributed system.  One of them listens on multiple ports and I just noticed that it actually makes an RPC call to itself from one thread invoked from one socket it listens to (web service call) to another port within the same app.  I verified that it could accomplish the same thing if it just went and directly called the method that the remote procedure ultimately invokes as it is all within the same application, same JVM.  To make it even more mysterious, the call is completely synchronous, i.e. no callbacks involved.  The first thread totally sits and waits until it makes a call across the wire to itself and comes back.

Now, I am perplexed why anybody would do it this way.  It seems like calling somebody on the phone that sits in the same room as you do.  Can anybody provide an explanation why the developer before me would come up with the above mentioned model?  Maybe there is a reason and I am missing something.


答:
Reality itself is typed. You can't add lengths to weights. And while you can add feets to meters (both are units of lengths), you should scale at least one of the two. Failing to do so can crash your Mars mission, quite literally.

In a typesafe system, adding two lengths expressed in different units would have been either an error or would have caused an automatic cast.