问:
C# static practices coming from dynamic background
I have been dappling in C# after coming from several years in PHP.
I don't find the language particularly difficult, although there are considerably more constructs for me to get used to.

My question is to help me change way of thinking when using C#.  More specifically, to help me think of solutions in a static language when I've been spoiled by a dynamic one.

For my experiment I have created a simple copy of Symfony's EventDispatcher, which is used as so:

```<code>// PHP
class Sample
{
    public function __construct()
    {
        $dispatcher = new EventDispatcher();
        $dispatcher->addListener("sample.event", array($this, "onEvent"));
        $dispatcher->dispatch   ("sample.event", new Event());
    }

    public function onEvent(Event $event)
    {
        echo "Event Dispatched";
    }
}
</code>```

And this is my C# replica of EventDispatcher:

```<code>// C#
public delegate void EventListener (EventArgs args);

public class EventDispatcher
{
    protected Dictionary<string, EventListener> Listeners = new Dictionary<string, EventListener> ();

    public void AddListener (string eventName, EventListener listener)
    {
        EventListener l;
        Listeners.TryGetValue (eventName, out l);

        if (null == l) {
            l = listener;
            Listeners.Add (eventName, l);
        } else {
            l += listener;
        }
    }

    public void Dispatch(string eventName, EventArgs args)
    {
        EventListener listener;
        Listeners.TryGetValue (eventName, out listener);

        if (null != listener) {
            listener (args);
        }
    }
}
</code>```

And finally this is a copy of the PHP example:

```<code>// C#
class Application
{
    protected static EventDispatcher Dispatcher = new EventDispatcher();

    public static void Main(string[] args)
    {
        Dispatcher.AddListener ("sample.event", onEvent);
        Dispatcher.Dispatch    ("sample.event", new ErrorEventArgs(new Exception()));
    }

    public static void onEvent(EventArgs args)
    {
        Console.Out.WriteLine ("Event Dispatched");
    }
}
</code>```

Now the difference here is I am dispatching the event with ErrorEventArgs, a child class of EventArgs.  This is fine because the instance is substituted.

However, in PHP I can declare the listener as this to make certain the correct event is given to the method:

```<code>// PHP
public function onEvent(ErrorEvent $event)
{
    echo "Event Dispatched";
}
</code>```

However the same in C# will not work:

```<code>// C#
public static void onEvent(ErrorEventArgs args)
{
    Console.Out.WriteLine ("Event Dispatched");
}
</code>```

Because it does not match the delegate delegate void EventListener (EventArgs args).

Now I know .NET already has an event system and it looks like I am reinventing the wheel, also if you are familiar with Symfony2 you will know that it's event dispatcher is not a typical example of itself (events are usually globalized).

I could in this case only define the methods to conform with the required delegate and use introspection to determine whether the event should be handled.  Or I can implement AddListener and Dispatch as generic methods, at the cost of defining the dictionary as Dictionary<string, object>.

So my question boils down to whether I am approaching this the wrong way, whether this type of semi-dynamic programming is suitable, or is there a truely static way of implementing this?

Please bare in mind that this is an example experiment and in the real world I would look for traditional practices to implement this specific scenario.

EDIT:

Using the generics and object dictionary solution, here is the refactor:

```<code>public delegate void EventListener<T> (T args);

public class EventDispatcher
{
    protected Dictionary<string, object> Listeners = new Dictionary<string, object> ();

    public void AddListener<T> (string eventName, EventListener<T> listener)
    {
        EventListener<T> l;
        object obj;

        if (Listeners.TryGetValue (eventName, out obj)) {
            l = (EventListener<T>)obj; // handling required here if obj cannot be casted.
            l += listener;
        } else {
            l = listener;
            Listeners.Add (eventName, l);
        }
    }

    public void Dispatch<T> (string eventName, T args)
    {
        EventListener<T> l;
        object obj;

        if (Listeners.TryGetValue (eventName, out obj)) {
            l = (EventListener<T>)obj;
            l (args);
        }
    }
}
</code>```

答:
Let me ask a question : what you happen if in your PHP version I would write:

```<code>// PHP
class Sample
{
    public function __construct()
    {
        $dispatcher = new EventDispatcher();
        $dispatcher->addListener("sample.event", array($this, "onEvent"));
        $dispatcher->dispatch   ("sample.event", new WarnEvent()); // oops?
    }

    public function onEvent(ErrorEvent $event)
    {
        echo "Event Dispatched";
    }
}
</code>```

I'm not experienced with PHP, but this would probably cause some kind of error behavior. In best case, you can handle it, in worst case, you are going to spend days trying to hunt down the error.

C#'s static type system simply doesn't allow to even write code like that for above reasons. If you really don't mind this kind of behavior, you can write:

```<code>public static void onEvent(EventArgs args)
{
    if (!(args is ErrorEventArgs))
    {
        // something really wrong just happened
    }
    Console.Out.WriteLine ("Event Dispatched");
}
</code>```

And about your suggestion of using generics + object. I thinks this might be a good idea. As long as the object is fully encapsulated inside the Dispatcher.

Body:
Let me ask a question : what you happen if in your PHP version I would write:

// PHP
class Sample
{
    public function __construct()
    {
        $dispatcher = new EventDispatcher();
        $dispatcher->addListener("sample.event", array($this, "onEvent"));
        $dispatcher->dispatch   ("sample.event", new WarnEvent()); // oops?
    }

    public function onEvent(ErrorEvent $event)
    {
        echo "Event Dispatched";
    }
}

I'm not experienced with PHP, but this would probably cause some kind of error behavior. In best case, you can handle it, in worst case, you are going to spend days trying to hunt down the error.

C#'s static type system simply doesn't allow to even write code like that for above reasons. If you really don't mind this kind of behavior, you can write:

public static void onEvent(EventArgs args)
{
    if (!(args is ErrorEventArgs))
    {
        // something really wrong just happened
    }
    Console.Out.WriteLine ("Event Dispatched");
}

And about your suggestion of using generics + object. I thinks this might be a good idea. As long as the object is fully encapsulated inside the Dispatcher.


问:
Does agile contradict to Open/Closed principle?
O/C principle dictates that production code should not be changed if system behavior augmention is required. Otherwise it becomes hard to trust tests.

Following it as a dogma we are going to end with either deep inheritance trees or aggregations.

On the other hand we never tried this approach so the former statement is more of an assumption. We ignore O/C principle completely what expectedly brings us a lot of pain.
But augmention frequently comes along with interface changes so the design gets 'shaked' from iteration to iteration and I hardly imagine how it is even possible to adhere to O/C principle.

Shouldn't some notion of feature completeness exist only after achieving which code changes should be discouraged?

答:
The open/closed principle is a tricky one, and I think it's often misunderstood. In the strict sense, obviously it's impossible to add behaviour to an application without modifying its code, but what we really mean by open/closed is that it should be possible to extend the functionality of a given class without changing that class. Let me try to come up with a simple contrived example. Let's say you have an Order class, which has a price method that sums the prices of all the items in the order:

```<code>class Order
  def price
    items.inject(0) { |total, item| total += item.price }
  end
end
</code>```

Now, say you have a new requirement that some orders will be taxable (for customers in the same state, e.g.) and others won't be. Regardless of whether you want to adhere to O/C, you're going to have to change the Order class. That's just inevitable. But how do you do it? The obvious approach is a conditional:

```<code>class Order
  def price
    base_price = items.inject(0) { |total, item| total += item.price }
    if taxable?
      base_price * 1.1
    else
      base_price
    end
  end
end
</code>```

However, the thing to realise here is that the next time someone requests a change to how order prices are calculated, you will still have to make another change to this class. And as those changes stack on top of each other things get out of hand. So what open/closed says is that you should refactor the existing functionality in such a way that it becomes possible to make the change without altering Order.

There are a million ways of doing this, but for the sake of argument let's say you want to employ a sort of strategy pattern: your Order depends on one of many PriceCalculator subclasses to compute the price, and the calculator is injected when you instantiate an Order. Now, when you inevitably get the request to add a shipping fee to some orders (but not others), you can swap out the PriceCalculator with a PriceCalculatorWithShipping (or whatever), and not have to touch Order.

So to sum up, I'd say open/closed isn't discouraging code changes, it's just trying to make sure those changes happen in as few places as possible, and that you have fewer reasons to make extensive modifications to existing classes. I'd argue that in the medium term this makes applications much easier to change, and so is entirely compatible with the agile philosophy.

For a really great longer-form exposition of these ideas I'd recommend this talk by Sandi Metz: https://www.youtube.com/watch?v=8bZh5LMaSmE.

OwnerDisplayName: voroninp

ContentLicense: CC BY-SA 3.0

Body:
The open/closed principle is a tricky one, and I think it's often misunderstood. In the strict sense, obviously it's impossible to add behaviour to an application without modifying its code, but what we really mean by open/closed is that it should be possible to extend the functionality of a given class without changing that class. Let me try to come up with a simple contrived example. Let's say you have an Order class, which has a price method that sums the prices of all the items in the order:

class Order
  def price
    items.inject(0) { |total, item| total += item.price }
  end
end

Now, say you have a new requirement that some orders will be taxable (for customers in the same state, e.g.) and others won't be. Regardless of whether you want to adhere to O/C, you're going to have to change the Order class. That's just inevitable. But how do you do it? The obvious approach is a conditional:

class Order
  def price
    base_price = items.inject(0) { |total, item| total += item.price }
    if taxable?
      base_price * 1.1
    else
      base_price
    end
  end
end

However, the thing to realise here is that the next time someone requests a change to how order prices are calculated, you will still have to make another change to this class. And as those changes stack on top of each other things get out of hand. So what open/closed says is that you should refactor the existing functionality in such a way that it becomes possible to make the change without altering Order.

There are a million ways of doing this, but for the sake of argument let's say you want to employ a sort of strategy pattern: your Order depends on one of many PriceCalculator subclasses to compute the price, and the calculator is injected when you instantiate an Order. Now, when you inevitably get the request to add a shipping fee to some orders (but not others), you can swap out the PriceCalculator with a PriceCalculatorWithShipping (or whatever), and not have to touch Order.

So to sum up, I'd say open/closed isn't discouraging code changes, it's just trying to make sure those changes happen in as few places as possible, and that you have fewer reasons to make extensive modifications to existing classes. I'd argue that in the medium term this makes applications much easier to change, and so is entirely compatible with the agile philosophy.

For a really great longer-form exposition of these ideas I'd recommend this talk by Sandi Metz: https://www.youtube.com/watch?v=8bZh5LMaSmE.

OwnerDisplayName: Louis Simoneau


问:
How to enforce good/better source code control practices?
I suspect that I'm focusing on the wrong problem so I will first describe what I think the problem is before presenting the possibly suboptimal solution I envision.

Current Situation:

Currently my co-workers commit their code changes only after loooong periods of time in huge chunks with changes spreading all over the project(s). That's progress I guess, because not long ago they just put .zip archives on some network share. Still, merging is a nightmare - and frankly I've had enough of it. And I'm also tired of talking and explaining and begging. This just has to stop - without me constantly being "the bad guy".

My solution:

Since there seems to be no awareness of and/or no interest in the problems and I cannot expect any efforts to last longer than a few days ...strike that, hours, I'd like the subversion server to do the nagging.

My question:

Am I way off-base here or am I looking at the wrong problem?  It seems like I'm missing something, and I think I'm asking the wrong thing by looking at tools to solve my problem.

Should I be looking for a tool to solve this problem or what is it I should do to fix this?

答:
You're looking for a technical solution to a human problem. That rarely works.

The reason for that is because if team members do not accept something (nor understand the implications), instead of following the rules, they'll attempt to circumvent them. That's exactly why, for example, developers should accept and understand style rules instead of just being forced to comply by a checker.

Here are a few approaches I either used in the past or have in mind without actually having an opportunity to them in practice. Some may not apply to your case, depending on the position you have in a team (if you're a team lead with excellent reputation, chances are you'll have a better opportunity to enforce your view than if you're an undergraduate who just joined a team for the duration of your internship).

Discuss the issue with your coworkers and explain the consequences of large commits. Maybe they simply don't understand that complicated merges are a direct consequence of rare commits, and than small, frequent commits will make merges (relatively) easy.

I knew a lot of programmers who were simply convinced that merges are always complicated. They did at most one commit per day, avoided to use powerful tools such as Visual Studio's diff and auto-merge, and had a poor practice of manual merging (unless “Take mine” with no further diff inspection is actually a good practice). For them, this had nothing to do with them, and was the inherent nature of a merge.

Give concrete examples of what is happening in other companies (especially the ones your coworkers have a deep respect for). They may simply be unaware that it is an issue, and be convinced that a maximum one commit per day is what every team does.

Some people are unaware that there are teams of 5-10 members who make up to 50 pushes to production, which translates into an average of 5-10 commits per day per person. They may not understand neither how is it possible, nor why would anyone do it.

Lead by example. Do enough small commits yourself. If possible, do a short presentation showing their and your merges side by side over a week (I'm not sure if extracting this sort of information is easy from a version control). Emphasize on the eventual mistakes they've done during their merges and compare it to the number of errors you've done (which should be close to zero).

Use the “I told you” technique, when appropriate. When you see your colleagues suffer over a painful merge, loudly comment that small, frequent commits could make merges (relatively) painless.

Explain that there is no minimum duration to make a commit. A commit may even correspond to a minor change made in a few seconds. Renaming a file, removing an obsolete comment, correcting a typo are all tasks which can be committed immediately.

Programmers shouldn't fear doing a tiny commit, but rather aggregating many changes into one huge commit.

Work with individuals instead of a team, when appropriate. If there is a person who particularly refuses to do frequent, small commits, talk with this person individually to see why is he refusing it.

They may give perfectly valid reasons which may give you the hint about what's going on with a team. Some reasons I've heard myself:

“My teacher/mentor told me that the best practice is to do one commit per day.” It doesn't surprise me, given what I had to hear from my teachers in college.

“My colleagues told me that I should make less commits.” I've been told that too in some teams, and I understand their point. We had a log which was practically filled with my commits (not hard to do when four teammates don't even do one commit per day), which frustrated my coworkers.

“I thought small commits make it difficult to find a revision.” Somehow valid point, even when the team puts an effort into writing descriptive log messages.

“I don't want to waste too much space on our version control server.” The person obviously doesn't understand how commits are stored (nor how cheap the storage space is).

“I think a commit should correspond to a specific task.” Given that often, a task corresponds to some work to be done in one day (such as in visual management's task boards), this is not a coincidence. The person should then learn to make a difference between a task in a backlog (2 to 8 hours of work) and a logically isolated change which should be committed (a few seconds to a few hours of work). This is also related to point 5.

Search for the reason the team is not doing commits more frequently. You may be surprised by the results.

Recently, I mentioned in a different answer that speed of a commit matters, and even hundreds of milliseconds may push developers to commit on less frequent basis.

Other reasons may include:

Overly complicated rules to write a commit message.

The rule which forces the developer to link the commit to a task from a bug tracking system.

The fear of breaking the build.

The unwillingness to deal with a risk of breaking the build right now: if you do a commit on Friday evening just before leaving, you can postpone dealing with broken build until Monday.

The fear of doing a merge.

Determine whether developers understand that there are other benefits to commit often. For example, a Continuous Integration platform is a huge incentive to have frequent commits, since it allows to pinpoint precisely where the regression was introduced.

I would rather prefer CI platform telling me that I broke the build in the revision 5023 which consists of a change in two methods in one file I did fifteen minutes ago, rather than in the revision 5023 consisting of changes which span four dozen files and represent 13 hours of work.


问:
What is the value of workflow tools?
I'm new to Workflow developement, and I don't think I'm really getting the "big picture". Or perhaps to put it differently, these tools don't currently "click" in my head.

So it seems that companies like to create business drawings to describe processes, and at some point someone decided that they could use a state machine like program to actually control processes from a line and boxes like diagram. Ten years later, these tools are huge, extremely complicated (my company is currently playing around with WebSphere, and I've attended some of the training, its a monster, even the so called "minimalist" versions of these workflow tools like Activiti are huge and complicated although not nearly as complicated as the beast that is WebSphere afaict).

What is the great benefit in doing it this way? I can kind of understand the simple lines and boxes diagrams being useful, but these things, as far as I can tell, are visual programming languages at this point, complete with conditionals and loops. Programmers here appear to be doing a significant amount of work in the lines and boxes layer, which to me just looks like a really crappy, really basic visual programming language.

If you're going to go that far, why not just use some sort of scripting language? Have people thrown the baby out with the bathwater on this? Has the lines and boxes thing been taken to an absurd level, or am I just not understanding the value in all this?

I'd really like to see arguments in defense of this by people that have worked with this technology and understand why its useful. I don't see the value in it, but I recognize that I'm new to this as well and may not quite get it yet.

答:
The Value

The value proposition is that workflows can be created or altered quickly and easily to suit the changing nature of a business. An important part of realising this value proposition, is that business processes themselves are units of resource in the system.

Workflows as units of resource, means that a business process is defined as a single 'unit'. To understand what I mean by this, consider any computer program written for a business. It implements a business process for sure, but the logic for that process is likely to be spread around the source code to a certain extent. A workflow tool should allow the process to be defined in one well contained place. It could be in one single config file, or extracted from one visual diagram, or it could mean that the workflow is in a single module of code that can be plugged in, perhaps even swapped or configured on the fly.

Why the Value may not be realised

This value proposition can be undermined by the difficulties of covering non-vanilla cases, integrating with UI technology which changes very rapidly, bad practices such as using the workflow tool as a wrapper only and putting the real logic in code anyway.

Poor design of the tools themselves may be a limiting factor. An example would be a tool that requires all parameters passed between processes to be in a Java Map, with restrictions on what values you can actually put on the map, instead of just plain old method parameters (I'm thinking of one of the more popular tools in particular that does this).

I think it is probably fair to say that IBM as a big player with a large technology eco-system, fairs better than the others. If they also control the UI technology, and database and SOA technology that is used in conjunction with the workflow tool, they stand a better chance of coming up with an eco-system that integrates together nicely, and creates an opportunity to really make use of this idea.

It is definitely true that the effort of writing the interfacing between a workflow tool and the other parts of a system can completely negate the whole value proposition. It is always worth considering if there is a better way of doing things.

Programming is Workflow

The truth is that programming (in imperative languages at least) IS WORKFLOW already. You may have code that implements workflow which is to do with handling the system technology; accessing files and running SQL queries and so on. You may have code that implements business workflow; setting the state of a document and passing it to a reviewer for example.

Recognizing this and designing your code to cleanly split these separate concerns is hard to pull off completely, but you can certainly get a long way with doing just that.

I agree with you, sometimes we end up using these tools because someone else decided it was a good idea, and they are just too complex and making our job harder. I don't think that is always the case, it needs some careful consideration to decide if its worth it or not.

Title: What is the value of workflow tools?


答:
"O/C principle dictates that production code should not be changed if system behavior augmention is required."

My understanding of O/C was not that a programmer should not modify a class over time (there's no fundamental risk to tests as we can and should always update our tests alongside any changes to functionality), but that classes shouldn't expose enough of their internal workings that other code can effectively modify their behaviour. That is, if I create a class Foo with a complete set of unit tests, it should not be possible to create a class DerivedFoo such that if I use it in a Foo test, that test will fail.

Title: Does agile contradict to Open/Closed principle?

Body:
O/C principle dictates that production code should not be changed if system behavior augmention is required. Otherwise it becomes hard to trust tests.

Following it as a dogma we are going to end with either deep inheritance trees or aggregations.

On the other hand we never tried this approach so the former statement is more of an assumption. We ignore O/C principle completely what expectedly brings us a lot of pain.
But augmention frequently comes along with interface changes so the design gets 'shaked' from iteration to iteration and I hardly imagine how it is even possible to adhere to O/C principle.

Shouldn't some notion of feature completeness exist only after achieving which code changes should be discouraged?


问:
Maintenance cost of SIMD programming code base

Question:

The software industry's consensus is that clean and simple code is fundamental to the long-term viability of the code base and the organization that owns it. These properties lead to lower maintenance costs and increased likelihood of the code base being continued.

However, SIMD code is different than general application code, and I would like to know if there is a similar consensus regarding clean and simple code applying specifically to SIMD code.

Background to my question.

I write plenty of SIMD (single-instruction, multiple data) code for various image processing and analysis tasks. Recently I also had to port a small number of these functions from one architecture (SSE2) to another (ARM NEON).

The code is written for shrink-wrapped software, therefore it cannot depend on proprietary languages without unrestricted redistribution rights such as MATLAB.

An example of typical code structure:

Using OpenCV's matrix type (Mat) for all memory, buffer and lifetime management.

After checking the size (dimensions) of input arguments, the pointers to the start address of each row of pixels is taken.

The pixel count, and the start addresses of each row of pixels from each input matrix is passed into some low-level C++ functions.

These low-level C++ functions use SIMD intrinsics (for Intel Architecture, and ARM NEON), loading from and saving to raw pointer addresses.

Characteristics of these low-level C++ functions:

Exclusively one-dimensional (consecutive in memory)

Does not deal with memory allocations.
(Every allocation, including temporaries, are handled by the outer code using OpenCV facilities.)

The range of name lengths of symbols (intrinsics, variable names, etc) are roughly 10 - 20 characters, which is quite excessive.
(Reads like techno-babble.)

Reuse of SIMD variables is discouraged because compilers are quite buggy in correctly parsing code that is not written in the "single-assignment" coding style.
(I've filed several compiler bug reports.)

What aspects of SIMD programming would cause the discussion to differ from the general case?  Or, why is SIMD different?

In terms of initial development cost

It is well-known that the initial development cost of C++ SIMD code with good performance is about 10x - 100x (with a wide margin) compared to casually-written C++ code.

As noted in the answers to Choosing between performance vs readable/cleaner code? , most code (including casually-written code and SIMD code) is initially neither clean nor fast.

Evolutionary improvements in code performance (in both scalar and SIMD code) is discouraged (because it is seen as a kind of software rework), and the cost and benefit is not tracked.

In terms of propensity

(e.g. the Pareto principle, a.k.a. the 80-20 rule)

Even if image processing only comprises 20% of a software system (in both code size and functionality), image processing is comparatively slow (when viewed as a percentage of CPU time spent), taking more than 80% of time.

This is due to the data size effect: A typical image size is measured in megabytes, whereas the typical size of non-image data is measured in kilobytes.

Within the image processing code, a SIMD programmer is trained to automatically recognize the 20% code comprising the hotspots by identifying the loop structure in the C++ code. Thus, from a SIMD programmer's perspective, 100% of the "code that matters" is performance bottleneck.

Often in an image processing system, multiple hotspots exist and take up comparable proportions of time. For example, there may be 5 hotspots each taking up (20%, 18%, 16%, 14%, 12%) of total time. To achieve a high performance gain, all of the hotspots need to be rewritten in SIMD.

This is summarized as the balloon-popping rule: a balloon cannot be popped twice.

Suppose there are some balloons, say 5 of them. The only way to decimate them is to pop them one by one.

Once the first balloon is popped, the remaining 4 balloons now comprises a higher percentage of total execution time.

To make further gains, one must then pop another balloon.
(This is in defiance to the 80-20 rule of optimization: a good economical outcome can be achieved after the 20% of lowest-hanging fruits has been picked.)

In terms of readability and maintenance

SIMD code is patently hard to read.

This is true even if one follows every software engineering best-practice e.g. naming, encapsulation, const-correctness (and making side-effects obvious), function decomposition, etc.

This is true even for experienced SIMD programmers.

Optimal SIMD code is very contorted, (see remark) compared to its equivalent C++ prototype code.

There are many ways to contort SIMD code, but only 1 out of 10 such attempts will attain acceptably fast results.

(That is, in the tunes of 4x-10x performance gains in order to justify to high development cost. Even higher gains have been observed in practice.)

(Remark)

This is the main thesis of the MIT Halide project - quoting the paper's title verbatim:

"decoupling algorithms from schedules for easy optimization of image processing pipelines"

In terms of forward applicability

SIMD code is strictly tied to a single architecture. Each new architecture (or each widening of SIMD registers) requires a rewrite.

Unlike the majority of software development, each piece of SIMD code is typically written for a single purpose that never changes.
(With the exception of porting to other architectures.)

Some architectures maintain perfect backward compatibility (Intel); some fall short by a trivial amount (ARM AArch64, replacing vtbl with vtblq) but which is sufficient to cause some code to fail to compile.

In terms of skills and training

It is not clear what knowledge prerequisites are required to properly train a new programmer to write and maintain SIMD code.

College graduates who have learned SIMD programming in school seems to despise and dismiss it as an impractical career track.

Disassembly-reading and low-level performance profiling are cited as two fundamental skills for writing high-performance SIMD code. However, it is unclear how to systematically train programmers in these two skills.

Modern CPU architecture (which diverges significantly from what is taught in textbooks) makes training even more difficult.

In terms of correctness and defect-related costs

A single SIMD processing function is actually cohesive enough that one can establish correctness by:

Applying formal methods (with pen-and-paper), and

Verifying output integer ranges (with prototype code and performed outside run-time).

The verification process is, however, very costly (spends 100% time on code review and 100% time on prototype model checking), which triples the already-expensive development cost of SIMD code.

If a bug somehow manages to slip through this verification process, it is nearly impossible to "repair" (fix) except to replace (rewrite) the suspected defective function.

SIMD code suffers from the blunt of defects in the C++ compiler (optimizing code generator).

SIMD code generated using C++ expression templates also suffers greatly from defects of the compiler.

In terms of disruptive innovations

Many solutions have been proposed from academia, but few are seeing widespread commercial usage.

MIT Halide

Stanford Darkroom

NT2 (Numerical Template Toolbox) and the related Boost.SIMD

Libraries with widespread commercial usage do not seem to be heavily SIMD-enabled.

Open-source libraries seem lukewarm to SIMD.

Recently I have this first-hand observation of this after profiling a large number of OpenCV API functions, as of version 2.4.9.

Many other image processing libraries I have profiled also do not make heavy use of SIMD, or they miss the true hotspots.

Commercial libraries seem to avoid SIMD altogether.

In a few cases, I have even seen image processing libraries reverting SIMD-optimized code in an earlier version to non-SIMD code in a later version, resulting in severe performance regressions.
(The vendor's response is that it was necessary to avoid compiler bugs.)

This Programmer's question:  Does low latency code sometimes have to be "ugly"?  is related, and I previously wrote an answer to that question to explain my view points a few years ago.

However, that answer is pretty much "appeasement" to the "premature optimization" viewpoint, i.e. to the viewpoint that:

All optimizations are premature by definition (or, short-term by nature), and

The only optimization that has long-term benefit is toward simplicity.

But such viewpoints are contested in this ACM article.

All of that leads me to ask:

SIMD code is different than general application code, and I would like to know if there is a similar industry consensus regarding the value of clean and simple code for SIMD code.

答:
I did not write much SIMD code for myself, but a lot of assembler code some decades ago. AFAIK using SIMD intrinsics is essentially assembler programming, and your whole question could be rephrased just by replacing "SIMD" by the word "assembly". For example, the points you already mentioned, like

the code takes 10x to 100x to develop than "high level code"

it is tied to a specific architecture

the code is never "clean" nor easy to refactor

you need experts for writing and maintaining it

debugging and maintaining is hard, evolving really hard

are in no way "special" to SIMD - these points are true for any kind of assembly language, and they are all "industry consensus". And the conclusion in the software industry is also pretty much the same as for assembler:

don't write it if you don't have to - use a high level language whereever possible and let compilers do the hard work

if the compilers are not sufficient, at least encapsulate the "low level" parts in some libraries, but avoid to spread the code all over your program

since it is almost impossible to write "self-documenting" assembler or SIMD code, try to balance this by lots of documentation.

Of course, there is indeed a difference to the situation with "classic" assembly or machine code: today, modern compilers typically produce high quality machine code from a high level language, which is often better optimized than assembler code written manually. For the SIMD architectures which are popular today, the quality of the available compilers is AFAIK far below that - and maybe it will never reach that, since automatic vectorization is still a topic of scientific research. See, for example, this article which describes the differences in opimization between a compiler and a human, giving a notion that it might be very hard to create good SIMD compilers.

As you described in your question already, there exist also a quality problem with current state-of-the-art libraries. So IMHO best we can hope is that in the next years the quality of the compilers and libraries will increase, maybe the SIMD hardware will have to change to become more "compiler friendly", maybe specialized programming languages supporting easier vectorization (like Halide, which you mentioned twice) will become more popular (wasn't that already a strength of Fortran?). According to Wikipedia, SIMD became "a mass product" around 15 to 20 years ago (and Halide is less than 3 years old, when I interpret the docs correctly). Compare this to the time compilers for "classic" assembly language needed to become mature. According to this Wikipedia article it took almost 30 years (from ~1970 to the end of the 1990s) until compilers exceeded the performance of human experts (in producing non-parallel machine code). So we may just have to wait more 10 to 15 years until the same happens to SIMD-enabled compilers.


问:
Is it better to use strings or int to reference enums outside the java part of the system?
We were having a discussion at my work about the use of enums in Java.

A coworker was arguing that when using enums on the server-side, whenever required we should use string to reference to it (for instance when sending data from JS to the server or when storing in the database), arguing that this is much more clearer for the developer and also arguing that it would fail fast in case of typos.

I always used integers to identify enums in these cases, because it would be immutable identifiers and would not have case and typo problems (even though if a dev made the mistake of using the value 2 instead of 1, it would not fail fast).

Being very analytic about these arguments, I would say that using strings is better, but I get a strange feeling about it (as if it was not a good approach).

Is there any best practice regarding this discussion that could guide me?

Edit: Whenever it is possible, we use the enum itself, so all our Java code uses the Enum. By "reference to an enum" I mean: referencing the value in the enum when exchanging data from JavaScript to the server or storing data in the database

答:
Good question.  The use of enum in Java is primarily meant to handle information that is somewhat categorical in nature.  The classic example being using enum to handle the four types of suits that a card could have.  It provides all the performance advantage of using an integer and it is also clear in your program.

So outside of Java, why wouldn't you continue using an integer?  Perhaps you don't have enum types outside Java, though that doesn't mean you can't continue using integer for performance purposes, right?  Yes, that's completely true, though consider what happens when you add a new enum value.  If you don't add it to the end, all the other enum values after the new value will be increased by one.  Well we'll just specify the number so it can't change or we'll always add it to the end.  Are you confident that your fellow coworkers will always do right by that?  Meh?  Probably?  Hopefully?  Maybe you're not 100% on that.  Keep that in mind for a second.

Your boss tells you that there's a mess at the client using your software after the last update.  Now all entities X act like they were assigned enum value Y even if they were really assigned Z.  You check the repository and yes, someone added a new enum value and didn't follow your guidelines as you asked them to.  Now you have the added complication that on the database it is written 4, when it really should be 3, excluding records that have been inserted prior to the update which really are 4.  What enum value pertains to 4 anyway?  Isn't it Y?  You can't remember.  You need to check the program to verify.  Simply put, it's a mess.

If instead, your database wrote "HEARTS", "DIAMONDS", "SPADES", "CLUBS", you've lost little in terms of space and gained so much.  It's true we're talking about a minor performance hit, but you really should not be accessing the database that often to make a difference.  As for space, leave that to the systems administrators (not. your. problem.).

If you've made a simple program that is easy to make changes to, you've done yourself a favor in the long run, trust me.  This aspect of it is no different in my humble opinion.


问:
Releasing Qt-based controls under University of Illinois/NCSA
Qt is a graphic framework which has a double license:

commercial (statically link and modify at your wish)

LGPL (you should either dynamically link against it or release your source code, any modification should be released under GPL)

If I create some Qt-based controls for an application that can link dynamically against Qt, can I release my controls under University of Illinois/NCSA license? I'd like those to be used in any project (including commercial projects) by just retaining the copyright that I did those.

答:
The University of Illinois/NCSA license is a permissive, MIT-style open source license.

As the license does not impose any restrictions on redistribution or modification that are not also in the (L)GPL, the NCSA license is compatible with the (L)GPL license. This means that there are no legal restrictions on distributing an application that contains both code under the GPL license and code under the NCSA license.

Given this, you are free to release your control under the NCSA license.

The only thing that is not allowed is to distribute a binary-only version that is statically linked to the LGPL Qt distribution, because that would violate the LGPL terms.

For reference, when combining (L)GPL code with non-GPL code, the following rules apply:

If the non-GPL code is distributed on its own and without dependencies on GPL-only code, then the GPL restrictions don't apply.

If the non-GPL code depends on GPL-only code or is distributed as one application/library with GPL code, then the GPL restrictions apply to the entire application/library. In particular, the source code for the entire application/library must be available.

As Qt is dual licensed, it is possible to use (and distribute) your control in a closed-source application without distributing source code, as long as that application uses the commercial Qt version. This is something for the application authors to ensure.


问:
Are there any actual drawbacks to self-referential method chaining?
I recently suggested a method of chaining be implemented for a certain class in a certain project so readability of the code could be improved. I got a "fluent interfaces should not be implemented just for convenience, but for semantics" answer and had my suggestion shot down. I answered that I was not suggesting a fluent interface but method chaining itself (both can be confused with each other, read at bottom) to improve readability and coding comfort, suggestion was shot down again.

Anyhow, this got me thinking that maybe I could be incurring in a bad practice by always returning "this" in methods that are not supposed to return anything (e.g. setters).

My question is: can applying the previous convention be regarded as bad practice or abuse?, why?. I don't think there are any performance drawbacks, or are there?.

答:
Yes, there are drawbacks

Code that is easy to read is good, but also beware of what the code communicates as well. When an object's methods always return the object, it communicates a couple of things:

I require advanced configuration that isn't necessarily obvious in which order things should be set or configured

Each subsequent method call builds on the last

Valid Use Case: Ad Hoc Database Queries

Class libraries exist in most every language that allow you to query a database without resorting to hard coded SQL. Take the Entity Framework for .NET as an example:

```<code>DBContext db = new DBContext();
List<Post> posts = db.Posts
    .Where(post => post.Title.Contains("Test"))
    .OrderBy(post => post.DateCreated)
    .ToList();
</code>```

This is a fluent interface where each subsequent method call builds on the previous one. Reading these calls logically makes sense in the context of querying a database.

Invalid Use Case: Syntactical Sugar For Setting Properties

Now let's use the same pattern with the Post class:

```<code>public class Post
{
    public string Title { get; set; }
    public DateTime DateCreated { get; set; }
    public string Body { get; set; }

    public Post SetTitle(string title)
    {
        Title = title;

        return this;
    }

    public Post SetDateCreated(DateTime created)
    {
        DateCreated = created;

        return this;
    }

    public Post SetBody(string body)
    {
        Body = body;

        return this;
    }
}
</code>```

Now let's look at how you would use this class:

```<code>Post post = new Post()
    .SetTitle("Test")
    .SetDateCreated(DateTime.Now)
    .SetBody("Just a test");
</code>```

When I see this code, I immediately ask this question: "After calling SetBody, does it query the database? Do I need to call another method to say 'I'm done?'"

What do the chained method calls communicate to the code using the Post class?

I have a complicated setup

Each method call builds on the previous one

Is this actually true? No. The Post class does not have a complicated setup. Setting the title, date created and body do not build on each other towards a more complicated end goal. You've mashed a square peg into a round hole.

The drawback to self-referential method chaining is that you communicate that multiple method calls are required to do something, and that each call builds off the last. If this is not true, then method chaining could be communicating the wrong thing to other programmers.

When your coworkers said:

Fluent interfaces should not be implemented just for convenience, but for semantics

They were absolutely correct. A fluent interface, or method chaining, communicates something in and of itself which might not be true.

Body:
Yes, there are drawbacks

Code that is easy to read is good, but also beware of what the code communicates as well. When an object's methods always return the object, it communicates a couple of things:

I require advanced configuration that isn't necessarily obvious in which order things should be set or configured

Each subsequent method call builds on the last

Valid Use Case: Ad Hoc Database Queries

Class libraries exist in most every language that allow you to query a database without resorting to hard coded SQL. Take the Entity Framework for .NET as an example:

DBContext db = new DBContext();
List<Post> posts = db.Posts
    .Where(post => post.Title.Contains("Test"))
    .OrderBy(post => post.DateCreated)
    .ToList();

This is a fluent interface where each subsequent method call builds on the previous one. Reading these calls logically makes sense in the context of querying a database.

Invalid Use Case: Syntactical Sugar For Setting Properties

Now let's use the same pattern with the Post class:

public class Post
{
    public string Title { get; set; }
    public DateTime DateCreated { get; set; }
    public string Body { get; set; }

    public Post SetTitle(string title)
    {
        Title = title;

        return this;
    }

    public Post SetDateCreated(DateTime created)
    {
        DateCreated = created;

        return this;
    }

    public Post SetBody(string body)
    {
        Body = body;

        return this;
    }
}

Now let's look at how you would use this class:

Post post = new Post()
    .SetTitle("Test")
    .SetDateCreated(DateTime.Now)
    .SetBody("Just a test");

When I see this code, I immediately ask this question: "After calling SetBody, does it query the database? Do I need to call another method to say 'I'm done?'"

What do the chained method calls communicate to the code using the Post class?

I have a complicated setup

Each method call builds on the previous one

Is this actually true? No. The Post class does not have a complicated setup. Setting the title, date created and body do not build on each other towards a more complicated end goal. You've mashed a square peg into a round hole.

The drawback to self-referential method chaining is that you communicate that multiple method calls are required to do something, and that each call builds off the last. If this is not true, then method chaining could be communicating the wrong thing to other programmers.

When your coworkers said:

Fluent interfaces should not be implemented just for convenience, but for semantics

They were absolutely correct. A fluent interface, or method chaining, communicates something in and of itself which might not be true.


问:
Is there any algorithm pattern to protect any content in the web to ensure I am the first one who created it?
A few years ago there was this hacker (don't remember who he was) that full disclosed a vulnerability in a given system, but to make sure nobody took credit for that, he created some kind of PGP key.

What I understood at the time is that he created a key to ensure he was the one who discovered it, but didn't disclosed who he was actually, just created some mechanism to be able to prove that he was the one who created the disclosure.

Ok. I get how algorithms and cryptography works. But I still don't understand how you can create a key to protect a given content disclosed in the web to prove you are the one who created it first! It is just words!

Is it really possible? What should be the process to ensure you can empirically prove it? Did I understood it correctly or I probably missed something regarding this case?

I hope this question is specific enough, basically it is just how to protect a content that you created in the web (a paragraph, a code, a word, etc.) and make sure you are the one who created it first, inside a given context.

With my knowledge I don't see how that is possible, but I am intrigued if there is a practical way to do it. Is there?

答:
This is a different take on valenterry's answer.

Here's how you would do it using PGP:

Generate a public/private key pair.

You keep the private key and you make sure it stays secret.

You encrypt your idea with your public key: P(idea)

You put P(idea) somewhere that is trusted(not by you, but in general) and will log the time.

When you need to prove you made the idea first, you get the timestamp from when you stored the data, and you decrypt your data with your secret key.  S(P(idea)) => idea

This method doesn't require releasing your secret key, which in general is a bad idea.  Granted, you can always make a new PGP key pair - it doesn't cost anything, but you shouldn't be recklessly handing out secret keys if you want to be credible.

The hardest part is proving the time, but in terms of documenting a vulnerability, we don't need a 100% bullet-proof-and-verifiable-in-court solution, we just need something that's 'good enough'.  The logs of a cloud storage provider(dropbox, rackspace, google, etc) is probably good enough, assuming they implement a secure service.

It's also worth noting that being the first one with an idea and timestamping it has never meant that you were the first person to think of the idea. If someone thought of it before you but never registered the idea with a timestamping mechanism, then they can't prove they made it before you.  So, if we're trying to figure out who made it first, and all we know is the time you came up with it, then we have to assume you came up with it first(the other person could be spewing lies).


问:
How to use MeshGrid method in Accord.Math Matrix Class?
I am using the Accord.Math Namespace for Visual Studio in c#.

I am trying to use the method MeshGrid<> under the Matrix class for the Accord.Math namespace. However, I am unsure how to implement this method even after reading the documentation for it seen here:
http://accord-framework.net/docs/html/M_Accord_Math_Matrix_MeshGrid__1.htm

Can anybody show me how to properly implement this method?

I have two Double[,] variables named xa and ya that I am trying to pass to MeshGrid. I have tried calling by using:

```<code>var q = Matrix.MeshGrid(xa,ya);
</code>```

But for this is says that the type arguments cannot be inferred from their usage.

The output of MeshGrid is a 2-Tuple.
T1 is T[,]
T2 is T[,]

答:
The Matrix.MeshGrid method takes inputs as type of T[] (not T[,]).

Method signature:

```<code>public static Tuple<T[,], T[,]> MeshGrid<T>(this T[] sequence1, T[] sequence2);
</code>```

Example:

```<code>var xi = new double[] {1, 2, 3, 4, 5};
var yi = new double[] {6, 7, 8, 9, 10};
Tuple<double[,], double[,]> grid = Matrix.MeshGrid(xi, yi);
</code>```

Body:
The Matrix.MeshGrid method takes inputs as type of T[] (not T[,]).

Method signature:

public static Tuple<T[,], T[,]> MeshGrid<T>(this T[] sequence1, T[] sequence2);

Example:

var xi = new double[] {1, 2, 3, 4, 5};
var yi = new double[] {6, 7, 8, 9, 10};
Tuple<double[,], double[,]> grid = Matrix.MeshGrid(xi, yi);


答:
It doesn't seem to add too much maintenance overhead if you consider using a higher-level language:

```<code>Vector<float> values = GetValues();
Vector<float> increment = GetIncrement();

// Perform addition as a vector operation:
List<float> result = (values + increment).ToList();
</code>```

vs

```<code>List<float> values = GetValues();
List<float> increment = GetIncrement();

// Perform addition as a monadic sequence operation:
List<float> result = values.Zip(increment, (v, i) => v + i).ToList();
</code>```

Of course you will have to face the limitations of the library, but you will not maintain it yourself. Could be a good balance between maintenance cost and performance win.

Link

Link

Title: Maintenance cost of SIMD programming code base

Body:
Question:

The software industry's consensus is that clean and simple code is fundamental to the long-term viability of the code base and the organization that owns it. These properties lead to lower maintenance costs and increased likelihood of the code base being continued.

However, SIMD code is different than general application code, and I would like to know if there is a similar consensus regarding clean and simple code applying specifically to SIMD code.

Background to my question.

I write plenty of SIMD (single-instruction, multiple data) code for various image processing and analysis tasks. Recently I also had to port a small number of these functions from one architecture (SSE2) to another (ARM NEON).

The code is written for shrink-wrapped software, therefore it cannot depend on proprietary languages without unrestricted redistribution rights such as MATLAB.

An example of typical code structure:

Using OpenCV's matrix type (Mat) for all memory, buffer and lifetime management.

After checking the size (dimensions) of input arguments, the pointers to the start address of each row of pixels is taken.

The pixel count, and the start addresses of each row of pixels from each input matrix is passed into some low-level C++ functions.

These low-level C++ functions use SIMD intrinsics (for Intel Architecture, and ARM NEON), loading from and saving to raw pointer addresses.

Characteristics of these low-level C++ functions:

Exclusively one-dimensional (consecutive in memory)

Does not deal with memory allocations.
(Every allocation, including temporaries, are handled by the outer code using OpenCV facilities.)

The range of name lengths of symbols (intrinsics, variable names, etc) are roughly 10 - 20 characters, which is quite excessive.
(Reads like techno-babble.)

Reuse of SIMD variables is discouraged because compilers are quite buggy in correctly parsing code that is not written in the "single-assignment" coding style.
(I've filed several compiler bug reports.)

What aspects of SIMD programming would cause the discussion to differ from the general case?  Or, why is SIMD different?

In terms of initial development cost

It is well-known that the initial development cost of C++ SIMD code with good performance is about 10x - 100x (with a wide margin) compared to casually-written C++ code.

As noted in the answers to Choosing between performance vs readable/cleaner code? , most code (including casually-written code and SIMD code) is initially neither clean nor fast.

Evolutionary improvements in code performance (in both scalar and SIMD code) is discouraged (because it is seen as a kind of software rework), and the cost and benefit is not tracked.

In terms of propensity

(e.g. the Pareto principle, a.k.a. the 80-20 rule)

Even if image processing only comprises 20% of a software system (in both code size and functionality), image processing is comparatively slow (when viewed as a percentage of CPU time spent), taking more than 80% of time.

This is due to the data size effect: A typical image size is measured in megabytes, whereas the typical size of non-image data is measured in kilobytes.

Within the image processing code, a SIMD programmer is trained to automatically recognize the 20% code comprising the hotspots by identifying the loop structure in the C++ code. Thus, from a SIMD programmer's perspective, 100% of the "code that matters" is performance bottleneck.

Often in an image processing system, multiple hotspots exist and take up comparable proportions of time. For example, there may be 5 hotspots each taking up (20%, 18%, 16%, 14%, 12%) of total time. To achieve a high performance gain, all of the hotspots need to be rewritten in SIMD.

This is summarized as the balloon-popping rule: a balloon cannot be popped twice.

Suppose there are some balloons, say 5 of them. The only way to decimate them is to pop them one by one.

Once the first balloon is popped, the remaining 4 balloons now comprises a higher percentage of total execution time.

To make further gains, one must then pop another balloon.
(This is in defiance to the 80-20 rule of optimization: a good economical outcome can be achieved after the 20% of lowest-hanging fruits has been picked.)

In terms of readability and maintenance

SIMD code is patently hard to read.

This is true even if one follows every software engineering best-practice e.g. naming, encapsulation, const-correctness (and making side-effects obvious), function decomposition, etc.

This is true even for experienced SIMD programmers.

Optimal SIMD code is very contorted, (see remark) compared to its equivalent C++ prototype code.

There are many ways to contort SIMD code, but only 1 out of 10 such attempts will attain acceptably fast results.

(That is, in the tunes of 4x-10x performance gains in order to justify to high development cost. Even higher gains have been observed in practice.)

(Remark)

This is the main thesis of the MIT Halide project - quoting the paper's title verbatim:

"decoupling algorithms from schedules for easy optimization of image processing pipelines"

In terms of forward applicability

SIMD code is strictly tied to a single architecture. Each new architecture (or each widening of SIMD registers) requires a rewrite.

Unlike the majority of software development, each piece of SIMD code is typically written for a single purpose that never changes.
(With the exception of porting to other architectures.)

Some architectures maintain perfect backward compatibility (Intel); some fall short by a trivial amount (ARM AArch64, replacing vtbl with vtblq) but which is sufficient to cause some code to fail to compile.

In terms of skills and training

It is not clear what knowledge prerequisites are required to properly train a new programmer to write and maintain SIMD code.

College graduates who have learned SIMD programming in school seems to despise and dismiss it as an impractical career track.

Disassembly-reading and low-level performance profiling are cited as two fundamental skills for writing high-performance SIMD code. However, it is unclear how to systematically train programmers in these two skills.

Modern CPU architecture (which diverges significantly from what is taught in textbooks) makes training even more difficult.

In terms of correctness and defect-related costs

A single SIMD processing function is actually cohesive enough that one can establish correctness by:

Applying formal methods (with pen-and-paper), and

Verifying output integer ranges (with prototype code and performed outside run-time).

The verification process is, however, very costly (spends 100% time on code review and 100% time on prototype model checking), which triples the already-expensive development cost of SIMD code.

If a bug somehow manages to slip through this verification process, it is nearly impossible to "repair" (fix) except to replace (rewrite) the suspected defective function.

SIMD code suffers from the blunt of defects in the C++ compiler (optimizing code generator).

SIMD code generated using C++ expression templates also suffers greatly from defects of the compiler.

In terms of disruptive innovations

Many solutions have been proposed from academia, but few are seeing widespread commercial usage.

MIT Halide

Stanford Darkroom

NT2 (Numerical Template Toolbox) and the related Boost.SIMD

Libraries with widespread commercial usage do not seem to be heavily SIMD-enabled.

Open-source libraries seem lukewarm to SIMD.

Recently I have this first-hand observation of this after profiling a large number of OpenCV API functions, as of version 2.4.9.

Many other image processing libraries I have profiled also do not make heavy use of SIMD, or they miss the true hotspots.

Commercial libraries seem to avoid SIMD altogether.

In a few cases, I have even seen image processing libraries reverting SIMD-optimized code in an earlier version to non-SIMD code in a later version, resulting in severe performance regressions.
(The vendor's response is that it was necessary to avoid compiler bugs.)

This Programmer's question:  Does low latency code sometimes have to be "ugly"?  is related, and I previously wrote an answer to that question to explain my view points a few years ago.

However, that answer is pretty much "appeasement" to the "premature optimization" viewpoint, i.e. to the viewpoint that:

All optimizations are premature by definition (or, short-term by nature), and

The only optimization that has long-term benefit is toward simplicity.

But such viewpoints are contested in this ACM article.

All of that leads me to ask:

SIMD code is different than general application code, and I would like to know if there is a similar industry consensus regarding the value of clean and simple code for SIMD code.

Body:
It doesn't seem to add too much maintenance overhead if you consider using a higher-level language:

Vector<float> values = GetValues();
Vector<float> increment = GetIncrement();

// Perform addition as a vector operation:
List<float> result = (values + increment).ToList();

vs

List<float> values = GetValues();
List<float> increment = GetIncrement();

// Perform addition as a monadic sequence operation:
List<float> result = values.Zip(increment, (v, i) => v + i).ToList();

Of course you will have to face the limitations of the library, but you will not maintain it yourself. Could be a good balance between maintenance cost and performance win.

Link

Link

ContentLicense: CC BY-SA 4.0


问:
How to define "or" logically
Recently, I came across a problem that required me to define the logical "OR" operator programmatically, but without using the operator itself.

What I came up with is this:

OR(arg1, arg2)
  if arg1 = True and arg2 = True
     return True

  else if arg1 = True and arg2 = False
     return True

  else if arg1 = False and arg2 = True
     return True

  else:
     return False
```

Is this logic correct, or did I miss something?

答:
The logic is perfectly correct, but can be simplified:

```<code>or(arg1, arg2)
  if arg1 = True
     return True
  else if arg2 = True
     return True
  else
     return False
</code>```

And presumably your language has an OR operator so - unless it's against the spirit of the question - why not

```<code>or(arg1, arg2)
  if arg1 = True or arg2 = True
     return True
  else
     return False
</code>```

Title: How to define "or" logically

Body:
The logic is perfectly correct, but can be simplified:

or(arg1, arg2)
  if arg1 = True
     return True
  else if arg2 = True
     return True
  else
     return False

And presumably your language has an OR operator so - unless it's against the spirit of the question - why not

or(arg1, arg2)
  if arg1 = True or arg2 = True
     return True
  else
     return False


答:
I'd say that's correct, but could you not condense it down to something such as this?

```<code>or(arg1, arg2)
    if arg1 == true
        return true
    if arg2 == true
        return true

    return false
</code>```

Since you're doing an or comparison, I don't think you really need to check the combination. It just matters if one of them is true to return true. Otherwise we want to return false.

If you're looking for a shorter version that is less verbose, this will also work:

```<code>or(arg1, arg2)
    if arg1
        return arg1
    return arg2
</code>```

Title: How to define "or" logically

Body:
Recently, I came across a problem that required me to define the logical "OR" operator programmatically, but without using the operator itself.

What I came up with is this:

OR(arg1, arg2)
  if arg1 = True and arg2 = True
     return True

  else if arg1 = True and arg2 = False
     return True

  else if arg1 = False and arg2 = True
     return True

  else:
     return False
```

Is this logic correct, or did I miss something?

Body:
I'd say that's correct, but could you not condense it down to something such as this?

or(arg1, arg2)
    if arg1 == true
        return true
    if arg2 == true
        return true

    return false

Since you're doing an or comparison, I don't think you really need to check the combination. It just matters if one of them is true to return true. Otherwise we want to return false.

If you're looking for a shorter version that is less verbose, this will also work:

or(arg1, arg2)
    if arg1
        return arg1
    return arg2


问:
Do I need to follow hypermedia format or a schema?
I'm quite new to the field. So I need to clarify what would be the best method to follow in the following situation.

I'm developing a restful API using php and use json to represent resources. While I'm at this I read it's considered to be a good practice to conform with "HATEOAS" (is it?). So I wanted to use something like HAL or JSON-LD right? What if I do my own formatting while using IANA link relation representations and let my clients know what they should expect via json-hyper schema?

What is the best method? Did I got everything messed up?

答:
HATEOAS is a somewhat controversial topic. Many people feel it's an example of overengineering and see no practical benefit to it. I believe it offers a natural and sensible approach to implementing Web APIs, with the benefits of increased decoupling between server and clients and a lower burden on client developers (see my answer to "REST HATEOAS - How does the client know link semantics?").

Having said that, to my knowledge there are still few well established conventions for Web APIs. You'll be doing your users and yourself a favour by adopting an existing standard like HAL, though:

Experienced developers will understand your API right away without having to learn your own unique conventions.

You'll be able to use libraries like ROAR that generate standards-compliant representations more or less automatically.

Clients will be able to use tools like HyperResource that consume standards-compliant APIs without additional programming.

Should you decide to define your own formats and conventions (which is still the norm for API designers, as far as I know), not only will you be burdening the world with yet another wholly non-standard API you'll be increasing your own burden in terms of delivering documentation and sample code to clients.


答:
If you only have and and not, you can use DeMorgan's law to flip around and:

```<code>if not (arg1 = False and arg2 = False)
  return True
else
  return False
</code>```

... or (even more simply)

```<code>if arg1 = False and arg2 = False
  return false
else
  return true
</code>```

...

And since we've all apparently become fixated on optimizing something that is almost always available as a machine instruction anyway, that boils down to:

```<code>return not(not arg1 and not arg2)

return arg1 ? true : arg2
</code>```

etc. etc. etc. etc.

Since most languages provide a conditional-and, odds are the "and" operator implies a branch anyway.

...

If all you have is nand (see wikipedia):

return nand(nand(arg1, arg1), nand(arg2, arg2))

Title: How to define "or" logically

Body:
If you only have and and not, you can use DeMorgan's law to flip around and:

if not (arg1 = False and arg2 = False)
  return True
else
  return False

... or (even more simply)

if arg1 = False and arg2 = False
  return false
else
  return true

...

And since we've all apparently become fixated on optimizing something that is almost always available as a machine instruction anyway, that boils down to:

return not(not arg1 and not arg2)

return arg1 ? true : arg2

etc. etc. etc. etc.

Since most languages provide a conditional-and, odds are the "and" operator implies a branch anyway.

...

If all you have is nand (see wikipedia):

return nand(nand(arg1, arg1), nand(arg2, arg2))


问:
Is this a pattern? If so, which one? (Contexts, Requests and Responses)
In most of my code nowadays, I adapted to an approach which I think I read in the context of "Clean Code". It was some years ago and I cannot find the source again, so I would like to know what the name of this pattern really is (and if it's even a pattern).

The approach is so simple, yet powerful that I am baffled as to how hard it is to find sources on this, but that might just be lack of proper search words.

The approach is, in order to write unit-testable code, to decouple the function and presentation of an application (duh) in such a way, that the two parts actually communicate via an interface which I always describe as "HTTP-Like".

That is, the functions your code can do are one project, and the display etc. are another project. The only communication between the two is done by using Context classes, which accept a Request and return a Response for anything your code can do.

So, e.g. if your code translates a sentence, you'd have a TranslationContext, along with the TranslationRequest (which has a property text) and the TranslationResponse (with a property text that includes the translated text).

The Context class is the only interface from your actual application code to the "outside", whereas the "view" is also seen as the outside.

From the presentation side, you' call

```<code>TranslationContext ctx = new TranslationContext(interfacedDbOrTextfile);
TranslationRequest req = new TranslationRequest("hallo welt");
TranslationResponse resp = ctx.Process(req);
Console.Write(resp.Text); // "hello world"
</code>```

Additionally, any data sources (DB, Text files, etc) must be interfaced and decoupled when passed to the core, so you can always replace them with UnitTestable things like in-memory strings instead of textfiles or arraylists instead of database rows.

Is what I am describing just the application of known patterns in a way that I cannot see? Or is it a pattern which was not described yet? Or am I just using different words for things that already exist?

答:
The Context Object is a pattern used in a number of places, including J2EE, ASP.NET MVC and Entity Framework.  It is used "to encapsulate state in a protocol-independent way to be shared throughout your application."

Context Object is a controversial pattern.  Although it can be thought of as a means of Inversion of Control or Dependency Injection, it's really more like a convenient way of passing the working environment around (akin to a "god object.") Context objects can be difficult to mock, therefore testability of the code that uses them is negatively impacted.

Further Reading

Context Object

Passing around ginormous context objects

Mocking HttpContext

Requests and responses are just message passing.


答:
Consider all the stylistic elements of programming languages (as opposed to hand-coding machine language), and it weakens the "semantics not convenience" argument.

A whole lot of what we do as engineers is providing convenience around semantics.

Title: Are there any actual drawbacks to self-referential method chaining?

Body:
I recently suggested a method of chaining be implemented for a certain class in a certain project so readability of the code could be improved. I got a "fluent interfaces should not be implemented just for convenience, but for semantics" answer and had my suggestion shot down. I answered that I was not suggesting a fluent interface but method chaining itself (both can be confused with each other, read at bottom) to improve readability and coding comfort, suggestion was shot down again.

Anyhow, this got me thinking that maybe I could be incurring in a bad practice by always returning "this" in methods that are not supposed to return anything (e.g. setters).

My question is: can applying the previous convention be regarded as bad practice or abuse?, why?. I don't think there are any performance drawbacks, or are there?.


答:
Here is a solution without or, and, not, comparisons and boolean literals:

```<code>or(arg1, arg2)
  if arg1
    return arg1
  else
    return arg2
</code>```

It probably doesn't get much more fundamental than that ;)

Title: How to define "or" logically

Body:
Here is a solution without or, and, not, comparisons and boolean literals:

or(arg1, arg2)
  if arg1
    return arg1
  else
    return arg2

It probably doesn't get much more fundamental than that ;)


答:
The main drawback is a loss of clarity. For instance:

```<code>x.foo();
x.bar();
x.baz();
</code>```

Since none of those statements return a value (or if they do, it's been ignored), they can only be useful if they produce side effects. Contrast that to:

```<code>x.foo()
 .bar()
 .baz();
</code>```

First, it's not clear that bar and baz operate on objects of the same type as x, unless you know for a fact that x's class is the only class with those methods. Even if that's the case, it's not clear the methods operate on x, or new objects.

Highlighting the parts of the code that have side effects is useful, since you need to track those side effects to reason about the program. You have to weigh the potential loss of clarity against the potential gain in ease of writing code, but also consider that code is read more than it's written.

Body:
The main drawback is a loss of clarity. For instance:

x.foo();
x.bar();
x.baz();

Since none of those statements return a value (or if they do, it's been ignored), they can only be useful if they produce side effects. Contrast that to:

x.foo()
 .bar()
 .baz();

First, it's not clear that bar and baz operate on objects of the same type as x, unless you know for a fact that x's class is the only class with those methods. Even if that's the case, it's not clear the methods operate on x, or new objects.

Highlighting the parts of the code that have side effects is useful, since you need to track those side effects to reason about the program. You have to weigh the potential loss of clarity against the potential gain in ease of writing code, but also consider that code is read more than it's written.


问:
Continuous Integration for different languages
So, I understand Continuous Integration in theory, and have used Beanstalk. However, I've been charged with creating the process at my current PHP shop.

My hope was maybe PHPCI, since it looked perfect, and I'm a bit out of my depth. However, it turns out we will have to maintain a Java application.

Currently, we have 3 projects, each deploying to three servers(dev, qa, prod) with different requirements, so nine jobs. We were going to try Bitbucket, but that didn't work out financially, but we are firmly in the Atlassian camp(BitBucket, JIRA/ServiceDesk).

It seems to me that my only realistic option is Jenkins, because of the multi-language needs. And then Ant/Gradle/Maven for the the Java stuff.

Is this correct?

How difficult will it be to set this up?

Is this a fairly flexible solution? It seems common.

答:
Most CI servers are somewhat language agnostic -- at the end of the day you are really just executing shell scripts with some fancy reporting -- so your general premise is correct.

The physical deployment of PHP apps is typically pretty simple -- they are just files on disk so rsync is your friend here. The difficult parts come in with things like user data in files and managing databases but presuming you've got a decent plan you are good to go. The question to be asking yourself is "can I deploy this ideoimpotently from a command line with no human intervention."

Using jenkins or a similar product to deploy multiple platforms is a fairly common solution. There should be plenty of help and expertise about making this happen.


问:
Concept for dealing with recurring appointments
Our company implemented a calendar system a few months back with recurring appointments,  using iCal strings to store the recurring appointment criteria.

We are starting to run into performance problems because of these.

Our current thinking is something along these lines...

A user requests all appointments for a date range.  Typically no more than 30 days.

We grab all non-recurring events from the database that fall in this date range.
Then we grab ALL events that contain an iCal string and run each one through a processor to determine if the event happens to repeat in our requested date range.  For example, the user may have the event starting on 1/1/14, repeating every Tuesday and Wednesday ongoing forever.  Another event, however, may begin on 1/1/14, repeating every Tuesday and Wednesday for one month and is now outside of our selected range.  The first one would repeat to our date range and be included.  The second one would not.

As far as I can tell, there really isn't any good way to determine any kind of filtering so that I don't have to pull every single one of these events from the database and test them to see if they match our date range.

As the number of recurring events grows, our database is being slogged down with requests that return more and more records, and our server needs to process each of these, most of which aren't necessary, but we don't know until we get them to the server and process them.

Has anybody faced this before, and is there a better algorithm that we can implement to limit the number of records returned?

答:
It seems to me that your problem is in your first sentence:

Our company implemented a calendar system a few months back with recurring appointments, using iCal strings to store the recurring appointment criteria.

Having to scan all records to determine date ranges is simply not scalable.  In essence, you're using your database as a flat file, so of course it is slow.

Instead of storing the raw iCal strings, interpret the strings before you put them in the database so that you can run queries like

select * from records
where startdate < "2014-01-30 00:00:00" and enddate > "2014-01-01 00:00:00";
```

If you really need the iCal string, you can leave that in the DB as well, but things like appointment start/end dates should be db columns so you can efficiently search on them.

Body:
It seems to me that your problem is in your first sentence:

Our company implemented a calendar system a few months back with recurring appointments, using iCal strings to store the recurring appointment criteria.

Having to scan all records to determine date ranges is simply not scalable.  In essence, you're using your database as a flat file, so of course it is slow.

Instead of storing the raw iCal strings, interpret the strings before you put them in the database so that you can run queries like

select * from records
where startdate < "2014-01-30 00:00:00" and enddate > "2014-01-01 00:00:00";

If you really need the iCal string, you can leave that in the DB as well, but things like appointment start/end dates should be db columns so you can efficiently search on them.


答:
The two forms:

```<code>OR(arg1, arg2)
  if arg1
     return True
  else:
     return arg2
</code>```

OR

```<code>OR(arg1, arg2)
  if arg1
     return arg1
  else:
     return arg2
</code>```

Have as well as the code golf-like advantage of being a bit smaller than the other suggestions so far, one less branch. It's not even that silly a micro-opt to reduce the number of branches if we're considering the creation of a primitive that would hence be very heavily used.

Javascript's definition of || is akin to this, which combined with its loose typing means that the expression false || "abc" has the value "abc" and 42 || "abc" has the value 42.

Though if you already have other logical operators then the likes of nand(not(arg1), not(arg2)) might have the advantage of no branching at all.

Title: How to define "or" logically

Body:
The two forms:

OR(arg1, arg2)
  if arg1
     return True
  else:
     return arg2

OR

OR(arg1, arg2)
  if arg1
     return arg1
  else:
     return arg2

Have as well as the code golf-like advantage of being a bit smaller than the other suggestions so far, one less branch. It's not even that silly a micro-opt to reduce the number of branches if we're considering the creation of a primitive that would hence be very heavily used.

Javascript's definition of || is akin to this, which combined with its loose typing means that the expression false || "abc" has the value "abc" and 42 || "abc" has the value 42.

Though if you already have other logical operators then the likes of nand(not(arg1), not(arg2)) might have the advantage of no branching at all.


答:
One line of code:

```<code>return not (not arg1 and not arg2)
</code>```

No branching, no OR.

In a C-based language, it would be:

```<code>return !(!arg1 && !arg2);
</code>```

This is simply an application of De Morgan's laws: (A || B) == !(!A && !B)

Title: How to define "or" logically

Body:
One line of code:

return not (not arg1 and not arg2)

No branching, no OR.

In a C-based language, it would be:

return !(!arg1 && !arg2);

This is simply an application of De Morgan's laws: (A || B) == !(!A && !B)


答:
All the good answers have already been given. But I won't let that stop me.

```<code>// This will break when the arguments are additive inverses.
// It is "cleverness" like this that's behind all the most amazing program errors.
or(arg1, arg2)
    return arg1 + arg2
    // Or if you need explicit conversions:
    // return (bool)((short)arg1 + (short)arg2)
</code>```

Alternatively:

```<code>// Since `0 > -1`, negative numbers will cause weirdness.
or(arg1, arg2)
    return max(arg1, arg2)
</code>```

I hope no one would ever actually use approaches like these. They're here only to promote awareness of alternatives.

Update:

Since negative numbers can break both of the above approaches, here is another awful suggestion:

```<code>or(arg1, arg2)
    return !(!arg1 * !arg2)
</code>```

This simply uses DeMorgan's Laws and abuses the fact that * is similar to && when true and false are treated like 1 and 0 respectively. (Wait, you're saying this isn't code golf?)

Here's a decent answer:

```<code>or(arg1, arg2)
    return arg1 ? arg1 : arg2
</code>```

But that's essentially identical to other answers already given.

Title: How to define "or" logically

Body:
All the good answers have already been given. But I won't let that stop me.

// This will break when the arguments are additive inverses.
// It is "cleverness" like this that's behind all the most amazing program errors.
or(arg1, arg2)
    return arg1 + arg2
    // Or if you need explicit conversions:
    // return (bool)((short)arg1 + (short)arg2)

Alternatively:

// Since `0 > -1`, negative numbers will cause weirdness.
or(arg1, arg2)
    return max(arg1, arg2)

I hope no one would ever actually use approaches like these. They're here only to promote awareness of alternatives.

Update:

Since negative numbers can break both of the above approaches, here is another awful suggestion:

or(arg1, arg2)
    return !(!arg1 * !arg2)

This simply uses DeMorgan's Laws and abuses the fact that * is similar to && when true and false are treated like 1 and 0 respectively. (Wait, you're saying this isn't code golf?)

Here's a decent answer:

or(arg1, arg2)
    return arg1 ? arg1 : arg2

But that's essentially identical to other answers already given.


问:
Why isn't software abstract on a grander scale?
Consider the following example:

The user wants a program to calculate a few fibonacci numbers.
Sounds easy enough. pseudocode:

```<code>stdout.write("How many fibonacci numbers do you want to calculate? ")
int count = int(stdin.readline())
while count>0:
    stdout.writeline(calculate_next_fibonacci_number())
    count--
</code>```

Even a very simple program like this is already flawed:

The program writes to stdout. That is not actually what the programmer intended, is it? The intention is to display a bunch of number to the user, not to write some text to stdout - there's no guarantee the user will ever see the text that's written to stdout.

Similarly, the user input is read from stdin, which is a text (or file, if you prefer) interface, when in reality, the program requires a number, not text. This seems fundamentally wrong.

Let's think about what we're trying to do more abstractly. We want to:

Calculate a bunch of numbers. How many is up to the user.

display the results to the user.

Why, then, don't we write code exactly like that?
(Most) programming languages provide this thing called a "function", which accepts parameters and uses them to do something. Does that not sound exactly like what we're trying to do?

```<code>void display_fibonacci_numbers(
        int number "how many fibonacci numbers to calculate"):

        Sequence<int> numbers
        while number>0:
            numbers.append(calculate_next_fibonacci_number())
            number--

        notify(numbers)

display_fibonacci_numbers()
</code>```

This code is, of course, incomplete - the notify function isn't implemented anywhere, and the user needs some way of inputting a number. I would imagine the user's operating system or desktop manager or whatever to take care of that - it could display a terminal, it could generate a GUI, it could tell the user do write the number into the air with his nose; either way it does not (should not) concern me, the programmer.

It is my belief that software should be more abstract.

A few more examples.

inter-process communication. How do you do it? Sockets? Signals? DBus? Files? The goal isn't to use sockets, signals or dbus, it's to communicate with another process. Why not something like Process.from_name("Music player").play_random_song()?

Downloading files. local_file.write(remote_file.read())? Why not download(url), which could, depending on the system configuration, start the download right away, but with a low priority so as not to slow down other downloads, or add it to the download queue to be downloaded later, or whatever?

File paths. Why on earth are file paths still strings (in most languages, at least)? Why do we have to deal with whether the path separator is / or \, whether it's ., .., or whatever? Why is there no FilePath class that takes care of this?

Going a step further, why not apply the duck-typing concept to modules? For example, in python, HTML is often parsed using the beautifulsoup module:

```<code>from bs4 import BeautifulSoup

page= BeautifulSoup(urlopen('http://test.at'))
</code>```

Again, the programmer's goal wasn't to use BeautifulSoup, but to parse HTML. Why, then, would he explicitly tell his code to use the BeautifulSoup module? What he really wants is to

```<code>import HTMLParser
page= HTMLParser.parse(urlopen('http://test.at'))
</code>```

Duck-typing for modules: Who cares what module it is as long as it does what I want?

Why is programming not like this? Am I overlooking something, something that makes all this impossible (or impractical)?

答:
What you're asking is, "why can't we use Z or UML to fully-prototype and design a system?"

And the answer to that is, "Because, while you could build something that would build a system based on a UML or Z spec, verifying the spec becomes just as hard (if not more so) than implementing the spec."


问:
Use of for loop conditional statement unrelated to iterating variable
Is it considered decent form to write code like this:

```<code>int done = 0;
for (x = 0; !done; x++) {

    ... something involving 'x', that might end early ...

    if (!(x < max))
        done = 1;
}
</code>```

as opposed to having to use a while loop:

```<code>int done;
x = 0;
while (!done) {

    ... something involving 'x', that might end early ...

    if (!(x < max))
        done = 1;
    x++;
}
</code>```

or having to break out of a for loop:

```<code>for (x = 0; x < max; x++) {
    int done;

    ... something involving 'x', that might end early ...

    if (done)
        break;
}
</code>```

I prefer the first one, mainly because I dislike breaking out of loops, I prefer for loops, and it still looks neat. The second one appears to me as what others would likely use.

The third one feels icky to me, but it seemed like something else that others might use as opposed to the first.

What are the merits of using one over another? Should they each be used at different times, is one generally correct, or is it just a matter of personal preference?

答:
The first form

```<code>for ( x = 0; !done; x++ )
</code>```

is a little weird and non-intuitive IMO.  There's no obvious correlation between x and done in this context (you have to review the body of the loop to see that done is set when x is no longer less than max)

In that case, I would much rather see something like

```<code>for ( x = 0; x < max; x++ )
</code>```

However, you're concerned about the loop terminating before x is equal to max due to some other condition.  If you want to terminate the loop as soon as the other condition is true, you could do something like the following:

```<code>for ( x = 0; x < max && !other_exit_condition; x++ )
{
  // do the thing with x that may set other_exit_condition
  if ( other_exit_condition )
    continue;
  else
  {
    // rest of loop body
  }
}
</code>```

The continue statement goes back to the beginning of the loop, which then exits because other_exit_condition is now true.  But this is really no better than using a break, honestly.

Body:
The first form

for ( x = 0; !done; x++ )

is a little weird and non-intuitive IMO.  There's no obvious correlation between x and done in this context (you have to review the body of the loop to see that done is set when x is no longer less than max)

In that case, I would much rather see something like

for ( x = 0; x < max; x++ )

However, you're concerned about the loop terminating before x is equal to max due to some other condition.  If you want to terminate the loop as soon as the other condition is true, you could do something like the following:

for ( x = 0; x < max && !other_exit_condition; x++ )
{
  // do the thing with x that may set other_exit_condition
  if ( other_exit_condition )
    continue;
  else
  {
    // rest of loop body
  }
}

The continue statement goes back to the beginning of the loop, which then exits because other_exit_condition is now true.  But this is really no better than using a break, honestly.


问:
What is the point of using DTO (Data Transfer Objects)?
What is the point of using DTO and is it an out dated concept? I use POJOs in the view layer to transfer and persist data. Can these POJOs be considered as an alternative to DTOs?</p>

答:
Although DTO is not an outdated pattern, it is often applied needlessly, which might make it appear outdated.

From Java guru Adam Bien:

The most misused pattern in the Java Enterprise community is the DTO.
  DTO was clearly defined as a solution for a distribution problem. DTO
  was meant to be a coarse-grained data container which efficiently
  transports data between processes (tiers). ~ Adam Bien

From Martin Fowler:

DTOs are called Data Transfer Objects because their whole purpose is
  to shift data in expensive remote calls. They are part of implementing
  a coarse grained interface which a remote interface needs for
  performance. Not just do you not need them in a local context, they
  are actually harmful both because a coarse-grained API is more
  difficult to use and because you have to do all the work moving data
  from your domain or data source layer into the DTOs. ~ Martin
  Fowler

Here is a Java EE specific example of a common but incorrect use of the DTO pattern. If you're unfamiliar with Java EE, you just need to know the MVC pattern: a "JSF ManagedBean" is a class used by the View, and a "JPA Entity" is the Model in the MVC pattern.

So, for example, say you have a JSF ManagedBean. A common question is whether the bean should hold a reference to a JPA Entity directly, or should it maintain a reference to some intermediary object which is later converted to an Entity. I have heard this intermediary object referred to as a DTO, but if your ManagedBeans and Entities are operating within the same JVM, then there is little benefit to using the DTO pattern.

Futhermore, consider Bean Validation annotations (again, if you're unfamiliar with Java EE, know that Bean Validation is an API for validating data). Your JPA Entities are likely annotated with @NotNull and @Size validations. If you're using a DTO, you'll want to repeat these validations in your DTO so that clients using your remote interface don't need to send a message to find out they've failed basic validation. Imagine all that extra work of copying Bean Validation annotations between your DTO and Entity, but if your View and Entities are operating within the same JVM, there is no need to take on this extra work: just use the Entities.

IAmTheDude's link to  Catalog of Patterns of Enterprise Application Architecture  provides a concise explanation of DTOs, and here are more references I found illuminating:

HOW TO DEAL WITH J2EE AND DESIGN PATTERNS

How to use DTO in JSF + Spring + Hibernate

Pros and Cons of Data Transfer Objects

Martin Fowler's description of DTO

Martin Fowler explains the problem with DTOs -- apparently they were being misused as early as 2004


答:
Another way to express the logical operators as an integer arithmetic expressions (where feasible). This way can avoid lots of branching for a larger expression of many predicates..

Let True be 1
Let False be 0

if the summation of both is greater than 1 then it is true or false to be returned.

```<code>boolean isOR(boolean arg1, boolean arg2){

   int L = arg1 ? 1 : 0;
   int R = arg2 ? 1 : 0;

   return (L+R) > 0;

}
</code>```

Title: How to define "or" logically

Body:
Another way to express the logical operators as an integer arithmetic expressions (where feasible). This way can avoid lots of branching for a larger expression of many predicates..

Let True be 1
Let False be 0

if the summation of both is greater than 1 then it is true or false to be returned.

boolean isOR(boolean arg1, boolean arg2){

   int L = arg1 ? 1 : 0;
   int R = arg2 ? 1 : 0;

   return (L+R) > 0;

}


问:
Should a front-end developer ever specify JSON format for back-end developers?
I am taking the front-end role in a project. Should I be specifying for my back-end teammates the exact format of JSON that their PHP returns to my JavaScript?

For example, should I be telling them they should use a format similar to one described here:

Proper way to structure JSON for front-end consumption

Or should I keep my role as sterile as possible, and simply describe in words the inputs and outputs I need from their back-end interface? (Of course, if this happens it might be more difficult on my part to handle their different data structure formats)

答:
This is a conversation you should be having together, discussing the requirements and pros and cons of different formats.

If one side or the other is dictating what happens, you're going to end up with bad software and an unhappy team.


答:
In one of Tom Kyte's books he described a company with this exact problem. They did what you did and the performance was terrible.  His recommendation was to store each instance of a recurring appointment in the database up to some point in the future.  This solved all their performance problems with appointment searches.

Yes, there may be hundreds of occurrences of some appointments.  So what?  You only need to store the start times, and that's only a few kilobytes.  And you don't need to store them in the indefinite future.  Two or three years is plenty.  When is the last time you wanted to know what appointments you had on some date in 2017?

Title: Concept for dealing with recurring appointments

Body:
Our company implemented a calendar system a few months back with recurring appointments,  using iCal strings to store the recurring appointment criteria.

We are starting to run into performance problems because of these.

Our current thinking is something along these lines...

A user requests all appointments for a date range.  Typically no more than 30 days.

We grab all non-recurring events from the database that fall in this date range.
Then we grab ALL events that contain an iCal string and run each one through a processor to determine if the event happens to repeat in our requested date range.  For example, the user may have the event starting on 1/1/14, repeating every Tuesday and Wednesday ongoing forever.  Another event, however, may begin on 1/1/14, repeating every Tuesday and Wednesday for one month and is now outside of our selected range.  The first one would repeat to our date range and be included.  The second one would not.

As far as I can tell, there really isn't any good way to determine any kind of filtering so that I don't have to pull every single one of these events from the database and test them to see if they match our date range.

As the number of recurring events grows, our database is being slogged down with requests that return more and more records, and our server needs to process each of these, most of which aren't necessary, but we don't know until we get them to the server and process them.

Has anybody faced this before, and is there a better algorithm that we can implement to limit the number of records returned?