问:
How do you handle multiple developer configuration in a project?
I've been thinking lately about how I handle config changes between multiple developers on a project.

Specifically here about a Laravel project in PHP, but I guess this applies largely to all frameworks/languages.

In general would you say it's a best practice to not commit your configuration? If so what's your practice for doing this with version control such as git?

Or do you set up each configuration as a different environment config? E.g. In Laravel there are environment configs, so each developer has a directory of just their own different settings, and this is what I'm currently using. Then in the code you can set how Laravel will detect which environment it's on. This works, I'm just wondering what other developers working on projects with other developers do.

答:
There is no simple answer to this one, the trick I used (when writing php)was that if the project was in development mode it would look for an external config file at a certain place outside the environment. Just as a good measure a standard copy of this file was inside the environment so that when getting the source files, you would get a standard dev deployment. Other configs could be placed in the file outside

That way if new configs where added everybody would know about them from the standard file and could alter their files as needed. Also no git issue or risk of bad commits.

I am sure that there are better ways to do this, but it worked back then with 20 devs working on the same projects.

OwnerDisplayName: Andrew

OwnerDisplayName: lcornea


答:
Set up a different sub-domain with its own folder for every developers own dev branch. Run that on your dev server which should replicate your prod environment as much as possible (OS etc).

Have your developers map a network drive (or use FTP, that's not ideal though) and use that to access their code base.

Now create a config sub-folder for each developer environment where you keep the few specific variables. Keep the rest in a general config file used by everyone. Load the specific config file depending on the sub-domain. Default to the production one if no sub-domain is set.

Likely you will need to write a few lines of code to map the right config.

ContentLicense: CC BY-SA 3.0

Title: How do you handle multiple developer configuration in a project?

Body:
I've been thinking lately about how I handle config changes between multiple developers on a project.

Specifically here about a Laravel project in PHP, but I guess this applies largely to all frameworks/languages.

In general would you say it's a best practice to not commit your configuration? If so what's your practice for doing this with version control such as git?

Or do you set up each configuration as a different environment config? E.g. In Laravel there are environment configs, so each developer has a directory of just their own different settings, and this is what I'm currently using. Then in the code you can set how Laravel will detect which environment it's on. This works, I'm just wondering what other developers working on projects with other developers do.

OwnerDisplayName: Patrick


答:
Laravel has magnificent built-in support for this. You can set-up a different environment for every developer's computer. Check out the following page for a detailed explanation: http://laravel.com/docs/configuration#environment-configuration

You can find out what your hostname (in Laravel documentation refered to as "your-machine-name") is by running echo gethostname(); using an empty PHP file.

On our projects we have set-up environments like 'john', 'peter', 'etc'. This assumes that developers do not switch between computers.

OwnerDisplayName: Ronald Hulshof


答: Use dist versions of your config files and add original config file names to .gitignore (e.g. config_prod.ini.dist is under version control and used as template for config_prod.ini which is in .gitignore)</p>

OwnerDisplayName: ishenkoyv


问:
How to Deal With The "Automation is Easy" Mindset?
The title says it all. Some employees in our company believe that automated tests are "easy" and that it "should take a day" to write suites of COM and UI tests. What can be done to counter this? </p>

<p>Note: I'm not asking about how to promote automation. That isn't the problem. Automated testing and processes are being promoted and requested all the time here. The issue is that some individuals don't understand that automation is not "easy" nor is it "fast". </p>

答:
The issue is that some individuals don't understand that automation is not "easy" nor is it "fast".

I don't agree with your premise here.

I am a big proponent of automated testing, no matter if it is unit testing, integration testing or UI testing. There are a lot of great tools available to implement automated tests.

Let's compare automated testing vs. manual testing based on the following example:

In a web application, test the "Change Password" functionality of an existing user using a browser.

Manual testing:

Start the webapplication

Open the browser

Damn, there is an error. Why? Oh, I forgot to start the database!

Okay, shut down the webapplication

Start the database

Start the webapplication

Refresh the browser

Hmm, what was the password of our test-user again?

Taking a look at the database

Oh, users table is empty! I have to create a new user.

Register a new user in the webapplication: Inputting username, password, email-address

Why can't I login with my new user? Oh, I need to click the confirmation link in the email!

Well, I have given the user an email like "test@example.com". Let's go to the database and set the "active" column to "Yes".

Login. This time it works!

Hmm, what did I want to test again...?

Easy? Not really. There are a lot of possible pitfalls in the process.

Fast? No. Manual work takes time.

Now, lets try to write an automated test:

We need to find tools for our programming language to automatically start the database and the web server. Research and implementing takes time.

Database needs to be in a clean state when the test starts. Creating the scripts takes time.

We need to write the test. Since we need a user, we also need to register a new one for our test. Takes time.

Finally, we can write what we want to test: Changing the password of the user. With our Browser-testing-tool, this is done fairly quick compared to the previous tasks.

Easy? No! We needed to research the tools, implement them, fix some bugs in our tests.

Fast? No! It takes even longer than doing a manual test.

But, there is a big difference here: For future tests, you only need to write the test itself, the last bullet point in the list - which was done comparable fast. All research and init-scripts do not need to be done for further tests.

And after you have written the test, you can start it easy. In a few seconds (or maybe minutes, if starting of the database and webapplication takes long) you see if the "Change password" routine works or works not. If there is a bug, fix it, and run the test again - you will immediately see if the bug is fixed. Fast and easy.

Writing automated tests is neither easy nor fast in the first place, but executing them is.

And this is the point where the invested time comes back.


答:
Configuration of an application is a different responsibility than the application itself. Don't mix the two. The concept of an environment or environment variable is a variable that is be referred to with a single name but have different values depending on the environment, hence the name "environment variable".  Your application should never, ever have to check which environment it's actually running in, it should always rely on the variables being set outside of it to have the correct value for a particular environment.  In short, if your code has to actively check which environment it's on, you're doing it wrong.

Store environment configuration outside of the code source, in a separate repository. That way you won't be inclined to take shortcuts and mix the two.


问:
How to alter the code at runtime in an interpreter?
While reading the difference between Compiler and interpreter , I found the following differences fromt he internet.

Advantages of using compiler:

Since compiler converts the program to native code of the target machine (object code), faster performance can be expected.

There is a scope for code optimisation.

Advantages of using interpreter:

Process of execution can be done in a single stage. There is no need of a compilation stage.

Alteration of codes possible during runtime.

Really useful for debugging the codes (because source code execution can be analyzed in an IDE)

Facilitates interactive code development.

In case of interpreter, stated as interpreter "there is a possibility of alter the code" and

In case of compiler "code optimization will be there"

Can anyone explain these 2 points ?

答:
Side note: use compiler/interpreter distinction carefully, as it has its caveats.

Some folks use this distinction to assert that their language is faster because it is compiled/interpreted. The real answer is that a language cannot be faster/slower than another one: is German faster than Japanese?

Many other assertions related to this distinction don't make sense neither.

Many languages use more complicated approaches. For example, C# is compiled to IL, which is then “translated into native code or executed by a virtual machine”.

Some languages are “interpreted, but not really”.

Now to answer your question:

Altering the code:

Since the code is interpreted, it's easier to change it on the fly when executing it. One of such examples is eval(), used in some languages to dynamically inject source code available only as a string.

This being said, this point is both incomplete and confusing. C# is a compiled language (aside DLR), and still, there are ways to inject custom code during the execution of the app.

Code optimization:

Often, compilers are not limited to simply converting code written in source language to target language. They also do optimizations. Imagine you write:

```<code>const int ratio = 14;

function getFactor()
{
    int factor = 2 * ratio;
    string debug = "The factor is " + factor;
    return factor;
}

function computeSomething(int expenses)
{
    int factor;
    float result = (float)(expenses + factor) / 2;
    return result;
}
</code>```

A compiler can use a few basic tricks to optimize this code:

Inline

The constant can be inlined, as well as the first function. This gives:

```<code>function computeSomething(int expenses)
{
    int factor = 2 * 14;
    string debug = "The factor is " + factor;
    float result = (float)(expenses + factor) / 2;
    return result;
}
</code>```

Remove unused code

Here, this will result in an important improvement, since string concatenation is often an expensive operation.

```<code>function computeSomething(int expenses)
{
    int factor = 2 * 14;
    float result = (float)(expenses + factor) / 2;
    return result;
}
</code>```

Optimize mathematical operations

Float multiplication is often cheaper than division.

```<code>function computeSomething(int expenses)
{
    int factor = 2 * 14;
    float result = (float)(expenses + factor) * 0.5;
    return result;
}
</code>```

Remove intermediary variables

```<code>function computeSomething(int expenses)
{
    return (float)(expenses + (2 * 14)) * 0.5;
}
</code>```

Note that many today's interpreters also do those optimizations, which means that when you use an interpreted language, you should write your code for humans, not for computers, letting the interpreter do work.

Body:
Side note: use compiler/interpreter distinction carefully, as it has its caveats.

Some folks use this distinction to assert that their language is faster because it is compiled/interpreted. The real answer is that a language cannot be faster/slower than another one: is German faster than Japanese?

Many other assertions related to this distinction don't make sense neither.

Many languages use more complicated approaches. For example, C# is compiled to IL, which is then “translated into native code or executed by a virtual machine”.

Some languages are “interpreted, but not really”.

Now to answer your question:

Altering the code:

Since the code is interpreted, it's easier to change it on the fly when executing it. One of such examples is eval(), used in some languages to dynamically inject source code available only as a string.

This being said, this point is both incomplete and confusing. C# is a compiled language (aside DLR), and still, there are ways to inject custom code during the execution of the app.

Code optimization:

Often, compilers are not limited to simply converting code written in source language to target language. They also do optimizations. Imagine you write:

const int ratio = 14;

function getFactor()
{
    int factor = 2 * ratio;
    string debug = "The factor is " + factor;
    return factor;
}

function computeSomething(int expenses)
{
    int factor;
    float result = (float)(expenses + factor) / 2;
    return result;
}

A compiler can use a few basic tricks to optimize this code:

Inline

The constant can be inlined, as well as the first function. This gives:

function computeSomething(int expenses)
{
    int factor = 2 * 14;
    string debug = "The factor is " + factor;
    float result = (float)(expenses + factor) / 2;
    return result;
}

Remove unused code

Here, this will result in an important improvement, since string concatenation is often an expensive operation.

function computeSomething(int expenses)
{
    int factor = 2 * 14;
    float result = (float)(expenses + factor) / 2;
    return result;
}

Optimize mathematical operations

Float multiplication is often cheaper than division.

function computeSomething(int expenses)
{
    int factor = 2 * 14;
    float result = (float)(expenses + factor) * 0.5;
    return result;
}

Remove intermediary variables

function computeSomething(int expenses)
{
    return (float)(expenses + (2 * 14)) * 0.5;
}

Note that many today's interpreters also do those optimizations, which means that when you use an interpreted language, you should write your code for humans, not for computers, letting the interpreter do work.


答:
After thinking about it a little I'm changing my answer a bit.

Every language can be compiled. But the problem is how much do you gain from such compilation. Compilation has two benefits: performance gain through optimalizations and early error checking through checking of type signatures. Both of those things depend heavily on indirection in code. Indirection is simply piece of code, that you can tell what will happen only when the code is running. In both cases of optimalization and type checking, any kind of indirection renders any attempts useless. Compiler cannot optimize code if it doesn't know what is going to happen next or what structure will function return and it cannot ensure if types are correct if it doesn't know what parameters the function can accept or what is contained in a variable.

Now, the languages that are usually compiled, like C++, C# and maybe Java, those contain little indirections. In C# for example indirections can only be achieved through virtual methods and delegates. Everything else is simple to predict without having to run the code. If you call Math.Sqrt, then compiler knows 100% it is going to call this one method only. The gain from compiling those languages is huge.

On the other side, languages that are often interpreted usually have tons of indirections. This often stems from dynamic type system. In python for example, reading attribute from class can return absolutely anything at any time. You never know if read attribute will be int, string or function pointer. So even if you compile those, the gain is pretty negligible. The compiler can maybe pre-parse the code and do some minor optimalizations, but it cannot do things like function inlining, which usually have much bigger impact on performance.

Edit, old post:

I don't know what exactly you don't understand, because both those points are pretty self-explanatory.

With compiled languages the structure of code and data is static during compilation and cannot change during runtime. This does not stem from using compiler, but is requirement to be able to compile a language. The static structure then give compiler information necessary to predict how code will execute and optimize it out. Languages like C++, C# or Java fall into this category.

With interpreted langauges, the structure of code and data can change. Usually, when the structure is not static, compilation becomes close to impossible, so interpretation is required. But, because interpreter doesn't require the static structure, language can contain feature and semantics that allows you to change the data and code at runtime. Dynamically typed languages like Python, Ruby or JavaScript fall into this category.

Title: How to alter the code at runtime in an interpreter?

Body:
While reading the difference between Compiler and interpreter , I found the following differences fromt he internet.

Advantages of using compiler:

Since compiler converts the program to native code of the target machine (object code), faster performance can be expected.

There is a scope for code optimisation.

Advantages of using interpreter:

Process of execution can be done in a single stage. There is no need of a compilation stage.

Alteration of codes possible during runtime.

Really useful for debugging the codes (because source code execution can be analyzed in an IDE)

Facilitates interactive code development.

In case of interpreter, stated as interpreter "there is a possibility of alter the code" and

In case of compiler "code optimization will be there"

Can anyone explain these 2 points ?


问:
Which is better: for valid buffer length?
This is a basic question. But, I think understanding this would be helpful to play with buffers in 'C'. Thanks.

Scenario

Just a sample snippet

```<code>char *test_buff = (char *) malloc(512); //allocate 512 bytes in heap memory.
bzero(test_buff, 512); //Reset buffer instead of Junk/garbage.
/* random code here to fill-in the buffer called "test_buff"...*/
...
/* Send data to another host/system [this doesn't matter though]
 * using buffer "test_buff".
 */
 sendto(sock_descriptor, test_buff /*buffer to send data*/, sizeof(*test_buff) ,...,...);  // option-1
/* OR */
 sendto(sock_descriptor, test_buff /*buffer to send data*/, 512 /* actual malloced size*/ ,...,...); // option-2
</code>```

Question

Which one of the two is better [between option-1/option-2] way of using buffer? I hope option-1 makes sense, as it has length/size of valid data bytes of send buffer or "test_buff" in this case.

Thanks for your time and input.

答:
sizeof(*test_buff) is computed at compile time and will evaluate to the size of a single element of test_buff. As test_buff is a buffer of char's, that element size is guaranteed to be 1.
If I have to choose between the two options that you give, then only option 2 has useful behaviour.

On the other hand, if there comes a time that you need to change the size of your buffer, then you have to make that change in 3 (or 4 or 5) places and you have to look out for the places where the number 512 is used with a different meaning. For this reason, my preferred way of writing the code is like this:

```<code>#define BUFFER_SIZE 512
char *test_buff = (char *) malloc(BUFFER_SIZE); //allocate 512 bytes in heap memory.
bzero(test_buff, BUFFER_SIZE); //Reset buffer instead of Junk/garbage.
/* random code here to fill-in the buffer called "test_buff"...*/
...
/* Send data to another host/system [this doesn't matter though]
 * using buffer "test_buff".
 */
 sendto(sock_descriptor, test_buff /*buffer to send data*/, BUFFER_SIZE /* actual malloced size*/ ,...,...); // option-2a
</code>```

If you want to send only the used portion of the buffer, you either need to keep explicitly track of that, or if your buffer only holds a single string, you can use strlen(test_buff)+1 (+1 to ensure the terminating '\0' is also transmitted).

Body:
sizeof(*test_buff) is computed at compile time and will evaluate to the size of a single element of test_buff. As test_buff is a buffer of char's, that element size is guaranteed to be 1.
If I have to choose between the two options that you give, then only option 2 has useful behaviour.

On the other hand, if there comes a time that you need to change the size of your buffer, then you have to make that change in 3 (or 4 or 5) places and you have to look out for the places where the number 512 is used with a different meaning. For this reason, my preferred way of writing the code is like this:

#define BUFFER_SIZE 512
char *test_buff = (char *) malloc(BUFFER_SIZE); //allocate 512 bytes in heap memory.
bzero(test_buff, BUFFER_SIZE); //Reset buffer instead of Junk/garbage.
/* random code here to fill-in the buffer called "test_buff"...*/
...
/* Send data to another host/system [this doesn't matter though]
 * using buffer "test_buff".
 */
 sendto(sock_descriptor, test_buff /*buffer to send data*/, BUFFER_SIZE /* actual malloced size*/ ,...,...); // option-2a

If you want to send only the used portion of the buffer, you either need to keep explicitly track of that, or if your buffer only holds a single string, you can use strlen(test_buff)+1 (+1 to ensure the terminating '\0' is also transmitted).


答:
As a practical example fire up EXCEL and create a macro.

Start the debugger and step through the code.

You can change the contents of any variable.
Execute arbitary expressions.
Change the source code (to a limited extent -- you cannot change code inside a loop or other control structure if the current statement is inside the structure etc. etc.)


答:
The statement is highly arbitrary because there are also code injection techniques which alter compiled programs. With interpreted languages it's just easier to do.

A lot of virusses work this way.


问:
Is doing two things on one line always a bad practice?
```<code>int *itPins = pins;
for(int i = 0; i < count; i++)
{
    ConfigureGpifPinAsGpioOutput(itPins);
    itPins++;
}
</code>```

I have the impression that doing two things in one line is bad practice. This is why I am advancing the iterator outside of the only statement, using it. However, I feel blindly following a rule.

Is this code less error-prone than if the ++ is inside the function invocation?

Why?

Does this rule apply to such ultra-simple cases or is it meaningful only for more complex program structures?

答:
It is not always bad practice to do multiple things on one line, but it does have a higher risk of making the program appear more complicated that it needs to be.

If I were pedantic, I could argue that in the line for(int i = 0; i < count; i++), you are also doing multiple things, but very few people would mark that line as a violation of the rule.

On the other hand, if you had a line like ConfigureGpifPinAsGpioOutput(itPins++);, then the increment in the argument does increase the complexity as I would have to look twice to be sure that the code is correct and I would probably flag it as needlessly complicated.

Another way to write the loop is like this:

```<code>for(int i = 0; i < count; i++)
{
    ConfigureGpifPinAsGpioOutput(pins[i]);
}
</code>```

where you rid yourself completely of the issue.

Body:
It is not always bad practice to do multiple things on one line, but it does have a higher risk of making the program appear more complicated that it needs to be.

If I were pedantic, I could argue that in the line for(int i = 0; i < count; i++), you are also doing multiple things, but very few people would mark that line as a violation of the rule.

On the other hand, if you had a line like ConfigureGpifPinAsGpioOutput(itPins++);, then the increment in the argument does increase the complexity as I would have to look twice to be sure that the code is correct and I would probably flag it as needlessly complicated.

Another way to write the loop is like this:

for(int i = 0; i < count; i++)
{
    ConfigureGpifPinAsGpioOutput(pins[i]);
}

where you rid yourself completely of the issue.


问:
Is double password protection safe for admin authentication?
I was looking at some of my past codes, and I viewed one of my admin boards I've made. I had it setup so that the admin has to define/enter 2 passwords for his account.
Is this really double-safe or was I deluded?
It seems that it's reaaally hard to force-match username and 2 passwords from the client side. I've got it limited to 100 tries, dogh...</p>

答:
Well, it is not uncommon.

Forgetting the fact that you are talking about you admin account for a second, it is typical for banks to require multiple passwords for users, often a password and some kind of pin number.

They often augment this with a number of security questions as well that you will be prompted to answer 1 of.

That being said for forum software that kind of thing might be overkill for a normal user.


答:
What are the major risks related to passwords?

The password is not secure enough. This makes it easy, for example, to brute force a password, or simply guess it.

Wrong hashing techniques are used or there is no hashing at all. It makes it easy for anyone who have (think programmers, DBAs, sysadmins) or can get (think SQL Injection) access to the database to do whatever he wants with the passwords of every user.

Man in the middle attacks, such as in the scenarios where there is no SSL and the machine connects through an unsafe network, such as wifi, make it easy to get the plain text passwords.

Finally, password theft can involve some techniques such as social engineering, as well as be as basic as spotting a password written on a post-it.

Lack of proper password reset policy. Working as a freelancer, I often receive root (yes, root) access passwords from my customers. Sadly, years later, those passwords would probably still work, despite the fact that there were dozen of other developers who have received the same password.

None are solved by using two passwords instead of one. Instead, forcing to have two passwords instead of one may have some negative effects:

It encourages the users to use weak passwords or to have two similar passwords,

Otherwise, chances are the passwords will be found on a post-it.

Title: Is double password protection safe for admin authentication?

Body:
I was looking at some of my past codes, and I viewed one of my admin boards I've made. I had it setup so that the admin has to define/enter 2 passwords for his account.
Is this really double-safe or was I deluded?
It seems that it's reaaally hard to force-match username and 2 passwords from the client side. I've got it limited to 100 tries, dogh...</p>


问:
How do you unit test private methods?
I am working on a java project. I am new to  unit testing. What is the best way to unit test private methods in java classes?</p>

答:
I usually make such methods protected. Let's say your class is in:

```<code>src/main/java/you/Class.java
</code>```

You can create a test class as:

```<code>src/test/java/you/TestClass.java
</code>```

Now you have access to protected methods and can unit test them (JUnit or TestNG doesnt really matter), yet you keep these methods from callers you did not want.

Note that this expects a maven-style source tree.

Title: How do you unit test private methods?

Body:
I usually make such methods protected. Let's say your class is in:

src/main/java/you/Class.java

You can create a test class as:

src/test/java/you/TestClass.java

Now you have access to protected methods and can unit test them (JUnit or TestNG doesnt really matter), yet you keep these methods from callers you did not want.

Note that this expects a maven-style source tree.


问:
fill containers with blocks of different sizes to have containers of "similar" sizes
I have a sorted list of blocks L that I want to put in 4 containers C, the containers have a size limit S. I want to have similar containers height, by similar I mean the size variance should be as small as possible.

eg:

```<code>S = 4
L = [1, 1, 3, 1, 2, 1, 2]
</code>```

should result in:

```<code>C = [[1, 1], [3], [1, 2], [1, 2]]
</code>```

note: it is always possible to put every blocks inside the 4 containers and I can't change blocks order

is there a way to do this efficiently? I couldn't find algorithm names about this but I didn't really know how to search for that

答:
This is a generalization of the knapsack problem (select elements of a set so that their sum is exactly n). You want to select four subsets of elements so that the sum of each subset is n (where n is 1/4 of the sum of all blocks), so it's a harder variant of the standard problem.

Unfortunately, the knapsack problem is known to be NP-complete, i.e. practically unsolvable for large n. However, there is a rich literature about approximate solutions, which is what you want.


答:
It depends. Most times I would to avoid such things, but in a simple case like this

```<code> ConfigureGpifPinAsGpioOutput(itPins++);
</code>```

is very idiomatic for most programmers with some experience in the C families of programming languages. With some experience in C code reading, it is IMHO as easy to grasp than the two lines of code in your example.

So I would not be too picky about this special case.

Title: Is doing two things on one line always a bad practice?

Body:
```<code>int *itPins = pins;
for(int i = 0; i < count; i++)
{
    ConfigureGpifPinAsGpioOutput(itPins);
    itPins++;
}
</code>```

I have the impression that doing two things in one line is bad practice. This is why I am advancing the iterator outside of the only statement, using it. However, I feel blindly following a rule.

Is this code less error-prone than if the ++ is inside the function invocation?

Why?

Does this rule apply to such ultra-simple cases or is it meaningful only for more complex program structures?

Body:
It depends. Most times I would to avoid such things, but in a simple case like this

 ConfigureGpifPinAsGpioOutput(itPins++);

is very idiomatic for most programmers with some experience in the C families of programming languages. With some experience in C code reading, it is IMHO as easy to grasp than the two lines of code in your example.

So I would not be too picky about this special case.


问:
What is the best implementation strategy for this html + javascript widget?
The Problem

I came up with this select-box variant in answer to a question on UX.SE:
https://ux.stackexchange.com/a/48951/16187

However, working out how to implement it has been driving me mad ever since I wrote that post.

What I've tried

Initially I figured it would be easy enough to do.

The strategy I would have taken would be to  model all the options in a javascript object, with their own name and selected properties, and then write a view function to display them as an unordered list or something.

I could then create the button as another javascript object, modelling it's own state, and have a view function spiting it out a representation of it into the widget. Maybe throw in some event code for transitioning between button states and I'm done.

Where I'm Stuck

The more I thought about that approach, the more I realised that having all of the state sitting in javascript objects was a terrible idea. I figured that I should probably store the state in the dom, and mutate it with javascript.

However I have no clear idea what that sort of architecture would look like, or how I should go about coding it.

Can you help me work out what a better high level implementation strategy for this sort of widget looks like?

答:
Why would you think that having the state in js would be bad? I think it's as good as in the DOM, if not better.

Anyway, you most likely already have the state in the DOM, since for display purposes you will add/remove classes from the list elements. In such cases I often just let jQuery find all elements with a certain class within the list. Get the amount of those elements and you already know how many were selected.

Put all of this functionality (count objects with class "selected_list_thing", update button state) in a function and add an mouse click handler to your list elements. There switch the class (eg add or remove "selected_list_thing" is blue) and then call the function you defined to update the button.


问:
How I could update a DB table using a POCO class bound in a DataGridView
I try to explain better using an example:

I create the class Product with attibutes like that:

```<code>private string _ProductCode = "";
[DisplayName("ProductCode")]
public String ProductCode
{
   get { return _ProductCode; }
   set { _ProductCode = value; OnPropertyChanged("ProductCode"); }
}
</code>```

the class Product implements the interfaces: INotifyPropertyChanged, IEditableObject

I create the class ProductList that has the following property:

```<code>public List<Product> Products { get; set; }
</code>```

I bind the class Product in a DataGridView control using the DataSource property

```<code>MyGridView.DataSource = ProductList.Products;
</code>```

I have the table ProductListTable on db SqlServer that has more column than the class Product

When I push the button "Save" on my form I'd would like to save the ProductList on ProductListTable

I'm really confusing about the way to follow ... I have to use Link 2 Sql, Entity Framework? I'm searching for something simple for a small project, but using POCO classes for rapresenting the data layer.

Now how I should proceed for linking class to DB table?

答:
It sounds like Entity Framework is what you want, specifically using the code first techniques.

With code first you're able to create a 'DbContext' class that can hold DbSet<> objects, similar to your List. These DbSets are linked to the database by entity framework and they will take care of the database interaction as long as you call SaveChanges() on them. This method will allow you to keep you POCO objects.

This book: Programming Entity Framework: Code First   is great. It'll quickly bring you up to speed (only a few hundred pages).

I'll also add this as quite a full tutorial without buying a book: Code-First Development with Entity Framework 4

OwnerDisplayName: tagetek


问:
Iterative process modelling on a kanban board
Kanban does assume a more-or-less linear flow.  Most of the time this models software development quiet well: we crate a user story, we code it, we test it, and we deploy it.  However, some internal stages are iterative in nature and thus not so obvious.  Take this (simplistic) board:

```<code>||   Planning   ||  Development ||     Test     ||     Deploy   || Done ||
|| doing | done || doing | done || doing | done || doing | done ||      ||
||       |      ||       |      ||       |      ||       |      ||      ||
</code>```

Let's focus on the Planning column. If this is where user stories are developed, clearly some sort of iteration is needed: write story, refine/conversation, improve story, customer feedback, etc ... These stages are not modelled on the board at all.

Let's focus on the Test column. Once a story (or task/whatnot) is being tested: some tests will pass, some will fail.  Thus the story needs more work.  Should it go back into the develop column?  Stay in a special test/development?  Should there be Test/Fail and Tests/Pass columns?  Over complicating the board is not an optimal solution.

Should you model iterative processes using Kanban?

If so, how? If not, why not?

答:
From what you're saying in both situations the work item is still going to be in the "Doing" column, there seem to be different stages of "Doing".

Perhaps if you want to track down to this fine level of detail you could have boards for each stage. So a planning board may look like:

```<code>|| Write Story  ||     Refine   || Customer Feedback ||
|| Doing | Done || Doing | Done || Doing | Done      ||
</code>```

Then move your work items back and forth in these boards while leaving the high level ones in the doing column until the lower boards are complete?

Body:
From what you're saying in both situations the work item is still going to be in the "Doing" column, there seem to be different stages of "Doing".

Perhaps if you want to track down to this fine level of detail you could have boards for each stage. So a planning board may look like:

|| Write Story  ||     Refine   || Customer Feedback ||
|| Doing | Done || Doing | Done || Doing | Done      ||

Then move your work items back and forth in these boards while leaving the high level ones in the doing column until the lower boards are complete?


答:
There are two ways to deal with iterative processes on a KanBan board:

The progression over the columns on the board from left to right indicates the ideal flow of a task. If a task can't follow the ideal flow (for example, if the verification test failed), then the task's marker can be moved back to a column on the left, indicating that that stage of the process was not really completed.

For small-loop iterations, you can just make the granularity of the column such that the entire loop stays within that column. For example, the preparation of a story for implementation could be a single step in the process, regardless of how often you go back-and-forth with the customer.

With that in mind, I think your 'simplistic' board is already too complex. I would change it to

```<code>||          TO DO              ||                            DOING                   || DONE ||
||   Planning | Ready for      ||  Development | Ready for | Test | Tested | Deploy  ||      ||
||            | Implementation ||              | Test      |      |        |         ||      ||
</code>```

Here, the 'Ready for XXX' and 'Tested' columns are buffers so that the different stages of development can work in relative isolation from each other.
On this board, a task should not be moved to 'Ready for XXX' or 'Tested' until it can be picked up from that buffer by the next team. If, for example, a test fails, then that task should be moved back to 'Ready for Implementation' so it can be picked up again by the development team.

Title: Iterative process modelling on a kanban board

Body:
Kanban does assume a more-or-less linear flow.  Most of the time this models software development quiet well: we crate a user story, we code it, we test it, and we deploy it.  However, some internal stages are iterative in nature and thus not so obvious.  Take this (simplistic) board:

```<code>||   Planning   ||  Development ||     Test     ||     Deploy   || Done ||
|| doing | done || doing | done || doing | done || doing | done ||      ||
||       |      ||       |      ||       |      ||       |      ||      ||
</code>```

Let's focus on the Planning column. If this is where user stories are developed, clearly some sort of iteration is needed: write story, refine/conversation, improve story, customer feedback, etc ... These stages are not modelled on the board at all.

Let's focus on the Test column. Once a story (or task/whatnot) is being tested: some tests will pass, some will fail.  Thus the story needs more work.  Should it go back into the develop column?  Stay in a special test/development?  Should there be Test/Fail and Tests/Pass columns?  Over complicating the board is not an optimal solution.

Should you model iterative processes using Kanban?

If so, how? If not, why not?

Body:
There are two ways to deal with iterative processes on a KanBan board:

The progression over the columns on the board from left to right indicates the ideal flow of a task. If a task can't follow the ideal flow (for example, if the verification test failed), then the task's marker can be moved back to a column on the left, indicating that that stage of the process was not really completed.

For small-loop iterations, you can just make the granularity of the column such that the entire loop stays within that column. For example, the preparation of a story for implementation could be a single step in the process, regardless of how often you go back-and-forth with the customer.

With that in mind, I think your 'simplistic' board is already too complex. I would change it to

||          TO DO              ||                            DOING                   || DONE ||
||   Planning | Ready for      ||  Development | Ready for | Test | Tested | Deploy  ||      ||
||            | Implementation ||              | Test      |      |        |         ||      ||

Here, the 'Ready for XXX' and 'Tested' columns are buffers so that the different stages of development can work in relative isolation from each other.
On this board, a task should not be moved to 'Ready for XXX' or 'Tested' until it can be picked up from that buffer by the next team. If, for example, a test fails, then that task should be moved back to 'Ready for Implementation' so it can be picked up again by the development team.


问:
Are all scripting languages dynamically typed?
Are all scripting languages dynamically typed?

I am using TCL. It is a scripting language and it does not enforce or allow type delaration of variables. It is instead a dynamically-typed language with ducktyping. The type of a variable is assumed by the interpreter according to the value assigned to it.

I would really like to know is there are scripting languages that are strictly/strongly typed.

答:
No. Whether a language is statically or dynamically typed, or weakly or strongly typed, has nothing to do with interpretation/compilation, which is an orthogonal implementation detail.

Perhaps the best example to use here would be Hugs, The Haskell interpreter. For those unaware, Haskell is a very strongly typed language with a very powerful type system, and is somewhat of a poster child for strong type systems with solid mathematical backing.

Consider also a language such as Scala, which has a type system with similar power and is very difficult to place between compiled and interpreted. It "compiles" down to JVM Bytecode, which is then interpreted/compiled/JITted depending on various implementation details of the JVM it happens to be running on.


问:
How to best protect from 0 passed to std::string parameters?
I have just realized something disturbing.  Every time I have written a method that accepts a std::string as a paramater, I have opened up myself to undefined behaviour.

For example, this...

```<code>void myMethod(const std::string& s) {
    /* Do something with s. */
}
</code>```

...can be called like this...

```<code>char* s = 0;
myMethod(s);
</code>```

...and there's nothing I can do to prevent it (that I am aware of).

So my question is: How does someone defend themself from this?

The only approach that comes to mind is to always write two versions of any method that accepts an std::string as a parameter, like this:

```<code>void myMethod(const std::string& s) {
    /* Do something. */
}

void myMethod(char* s) {
    if (s == 0) {
        throw std::exception("Null passed.");
    } else {
        myMethod(string(s));
    }
}
</code>```

Is this a common and/or acceptable solution?

EDIT:  Some have pointed out that I should accept const std::string& s instead of std::string s as a parameter.  I agree.  I modified the post.  I don't think that changes the answer though.

答:
How about you provide on overload the takes an int parameter?

```<code>public:
    void myMethod(const std::string& s)
    {
        /* Do something with s. */
    }

private:
    void myMethod(int);
</code>```

You don't even have to define the overload. Trying to call myMethod(0) will trigger a linker error.

Body:
How about you provide on overload the takes an int parameter?

public:
    void myMethod(const std::string& s)
    {
        /* Do something with s. */
    }

private:
    void myMethod(int);

You don't even have to define the overload. Trying to call myMethod(0) will trigger a linker error.


问:
How to identify unstable floating point computations?
In numerics, it is very important to be able to identify unstable
schemes and to improve their stability.  How to identify unstable
floating point computations?

I am working on a very complex simulation where many numerical schemes work together and I am looking for a methodic to identify its weak parts.  I am working on a physical model involving differential equations.  A bird's eye view of the overall process is:

(Preliminary step) Gather physical observations P.

Determine initial parameters of the simulation. This uses an optimisation algorithm, where we walk in a parameter space and look for parameters C such that some error function E(F(C), P) is minimised, where F is some derived quantity of the parameters.

Plug C in the simulation engine. This is an Euler scheme of the EDP, so that at each time step, we compute the terms driving the dynamic (each of them is a complex function, potentially subject to instability) and feed the Euler scheme with these dynamic terms to compute the next state. This goes on for thousands time points.

At the end of the simulation, we compute some function Proof(S) of the final state S and compare to some quantities Require(P) deduced from the observed quantities. This is not a formal proof of the result, more a plausibility check.

Also, I see a tower of complex operations (computation of dynamic terms, within the Euler scheme, within the Proof). And would like to recognise “bad parts” and fix them.

I speculate that using a software implementation of floating point
numbers with reduced precision would magnify the unstability of
numerical schemes, thus easing the comparison between different
implementations.  Is this a common technique to investigate this
question?  Is it possible to use a virtual machine, as Bochs, to
achieve this without altering the program?

To deal appropriately with the stability question, it is sometimes
acceptable to target the typical input of the numerical procedure, so
that it can be tuned to do well on that input and maybe less well on
other valid, yet unlikely, input.  Given a sample of typical inputs,
it is possible to snoop some intermediate results and prepare a
statistical profile for them.  Again, is this a common technique to
study stability issues?  Is a virtual machine useful for this?

答:
The study of stability of floating point computation is part of numerical analysis and if you really want a sound result, you really want someone knowledgeable in that domain to do the analysis of the algorithms used.

There are some things which can help to experimentally identify unstable algorithms.  Running with rounding set to different modes (up/down/random) or with different precision and checking that the result don't vary too much.  Answering is this too much? isn't simple at all and even when the answer is no that doesn't mean the algorithm is stable, just that it wasn't detected unstable on the data set you used.

Interval arithmetic has been proposed in comments.  When I looked at it, even the most rabid proponent of interval arithmetic admitted that it worked well with algorithms designed for interval arithmetic but that switching to it without analyzing the algorithm and ensuring that it hadn't patterns known not to work well wouldn't be useful (opponents seemed of the opinion that the pre-conditions for interval arithmetic to be useful where too restrictive to be of practical interest)


问:
Javascript Use of Window Object through alternate means
Disregard your usual paradigms for a mere moment here....

I've seen a few sites who have used the following logic:

```<code>a=[];
b=a["sort"];
c=(b)();
c["alert"](1); // intended output: Alert displaying "1"...
</code>```

supposedly to access (I'm guessing here) the primitive of the sort function to gain access to the window object, and then using that to access "alert"...

I've tried executing this ages ago, and it worked a charm... But some twelve months or so later, I tried to run it again... and it's failed to execute, and given the following error:

```<code>TypeError: can't convert undefined to object
</code>```

My question is, why wouldn't this logic work? It seems quite sound... Or is it?

And yes, I know that I could just do alert(1) and be done with it, but I'm trying to get an understanding of alternate ways of coding... I work in IT security and I know certain people have ways of using XSS to find alternative ways to perform generic functions to compromise systems...

答:
Let's look at the ECMAScript 5 specification for Array.prototype.sort:

Let obj be the result of calling ToObject passing the this value as the argument.

And when we examine ToObject, we see a familiar entry in the table:

```<code>Argument Type    Result
=============    ======
Undefined        Throw a TypeError exception.
...
Object           The result is the input argument (no conversion).
</code>```

This suggests that Chrome's invocation of b() has a this value that is not undefined, but other browsers do pass a this of undefined, resulting in a TypeError.

First we must examine how this is set when we perform a function call (which EMCAScript refers to as a CallExpression):

If Type(ref) is Reference, then

If IsPropertyReference(ref) is true, then

Let thisValue be GetBase(ref).

Else, the base of ref is an Environment Record

Let thisValue be the result of calling the ImplicitThisValue concrete method of GetBase(ref).

Else, Type(ref) is not Reference.

Let thisValue be undefined.

Because ImplicitThisValue of a function's Environment Record always returns undefined, the only case in which this is not undefined is during property access, e.g., foo.bar(). Therefore, we can conclude that the execution of a "bare" function invocation like b() will always use a thisValue of undefined.

But in the Chrome console, this isn't undefined for b(). To understand how this could happen (and how it could differ between browsers), we need to examine strict mode behavior in Section 10.4.3, Entering Function Code:

The following steps are performed when control enters the execution context for function code contained in function object F, a caller provided thisArg, and a caller provided argumentsList:

If the function code is strict code, set the ThisBinding to thisArg.

Else if thisArg is null or undefined, set the ThisBinding to the global object.

where "ThisBinding" is "The value associated with the this keyword within ECMAScript code associated with this execution context."

Thus, non-strict code replaces an null or undefined thisArg with the global object, window. Strict mode, on the other hand, does not convert null or undefined to the global object when used for this. (sort returns the sorted this object, so c = b() simply stores the returned the global object into c.)

Therefore, we can conclude that Firefox's implementation of the Array.prototype.sort function runs in strict mode, while Chrome's implementation does not.

You can see further evidence of this by how sort handles non-object this values. If oyu run typeof []["sort"].apply(5) in various browsers, Chrome will output "number", while Firefox outputs "object", demonstrating the object autoboxing that occurs only in non-strict mode. Firefox's strict sort function does not box the 5 and returns the raw primitive.

Body:
Let's look at the ECMAScript 5 specification for Array.prototype.sort:

Let obj be the result of calling ToObject passing the this value as the argument.

And when we examine ToObject, we see a familiar entry in the table:

Argument Type    Result
=============    ======
Undefined        Throw a TypeError exception.
...
Object           The result is the input argument (no conversion).

This suggests that Chrome's invocation of b() has a this value that is not undefined, but other browsers do pass a this of undefined, resulting in a TypeError.

First we must examine how this is set when we perform a function call (which EMCAScript refers to as a CallExpression):

If Type(ref) is Reference, then

If IsPropertyReference(ref) is true, then

Let thisValue be GetBase(ref).

Else, the base of ref is an Environment Record

Let thisValue be the result of calling the ImplicitThisValue concrete method of GetBase(ref).

Else, Type(ref) is not Reference.

Let thisValue be undefined.

Because ImplicitThisValue of a function's Environment Record always returns undefined, the only case in which this is not undefined is during property access, e.g., foo.bar(). Therefore, we can conclude that the execution of a "bare" function invocation like b() will always use a thisValue of undefined.

But in the Chrome console, this isn't undefined for b(). To understand how this could happen (and how it could differ between browsers), we need to examine strict mode behavior in Section 10.4.3, Entering Function Code:

The following steps are performed when control enters the execution context for function code contained in function object F, a caller provided thisArg, and a caller provided argumentsList:

If the function code is strict code, set the ThisBinding to thisArg.

Else if thisArg is null or undefined, set the ThisBinding to the global object.

where "ThisBinding" is "The value associated with the this keyword within ECMAScript code associated with this execution context."

Thus, non-strict code replaces an null or undefined thisArg with the global object, window. Strict mode, on the other hand, does not convert null or undefined to the global object when used for this. (sort returns the sorted this object, so c = b() simply stores the returned the global object into c.)

Therefore, we can conclude that Firefox's implementation of the Array.prototype.sort function runs in strict mode, while Chrome's implementation does not.

You can see further evidence of this by how sort handles non-object this values. If oyu run typeof []["sort"].apply(5) in various browsers, Chrome will output "number", while Firefox outputs "object", demonstrating the object autoboxing that occurs only in non-strict mode. Firefox's strict sort function does not box the 5 and returns the raw primitive.


问:
In DDD are Value Object always at the leaves of a relationship tree?
If I have a photo album where each Photo can be commented on, Should the Comments be Entities or Value Object in these cases:

Comments can be added only anonymously (i.e. Comments have no relation to anything except the photo).

Comments can be added by registered users (i.e. there will be a relation between the Comment and User entity).

Comments can have comments (i.e. 1 user replying to another).

I think modeling a Comment as Value Object in the first case sounds reasonable as a Comment won't have any relationships with any other entities.

However, what about the 2nd and 3rd case? Does the need to identify which user Posted which Comment changes it from a Value Object into Entity?

答:
Here are two excellent posts on how to identify what is a value object and what is an entity. I know that when I am stuck on which to use, I always give these articles a read.

To help you chose which one to use, think about the lifecycle of the Comments object.

Value Object

"An object that represents a descriptive aspect of the domain with no conceptual identity is called a VALUE OBJECT. VALUE OBJECTS are instantiated to represent elements of the design that we care about only for what they are, not who or which they are." [Evans 2003]

Entity

"An ENTITY is anything that has continuity through a life cycle and distinctions independent of attributes that are important to the application’s user." [Evans 2003]

After reading the articles, in particular the Value Objects one, and the story about identifying a child, ask yourself:

Can a comment exist without a Photo??

If all entities are unique, what would make a unique Comment?

Value Objects are immutable, if I change a comment, how does that impact a Value Object Comment?


答:
Well, in my nomenclature -- which isn't universal, but whose is? -- TCL is not a dynamically typed language, it is an untyped one like assembly languages, BCPL, BLISS.

Statically typed languages have types deduced by a static analysis.

Dynamically typed languages have a type attached to values.

Untyped one have only one kind of values (words for BCPL and BLISS, strings for TCL) and the type is deduced from the operation which is applied more or less blindly on the values.

Automatic conversion may blur the difference between dynamically typed languages and untyped one.  Evolution may also give an untyped language some dynamically typed flavor (that's perhaps the case for TCL -- I've not used for too long --, it's also the case for some shells which started as untyped and then acquired values which are of a different kind -- arrays for instance) making their behavior inconsistent and difficult to explain.

Duck typing is another axis.  It's a way to check type conformance.  It's a variant of structural typing where only the used characteristics are checked.  It can be used in statically typed languages (templates in C++ is duck typing); in dynamic languages the check may be delayed to use time.

Body:
Are all scripting languages dynamically typed?

I am using TCL. It is a scripting language and it does not enforce or allow type delaration of variables. It is instead a dynamically-typed language with ducktyping. The type of a variable is assumed by the interpreter according to the value assigned to it.

I would really like to know is there are scripting languages that are strictly/strongly typed.


答:
I think the purpose of a Kanban board is to very quickly display where is everything at this moment along with an easy way to move things from on area to another.

There are some details especially about the history or the path taken by a particular story. You may want a separate feature to your board that is more of a log a story.

Here are two stories that let's say they both started step #2 (Very first Development) at the same time.
Example: Story "A"

Planning

Development

Testing

Development

Testing

Development

Example: Story "B"

Planning

Development

Both Story "A" and "B" are in Development, but there is clearly something different going on with both of these stories. Is story A being sent to testing too quickly or is it much more complicated? When another person gets involved with these delayed stories, the kanban board is only going to provide enough information to indicate they're still in development, but not show the details of the iteration between testing and development.

If this is a problem, you may need some other type of report. Unfortunately with a paper system, you'd need to track the date/time when stories are moved from one place to another. It may be more practical to just ask those involved,"How's it going?"


问:
Graph traversal and filtering in indoor navigation and path finding
Which of the following options take less processing time/is less expensive in a graph traversal algorithm for a (indoor)navigation system?

a) To produce all possible paths between start and destination points (nodes on the graph) then applying a filtering mechanism to match the navigation user's capabilities and preferences (such as finding all the paths on the graph and then exclude stairways for wheelchair users), or

b) As soon as the user's profile is available to the system, filter the graph and exclude the paths which are non-traversable for this user and then run a shortest path algorithm?

答:
It seems to me  to me that your user-matching removes a few nodes from the graph and changes the cost of others. That is, preferences make some paths cheaper (better), and actually modifies the graph itself (removal of stair nodes for wheelchair users).

That means that you need to run shortest-path on the modified graph to get the result at all - only b) will work. The precomputed version a) does not take into account the preference-changed path costs and removed nodes and therefore will give you wrong answers.


答:
Should you model iterative processes using Kanban?

If so, how? If not, why not?

What do you get out of modeling/tracking that?
Unless development gets started on stories which aren't ready yet (never seen by customers, ambiguous, etc), you'd be solving a problem you don't have.

Practically: you don't need to track every single phase of coding (wrote tests, wrote API, wrote code, wrote more tests, local validation, pushed, CI in progress, CI results verified) because you expect whoever is in charge of the story to take care of that properly. Why not have the same kind of expectation on the PO?


问:
Are Language Comparisons Meaningful?
Dr Bjarne Stroustrup in his book D&E says

Several reviewers asked me to compare C++ to other languages. This I have decided against doing. Thereby, I have reaffirmed a long-standing and strongly held view: "Language comparisons are rarely meaningful and even less often fair" . A good comparison of major programming languages requires more effort than most people are willing to spend, experience in a wide range of application areas, a rigid maintenance of a detached and impartial point of view, and a sense of fairness. I do not have the time, and as the designer of C++, my impartiality would never be fully credible.

-- The Design and Evolution of C++(Bjarne Stroustrup)

Do you people agree with his this statement "Language comparisons are rarely meaningful and even less often fair"?

Personally I think that comparing a language X with Y makes sense because it gives many more reasons to love/despise X/Y :-P

What do you people think?

答:
I think Stroustrup is entirely correct. Adequately comparing two languages on their technical merits requires enough familiarity with both to write idiomatic code and use the same design patterns normally used by programmers who are very productive in both languages. Someone who doesn't have that level of knowledge of both languages may see things that aren't explicitly provided for by the language that he's not as familiar with, and assume there would be problems as a result.

For example, someone who doesn't use Python on a regular basis may assume that Python users regularly have trouble because of indentation. Or someone not familiar with Common Lisp may look at the lack of polished libraries, but not know that the FFI is powerful enough to write wrappers for C libraries with nominal effort. Someone not familiar with Ruby may see the lack of static typing and assume type errors would be a major problem. Finally, someone not familiar with Haskell may see the lack of assignment, and assume it can't handle state.

Now all of this assumes that languages actually are compared only on their technical merits.

ContentLicense: CC BY-SA 2.5

Title: Are Language Comparisons Meaningful?


答:
Instead of filtering the graph to remove paths why don't you add weights to the nodes or edges and then implement your shortest path algorithm so that it is simply unable to traverse nodes above some configurable weight threshold.

Depending on how complicated your constraints are, you could also modify the algorithm to check sets of constraints as it traverses the graph. Basically, what I'm trying to say is that you can filter to ignore unsuitable nodes as part of your shortest path algorithm, you really don't need to do this in two steps.

Title: Graph traversal and filtering in indoor navigation and path finding

Body:
Which of the following options take less processing time/is less expensive in a graph traversal algorithm for a (indoor)navigation system?

a) To produce all possible paths between start and destination points (nodes on the graph) then applying a filtering mechanism to match the navigation user's capabilities and preferences (such as finding all the paths on the graph and then exclude stairways for wheelchair users), or

b) As soon as the user's profile is available to the system, filter the graph and exclude the paths which are non-traversable for this user and then run a shortest path algorithm?


问:
DTOs matter on ASP.NET MVC JSON Action?
I recently came across an internal project where they were using DTOs throughout the solution, and when I sat down to implement a new MVC controller with a JSON REST endpoint I decided to use an anonymous object convention instead of declaring a discrete DTO class solely for the purpose of this API endpoint.

```<code>public JsonResult GetSomeSuch()
{
    // .. do work

    return Json(new {
        PropA = domainObj1.PropA,
        PropB = domainObj2.PropB
    });
}
</code>```

.. as opposed to the team convention, ..

```<code>public JsonResult GetSomeSuch()
{
    // do work ..

    return Json(new SomeSuchDTO2 {
        PropA = domainObj1.PropA,
        PropB = domainObj2.PropB
    }
}

// in some other file off in some other project and folder
public class SomeSuchDTO2 {
    public int PropA { get; set; }
    public string PropB { get; set; }
}
</code>```

Setting aside that I should have used a DTO here purely because it was already a team convention, team conventions trump best practices until team conventions change by team leadership, of which I was not yet a part. I'm not concerned about the social / team dynamic and consistency aspects so much as the technical value of details in specifically that point at which an action in C# becomes serialized to Javascript, as I have since moved on from that company.

The team convention was to return only DTOs and the DTOs were managed in a completely different project (among about 50 projects in the solution), I felt that in this case perpetuating that pattern was excessive and difficult to maintain. Further, my belief is that DRY must correlate with YAGNI; only refactor when you're repeating the same thing twice, as such you would only refactor to use a DTO if that same return object structure reflecting the same context is in fact needed elsewhere, not just possibly but actually, but since this is being serialized to Javascript where it will be consumed anyway there is really no point anyway.

I suppose a case can be made for DTOs as specification documentation, but when it becomes more of a maintenance chore to declare them and to look them up in their special folder in a project I would argue that there are more appropriate places and means to document the interface; indeed the anonymous object declaration seems to be documentation enough. I should also add that in our case everything was internal, this was a standalone project, so it's not like this was an externally exposed API.

I was wondering if there are other angles to this that I didn't consider as to why I should use DTOs, something reasonably convincing?

答:
Personally, I've transitioned from anonymous JsonResults to DTOs.  I loved the anonymous objects - they're quick, easy, don't really hurt anything (transferring Json from server to UI, I could care less about type checking ... this is just a serialization vehicle).  In an agile environment ... it's fine by me.

Over time, though, I noticed that when returning data, especially to a UI, it more often that not needed a little standardized formatting/conversion/processing.  I started seeing duplicate code to do date/time formatting, unit-of-measure conversions, etc.  With DTO's, I can enforce some standardization around these things via core interfaces and some extension methods.  So we are talking about reuse, but not of the DTO itself.

Sure - there are cases where it doesn't make sense.  If you are sending down a tuple of some integers, who cares.  However, I think it's easier for a team to just stick to the rule "create DTOs for Json actions" and it'll provide benefit in the long run.  The cost is cheap.

Title: DTOs matter on ASP.NET MVC JSON Action?


答:
This is a matter of style, and there is no definitive answer.  There are some C programmers who are more dogmatic and will hate code with multiple statements per line. Almost no one will object to code where each statement is on a separate line. If there are people on your team who hate seeing multiple statements on a line, then just don't do it.  Argue about something more important.  If the team is just you, then suit yourself.

In your specific example, the best answer is to eliminate i and directly iterate through the pin numbers:

```<code>for(int* p = pins; p < pins + count; ++p) {
    ConfigureGpifPinAsGpioOutput(p);
}
</code>```

BTW, there are lots of C programmers who will really hate your quasi-Hungarian naming conventions.

Body:
This is a matter of style, and there is no definitive answer.  There are some C programmers who are more dogmatic and will hate code with multiple statements per line. Almost no one will object to code where each statement is on a separate line. If there are people on your team who hate seeing multiple statements on a line, then just don't do it.  Argue about something more important.  If the team is just you, then suit yourself.

In your specific example, the best answer is to eliminate i and directly iterate through the pin numbers:

for(int* p = pins; p < pins + count; ++p) {
    ConfigureGpifPinAsGpioOutput(p);
}

BTW, there are lots of C programmers who will really hate your quasi-Hungarian naming conventions.


问:
How to do a clean refactoring of an If Else Code without leaving any free blocks?
```<code>if(condition1)
{
   Statement1A;
   Statement1B;
}
else if(condition2)
{
  Statement2;
}
else if(condition3)
{
  Statement3;
}
else
{
   Statement1A;
   Statement1B;
}

   return;
</code>```

I would like to refactor that code so that I do not duplicate Statements. I always need to check condition1 before any other condition. (So I cannot just change the order of the conditions). I also do not want to write &&!condition1 after every other condition.

I solved it like this

```<code>if(condition1)
{
}
else if(condition2)
{
  Statement2;
  return;
}
else if(condition3)
{
  Statement3;
  return;
}

Statement1A;
Statement1B;
return;
</code>```

However I do not think an empty if condition will be easily understandable by others (even by me after a while).

What is a better approach?

答:
Two things come to mind :

Switch-case

though not always applicable it can make the code easier to read and maintain.  Basically capture the different condition in an enum and create a function to analyse the context to produce the correct enum value, then switch it and run the appropriate code.

you will achieve two things :

seperate the logic code from the execution.  This isolation will make it easier to fix bugs in each parts.

easy to read control flow.  The switch structure is pretty intuitive, especially so if you carefully name the enum values.

Functions

Capture each block of code in a function and in the beginiing of the function put some code to decide if you should execute or not.

Here you do not get the seperation mentioned above but you do break the IF-ELSE code flow stcructure in more maneagable bits.  Plus should a bug occur it will be faster to fix as it will be isolated to a single block of code.

Biggest advantage though is that a change in any one of these functions will not affect other parts of the code.

Functions do have an edge over the switch case is that they could be added dynamically.  One natural extention would be something akin to the command pattern where a first part of the code analyse the context and queues actions in a list then a second part simply execute these actions sequentially.

Regardless of chosen strategy KEEP IT SIMPLE sometimes a series of if-else is enough and should just remain as-is.  As a rule of thumbs if I return in the same code to fix what seems like the same problem or if I find myself often having to scratch my head to add yet a noew condition then perhaps it`s time to seek alternatives such as ones mentionned above.


问:
which sorting algorithms is using OrderBy
I am litle curious about what sorting algorithm is using OrderBy (linq to objects). quicksort? heapsort? introspective sort ?

As i see in MSDN documentation List.Sort is using introspective sort:
see: http://msdn.microsoft.com/en-us/library/b0zbh7b6.aspx

Maybe OrderBy use the same?

If we go to order by documetation i don't see much information:
http://msdn.microsoft.com/en-us/library/system.linq.enumerable.orderby(v=vs.110).aspx

答:
Apparently it uses a stable quicksort.

This has been verified using reflector.

See: https://stackoverflow.com/a/2792105/569777


答:
I like to use anonymous type in this case because:

This is the final step of data transformation on server. A type system only increases the burden to programmers without adding any value to the operation. Regarding the strong typed client mentioned by Mike, I actually see it cause more problems in real world - the coupling between the service project and all the client projects tends to increase over the time.

Anonymous types don't pollute the namespace. Don't underestimate the advantage of this. In a project where developers come and go and business change their mind frequently, a developer tends to choose a misleading name for the new DTO he/she has to create if the "good" one was taken, which renders both name misleading to the later maintainers.

Title: DTOs matter on ASP.NET MVC JSON Action?

Body:
I recently came across an internal project where they were using DTOs throughout the solution, and when I sat down to implement a new MVC controller with a JSON REST endpoint I decided to use an anonymous object convention instead of declaring a discrete DTO class solely for the purpose of this API endpoint.

```<code>public JsonResult GetSomeSuch()
{
    // .. do work

    return Json(new {
        PropA = domainObj1.PropA,
        PropB = domainObj2.PropB
    });
}
</code>```

.. as opposed to the team convention, ..

```<code>public JsonResult GetSomeSuch()
{
    // do work ..

    return Json(new SomeSuchDTO2 {
        PropA = domainObj1.PropA,
        PropB = domainObj2.PropB
    }
}

// in some other file off in some other project and folder
public class SomeSuchDTO2 {
    public int PropA { get; set; }
    public string PropB { get; set; }
}
</code>```

Setting aside that I should have used a DTO here purely because it was already a team convention, team conventions trump best practices until team conventions change by team leadership, of which I was not yet a part. I'm not concerned about the social / team dynamic and consistency aspects so much as the technical value of details in specifically that point at which an action in C# becomes serialized to Javascript, as I have since moved on from that company.

The team convention was to return only DTOs and the DTOs were managed in a completely different project (among about 50 projects in the solution), I felt that in this case perpetuating that pattern was excessive and difficult to maintain. Further, my belief is that DRY must correlate with YAGNI; only refactor when you're repeating the same thing twice, as such you would only refactor to use a DTO if that same return object structure reflecting the same context is in fact needed elsewhere, not just possibly but actually, but since this is being serialized to Javascript where it will be consumed anyway there is really no point anyway.

I suppose a case can be made for DTOs as specification documentation, but when it becomes more of a maintenance chore to declare them and to look them up in their special folder in a project I would argue that there are more appropriate places and means to document the interface; indeed the anonymous object declaration seems to be documentation enough. I should also add that in our case everything was internal, this was a standalone project, so it's not like this was an externally exposed API.

I was wondering if there are other angles to this that I didn't consider as to why I should use DTOs, something reasonably convincing?


答:
They say that coming up with names is hard, but I believe it's only hard because too many idealist programmers insist you should always extract declaration from usage whenever possible. That stupid rule enforces you to name those extracted declarations, and when the sole reason for extracting them is to obey that rule, it's ofter hard to name them. Even if the language would have forced you to use a declaration anyways, that rule requires that you move the declaration somewhere far away from the usage, thus preventing you from using the context of the usage as implicit reference while choosing a name, something that could have made the naming task much easier.

How was that related to your question? When pondering whether to extract declaration from usage or not, I find that a great rule of thumb is to consider what name you'll give the thing you declare. If the name comes naturally, it means it has some meaning in the domain, and you should make it a DTO. If you have to struggle to find a name, and all that you can come up with is something directly related to the usage(like <methodname>_result) or a direct description of the fields inside1(like IntAndStringTuple), you shouldn't make it a DTO unless something in the project is built in a way that forces you to use DTOs.

In your case, the method is named GetSomeSuch and the DTO is named SomeSuchDTO2. These are ofcourse not the real names used in the project, but I'll use them for the example. If SomeSuchDTO2 is named after GetSomeSuch - that is, if without GetSomeSuch there is no direct way to understand what SomeSuchDTO2 means - then you shouldn't make a DTO. But if GetSomeSuch is named after SomeSuchDTO2 - that is, if SomeSuch is a thing - then you should create a DTO.

1When I say "direct description of the fields", I mean description of the fields that compose the object rather than description of the object as a whole. For example: PersonName is not a direct description of the fields. even though you can understand what the fields would be from it, it describes the object as a whole. A direct description of the fields for the same class would be something like FirstNameAndLastName.

Title: DTOs matter on ASP.NET MVC JSON Action?


问:
Add javascript to all pages (for caching), or only to the pages that require the scripts (for a smaller page size)?
I'm using bits of javascript on my website for an image gallery, smooth scrolling, etc. These scripts are not used on every page (not every page has an image gallery for example). However, the scripts aren't very big and most users will have to download them eventually (the image gallery is very central to the website).

Now what would be the pros and cons of these two (mutually exclusive) ways of including the scripts:

add them to every page (so that they'll all be cached after one page view)

add them only on the pages on which they are used (so they're cached only when they're needed, but some pages will take a little longer)?

I'm not asking for opinions, I'd just like to know if there are any pros and cons I've overlooked.

答:
Another consideration is maintenance.  If you have code in 10 files, that's 10 files you have to check in order to make one change.

Re lazyloading: that's an extra file that has to be loaded (the jquery plug-in)?  How big? Why would you want to add code that probably isn't necessary.  For example, you could pre-load the first 5-6 images with your HTML, then include the rest once the page has loaded/rendered.

If you're really worried about load times, try both...time it.


问:
Google App Engine, what are the advantages and limitations?
First, does anyone know a specific advantage of Google App Engine for Python Apps?
Is there some advantage over Heruko or Appfog?
And about the database API?

Now, what about the limitations?
Are there any factors that make you think twice about using GAE?

答:
Here's my two cents:

Cons:
 - Slow development process. It's painfully slow to debug something related to data.


答: Don't know why no one has mentioned this yet, but Google App Engine Launcher is a really neat little tool. It allows you to run your app on localhost, and when you make changes it automatically updates the app in real time without having to re-deploy. I'm sure that Heroku has something like this by now, but my experience with GAE Launcher has been really smooth. Also, a big con to GAE run apps is that error messages are really tricky to find. When you run an app on localhost, you can read the log in the provided console, but if you want to debug a deployed application, the only way to check logs is on the Admin Console, which is a website. </p>

Title: Google App Engine, what are the advantages and limitations?

Body:
First, does anyone know a specific advantage of Google App Engine for Python Apps?
Is there some advantage over Heruko or Appfog?
And about the database API?

Now, what about the limitations?
Are there any factors that make you think twice about using GAE?


问:
What does it mean for "Bootstrap" to be precompiled?
I was reading the Bootstrap Getting Started docs, and I came across "Pre-Compiled Source" as a feature. Upon downloading the package, I just found .js, .css, and some image files. What is there to be "pre-compiled"? Doesn't it all compile in the browser upon page load? I'm guessing this means that it was compiled and tested before shipping, but I don't understand why this would need to be advertised.

答:
Precompiled, as it relates to Javascript and CSS, is a misnomer.  When some Javascript or CSS library or framework says it is precompiled, what they usually mean is that it is minified or obfuscated.

I think the term has been popularized for use with these frameworks to give the impression that being "compiled" somehow makes them more efficient or "performant" (another made-up term), but it is a misuse of the word all the same.

What they may be referring to is the use of the LESS preprocessor to produce CSS that is compatible with the browser.


答:
Bootstrap uses LESS files to generate CSS files. The pre-compiled version contains the output from LESS. If you want to modify Bootstrap you'll need the source and then compile the LESS files into CSS.

The term compile is the word LESS uses on their website.

http://lesscss.org/

Doesn't it all compile in the browser upon page load? I'm guessing this means that it was compiled and tested before shipping, but I don't understand why this would need to be advertised.

No, Bootstrap can not run in the browser without the CSS being compiled using LESS.

Title: What does it mean for "Bootstrap" to be precompiled?

Body: I was reading the Bootstrap Getting Started docs, and I came across "Pre-Compiled Source" as a feature. Upon downloading the package, I just found .js, .css, and some image files. What is there to be "pre-compiled"? Doesn't it all compile in the browser upon page load? I'm guessing this means that it was compiled and tested before shipping, but I don't understand why this would need to be advertised.


答:
Gibson's links are useful, but the short answer is:

Comments are not value objects, in any of the listed scenarios.

You might model comments on a photo in Scenario 1 as an attribute of a photo containing a collection of strings, in which case you don't need an 'object' for them at all.

In the other two scenarios, the relationships require an identity for a comment, which makes Comment an entity.

Title: In DDD are Value Object always at the leaves of a relationship tree?

Body:
If I have a photo album where each Photo can be commented on, Should the Comments be Entities or Value Object in these cases:

Comments can be added only anonymously (i.e. Comments have no relation to anything except the photo).

Comments can be added by registered users (i.e. there will be a relation between the Comment and User entity).

Comments can have comments (i.e. 1 user replying to another).

I think modeling a Comment as Value Object in the first case sounds reasonable as a Comment won't have any relationships with any other entities.

However, what about the 2nd and 3rd case? Does the need to identify which user Posted which Comment changes it from a Value Object into Entity?


问:
Implementing a programming language without the specification
What if, instead of looking at the C++ specification, you analyze the behavior (by inspecting the source code and testing with sample inputs) of existing C++ compilers and use your knowledge of C++ to create a new compiler? Is it a good way to implement programming languages? What are the possible disadvantages of this approach?</p>

答:
To me, your questions sounds very similarly to "what if instead of using blueprints, we design a bridge by observing other bridges combined with our own experience of driving and walking over a bunch of bridges in the past?"

... I wouldn't step a foot on a bridge built with that methodology.

Specifications are there for a reason. People spend a lot of time and energy to generate those 100+ to 1000+ page documents so that every feature and every functionality is nailed down and ideally no ambiguity is left when it comes to deciding how a certain feature should work.

In essence, those specifications are the blueprints for a programming language. Without them the language would be as good as that hypothetical bridge that I just talked about.


答:
This happens all the time in the industry--it's reverse engineering. The compiler in this case is a black box (you have no idea how it does what it does), but you have some knowledge of its inputs and outputs. You can then create a new program that performs the same operations on inputs to get identical outputs.

In general, it's not a great way to do something if you have specifications, but there are cases where you need to find the internal workings of a system you have no control over, and this is a perfectly valid practice. You may, for instance, miss important implementation details and corner cases that don't always appear in the code, but are very clearly spelled out in the specification documents.

Title: Implementing a programming language without the specification

Body: What if, instead of looking at the C++ specification, you analyze the behavior (by inspecting the source code and testing with sample inputs) of existing C++ compilers and use your knowledge of C++ to create a new compiler? Is it a good way to implement programming languages? What are the possible disadvantages of this approach?</p>


答:
Yes you can reverse engineer a compiler. An example off the top of my head, microsoft reverse-engineered the original JavaScript (maybe then livescript) compiler.

Why is this generally bad? Well consider what happened with JS, Microsoft almost exactly duplicated JavaScript, down to the bugs. This meant that every implementation quirk that really wasn't meant to be there was now part of their version. In fact, later on during the standardization process, MS forced some of these bugs into the standard*!
In your example, you'd find it almost impossible to reverse engineer C++/C, implementation defined behavior/undefined behavior is rampant! You'd have no way to separate standard, undefined, and incorrect behavior. This would lead to a substandard and overly complicated implementation on your part.

TLDR: Possible, yes. Advisable, no.

*And have subsequently poured money into typescript trying to undo some of those mistakes


问:
Why do programming languages encompass so much?
Programming languages are not just language. Each one tends to have:

Its own IDEs,

Its own package managers,

Its own set of libraries,

Its own build tools,

Its own data formats.

If you are writing Java syntax, you will be using Maven packages; if you are writing Haskell syntax, you will be using Cabal packages. What's the connection? Why does each programming language create its own world?

答:
Mostly because it has to. There really is no single, usable, method of interop between all languages. And I doubt there ever will be. The semantic differences between languages vary far too much.

Most of the differences stem from this

Libraries are written in the idioms of  their language, which carry poorly to many others.

Complete build tools might include a notion of package management => libraries => per language

IDE's have to talk about their language's syntax, types and semantics. You can't get more language specific than that.

Though on this point, there are quite a few tools that focus on being "platforms". Some that spring to mind: Emacs, IntelliJ, Eclipse...

The only one on this list I think would be reasonable to share would be package managers. It would be closer to apt-get than anything. The main reason this hasn't happened is just too much effort.

Trying to dislodge decades old package management systems is a hard game to play, and no one has simply done it well enough yet. Additionally it's an uphill struggle since per-language communities are much more tightly bonded than "all programmers", but again this is nontechnical.


答:
Why does each programming language create its own world?

They don't.  You're mistaking the direction of correlation, and grouping together two different aspects.

Development tools (IDE's, package managers, and build tools) are best categorized by the persons who created them, and for whom they were created.  Eclipse and Visual Studio are part of very different tech stacks, but the differences can be clearly identified by who does what.  These tend to cluster together with language selection, since the same people have their choice of languages as well.

Common Code (libraries, example code, and idioms) are created within a given language, and are only useful if you can include them in the same compiler / runtime as the code of your overall language.  Thus, they'd be separate and per-language no matter what.

You can definitely take advantage of the Development Tools and Common Code from one technical stack in another, but doing so is often a less than efficient choice.

If you take advantage of a language's turning-completeness, you can definitely write an interpreter within it for any other extant language.  But doing so is going to either introduce a heck of a lot of complexity to your final program or limit yourself to the idiosyncrasies of both.

All OS's allow some form of communication between running programs, be it via XHR's in JavaScript or COM in the old windows world or direct "command-line" piping in the Linux world.  But doing this almost always introduces overhead and runtime complexities, and costs you either RAM or execution speed.

Title: Why do programming languages encompass so much?

Body:
Programming languages are not just language. Each one tends to have:

Its own IDEs,

Its own package managers,

Its own set of libraries,

Its own build tools,

Its own data formats.

If you are writing Java syntax, you will be using Maven packages; if you are writing Haskell syntax, you will be using Cabal packages. What's the connection? Why does each programming language create its own world?


问:
Faster reads, on an infinite grid plane, than using a hash table?
In the past, I've had alot of success just using a .NET Dictionary, with a TKey consisting of the X,Y coordinates merged together. However its read performance, despite being amortized constant time, is a bottleneck in a design of mine.

My application needs to perform alot of reads, and would benefit greatly if the performance of the read was similar to that of an array. My data has a very high degree of spatial locality; attached is an image showing two graphs of the distribution of some sample data on a 2D plane (sorry for the lack of labels); on the left is data with low spatial locality, and on the right is how my data looks (it's always a single connected mass, and blocky in form).

I could use an array (by computing a bounding rectangle (bRect) around the data and then doing data=array[(y-bRect.Top)*bRect.Height+x-bRect.Left]) but then I'd have to rebuild the entire array each time bRect.Height changed, or bRect.Width increased.

And so my question is, given the high degree of spatial locality, is Dictionary really the best choice here? Is there another approach I could take where I could get near array like read performance, yet not have to rebuild the array when data is added? (I don't need to ever remove data)

答:
I don't really think there is way to make it quicker. According to MSDN, the retrieval time of Dictionary is close to O(1), which is as fast as you can get. And the way you calculate the hash value has no room for hash collisions.

Only thing you can do right now is to change the algorithm to minimize number of reads.

So, I tried to do some quick benchmarking. I used 3 algorithms: HashSet, Dictonary and simple Quadtree implementation. I made 2 datasets, first with completely random distribution. Second was made by creating random boxes and filling them up. The read dataset was random distribution. HashSet and Dictionary are pretty similar. Results are in ticks:

HashSet

Even distribution : 350000

Box distribution : 430000

Quadtree

Even distribution : 1510000

Box distribution : 490000

You can get my source here: http://www.mediafire.com/download/4bcrc41my23k9ef/PerfTest.zip

Your test makes little sense. First, why are you saving points? Shouldn't you save booleans or ints or something? Also, if you are filling the whole space, then dictionary is not going to make much sense. Second, the way your read the data has huge memory locality. Are you sure you are going to read the data in such sequential manner? This is reason why I'm randomizing the order of access in my benchmark. Next thing is that rectangle of yours. Are you sure you are going to have only one? Your algorithm is going to break down if you are going to have multiple. And are you sure you are able to rebuild such bounding box from the data you have on the input? Is this algorithm you can directly use in your problem or is it something you need to change? If later, then the results are useless and non-informative.


问:
Is there a reason for the crossover from the Ruby community to the Clojure Community?
When we look at the the overlap between the Ruby Community - we see the following overlaps:

Think Relevance (now Cognitect) has switched from Ruby to Clojure

Jay Fields has switched from Ruby to Clojure

David Chelimsky - author of RSpec - has left the Ruby RSpec project to join Cognitect

Is there some linguistic similarity that has lead to this crossover?

答:
Here are my two cents. Both languages target the same market:

web applications

dynamically typed

new and shiny (vs c++, java, c#)

Ruby is not really new and shiny anymore, and that would be one of the explanation for the shift of some companies toward clojure. Except from that, clojure has some advantages:

heavy use of immutable data structures

clean & modern lisp syntax

Although clojure is fairly new, some tools like lighttable/emacs have enough momentum to create a good enough working environment. Clojure is also relatively easy to learn (compared to haskell).

So I would say that some people are attracted to clojure because it's new and they want to explore the functional paradigm.


问:
processing Postfix log with python
I need to process all log messages from Postfix (/var/log/mail/mail.log), and print a summary/statistics (how many emails were sent/received and from/to which email addresses)

The situation is made more complicated by the fact, that Postfix has multi-line log entries (in contrast, Apache for example, has single line entries and the task would have been much easier).

A sample Postfix log might look something like this:

```<code>2013-12-03 14:40:45  postfix:  6F1AA10B: client=unknown[64.12.143.81]
2013-12-03 14:40:45  postfix:  6F1AA10B: message-id=<529DDF56.6050403@aol.com>
2013-12-03 14:40:45  postfix:  6F1AA10B: from=<martin.vegter@aol.com>, size=1571, nrcpt=1 (queue active)
2013-12-03 14:40:45  postfix:  6F1AA10B: to=<martin@example.com>, relay=local, delay=0.13, delays=0.13
2013-12-03 14:40:45  postfix:  6F1AA10B: removed

2013-12-03 14:52:07  postfix:  9DD9610B: client=unknown[209.85.219.65]
2013-12-03 14:52:07  postfix:  9DD9610B: message-id=<CANE3EAQUsGwj6ZBAU-awymzsG=76XZnHih@mail.gmail.com>
2013-12-03 14:52:07  postfix:  9DD9610B: from=<martin.vegter@gmail.com>, size=2388, nrcpt=1 (queue active)
2013-12-03 14:52:07  postfix:  9DD9610B: to=<martin@example.com>, orig_to=<martin@example.com>, relay=local
2013-12-03 14:52:07  postfix:  9DD9610B: removed
</code>```

Every email message that was processed by Postfix has a unique message ID (in my example 6F1AA10B).

What would be the best approach to process the logs in Python? What data structure would you recommend to use for storing the entries?

答:
How you store your items depends on how you are processing them further; in-memory aggregating is very different from storing individual items in rows in a SQL database, for example.

Parsing could be done by grouping records on a specific element in the line. Presumably an event for a given message ID can span multiple timestamps, but you can parse out lines into a dictionary, then use itertools.groupby() to group parsed lines.

I'll not go into the line parsing itself, but if we assume that a dictionary is produced to with a msgid key you can do:

```<code>from itertools import groupby
from operator import itemgetter

for msgid, messages in groupby(parsed_lines, key=itemgetter('msgid')):
    for message in messages:
        # Each `message` is a dictionary where the `msgid` is the same
</code>```

Title: processing Postfix log with python

Body:
How you store your items depends on how you are processing them further; in-memory aggregating is very different from storing individual items in rows in a SQL database, for example.

Parsing could be done by grouping records on a specific element in the line. Presumably an event for a given message ID can span multiple timestamps, but you can parse out lines into a dictionary, then use itertools.groupby() to group parsed lines.

I'll not go into the line parsing itself, but if we assume that a dictionary is produced to with a msgid key you can do:

from itertools import groupby
from operator import itemgetter

for msgid, messages in groupby(parsed_lines, key=itemgetter('msgid')):
    for message in messages:
        # Each `message` is a dictionary where the `msgid` is the same