答:
You don't need any DB operation for synchronization between the operations.

If you need all the 3 operations to be performed on your data sequentially :

Send your Data to Queue A, which has consumers which perform operation A and at the end of it, send it to Queue B. Consumers on Queue B will perform Operation B and send the data to Queue C where consumers will be waiting to perform operation C on it.

If you need all the 3 operations to be performed on your data, but sequence is not important :

Keep only one Queue with 10 Consumers for each Operation (5 on each machine) Add headers OpA=true, opB=true, opC=true to your data when adding it to the Queue for the first time. The Consumers for Operation A should only select those WHERE opA=true . Use Selective Consumer for this (http://camel.apache.org/selective-consumer.html) When a Consumer gets the message, it should perform it's respective operation, remove it's respective header and send the data back to the same Queue (only if there are any more headers after removing it's own header).

Title: Design a Queuing Solution with Clustering and Multiple Consumers

Body:
It is a Design Problem which I am listing out here.

I have different set of business operations that are carried out for different business entities.

Operations:

Operation A

Operation B

Operation C

For Example I have an Entity A. Entity A's data could be in parts, for example:

Entity A's (Jan Data)

Entity B's (Feb Data) etc.

To complete a Use Case all Operations (A,B,C) should be performed. Now these operations are performed and are independent of each other and can be performed in parallel, the only condition is that they should be of different Entities. So Entity A can't have all the operations (A, B or C) executing in parallel. And these operations are running on server side.

How to scale this and provide a solution?

I am thinking of following solution and would like to have inputs from the community on this.

I am thinking of three Queues for operations which I mentioned above

Queue A carrying out Operation A

Queue B carrying out Operation B

Queue C carrying out Operation C

And all the Consumers will be listening to these Queues.

Consumer A (or multiple consumers)

Consumer B (or multiple consumers)

Consumer C (or multiple consumers)

And my server would be load balanced, and I will be having a Single Message Queue containing these three queues.

So it is possible that I have 2 servers running and on every server there are for example 5 threads (Consumers) running, so there will be 10 instances of Consumer A running in parallel picking the data from the Message Queue A.

As I stated earlier that for the same entity A(that is the business use case which I have) all these operations (Operation A, Operation B and Operation C) can't be running in parallel, they should be only of them being executed.

So what I am thinking is to have a database entry for the Entity A and all the consumers must check whether there is a Database Entry for Entity A,

if not then

Make an entry to Database for Entity A

Go and execute the Operation

Remove the Entry from the Database for Entity A

if there is an entry in Database found

Enqueue the Data for Entity A again from the Queue where it was picked.

Is there any better solution possible for such a design problem?


问:
Object creation: when should I expose a factory vs wrapping class?
I am having some problems trying to figure out when to use a factory or to wrapper class. This question is slightly geared towards C# I guess, so I'm not sure if this is the correct place to ask.

Say there is a library that exposes an interface called IStackExchangeClient, and multiple differing implementations of that interface (private implementations that clients will not know about), HttpStackExchangeClient, UdpStackExchangeClient.

We can allow the object creation of the interface through a factory, like StackExchangeClientFactory.Create(), or we can wrap it up in another class like StackExchangeClient that internally does the same thing, but simply proxies.

In the second instance, clients can write new StackExchangeClient() which feels much more natural to me. So my question is, when do I choose one or the other? What are some of the considerations that I should be taking in?

I've thought hard about it and I can't choose between one style or the other, but clearly some parts of the .NET framework use factories, and some parts don't, even when there are multiple underlying implementations of its interface.

Thank you!

答:
Use a wrapping class when the interface is private, i.e. the user can't create their own new derived classes. This is usually a pretty questionable design.

Otherwise, make the interface and derived classes public.

Don't use a factory method unless it has some language-required advantage, e.g. generic type deduction, which you can see in Tuple.Create.

ContentLicense: CC BY-SA 3.0


问:
Is the rule of 5 a valid extension of the rule of 3, or does it imply premature optimization?
I'm familiar with the notion in c++ of the rule of 3, however since the release of C++11 I've seen some sources suggesting it should be extended to a "rule of 5", I.e. the move constructor and move assignment operator should also be implemented whenever the others are. What is the rationale behind such a rule? My understanding is that in most cases implementation of move semantics is only necessary as an optimization - am I wrong about this, or is the so-called rule of five about optimizing my code (and, therefore, substantially less important than the rule of 3, which is about avoiding pitfalls that can lead to unexpected behaviors)?</p>

答:
Move semantics were added to C++11 in such a way that the "rule of 3" is still valid and sufficient to avoid the pitfalls that it is meant to avoid (althogh there are better ways in most situations by applying the "rule of 0").

There are also no additional pitfalls added to the language that the "rule of 5" would help you avoid. In that sense, the rule of 5 is more about optimization than about avoiding pitfalls.

On the other hand, treating the "rule of 5" as an extension to the "rule of 3" does help you to remember that in C++11 there are two additional special member functions that you need to think about when your class does something special that needs a destructor or copy-constructor.

And although move semantics are an optimization, providing a move-constructor and/or move-assignment operator doesn't mean you are prematurely optimising, just like selecting the right algorithm isn't a premature optimization.

Adding support for move semantics to your class, when it makes sense, usually takes little effort and doesn't detract from the readability/maintainability of the code.


问:
Algorithm to merge two sorted arrays with minimum number of comparisons
Given are two sorted arrays a, b of type T with size n and m. I am looking for an algorithm that merges the two arrays into a new array (of maximum size n+m).

If you have a cheap comparison operation, this is pretty simple. Just take from the array with the lowest first element until one or both arrays are completely traversed, then add the remaining elements. Something like this https://stackoverflow.com/questions/5958169/how-to-merge-two-sorted-arrays-into-a-sorted-array

However, the situation changes when comparing two elements is much more expensive than copying an element from the source array to the target array. For example you might have an array of large arbitrary precision integers, or strings, where a comparison can be quite expensive. Just assume that creating arrays and copying elements is free, and the only thing that costs is comparing elements.

In this case, you want to merge the two arrays with a minimum number of element comparisons. Here are some examples where you should be able to do much better than the simple merge algorithm:

```<code>a = [1,2,3,4, ... 1000]
b = [1001,1002,1003,1004, ... 2000]
</code>```

Or

```<code>a = [1,2,3,4, ... 1000]
b = [0,100,200, ... 1000]
</code>```

There are some cases where the simple merge algorithm will be optimal, like

```<code>a = [1,3,5,7,9,....,999]
b = [2,4,6,8,10,....,1000]
</code>```

So the algorithm should ideally gracefully degrade and perform a maximum of n+m-1 comparisons in case the arrays are interleaved, or at least not be significantly worse.

One thing that should do pretty well for lists with a large size difference would be to use binary search to insert the elements of the smaller array into the larger array. But that won't degrade gracefully in case both lists are of the same size and interleaved.

The only thing available for the elements is a (total) ordering function, so any scheme that makes comparisons cheaper is not possible.

Any ideas?

I have come up with this bit in Scala. I believe it is optimal regarding the number of comparisons, but it is beyond my ability to prove it. At least it is much simpler than the things I have found in the literature.

And since the original posting, I wrote a blog post about how this works.

答:
The normal merge sort algorithm - merge step with normally apply n + m -1 comparisons, where one list is of size n and and the other list is of size m. Using this algorithm is the most simplest approach to combine two sorted lists.

If the comparisons are too expensive you could do two things - either you minimize the number of comparisons or you minimize the cost of comparisons.

Let's focus on the minimization of the cost of comparison. You and only you can decide whether the data you are comparing can be quantized or not. If you can quantize them, which is a form of implementing a hash method, which is keeping the ordering. E.g. if your Data is compared by Name, Then the first tname, ... you can take the first to Chars of the name "Klaehn, Ruediger" and reduce/quantize your data element to "Kl.Ru", if you compare it to "Packer, The" you preserve the ordering "Pa.Th" - you can now apply a cheaper comparison algorithm, comparing the reduced values. But if you find another "Kl.Ru", you now have a near value, and you might now switch to a more expensive approach comparing these elements.

If you can extract this quantized value from your data, faster than comparing it, this is the first thing you do, you compare the quantized or hashed value first. Please keep in mind, that this value needs to be computed only once, so you can compute it on creating the data element.

I also mentioned another way, to minimize your comparisons.

I had a look into the classic book TAOCP- Volume 3-Sorting and Searching, (pp.197-207, section 5.3.2) which has full 10 pages on this topic. I found two references to algorithms which are faster than n+m-1 comparisons.

First there is the Hwang-Lin merge algorithm and the second an improvement by Glenn K Manacher - both are cited by TAOCP as well as an algorithm by Christen, which approaches the lower bound of needed comparisons, on special conditions on the length n and m of the lists.

The algorithm of Manacher was presented in Journal of the ACM Vol. 26 Number 3 on pages 434-440: "Significant Improvements to the "Hwan-Lin" Merging Algorithm". the list with m items and the list with n items can be of different length, but they must also be odered by the numbers of elements they contain m<=n

The Hwang-Lin algorithm breaks the lists to merge, apart to smaller lists and sorts the lists by comparing the first element of each sublist, and to decide whether some elements in the sublist need to be compared or not. If the first list is smaller than the second list, then the chance is high, that consecutive elements of the longer list can be transferred into the resulting list without comparison. If the first element of the small ist is greater than the first element of the splitted larger list, all elements in front of sublist can be copied without comparison.

Average case analysis of the merging alorithm of Hwang and Lin (Vega, Frieze, Santha) in Section 2 you can find a pseudocode of the HL-Algorithm. Which is a lot better than my description. And you can see why there are fewer comparisons - the algorithm uses a binary search, to find the index, where to insert the element from the shorter list.

If the lists are not interleaved like in your last example, you should have a remaining smaller and a remaining larger list in most cases. This is when the the HL-algorithm starts to perform better.


答:
The Rule of Five is ex-idiomatic. It was only idiomatic for a very brief period before the Rule of Zero.

The principle of the Rule of 3 became obsolete when writing your own resource-handling classes became obsolete, which is when your compiler supports rvalue references. If you implement in terms of unique_ptr, which you can for virtually every resource because the deleter is so customizable, you only need a custom copy assignment and copy constructor- so two. If you need to support copying. There's no reason to implement your own destructor, move assignment operator, or move constructor in the vast majority of cases.

Rule of Three code isn't broken but it's much harder to maintain than Rule of Zero code and not as efficient either. Move operations offer correctness in many cases like unique_ptr so you should strive to support them wherever possible.

It's copy operations that are now largely redundant in the face of moves, not the other way around.

In other words, unique_ptr is so flexible, and defaulted operations so useful, that there's hardly ever any reason to implement your own special members anymore. In fact, there's hardly any reason to implement your own resource handling classes. And since those classes virtually never exist, there's no reason to to have an idiom to construct them.

Title: Is the rule of 5 a valid extension of the rule of 3, or does it imply premature optimization?

Body: I'm familiar with the notion in c++ of the rule of 3, however since the release of C++11 I've seen some sources suggesting it should be extended to a "rule of 5", I.e. the move constructor and move assignment operator should also be implemented whenever the others are. What is the rationale behind such a rule? My understanding is that in most cases implementation of move semantics is only necessary as an optimization - am I wrong about this, or is the so-called rule of five about optimizing my code (and, therefore, substantially less important than the rule of 3, which is about avoiding pitfalls that can lead to unexpected behaviors)?</p>


问:
What kind of user info is ok to be stored as plain text in SQL Database?
I'm practicing around building e-commerce asp.net applications that allows for users to register to the site and their user credentials are stored in a MySQL database. In my sample project the registration asks for the users username, password, full name, phone #, email, home address (for shipping of products purposes).

I know its good practice to store the user's password as a salted hash.  But is there any other info that should be stored the same way or not?  For example should the username also be stored as a salted hash or even encrypted?

I guess I'm just wondering what kinds of information is it ok to be stored in plain text in MySQL database for an asp.net web app? And which items are recommended to be stored in a salted hash? And which items should be stored using encryption?

答:
Every e-commerce system I've seen stores the basic user data - name, addresses, etc - unencrypted.  I think that's the answer you are after.  The only pieces of data that are typically encrypted are the password (hashed) and credit card info (if that's stored on site).  I would probably add to that SSN's and any other sensitive financial information (bank account #'s) should be encrypted.

The reason for this is pretty simple - searching.  You can't do queries such as "last_name LIKE 'SMIT%'" when the data is encrypted.

You might, though, investigate hard drive encryption if you are concerned about the physical security of the machine.


问:
Overriding methods by passing as argument the subclass object where the supertype is expected
I am just learning Java, and am not a practicing programmer.

The book I am following says that when overriding a method, the argument types must be the same, but the return types can be polymorphically compatible.

My question is why can't the arguments passed to the overriding method not be a subclass type of the super-type expected?

In the overloaded method, whatever method I call on the object is guaranteed to be defined on the object.

Notes on suggested duplicates:

The first suggestion appears to be about class hierarchy and where to put functionality.  My question is more focused on why the language restriction exists.

The second suggestion explains how to do what I'm asking, but not why it has to be done that way.  My question is focused on the why.

答:
The problem is not what you do in the overriding method with the object given as argument.  The problem is what is the type of arguments the code using your method is allowed to feed to your method.

An overriding method must fulfill the contract of the overridden method.  If the overridden method accepts arguments of some class and you override it with a method that accepts only a subclass, it doesn't fulfill the contract.  That is why it is not valid.

Overriding a method with a more specific type of argument is called overloading.  It defines a method with the same name but with a different or more specific type of argument.  An overloaded method is available besides the original method.  Which method is called depends on the type known at compile time.  To overload a method you have to drop the @Override annotation.


问:
Workaround for Java checked exceptions
I appreciate a lot the new Java 8 features about lambdas and default methods interfaces. Yet, I still get bored with checked exceptions. For instance, if I just want to list all the visible fields of an object I would like to simply write this:

```<code>    Arrays.asList(p.getClass().getFields()).forEach(
        f -> System.out.println(f.get(p))
    );
</code>```

Yet, since the get method might throw a checked exception, which does not agrees with the Consumer interface contract, then I must catch that exception and write the following code:

```<code>    Arrays.asList(p.getClass().getFields()).forEach(
            f -> {
                try {
                    System.out.println(f.get(p));
                } catch (IllegalArgumentException | IllegalAccessException ex) {
                    throw new RuntimeException(ex);
                }
            }
    );
</code>```

However in most cases I just want the exception to be thrown as a RuntimeException and let the program handle, or not, the exception without compilation errors.

So, I would like to have your opinion about my controversial workaround for checked exceptions annoyance. To that end, I created an auxiliary interface ConsumerCheckException<T> and an utility function rethrow (updated according to the sugestion of Doval's comment) as follows:

```<code>  @FunctionalInterface
  public interface ConsumerCheckException<T>{
      void accept(T elem) throws Exception;
  }

  public class Wrappers {
      public static <T> Consumer<T> rethrow(ConsumerCheckException<T> c) {
        return elem -> {
          try {
            c.accept(elem);
          } catch (Exception ex) {
            /**
             * within sneakyThrow() we cast to the parameterized type T.
             * In this case that type is RuntimeException.
             * At runtime, however, the generic types have been erased, so
             * that there is no T type anymore to cast to, so the cast
             * disappears.
             */
            Wrappers.<RuntimeException>sneakyThrow(ex);
          }
        };
      }

      /**
       * Reinier Zwitserloot who, as far as I know, had the first mention of this
       * technique in 2009 on the java posse mailing list.
       * http://www.mail-archive.com/javaposse@googlegroups.com/msg05984.html
       */
      public static <T extends Throwable> T sneakyThrow(Throwable t) {
          throw (T) t;
      }
  }
</code>```

And now I can just write:

```<code>    Arrays.asList(p.getClass().getFields()).forEach(
            rethrow(f -> System.out.println(f.get(p)))
    );
</code>```

I am not sure that this is the best idiom to turn around the checked exceptions, but as I explained, I would like to have a more convenient way of achieving my first example without dealing with checked exceptions and this is the simpler way that I found to do it.

答:
Advantages, disadvantages, and limitations of your technique:

If the calling-code is to handle the checked exception you MUST add it to the throws clause of the method that contains the stream.
The compiler will not force you to add it anymore, so it's easier to forget it. For example:

```<code>public void test(Object p) throws IllegalAccessException {
    Arrays.asList(p.getClass().getFields()).forEach(rethrow(f -> System.out.println(f.get(p))));
}
</code>```

If the calling-code already handles the checked exception, the compiler WILL remind you to add the throws clause to the method declaration
that contains the stream (if you don't it will say: Exception is never thrown in body of corresponding try statement).

In any case, you won't be able to surround the stream itself to catch the checked exception INSIDE the method that contains the stream
(if you try, the compiler will say: Exception is never thrown in body of corresponding try statement).

If you are calling a method which literally can never throw the exception that it declares, then you should not include the throws clause.
For example: new String(byteArr, "UTF-8") throws UnsupportedEncodingException, but UTF-8 is guaranteed by the Java spec to always be present.
Here, the throws declaration is a nuisance and any solution to silence it with minimal boilerplate is welcome.

If you hate checked exceptions and feel they should never be added to the Java language to begin with (a growing number of people think this way,
and I am NOT one of them), then just don't add the checked exception to the throws clause of the method that contains the stream. The checked
exception will, then, behave just like an UNchecked exception.

If you are implementing a strict interface where you don't have the option for adding a throws declaration, and yet throwing an exception is
entirely appropriate, then wrapping an exception just to gain the privilege of throwing it results in a stacktrace with spurious exceptions which
contribute no information about what actually went wrong. A good example is Runnable.run(), which does not throw any checked exceptions.
In this case, you may decide not to add the checked exception to the throws clause of the method that contains the stream.

In any case, if you decide NOT to add (or forget to add) the checked exception to the throws clause of the method that contains the stream,
be aware of these 2 consequences of throwing CHECKED exceptions:

The calling-code won't be able to catch it by name (if you try, the compiler will say: Exception is never thrown in body of corresponding try
statement). It will bubble and probably be catched in the main program loop by some "catch Exception" or "catch Throwable", which may be what you
want anyway.

It violates the principle of least surprise: it will no longer be enough to catch RuntimeException to be able to guarantee catching all
possible exceptions. For this reason, I believe this should not be done in framework code, but only in business code that you completely control.

References:

http://www.philandstuff.com/2012/04/28/sneakily-throwing-checked-exceptions.html

http://www.mail-archive.com/javaposse@googlegroups.com/msg05984.html

Project Lombok annotation: @SneakyThrows

Brian Goetz opinion (against) here: https://stackoverflow.com/questions/27644361/how-can-i-throw-checked-exceptions-from-inside-java-8-streams

Workaround for Java checked exceptions *

NOTE: If you decide to use this technique, you may copy the LambdaExceptionUtil helper class from StackOverflow: https://stackoverflow.com/questions/27644361/how-can-i-throw-checked-exceptions-from-inside-java-8-streams . It gives you the complete implementation (Function, Consumer, Supplier...), with examples.

Title: Workaround for Java checked exceptions

Body:
Advantages, disadvantages, and limitations of your technique:

If the calling-code is to handle the checked exception you MUST add it to the throws clause of the method that contains the stream.
The compiler will not force you to add it anymore, so it's easier to forget it. For example:

public void test(Object p) throws IllegalAccessException {
    Arrays.asList(p.getClass().getFields()).forEach(rethrow(f -> System.out.println(f.get(p))));
}

If the calling-code already handles the checked exception, the compiler WILL remind you to add the throws clause to the method declaration
that contains the stream (if you don't it will say: Exception is never thrown in body of corresponding try statement).

In any case, you won't be able to surround the stream itself to catch the checked exception INSIDE the method that contains the stream
(if you try, the compiler will say: Exception is never thrown in body of corresponding try statement).

If you are calling a method which literally can never throw the exception that it declares, then you should not include the throws clause.
For example: new String(byteArr, "UTF-8") throws UnsupportedEncodingException, but UTF-8 is guaranteed by the Java spec to always be present.
Here, the throws declaration is a nuisance and any solution to silence it with minimal boilerplate is welcome.

If you hate checked exceptions and feel they should never be added to the Java language to begin with (a growing number of people think this way,
and I am NOT one of them), then just don't add the checked exception to the throws clause of the method that contains the stream. The checked
exception will, then, behave just like an UNchecked exception.

If you are implementing a strict interface where you don't have the option for adding a throws declaration, and yet throwing an exception is
entirely appropriate, then wrapping an exception just to gain the privilege of throwing it results in a stacktrace with spurious exceptions which
contribute no information about what actually went wrong. A good example is Runnable.run(), which does not throw any checked exceptions.
In this case, you may decide not to add the checked exception to the throws clause of the method that contains the stream.

In any case, if you decide NOT to add (or forget to add) the checked exception to the throws clause of the method that contains the stream,
be aware of these 2 consequences of throwing CHECKED exceptions:

The calling-code won't be able to catch it by name (if you try, the compiler will say: Exception is never thrown in body of corresponding try
statement). It will bubble and probably be catched in the main program loop by some "catch Exception" or "catch Throwable", which may be what you
want anyway.

It violates the principle of least surprise: it will no longer be enough to catch RuntimeException to be able to guarantee catching all
possible exceptions. For this reason, I believe this should not be done in framework code, but only in business code that you completely control.

References:

http://www.philandstuff.com/2012/04/28/sneakily-throwing-checked-exceptions.html

http://www.mail-archive.com/javaposse@googlegroups.com/msg05984.html

Project Lombok annotation: @SneakyThrows

Brian Goetz opinion (against) here: https://stackoverflow.com/questions/27644361/how-can-i-throw-checked-exceptions-from-inside-java-8-streams

Workaround for Java checked exceptions *

NOTE: If you decide to use this technique, you may copy the LambdaExceptionUtil helper class from StackOverflow: https://stackoverflow.com/questions/27644361/how-can-i-throw-checked-exceptions-from-inside-java-8-streams . It gives you the complete implementation (Function, Consumer, Supplier...), with examples.


问:
Is it a code smell if you are frequently creating an object just to call a method on it
I inherited a code base where there is a lot of code that goes something like this:

```<code>SomeDataAdapter sda = new SomeDataAdapter();
sda.UpdateData(DataTable updateData);
</code>```

And then sda is never used again.

Is that a code smell that indicates that those methods should actually be static class methods instead?

答:
Define "frequently." How often do you instantiate the object? A lot - probably not?

There are likely bigger problems to worry about in your system. Going static will communicate more clearly to the programmer that internal state of the object isn't changing - great.

If state is being messed with though, and you have to start making other things static to make the first thing static, then you have to ask what parts need to be static and what parts don't.

Yes it's a code smell, just a small one.


问:
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)?

答:
Many of your examples seem to be problems in search of a solution. In a real-world scenario, your Fibonacci number function would actually be a function that takes 1 or 2 (for arbitrary subsets of the Fibonacci sequence) int parameters and returns a list of integers, and the calling process would handle getting the input and displaying the output. If you have the code you provided in the real world, it's because the developer never intended it to be anything other than something (s)he ran from the command line.

For many of your other examples, the answer is often simply "because eventually you're going to need to instantiate something and get things done". At some point, for code to run, something has to be an actual implementation. In that case, it's a filesystem library (in addition to the C# library Telastyn listed, there's the os module in Python, and the Java File-related libraries (in java.io and java.nio). Adding another layer of abstraction isn't going to make anything in the examples you listed better.

There's a balancing act that goes into the decision-making process of whether to use some type of abstraction - How much more complicated does this make maintaining the code? (as discussed by Mason Wheeler) How much easier does this make writing the code? Do the benefits I expect to get from this really justify the extra time it takes to put this in place? And perhaps the most important question - Do I really anticipate there ever being more than 1 implementation here?

You find you come to a point, often much sooner than you seem to think, where adding more abstraction just isn't worth the effort.


答: Make it a static method and move on. There is nothing wrong with static methods. Once a pattern of use emerges then you can worry about abstracting the pattern.</p>

Title: Is it a code smell if you are frequently creating an object just to call a method on it

Body:
I inherited a code base where there is a lot of code that goes something like this:

```<code>SomeDataAdapter sda = new SomeDataAdapter();
sda.UpdateData(DataTable updateData);
</code>```

And then sda is never used again.

Is that a code smell that indicates that those methods should actually be static class methods instead?


问:
Is there a useful correlation between choice of web-browser/search engine and good quality programmers?
After a round of interviews earlier this year, which included some practical questions with access to a computer, I noticed that, at least for the applicants we were seeing, there was a high inverse correlation between quality of applicant and the explicit choice of Bing/IE as the stack for search/browsing the web. That is, a large number of applicants specifically searched for IE to use as a browser when browsing the web, and specifically used Bing as the search engine (even typing in Bing in the Google search box, before starting a search)

Is this just an outlier, a fluke, or is there some sort of information I can obtain from this observation? So far, my best candidates are:

Yes, there is a high correlation between good programmers and choice of web browser/search engine

There is some training program, or community, which advises using IE/Bing during interviews, and I just happened to run into a poor selection from that group

Something else is going on, that I don't know about, but someone here might know.

So, to repeat, the question is: is there more than coincidence at work here, and, if there is, what's going on?

答:
You're trying to make an argument of the form:

p -> q

q therefore p

ie:

if 'they were bad programmers' (p) then 'they used IE' (q)

someone uses IE (q) therefore they are a bad programmer (p)

This is affirming the consequent. It's a logical fallacy, and it's a ridiculous proposal in the first place. You are not your browser. There's nothing to be obtained from this observation other than the fact that you had a bunch of bad candidates and have to better your phone screen process.

ContentLicense: CC BY-SA 2.5


问:
Common techniques to integrate social media login to a website that requires account registration
I have developed websites where users would register with a username of their choice and an entry would be created in a database table that holds their username and some sort of salted password. They would then use this account to interact with the services that the site provides.

Now I am interested in providing the ability to sign up an account using facebook, twitter, and basically anything else that might be popular. However, I don't know how to integrate the login workflow into my system.

I have read the docs for Facebook Login but am still not sure how I would integrate it with my user-account design.

This is how my own registration workflow works

Choose username/password

Create an entry in the "users" table

Authenticate by checking the username and see if the password matches

With facebook login, they would ideally simply click a button that says "login with facebook" and they would authenticate with their facebook credentials. However, I don't know where I would go about creating a user to associate with their facebook identity.

So my question is...

What is a common way to use facebook (or other social media such as twitter or linkedin) to register an account into the system? I would like to manage all users uniformly, so no matter how they choose to register an account, they would all be handled in a single users table where I could do queries and associations without having to worry about how the account was created or how they logged in.

答:
If you use python for your server side, checkout Python Social Auth

This way you won't need to get into the complexities of OAuth. I have integrated it into my website, which uses the Django framework, without needing to write even a single line of additional code for social login.


答:
The smell here is that this is procedural code wrapped (minimally) in objects.

There must be a reason why you want to perform that operation on the data table, but rather than modelling that operation with other related operations that share some semantics (i.e. have a common meaning), the author just spun up a new procedure inside a new class & called it.

In a functional language, that's how you'd do things ;) but in the OO paradigm, you'd want to model some abstraction that included that operation. Then you'd use OO techniques to flesh out that model and provide a set of related operations that preserve some semantics.

So the main smell here is that the author is using classes, probably because the compiler requires it, without organizing operations into a conceptual model.

BTW watch out anytime you see the program being modeled instead of the problem space. It's a sign that the design could benefit from more thought. In other words, watch out for classes all being "xAdaptor" and "xHandler" and "xUtility", and usually tied to an anemic domain model. It means someone is just bundling code up for convenience, as opposed to actually modelling the concepts they want to implement.


问:
Design solution for communicating between multiple layers
We are developing a framework that has several layers and would be deployed in a multi-threaded environment. Each layer may have its own input/output data type. The top layer takes the input, performs some actions on the data and passes it to the next layer. Based on the output from next layer, the top layer would perform another set of operations on the data. Similar interactions happen between other layers.

There are two use cases that we did not incorporate in the original design:

Generating metrics over what operations each layer performs on the data.

The change in data in one layer affects the operations in another layer, the changes should be recorded and each layer should have capability to fetch the list of changes.

After some thinking we came up with two approaches to accommodate these requirements in the existing design:

Use observer pattern and let each layer report its changes. These changes can be then pulled by the layer that needs to use it.

Pros:

Since we have already come up with a design for the framework and most of work is complete. This approach would not require significant changes to the existing design, if any.

Cons:

It becomes difficult to manage(record and report back) changes corresponding to each layer.

There is a central class that aggregates all the data for each request. This class acts like a global variable. It has to be initialized and deleted by the top layer. Adding another top layer could be error prone.

Extend the current data classes using an interface equivalent to EventContainer. This way all the layers would record their changes and spit them in their output.

Pros:

This approach is much more cleaner and extendable than observer pattern.

Cons:

It would require significant design changes.

The concept of data having events as well merges two separate concerns into one and does not seem like a good idea.

We would like to know if there is a design pattern or any other solution that solves this problem?

Which of the above two solutions should be given higher preference considering that we would want a flexible, extendable and cleaner solution?

答:
Making the whole communication between your layers event-based would probably solve your problems. But instead of adding the event mechanics to the existing paths of communication (which might merge two separate concerns, as you wrote), you could replace the existing way of communication completely by an event-based system.

If each of your layers communicates with each other layers just by events, it will typically be only a small step to make those layers independent from each other, which will help to decrease the overall compile-time coupling in your system, with advantages like easier testing, easier monitoring, easier event-recordind.

Two architectural patterns which can help you to achieve this are Command Query Responsibility Segregation (CQRS) and Event Sourcing. Especially the latter will persist every interesting event, which will make it very easy to solve exactly the two problems you mentioned at the beginning of your question.

Of course, as you have correctly noted by yourself, this might be a significant design change, and you have to decide by yourself if that is feasible in your current situation.


答:
I suppose I can put up my own experience and let others infer the relationship.  I like to think I'm a good developer.  And here are the browsers installed on my system:

IE 9 Beta (32 and 64 bit)

Chrome Dev. 9.0

Firefox Minefield 4.08pre (64 bit)

Firefox 3.6.13 (32 bit)

Opera 10.63

Safari 5.03

This is because I care about web standards and want my development to look good and work proper on as many platforms as I can muster.  All being told, Chrome is my current browsing favorite due to its top-shelf performance on JavaScript-heavy sites and decent library of add-ons now.

Oh, edited to add: I will set my default search engine to Google in every case.

Title: Is there a useful correlation between choice of web-browser/search engine and good quality programmers?

Body:
After a round of interviews earlier this year, which included some practical questions with access to a computer, I noticed that, at least for the applicants we were seeing, there was a high inverse correlation between quality of applicant and the explicit choice of Bing/IE as the stack for search/browsing the web. That is, a large number of applicants specifically searched for IE to use as a browser when browsing the web, and specifically used Bing as the search engine (even typing in Bing in the Google search box, before starting a search)

Is this just an outlier, a fluke, or is there some sort of information I can obtain from this observation? So far, my best candidates are:

Yes, there is a high correlation between good programmers and choice of web browser/search engine

There is some training program, or community, which advises using IE/Bing during interviews, and I just happened to run into a poor selection from that group

Something else is going on, that I don't know about, but someone here might know.

So, to repeat, the question is: is there more than coincidence at work here, and, if there is, what's going on?


问:
Developing a compiler for a self made CPU Architecture
Recently ive been consumed by creating my own simple CPU architecture that at some point could be easily implemented in hardware (No FPGA, but actual Logic Gate circuits). Naturally to fulfill this requirement i went with a simple 4 Bit CPU, with a 4kB program space and 256 Byte RAM.

It supports all the fundamental operations such as ADD, Subtract, AND, LOAD, STORE etc. Before i start committing this to hardware i want to develop a moderately powerful software stack that could compile the a C/ C like language for the architecture, so the cpu could be programmed using a high level language. Currently i have written a working assembler in VB.NET, but now im stuck on how to approach the final goal of a working compiler.

Specifically i have the following questions:

What should be my next step, and how should i approach writing a compiler?

Even though a 4 Bit CPU is simple, it is not very useful as it cannot handle large calculations at once, thus my final goal would be abstract this inability by developing a software stack that to the user would be like programming a 16 bit (or larger) CPU. Currently i manually write assembly that can span larger numbers over multiple registers and perform calculations between them, but ultimately what part of the software stack deals with handling numbers and calculations which are greater than the size of the physical registers?

What part of the software stack deals with Subroutine calling etc?

Please let me know if i need to clarify anything.

答:
LLVM backend is the primary sane way of doing this. If you lower LLVM IR to assembly or microcode, you can roll from there and just use the numerous LLVM frontends to convert higher languages like C++ into LLVM IR.

In other words, LLVM was explicitly designed to support this scenario.

The full stack goes like this:

Frontend (e.g. Clang for C and C++) - source code -> LLVM IR

Optimizer (LLVM) - LLVM IR -> LLVM IR

Backend (you) - LLVM IR -> assembly/microcode/whatever

The first part is provided for you on a per-language basis. So for C and C++, you can use Clang, for D you can use LDC, etc. The second part is provided by LLVM- they provide a large number of target-independent optimization routines and some target-aware ones. Finally, you provide a translation service from LLVM IR to your architecture-specific code.

Note that LLVM IR makes a few guarantees, because they are targetted at real platforms. For example, they assume IEEE754 floating-point support and 8-bit bytes as well as various types of pointer support. You will need to support all of these anyway if you want to compile languages like C to target your architecture in general. If you are willing to restrict the source language a bit beyond normal you can get away without implementing all of these features- for example, if the C code doesn't use floats, in principle there's no reason why the frontend should emit float-using LLVM IR code.

LLVM IR is a common middle-ground that you can compile any language to target, and then from there, can be lowered for any CPU. Basically, all you need to do is support the primitives, and then provide an LLVM backend to convert from LLVM IR to your assembly. LLVM and language frontends will do all the rest.


问:
How did programming work when programmers used punchcards?
I saw this: Learning to program on punchcards

and I've seen this: http://en.wikipedia.org/wiki/Computer_programming_in_the_punched_card_era

but they leave much to be desired.

Can anyone explain to me how programmers programmed when they used punchcards? Specifically, the system of input computers used to derive instructions from the punched card itself.

I do not know much about retrocomputers, so any one system of input you are familiar with would be fine. More general answers are also appreciated.

答:
In some ways "not too different", in some ways "very different".

They wrote source code, usually on paper. This was then re-typed onto punch cards (one line of source code per card, usually with line numbers, so you could easily fix up your source code when it ended up in the wrong order).

The program was then prefixed with cards having instructions for how to compile the program and suffixed with instructions for how to run it and what data file(s) to use (or tapes to mount, etc, etc).

This as then submitted to the operators, who would load the cards into a speed-reader, run the job and return the results on print-outs.

With a typical edit/compile/results cycle being on the order of "tomorrow", people tended towards being more paranoid bench-checking their code for syntax errors.

With no possibility of interactive debugging, the only way to trace the execution would be debug lines printed during the execution.

In some places, the code would be on punch tape rather than on punch cards.


问:
How do I set up a micro-services architecture that can take advantage of a common, centralized security service?
I've obviously heard a lot about the Micro-Services Architecture and think it makes a lot of sense (especially with the success stories of Netflix).

I'd like to implement a small Grails application in Micro-Services (although the framework doesn't matter too much). My question is about the "Security" or "Users" Micro Service. My initial thought would be to create an application with a REST interface where my other Micro-Services would query the Security Service's REST interface. However, security would be duplicated in every service. This seems like an inevitable problem, but what is the best way to handle this?

Should I be implementing Spring Security in every service and querying a User Service with simple rights information? Or could a central Security Service work in some way?

答:
To avoid duplication and improve maintainability, I would go with the central Security Service.

It is said that an image says more than thousand words:

OAuth is the common way to secure RESTful APIs.

Look at OAuth article on Wikipedia].


答:
as @user454322 says, look up the OAuth2 spec ... it's widely-used and well-supported standard that will work well.

There are (bascially) 2 ways to deploy an authorization server:

1) As a reverse proxy (as shown in @user454322 's diagam). This is when all requests from the outside go through the OAuth2 server and then to your services. This centralizes the authorization concern, so that it's handled before any request reaches the services. This is the same as terminating SSL in the load balancer. In essence, the authorization server becomes a part of your network middleware, like the firewalls and load balancers.

the primary downside is that implementing a reverse proxy can be tricky, especially if you have large payloads, or are doing clever things with HTTP (HTTP starts simple, but there are lots of complicated wrinkles it adds)

you can buy "API management" solutions which provide the reverse proxy functionality, but add things like OAuth, metrics, throttling, etc.

2) As an authorization server. This is a slightly different layout, where each service takes requests directly (from the load balancers, etc.) Each request comes with an access token in the header. The service then makes an HTTP call to the authorization service to validate the token. The authorization server is responsible for authenticating users and granting tokens in the first place, your micro-services don't have to do that part.

the primary downside is that each incoming request has to make a round trip to the auth service. That adds to your latency.

a secondary downside is that you have to make sure the every one of your services calls the auth service -- otherwise, any services you don't will be open to the internet and unprotected.

Title: How do I set up a micro-services architecture that can take advantage of a common, centralized security service?

Body:
I've obviously heard a lot about the Micro-Services Architecture and think it makes a lot of sense (especially with the success stories of Netflix).

I'd like to implement a small Grails application in Micro-Services (although the framework doesn't matter too much). My question is about the "Security" or "Users" Micro Service. My initial thought would be to create an application with a REST interface where my other Micro-Services would query the Security Service's REST interface. However, security would be duplicated in every service. This seems like an inevitable problem, but what is the best way to handle this?

Should I be implementing Spring Security in every service and querying a User Service with simple rights information? Or could a central Security Service work in some way?


问:
Efficient way to represent hexagonal board logic for abalone-like games
I have to implement the AI for Abalone game and I'm wondering what is the best way to represent the board logic using Java without wasting too much resources in all checks and updates routines involved.

Is better use various lists? A matrix of Cell objects?
Any suggestion?

答:
1) since the board has a fixed-size that you're just going to be feeding into an AI anyway, you could just represent it as a one-dimensional array with the right number of cells, where each cell represents a space on the board. Mapping that one-dimensional array to the screen for presentation might be a little weird, but it's a problem you only have to solve once.

The same would apply to validating moves; you could either create an adjacency matrix that tells which cells are adjacent to others, or come up with logic to determine adjacency. Either way, it would be a one-time cost.

2) Notice that every hex grid is also a two-axis grid, except that the axes are 60 or 120 degress from each other, instead of 90 degrees like grids where X and Y are perpendicular. (Old hex-tile boardgames used this trick to label the hexes 1, 2, 3 in one direction, and AA, BB, CC in the other, skewed, direction.)

I've looked at source code for computer implementations of board games that use (2), and they provided routines like "find a line between hexes" or "find the distance between hexes." That was a long time ago, so the details are lost to time, but I remember it wasn't too hard.

(It was more integer math rather than the Pythagorean stuff. ;))


问:
where should put showSchedule method in school system
In my school management system I have this partial  class diagram:

1- In one use case student want to see his schedule of classes in week. this is what i suppose to do:

get student object from session and call method getSchedule() on that and it will give me schedule(this method calls StudentCourse, Course for getting schedule).

Is this good place to put getSchedule() method or I should place it elsewhere?

..................................................

2- In another use class student's parent want to see his child class schedule, I plan to do something like this:

Because I have some use cases that parent want to see some other student information(course mark...), I create interface StudentParentInterface that have method getSchedule() and other methods and student implement StudentParentInterface, then parent has reference to StudentParentInterface not student obj directly.

parent call getSchedule() method on StudentParentInterface, is this correct?

答:
As said in the comments on the OP, I don't think an interfacing class class makes sense in this instance since there is a direct relationship between Students and Parents.

Think about the relationships of the concepts outside of the system. Does every student have a schedule?

By my limited knowledge I'd say it sounds like it. It also sounds like you'll want parents to possibly have something student.getGrades(). So your you'll probably have multiple parents to one student. The student wouldn't need to be aware of the parent but the parent needs to be aware of the student so each Parent object will have a Student property and would probably call getSchedule() and getGrades() etc.


问:
Sacrificing readability for shorter compiled code
I'm currently revisiting some code I wrote in sass when I was learning it and I came upon a particular issue. I was able to write the code in 2 ways, with the first being the more readable code and the second being the shortest code when compiled to css. I was wondering whether it's a good thing to sacrifice some readability for smaller compiled file sizes.

In the current example, option 1 takes up about 60% less characters compiled in css. At 12 colums, the first option uses ~750 less characters than the second option. This is actually pretty significant.

The example is pretty straightforward, but when extending the code readability of the sass file can become more complicated. Is it worth sacrificing this for the smaller compiled file?

Option 1

```<code>// Create all columns
@for $i from 1 through $column_amount {
  .col-#{$i}-lg, .col-#{$i}-md, .col-#{$i}-sm {
    width: $i * $col-size;
  }
}

// At medium screen and below set all large columns to 100%
@media all and (max-width: $screen-medium-max) {
  @for $i from 1 through $column_amount {
    .col-#{$i}-lg {
      width: 100%;
    }
  }
}

// At small screen and below set all medium columns to 100%
@media all and (max-width: $screen-small-max) {
  @for $i from 1 through $column_amount {
    .col-#{$i}-md {
      width: 100%;
    }
  }
}
</code>```

Option 2

```<code>// Creating the columns
@for $i from 1 through $column_amount {
  .col-#{$i}-lg, .col-#{$i}-md, .col-#{$i}-sm {
    width: $i * $col-size;
  }

  // At medium screen and below set all large columns to 100%
  @media all and (max-width: $screen-medium-max) {
    .col-#{$i}-lg {
      width: 100%;
    }
  }

  // At small screen and below set all medium columns to 100%
  @media all and (max-width: $screen-small-max) {
    .col-#{$i}-md {
      width: 100%;
    }
  }
}
</code>```

If any extension exists that does this for me, it'd be nice to know about it, but it won't answer the question

答:
The level of optimization that you describe can be a useful consideration in the final stages of the project:
your project is finished, meets the requirements,
everything is working well,
and you would like to squeeze a few extra bits of performance out of it without changing any other aspect.

If you are at this stage,
and the performance gain is believed to improve the success of the project,
then by all means do it.

If you are not at this stage yet,
then spend your time and effort on something else that's higher priority.
Until your project is mostly done,
it may still go through changes, sometimes even big changes.
This piece of code that you would optimize might get refactored and possibly even eliminated from the final product.


问:
How do you implement an active sessions system like LinkedIn?
With some platforms, like LinkedIn, you can see a list of all sessions where you are logged in, and you can even log them out on a distance.

How would you implement something like that? I'm not talking about specific code, but more about general flow.

I am using ASP.NET myself, but I think a general approach would be more useful for others.

Right now I'm thinking about something like this:

User logs in, validate email/password combination

Generate GUID, save to database together with UserId and Browser information.

Put Cookie in a serverside-read-only cookie.

When user wants to sign out from a distance, we remove the GUID in the database.

One of the security issues here would be that the GUID can get intercepted, and an attacker could use this to login.

So how to do this properly?

答:
Every session is going to have some authorization artifact it uses to track sessions. (In web applications, usually this is a token in a cookie, although it doesn't have to be in a cookie.)

When your application grants those authorization artifacts, you can keep a link back to a user, and then manage them as a one-to-many relationship -- one user has many tokens. Then you provide all the usual operations like list all, filter/search, delete, etc.

Usually, when a service or framework grants a new authorization artifact, it just looses track of the old ones. So there's an implicit one-to-many (one user, many sessions), except that the other sessions are just lost and eventually cleaned up. But you can track them explicitly if you keep a link to the user account.


问:
Is it ethical for a programmer to promote his/her own library?
Fairly recently I started maintaining my own open source JavaScript library. I created it to solve a pretty specific need but fairly regularly see questions that can be solved (in whole/part) by using my library.

I post my answer and make sure to always include that I maintain the library.

I feel for open source projects this may not be such a big deal but where do you draw the line? (ex: commercial products) Is it ethical for a programmer to promote his/her own library? When is it not?

答:
It is definitely ethical. Sometimes it is better to receive support directly from author of the technology, especially, if the author offers better solution. </p>

<p>On the other hand the current form of disclosure of affiliation is like an excuse: "Sorry, I am an author of this technology..., but the right answer is...". I think it is better to have special icon for the answer "Answer from author". This is wont be an excuse, but Additional information for participants. </p>

Body:
It is definitely ethical. Sometimes it is better to receive support directly from author of the technology, especially, if the author offers better solution.

On the other hand the current form of disclosure of affiliation is like an excuse: "Sorry, I am an author of this technology..., but the right answer is...". I think it is better to have special icon for the answer "Answer from author". This is wont be an excuse, but Additional information for participants.


问:
Thoughts on refactoring a generic DAO
I have designed a generic DAO in Java and am having confusion about whether to refactor it to different design or not.

PS Don't say to use already existing 3rd person framework. I know there are some present but I am learning Java and design patterns so I need all the practice I can get.

If there's any confusion then please ask and I'll clarify.

The current Architecture

DAOFactory.java

Contains the abstract methods for creation of DAODelete, DAOUpdate and 2 other interfaces to be implemented by concrete implementation classes

public Class method for creating the concrete Factories for producing the concrete implementaton of the interfaces mentioned before.

DAORead.java

```<code>public abstract <T> T getPojoForPrimarKey(Connection con,
        TableName tableName, String primaryKey) throws SQLException;
</code>```

Similarly in my other interfaces I have generic methods.

Here Connection is passed by the service layer and closed there for transaction management.

TableName is a enum that I am using to have a switch case in  lower layer of DAO for calling Table Specific functions. Also using an enum is helping me in type checking.

OracleRead.java

```<code>@Override
    public <T> T getPojoForPrimarKey(Connection con, TableName tableName,
            String primaryKey) throws SQLException {
        T currentPojo = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            String queryString = OracleSpecifics.queryString(tableName,
                    primaryKey, QueryType.READ);
            ps = con.prepareStatement(queryString);
            rs = ps.executeQuery();
            if (rs.next()) {
                currentPojo = OracleSpecifics.getPojoFromResultSet(tableName, rs);
            }
        } finally {
            DAOFactory.closeAll(ps, rs);
        }
        return currentPojo;
    }
</code>```

This is how I have implemented the deleteFrom function from the interface in concrete class.

Here OracleSpecifics is the lower layer of DAO that I was talking about. It basically contains switch cases to check for specific Table.

OracleSpecifics.java

```<code>// These functions call table-specific functions
    @SuppressWarnings("unchecked")
    static <T> T getPojoFromResultSet(TableName tableName, ResultSet rs)
            throws SQLException {

        switch (tableName) {
        case ASEEM_MASTER:
        case ASEEM_MASTER_TEMP:
            return (T) SpecAseemMaster.getPojo(rs);
        case AUTHENTICATE_TABLE:
            return (T) SpecAseemRole.getPojo(rs);
        default:
            return null;
        }
    }
</code>```

Here SpecAseemMaster and SpecAseemRole are Table Specific (specifically schema specific) layer producing the required fields

SpecAseemRole.java

```<code>public static AuthenticationDetails getPojo(ResultSet rs)
            throws SQLException {
        AuthenticationDetails cur = new AuthenticationDetails();
        cur.setUserName(rs.getString(1));
        cur.setPassWord(rs.getString(2));
        cur.setRole(rs.getString(3));
        return cur;
    }
</code>```

In service layer

The type by which the DAO's method is called is the Pojo representing the schema's data fields. e.g.

```<code>myFactory.getDAORead().<AuthenticationDetails> getPojoForPrimarKey(con, CUR_TABLE, primaryKey);
</code>```

Here AuthenticationDetails is my Pojo aka DTO for communicating between my Controller and service layer (in Model).

Please Note that I am giving just a overview in the simplest way. There are 4 interfaces in the abstract DAOs containing total of 8 abstract methods implemented by 4 concrete classes. All these generic classes depend on a lower layer in DAO for getting specific queries, PreparedStatements etc. which further calls functions in auxiliary utility class's functions depending on the tableName passed down this hierarchy. The bottom layer is schema-specific.

My Confusion about refactoring

I am satisfied with the upper layers of DAO but I think that at the the lower level specifically at the OracleSpecific the things which should have strong coupling has weak coupling. e.g. there are different methods for getting PreparedStatement for insert and update. Each of these in turn have switch case for calling the functions in lower utility class.

Although the schema-specific methods are tied together in different utility classes the method call themselves in OracleSpecifics.java do not have any coupling.

I am thinking whether I should change the enum TableName to contain a specific state. The state I am thinking for the schema-specific lowest level utility classes.

These specific states contained in enums can be used to change the state of DAO and the DAO can then call the specific functions based on an interface implemented by all such states. Thus depending on the state the behavior can change automatically.

Is this design decision correct or would it be meaningless?

Any other thing that I might have overlooked?

Any thoughts about the design itself?

Would I lose type safety introduced by generics due to this change?

答:
The first question you need to answer is whether you intend to create a truly generic data access framework, or if you want it tailored to the particular project you're working on.

A generic framework has the benefit of being portable and fairly loosely coupled to the underlying DB + schema, but at the cost of pushing schema level information further up in the framework.  The more generic you make the framework, the more of this knowledge you push up.

A tailored framework alleviates the higher levels of the application from worrying about the schema or DB details, but at the cost of cost of being less re-usable in other environments.  As a corollary, the more specific you make the framework, the less easy it is to extend elsewhere.

If you're not sure on which of those to pick, the generic framework makes sense if you suspect there will be changes at the DB layer or the schema.  Likewise, if those are pretty well locked down then the tailored framework is the better approach.  Ultimately, you're optimizing the system to more easily handle where you think the change will occur.

As to whether or not you should refactor - the answer is No unless you're encountering a specific problem.

Worrying about the degree of coupling is nice, I suppose, but doesn't necessarily address a specific problem that you may be seeing.  Refactoring based upon presumed issues within coupling is actually more likely to create a problem for you than just leaving it alone.

Likewise with expressing the state.  If the current mechanism of an enum is sufficient and doesn't present an apparent problem then don't change what you've got.

Overall, it sounds like you've designed a reasonably robust data access method for the domain you're working worth.  Your question doesn't list any specific issues that are holding back your development, so I'd call it good and move to the next stage of development.

Title: Thoughts on refactoring a generic DAO


答:
At a general level, the method needs to go with the object that has responsibility for the function.

Based upon your domain, the Student has the responsibility to provide a GetSchedule() method, as the Student owns the courses that it is taking.

And while the Parent may have a requirement to be able to see the Students schedule, the responsibility to execute that method still lies with the Student and not the Parent.

So in the simple case, GetSchedule() belongs to the Student.

In the more complex case, you may have a requirement that says the Parent should be able to see the Student's schedule without involving the Student.  At that point, you'll need another means to access the system so that the Parent can call the equivalent of GetSchedule(Student) and pass their Student as a parameter to the new method.

So you might create a class called Registrar, and a Parent can invoke Registrar.GetSchedule(Parent.MyStudent) in order to allow the Parent to find out the Student's schedule without involving the Student.

Title: where should put showSchedule method in school system

Body:
In my school management system I have this partial  class diagram:

1- In one use case student want to see his schedule of classes in week. this is what i suppose to do:

get student object from session and call method getSchedule() on that and it will give me schedule(this method calls StudentCourse, Course for getting schedule).

Is this good place to put getSchedule() method or I should place it elsewhere?

..................................................

2- In another use class student's parent want to see his child class schedule, I plan to do something like this:

Because I have some use cases that parent want to see some other student information(course mark...), I create interface StudentParentInterface that have method getSchedule() and other methods and student implement StudentParentInterface, then parent has reference to StudentParentInterface not student obj directly.

parent call getSchedule() method on StudentParentInterface, is this correct?


问:
Clients connect to WCF or SQL Server
I was wondering what is the prefered way of accessing data from a server in a client application. The data will be stored on the server in an SQL database and I have previously used the following 2 methods for getting at the data:

1) Client's connect directly to the SQL database

2) Client's connect to a web service which runs the sql query and then returns the data.

The program in this instance will be written in C#, so the following technologies will be in place:

Entity Framework (connect to sql database)

WCF (web service)

From a coding point of view all of the data access logic will be written in repository classes whatever method is used, so the physical setup of connections should not have any effect on this. For example a simple repository might be:

```<code>public interface IUserRepository
{
    IList<User> GetUsers();
}

public SqlUserRepository : IUserRepositoy
{
   private MyDbContext _dbContext;
   public SqlUserRepository(MyDbContext dbContext)
   {
      _dbContext = dbContext;
   }

   public IList<User> GetUsers()
   {
       return _dbContext.Users.ToList();
   }
}

public WcfUserRepository : IUserRepository
{
   IUserRepositoy _userRepository;

   public WcfUserRepository(IUserRepository userRepository)
   {
   }

   [WebGet(UriTemplate = "/users")]
   public IList<User> GetUsers()
   {
      return _userRepository.GetUsers();
   }
}
</code>```

So either way the clients will only rely on IUserRepository.

The problem I am having is that using WCF seems more secure, and the extra level of abstraction makes it easy to change database providers. However when using this solution before I have always had problems with performance, and big problems if a large data set is returned.

For example if there are 10,000 users in the system using entity framework this is no problem, but depending on how WCF is configured this can cause a message too large error.

Any opinions on the best practise here?

How can WCF be configured to reduce the likelihood of giving problems for large return values?

Obviously WCF will always be slower than direct SQL as there is an extra step of negotiation the SOAP / REST message. But can this be optimized down to an acceptable level?

答:
By all means, the second option you said is the better choice:  2) Client's connect to a web service which runs the sql query and then returns the data.

There are a lot of reasons as to why the DB access should be shunted away from the client, but here's a list of some of the more important considerations:

Security

Using a web service allows you to filter and sanitize the incoming requests prior to executing the query.

Access credentials directly into the database or stored only within the web service and not at every single client trying to access the database.  A central location makes it much easier to provide routine security maintenance like updating passwords and locking out previous users of the system.

Administration

When you need to update the database (move to another server, schema, etc...), it's much more easily accomplished when there's a single connection point to worry about instead of a large number of distributed connection points.

Using a service provides a looser coupling between the clients and the database.  That makes it easier to scale the database, the number of clients, and to change out the types of clients as necessary.

If query loads get too heavy, it's easier to enable data caching within the web service than it would be within the database environment.

Title: Clients connect to WCF or SQL Server


问:
Design Pattern for Cron/Schedule in MongoDB
I am looking for a good pattern to handle cron-type data in a MongoDB collection (or collections).  I have been peeking at the O'Reilly book MongoDB Applied Design Patterns and poking around the MongoDB site's Use Cases site but neither seems to to cover this type of data.

Is there a good pattern for storing repeating schedules that would lend itself to being able to find documents matching the current schedule, or should I simply store the data same as crontab and do all the calculations in-app?

答:
I think you're going to be better off just storing the data as is; let MongoDB work its magic on top of that.  Mongo's internal structures ought to handle identifying the repeating patterns for you, based upon the key-value-pair structures you choose to represent the data.

Trying to create your own means of identifying repeating patterns duplicates the work that the non-relational DB can provide, and is also likely to start wrapping itself around the axle for edge cases.  You'll also have to provide that translation functionality for every job entry you wish to track within MongoDB.

To be more specific, I'm suggesting setting up a key value pair for each of the traditional fields used in specifying the cron job.  So a given job entry in your MongoDB schema would have values for the frequency, minute, hour, day, etc...

Your queries would then be able to check for "where frequency equals ..." and "where day equals ..." and so on.

If you look at this mongodb tutorial on querying timestamps, you'll find that it's pretty trivial to specify ranges in order to find recurring patterns within the data.  And while that tutorial is focused on finding timestamps within blog posts, it would be pretty easy to alter the key-value pairs they used to reflect the data you have from the cron jobs.

If you're stuck on the particular format of the data, then you may also want to look at this StackOverflow question as it is fairly similar to yours.  The accepted answer highlights the standard cron job specification syntax and provides an ACM link to the cron data structure. Other answers suggest other formats such as iCalendar.


问:
Implementing internal own authentication
Let me apologize first if this topic is too subjective, or not concrete enough.

I want to implement a login api for an internal web service at my company using asp web api 2. I know how to implement a owin bearer token system for a single user login scenario for a single website.

The issue here is that I want to provide a single login source for all of the different web applications we will be making in the future. A user will login to this source, it will save the token as a cookie(or something similar) and then continue to work from any number of different web apps afterwards.

So far it seems like all the external web tokens are oriented with Facebook or Google, so external login doesn't seem to be the way i want to go. I don't want to have to rewrite the authentication logic every time I make a new web application. How should I design my application so that I can login using a simple single web API for tokens, and easily integrate authentication into any new web application we make?

*Note: We have an internal Nuget repository that we can use to this effect.

答:
It sounds like you're looking for something like what OAuth already provides, or something falling under the broader category of Single Sign-on, Federated Identity and / or a Credential Service Provider.

So the good news is that there is likely something that already exists and will meet your needs.  The benefits here are that you're not rolling your own authentication system and therefore having to chase down implementation flaws that you inadvertently created.  Another benefit is that those approaches are more common or recognized and more likely to be acceptable to other users.

The bad news is that you'll need to update the system at least a little bit each time you allow for another provider.  Improvements have been made to make things more generic, but there is still a modest measure of coding required for each provider you wish to utilize.

If all of the authentication services you wish to use are internal, then you'll likely be able to setup the providers once and be done with if for a while.

As far as structuring your web applications, simply build a dedicated module to handle the authentication component at the web service layer.  Each page / service call pulls the claimed user credentials from the client and then passes those credentials over to the authentication component.  The authentication component can provide a simply boolean to allow the rest of the call to continue or to exit out.


问:
Silverlight-based "area of influence" algorithm
I am in the process of developing a tool for a game called MechWarrior: Online which defines a map of an area of space called the Inner Sphere. Essentially, this map is a set of points (planets) on a Cartesian plane. Each planet can be owned by a different faction, typically represented on the map by a colour. I can colour the various planet points as different colours, but ideally what I'd like to do is show an area of influence (circle) for each faction by using something like a GeometryGroup to help with the simplification of the geometry object that using 2,240 points' circles will produce before rendering it to a Silverlight Canvas.

However, I'm not even sure that this is a good idea or the right way to approach this problem from an algorithmic standpoint, specifically because where there are overlaps of influence at the faction "borders", how do I deal with the overlaps to effectively show a reduced area of influence for both factions in the resulting geometry?

Edit for clarity:

Thank you for the responses so far - colour interpolation was not what I meant, tho it is similar in principle.

The image above shows two faction planets (one red point, one blue point) and their areas of influence (one red circle, one blue circle). In the top image, it shows the areas of influence (territory) and their overlap. The bottom image shows how I would like to resolve the overlap.

This is the simplest example I can think of and it may be that expanding this out over multiple planets would work to get the result I am looking for, but it could become computationally expensive as if you have n planets in one faction and n planets in another, you could end up with a Big O notation value of n squared at the minimum for two factions due to the required number of comparisons for checking intersection and there are actually 10 factions each with a minimum of two neighbours, so it's more than n squared anyway.

I am not sure of what effect doing a 1 to n faction iteration while doing a search for planets in influence diameter range (i.e. intersection range) would have on that n squared value however, as it's impossible to predict deterministically how many planets will be in range to each other planet. I am assuming that it's unlikely to be very many.

答:
Translucent circles approach:

One approach would be to use translucent overlays to show area of influence. You should be able to map the faction's influence strength to the intensity of the color representing the faction.

From there, simply allow the overlapping colors to combine and the resulting color demonstrates that there is a reduced level of influence in the area shared by the two factions.

If the geometric shapes you're using won't allow you to blend the colors of overlapping shapes, then you'll need to detect the cases where there is overlap and run a calculation to determine what the blended color should be.

Colored edges approach:

Another approach would be to determine where the circles of influence intersect with other circles, and then create a polygon based upon the intersection points and the remainders of the circles.  This approach aligns with what you drew in your question - you would have circles with the edges colored, but the centers transparent.

Given that any particular circle could be trimmed multiple times, you would probably want to start with a circle to represent the influence, but treat it as a polygon when calculating successive rounds of overlap.  The point here is to carry the existing shape forward and not make assumptions about the initial shape.  That way you avoid recalculating influence conflict from a previously completed round.

In other words, Planet ABC may have to have the circle of influence trimmed three times due to competing, nearby Factions.  Planet ABC's influence starts as a circle mapped onto a polygon, becomes a true polygon after the first trim, and remains a polygon for the next two trimmings.

Responsiveness optimizations

Hidden within your question are concerns regarding the overall performance or responsiveness of generating this mapping.  There are some things you could consider to decrease the number of calculations necessary.

The first consideration would be to reduce the number of planets for consideration.  So you would remove any planet that's far enough away from any other planet so that the circle of influence wouldn't possibly be affected.

Another consideration would be to build a directed acyclic graph (DAG) for the planets.  Use the DAG to determine the neighbors to a given planet.  Similar to the first optimization, there's no point in checking for overlap when the planets are too far apart.

Said another way, you don't check Faction A's planets against all of Faction B's planets.  You only check A's planets against B's when they are neighbors.

A final way to optimize the responsiveness of the algorithm is to determine the markers that affects the circle of influence and only update those planets where the markers have changed.  This requires more overhead on your part because you have to track the state of the game.  But there's no point in calculating something that hasn't changed from the previous round of calculations.


问:
Developer account in OSX, admin or managed user with our without sudo access?
The tutorials I find to setup OS-X for web developer programming (installing x-code ruby js cocoapods sql c asm etc) leave out if it should be done from an admin account, or standard (managed) user.

Some methods I've seen used: Setup dev environment from admin account, then change admin to managed user. Or install dev environment from managed user, using sudo or otherwise editing settings to get things to install.

What's the consensus for using Terminal from a managed & parental controlled account? What's the correct procedural way to setup a fresh 10.10 install? Thanks.

答:
I don't work with Ruby, but I work with Django/Python and I think the same principles apply.

Don't install stuff system-wide. To give you an idea, these are different methods for installing Python packages, from worst to best:

sudo pip install django: install Django system-wide. Don't do this.

pip install --user django: install Django for current user only. Better, but not great.

virtualenv --distribute myproject; . myproject/bin/activate; pip install django: create a virtualenv and install Django in there. Best. Multiple web projects can exist, each using different versions of Django, different versions of dependencies, as needed

You get the idea. The equivalent must exist for Ruby on Rails too.

As much as possible, install everything in user space.

Some things won't really be practical this way,
for example a MySQL database or Apache web server.
It depends on how much modularity you need.
If you will never ever need isolated MySQL servers running,
then you can just install one and isolate uses by separating MySQL user accounts.

The most professional option is probably to work in sandboxes,
using software such as Vagrant or Docker.


问:
How do you decide member objects?
So OOP is about breaking down functionality, making each class responsible for one thing etc. But let's take the example where an object is using another object. First thing that comes to mind "composition, of course!".

I have 2 questions:

Question 1:
Suppose you have a Shop that is loading products from a file using a ProductsLoader, which in term uses a ProductsFileReader. You design the API so that the code is simple and readable and specialized. Therefore, you are able to do this:

```<code>    std::shared_ptr<Shop> shop = std::shared_ptr<Shop> (new Shop());
    shop->LoadProducts();
    // other code
</code>```

In LoadProducts you do this:

```<code>    m_productsLoader.LoadAllProducts();
    std::shared_ptr<Products> products = m_productsLoader.GetAllProducts();
    this->setProducts(products);
</code>```

In LoadAllProducts() you do this:

```<code>    prodFileReader.OpenProductsFile();
    prodFileReader.LoadDescriptionsFromFile();
    prodFileReader.CloseProductsFile();

    // create products from descriptions
    std::shared_ptr<Descriptions> descriptions = prodFileReader.GetProductsDescriptions();
    this->createProductsFromDescriptions(descriptions);
</code>```

And now here is the question: Do you keep ProductsLoader m_productsLoader as a Shop member? The same for ProductsFileReader: should it be a member of ProductsLoader? The alternative for this is creating these objects on the stack: when your functions are using one of them, you just declare, initialize etc them in the function and they get destructed when the function exits. Does this defy the purpose of OOP?

Question 2:
Let's say you decided to keep them as members. How do you know wheter you should keep the whole object as a member, or you should keep only a pointer to it (and, of course, in the constructor, instantiate the member and assign the pointer). Whether you access the member with the dot opperator . or arrow ->, it will perform the same functionality.

答:
The subject you're asking about is called modeling, and it's as much an art as a science.  But the first step of OO modeling is to forget about the mechanics of the language and think about the properties of the things you're modeling.

A class named ProductsLoader is suspect to begin with.  Names like that indicate that you're thinking procedurally instead of in an OO way.  Instead, the Product class should have functions to produce instances and collections of itself from files or streams or whatever.

So back to your question, but substituting Product for ProductsLoader as the example.  How to decide whether Product (or a collection of them) should be a member of Shop?  Well, do shops have products in them?  That's the kind of question that should guide your decisions in OO modeling, not how the particular language you're using allocates things.

Title: How do you decide member objects?


问:
How do you explain the source of an identifed problem without making it an "excuse"?
As practicing (as much as I can at least) "egoless programming", I'm focused on solving problems whoever wrote the code related (if that person don't make dangerous code) and I like to understand the problem fully to be sure to solve the source of this problem.

However, when someone point my code to show an error, I often try to remember what was my mindset while writing the defect code and I try to explain it to the guy pointing the error to make clear what was the error source (for example when some names are badly choosen, you might use them in a way they were not intended too and with apparent success...).

Now, I do this by reflex and most of my coworkers does understand this reflex. But I also worked in companies where coworkers were in a mentality of blaming the people that made the identified problems, with or without humour (for me it have the same not-sane effect anyway).

In this kind of environnement, explaining why you got to write wrong code makes others think that you're trying to build an excuse. I remarked that most non-technical people (or non domain-expert, not sure, it's only personal experience) also react exactly like that when you try to explain why you did something wrong.

So, when dealing with this kind of people/environnement, how do you manage to explain the real source of the error without looking like "making an excuse"? (and is it even worth trying to explain?)

答:
In many respects I don't think you can avoid this conflict.

Explaining the rationale as to how you arrived at whatever point you are at is important in understanding the "big picture" around the issue in many cases, but there will always be people that see this as an excuse.

Selecting who you have the conversation with is the closest I have ever gotten to managing this issue.  In general that boils down to don't discuss it with the business folks and only discuss it with people that are going to be involved with the technical solution.

Even that approach fails at times because today's wrong is often linked to a previous requirement and you get drug down that path anyway.

In general I try not to worry about it too much as if people are going to pick that as something to raise as an issue about you they are likely sufficiently motivated they will find another issue as soon as you can "correct" this one.


问:
Why C language is taught as the basis of Computer Programming Languages?
I often wondered why C Language is taught as the basis of programming languages everywhere. There are a lot of modern languages like Java, Python etc. which makes the syntax and programming easier. Why are we still holding the C Language up in the front as the basics of programming languages?</p>

答:
It's not taught as the basis of Programming Languages. It's taught as the basis of how the machine works.

A programming language displays some facade to the programmer. Some abstraction. In functional languages it is functions. In logic languages it is logic. In OOP languages it is objects. In C it is the machine.

C, while hiding away the implementation details of the hardware on which it runs (register names, memory management, etc), gives the user the impression it deals with a machine. A computer. This gives students better appreciation of how the computer works. Some universities don't see "how the computer works" a necessary knowledge (it isn't), so they don't teach C.


答:
I wouldn't say that c is taught as "the basis of programming languages" very much, to be honest. Most introductory programming courses are in Java, with a handful in more esoteric languages (I know of universities that teach their intro to programming courses in ocaml and scheme, for example). Even before the shift to java, C wasn't a popular choice for a first language; my course taught Pascal, for example, and I believe this was reasonably common.

What C is popular for is courses on low level programing, where hardware interaction is required, and there's a good reason for that: few other languages are capable of handling that kind of job well, and C is ubiquitous in the embedded development industry.

Title: Why C language is taught as the basis of Computer Programming Languages?

Body: I often wondered why C Language is taught as the basis of programming languages everywhere. There are a lot of modern languages like Java, Python etc. which makes the syntax and programming easier. Why are we still holding the C Language up in the front as the basics of programming languages?</p>


问:
Is it difficult to develop a programming language which is closely related to human language?
Will it be difficult in developing a programming language which is much more closer to our language ?

It was just to know the view of programmers across the globe towards the natural programming language.Thanks for giving me your views on this .

答:
Assuming I get the gist of your question  (something to the effect of, "why aren't programming languages more like natural languages"):

Yes, but that's a very complicated subject, well beyond the scope of a one-line question.

But I'll point you to:

AppleScript
(what happens when a programming language tries to be more "natural").

And,

Legalese (what happens when a natural language tries to be more "programmatic").


答: Not only would it be difficult to develop it, as doing so would basically require artificial intelligence, it would also be harder to use than current programming languages. A programming language today requires an investment of time to learn the syntax and available library functions, but in return for that investment you get a payoff in terms of a clear, unambiguous language that has a precisely defined and (usually) easy to understand meaning for well-formed programs. Human languages are imprecise and ambiguous, which makes them unsuitable for programing tasks.</p>

Title: Is it difficult to develop a programming language which is closely related to human language?

Body:
Will it be difficult in developing a programming language which is much more closer to our language ?

It was just to know the view of programmers across the globe towards the natural programming language.Thanks for giving me your views on this .


问:
Event Driven Objects/Framework on the Web
I am a desktop developer moving to web development. The guys who I am working with use procedural PHP, and coming from an event driven, objectified perspective (using WPF and C#.Net) I am completely lost in what seems like the Wild West of development.

Are there any language/framework combinations that allow some semblance of state between the client and server and 'persists' the objects? Is there a way to create a definition of an object that is synonymous between the client and the server?

Is there anything that incorporates testing and UI components as well?

I realize this is a somewhat broad and biased question, however it is one that I have not found any definitive answers for. Any advice and guidance would be thoroughly appreciated!

答:
There are a very wide variety of web frameworks in different languages, and in considering them it is always worth bearing in mind that in reality there is only a single type of event in server-side web development (the browser requested to load some data).

That said, there are some frameworks that attempt to emulate a system where the server responds to events occurring on client side objects; ASP.net is probably the best known of these.

A lot of developers are of the opinion that they're more trouble than they're worth, however. They abstract away too much of the detail of how the web works, and take away too much control.

Most modern, large web sites are developed using the MVC pattern. As a C# developer you may want to look into ASP.net MVC; other popular implementations are ruby on rails and Spring MVC for Java.


答:
What do you mean by "closely related"? What problem is this langugae going to solve?

Do you believe that by making a programming language look like a human language, i.e. using many keywords and little interpunction in its syntax, programming will be easier? That is not hard to do at all, but futile. It has been tried many times, most prominently with SQL and COBOL. Such languages are often disliked by programmers for their verbosity, and they do not help people to learn programming more easily.

What makes programming difficult is not dealing with syntax consisting of weird symbols, but the task of translating the requirements into unambiguous, detailed instructions, and dealing what all the special cases that nobody thought of when formulating the requirements.

Or do you envision the ability to use free-form natural language to instruct a computer? That, too, has been tried for a long time, and it's one of the hardest unsolved problems in artificial intelligence, because it would not be a programming language at all, lacking formal syntax and semantics. Basically, you need a compiler or interpreter that is as intelligent as a human. In fact, this is pretty much the job human programmers do.