问:
Library/API Runtime Between Versions
I was having a conversation with a friend about the C# StringBuilder class, and what it's behavior was.  I'll paraphrase, but my side of the conversation was something like this (I oversimplified because exactly how StringBuilder works isn't important for my question)

StringBuilder is more efficient to use for extensive string concatenation than to simply use +.  The reason is that StringBuilder doesn't dynamically create a new string for each concatenation operation.  It waits until all of your desired concatenations are "built" and only then does it dynamically allocate the space and give you back your string.

My friend said something along the lines of

That may be true today, but if you are reliant on such optimizations, then you should make your own StringBuilder.  In future versions, there's no reason that StringBuilder couldn't use simple string concatenation (str1 = str2 + str3).  Libraries only guarantee functional equivalence, not runtime equivalence.

Is this true for libraries?

If I use a library's Sort() function that has runtime O(n*log(n)), is it possible that a future version would change the runtime to O(n^2)?

Is the same true for executable tools?

Could (for example) grep's runtime fundamentally change in the future?

That aside, wouldn't it be good practice for a library/API/tool developer to keep runtimes for the same calls similar over time?

答:
Short answer: it is not true for libraries or tools "in general". Each vendor can guarantee for his library or tool whatever he wants. There are libraries and tools where the vendor does guarantee

functional equivalence and the equivalence of certain aspects of non-functional requirements

just functional equivalence, not more

only syntactical equivalence

none of the above

And even if a vendor guarantees you something for a specific version or version line of a product, noone can hinder him legally to create a new version or product line with a different API, or different non-functional behaviour. So the question "can it be changed" is not really the correct one, instead you should ask is "how likely is that for a specific tool or library?"

For your example of the StringBuilder class, IMHO it is absurdly unlikely that MS will change its run time behaviour within the .NET framework in a manner so the effort of writing such a class by yourself will ever be worth it. The cite of your friend sounds more like superstitious nonsense an overcautious misconception for me, at least for this case.

Microsoft added such a class explicitly to the .NET framework to provide a mutable alternative to the immutable String class with certain performance aspects in mind, the documentation of that class is very detailed about that. MS in the past tried to keep newer versions of the .NET framework mostly backwards compatible to older versions, even if that means not to fix certain bugs or live with some imperfectness. And changing the run time behaviour of a StringBuilder in a significant manner would not break only your program, but most probably ten thousands of other programs - StringBuilder is one of the central core classes of the framework, and widely used among the .NET ecosystem. That is nothing any sensible library vendor would change lightheartly. When they annoy their customers too much, customers start looking for a different vendor, and that will cost them money.

The same is true for lots of other tool or library vendors, and those which do not care for this risk to annoy their customers until they look for a different vendor.

To give you another example: the C++ standard library gives explicit specifications for the run time behaviour of std::sort, it guaranteed to be O(n * log(n)) for the average case, see Wikipedia.

And for question of "grep": I am pretty sure there already exist different implementations of grep from different older unix or unix-like systems, and I would be astonished if they all have the same run time behaviour. The Posix standard makes them have same command line switches, at least for any Posix-conform OS. However, today the fact Linux including GNU grep is so popular, you can probably rely even on its non-functional behaviour at least on any decent Linux system.

Title: Library/API Runtime Between Versions


问:
Best practice for getting last record inserted in DB
Me and a couple of colleagues are having a discussion regarding the following case:

In an OrderStatus table we're keeping track of all the statuses an order goes through in time, including "Pending", "Available", "Returned", etc. There's a timestamp field in the table to keep track of when each status was added. However, there are two statuses that may be added exactly at the same time, one after the other, in which case both may have exactly the same timestamp (including milliseconds). In another part of the code, we need to check which is the last status an order was set to, and since we're ordering by the timestamp we can easily get the wrong record from the database.

The table has an IDENTITY(1,1) primary key, and my colleagues think we should order using that column, since that will always necessarily give you the latest record added to the table.

I feel like an IDENTITY column should be voided of any meaning and thus should not be used for any sorting, and would rather do a Thread.Sleep(1) to make sure the timestamps are different and we can keep sorting using that field.

What do you think is the best practice in this case?

答: Use numeric status values where the higher value represents the higher state. Add that to your index in descending order.  Then pull back the result ordered by time stamp and the status in descending order.  This way the entry with the farthest progression is always returned.

ContentLicense: CC BY-SA 3.0


问:
Enum with a lot of boolean properties
I'm currently working on a webapp where we often need to condition some server logic based on the page that is going to be returned to the user.

Each page is given a 4-letter page code, and these page codes are currently listed in a class as static Strings:

```<code>public class PageCodes {
    public static final String FOFP = "FOFP";
    public static final String FOMS = "FOMS";
    public static final String BKGD = "BKGD";
    public static final String ITCO = "ITCO";
    public static final String PURF = "PURF";
    // etc..
}
</code>```

And often in the code we see code like this (1st form):

```<code>if (PageCode.PURF.equals(destinationPageCode) || PageCodes.ITCO.equals(destinationPageCode)) {
    // some code with no obvious intent
}
if (PageCode.FOFP.equals(destinationPageCode) || PageCodes.FOMS.equals(destinationPageCode)) {
    // some other code with no obvious intent either
}
</code>```

Which is kind of horrible to read because it doesn't show what common property of these pages led the author of the code to put them together here. We have to read the code in the if branch to understand.

Current solution

These ifs have partly been simplified using lists of pages which are declared by different people in different classes. This makes the code look like (2nd form):

```<code>private static final List<String> pagesWithShoppingCart = Collections.unmodifiableList(Arrays.asList(PageCodes.ITCO, PageCodes.PURF));
private static final List<String> flightAvailabilityPages = Collections.unmodifiableList(Arrays.asList(PageCodes.FOMS, PageCodes.FOFP));

// later in the same class
if (pagesWithShoppingCart.contains(destinationPageCode)) {
    // some code with no obvious intent
}
if (flightAvailabilityPages.contains(destinationPageCode)) {
    // some other code with no obvious intent either
}
</code>```

...which is expresses the intent much better. But...

Current Problem

The problem here is that if we add a page, we would in theory need to go through all the code base to find if we need to add our page to an if() or to a list like that.

Even if we moved all those lists to the PageCodes class as static constants, it still needs discipline from developers to check if their new page fits in any of those lists and add it accordingly.

New solution

My solution was to create an enum (because there is a finite well-known list of page codes) where each page contain some properties that we need to set:

```<code>public enum Page {
    FOFP(true, false),
    FOMS(true, false),
    BKGD(false, false),
    PURF(false, true),
    ITCO(false, true),
    // and so on

    private final boolean isAvailabilityPage;
    private final boolean hasShoppingCart;

    PageCode(boolean isAvailabilityPage, boolean hasShoppingCart) {
        // field initialization
    }

    // getters
}
</code>```

Then the conditional code now looks like this (3rd form):

```<code>if (destinationPage.hasShoppingCart()) {
    // add some shopping-cart-related data to the response
}
if (destinationPage.isAvailabilityPage()) {
    // add some info related to flight availability
}
</code>```

Which is very readable. In addition, if someone needs to add a page, he/she is forced to think about each boolean and whether this is true or false for his new page.

New Problem

One problem I see is that there will be maybe 10 booleans like this, which makes the constructor really big and it might be hard to get the declaration right when you add a page.
Does anyone have a better solution?

答:
You can take the idea one step further and define an enum for your page features instead of using booleans.

This makes it easy to add/remove a feature to a page and makes the page definitions instantly readable even when there are 30-40 potential features.

```<code>public enum PageFeature {
    AVAIL_PAGE,
    SHOPPING_CART;
}

public enum Page {
    FOFP(AVAIL_PAGE),
    FOMS(AVAIL_PAGE),
    BKGD(),
    PURF(SHOPPING_CART, AVAIL_PAGE),

    private final EnumSet<PageFeature> features;

    PageCode(PageFeature ... features) {
       this.features = EnumSet.copyOf(Arrays.asList(features));
    }

    public boolean hasFeature(PageFeature feature) {
       return features.contains(feature);
    }
 }
</code>```

Title: Enum with a lot of boolean properties

Body:
You can take the idea one step further and define an enum for your page features instead of using booleans.

This makes it easy to add/remove a feature to a page and makes the page definitions instantly readable even when there are 30-40 potential features.

public enum PageFeature {
    AVAIL_PAGE,
    SHOPPING_CART;
}

public enum Page {
    FOFP(AVAIL_PAGE),
    FOMS(AVAIL_PAGE),
    BKGD(),
    PURF(SHOPPING_CART, AVAIL_PAGE),

    private final EnumSet<PageFeature> features;

    PageCode(PageFeature ... features) {
       this.features = EnumSet.copyOf(Arrays.asList(features));
    }

    public boolean hasFeature(PageFeature feature) {
       return features.contains(feature);
    }
 }


问:
What was the reason behind using quotes in Go's import statements?
The usage of quotes in Go's import statement strikes me as unnecessary. Typical Go import statements look like:

```<code>import "foo/bar"
import other_name "foo/bar"
import (
    "foo/bar"
    x "foo/baz/bar"
)
</code>```

According to this Quora post, it's for unusual paths containing weird characters.

As far as I can tell:

C and C++ #include can have weird paths

Java is rather strict

You can work around in Python

It strikes me as an unusual concession to make - how often does one see import/includes using paths with spaces or other weird characters? What was the original reason behind requiring quoted paths? Is it documented anywhere?

答:
I don't know if they're a core golang dev, but Aram Hăvărneanu answered this question here:

The structure of an import path is not part of the language
  specification, so it has to be a string [...] to allow flexibility and to avoid locking in
  some semantics forever.

I agree with you: I think the majority of programmers won't need those non-standard paths and Aram's justification smells of YAGNI. Sure, support for some alternative import or "weird paths" might be nice, but it should be optional, without forcing this ugliness upon everyone.


问:
Branching and Merging Business Data
We're working on a project wherein the business users operate on a set of data that is periodically published.  We've labeled the publishing milestones as Versions, and, due to some business constraints, end up duplicating the data set for each Version that gets published.

During the work cycle, it often becomes prudent for one of the work groups to spin off a side project (which we've aptly named Project), work on that set of data for some time, then blend their changes back into the parent Version, possibly waiting until two or more Versions have been published before their changes get blended back in.

I can see the lights coming on from here.  "Ah ha!" you say, "That's the same thing that happens in [Git, SVN, TFS, etc.]; code branching and merging.  Just do that."

Right, that's a perfect lead-in to my question: how does one implement branching and merging for business data that is spread across seven or so primary objects and their representative children?  I understand clearly what I want to achieve, I just have no idea how to write the rules so that the developers can take it forward and implement successfully (I'm the solution architect for the project and a middling code writer).  I tried researching it, but am only finding articles on which Git (or Svn or whatever) commands to run to get the desired results.

By the way, we're developing on .NET 4.5 and implementing Workflow Foundation and Entity Framework if that helps matters.

Update

Per the question below, the "primary objects" are things like Model, Option, Version, Rule, and Note.  Each of those have child objects (a Rule has a set of Conditions), joining objects (RuleVersion), and of course properties (Option.Description, etc.).  One of the system's primary function is to track cost and retail price, so Price and Cost objects are hanging on the edges, each with their own set of properties (Price.Retail, Price.Calculated, etc.).

答:
What you want to do is called a three-way merge.

Basically, you take the original before the branch, and the end result of the two branches. You take the differences between both branches and the original, and merge those diffs together.

How do you merge the diffs? That's the tricky part, but here are several projects which implement the algorithm for arbitrary javascript objects:

https://github.com/dominictarr/xdiff

https://github.com/rsms/js-object-merge

https://github.com/falsecz/3-way-merge

You may find that you need to devise your own business-specific rules about how to merge. But I think you'll want to follow the same basic pattern of a three-way merge.

Title: Branching and Merging Business Data


问:
Single Responsibility - is this class doing too much?
I'm redesigning one of my programs which performs certain actions on processes of interest (known as "Monitored Processes" in my program).

Some actions I always need to do on those processes are:

Open process handle

Get process name

Close process handle

And some further actions I may need to do on some or all of them are:

Suspend process

Resume process

Set process priority

Set process affinity

Previously I simply had a "handler" class that opened a handle to the process, checked its name, and performed the requested actions. All of that is done via p/invoke, I don't use .NET's Process class at all.

However I am trying to redesign it from a SOLID perspective, so I created a "MonitoredProcess" object and have added a number of methods to it to perform the actions.

Here it is in its current state:

```<code>public class MonitoredProcess : IMonitoredProcess
{
    private readonly IMonitoredProcessConfig config;
    private readonly int processId;
    private IntPtr handle = IntPtr.Zero;
    private uint suspendResumeResult;
    private string processName;

    public IMonitoredProcessConfig Config { get { return config; } }
    public int ProcessId { get { return processId; } }
    public uint SuspendResumeResult { get { return suspendResumeResult; } }
    public string ProcessName
    {
        get
        {
            if (processName == null) PopulateProcessName();

            return processName;
        }
    }

    public MonitoredProcess(IMonitoredProcessConfig config, int processId)
    {
        if (config == null) throw new ArgumentNullException("config");

        this.config = config;
        this.processId = processId;
    }

    public bool OpenProcess()
    {
        var alreadyOpen = handle != IntPtr.Zero;
        if (alreadyOpen) return true;

        handle = Kernel32.OpenProcess(config.RequiredRights, false, (uint)processId);
        return handle != IntPtr.Zero;
    }

    public bool CloseProcess()
    {
        var closed = Kernel32.CloseHandle(handle);
        if (closed) handle = IntPtr.Zero;
        return closed;
    }

    public uint Resume()
    {
        suspendResumeResult = Ntdll.NtResumeProcess(handle);
        return suspendResumeResult;
    }

    public uint Suspend()
    {
        suspendResumeResult = Ntdll.NtSuspendProcess(handle);
        return suspendResumeResult;
    }

    private void PopulateProcessName()
    {
        var buffer = new StringBuilder((int)Kernel32.GeneralConstants.MAX_PATH);
        var success = 0 != Psapi.GetModuleFileNameEx(
            handle,
            IntPtr.Zero,
            buffer,
            (uint)buffer.Capacity
            );

        processName = success ? Path.GetFileName(buffer.ToString()) : string.Empty;
    }

}
</code>```

I haven't yet added methods to set the process's priority and affinity. The interface it implements is not yet really defined, it will become whatever the class eventually exposes.

Question:

Is this class doing too much? Should a "process" know how to suspend/resume itself, and set its own priority and CPU affinity? Or should I have other classes that take an IMonitoredProcess and perform these actions on them (which is how I did it before)?

In the way I did it before, the classes performing the actions implemented them through p/invoke, so they "knew" they could throw and catch Win32Exceptions to get the error message. If this MonitoredProcess class implements everything via p/invoke internally, the class calling those methods technically shouldn't know how they're implemented, so has no reason to either throw or catch Win32Exceptions.

And I wouldn't want to simply throw them from this class (even wrapped in a domain-specific exception) because in half the places I call the methods I'm not interested in the outcome, but I'd still have to try/catch it every time - and it creates objects unnecessarily.

Which again makes me wonder if this class should really be doing all these actions, ostensibly on itself, but it uses external functions to perform all of them.

答:
This depends largely on the context of a "MonitoredProcess" in your application as a whole.

Look at the name you've used: MonitoredProcess.

This implies that the process is being monitored by something.  It may be a different class, method, or something else in your application that is monitoring your processes and deciding on whether or not it wants to perform these actions.

Based on that you have to determine if the monitor itself is more concerned with executing actions on processes or tracking results of actions on processes.  In the former situation, it would be the monitor that wants to be able to perform the Open(), Close(), Resume(), and Suspend() operation on a process, not the MonitoredProcess that wants to perform these actions on itself.  In the latter, the MonitoredProcess would want to contain these actions and the monitor would simply wants to invoke these operations on the process itself and manage the results.


问:
Indentation in a multi-language file
In files that contain multiple languages (ie. template files), are there any best practises regarding indentation?

I mostly use this:

```<code><div>
    IF FOO
        <div>
            <p>contents>
        </div>
    END FOO
</div>
</code>```

Indent for every new block, regardless of language. This has a few disadvantages, though. In more complex files it can break the indentation of either language:

```<code><div>
    IF FOO
        <div someattribute>
    ELSE
        <div otherattribute>
    END FOO
        <p>content</p>
    </div>
</div>
</code>```

I've also seen this used:

```<code><div>
    IF FOO
    <div>
       <p>contents>
    </div>
    END FOO
</div>
</code>```

Ie. indent only one language. This has the advantage of always being consistent, but in more complex files can almost completely hide some implementation details, like a block being conditional.

The goal here is obviously maximizing readability.

答:
(As promised, my experience-based opinion.)

First of all, if you can prevent it: do not mix languages, whenever possible.

As per your example, it seems like you are mostly wondering about how it should be done in a templating language mixed with HTML, which I will cover. (Another case would be building SQL using another programming language, which would require completely different rules).

As an example, I'll cover Django (but PHP, Twig, etc) work mostly the same. Django has logical blocks in the templates (open {% tag %}, closing {% endtag %}). HTML has logical blocks (open <tag>, closing </tag>). Here I mostly follow this rule: increment after an open tag, decrement after a closing tag. Another rule I keep: make sure that the content of a Django block is well-formatted HTML.

As per your example, in my opinion this is very wrong:

```<code><div>
    IF FOO
        <div someattribute>
    ELSE
        <div otherattribute>
    END FOO
        <p>content</p>
    </div>
</div>
</code>```

Why? Because the content of the IF is not a well-formatted HTML. What should it look like than?

```<code><div>
    <div
        IF FOO
            someattribute
        ELSE
            otherattribute
        END FOO
    >
        <p>Content</p>
    </div>
</div>
</code>```

(Because your template language does not have nice parsable syntax, we need the new-lines. In Django, I'd write)

```<code><div>
    <div {% if foo %}someattribute{% else %}otherattribute{% endif %}>
        <p>Content</p>
    </div>
</div>
</code>```

As an added benefit, imagine that the
 has 7 attributes, 1 of which is decided upon based a specific condition. Or worse: what if it has 3 attributes each of which is dependent on a condition. That would give 8 different tags.

Keeping in mind the "rule" to keep the content of all blocks well-formatted within that scope, you do not have to worry about breaking the indent of either language.

Another point which I would like to cover (though slightly off-topic): If the opening and closing tags are on different lines, they should be the only tag on that line.

Wrong:

```<code><ul>
    <li>This is something <a href="http://programmers.stackexchange.com">Usefull</a>
    </i>
</ul>
</code>```

Instead, prefer

```<code><ul>
    <li>This is something <a href="http://programmers.stackexchange.com">Usefull</a></li>
</ul>
</code>```

or

```<code><ul>
    <li>
        This is something <a href="http://programmers.stackexchange.com">Usefull</a>
    </li>
</ul>
</code>```

(Warning: advanced usage): Sometimes you have multiple wrapping tags, you may (not must) consider this as one tag:

```<code><ul>
    <li><div id="something"><span class="like this">
        Something like this
    </span></div></li>
</ul>
</code>```

However, preferably don't mix languages (host/guest) when doing this. And preferably don't do this with for loops. And don't do this with .

TL;DR for your specific question.

You have a 'host' (eg. Django templating) and 'guest' language (eg. HTML). Make sure the host language blocks always contain correctly formatted guest language blocks.

The opening of each block should increment the indentation, the closing of each block should line up with the opening.

And the general note which I should add (as always): use your own judgement. Know when to deviate.

Title: Indentation in a multi-language file

Body:
(As promised, my experience-based opinion.)

First of all, if you can prevent it: do not mix languages, whenever possible.

As per your example, it seems like you are mostly wondering about how it should be done in a templating language mixed with HTML, which I will cover. (Another case would be building SQL using another programming language, which would require completely different rules).

As an example, I'll cover Django (but PHP, Twig, etc) work mostly the same. Django has logical blocks in the templates (open {% tag %}, closing {% endtag %}). HTML has logical blocks (open <tag>, closing </tag>). Here I mostly follow this rule: increment after an open tag, decrement after a closing tag. Another rule I keep: make sure that the content of a Django block is well-formatted HTML.

As per your example, in my opinion this is very wrong:

<div>
    IF FOO
        <div someattribute>
    ELSE
        <div otherattribute>
    END FOO
        <p>content</p>
    </div>
</div>

Why? Because the content of the IF is not a well-formatted HTML. What should it look like than?

<div>
    <div
        IF FOO
            someattribute
        ELSE
            otherattribute
        END FOO
    >
        <p>Content</p>
    </div>
</div>

(Because your template language does not have nice parsable syntax, we need the new-lines. In Django, I'd write)

<div>
    <div {% if foo %}someattribute{% else %}otherattribute{% endif %}>
        <p>Content</p>
    </div>
</div>

As an added benefit, imagine that the <div> has 7 attributes, 1 of which is decided upon based a specific condition. Or worse: what if it has 3 attributes each of which is dependent on a condition. That would give 8 different tags.

Keeping in mind the "rule" to keep the content of all blocks well-formatted within that scope, you do not have to worry about breaking the indent of either language.

Another point which I would like to cover (though slightly off-topic): If the opening and closing tags are on different lines, they should be the only tag on that line.

Wrong:

<ul>
    <li>This is something <a href="http://programmers.stackexchange.com">Usefull</a>
    </i>
</ul>

Instead, prefer

<ul>
    <li>This is something <a href="http://programmers.stackexchange.com">Usefull</a></li>
</ul>

or

<ul>
    <li>
        This is something <a href="http://programmers.stackexchange.com">Usefull</a>
    </li>
</ul>

(Warning: advanced usage): Sometimes you have multiple wrapping tags, you may (not must) consider this as one tag:

<ul>
    <li><div id="something"><span class="like this">
        Something like this
    </span></div></li>
</ul>

However, preferably don't mix languages (host/guest) when doing this. And preferably don't do this with for loops. And don't do this with .

TL;DR for your specific question.

You have a 'host' (eg. Django templating) and 'guest' language (eg. HTML). Make sure the host language blocks always contain correctly formatted guest language blocks.

The opening of each block should increment the indentation, the closing of each block should line up with the opening.

And the general note which I should add (as always): use your own judgement. Know when to deviate.


答:
I would call the class ProcessController rather then MonitoredProcess. The class instance is not a process, it encapsulates a process handle. And it does not just monitor, it controls.

You could pass the process id to the constructor.

This would get your terminology straight, it would be clear your class controls a process for you. Which is one responsibility. So you're good.

Title: Single Responsibility - is this class doing too much?

Body:
I'm redesigning one of my programs which performs certain actions on processes of interest (known as "Monitored Processes" in my program).

Some actions I always need to do on those processes are:

Open process handle

Get process name

Close process handle

And some further actions I may need to do on some or all of them are:

Suspend process

Resume process

Set process priority

Set process affinity

Previously I simply had a "handler" class that opened a handle to the process, checked its name, and performed the requested actions. All of that is done via p/invoke, I don't use .NET's Process class at all.

However I am trying to redesign it from a SOLID perspective, so I created a "MonitoredProcess" object and have added a number of methods to it to perform the actions.

Here it is in its current state:

```<code>public class MonitoredProcess : IMonitoredProcess
{
    private readonly IMonitoredProcessConfig config;
    private readonly int processId;
    private IntPtr handle = IntPtr.Zero;
    private uint suspendResumeResult;
    private string processName;

    public IMonitoredProcessConfig Config { get { return config; } }
    public int ProcessId { get { return processId; } }
    public uint SuspendResumeResult { get { return suspendResumeResult; } }
    public string ProcessName
    {
        get
        {
            if (processName == null) PopulateProcessName();

            return processName;
        }
    }

    public MonitoredProcess(IMonitoredProcessConfig config, int processId)
    {
        if (config == null) throw new ArgumentNullException("config");

        this.config = config;
        this.processId = processId;
    }

    public bool OpenProcess()
    {
        var alreadyOpen = handle != IntPtr.Zero;
        if (alreadyOpen) return true;

        handle = Kernel32.OpenProcess(config.RequiredRights, false, (uint)processId);
        return handle != IntPtr.Zero;
    }

    public bool CloseProcess()
    {
        var closed = Kernel32.CloseHandle(handle);
        if (closed) handle = IntPtr.Zero;
        return closed;
    }

    public uint Resume()
    {
        suspendResumeResult = Ntdll.NtResumeProcess(handle);
        return suspendResumeResult;
    }

    public uint Suspend()
    {
        suspendResumeResult = Ntdll.NtSuspendProcess(handle);
        return suspendResumeResult;
    }

    private void PopulateProcessName()
    {
        var buffer = new StringBuilder((int)Kernel32.GeneralConstants.MAX_PATH);
        var success = 0 != Psapi.GetModuleFileNameEx(
            handle,
            IntPtr.Zero,
            buffer,
            (uint)buffer.Capacity
            );

        processName = success ? Path.GetFileName(buffer.ToString()) : string.Empty;
    }

}
</code>```

I haven't yet added methods to set the process's priority and affinity. The interface it implements is not yet really defined, it will become whatever the class eventually exposes.

Question:

Is this class doing too much? Should a "process" know how to suspend/resume itself, and set its own priority and CPU affinity? Or should I have other classes that take an IMonitoredProcess and perform these actions on them (which is how I did it before)?

In the way I did it before, the classes performing the actions implemented them through p/invoke, so they "knew" they could throw and catch Win32Exceptions to get the error message. If this MonitoredProcess class implements everything via p/invoke internally, the class calling those methods technically shouldn't know how they're implemented, so has no reason to either throw or catch Win32Exceptions.

And I wouldn't want to simply throw them from this class (even wrapped in a domain-specific exception) because in half the places I call the methods I'm not interested in the outcome, but I'd still have to try/catch it every time - and it creates objects unnecessarily.

Which again makes me wonder if this class should really be doing all these actions, ostensibly on itself, but it uses external functions to perform all of them.


问:
Charging a fee for GPL software
If I extend software under the GPL licence, I understand that my source code must be freely available. If my source code is freely available can I legally charge a fee for using the version that I publish?

My situation: I'm using data from a program in my program and have an In-App purchase for access to all the data. The source code is publicly available and is mentioned in the program. Is this allowed?

Obviously my software isn't extremely popular and I'm banking on the fact that lots of people have no interest in downloading and compiling it themselves.

I have read Can I use GPL software in a commercial application which discusses in detail, whether or not my software would have to be under the GPL, but it does not discuss money!

答:
You can charge any amount that you like for the software.

You can charge your cost for providing the source code for the software.

You may not charge any money for the license to the software.

If you always give people the software together with the source code, then you have no other obligations towards anyone. The same happens when the source code is available at some commercial site, where you can just refer anyone to that commercial site. But if you provide software that is for example modified by you, or not available on a commercial site, without providing the source code, then you must provide the software to anybody who asks for it. That makes it kind of difficult to charge large amounts for the software and find any takers.

And since anyone who received the software with source code from you has the right to pass it on for free (or for half what you charge), that also makes it hard to charge huge amounts.

Title: Charging a fee for GPL software


问:
Erlang function naming conventions
I understand the general Erlang conventions for functions are using snake case or camel case, but what about exported functions?

For example, say I have a gen_server module that defines a check to see if a username is in use:

I have a call along the lines of handle_function(is_username_in_use, UserName).

I also have an exported function named is_username_in_use that calls gen_server:call/2.

But internally I also define a method that checks a database. So I end up with something along the lines of check_if_username_is_in_use_in_db for internal use.

It quickly gets out of hand, and I end up with uncomfortably long names. Is there a common convention used to name functions like these? I was thinking something along the lines of is_username_in_use_internal

答:
From official Erlang Programming Rules:

Function names:

The function name must agree exactly with what the function does. It should return the kind of arguments implied by the function name. It should not surprise the reader. Use conventional names for conventional functions (start, stop, init, main_loop).

Functions in different modules that solves the same problem should have the same name. Example: Module:module_info().

Some kind of naming convention is very useful when writing lots of different functions. For example, the name prefix "is_" could be used to signify that the function in question returns the atom true or false.

Also this project helps you programmatically to be compatible by those rules.

Title: Erlang function naming conventions


问:
Domain driven design with eventual consistency
My domain aggregate is struggling with simple business logic so I feel like I have modeled it incorrectly.

The architecture I have right now uses domain driven design with event sourcing. The domain aggregate produces events which get digested by subscribers. Eventually the event queue will be empty and the view store completely updated.

Where it struggles is with large collections of objects on the aggregate and the need to coordinate between domains.

So one aggregate is a ChatAggregate which allows people to add comments. However a user can only add one comment per hour or one comment for the entire life time. My domain object cannot keep all comments in memory but I still need my ChatAggregate to apply this business logic. Because of the eventually consistent nature of my view store, I cannot let my aggregates query it for business logic. How does DDD handle when an aggregate needs access to a lot of data?

The second issue I have is when a domain command is being processed, I want to query other aggregates before processing the command with the target aggregate. An example is before creating a new ChatCommentAggregate instance, I would like to query the ChatAggregate to see if the chat is still open.

答:
So one aggregate is a ChatAggregate which allows people to add comments. However a user can only add one comment per hour or one comment for the entire life time. My domain object cannot keep all comments in memory but I still need my ChatAggregate to apply this business logic. Because of the eventually consistent nature of my view store, I cannot let my aggregates query it for business logic. How does DDD handle when an aggregate needs access to a lot of data?

It re-examines the design to see if the actual requirement is being expressed correctly.  A big part of the value prop of DDD is that when the programmers are having trouble fitting the business requirements into the the design, it often
means that the programmers don't understand the real requirements.

To extend your example; you might discover that CommentSubmitted and CommentPublished are actually two different events, and that there is a business process responsible for figuring out if one event necessarily leads to the other.

The write model might look like:

```<code>User.submitComment(comment) ->
    [ CommentSubmitted
    , UserMuted
    ] ;

Chat.publishComment(comment) ->
    [ CommentPublished
    ] ;
</code>```

Where submitComment throws an exception if the muted state of the user doesn't support commenting, and the Chat refuses to publish comments where the user has already done so.

The glue that makes this work is a state machine that is listening to the event stream (warning: pseudo-code ahead).

```<code>IDLE {
    onCommentSubmitted(event) {
        asyncDispatch(publishComment(event));
        asyncDispatch(scheduleTimer(event));
        transitionTo(PUBLISHING)
    }
    onTimer(event) {
        asyncDispatch(unmuteUser(event));
    }
}
PUBLISHING {
    onCommentPublished(event) {
        transitionTo(IDLE)
    }
}
</code>```

This is a process manager; it's interacting with the domain purely through the api (listening to events, and dispatching commands).  It's also scheduling messages to its future self (via the timer).

Note that, as shown here, it doesn't generally get to invoke commands on the domain model directly.  The problem is that you are only supposed to update one history per transaction, and the process manager needs to persist its own state transitions.

It's worth noting too the space that has opened up, to push on the requirements: do we mute the user when they submit a comment (and then try to recover if the comment isn't published) or do we only mute the user if a comment is published.  Maybe we only mute the user for a few minutes when they submit, but for the full day once we have the acknowledgement that the comment is published, and so on.

The second issue I have is when a domain command is being processed, I want to query other aggregates before processing the command with the target aggregate.

Go right ahead -- that's a perfectly reasonable thing for the application to do.  You need to keep in mind that any query you run returns stale results -- you can't ever know what's "now" except within the domain aggregate itself, but "not too long ago" is almost always going to be good enough.

A common pattern for validation is that the human operator (sitting in front of the client), will try to verb, and the app will say "nope - the latest information we have says that we can't do that."  But if everything checks out, then the command is dispatched to the server, which can do its own validation prior to sending the command to the aggregate for processing.

But once you are in the aggregate, the only things you should be looking at are the current state of the aggregate, and the state captured in the command.

OwnerDisplayName: LivingOnACloud

Body:
So one aggregate is a ChatAggregate which allows people to add comments. However a user can only add one comment per hour or one comment for the entire life time. My domain object cannot keep all comments in memory but I still need my ChatAggregate to apply this business logic. Because of the eventually consistent nature of my view store, I cannot let my aggregates query it for business logic. How does DDD handle when an aggregate needs access to a lot of data?

It re-examines the design to see if the actual requirement is being expressed correctly.  A big part of the value prop of DDD is that when the programmers are having trouble fitting the business requirements into the the design, it often
means that the programmers don't understand the real requirements.

To extend your example; you might discover that CommentSubmitted and CommentPublished are actually two different events, and that there is a business process responsible for figuring out if one event necessarily leads to the other.

The write model might look like:

User.submitComment(comment) ->
    [ CommentSubmitted
    , UserMuted
    ] ;

Chat.publishComment(comment) ->
    [ CommentPublished
    ] ;

Where submitComment throws an exception if the muted state of the user doesn't support commenting, and the Chat refuses to publish comments where the user has already done so.

The glue that makes this work is a state machine that is listening to the event stream (warning: pseudo-code ahead).

IDLE {
    onCommentSubmitted(event) {
        asyncDispatch(publishComment(event));
        asyncDispatch(scheduleTimer(event));
        transitionTo(PUBLISHING)
    }
    onTimer(event) {
        asyncDispatch(unmuteUser(event));
    }
}
PUBLISHING {
    onCommentPublished(event) {
        transitionTo(IDLE)
    }
}

This is a process manager; it's interacting with the domain purely through the api (listening to events, and dispatching commands).  It's also scheduling messages to its future self (via the timer).

Note that, as shown here, it doesn't generally get to invoke commands on the domain model directly.  The problem is that you are only supposed to update one history per transaction, and the process manager needs to persist its own state transitions.

It's worth noting too the space that has opened up, to push on the requirements: do we mute the user when they submit a comment (and then try to recover if the comment isn't published) or do we only mute the user if a comment is published.  Maybe we only mute the user for a few minutes when they submit, but for the full day once we have the acknowledgement that the comment is published, and so on.

The second issue I have is when a domain command is being processed, I want to query other aggregates before processing the command with the target aggregate.

Go right ahead -- that's a perfectly reasonable thing for the application to do.  You need to keep in mind that any query you run returns stale results -- you can't ever know what's "now" except within the domain aggregate itself, but "not too long ago" is almost always going to be good enough.

A common pattern for validation is that the human operator (sitting in front of the client), will try to verb, and the app will say "nope - the latest information we have says that we can't do that."  But if everything checks out, then the command is dispatched to the server, which can do its own validation prior to sending the command to the aggregate for processing.

But once you are in the aggregate, the only things you should be looking at are the current state of the aggregate, and the state captured in the command.


问:
What software do you use to help plan your team work, and why?
Planning is very difficult. We are not naturally good at estimating our own future, and many cognitive biases exacerbate the problem. Group planning is even harder. Incomplete information, inconsistent views of a situation, and communication problems compound the difficulty.

Agile methods provide one framework for organizing group planning--making planning visible to everyone (user stories), breaking it into smaller chunks (sprints), and providing retrospective analysis so you get better at planning. But finding good tools to support these practices is proving tricky.

What software tools do you use to achieve these goals? Why are you using that tool? What successes have you had with a particular tool?

答:
Is it ok to answer none?

You seem to imply that software tools are required for successful agile planning. I don't agree. If your team is using scrum or XP correctly ("by the book"), you shouldn't have to use any software tools at all for planning.

In many cases, adding software tools to an agile process is just a way to avoid having to deal with the actual underlying problem related to poor communication or trust. Such problems are best solved by other means.

My recommendation is to start without any digital tools and only add them later when you really understand why you need them.

(Distributed teams are a special case)

ContentLicense: CC BY-SA 2.5


问:
How can I better document these data relationships/transformations?
I'm working on a project that uses RxJS to perform data transformations on varying sources of data, and I'm in the process of writing some documentation for it.

I want to find an effective way to document the following:

An abstract way to describe the cardinality and relationships of the data.

An abstract description of the data transformations.

Here are two examples of how I'm describing a data transformation. Table headers are the destination fields, the second row is the source data or a transformation done on the source data to get the desired data.

I can see that the Github Markdown format is very limited for this purpose, which is why I'm asking for help on this.

I also have a few ERD diagrams that looks like this:

I'm not sure of a clean way to document how the transformations relate to the schema, and what assumptions about cardinality are made within those transformations (getStudentTestScoreDcid in particular)

答:
Data Flow Diagrams sound like what you need

From Wikipedia:

A data flow diagram (DFD) is a graphical representation of the "flow" of data through an information system, modelling its process aspects. A DFD is often used as a preliminary step to create an overview of the system, which can later be elaborated.2 DFDs can also be used for the visualization of data processing (structured design).

A DFD shows what kind of information will be input to and output from the system, where the data will come from and go to, and where the data will be stored. It does not show information about the timing of process or information about whether processes will operate in sequence or in parallel (which is shown on a flowchart)

Emphasis above is mine

The whole point of the DFD is to show the transformational aspects of data as it moves through the system. You will always have an input (from a user, data storage, or another process) that feeds into a process with an output (to the screen, data storage, or another process). If you don't have those three elements, you don't include it on the DFD. One other item worth mentioning, a large number (I would say most) DFDs do not have a starting point or ending point on the complete diagram.

There are at least 2 different symbologies used (Gane-Sarson, and Yourdon & Coad).

The example below shows how data from a Customer goes into the Process Order process which outputs data being stored in the Transaction data storage. Duplicated data stores are usually included to show the process more easily and are often marked with an altered symbol for the data store (a D in a gray box on this example).

Sample from Visual Paradigm


问:
What are good habits for designing command line arguments?
While developing the application I started to wonder - How should I design command line arguments?

A lot of programs are using formula like this -argument value or /argument value. Solution which came to my mind was argument:value. I thought it is good because with no white spaces there is no way that values and arguments can be messed up. Also it is easy to split a string into two on the first from the left : character.

My questions are:

Is popular -argument value formula better than argument:value (more readable, easier to write, bugfree, easier to understand by expert developers)?

Are there some commonly known rules which I should follow while designing command line arguments (other than if it works it is OK)?

Asked for some more details I will provide it. However I think they should not affect the answers. The question is about a good habits in general. I think they are all the same for all kinds of applications.

We are working on an application which will be used in public places (touch totems, tables). Applications are written using Qt Quick 5 (C++, QML, JS). Devices will have Windows 8.1/10 installed. We will provide front-end interface to manage the devices. However some advanced administrators may want to configure the application on their own. It is not very important from the side of the business but as I agree with what Kilian Foth said I do not want my application to be a pain for a user. Not finding in the Internet what I want I asked here.

To more advanced Stack Exchange users: I wanted this question to be general. Maybe it qualifies to the community wiki (I do not know if existing question can be converted with answers). As I want this question to be operating system and programming language independent the answers appearing here can be a valuable lesson for other developers.

答:
Some valuable comments provided already (@Florian, Basile), but let me add ...
OP says,

We will provide front-end interface to manage the devices. However some advanced administrators may want to configure the application on their own

But also remarks:

I did not want this question to be platform or language specific

You must consider your target audience  - advanced administrators. What platform do they normally work on - Win/ Unix/ Mac? And what platform does you app run on? Follow whatever CLI conventions have already been established for that platform. Do your "advanced" admins want/ need a GUI based tool?

You want the interface to be consistent internally and with other admin tools. I don't want to stop and think is it cmd -p <arg> or cmd -p:<arg> or cmd /p <arg>. Do I need quotes 'cos there's a space? Can I cmd -p <val1> <val2> or cmd -p <val1> -p <val2> for multiple targets? Are they order specific? Overloadable? Does cmd -p2 <arg> -p1 <arg> work too? Does ls -l -r -t dir1 dir2 == ls -trl dir1 dir2 ?

For my Unix admin tools, I have always kept the guidance provided by Heiner's Shelldorado in mind along with the other references mentioned.

As important as designing the CLI is to make sure your application is designed to work with command line arguments the same as from the GUI - ie: no business logic in the GUI or use the common command called from both GUI and CLI.

Most UNIX based administrative tools are actually designed first as command lines tools and the provided GUI simply facilitates "populating" the options for the command line. That approach allows for automation, use of response files, etc. and hands-off management (less work for me!)

As classic toolset used w/this approach is Tcl/Tk. Not suggesting you switch tools; just consider design approach from writing a GUI-based administrative app to the app as a command line tool first; then layer the GUI on top for convenience. At some point you'll likely discover the GUI is a pain (and error prone) if you have to do multiple configs and re-entering generally the same options over and over and you'll look for an automated approach.

Remember your admin likely has to type in the correct values in the correct boxes anyway, so how much effort are you relieving anyway w/GUI ?


问:
Why Garbage Collection if smart pointers are there
These days, so many languages are garbage collected. It is even available for C++ by third parties. But C++ has RAII and smart pointers. So what's the point of using garbage collection? Is it doing something extra?

And in other languages like C#, if all the references are treated as smart pointers(keeping RAII aside), by specification and by implementation, will there still be any need of garbage collectors? If no, then why is this not so?

答:
Since no one has looked at it from this angle, I'll rephrase your question: why put something into the language if you can do it in a library? Ignoring specific implementation and syntactic details, GC/smart pointers is basically a special case of that question. Why define a garbage collector in the language itself if you can implement it in a library?

There are a couple of answers to that question. The most important first:

You ensure that all code can use it to interoperate. This is, I think, the big reason why code reuse and code sharing didn't really take off until Java/C#/Python/Ruby. Libraries need to communicate, and the only reliable shared language they have is what's in the language spec itself (and, to a degree, its standard library). If you've ever tried to reuse libraries in C++, you've likely experienced the horrendous pain that no standard memory semantics causes. I want to pass a struct to some lib. Do I pass a reference? Pointer? scoped_ptr? smart_ptr? Am I passing ownership, or not? Is there a way to indicate that? What if the lib needs to allocate? Do I have to give it an allocator? By not making memory management part of the language, C++ forces each pair of libraries to have to negotiate their own specific strategy here, and it's really hard to get them all to agree. GC makes that a complete non-issue.

You can design the syntax around it. Because C++ doesn't encapsulate memory-management itself, it has to provide a range of syntactic hooks to let user-level code express all of the details. You have pointers, references, const, dereferencing operators, indirection operators, address-of, etc. If you roll memory management into the language itself, the syntax can be designed around that. All of those operators disappear and the language gets cleaner and simpler.

You get a high return on investment. The value any given piece of code generates is multiplied by the number of people using it. This means that the more users you have, the more you can afford to spend on a piece of software. When you move a feature into the language, all users of the language will be using it. This means you can allocate more effort to it than you could to a library only used by a subset of those users. This is why languages like Java and C# have absolutely first-rate VMs and fantastically high-quality garbage collectors: the cost of developing them is amortized across millions of users.


问:
DDD Request Validation Handling
I stuck somewhere that I can't find a solution! There are plenty of validation questions here, but as far as I see, most of them were asking about entity validation. But what about request validation?

I'm developing a service for web application. Basically I have 3 modules which are Web, Domain and Repo. A request for Web project has dependencies to other technologies(JAX-WS auto generated classes) and they are not suitable to be used in domain. So I convert them to new request class to make more suitable for domain service. I also add defaults() and validate() methods to the new request class. So some part of request validation is handled in validate() method. I call them at first line of corresponding method in domain service. So before starting the operation, I know whether request is valid or not. Later on, I have kind-of-validation codes. At this point, I'm not very ensure that which part of the validation truly belongs request validation and which part of it belongs domain service as business logic. I believe every piece of code belongs where it has to be! But sometimes it is hard do decide:D For instance when you need to use repository while you are validating. Let me explain with example.

Let's say you need to do implement a method where customers can be added or deleted from account. Aside null control(validation-phase1), you might check either the customer is valid or not. You need repository. Then you check either account is open or not. You need repository. Boom! Then you do check either customer in request has been already added or you try to delete the customer who doesn't belong to account and so on... May be you think that those situations are not validation, but business logic. I think they are validation, because first, you check customers and account, then do other stuff. What do you think about using repository from validation method, what is your edge for validation? Do you consider above situation I mentioned as a validation? Thanks in advance.

答:
First of all, if you have a Account aggregate, and a Customer aggregate, and the business rule for modifying an account by adding a Customer requires looking at the customer state and at the account state, then either your business rules or your model are wrong.  The aggregate boundary is to include all of the state needed to enforce the business invariant.

All business rule validation belongs inside the aggregate that owns the state being checked.

All of the validation about the arguments to the command -- is this really a customer, is that number really between 1 and 100, and so on -- that logic belongs outside of the aggregate.  It's part of the command validation done by the command handler (although it is useful to have that same validation logic generally available -- the client should be able to validate a command before sending it to the command handler to be passed to the aggregate).

Title: DDD Request Validation Handling


答:
Validation rules are business logic.

Even a simple "NULL" check is a business rule -- someone decided it was OK for "Middle Name" to be missed but "County" must always be filled in.

Most validation rules are not that simple and may even require access to external services e.g. "is the guy on the credit agencies s**t list".

So forget about Validation as a separate step, they are not even a "special" business rule, in any application dealing with external input most of the business rules will be some sort of validation.

Having said that validation falls into two general categories:-

"is the request well formed" -- are the numbers numbers, are compulsory fields filled in etc. these checks can be done up front (often in the requestors web browser) and do not require access to any external data.

"are the contents valid" -- this almost always requires access to other data.

The real problem is confusing a subset of validation "check the request is well formed" with the whole of validation.

Title: DDD Request Validation Handling

Body:
I stuck somewhere that I can't find a solution! There are plenty of validation questions here, but as far as I see, most of them were asking about entity validation. But what about request validation?

I'm developing a service for web application. Basically I have 3 modules which are Web, Domain and Repo. A request for Web project has dependencies to other technologies(JAX-WS auto generated classes) and they are not suitable to be used in domain. So I convert them to new request class to make more suitable for domain service. I also add defaults() and validate() methods to the new request class. So some part of request validation is handled in validate() method. I call them at first line of corresponding method in domain service. So before starting the operation, I know whether request is valid or not. Later on, I have kind-of-validation codes. At this point, I'm not very ensure that which part of the validation truly belongs request validation and which part of it belongs domain service as business logic. I believe every piece of code belongs where it has to be! But sometimes it is hard do decide:D For instance when you need to use repository while you are validating. Let me explain with example.

Let's say you need to do implement a method where customers can be added or deleted from account. Aside null control(validation-phase1), you might check either the customer is valid or not. You need repository. Then you check either account is open or not. You need repository. Boom! Then you do check either customer in request has been already added or you try to delete the customer who doesn't belong to account and so on... May be you think that those situations are not validation, but business logic. I think they are validation, because first, you check customers and account, then do other stuff. What do you think about using repository from validation method, what is your edge for validation? Do you consider above situation I mentioned as a validation? Thanks in advance.


答:
The most useful way I've found to understand the Single Responsibility Principle is to interpret "responsibility" as "reason why the code might change". This is because the primary goal of SRP is to group together code that will change together, and (as far as possible) keep code that will change for different reasons separated from it. By doing this, we make it easier to make those changes if they're ever required.

In your case, the only reason you'd need to change your code is if the underlying system code that you're calling via p/invoke changes (either because a new version of Windows changes  API in future, or if you decide to port your project to a different operating system).

From this point of view, your code is fine, although it may be worth (if you can ever foresee the possibility of an OS change) grouping this code together with any other system-dependent code, either in a subdirectory or in a library project.

The one change I would make is to encapsulate your return values in something more meaningful than a generic uint - as it stands, your clients need to understand the underlying Win32 implementation, which is a violation of the Law of Demeter. Create a specific return type that encapsulates the value returned by Windows.


问:
How do you test a selenium framework?
I have a very large selenium test framework I use to test a web application. It it built around a page-object pattern.

This week I fixed a bug in a low level piece of the framework, but it broke many other pieces that got built on top of this bug. I didn't have a good way to find these spots without running the full test suite of several thousand UI tests.

Since this isn't ideal, what strategy can I use to unit test new functionality and page-object modifications/additions to the framework?

答:
When you change the behaviour of a component in your framework, then just unit-testing that component is not sufficient. You must also re-run the integration- and higher-level tests that involve that component to verify what effect your changes have on the rest of the system.

If you find that the effect of your change is very large, the either you should reconsider the need for the change or bite the bullet and iteratively address the adverse effects.

Title: How do you test a selenium framework?


问:
Why does Scala name monadic composition as "for comprehension"?
Not sure if it's an appropriate question, but here it goes.

I know Haskell's do notation pretty well. And I realized that Scala's "for comprehension" really is just mostly the same as do notation in Haskell. Something I don't quite understand is why did the designers choose this name though? The words "for" and "yield" seem likely to confuse programmers who are used to, for example, Java's for loop and Ruby's yield.

Yet I don't think monadic composition has much in common with those things at all. Is there a particular reason to name this syntactic sugar with such words? Or is it just mostly because a lot of keywords have already been occupied in the language, so they had to use these keywords which are relatively free?

答:
I don't have much background on why Scala designers made that particular wording choice, but in F#, local "do notation" equivalent is called a "computation expression". The reasoning behind that word choice is one of PR and marketing rather than any concrete technical reason.

Haskell has a hard-earned reputation of being a language for academics, rocket scientists and other weirdos, and one of the things that perpetuates this notion is its community's insistence on using type-theory derived terms out in the open. This is also a form of marketing - you could say its a conscious choice that makes it appealing to certain kinds of people (functional programming hipsters) and off-putting to others (say, like enterprise adopters).

For F#, they went with "computation expressions" and "computation expression builders" instead of "do notation" and "monads" supposedly in an attempt to not scare away people who are likely to pick up the language (existing .NET programmers who don't have a functional programming background).

I would imagine the case is similar for Scala. By calling it "for comprehensions", they want to sell the feature to people who might know nothing of monads, but who are familiar with list comprehensions in languages like Ruby and Python.


答:
I think you are creating a bit of a false dichotomy here.

Haskell has monad comprehensions built into the language. One reason for that is the use of monads for imperative-style I/O. Therefore, the designers of Haskell decided to make it look mostly like a code block in a generic C-style language, complete with curly braces, semicolons and even return. The assignments look a bit non-C, but left-arrow for assignment is typical imperative pseudo-code.

So, monad comprehensions in Haskell were designed to look like imperative sequential side-effecting code blocks in some generic C/pseudo-code like language, because that's an important use case for them. This even permeates the API design with the naming of the aforementioned return function. Note that the name of this function doesn't really make much sense if you use it outside of do notation.

C# has monad comprehensions built into the language. One reason for that is the use of monads for querying of datasets. Therefore, the designers of C# decided to make it look mostly like SQL or XQuery, complete with FROM, SELECT, WHERE, GROUPBY, ORDERBY, etc. The order looks a bit non-SQL (FROM before SELECT), but it's the same one as used by XQuery, actually.

So, monad comprehensions in C# were designed to look like SQL queries, because that's an important use case for them. This even permeates the API design with the naming of e.g. Select for the transformation function (instead of map), SelectMany (instead of flatMap), Where (instead of select / filter), etc. Note that the names of these operations don't really make much sense if you use them outside of a query comprehension, and in fact, can even be actively confusing (Select is what is usually called map, whereas what is usually called select is Where).

Scala has monad comprehensions built into the language. One reason for that is the use of monads for collection operations. Therefore, the designers of Scala decided to make it look mostly like a for loop in a generic C-style language. The assignments look a bit non-C, but left-arrow for assignment is typical imperative pseudo-code.

So, monad comprehensions in Scala were designed to look like imperative for loops in some generic C/pseudo-code like language, because that's an important use case for them.

Note that both in the case of C# and Scala, the comprehension syntax can actually do more and/or less than just perform the two monadic operations join and bind (or map and flatMap). In Scala, a for comprehension without yield translates into foreach, i.e. an imperative side-effecting iteration, which really doesn't have much to do with monads at all. A yield can have a guard (yield foo if bar), which translates into a call to a call to withFilter, i.e. filtering elements. C# can do the same with Where. C# can also do aggregation (group by) and sorting (order by).

They are actually more of a generalization and/or fusion of monad comprehensions and list comprehensions generalized to arbitrary collections and monads. Note that Haskell also has both, but they are different things, in C# and Scala, they are fused together.

Haskell has a lot of history, and has pioneered a lot of concepts. As is typical with pioneers, often the people who come after them, discover better, shorter, safer routes. Maybe if Haskell had been designed with hindsight instead of innovation, they also would have generalized list comprehensions to work with more collections, and would have fused monad comprehensions and generalized collection comprehensions together, who knows? I know that there are proposed variants and extensions to Haskell, which add Arrow Comprehensions, for example.

The words "for" and "yield" seem likely to confuse programmers who are used to, for example, Java's for loop and Ruby's yield.

The first one is intentional. It is supposed to look like a for loop.

The second one is unfortunate. The word yield has two (actually related but not obviously so) meanings. One is the meaning used in concurrency, coroutines, fibres, threads, and for the yield keyword and the Fibre#yield method in Ruby, where it means that a piece of code yields control of execution to another piece of code. The other is the meaning of a computation yielding a result. That's the meaning that is used in Python generators, C# generators, and interestingly also in Ruby, in the Enumerator::Yielder#yield method, and is the interpretation that is meant for the yield keyword in Scala for comprehensions.

Title: Why does Scala name monadic composition as "for comprehension"?

Body:
Not sure if it's an appropriate question, but here it goes.

I know Haskell's do notation pretty well. And I realized that Scala's "for comprehension" really is just mostly the same as do notation in Haskell. Something I don't quite understand is why did the designers choose this name though? The words "for" and "yield" seem likely to confuse programmers who are used to, for example, Java's for loop and Ruby's yield.

Yet I don't think monadic composition has much in common with those things at all. Is there a particular reason to name this syntactic sugar with such words? Or is it just mostly because a lot of keywords have already been occupied in the language, so they had to use these keywords which are relatively free?


问:
Git Bisect found the buggy commit. Now what?
I've seen blog posts explaining how to do Git bisect, but how to you fix the bug, commit again and maintain the rest of the commit history?

An actual problem I faced in my project was like this (the image is just an approximation):

I had marked the top-most commit as bad and the bottom-most commit as good. Some of the commits below Buggy commit A were good commits, because somewhere in-between, I had commented out the code that made Bad commit B a bad commit. So naturally, git bisect led me first to Buggy commit B.

I corrected the bug and when I tried to commit, I wanted the changed lines of code to be applied to the same Buggy commit B commit. But I found out that:

Git would always create a new commit for this

I got the detached head error:

```<code>HEAD detached at b855e36 Changes not staged for commit:
modified:  main.cpp
</code>```

Now what do I do?

Create a new branch (let's name it fixedit) and commit this corrected code? If I do, then what happens to all the other commits above it? Should I do a rebase (never done it before) of the commit above Buggy commit B and put the entire line of commits onto the new fixedit branch?

I would prefer that I'd just be allowed to modify the existing commit and the entire tree would remain as it is, but then I'd have to go to all the commits above Buggy commit B and fix the buggy line in all those commits. That doesn't make sense. So how does one fix bugs and yet retain commit history properly?

UPDATE:

So I do a git bisect reset and fix the code on the topmost commit (because I know which line of Buggy commit B introduced the bug)?

Like I mentioned earlier, the code that caused Buggy commit B was commented out in one of the commits just before Buggy commit A.

So when in my topmost commit I see that the line is commented out, I realize that the bug is somewhere else.

During the first bisect since one of the commits between A and B was marked good, I'd choose that as the good commit for my second Git bisect attempt, and the topmost newest commit as the bad commit and then continue with Git bisect. That'd lead me to Buggy commit A.

Then I'd do a git bisect reset and correct the lines of code in the topmost code and then commit.

Ok; that makes sense. Thanks :-)

答:
You don't fix that commit. It's a fact that the bug was introduced, and that's okay. You found it now and know how to fix it, great! Like with any other bug fix, you create a new commit on top of the current state of the project. This retains the history and fixes the bug. Rewriting the history so that the bug never existed is pointless, even dangerous:

It doesn't have any benefit. I mean, you could pretend the bug never existed, but if you had to bisect then the bug probably existed for a while and was encountered by users. Rewriting git history doesn't undo that. It will just make it harder to understand what happened.

If the code at and around the location of the bug fix has changed in other ways since the bug was introduced (which seems to be the case), you can't just fix the bug in commit B and rebase all later commits on that. You would have to manually resolve conflicts, which takes time and risks introducing new bugs.

The usual concerns regarding rewriting history apply in force. Rebasing so many publicized commits will cause trouble for everyone who has work based on these commits (e.g. if they have a branch based on a commit after B, they won't be able to merge this branch easily). More generally, it breaks all references to specific commits (that came after B), since all hashes change.


问:
Library/API Runtime Between Versions
I was having a conversation with a friend about the C# StringBuilder class, and what it's behavior was.  I'll paraphrase, but my side of the conversation was something like this (I oversimplified because exactly how StringBuilder works isn't important for my question)

StringBuilder is more efficient to use for extensive string concatenation than to simply use +.  The reason is that StringBuilder doesn't dynamically create a new string for each concatenation operation.  It waits until all of your desired concatenations are "built" and only then does it dynamically allocate the space and give you back your string.

My friend said something along the lines of

That may be true today, but if you are reliant on such optimizations, then you should make your own StringBuilder.  In future versions, there's no reason that StringBuilder couldn't use simple string concatenation (str1 = str2 + str3).  Libraries only guarantee functional equivalence, not runtime equivalence.

Is this true for libraries?

If I use a library's Sort() function that has runtime O(n*log(n)), is it possible that a future version would change the runtime to O(n^2)?

Is the same true for executable tools?

Could (for example) grep's runtime fundamentally change in the future?

That aside, wouldn't it be good practice for a library/API/tool developer to keep runtimes for the same calls similar over time?

答:
...if you are reliant on such optimizations, then you should make your own ...

It's a bit of a tautology to say so, but code guarantees only what it guarantees.  One of the purposes of having libraries is to form a boundary that's opaque to callers, and the only time there should be a complaint is when one of the guarantees isn't being met.  (This is concept is at the core of design by contract.)

Your colleague is saying that if you become reliant on a particular implementation, you've established requirements that go beyond the original guarantees.  That doesn't mean you have to write your own, but it does you can't switch to another implementation until you've verified that it meets all of the requirements.

If I use a library's Sort() function that has runtime O(n*log(n)), is it possible that a future version would change the runtime to O(n^2)?

Sure.  I doubt anyone would do that intentionally unless someone said "I need you to make this code run slower."  A future version might perform better, which is something most folks like.  But, again, if you've dependent on the library to operate in a certain amount of time, a faster implementation won't meet your requirements and will break your software.

... wouldn't it be good practice for a library/API/tool developer to keep runtimes for the same calls similar over time?

Not really.  The reductio ad absurdum implication would be that implementations should be immutable because someone may have become dependent on the incorrect behavior brought about by a bug.  The only changes you could make would be the additions of functions that do completely new things or are modified versions of those that already exist (e.g. foo(), foo_version_2(), etc.).  There are a few very rare cases where that's desirable, but most times if you think you need this, you should probably think again.

Good practice for library developers is to document changes in a way that library users can understand what's changed.  Good practice for users of that library is to understand the changes and the effects on their own code before deploying them.  Bad practice is to drop in a new version of a library just because one has become available.

All of this applies to whole programs the same way it applies to libraries.  Most people expect that the -x switch in grep(1) will print only lines exactly matching the pattern.  A change in that behavior between versions will break things that depend on it and require the dependencies to change or that the old version remain in place.

Title: Library/API Runtime Between Versions


问:
Create/destroy multiple threads for animations/light effects
I'm developing a program for the Razer Deathadder and Firefly, I am going to create new light effects for the Firefly. I'm doing this in C# with the Colore library.

Now these light effects will be created using Threads with while(true) statements but how can I check and destroy threads and then reactivate them again when the user has pressed a button/radiobutton?

Right now the threads are either going over each other or getting deactivated and won't "turn on" again, is there a simpler way to do this?

I haven't tried anything other than creating functions to kill the threads but then they will not activate again.

This is how I have done it:

```<code>private void button1_Click(object sender, EventArgs e) {
    Thread effect = new Thread(new ThreadStart((Effect));
    effect.Start();
}

private void Effect() {
    while(true) {
       // lightning effect will go here
    }
}
</code>```

答:
I think that using threads for this is an overkill, especially since (I'm assuming) those threads will stay idle most of the time.

Instead, what you can do is to use async-await to be more efficient. If you really need to run the code on a separate thread from the current one, you can use Task.Run() for that.

To control whether the code is enabled or not, you don't need to stop the code, you can just pause it.

To achieve that, you can either periodically check whether the code is enabled:

```<code>while (true)
{
    if (isEffectEnabled)
    {
        // lightning effect will go here
    }
    else
    {
        await Task.Delay(someReasonableDelay);
    }
}
</code>```

Or you can use AsyncManualResetEvent from the AsyncEx library that lets you wait until the effect is enabled:

```<code>while (true)
{
    await isEffectEnabledEvent.WaitAsync();
    // lightning effect will go here
}
</code>```

Body:
I think that using threads for this is an overkill, especially since (I'm assuming) those threads will stay idle most of the time.

Instead, what you can do is to use async-await to be more efficient. If you really need to run the code on a separate thread from the current one, you can use Task.Run() for that.

To control whether the code is enabled or not, you don't need to stop the code, you can just pause it.

To achieve that, you can either periodically check whether the code is enabled:

while (true)
{
    if (isEffectEnabled)
    {
        // lightning effect will go here
    }
    else
    {
        await Task.Delay(someReasonableDelay);
    }
}

Or you can use AsyncManualResetEvent from the AsyncEx library that lets you wait until the effect is enabled:

while (true)
{
    await isEffectEnabledEvent.WaitAsync();
    // lightning effect will go here
}


问:
Is it effective to review code in language I don't know?
I'm an experienced developer, but have not done many code reviews. I'm being asked to review code written in Python but I do not know Python.

Does it make any sense at all to review code in a language I don't know?

答: I was once asked to audit a project that was being undertaken by a subcontractor and appeared to have serious performance problems. I fairly quickly established that the critical factor was a single Perl module. I had never come across Perl before and we had no-one in the organisation who knew it, so I set about trying to understand it myself. I never got as far as understanding the detail, but it was very clear that the algorithm it was using was quadratic in data size and this was the cause of all the trouble. So yes, reading code in a language you don't fully understand can definitely be productive. The bonus is that you learn new tricks while you're about it.</p>


问:
Methods of debugging code (Nightmare situation)
I am frequently tasked with debugging an application at my job. It is a BI Application that we deploy to businesses, which includes a test environment, and a production environment. I am wondering if there are any apps/tools/methods that people can suggest, based on these constraints:

Debugger cannot be used on client site or locally, because the software depends on custom third party applications that we don't have test environments for. (EDIT: to be fair, it is possible to debug locally in some cases. If we use nothing but the core code. Much of the problematic code resides in a dll that encapsulates third party specific communication: sockets, process pipes, soap calls, custom logic that changes the behavior of core code. Typically during an implementation or enhancement for a client, we would be writing new code to this area.)

There is virtually no logging done in our apps. There are no unit tests.

Version control only has 1 version of the full solution (using source safe 2005). So it's not possible to get a previous version of the entire solution, only individual files. (Unless someone knows ways around this).

Cannot reproduce locally, often times cannot reproduce on test environment (High chance that test and production are not the same version).

There is a high chance that the version the client is using is different from the one on source safe. This is because individual files are updated, which have embedded custom logic for that specific client. Often what happens is an update is made to a binary, which requires changes to several other binaries, but when a commit is done, no one has any record or knowledge of this. A somewhat common error I see is the 'Function/Method not found' or 'Method call has too many/too few parameters specified' on a clients environment.

This is a .net VB solution

Cannot install any software on client sites, but can locally

Our application is extremely customizable, but unfortunately the customization logic is spread out across all the classes and files, from the front end all the way to the data layer, including custom changes made to the database on a per client basis.

There are virtually no comments in the code. There is no documentation about the architecture. No documentation about the api. The only thing we have are hundreds upon hundreds of email chains that somewhat explain what's going on. The only people that know the code are the ones that originally wrote it, but they're no longer developers per say so they don't get involved that much.

And before you say it... yes I know; I want to shoot myself as well. It doesn't help that there's spaghetti code, hundreds of compiler warnings, and broken polymorphism that REALLY should be fixed, but I don't have a say in it.

Most common sort of errors I run into are null reference errors, invalid casts, and missing functions/function signature mismatches. Sometimes I am lucky and event viewer will log the class, method, and exception message. It's not the most helpful, but it's still something. The worst are the errors that have no trace, no repro steps besides a screenshot, and are generic error messages like the ones mentioned above. Sometimes it's not possible to find out why they occurred, only to pray that the environment is not properly configured, and that it will go away later.

I know this comes off as a bit of a rant, and to some extent it is. But I'm desperate for options. Are there other methods / tools I can use?

答:
I would say (5) is the one you need to fix first. If you don't know which code is running in production, you have no safe way of reproducing and fixing problems. This makes any other change you introduce dangerous, since it might cause problems you cannot foresee and cannot reproduce.

You may need to do some detective work and perhaps reverse engineering to figure out which version(s) of code and which libraries is deployed. (And you need a have a consistent build and deploy system to bring all deployed code in line with source control going forward.)

You might have to build multiple test environments to replicate the various deployments. (Of course the simplest fix is to create a new clean build and deploy it consistently everywhere, but it sounds like this is not possible?)

Only when you know the exact versions deployed and have corresponding test environments, should you start trying to fix/improve the code.

Your next priority should be to consolidate to a single code base which can be deployed everywhere. It sounds like you have various versions of code deployed due to customization? You should consolidate to a single version and then use configuration switches for the custom logic.

After this, you can start to carefully improve to code base to allow easier debugging. Adding logging is probably the least risky improvements.

You will want to add automated tests, but unittests are often hard to add to a project which is not initially designed for testing. Instead I recommend starting out with automated end-to-end integration tests. These are more tricky to set up, but does not require you to re-architect the solution, so are less risky.


答: You will need to run all tests eventually as you're making a fundamental change. However you don't have to run all tests at once. Rather then fixing the bug, you could deprecate the bug causing functionality, create a new and correct bit of functionality which over time replaces the old functionality.</p>

Title: How do you test a selenium framework?

Body:
I have a very large selenium test framework I use to test a web application. It it built around a page-object pattern.

This week I fixed a bug in a low level piece of the framework, but it broke many other pieces that got built on top of this bug. I didn't have a good way to find these spots without running the full test suite of several thousand UI tests.

Since this isn't ideal, what strategy can I use to unit test new functionality and page-object modifications/additions to the framework?


问:
How to scale a C# server application
I'm writing a pretty simple TCP server/client application for the first time. It is a personal project for education, but I really like my applications to be extendable and to scale in case the code is good enough to use somewhere else.

I am using Microsofts async TCP server/client as my starting point
https://msdn.microsoft.com/en-us/library/fx6588te(v=vs.110).aspx

My end goal is to be able to have some kind of ServerManager application that the client first connects to, and is then redirected based on that servers capacity.

My question is: Do the clients get redirected to a different physical server, or does the ServerManager spawn another instance of TCPServer on the same machine and connect them there (not even sure how that would work)?

答:
Well it kind of depends on what resources you already have. For instance if the application uses all the cores on the computer, simply spinning up another instance of the application won't necessarily mean that requests get handled faster because the two applications would be sharing the resources on the server.

The important thing to remember here is that generally it doesn't make much sense to spin up another instance of the application which would end up using the same resources as the first instance (for example if they try to access the same hard drive at the same time, the operating system has to keep switching access to the hard drive between the two applications).

Most multi-server environments use load balancers to automatically distribute requests between physical machines (see the link in JeffO's answer or https://azure.microsoft.com/en-gb/documentation/articles/load-balancer-overview/) so one of the things you have to keep in mind when programming for scalability, is that one client might not always have every request handled by the same server.

Title: How to scale a C# server application


问:
Packages : opposing forces of reusability and developability

In choosing the classes to group together into packages, we must consider the opposing forces involved in reusability and
  developability. Balancing these forces with the needs of the
  application is nontrivial. Moreover, the balance is almost always
  dynamic. That is, the  partitioning that is appropriate today might
  not be appropriate next year. Thus, the composition of the packages
  will likely jitter and evolve with time as the focus of the project
  changes from developability to reusability.

From : Agile software development : PPP

What are these opposing forces of reusability and developability?

答:
It is about getting your dependencies straight. If you need some new functionality, you always have a choice to either keep it private to your problem domain code or put it in a package with general support stuff that is accessible to all. If you keep it private, it is yours. You can change, fix, extend it whenever you like and you can be sure you will not break any code you do not know about. That is very "developable" but it is not reusable. If you need the same thing in your next project, you will have to copy the code to the package you are working on at that moment. Or decide to move the code to the general support stuff package after all, which would be an example of the evolving composition the book talks about.

Once code is in a reusable library, you will have lost some developability. You will have to be very careful when changing anything about it because you may break a client. If there is an error in it, you may not even be able to fix it because client code may depend on it. It has become "legacy".

Sometimes being able to change/fix something in a central spot, knowing it will effect everything at once, can be great. And sometimes it can be a curse. Making the call on the way to go is the non-trivial task your book is talking about.


问:
Finding similar high dimensional vectors
I have two lists (sizes m and n) containing high dimensional bit-vectors. All vectors have the same number of dimensions and use Hamming distance as measure if distance.

For each element in the first list I want to find the closest elements in the second list. Such a closest element may differ by several thousand bits from the element I'm searching for.

The naive approach would be computing the hamming distance for each pair of vectors, but that has runtime O(m*n) making it infeasible. So I'm looking for an algorithm that's significantly faster.

Lets say I have d=10000, m=1 billion and n=100 billion and I want the algorithm to terminate in a couple of CPU days.

The elements in the first list are created by taking a random element from the second list and flipping each bit with the same probability p < 0.5. I want to support values of p that are as close as possible to 0.5. I'm fine with probabilistic algorithms that find matches with high probability.

答:
Just an intuition. I haven't gone through the details and may be embarrassingly mistaken.

As your vectors all have the same dimensionality d and their distance is defined as their hamming distance you can represent each vector as a point on a d-dimensional hyper cube. The path between two points will be the hamming distance of their coordinates.

All points in n are marked. This takes O(n) time. Then for each point in m you execute a breadth first search for any marked node. This will take O(m * (|V|+|E|)) time. Altogether O(n + m * (|V|+|E|)) time. However as the number of vertices and edges are derived from d and d is a constant we are left with O(n + m).

You could bring this constant factor down by applying a more efficient search algorithm. Eg: Efficiently find binary strings with low Hamming distance in large set


问:
How are you using the Managed Extensibility Framework?
I have been working with MEF for about 2 weeks. I started thinking about what MEF is for, researching to find out how to use MEF, and finally implementing a Host with 3 modules. The contracts are proving to be easy to grasp and the modules are easily managed.

Although MEF has a very practical use, I am wondering to what extent? I mean, is everyone going to be rewriting existing applications for extensibility?

Yes, that sounds, and is insanely impractical. Rhetorically speaking:

how is MEF affecting the current
trends in programming?

have you begun looking for
opportunities to use MEF?

have you begun planning a major
re-write of an existing app that may
benefit from extensibility?

That said, my questions are:

how do I know when I should plan a new project with extensibility?

how will I know if an existing project needs to be re-written for extensibility?

Is anyone using MEF?

答:
Currently I'm using MEF in a few projects that incorporate the Repository pattern.

One is using different repository types while unit testing, the other project is using local (straight to DB), remote (WCF) and testing repositories.

Both projects can change their repository types via code or configuration, and is based on  MEF metadata/ Lazy types.

To date, both projects are running incredibly well.


问:
C# / VB.NET build expression trees only from lambda expressions -- why?
Based on the context, C# can generate the expression tree for a LambdaExpression from lambda expression syntax:

Expression<Func<string, int>> expr1 = s => s.Length;
```

<p>as can VB.NET:</p>

<pre class="lang-vb prettyprint-override"><code>Dim expr1 As Expression(Of Func(Of String,Integer)) = Function(s) s.Length
</code>```

Why can't either language compiler generate an expression tree from other expression types?

C#:

Expression<DateTime> expr2 = DateTime.Now
```

<p>VB.NET:</p>

<pre class="lang-vb prettyprint-override"><code>Dim expr2 As Expression(Of DateTime) = DateTime.Now
</code>```

I am assuming this behavior is either by design; or there are technical reasons that make this unfeasable; or it is unnecessary for the requirements that made Expressions necessary in the first place -- LINQ queries. I would like further details on the subject.

答: We already know that the expression tree generation mechanism is able to generate an expression tree for any arbitrary expression, because it's able to generate an expression tree for any arbitrary expression within a lambda.  So it's clear that technical limitations are not the problem.  The most likely answer is that it was used only set up for automatic transformations of lambdas to expression trees because that was the only thing that LINQ needed it for, and expression trees were created for LINQ.</p>


答:
In my opinion, the main reason this feature is not available due to the language designers not being required to implement it or not seeing point in doing that since this can be already achieved with lambdas in a sane manner.

The feature is most probably technically possible, since all expressions can be written as lambdas

The adoption would mean that each expression can either generate expression tree or be evaluated

Can lead to decrease in code readability due to big semantic difference between T and Expression<T> in comparison with Func<T> and Expression<Func<T>>

The ambiguity already exists for lambdas

Resolved automatically in VB.NET by defaulting to delegate

Not resolved automatically in C#, presumably the designers wanted the programmers to disambiguate between the options themselves

Therefore, this feature would go against previous design decisions, either defaulting to a delegate or by removing anonymous types.

Title: C# / VB.NET build expression trees only from lambda expressions -- why?

Body:
Based on the context, C# can generate the expression tree for a LambdaExpression from lambda expression syntax:

Expression<Func<string, int>> expr1 = s => s.Length;
```

<p>as can VB.NET:</p>

<pre class="lang-vb prettyprint-override"><code>Dim expr1 As Expression(Of Func(Of String,Integer)) = Function(s) s.Length
</code>```

Why can't either language compiler generate an expression tree from other expression types?

C#:

Expression<DateTime> expr2 = DateTime.Now
```

<p>VB.NET:</p>

<pre class="lang-vb prettyprint-override"><code>Dim expr2 As Expression(Of DateTime) = DateTime.Now
</code>```

I am assuming this behavior is either by design; or there are technical reasons that make this unfeasable; or it is unnecessary for the requirements that made Expressions necessary in the first place -- LINQ queries. I would like further details on the subject.