答:
tl;dr - I second Basile's solution, and don't believe you've thought through the implications of your preferred 303 redirect.

1.) The server redirects the user to /projects/2 (their previously visited page, which would of course always be a valid one) with a helpful notification at the top such as "Sorry, but you do not have permission to access XXXX"

What happens when someone follows a link from an email or IM message? Then there is no previously-visited page, valid or otherwise.

What happens when someone writes a wget or crawler script to report the status of a list of projects? Some projects will silently and mysteriously report the status of the last permitted project instead.

What happens when someone doesn't see the "helpful" notification at the top? They'll be looking where they expect to see the project's content.

I just don't see how an informative 403 page - which can explain what happened, be as pretty as you like, include a list of projects the user can view and/or a way to request access to the denied project and still be actually correct - I just don't see how this is less friendly.


问:
Where to master HTML, CSS and Javascript?

Possible Duplicate:

  What should a developer know before building a public web site?

I gotten interested in web-development lately. I am still a student. I learnt basics of HTML, CSS and JavaScript. Then I thought I should improve my server side scripting. So I am learning Struts2 and I am doing better there. Now I have decided I should finally put my skill to some use.

So my friend and I have decided to come up with a fun website for our class. But now I am realizing that, though I know server-side scripting to a good extent (not great, just good considering I am a beginner), I am nowhere near good in the basic elements viz. HTML, CSS and JavaScript. I mean, I can't do cool stuff with it.

I am aware of w3schools, but it would be great if you can point out a more intuitive place where I can learn to do all the cool stuff in a short time.

Some of the problems I am facing are:

1) How should I design the basic layout of my website?

2) How can I use 3rd party APIs like that of Facebook graph?

答:
Depends what you mean by "mastering", learning all the nitty gritty details is one thing and getting up a site is another.

If you're into the nitty gritty implementation...

You can get the basics down using the other posts but eventually you'll have to deal with cross-browser compatibility. I reference quirksmode on a very frequent basis. Which is useful when you have to deal with issues that make you wonder why something doesn't look right in certain browsers.

There are a wealth of sites that you'll have to reference to figure out why/what something renders the way it does. It's usually a little more involved, but is where you tend to lean towards when developing a web application (especially where security is a concern).

Personally, this is where I spend most of my time and use tools like Google Web Toolkit.

If you're into it just for the design...

You might want to check out from frameworks, this will get you over the technical humps and get you to design. Here are a few that come to mind that I like to use/used for simple sites:
CSS frameworks:

Blueprint

Grid 960

JavaScript frameworks:

jQuery

Prototype (Used to use)

Mootools (Used to use)

Scriptaculous (Used to use)

You can set up your own mini web development server by using a fully fledged development environment using an all-in-one kit such as BitNami stacks or LAMP/WAMP down to simple web servers like TinyWeb.

ContentLicense: CC BY-SA 2.5

ContentLicense: CC BY-SA 3.0


问:
How to handle time based events in web application backend?
I'm currently working on design of small web service, and I have a following feature (simplified):

After some user actions, there is a wait period (ranging from 30sec to 40h) and after it's passed, the server should update database entry for the task, unless user cancelled it during the waiting period.
Executed task is quite simple, just a few calculations, but it must be delayed.

And I have no idea how to do this...

I know that similar situation happens in some browser based games, where you upgrade a building and get bonuses from it after it's finished, but I was unable to find how this feature is achieved. So, my question is, is there a "state-of-art" method of dealing with such problems in backend, or any method at all?

答:
A simple approach is to have a pending tasks table in your database.

To create a task, add a row to the table that contains the action time, and any other data needed to process the task. You have a background thread that periodically queries the table, actions any tasks that have come due, and removes completed tasks from the table.

I wouldn't describe this as "state of the art" but it is a relatively simple and reliable way to do what you need.

When a task completes, do you need to notify users? That makes things somewhat more complicated as you'd need all clients to maintain a WebSocket connection to your server, so you can push notifications.


问:
How to represent an algorithm as a class?
I am trying to understand how to design classes which take an input, do some processing, and return a result. More specifically, should the object store the intermediate results between function calls as state, or should all methods be static, and pass params around.

Example: I am currently working on a class which detects, and removes headers & footer from pages in a document. The algorithm steps are outlined below:

HeaderFooterFilter

```<code>removeHeaderFooterText(pages) ->
  candidates = getBoundaryBlocks(pages);
  toRemove = detectRepeatingBlocks(candidates);
  removeBlocks(toRemove);
</code>```

So my question is, should pages, candidates, toRemove be members of the class, or should the methods be static and pass them around?

Is there a design-pattern which deals with algorithm classes?

答:
There is nothing wrong with writing pure functions as statics in object oriented programming.  Given your example, it does not appear necessary to make instance fields out of the intermediate results.

However, you should be prepared to refactor when and if necessary, into a real object.  Several reasons you may need to do that are:

You choose to define the algorithm via an interface (or abstract base
class) so you can have
different implementations.  Then you need multiple classes for the
varied implementations, and you will need an object to hold the
state, which in this case the state is the choice among the algorithms.

You realize that you need more than one operation (function) on the
object and it must persist state in between these invocations.


答:
You can do this a few different ways. What you ultimately do depends on your server-side stack, your permissions on the server, and the nature of the side-effects needed for the delayed event.

As I see them, your options are:

An "embedded" scheduled task. If you're using a service-like backend technology (like a node server), you can put timeouts right in the application. And in some cases, as with node, you could even schedule a timeout for each individual event (if it turns out being more efficient than processing an event queue in your case).

A separate cron job, windows task, or backgrounded process, daemon, or service. A cron job should be able to execute up to once per minute. A windows task can execute up to every 5 minutes. A background job, daemon, or service can use sleeps/waits/timeouts to do things on even shorter intervals.

Client-driven event processing. If no side effects are required when the data isn't actively being used (e.g., all the users who care about the value are signed out), you can avoid the overhead and complexity of regularly "upkeep" jobs by just computing certain values and/or processing events as-needed during client requests. E.g., for each request from and about UserX, get entries from "the queue" where event_fire_time <= now and event_user = UserX, and "complete" those events.

In all scenarios, be sure to lock (with transactions if using a DBMS) the records your event code is processing.

Title: How to handle time based events in web application backend?

Body:
I'm currently working on design of small web service, and I have a following feature (simplified):

After some user actions, there is a wait period (ranging from 30sec to 40h) and after it's passed, the server should update database entry for the task, unless user cancelled it during the waiting period.
Executed task is quite simple, just a few calculations, but it must be delayed.

And I have no idea how to do this...

I know that similar situation happens in some browser based games, where you upgrade a building and get bonuses from it after it's finished, but I was unable to find how this feature is achieved. So, my question is, is there a "state-of-art" method of dealing with such problems in backend, or any method at all?


答:
Before discussion, notice that

When given a business use case, object-oriented programmers can usually arrive at some consensus about how the surface design (the partitioning of responsibilities between classes, and the publicly accessible methods and properties of these classes) should look like.

On the other hand, when given an algorithm,

Object-oriented programmers can still arrive at some consensus about the surface design, based on the inputs, steps (process), and outputs.

There is much less consensus about how the algorithm's internals shall be partitioned, because those belongs to implementation detail which the algorithm's user would not be concerned about.

Object-oriented design does not prescribe ways to design algorithms. The algorithm's basic framework should be decided first, and then an object-oriented design (encapsulation) can be produced.

If the algorithm's basic framework needs to be changed, for example, to exploit a faster algorithm that has a requirement for a different framework, most of that object-oriented design would have to be re-done.

Object-oriented algorithm design is an entirely different topic, with an emphasis on fundamental (functional) building blocks that are reusable across many different types of algorithms. A consequence is that there is higher code reusability. The drawback is that it would have required the designers to adopt a different approach to algorithms, which is not typically taught in schools.

As far as object-oriented design is concerned, the breakdown of an algorithm into steps depends on whether the algorithm itself is better seen, from its user's point of view, as being executed in one single step, or as multiple steps.

Keep in mind that the algorithm's user doesn't need to know how the steps are decomposed, unless the user has a need to intervene between the steps, such as:

Interrupt the algorithm in between two steps,

Inspect the algorithm's data in between two steps,

Alter the algorithm's settings in between two steps,

etc.

If the algorithm is better seen as a single step, the object-oriented implementation can choose to hide all of the implementation details, leaving only the "inputs, process, outputs" visible to the user. Furthermore, one can provide a factory method or a utility function that takes the input and computes the output. This factory or utility method can make use of the object-oriented implementation.

In general it is better to store the algorithm states (mutable data) in the object itself, and make them private. There are exceptions:

Not if doing so increases confusion. For example, if pages means one thing in some parts of the algorithm, and then pages is rewritten with something different in other parts of the algorithm. If this is the case,

They should be given different and descriptive names to avoid confusion.

If the pages has its own state transitions (like a state machine), that logic should be extracted into its own class.

Class design for algorithms that are parallelized

If it is being used by its users concurrently (being called from multiple threads), the algorithm needs to either freeze (its data becomes immutable after the main computation has finished), or it needs lock-based protection.

If the algorithm itself is parallelized,

Mutable states should be partitioned, and each worker should have exclusive access to its piece of mutable state.

The algorithm's execution should be partitioned into phases. Once a phase has completed, the phase's output data becomes frozen.

Title: How to represent an algorithm as a class?

Body:
I am trying to understand how to design classes which take an input, do some processing, and return a result. More specifically, should the object store the intermediate results between function calls as state, or should all methods be static, and pass params around.

Example: I am currently working on a class which detects, and removes headers & footer from pages in a document. The algorithm steps are outlined below:

HeaderFooterFilter

```<code>removeHeaderFooterText(pages) ->
  candidates = getBoundaryBlocks(pages);
  toRemove = detectRepeatingBlocks(candidates);
  removeBlocks(toRemove);
</code>```

So my question is, should pages, candidates, toRemove be members of the class, or should the methods be static and pass them around?

Is there a design-pattern which deals with algorithm classes?


问:
Should I use nhibernate for a one row settings table
I am using nhibernate in a quite big project.

The system has a settings table which has 10 columns and one row, there is always a row and it should not be deleted only modified.
Concurrency can be a problem as many clients can increment columns in the table like LastReceiptId.

I'm thinking, it can't be a good idea to use nhibernate to solve this.

With raw sql I can use a transaction and lock the table with for example: With (TABLOCKX, HOLDLOCK) while I select and update the LastReceiptId.

Do you have any reasons to use or not use nhibernate in a situation like this?

答:
Regardless of what you use, you need to make sure you lock the single record in your settings table for the shortest time possible. It is the bottle-neck in all of your receipt creation. Because of this, I would do this in 3 steps with the first 2 wrapped in a transaction (I believe Nhibernate can do that.).

Create Receipt Record

Start Transaction: Update Settings table with new Receipt ID (Realizing you need to get the current ID and perform calc to increment.)

Insert new Receipt ID into receipt table:  End Transaction.

Save Receipt Record

Update receipt table based on the Receipt ID for the current user entry with all other relevant data provided by the user. No reason to wait for the user to save the entire record.

I don't believe your database will allow another transaction to read the dirty record in the Settings table during the transaction that wraps steps #1 & #2 unless you do some sort of "no lock" over-ride.

If the user changes her mind after the unique receipt id is created, you'll just have to do some sort of delete (maybe soft) for that record and just eat the ID. I realize in some VAT systems these ID's are important and must be incremental which is why I recommend the soft delete.


答:
You have to store your tasks, for instance in a db table, with at least the following informations:

time_to_execute: timestamp of when task was set + delay in seconds

to_do: things to do (this must be understandable by your backend)

executed: 1 if the task has been executed already, otherwise 0 (default)

Then, you need a scheduler, could be a cron, which will periodically, say every minute, query an endpoint in your backend which in turn will query the db table containing your scheduled tasks. The pseudo-code for this should be like:

```<code>my_backend_endpoint_callback():
    for task in tasks:
        if not task.executed and time_to_execute > time.now():
            result = execute_my_task(task.to_do)
            if result == 0:
                # task was properly executed
                task.executed = 1
</code>```

You can then have another loop which will delete all the executed tasks, depending on whether you want to keep their trace or not.

Body:
You have to store your tasks, for instance in a db table, with at least the following informations:

time_to_execute: timestamp of when task was set + delay in seconds

to_do: things to do (this must be understandable by your backend)

executed: 1 if the task has been executed already, otherwise 0 (default)

Then, you need a scheduler, could be a cron, which will periodically, say every minute, query an endpoint in your backend which in turn will query the db table containing your scheduled tasks. The pseudo-code for this should be like:

my_backend_endpoint_callback():
    for task in tasks:
        if not task.executed and time_to_execute > time.now():
            result = execute_my_task(task.to_do)
            if result == 0:
                # task was properly executed
                task.executed = 1

You can then have another loop which will delete all the executed tasks, depending on whether you want to keep their trace or not.


问:
UI Design patterns for non-screen based project
I'm working on a small project coded in Python that uses a single button for input and a RGB LED as feedback, which responds to button presses and asynchronous events from the network.

As the feedback has become more complex (lot's of ifs, elses and timers), it's become evident that a UI design pattern, such as MVC or MVP would help segregate functionality.

The code is already mostly MVC in style, where the button input is tied to a controller thread and the LED has a View thread where animations are loaded under control of the controller. A model exists for interacting with a remote device.

For example, the button initiates an HTTP request by calling on the controller, which initiates an async request to the model and returns a View that blinks the LED to let the user know the request is inflight.

Where I'm struggling is how to deal with the response from the Model when the HTTP request is completed. The View could poll the Model but that seems wasteful and perhaps slow. Which makes me wonder if the Model should update the View either by raising an event or using a controller supplied callback, but these would technically tie the Model to the View.

I'm also wondering if MVP would be a better approach to this and use the Presenter to arbitrate between View and Model.

Your thoughts on approach for this totally over engineered gadget would be appreciated.

答:
When the HTTP request is completed, it will return some data, and the model gets updated by the responsible part of your system. Then raise an event which tells everyone who subscribed to it "new data has arrived", but do not pass the actual data in this event, only the relevant information for the subscribers which part of the model has changed. The event itself can carry something like an ID or primary key together with a type information, but not more.

The view, or some kind of controller beeing responsible for updating the view, has subscribed to that event. It pulls the data to be displayed out of the Model. That way, the Model does not need to know anything about the View, only the View or the responsible Controller has to know about the Model (but not more as it had already to know before). What is more important, you do not tie the View to the part of the system that deals with the HTTP requests.

MVP is good when you have the requirement to change actual view by a different one, but want to keep the core presentation logic (the part in the Presenter) the same. It is also helpful when you need to mock out the view for testing purposes. If this is useful in your case, or not, will depend on the complexity of your system as a whole, but most probably not on your requirement for processing asynchronous HTTP requests.


问:
Is it okay to return the "wrong" HTTP status code in order to show a more user-friendly error page?
Located here, RFC 7231 is all about the HTTP protocol (status codes)

I'm developing an in-house web application for my university's department and I'm at a (probably unimportant) impasse.

I'm reducing users' access to resources based on role (student, faculty, staff) and have it working just fine. However, when a user attempts to access a resource and is denied access, the HTTP specification says that I should use a 403: Forbidden, marking that the user does not have access to the resource, or "hide" the resource by returning a 404: Not Found.

I could do that, but I want to make the site a bit more user-friendly and instead redirect them to their previous page with a flash (NOTE that I did not say 'Adobe Flash'; see comments) message instead of having a 403/4 page with links back to different parts of the site. I think it's a pretty UX-friendly feature.

However, that would involve redirecting via a 303: See Other, which more or less violates the standard.

Obviously it really doesn't matter as this is an in-house project for a lab at my university and nobody here is going to be complaining, but imagine this site were under the Google umbrella.

So, is it bad practice to deviate from the [HTTP] standard like this?

Edit: There seems to be a lot of confusion, so let me give an example:

User with id = 1 attempts to access mydomain.com/projects/2 through their profile (by clicking a link). User 1 is the owner of project 2, so the server accepts the requests and renders the page with the project.

User 1 then types mydomain.com/projects/3 into the URL. User 1 is not the owner of project 3. Therefore, when the request gets to the server, the server denies it.

Now, there are two options:

1.) The server redirects the user to /projects/2 (their previously visited page, which would of course always be a valid one) with a helpful notification at the top such as "Sorry, but you do not have permission to access XXXX" or

2.) The server renders a 403: Forbidden page, or a 404: Not Found page, which would probably have that same text (if it was a 403), and maybe even a JavaScript-enabled "back" link

Personally, the redirect seems way more user friendly to me, and eliminates seeing an unfriendly page like 403: Forbidden and having to click a "back" button. However, doing the redirect goes against the HTTP protocol standard.

答:
I'm going to give a second answer, because looking back at what you wrote I think you might be asking a more basic question than I thought, one that deserves a very simple answer:

Is it okay to return the “wrong” HTTP status code in order to show a more user-friendly error page?

Yes.

The HTTP specification does not say that web applications (such as yours or Stack Exchange) are supposed to return a 403 status whenever the user steps out of bounds. Rather, it says that 403 exists and has a meaning, and that HTTP clients are supposed to react in certain ways when they see it. (It also implies that forbidding access is a common enough thing that it deserves its own 400 series status code.)

So unless you're writing an HTTP client or server (which should strictly follow the spec) or doing something like a REST API (the REST client being a special case of HTTP client) you have total freedom to never return a 403 status at all. You are not violating the spec by not returning 403.

My other answer gives examples of when returning 403 makes sense and when it might not.


问:
Clustering Strings on the basis of Common Substrings
I have around 10000+ strings and have to identify and group all the strings which looks similar(I base the similarity on the number of common words between any two give strings). The more number of common words, more similar the strings would be. For instance:

How to make another layer from an existing layer

Unable to edit data on the network drive

Existing layers in the desktop

Assistance with network drive

In this case, the strings 1 and 3 are similar with common words Existing, Layer and 2 and 4 are similar with common words Network Drive(eliminating stop word)

The steps I'm following are:

Iterate through the data set

Do a row by row comparison

Find the common words between the strings

Form a cluster where number of common words is greater than or equal to 2(eliminating stop words)

If number of common words<2, put the string in a new cluster.

Assign the rows either to the existing clusters or form a new one depending upon the common words

Continue until all the strings are processed

I am implementing the project in C#, and have got till step 3. However, I'm not sure how to proceed with the clustering. I have researched a lot about string clustering but could not find any solution that fits my problem. Your inputs would be highly appreciated.

答:
Similar to this question/answer and I've used the c# algorithim posted there in the past and it works like a charm here's the algorithim from the linked answer:</p>

```<code>public class SimilarityTool
{
    public double CompareStrings(string str1, string str2)
    {
        List<string> pairs1 = WordLetterPairs(str1.ToUpper());
        List<string> pairs2 = WordLetterPairs(str2.ToUpper());

        int intersection = 0;
        int union = pairs1.Count + pairs2.Count;

        for (int i = 0; i < pairs1.Count; i++)
        {
            for (int j = 0; j < pairs2.Count; j++)
            {
                if (pairs1[i] == pairs2[j])
                {
                    intersection++;
                    pairs2.RemoveAt(j);//Must remove the match to prevent "GGGG" from appearing to match "GG" with 100% success

                    break;
                }
            }
        }

        return (2.0 * intersection) / union;
    }
</code>```

Body:
Similar to this question/answer and I've used the c# algorithim posted there in the past and it works like a charm here's the algorithim from the linked answer:

public class SimilarityTool
{
    public double CompareStrings(string str1, string str2)
    {
        List<string> pairs1 = WordLetterPairs(str1.ToUpper());
        List<string> pairs2 = WordLetterPairs(str2.ToUpper());

        int intersection = 0;
        int union = pairs1.Count + pairs2.Count;

        for (int i = 0; i < pairs1.Count; i++)
        {
            for (int j = 0; j < pairs2.Count; j++)
            {
                if (pairs1[i] == pairs2[j])
                {
                    intersection++;
                    pairs2.RemoveAt(j);//Must remove the match to prevent "GGGG" from appearing to match "GG" with 100% success

                    break;
                }
            }
        }

        return (2.0 * intersection) / union;
    }


答:
Well,first things first,
There's no single source of mastery and no tutorial is good enough for mastering any technology. It's only through working on live projects and continous series of error, recovery and result analysis in different situations that you gain sucessive experience and expertise.

now what remains is learning the language syntax and for that w3schools is a very good site for html and css. it explains almost all the tags in a very presentable manner. For javascript and related techs like ajax and jquery google is the best book you'll ever have. becoz no single book will ever answer all your javascript queries in a single volume.
gook luck.

Title: Where to master HTML, CSS and Javascript?

Body:
Possible Duplicate:

  What should a developer know before building a public web site?

I gotten interested in web-development lately. I am still a student. I learnt basics of HTML, CSS and JavaScript. Then I thought I should improve my server side scripting. So I am learning Struts2 and I am doing better there. Now I have decided I should finally put my skill to some use.

So my friend and I have decided to come up with a fun website for our class. But now I am realizing that, though I know server-side scripting to a good extent (not great, just good considering I am a beginner), I am nowhere near good in the basic elements viz. HTML, CSS and JavaScript. I mean, I can't do cool stuff with it.

I am aware of w3schools, but it would be great if you can point out a more intuitive place where I can learn to do all the cool stuff in a short time.

Some of the problems I am facing are:

1) How should I design the basic layout of my website?

2) How can I use 3rd party APIs like that of Facebook graph?


答:
If removeHeaderFooterText is an atomic operation, then the solution is simple. Your class for implementing the algorithm will have one public entry point. In pseudo code, it would look something like this:

```<code>class HeaderFooterFilter
{
    public removeHeaderFooterText(pages);
};
</code>```

No caller should ever be concerned with how removeHeaderFooterText works. All they need to know is what it does. Internally, you can do whatever you want. You can completely replace the internals someday if you want. As long as your entry point remains the same, callers don't have to change anything.

As an implementation detail, this can be done as a static. I tend to avoid static in these situations, because it is a little simpler handling concurrency.

Body:
If removeHeaderFooterText is an atomic operation, then the solution is simple. Your class for implementing the algorithm will have one public entry point. In pseudo code, it would look something like this:

class HeaderFooterFilter
{
    public removeHeaderFooterText(pages);
};

No caller should ever be concerned with how removeHeaderFooterText works. All they need to know is what it does. Internally, you can do whatever you want. You can completely replace the internals someday if you want. As long as your entry point remains the same, callers don't have to change anything.

As an implementation detail, this can be done as a static. I tend to avoid static in these situations, because it is a little simpler handling concurrency.


问:
What is an efficient data structure for syntax highlighting in text editors?
I'm creating a very small text editor in C++ with the ncurses library. So far, it works great. I have implemented the Gap Buffer data structure to make the editing more efficient than a line-based buffer. I have considered the Ropes data structure, but it seemed very complex for this.

However, I'm now trying to implement syntax highlighting and I can't think of a decent way of doing this. Should I highlight the entire file, or just the current view (not highlighting the non-viewable parts of the file)?

Should I give in and use hopes, storing the micro-buffers with style data (bold, color, etc.)?

答:
I don't think you should have syntax highlighting information directly in your text buffer. Instead, I would add additional data structures for the display code.

Here's why:

Once you're providing functionality like selections etc, you'll probably need an anchor concept (a steady pointer to a specific location in the buffer, even when characters are inserted or deleted before that location - see below for implementation idea). If you're dealing with longer texts, you might also need these anchors to provide a fast index into your buffer for line beginnings (since you're using a gap buffer, you need some way to translate line numbers into buffer positions).

What I'm getting at is that you need several supporting data structures besides the actual text buffer in order to provide fast standard editor commands and display the currently visible buffer. Despite your decision to use a gap buffer, editors are line-based and you'll need to support that somehow. So why not write a line-based syntax highlighter which will take the text buffer, an anchor into it (which should ideally be the beginning of a line) and a highlighting state and output a list of ("text fragment", "style information") pairs up to the end of the line, which your display code will use to actually display this line? If it's not fast enough to do on the fly, you can create a cache of these lists indexed by the line number. You could probaby do it for the whole file at once, too, but I suspect that performance will suffer if you do that every time you move around in your file.

You'll need the highlighter state as an input because of multi-line tokens such as long comments or strings. Since usually an editor will display a file from the beginning when it is first opened, you can start with an "all clear" state, call the highlighter code to spit out the first line's highlighting information, and keep the highlighter state at the end of the line (eg, "currently in a multi-line comment" or "all-clear") as the state of the highlighter at the beginning of the next line.

So, basically, I'd suggest implementing anchors and then keeping around an index to quickly translate line numbers into text buffer anchors. Then I'd augment this index to not only provide anchors into the text buffer but also a cache of syntax highlighter output for that line and highlighter state at the the beginning of this line. So if the user is editing a line, you don't have to restart the highlighter at the very top of the buffer; you simply restart highlighting at the beginning of the line; and if you're clever, you can also avoid having to highlight the whole rest of the buffer (I'm thinking you stop highlighting as soon as you reach either the bottom of the visible area or a line with the same state your highlighter is actually in, but flag the following highlighter states as "possibly outdated" if the highlighter state doesn't match the cached one for the next line.

I think you can get away with only highlighting very small sections of your buffer this way, usually just a single line, except for editor commands like "jump to the end of the file", in which case you'll need to syntax-highlight the whole buffer in order to determine the state the highlighter should be in at then end of the file.

Edit: How to implement anchors

To implement anchors, you can use events and listeners. Whenever the buffer gets a command to insert or delete text, it notifies all it's listeners that these events are about to happen. Anchors then subscribe to these events as listeners, and update themselves based on the kind of event. For example, if three characters are inserted before the anchor position, you need to add 3 to the anchor position; if characters are inserted after the anchor position, no action is needed, etc. So, basically, an anchor is an object that keeps track of a buffer offset and subscribes to change events from the gap buffer.

ContentLicense: CC BY-SA 4.0


问:
Checking array size in C/C++ to avoid segmentation faults
So it's well known that C does not have any array bounds checking when accessing memory. Nowadays, if you call myArray[7] when you initialised it as int myArray[3], your program will get a segfault and crash thanks to protected memory.

Now, if you have an argument in a function such as myFunc(int *yourArray), but you know you need at least 8 slots in the array, is it possible to check if myArray[7] is illegal beforehand in order to throw a custom error:

```<code>"Sorry, yourArray is too small for this function. We need 8 ints of space."
</code>```

rather than

```<code>"Segmentation fault."
</code>```

答:
Checking array bounds like you want is implementation specific, because buffer overflow is an example of undefined behavior (and this explains why UB can be really bad).

It is also an undecidable problem in general. You can easily show that statically finding (by static program analysis, e.g. of the C++ source code, without actually running the program) every buffer overflow is equivalent to the halting problem. Read also about Rice's theorem.

However, several (partial) practical tools exist (notably on Linux):

you could add assert or static_assert-s in your code, and/or runtime checks.

you might find and use a static code analyzer à la Frama-C (it works for C code currently).

you could customize your GCC compiler using MELT.

You should compile your code with all warnings & debug info, e.g. g++ -Wall -Wextra -g if using GCC.

You might run your program with valgrind, at least for tests.

you could use the address sanitizer, e.g. add -fsanitize=address to your compilation flags (when testing)

notably in C (and sometimes in C++) it is a good convention to pass both array pointers and their size (like e.g. snprintf(3) or strncmp(3) do). In C, you might also use flexible array members in struct and store the flexible array's size inside the struct

BTW C and C++ pointer arithmetic abilities make finding buffer overflow even harder.

In C++11 you'll better avoid plain arrays and raw pointers and use standard containers and smart pointers.


问:
Why is String immutable in Java?
I couldn't understand the reason of it. I always use String class like other developers, but when I modify the value of it, new instance of String created.

What might be the reason of immutability for String class in Java?

I know there are some alternatives like StringBuffer or StringBuilder. It's just curiosity.

答:
I am really surprised nobody pointed this out.

Answer: It would not benefit you significantly, even if it was mutable. It would not benefit you as much as that causes additional trouble. Let's examine two most common cases of mutation:

Changing one character of a string

Since each character in a Java string takes either 2 or 4 bytes, ask yourself, would you gain anything if you could mutate the existing copy?

In the scenario you are replacing a 2 byte character with 4 byte one (or vice-versa), you have to shift the remaining part of the string by 2 bytes to the left or to the right. Which is not that different from copying the whole string altogether from the computational point of view.

This is also a really irregular behavior which is generally unwanted. Imagine someone testing an application with English text, and when the application gets adopted to foreign countries, such as China, the whole thing starts performing strangely.

Appending another string (or character) to the existing one

If you have two arbitrary strings, those sit at two distinct memory locations. If you want to change the first one by appending the second, you cannot just request additional memory at the end of the first string, since it probably already is occupied.

You have to copy the concatenated string to a whole new location, which is exactly the same as if both strings were immutable.

If you want to do appends efficiently, you might want to use StringBuilder, which reserves a pretty hefty amount of space at the end of a string, just for this purpose of a possible future append.

Title: Why is String immutable in Java?


答:
There is no way in C(++) to get the length of an array from a pointer to its first element.  (There are platform-specific functions like _msize in MSVCRT, but that only works on malloced pointers.)

What's typically done when passing arrays to functions is to pass the length along with the pointer so that bounds-checking can be done at runtime.

```<code>void myFunc(int* yourArray, int length)
{
    if (length < 8)
    {
        puts("Sorry, yourArray is too small for this function. We need 8 ints of space.");
        return;
    }

    // ...
}

void caller()
{
    int arr[LEN];
    myFunc(arr, LEN);
}
</code>```

Title: Checking array size in C/C++ to avoid segmentation faults

Body:
So it's well known that C does not have any array bounds checking when accessing memory. Nowadays, if you call myArray[7] when you initialised it as int myArray[3], your program will get a segfault and crash thanks to protected memory.

Now, if you have an argument in a function such as myFunc(int *yourArray), but you know you need at least 8 slots in the array, is it possible to check if myArray[7] is illegal beforehand in order to throw a custom error:

```<code>"Sorry, yourArray is too small for this function. We need 8 ints of space."
</code>```

rather than

```<code>"Segmentation fault."
</code>```

Body:
There is no way in C(++) to get the length of an array from a pointer to its first element.  (There are platform-specific functions like _msize in MSVCRT, but that only works on malloced pointers.)

What's typically done when passing arrays to functions is to pass the length along with the pointer so that bounds-checking can be done at runtime.

void myFunc(int* yourArray, int length)
{
    if (length < 8)
    {
        puts("Sorry, yourArray is too small for this function. We need 8 ints of space.");
        return;
    }

    // ...
}

void caller()
{
    int arr[LEN];
    myFunc(arr, LEN);
}


答: Use a custom wrapper for malloc (or write your own) that keeps additional information about the blocks it allocates.  The one I use adds a few "guard bytes" to every allocation, embeds the length of the allocation as the a[-1], and checks the guard bytes and other things upon deallocation.</p>


问:
Is it ok if a method returns a new instance of the class it's in?
I have a class called MyClass and a method called Get that looks something like this:

public class MyClass{
    string ClassVariable1 {get; set;}
    string ClassVariable2 {get; set;}
    string ClassVariable3 {get; set;}

    public MyClass Get()
    {
           ..set variable1, variable2, and variable3..

                return new MyClass()
                {
                    ClassVariable1 = variable1,
                    ClassVariable2 = variable2,
                    ClassVariable3 = variable3
                };
            }
        }
        return null;
    }
}
```

<p>Is there anything wrong with doing this from a design perspective? Or any other perspective?</p>

<p>I guess to make this more clear, I'll post my actual code. I'm writing a facebook app and my class is called "FacebookAccount". The idea is that this will hold stuff related to a facebook account, like screen name, user email, recent status updates made by the user, etc. </p>

<pre class="lang-cs prettyprint-override"><code>class FacebookAccount
{
    public AccessToken AccessToken { get; set; }
    public string ScreeName { get; set; }
    public long FacebookUserId { get; set; }
    public string Email { get; set; }
    public List<StatusUpdate> ScheduledStatusNotPosted { get; set; }
    public List<StatusUpdate> StatusPosted { get; set; }
    private Facebook Facebook { get; set; }

    public List<FacebookAccount> Get(string userId)
    {
        var facebookAccountList = new List<FacebookAccount>();

        using (DataContext dc = new DataContext())
        {
            var facebookAccount = dc.FacebookAccounts.Where(x => x.UserId == userId);

            foreach (var account in facebookAccount)
            {
                var faceBook = new Facebook(account.UserId, account.UserId);
                facebookAccountList.Add(new FacebookAccount()
                {
                    AccessToken = new AccessToken() { Token = account.Token, TokenSecret = string.Empty },
                    Email = account.Email,
                    ScreeName = account.Email,
                    ScheduledStatusNotPosted = faceBook.GetScheduledStatusUpdates(),
                    FacebookUserId = account.UserId
                });
            }
        }

        return facebookAccountList;
    }

    public FacebookAccount Get(string userId, long facebookUserId)
    {
        using (DataContext dc = new DataContext())
        {
            var facebookAccount = dc.FacebookAccounts.FirstOrDefault(x => x.UserId == userId && x.UserId == facebookUserId);
            if (facebookAccount != null)
            {
                Facebook = new Facebook(facebookAccount.UserId, facebookAccount.UserId);

                return new FacebookAccount()
                {
                    AccessToken = new AccessToken() { Token = facebookAccount.Token, TokenSecret = string.Empty},
                    Email = facebookAccount.Email,
                    ScreeName = facebookAccount.Email,
                    ScheduledStatusNotPosted = Facebook.GetScheduledStatusUpdates(),
                    FacebookUserId = facebookAccount.UserId
                };
            }
        }
        return null;
    }
}
</code>```

答:
I don't know if it is "wrong," but it certainly doesn't look "right" to me. I don't know why you are doing what you are doing. It looks like you are creating instances, but it's unclear if these are new instances or copies.

If you are creating new instances, the usual way to do that is to use a static factory method. It's static so you don't have to have an existing instance to create a new instance.

```<code>    static public MyClass Create()
    {
        // set variable1, 2, 3...

        return new MyClass()
        {
            ClassVariable1 = variable1,
            ClassVariable2 = variable2,
            ClassVariable3 = variable3
        };
    }
</code>```

If that is a copy method, then call it "Copy." Calling it "Get" makes no sense in either case.

Body:
I don't know if it is "wrong," but it certainly doesn't look "right" to me. I don't know why you are doing what you are doing. It looks like you are creating instances, but it's unclear if these are new instances or copies.

If you are creating new instances, the usual way to do that is to use a static factory method. It's static so you don't have to have an existing instance to create a new instance.

    static public MyClass Create()
    {
        // set variable1, 2, 3...

        return new MyClass()
        {
            ClassVariable1 = variable1,
            ClassVariable2 = variable2,
            ClassVariable3 = variable3
        };
    }

If that is a copy method, then call it "Copy." Calling it "Get" makes no sense in either case.


问:
Is using hashes for primary keys a good idea?
The Austrian electronic ID card relies on the so-called sector identifiers. For example a hospital gets to identify a person by getting a sectorId for that person, which is computed roughly as follows:

```<code>sha1(personalId + "+" + prefix + sectorId); // prefix is constant and irrelevant
</code>```

Is that a good idea? I think the possibility of collision, no matter how small, poses a risk.

In hashtables, when there's a collision, you have other means of establishing equality, but with primary keys you can't possibly have two that are identical. That can be circumvented by a composite key, but then the point of a unique sector identifier is lost.

Is it ok to do that and is there a good way to have it that way without it breaking at some point?

答: Using a Hash or GUID as Primary Key is also bad idea because it causes Index Fragmentation and frequent Page Splits.</p>


答:
The answer is really fairly simple: if you want safety, use something that actually provides it--and that's not C, and not raw C-style arrays.

Without departing too far from the basic style of C and raw arrays, you can use C++ and an std::vector with [i] replaced by .at(i), and get bounds checking.

Using std::vector instead makes most of the problems with arrays easy. You can check the current size of the vector with its .size() member function. Most of the time, you don't need to do that though, because when you want to add something to it you just use its .push_back() member function.

At least in theory, you can sort of do most of the same sorts of things in C, but doing so gets relatively ugly. Although it's not terribly difficult to define a wrapper that (for example) puts a pointer and a current allocation size into a struct, you have to define functions to do all the manipulation on it, and even then you have to live with the fact that existing code won't know how to use it or deal with it. I've done this a few times, and if you need it badly enough you can make it work--but I long ago decided it just wasn't worth the pain.


答:
A function that receives a pointer does not know of the length of the corresponding array.  You must pass in as a parameter yourself explicitly:

```<code>void myFunc(int *yourArray, size_t yourArrayLen)
</code>```

Once you've done that, throwing an error is trivial.

Of course, this still leaves the possibility that your caller might give you the wrong length.  You can't really prevent that without either:

implementing a custom data type to store arrays and then making sure the length stays in-sync with the true length at all times using encapsulation, or

allowing static arrays only, e.g.

```<code>void myFunc(int (*yourArray)[8]);
</code>```

Body:
A function that receives a pointer does not know of the length of the corresponding array.  You must pass in as a parameter yourself explicitly:

void myFunc(int *yourArray, size_t yourArrayLen)

Once you've done that, throwing an error is trivial.

Of course, this still leaves the possibility that your caller might give you the wrong length.  You can't really prevent that without either:

implementing a custom data type to store arrays and then making sure the length stays in-sync with the true length at all times using encapsulation, or

allowing static arrays only, e.g.

void myFunc(int (*yourArray)[8]);


问:
Is using a lot of free third party libraries a good thing?
For your proprietary projects at work?

Problems I can imagine are: more dlls (can be a problem when for example you have to include a big DLL but you only need few functions in that library), a lot of license statements you have to take care of when releasing your product, the reliability of the library, etc.

Any other potential problems? What is your opinion?
(info: I'm using .NET at work)

答:
Why you should always consider an existing library over the complete rewrite your fellow developer is proposing, usually because it will be “better”:

Authors are experts in the domain covered by the library. This ensure that you will get perfect implementation. A good example is SharpMap. The authors are experts in geospacial softwares.

They will be more stable than your own as it is used by hundred if not multiple thousands of other users. You will certainly encounter problems, but most of them have already been encountered by others and fixed by authors. If they don't fix them, it's a good opportunity for you to contribute!

You will learn from others code or design. Many popular libraries are written by top notch developers and usually put in light good coding practices and design. You will learn by using them.

Save tons of money. The equivalent of hundred if not thousands man days of work for free or at the very worst one man day.

Get premium support. Paid libraries usually comes with free support from top notch developers you can contact 24h a day. Many developers of free libraries also provide that level of support. Exposing your team to those developers with be beneficial for them.

New features will appears automatically without efforts in your product. If you are using the reporting engine from vendor X, and vendor X releases the new feature Y. You will be able to provide that Y feature to your customer at no cost, with very low effort. You can then consider the authors of your libraries as other teams working for you, for free or very little money!

Unless your are an expert in the domain, you have thousands of users, you have nothing to learn from otehrs, you have tons of money, you don't need any support and you have plenty of resources, there is no reason to reinvent the wheel.

In response to Lenny's comment below, I will use this fact: when reporters asked Shepard what he thought about as he sat atop the Redstone rocket, waiting for liftoff, he had replied,

The fact that every part of this ship was built by the low bidder

Do you think NASA would have been able to send men to the moon if they tried to build the components of their rocket themselves?


问:
Why does Python's math.ceil return a float?
While I understand that the difference between integral and long values is blurred in Python, the difference between floats and integral values is not.

Therefore, I'm having a difficult time understanding why math.ceil and math.floor return floating-point values when the only possible outcome is integral, whole numbers.

For example, if I do the following:

```<code>math.ceil(7.5)
</code>```

I'd expect to get an integer with the value 8, not a floating-point number with the value 8.0. The same goes for math.floor.

Why does Python in this case use floating-point numbers where integers make more sense based on the output?

答:
Because 8.0 is a perfectly good floating point number.

Let's generalize the concept of math.ceil to include a "digits" parameter; that is, you get to choose the number of digits after the decimal point that you want to keep.  This isn't as far-fetched as it sounds; the Round function already has this ability.

By this new definition, Math.Ceil(12.755, 2) would return 12.76, which you wouldn't be able to return as an int.  The only values that could be returned as int would be those of the form Math.Ceil(x, 0), but it probably doesn't make much sense to have a function that returns a different type based on the value of one of its input parameters.

Anyway, it's more convenient to stay in the floating-point realm for working with these numbers, especially since any subsequent math on the returned numbers is almost certainly going to involve floating point anyway.


答:
It's not uncommon to have methods that return the type of the class--how else would a clone function work?

However, that's not what you are doing here.  You have a class that returns a list of itself.  I do not believe this function belongs in your Facebook class.

Title: Is it ok if a method returns a new instance of the class it's in?

Body:
I have a class called MyClass and a method called Get that looks something like this:

public class MyClass{
    string ClassVariable1 {get; set;}
    string ClassVariable2 {get; set;}
    string ClassVariable3 {get; set;}

    public MyClass Get()
    {
           ..set variable1, variable2, and variable3..

                return new MyClass()
                {
                    ClassVariable1 = variable1,
                    ClassVariable2 = variable2,
                    ClassVariable3 = variable3
                };
            }
        }
        return null;
    }
}
```

<p>Is there anything wrong with doing this from a design perspective? Or any other perspective?</p>

<p>I guess to make this more clear, I'll post my actual code. I'm writing a facebook app and my class is called "FacebookAccount". The idea is that this will hold stuff related to a facebook account, like screen name, user email, recent status updates made by the user, etc. </p>

<pre class="lang-cs prettyprint-override"><code>class FacebookAccount
{
    public AccessToken AccessToken { get; set; }
    public string ScreeName { get; set; }
    public long FacebookUserId { get; set; }
    public string Email { get; set; }
    public List<StatusUpdate> ScheduledStatusNotPosted { get; set; }
    public List<StatusUpdate> StatusPosted { get; set; }
    private Facebook Facebook { get; set; }

    public List<FacebookAccount> Get(string userId)
    {
        var facebookAccountList = new List<FacebookAccount>();

        using (DataContext dc = new DataContext())
        {
            var facebookAccount = dc.FacebookAccounts.Where(x => x.UserId == userId);

            foreach (var account in facebookAccount)
            {
                var faceBook = new Facebook(account.UserId, account.UserId);
                facebookAccountList.Add(new FacebookAccount()
                {
                    AccessToken = new AccessToken() { Token = account.Token, TokenSecret = string.Empty },
                    Email = account.Email,
                    ScreeName = account.Email,
                    ScheduledStatusNotPosted = faceBook.GetScheduledStatusUpdates(),
                    FacebookUserId = account.UserId
                });
            }
        }

        return facebookAccountList;
    }

    public FacebookAccount Get(string userId, long facebookUserId)
    {
        using (DataContext dc = new DataContext())
        {
            var facebookAccount = dc.FacebookAccounts.FirstOrDefault(x => x.UserId == userId && x.UserId == facebookUserId);
            if (facebookAccount != null)
            {
                Facebook = new Facebook(facebookAccount.UserId, facebookAccount.UserId);

                return new FacebookAccount()
                {
                    AccessToken = new AccessToken() { Token = facebookAccount.Token, TokenSecret = string.Empty},
                    Email = facebookAccount.Email,
                    ScreeName = facebookAccount.Email,
                    ScheduledStatusNotPosted = Facebook.GetScheduledStatusUpdates(),
                    FacebookUserId = facebookAccount.UserId
                };
            }
        }
        return null;
    }
}
</code>```


问:
Manuals - How Up To Date?
If you have a product that has been in the market for a long time, but it is still in active development daily - how often should the manuals be updated? If your users are constantly updated to the bleeding edge version due to the fact that your organization sees fit that the latest bug fixes are always in the shipped version. Meaning, you could fix a bug one day and next day it is hitting production.</p>

答:
I assume you are talking about end user documentation. Writing documentation is a pain in the @$$ and while I've developed some technique to convince me the inverse, I still have problems with that. This is how I try to manage it:

Integrate the update of the documentation in your DoD (Definition of done)

This will ensure that your documentation will be up to date at the end of each user story completion.

Here is the definition of done we wrote. I tried to keep the original formattings, so you get the idea. It's an A4 page put on the whiteboard.

---------- 8< ------------ Cut Here ------------ 8< ----------

The not negotiable

Definition of “Done”

Code with 80% unit test coverage, committed in repository

Screenshots if applicable (1024x728, 395x281, 170x121 & 729x329)

Feature descriptions if applicable (50 chars, 100 chars)

Full end user documentation

What's new file properly updated

---------- 8< ------------ Cut Here ------------ 8< ----------

Of course you can add a review process in the documentation. We have that since none of us are native English speakers.

One of the advantage of the Definition of Done like this is that your product is potentially shippable at the end of each user story completion.

Use this technique in combination with this one.

Title: Manuals - How Up To Date?


问:
Operations Job Queue Strategy
I am working on an application that requires manual approval of requests put in by customers. Now lets say there are 10 people involved in this operation and each of them has a webpage in front of them that serves one request after the other. The problem I am trying to solve is that the same customer request should not be served to multiple operations' people to prevent duplication of work. Right now I am solving it in the following way :

MySQL Table:

```<code>CREATE TABLE customer_request
(
   id             int PRIMARY KEY AUTO_INCREMENT,
   request_type   tinyint NOT NULL,
   request_body   varchar(100) NOT NULL,
   insert_date    timestamp DEFAULT current_timestamp,
   lock_status    tinyint,
   lock_time      datetime
);
</code>```

Whenever I serve a request to an operations person, I set the lock_status column to 1 and and the lock_time to now(). When the request has been processed(accepted/rejected/escalated) the lock is released.

Secondly there is a background thread that clears any lock older than x minutes.

I think there should be a better way to handle this. Any help is greatly appreciated.

答:
I think your solution is fine in general. It's an example of pessimistic locking and invaluable if you want to spare resources or process high-contention data.

In your case, as the purpose of these operators is solely to process your customer requests, contention seems likely. So, you're locking the record ensuring that it does not appear in other operator's queries. That is, you can distinguish locked and "available" records using your lock_status field.

IMO there is one thing missing. This is clearly a record stored in a database (i.e. you aren't removing requests from a queue or some internal message bus - e.g. JMS, MQ). So, you want to ensure that the record you lock was not locked during the time you acquired it (lost update problem).

So, I'd recommend an additional "lock" field that is effectively a lock on the record. This is to ensure that only you have locked the record in advance. Usually this would just be called something along the lines of "lock" or "version" but for to avoid confusion with lock_status I'll call it "record_lock".

When you query the customer_query, include record_lock in the query. Then, when executing the update to update your lock_status and lock_time your update should also include a new where clause where record_lock = /*the value retrieved during your query*/

This ensures that you are the only operator updating that record and that it hasn't changed since your read. How do you know it hasn't changed? The where clause stipulates that the record_lock is as it was when you queried it. What would have changed it? You! When you do the update above, you should also include an update to the record_lock to some new value (sequence, record-hash, datetime derived? it's up to you).

To summarise, read record_lock on your query and ensure it's unchanged when you update (the where clause). Include update to the record_lock in the update itself to indicate you've changed it - and so invalidate anyone else trying to lock this record. Once it's locked, it should be yours to work with providing these record all abide by your locked_status to query.

Example:

```<code>select request_id, lock_status, lock_time, record_lock
  from customer_request
  where lock_status = 0;
...
update customer_request
  set lock_status = 1,
      lock_time = ...,
      record_lock = <somenewvalue>
  where request_id = <requestid>
    and record_lock = <originalrecordlock>;
</code>```

Implications:

Pessimistic locking requires some overhead in managing the lock.

There is a risk of deadlock if you require multiple lock acquisition.

three is a risk of a record not being processed that you address with your timeout threshold.

Notes:

I would only name it "status" if it's a genuine status field. If it's just a binary true/ false, just call it "locked", "locked_flag", "is_locked" or whatever according to your standards.

Interesting aside... When we update lock_status, this update is using optimistic locking (using a record lock field). When you process the customer_request, we're using pessimistic locking (we've locked the record in advance).

Title: Operations Job Queue Strategy

Body:
I think your solution is fine in general. It's an example of pessimistic locking and invaluable if you want to spare resources or process high-contention data.

In your case, as the purpose of these operators is solely to process your customer requests, contention seems likely. So, you're locking the record ensuring that it does not appear in other operator's queries. That is, you can distinguish locked and "available" records using your lock_status field.

IMO there is one thing missing. This is clearly a record stored in a database (i.e. you aren't removing requests from a queue or some internal message bus - e.g. JMS, MQ). So, you want to ensure that the record you lock was not locked during the time you acquired it (lost update problem).

So, I'd recommend an additional "lock" field that is effectively a lock on the record. This is to ensure that only you have locked the record in advance. Usually this would just be called something along the lines of "lock" or "version" but for to avoid confusion with lock_status I'll call it "record_lock".

When you query the customer_query, include record_lock in the query. Then, when executing the update to update your lock_status and lock_time your update should also include a new where clause where record_lock = /*the value retrieved during your query*/

This ensures that you are the only operator updating that record and that it hasn't changed since your read. How do you know it hasn't changed? The where clause stipulates that the record_lock is as it was when you queried it. What would have changed it? You! When you do the update above, you should also include an update to the record_lock to some new value (sequence, record-hash, datetime derived? it's up to you).

To summarise, read record_lock on your query and ensure it's unchanged when you update (the where clause). Include update to the record_lock in the update itself to indicate you've changed it - and so invalidate anyone else trying to lock this record. Once it's locked, it should be yours to work with providing these record all abide by your locked_status to query.

Example:

select request_id, lock_status, lock_time, record_lock
  from customer_request
  where lock_status = 0;
...
update customer_request
  set lock_status = 1,
      lock_time = ...,
      record_lock = <somenewvalue>
  where request_id = <requestid>
    and record_lock = <originalrecordlock>;

Implications:

Pessimistic locking requires some overhead in managing the lock.

There is a risk of deadlock if you require multiple lock acquisition.

three is a risk of a record not being processed that you address with your timeout threshold.

Notes:

I would only name it "status" if it's a genuine status field. If it's just a binary true/ false, just call it "locked", "locked_flag", "is_locked" or whatever according to your standards.

Interesting aside... When we update lock_status, this update is using optimistic locking (using a record lock field). When you process the customer_request, we're using pessimistic locking (we've locked the record in advance).


答: What you're talking about is called a pessimistic offline lock. There are a number of ways of implementing them - the most common is similar to the way you propose, except instead of a background task to clear out the expired locks you simply clear them when you try to lock them and they're too old.</p>

Title: Operations Job Queue Strategy

Body:
I am working on an application that requires manual approval of requests put in by customers. Now lets say there are 10 people involved in this operation and each of them has a webpage in front of them that serves one request after the other. The problem I am trying to solve is that the same customer request should not be served to multiple operations' people to prevent duplication of work. Right now I am solving it in the following way :

MySQL Table:

```<code>CREATE TABLE customer_request
(
   id             int PRIMARY KEY AUTO_INCREMENT,
   request_type   tinyint NOT NULL,
   request_body   varchar(100) NOT NULL,
   insert_date    timestamp DEFAULT current_timestamp,
   lock_status    tinyint,
   lock_time      datetime
);
</code>```

Whenever I serve a request to an operations person, I set the lock_status column to 1 and and the lock_time to now(). When the request has been processed(accepted/rejected/escalated) the lock is released.

Secondly there is a background thread that clears any lock older than x minutes.

I think there should be a better way to handle this. Any help is greatly appreciated.


答: Using 3rd party libraries is almost always a good thing. But sometimes, when I would only need one little helper method, I look at how the 3rd party lib implements it, and then implement it myself to limit dependencies (if I'm allowed to do so).</p>

Title: Is using a lot of free third party libraries a good thing?

Body:
For your proprietary projects at work?

Problems I can imagine are: more dlls (can be a problem when for example you have to include a big DLL but you only need few functions in that library), a lot of license statements you have to take care of when releasing your product, the reliability of the library, etc.

Any other potential problems? What is your opinion?
(info: I'm using .NET at work)


答:
Short answer

It was a bug.
Well, not exactly a bug, but the behavior was changed based on a proposal for Python 3.
Now, ceil and floor  return integers (see also delnan's comment).
Some details are here: http://www.afpy.org/doc/python/2.7/whatsnew/2.6.html

Why Python originally returned floats

This question has some nice answers about the behaviour before Python 3. Since the mathematical operators where wrappers around the C mathematical operators, it made sense to follow the convention of that language.  Note that in C, the ceil function takes and returns a double. This makes sense because not all floats can be represented by integers (for values with a big exponent, there is no direct representation with integers).

Python was historically not explicitely designed to formally conform to some of the properties of mathematical operations (that would not happen by accident).   Guido Von Rossum has acknowledged some early design mistakes and explained the rationale behind the types used in Python, notably why he preferred C types instead of reusing the ones in ABC. See for example:

Early Language Design and Development

The Problem with Integer Division.
The division operator used to perform truncation when given integers or long, which was generally unexpected and error-prone: see Changing the Division Operator.

The language is supposed to evolve, though, and people tried to incorporate numeric type systems from other languages. For example, Reworking Python's Numeric Model and
  A Type Hierarchy for Numbers.

Why it should be an integer

The fact that integer 8 is also a real number does mean that we should return a floating point value after doing floor(8.2), exactly because we would not return a complex value with a zero imaginary part (8 is a complex number too).

This has to do with the mathematical definitions of the operations, not the possible machine representations of values: floor and ceiling mathematical functions are defined to return integers, whereas multiplication is a ring where we expect the product of x and y from set A to belong to set A too.
Arguably, it would be surprising if 8.2 * 10 returned the integer 82 and not a floating point; similarly the are no good reasons for floor(8.2) to return 8.0 if we want to be conform to the mathematical meaning.

By the way, I disagree with some parts of Robert Harvey's answer.

There are legitimate uses to return a value of a different type depending on an input parameter, especially with mathematical operations.

I don't think the return type should be based on a presupposed common usage of the value and I don't see how convenient it would be. And if it was relevant, I'd probably expect to be given an integer: I generally do not combine the result of floor with a floating point.

Inconvenience of Python 3

Using the operations from C in Python could be seen as a leaky abstraction of mathematical operations, whereas Python generally tries to provide a high-level view of data-structures and functions. It can be argued that people programming in Python expect operations that just work (e.g. arbitrary precision integers) and prefer to avoid dealing with numeric types at the level of C (e.g. undefined behaviour of overflow for unsigned signed integers). That's why PEP-3141 was a sensible proposition.

However, with the resulting abstraction, there might be some cases where performance might degrade, especially if we want to take the ceiling or floor of big floats without converting them to big integers (see comment from Mark Dickinson). Some may argue that this is not a big deal if a conversion occurs because it does not impact the overall performance of your program (and this is probably true, in most cases). But unfortunately, the problem here is that the programmer cannot choose which behaviour suits the most her needs. Some languages define more expressive functions: for example Common Lisp provides fflor and fceiling, which return floating-point values. It would be preferable if Python could provide fceil too. Alternatively, a sufficiently smart compiler could detect float(math.ceil(x)) and do the right thing.

Title: Why does Python's math.ceil return a float?

Body:
While I understand that the difference between integral and long values is blurred in Python, the difference between floats and integral values is not.

Therefore, I'm having a difficult time understanding why math.ceil and math.floor return floating-point values when the only possible outcome is integral, whole numbers.

For example, if I do the following:

```<code>math.ceil(7.5)
</code>```

I'd expect to get an integer with the value 8, not a floating-point number with the value 8.0. The same goes for math.floor.

Why does Python in this case use floating-point numbers where integers make more sense based on the output?


问:
Strategy pattern and "Is a" relationship
Imagine, I have class IAlgo which is an interface. I have derived from him and implemented his single method called matchCount in different implementations - AlgoA1, AlgoA2, AlgoA3, AlgoB1, AlgoB2.

```<code>class IAlgo
{
    virtual int matchCount(T1* p1, T2* p2) = 0;
}

class AlgoA1 : public IAlgo
{
    virtual int matchCount(T1* p1, T2* p2) override
    {
         // impl here
    }
}
</code>```

The difference between AlgoAN and AlgoBK is that category A is interested in all params of matchCount and category B is interested in only first param and the rest can be null - category B does not use them.

So as an encapsulated algorithm I am using them in Strategy pattern. After some time, the product owner says that he wants a new type of algorithms (AlgoC category) that adds a new parameter to matchCount method - say T3*. So we should go back and change the whole hierarchy of IAlgo and all subclass to have the following form:

```<code>    virtual int matchCount(T1* p1, T2* p2, T3* p3) = 0;
</code>```

This is a design problem and it seems that I had encapsulated algorithm to use it as a strategy, but it didn't work. How I have to solve this problem? Is my problem is the fact that AlgoA, AlgoB and AlgoC families are not related and they should not derive from IAlgo and they don't have "IS A" relationship? Should I have different interfaces IAlgoA, IAlgoB and IAlgoC for different families? Or all this is fine and I need some other solution?

答:
Optional arguments.  Declaring the new arguments as optional allows you to add parameters and not break the existing code.

Overloads.  Split the strategy interface into multiple methods.

Messages.  Create a hierarchy of argument objects and pass them to the strategy instead of "loose" parameters.  The mature calling code, will keep sending the messages, and the mature strategy will keep work with them without breaking.  Add new types of messages as needed.


问:
How has an increase in the complexity of systems affected successive generations of programmers?
As a "new" programmer (I first wrote a line of code in 2009), I've noticed it's relatively easy to create a program that exhibits quite complex elements today with things like .NET framework for example. Creating a visual interface, or sorting a list can be done with very few commands now.

When I was learning to program, I was also learning computing theory in parallel. Things like sorting algorithms, principles of how hardware operates together, boolean algebra, and finite-state machines. But I noticed if I ever wanted to test out some very basic principle I'd learned in theory, it was always a lot more difficult to get started because so much technology is obscured by things like libraries, frameworks, and the OS.

Making a memory-efficient program was required 40/50 years ago because there wasn't enough memory and it was expensive, so most programmers paid close attention to data types and how the instructions would be handled by the processor. Nowadays, some might argue that due to increased processing power and available memory, those concerns aren't a priority.

My question is if older programmers see innovations like these as a godsend or an additional layer to abstract through, and why might they think so? And do younger programmers benefit more learning low-level programming BEFORE exploring the realms of expansive libraries? If so then why?

答:
My first program (as a 15 years old teenager) was in 1974 in PL/1 on punched cards for an IBM 370/168 mainframe. My father was working at IBM and I was lucky enough to be able to go to the datacenter on Sundays.

At that time, a program of several thousands of statements (i.e.punched cards) was a big program (and a heavy one too, since many thousand of punched cards weighted many kilograms). Visual interfaces did not exist (a typical program read from its "standard input" using a punched card command starting with //GO.SYSIN DD * IIRC, but I did not master JCL).
Algorithmics was important, and IIRC the standard library was quite small by today's standard.

Today, programs of several thousands lines are generally considered small. For example, the GCC compiler has more than ten millions lines of source code, and nobody is understanding them fully.

My feeling is that programming today is quite different from the 1970s, because you need to use much more resources (in particular, existing libraries and software frameworks). However, I guess that people developing datacenter software (e.g. search engines at Google) or some embedded software care as much about algorithms and efficiency than the average programmer of the 1970s.

I still think that understanding low-level programming is important even today (even if most programmers won't code themselves basic container algorithms like balanced trees, dichotomically accessed sorted arrays, etc...) because understanding the whole picture is still important.

A major difference between the 1970s and today is the ratio of cost between developer's (human) efforts and computer power.


问:
Does HTTPS prevents man in the middle sending the same message several times
If a User is exchanging messages with a Server over HTTPS, could a man in the middle intercept messages and send them several time.

For instance, George orders a laptop on a https e-commerce, could Dimitri, a man in the middle, intercept the message and send it 100 times to the e-commerce so George will have 100 laptop ordered?

答:
This is called a "replay attack", and HTTPS (a.k.a. SSL) does protect against it.

The way SSL does this is described here in the spec:

Outgoing data is protected with a MAC before transmission.  To

prevent message replay or modification attacks, the MAC is computed

from the MAC secret, the sequence number, the message length, the

message contents, and two fixed character strings.  The message type

field is necessary to ensure that messages intended for one TLS

Record Layer client are not redirected to another.  The sequence

number ensures that attempts to delete or reorder messages will be

detected.  Since sequence numbers are 64 bits long, they should never

overflow.  Messages from one party cannot be inserted into the

other's output, since they use independent MAC secrets.  Similarly,

the server-write and client-write keys are independent, so stream

cipher keys are used only once.

If the initial SSL handshake succeeded, then only the client and server should know the symmetric key used for encryption and MAC generation, and the server will simply ignore any message with an invalid MAC or a sequence number it's seen before.


问:
"Is a" relationship or, in other words Inheritance
Say I define an interface IAnimal which has a pure virtual (abstract) method called eat like this:

```<code>class IAnimal
{
   virtual void eat(Food*) = 0;
};
</code>```

In the future I inherit form IAnimal and create different animals. and eventually I find out that some animals need not only food to eat but also other things. Say in my context, (it is easy to imagine it is a game), Dogs can eat if there is a Food and a Plate. Mans can eat if there is a Food, Plate, Table and Chair. Does this mean that it is wrong to use inherit from IAnimal? Can we say that Dog and Man are not animals as you can not use them interchangeably.

答:
Trying to come up with an accurate taxonomy of the real-world objects you want to represent with a class hierarchy is usually not that productive. Your interfaces should represent functionality or behavior rather than ontological categories, because that's what interfaces are: one or more functions you can call.

In other words, name the interface IEater instead of IAnimal, and this problem simply disappears, since anything that has an eat() method is clearly capable of eating.

Regarding your examples of additional objects, it's possible that eat(Food, Plate) is a special case of the more general eat(Food, Plate, Table, Chair), but it's also possible that these are two completely different kinds of eating that shouldn't be lumped together. That depends entirely on what eating means in the context of your application. If they are completely different, don't be afraid to choose more explicit and descriptive names like IPlateEater or IEaterWithTableManners.


答:
As indicated in the comments, there is no mixed usage of algorithms from the different families.

If there is no use-case where an IAlgo can refer to either an AlgoAN or an AlgoBK instance, then there is no reason to have a shared interface for the various families of algorithms.

The best way forward is to introduce a new interface for each algorithm family:

```<code>class IAlgoA
{
    virtual int matchCount(T1* p1, T2* p2) = 0;
};

class IAlgoB
{
    virtual int matchCount(T1* p1) = 0;
};
</code>```

Then you drop the existing IAlgo interface and you fixup the locations where it was used to refer to the new family-specific interfaces.

At this point, adding a third, fourth or fifth family of algorithms does not interfere with the existing families, as they don't have to conform to a shared interface any more.

Title: Strategy pattern and "Is a" relationship

Body:
Imagine, I have class IAlgo which is an interface. I have derived from him and implemented his single method called matchCount in different implementations - AlgoA1, AlgoA2, AlgoA3, AlgoB1, AlgoB2.

```<code>class IAlgo
{
    virtual int matchCount(T1* p1, T2* p2) = 0;
}

class AlgoA1 : public IAlgo
{
    virtual int matchCount(T1* p1, T2* p2) override
    {
         // impl here
    }
}
</code>```

The difference between AlgoAN and AlgoBK is that category A is interested in all params of matchCount and category B is interested in only first param and the rest can be null - category B does not use them.

So as an encapsulated algorithm I am using them in Strategy pattern. After some time, the product owner says that he wants a new type of algorithms (AlgoC category) that adds a new parameter to matchCount method - say T3*. So we should go back and change the whole hierarchy of IAlgo and all subclass to have the following form:

```<code>    virtual int matchCount(T1* p1, T2* p2, T3* p3) = 0;
</code>```

This is a design problem and it seems that I had encapsulated algorithm to use it as a strategy, but it didn't work. How I have to solve this problem? Is my problem is the fact that AlgoA, AlgoB and AlgoC families are not related and they should not derive from IAlgo and they don't have "IS A" relationship? Should I have different interfaces IAlgoA, IAlgoB and IAlgoC for different families? Or all this is fine and I need some other solution?

Body:
As indicated in the comments, there is no mixed usage of algorithms from the different families.

If there is no use-case where an IAlgo can refer to either an AlgoAN or an AlgoBK instance, then there is no reason to have a shared interface for the various families of algorithms.

The best way forward is to introduce a new interface for each algorithm family:

class IAlgoA
{
    virtual int matchCount(T1* p1, T2* p2) = 0;
};

class IAlgoB
{
    virtual int matchCount(T1* p1) = 0;
};

Then you drop the existing IAlgo interface and you fixup the locations where it was used to refer to the new family-specific interfaces.

At this point, adding a third, fourth or fifth family of algorithms does not interfere with the existing families, as they don't have to conform to a shared interface any more.


问:
Is there any reason to use C++ instead of C, Perl, Python, etc.?
As a Linux (server side) developer, I don't know where and why should I use C++.

When I'm going for performance, the first and last choice is C.

When "performance" isn't the main issue, programming languages like Perl and Python would be good choices.

Almost all open source applications I know in this area have been written in C, Perl, Python, Bash script, AWK or even PHP, but no one uses C++.

I'm not discussing other areas like GUI or web applications, I'm just talking about Linux, CLI and daemons.

Is there any satisfactory reason to use C++?

答:
I'm just switching from C to C++, and I think the gain is considerable, even if you don't have a need for templates and OOP.

Better memory management

Stronger type system

A better standard library

Namespaces

etc.


答:
I have encountered this exact problem. I was not satisfied with the way I solved it, so I didn't answer your question right away. But on the other hand, neither does any of the other answers here.

My solution:

```<code>class IAlgo
{
    virtual int matchCount(AlgoParameters *) = 0;
}

class AlgoA1 : public IAlgo
{
    virtual int matchCount(AlgoParameters * algoParameters) override
    {
        a1 = algoParameters.a1;
        a2 = algoParameters.a2;
    }
}

class AlgoB1 : public IAlgo
{
    virtual int matchCount(AlgoParameters * algoParameters) override
    {
        b1 = algoParameters.b1;
        b2 = algoParameters.b2;
    }
}

class AlgoParameters
{
public:
    AParam1 a1;
    AParam2 a1;

    BParam1 b1;
    BParam2 b1;

    CParam1 c1;
    CParam2 c1;
}
</code>```

As you see I simply encapsuled all parameters for all algorithms into one data-structure. No, this is no nice solution. I do not suggest you use it. But it allows you to do a call like this:

```<code>IAlgo GetAlgorithm(EAlgorithmType eAlgoType)
{
    switch(eAlgoType){
    case eAlgoA1:
        return AlgoA1();
    case ...
    }
}
AlgoParameters SetAlgorithmParameters(EAlgorithmType eAlgoType)
{
    AlgoParameters algoParam;
    switch(eAlgoType){
    case eAlgoA1:
        algoParam.a1 = ...;
        algoParam.a2 = ...;
    case ...
    }
}
int UseAlgorithm(IAlgo algoritm, AlgoParameters parameters)
{
    return algorithm.matchCount(&parameters);
}
</code>```

It violates a lot of principles in software developement (e.g. Open/Close-Principle). But it works and is relatively easily expandable for new algorithms.

Body:
I have encountered this exact problem. I was not satisfied with the way I solved it, so I didn't answer your question right away. But on the other hand, neither does any of the other answers here.

My solution:

class IAlgo
{
    virtual int matchCount(AlgoParameters *) = 0;
}

class AlgoA1 : public IAlgo
{
    virtual int matchCount(AlgoParameters * algoParameters) override
    {
        a1 = algoParameters.a1;
        a2 = algoParameters.a2;
    }
}

class AlgoB1 : public IAlgo
{
    virtual int matchCount(AlgoParameters * algoParameters) override
    {
        b1 = algoParameters.b1;
        b2 = algoParameters.b2;
    }
}

class AlgoParameters
{
public:
    AParam1 a1;
    AParam2 a1;

    BParam1 b1;
    BParam2 b1;

    CParam1 c1;
    CParam2 c1;
}

As you see I simply encapsuled all parameters for all algorithms into one data-structure. No, this is no nice solution. I do not suggest you use it. But it allows you to do a call like this:

IAlgo GetAlgorithm(EAlgorithmType eAlgoType)
{
    switch(eAlgoType){
    case eAlgoA1:
        return AlgoA1();
    case ...
    }
}
AlgoParameters SetAlgorithmParameters(EAlgorithmType eAlgoType)
{
    AlgoParameters algoParam;
    switch(eAlgoType){
    case eAlgoA1:
        algoParam.a1 = ...;
        algoParam.a2 = ...;
    case ...
    }
}
int UseAlgorithm(IAlgo algoritm, AlgoParameters parameters)
{
    return algorithm.matchCount(&parameters);
}

It violates a lot of principles in software developement (e.g. Open/Close-Principle). But it works and is relatively easily expandable for new algorithms.


问:
Declarative Transactions in Node.js
Back in the day, it was common to manage database transactions in Java by writing code that did it. Something like this:

```<code>Transaction tx = session.startTransaction();
...
try {
   tx.commit();
} catch (SomeException e){
   tx.rollback();
}
</code>```

at the beginning and end of every method. This had some obvious problems - it's redundant, hides the intent of what's happening, etc. So, along came annotation-driven transactions:

```<code>@Transaction
public SomeResultObj getResult(...){

    ...
}
</code>```

Is there any support for declarative transaction management in node.js?

答:
In case you're using Express + Sequelize, you can do this by using a module called continuation-local-storage (henceforth cls, for brevity).

Show me the code

```<code>// When initializing your Sequelize instance
var Sequelize = require( "sequelize" );
Sequelize.cls = require( "continuation-local-storage" ).createNamespace( "db" );

module.exports = new Sequelize(...);
</code>```

Then, create some middleware to initialize your transaction in every request (in case no one has come up with one yet):

```<code>var sequelize = require(...);
app.use(function ( req, res, next ) {
  sequelize.transaction(function () {
    // Do something else, but be sure to invoke next middleware
    next();
  });
});
</code>```

What's going on here?

A cls namespace is declared for Sequelize to use. Must be set on the constructor;

A transaction is initialized in an Express middleware;

Every subsequent sequelize query will use the request transaction by default (you can override this behavior, obviously).

How this works?

This works because cls assigns a new storage for each new async chain (the request in this example), and they are passed down consistently and transparently.

It's some kind of magic that happens inside Node :)

Tips

It's very likely that you'll need to use some monkeypatch to take that approach. Depends on your Promises implementation.

https://www.npmjs.com/package/cls-bluebird

https://www.npmjs.com/package/cls-q

Further info

Check the Sequelize documentation.

Title: Declarative Transactions in Node.js

Body:
In case you're using Express + Sequelize, you can do this by using a module called continuation-local-storage (henceforth cls, for brevity).

Show me the code

// When initializing your Sequelize instance
var Sequelize = require( "sequelize" );
Sequelize.cls = require( "continuation-local-storage" ).createNamespace( "db" );

module.exports = new Sequelize(...);

Then, create some middleware to initialize your transaction in every request (in case no one has come up with one yet):

var sequelize = require(...);
app.use(function ( req, res, next ) {
  sequelize.transaction(function () {
    // Do something else, but be sure to invoke next middleware
    next();
  });
});

What's going on here?

A cls namespace is declared for Sequelize to use. Must be set on the constructor;

A transaction is initialized in an Express middleware;

Every subsequent sequelize query will use the request transaction by default (you can override this behavior, obviously).

How this works?

This works because cls assigns a new storage for each new async chain (the request in this example), and they are passed down consistently and transparently.

It's some kind of magic that happens inside Node :)

Tips

It's very likely that you'll need to use some monkeypatch to take that approach. Depends on your Promises implementation.

https://www.npmjs.com/package/cls-bluebird

https://www.npmjs.com/package/cls-q

Further info

Check the Sequelize documentation.


问:
How to represent an algorithm as a class?
I am trying to understand how to design classes which take an input, do some processing, and return a result. More specifically, should the object store the intermediate results between function calls as state, or should all methods be static, and pass params around.

Example: I am currently working on a class which detects, and removes headers & footer from pages in a document. The algorithm steps are outlined below:

HeaderFooterFilter

```<code>removeHeaderFooterText(pages) ->
  candidates = getBoundaryBlocks(pages);
  toRemove = detectRepeatingBlocks(candidates);
  removeBlocks(toRemove);
</code>```

So my question is, should pages, candidates, toRemove be members of the class, or should the methods be static and pass them around?

Is there a design-pattern which deals with algorithm classes?

答:
I am trying to understand how to design classes which take an input, do some processing, and return a result.

There is a principle in object orientated design called "Tell, don't ask"

What this generally means is you tell an object to change itself, you don't ask an object for data that you then manipulate some where else.

So when thinking about how to manage something like this in OO design you need to keep coming back to your objects and what they themselves do. In general you don't have an object that takes input from another objects, because you make the objects themselves do the work.

So taking your example there are a number of immediate design issues that spring up. You are passing a collection of pages into an unnamed object that will then process them. It is probably better to let the pages themselves handle this.

So instead of

```<code>removeHeaderFooterText(pages)
</code>```

try instead

```<code>pages.each |page|
    page.removeHeaderFooterText
</code>```

Nothing then outside of the page object itself has to know how the heck to remove header and footer text from the page.

You have the steps to achieve this as

```<code>getBoundaryBlocks
detectRepeatingBlocks
removeBlocks
</code>```

Straight away a a noun jumps out, "Boundary Blocks". A page has boundary blocks, so there shouldn't be something outside of the Page object concerned with getting boundary blocks out of the page

```<code>class Page
    def removeHeaderFooterText
        page_blocks.each |block|
            if block.repeating_block?
               page_blocks.delete(block)
</code>```

See you have an object in there of a block, which itself knows how to determine if it is a repeating block or not. If it is the page removes it. The page doesn't care how the block determines if it is a repeating block or not.

This is just a quick example. To be honest I'm not up on my document layout terminology so this might not quite match what you are trying to do.

tl;dr version

The point is that in object orientated design an "algorithm" will not be represented as a single list of procedural operations to carry out.

It will instead be a set of interactions between objects. As Adele Goldberg said about Smalltalk, one of the earliest object orientated languages, "In Smalltalk, everything happens some where else"

You will (should) never have a list of algorithmic procedural instructions to carry out an algorithm. You will instead of a number of interacting objects, each with a small unit of behaviours, that working together will produce the outcome of the algorithm. Every object is a self contained unit of behaviour and the system achieves its goals by each of these units telling other units to do something.

The issue of course with this is it requires a very different way of thinking about problems and algorithms, which is why often claims of object orientated design fall back on procedural design.

Body:
I am trying to understand how to design classes which take an input, do some processing, and return a result.

There is a principle in object orientated design called "Tell, don't ask"

What this generally means is you tell an object to change itself, you don't ask an object for data that you then manipulate some where else.

So when thinking about how to manage something like this in OO design you need to keep coming back to your objects and what they themselves do. In general you don't have an object that takes input from another objects, because you make the objects themselves do the work.

So taking your example there are a number of immediate design issues that spring up. You are passing a collection of pages into an unnamed object that will then process them. It is probably better to let the pages themselves handle this.

So instead of

removeHeaderFooterText(pages)

try instead

pages.each |page|
    page.removeHeaderFooterText

Nothing then outside of the page object itself has to know how the heck to remove header and footer text from the page.

You have the steps to achieve this as

getBoundaryBlocks
detectRepeatingBlocks
removeBlocks

Straight away a a noun jumps out, "Boundary Blocks". A page has boundary blocks, so there shouldn't be something outside of the Page object concerned with getting boundary blocks out of the page

class Page
    def removeHeaderFooterText
        page_blocks.each |block|
            if block.repeating_block?
               page_blocks.delete(block)

See you have an object in there of a block, which itself knows how to determine if it is a repeating block or not. If it is the page removes it. The page doesn't care how the block determines if it is a repeating block or not.

This is just a quick example. To be honest I'm not up on my document layout terminology so this might not quite match what you are trying to do.

tl;dr version

The point is that in object orientated design an "algorithm" will not be represented as a single list of procedural operations to carry out.

It will instead be a set of interactions between objects. As Adele Goldberg said about Smalltalk, one of the earliest object orientated languages, "In Smalltalk, everything happens some where else"

You will (should) never have a list of algorithmic procedural instructions to carry out an algorithm. You will instead of a number of interacting objects, each with a small unit of behaviours, that working together will produce the outcome of the algorithm. Every object is a self contained unit of behaviour and the system achieves its goals by each of these units telling other units to do something.

The issue of course with this is it requires a very different way of thinking about problems and algorithms, which is why often claims of object orientated design fall back on procedural design.