答:
It's not entirely clear whether you are talking about the semantics or the pragmatics here. Your question reads more about semantics, but in the comments you say you are asking about pragmatics. I'm going to answer about both.

Semantics

Pros: simplicity. Why have two concepts when one will do?

Cons: none.

Pragmatics

Pros: simplicity. Again.

Cons: Performance.

The Real World

The way this is used in the real world is that you have pure object-oriented languages whose implementations use primitives without ever exposing them to the programmer. So, the only people who need to deal with the complexities of having two different "things" (objects and non-object primitives) are the compiler/interpreter implementors. Language users simply use objects.

So, in other words, use semantics without primitives, but implement them with primitives. YARV, Rubinius, MRuby, MacRuby, pretty much all Lisp and Smalltalk (and derivatives such as Self and Newspeak) implementations, some ECMAScript implementations, and probably many others do this.

The techniques for doing this have been known since (at least) the 80s. Like many other useful things, the designers of the Java platform seem to have forgotten them when they designed the Java platform, and are now fighting a decades long uphill battle against backwards compatibility constraints to remove primitives again. (They are rumored to be gone by Java 10, but then again, this rumor has been going around for 15 years now.)

Title: What's the benefit of having everything as an object in dynamically typed programming language?

Body:
modern dynamic programming languages like Python, Ruby and Javascript all take the approach of treating everything as an object, what's the benefit of this approach, and what's the curse of it?

Benefit I can think of: first class functions, duck typing.

Drawbacks:performance issue, especially performance of primitive types like integer etc.


问:
Quickly write log entries from http requests in a MySQL database with high performance
When a dataset is saved in the software I use, a http request is triggered (fire-and-forget).
Information like the timestamp, the user and the changes made are listed in the GET parameters.

Now I need to write an application which is able to take thousands of requests per minute and write them into a MySQL database.

I think an Apache web server with PHP is too slow to handle that.

What do I use instead? What about a Java web server? Can someone recommend a solution for this?

答:
The first 3 things you need to do is profile, profile, and of course, profile.

You are trying to fix a problem that you don't know you actually have.  While its good to try to anticipate problems, it takes a lot of experience with databases to be able to really 'sense' them accurately.

Set up a test script - it sounds like it should be only a few lines of code - and insert a few million entries into your log table the way your application would.  See how long it takes.  If the timing is acceptable... you don't have to do anything.

If the timing is too slow, now start optimizing.  Things to look at:

Use ISAM as opposed to InnoDB for the table's storage engine.

Make sure you don't have a lot of indexes on the table, that slows down writes.

Or perhaps try a MEMORY table.

Try using INSERT DELAYED which will hold the inserts until the table is writable, but not lock your application.

If you find that is too slow, then its time to start considering other options.

ContentLicense: CC BY-SA 3.0


问:
How to represent "class" data type in C or any low level language?
I'm writing a simple language compiler,currently it's support functional programming only.

I want to support OOP concept, but I don't know how exactly I can represent an object ? just where to start .

Edit:

I just want to how to represent a class data type,not how to inheritance,.. etc.

答:
Simple objects are just a struct containing all the fields.  Methods typically take a pointer to that struct as their first argument (like python does).  This simple form of OOP is done all the time in C.

Inheritance makes things more complicated.  You can add a pointer to the parent object from the child object, or include all the parent's fields.  Virtual methods require a vtable as part of the struct, which is basically function pointers for every virtual method.  You need to add code that handles polymorphic dispatch.  Look at GObject for a good production-quality example of this kind of OOP in C.


问:
best way to attach database structure for an open source project
I have a small project which I want to publish on github. The problem is that my project contains a database. I don't know what is the best way to attach the database structure.

I may get the create queries and put in a file called db.sql, but is this the best way?

答:
I would do a separation between the schema of your database and the actual initial content. For a simple and small application, you could create a schema.sql and a seed.sql. The schema holds all the statements to create the database and its tables. The seed contains INSERT statements.

If you want to go one step further, seed the database through your code.

There's a good example of how to accomplish the migration of database structures and seeding the database in the framework Ruby on Rails. Have a look at the migration guide for Rails.


问:
Why do we have to mention the data type of the variable in C
Usually in C, we have to tell the computer the type of data in variable declaration. E.g. in the following program, I want to print the sum of two floating point numbers X and Y.

```<code>#include<stdio.h>
main()
{
  float X=5.2;
  float Y=5.1;
  float Z;
  Z=Y+X;
  printf("%f",Z);

}
</code>```

I had to tell the compiler the type of variable X.

Can't the compiler determine the type of X on its own?

Yes, it can if I do this:

```<code>#define X 5.2
</code>```

I can now write my program without telling the compiler the type of X as:

```<code>#include<stdio.h>
#define X 5.2
main()
{
  float Y=5.1;
  float Z;
  Z=Y+X;
  printf("%f",Z);

}
</code>```

So we see that C language has some kind of feature, using which it can determine the type of data on its own. In my case it determined that X is of type float.

Why do we have to mention the type of data, when we declare something in main()? Why can't the compiler determine the data type of a variable on its own in main() as it does in #define.

答:
The short answer is C needs types because of history / representing the hardware.

History: C was developed in the early 1970s and intended as a language for systems programming. Code is ideally fast and makes the best use of the capabilities of the hardware.

Inferring types at compile time would have been possible, but the already slow compile times would have increased (refer to XKCD's 'compiling' cartoon. This used to apply to 'hello world' for at least 10 years after C was published). Inferring types at runtime would not have fitted the aims of systems programming. Runtime inferrence requires additional run time library. C came long before the first PC. Which had 256 RAM. Not Gigabytes or Megabytes but Kilobytes.

In your example, if you omit the types

```<code>   X=5.2;
   Y=5.1;

   Z=Y+X;
</code>```

Then the compiler could have happily worked out that X & Y are floats and made Z the same. In fact, a modern compiler would also work out that X & Y aren't needed and just set Z to 10.3.

Assume that the calculation is embedded inside a function. The function writer might want to use their knowledge of the hardware, or the problem being solved.

Would a double be more appropriate than a float ? Takes more memory and is slower but the accuracy of the result would be higher.

Maybe the return value of the function could be int (or long) because the decimals were not important, although conversion from float to int is not without cost.

The return value could also be made double guaranteeing that float + float does not overflow.

All of these questions seem pointless for the vast majority of code written today, but were vital when C was produced.

Body:
The short answer is C needs types because of history / representing the hardware.

History: C was developed in the early 1970s and intended as a language for systems programming. Code is ideally fast and makes the best use of the capabilities of the hardware.

Inferring types at compile time would have been possible, but the already slow compile times would have increased (refer to XKCD's 'compiling' cartoon. This used to apply to 'hello world' for at least 10 years after C was published). Inferring types at runtime would not have fitted the aims of systems programming. Runtime inferrence requires additional run time library. C came long before the first PC. Which had 256 RAM. Not Gigabytes or Megabytes but Kilobytes.

In your example, if you omit the types

   X=5.2;
   Y=5.1;

   Z=Y+X;

Then the compiler could have happily worked out that X & Y are floats and made Z the same. In fact, a modern compiler would also work out that X & Y aren't needed and just set Z to 10.3.

Assume that the calculation is embedded inside a function. The function writer might want to use their knowledge of the hardware, or the problem being solved.

Would a double be more appropriate than a float ? Takes more memory and is slower but the accuracy of the result would be higher.

Maybe the return value of the function could be int (or long) because the decimals were not important, although conversion from float to int is not without cost.

The return value could also be made double guaranteeing that float + float does not overflow.

All of these questions seem pointless for the vast majority of code written today, but were vital when C was produced.


答:
Most O/RM frameworks provide a means to initialize and seed a database. Via Code. For example, Entity Framework allows you to create a custom database initializer and implement a seed method to provide seed data (If you just want to have it generate the database, creating a seed function is not necessary).

If you're not using an O/RM, you can use the built-in function of your Database to generate a create script. For example, here is a walkthrough of how to do it with SQL Server

Yet another option would be to put your database scripts into source control (if you're manually managing the database), of course you'd need a way to have the scripts compiled into a single execution, also taking care to order the scripts properly in order to properly handle dependencies between the tables.

Title: best way to attach database structure for an open source project

Body:
I have a small project which I want to publish on github. The problem is that my project contains a database. I don't know what is the best way to attach the database structure.

I may get the create queries and put in a file called db.sql, but is this the best way?


问:
Why would a program require a specific minimum number of CPU cores?
Is it possible to write code (or complete software, rather than a piece of code) that won't work properly when run on a CPU that has less than N number of cores? Without checking it explicitly and failing on purpose:

IF (noOfCores < 4) THEN don't run properly on purpose

I'm looking at a game's (Dragon Age: Inquisition) minimum system requirements, and it states a minimum of a four-core CPU. Many players say it does NOT run on two-core CPU's and EVEN on Intel Core i3s with two physical and two logical cores. And it's NOT a problem of computing power.

From my understanding, threads are completely isolated from the CPU by the OS since that cannot be done.

Just to clear things out:

I am NOT asking "Can I find out the number of CPU cores from code, and fail on purpose?" ... Such code would be ill-intentioned (forces you to buy a more expensive CPU to run a program - without the need of computational power). I am asking that your code, say, has four threads and fails when two threads are run on the same physical core (without explicitly checking system information and purposely failing).

In short, can there be software that requires multiple cores, without needing additional computing power that comes from multiple cores? It would just require N separate physical cores.

答:
Three realtime threads that never sleep and one other thread. If there are less than four cores, the fourth thread never runs. If the fourth thread needs to communicate with one of the realtime threads for the realtime thread to finish, the code will not finish with less than four cores.

Obviously if realtime threads are waiting on something that doesn't allow them to sleep (such as a spinlock) the program designer screwed up.


问:
Database design for objects with multiple states
Context

I'm designing a database which, simplified, should be able to handle users sending job requests to each other, and after that a job can be started, finished, and reviewed. The design should be scalable (think millions of users).

Approaches I've considered:

Gargantuan table

One approach, probably not the best one, would be to simply store ALL jobs in one, huge table jobs. This table would need a state column to represent which state the job is currently in (e.g. ACCEPTED, STARTED, FINISHED, REVIEWED e.t.c.). The biggest problem with this approach that I can see is that jobs in different states have different types of data that are relevant to them. For example, a job request has a preliminary agreed upon price, but that could change before the job is started, and change again before the job is finished. This could of course be solved by just adding more columns to the table and naming them properly, but it will probably become a huge bottleneck performance-wise very early to have one table containing all the different types of possible data for all the different possible states of a job.

Different tables for different states

This approach would be to have multiple tables, for example job_requests, jobs_started, jobs_finished, tables who in turn can have substates, e.g. job_requests could have the sub-states PENDING, ACCEPTED, while the jobs_finished table would have the substates COMPLETED, CANCELLED, REVIEWED.

With this approach each table only contains data which is relevant to the current job state, but on the other hand some data might be duplicated (for example the user ids of the job requester, and job receiver -- on the other hand this information could be stored in yet another table?).
The problem with this approach is that I can't think of a good solution on how to archive all the information when transitioning between states. For example, once a job request has been accepted, and then started, it should be deleted from the job_requests table and moved into the jobs_started table, but it's just a matter of time before a stakeholder wants to know for example how long the average time is between a job request being created, until it's been started, at which point I'd need the data from the job_requests table to be able to calculate it.

It feels like this type of problem should be easy to solve, but I really can't think of any good solution which "feels right", any solution I come up with feels ugly and I can immediately think of a number of things which makes the solution bad.

Very grateful for any feedback or tips on approaches I could take. Thanks in advance!

答: How about a hybrid of the two? A large table with a JobID and state (and perhaps other information common to all jobs and states), with other tables to manage the additional state information. That reduces (or eliminates) the duplication of data, but keeps things more manageable.</p>


答:
Sounds like you have 3 major categories of data you are trying to store:

General job data (job id, job requester id, job receiver id etc)

State transitions (job started, job finished)

State-specific job data

(optional) job-related events (price changed, job receiver user reassigned etc)

The key is to separate event-like data from everything else.

Schema design

Here are some details:

1. General jobs table

All of the information that's NOT state-specific goes into the (let's say) "jobs" table. Auto-generated primary key: job_id

2. State transitions table

All information about state transitions goes into "job_state_transitions" table, which might have the following columns:

job_transition_id

job_id

created_at

from_state

to_state

Ideally this table is append-only. Nothing is every updated or removed here.

Using such a table, you can find out the latest status of any particular job by selecting the latest row for a given job_id from the job_transitions table. You can further denormalize that and introduce a "job_state" column, the contents of which are updated every time a new row is inserted in job_transition table (stored procedures might help here if that's your thing).

You can also do all sorts of analysis on state transitions, because the timing data is preserved (created_at is a date/time field that can help with that)

3. State-specific job data

All of the state-specific data goes into "[state]-jobs" tables. Primary key: some sequence id. Main index: job_transition_id

4. Optional event data

You can also introduce an "audit trail" table that allows you to keep track of various changes that users might request for each job, like your "change agreed upon price". This is a generalization of the state transition table: one main table that contains events and a one supplemental table for each event type (for example, price_changes table with job_id, created_at, from_price and to_price columns).

Scaling to millions of users

If the main "jobs" table grows to be unwieldy, you can shard it by job_id or requesting_user_id or something like that.

Likewise, events table should be append-only and can be rotated or purged of events related to jobs that have been finished.

Title: Database design for objects with multiple states

Body:
Context

I'm designing a database which, simplified, should be able to handle users sending job requests to each other, and after that a job can be started, finished, and reviewed. The design should be scalable (think millions of users).

Approaches I've considered:

Gargantuan table

One approach, probably not the best one, would be to simply store ALL jobs in one, huge table jobs. This table would need a state column to represent which state the job is currently in (e.g. ACCEPTED, STARTED, FINISHED, REVIEWED e.t.c.). The biggest problem with this approach that I can see is that jobs in different states have different types of data that are relevant to them. For example, a job request has a preliminary agreed upon price, but that could change before the job is started, and change again before the job is finished. This could of course be solved by just adding more columns to the table and naming them properly, but it will probably become a huge bottleneck performance-wise very early to have one table containing all the different types of possible data for all the different possible states of a job.

Different tables for different states

This approach would be to have multiple tables, for example job_requests, jobs_started, jobs_finished, tables who in turn can have substates, e.g. job_requests could have the sub-states PENDING, ACCEPTED, while the jobs_finished table would have the substates COMPLETED, CANCELLED, REVIEWED.

With this approach each table only contains data which is relevant to the current job state, but on the other hand some data might be duplicated (for example the user ids of the job requester, and job receiver -- on the other hand this information could be stored in yet another table?).
The problem with this approach is that I can't think of a good solution on how to archive all the information when transitioning between states. For example, once a job request has been accepted, and then started, it should be deleted from the job_requests table and moved into the jobs_started table, but it's just a matter of time before a stakeholder wants to know for example how long the average time is between a job request being created, until it's been started, at which point I'd need the data from the job_requests table to be able to calculate it.

It feels like this type of problem should be easy to solve, but I really can't think of any good solution which "feels right", any solution I come up with feels ugly and I can immediately think of a number of things which makes the solution bad.

Very grateful for any feedback or tips on approaches I could take. Thanks in advance!


问:
Handling Slow External API's in Web Application
So I have got an application that during various steps during a user's process will transmit and receive data from external API's.

Currently the way this is handled is fairly rudimentary.  There will be a POST, DELETE, or PUT to the app.  The application will then make the necessary updates in the DB and submit any information to the API's we are using.  After we get responses from the DB and API we respond to the user's request.

When I started with the application the external services we were using were small, however after working on it for a few years, the number has been growing.  After working with a particularly painful API that has slow response times, I'm finding that this response to the user's request is starting to feel slow and painful.

How does one handle interacting with slow external services?

Now on a higher level, I know that one solution would be to implement a queue-like system for these API's where the calls are pushed into a queue and a separate process / service processes them.  Update the DB, fire off the API updates to the micro-service and call it a day.  Are there any other approaches for handling this sort of situation?

答:
Calling the external API asynchronously is the best bet.

When it is part of a user's process, it is best to show feedback that the call is underway and some indication whether the call finished and succeeded or failed once you get response.
If possible, let the user give other input while waiting for feedback on the external API, this negates the "slow and painful" feeling.


答:
It is unlikely that these "minimum requirements" represent something below which the game will not run.  Far more likely is that they represent something below which the game will not run with acceptable performance.  No game company wants to deal with lots of customers complaining about crappy performance when they are running it on a single core 1 Ghz box, even if the software could technically run.  So they probably deliberately design to fail hard on boxes with fewer cores than would give them acceptable performance.

One important metric in game performance is the frame rate.  Typically they run at either 30 or 60 frames per second.  This means that the game engine has to render the current view from the game state in a fixed amount of time.  To achieve 60 fps, it has just a bit more than 16 msecs to do this.  Games with high-end graphics are extremely CPU bound and so there's a huge give-and-take between trying to push higher quality (which takes more time) and the need to stay in this time budget.  Thus, the time budget for each frame is extremely tight.

Because the time budget is tight, the developer ideally wants exclusive access to one or more cores.  They also likely want to be able to do their rendering stuff in a core, exclusively, as it's what has to get done on that time budget, while other stuff, like calculating the world state, happens on a separate process where it won't intrude.

You could, in theory, cram all this onto a single core, but then everything becomes much harder.  Suddenly you have to make sure all that game state stuff happens fast enough, and allows your rendering to happen.  You can't just make them two software threads because there's no way to make the OS understand "thread A must complete X amount of work in 16 msecs regardless of what thread B does".

Game developers have zero interest in making you buy new hardware.  The reason they have system requirements is that the cost of supporting lower end machines is not worth it.

Title: Why would a program require a specific minimum number of CPU cores?

Body:
Is it possible to write code (or complete software, rather than a piece of code) that won't work properly when run on a CPU that has less than N number of cores? Without checking it explicitly and failing on purpose:

IF (noOfCores < 4) THEN don't run properly on purpose

I'm looking at a game's (Dragon Age: Inquisition) minimum system requirements, and it states a minimum of a four-core CPU. Many players say it does NOT run on two-core CPU's and EVEN on Intel Core i3s with two physical and two logical cores. And it's NOT a problem of computing power.

From my understanding, threads are completely isolated from the CPU by the OS since that cannot be done.

Just to clear things out:

I am NOT asking "Can I find out the number of CPU cores from code, and fail on purpose?" ... Such code would be ill-intentioned (forces you to buy a more expensive CPU to run a program - without the need of computational power). I am asking that your code, say, has four threads and fails when two threads are run on the same physical core (without explicitly checking system information and purposely failing).

In short, can there be software that requires multiple cores, without needing additional computing power that comes from multiple cores? It would just require N separate physical cores.


问:
How to handle a memory consuming member variable
Suppose multiple objects of the same class should do something which takes a memory consuming resource. Which of the following approaches is recommended to define and use the resource by the objects?

Each object create the resource in its own

Declare one resource instance and pass it to each object

Using a static object for the resource

Let me give an example

```<code>class Section
{
     public Bitmap Page {/* set and get the page...*/}
     Bitmap page = new Bitmap(600,800);
     Draw(SomeThing s)
     {
        // Draw something on the page
     }
}

// in somewhere else
foreach (Section sect in Sections)
{
     sect.Draw(something);
     Video.AddFrame(sect.Page);
}
</code>```

Versus.

```<code>class Section
{
     public Bitmap Page {/* set and get the page ...*/}
     Bitmap page; // it isn't instantiated in the class
     Draw(SomeThing s)
     {
        // Clear the page
        // Draw something on it
     }
}

// in somewhere else
Bitmap screen = new Bitmap(600,800); // the common resource
foreach (Section sect in Sections)
{
     sect.Page = screen;
     sect.Draw(something); // it clears it before drawing something on it
     Video.AddFrame(screen);
}
</code>```

Versus.

```<code>class Section
{
     public Bitmap Page {/* set and get the page ...*/}
     static Bitmap page = new Bitmap(600,800);
     Draw(SomeThing s)
     {
        // First Clear the page
        // Draw something on the page
     }
}

// and in somewhere else
foreach (Section sect in Sections)
{
     sect.Draw(something);
     Video.AddFrame(sect.Page);
}
</code>```

First I used the first approach but it consumed a lot of memory as each object in my Sections array had a bitmap. Then I was forced to try the second approach. Is this new approach better than the former on all aspects? What is the guideline for such situations?

答:
If there is no individual bitmap for each section, it is probably not worthwhile to even have a member in Section pointing to the bitmap. You could instead pass the bitmap as a parameter to the Draw method:

```<code>class Section
{
     Draw(Bitmap page, SomeThing s)
     {
        // Draw something on the page
     }
}

// somewhere else

Bitmap screen = new Bitmap(600,800); // the common resource

foreach (Section sect in Sections)
{
     sect.Draw(screen, something);
     Video.AddFrame(screen);
}
</code>```

That way it is more flexible and you could decide to create new resource whereever you are using the section method(s).

Body:
If there is no individual bitmap for each section, it is probably not worthwhile to even have a member in Section pointing to the bitmap. You could instead pass the bitmap as a parameter to the Draw method:

class Section
{
     Draw(Bitmap page, SomeThing s)
     {
        // Draw something on the page
     }
}

// somewhere else

Bitmap screen = new Bitmap(600,800); // the common resource

foreach (Section sect in Sections)
{
     sect.Draw(screen, something);
     Video.AddFrame(screen);
}

That way it is more flexible and you could decide to create new resource whereever you are using the section method(s).


问:
Lower and Upper bound of an algorithm
I am learning about analysis of algorithms.

I came across the term "upper bound" and "lower bound" in "worst-case" running time of an algorithm.

Are they applicable to only the "worst case" or can be used with other cases also ("average case " and "best case")?

答: They are applicable to all cases, but "worst case" is the one most programmers go by because it's just better to always assume the worst case (when designing algorithms).</p>

Title: Lower and Upper bound of an algorithm


问:
'import module' vs. 'from module import function'
I have always been using this method:

```<code>from sys import argv
</code>```

and use argv with just argv. But there is a convention of using this:

```<code>import sys
</code>```

and using the argv by sys.argv

The second method makes the code self documented and I (really) adhere to it. But the reason I prefer first method is it is fast because we are importing only the function that is needed rather than import the whole module (which contains more useless functions which python will waste time importing them). Note that I need just argv and all other functions from sys are useless to me.

So my questions are. Does the first method really makes the script fast? Which method is preferred most? Why?

答:
I recently asked this question to myself. I timed the different methods.

requests library

```<code>def r():
    import requests
    return 'hello'
timeit r() # output: 1000000 loops, best of 3: 1.55 µs per loop

def rg():
    from requests import get
    return 'hello'
timeit rg() # output: 100000 loops, best of 3: 2.53 µs per loop
</code>```

beautifulsoup library

```<code>def bs():
    import bs4
    return 'hello'
timeit bs() # output: 1000000 loops, best of 3: 1.53 µs per loop

def be():
    from bs4 import BeautifulSoup
    return 'hello'
timeit be() # output: 100000 loops, best of 3: 2.59 µs per loop
</code>```

json library

```<code>def js():
    import json
    return 'hello'
timeit js() # output: 1000000 loops, best of 3: 1.53 µs per loop

def jl():
    from json import loads
    return 'hello'
timeit jl() # output: 100000 loops, best of 3: 2.56 µs per loop
</code>```

sys library

```<code>def s():
    import sys
    return 'hello'
timeit s() # output: 1000000 loops, best of 3: 1.55 µs per loop

def ar():
    from sys import argv
    return 'hello'
timeit ar() # output: 100000 loops, best of 3: 2.87 µs per loop
</code>```

It seems to me that there is a slight difference in performance.

Body:
I recently asked this question to myself. I timed the different methods.

requests library

def r():
    import requests
    return 'hello'
timeit r() # output: 1000000 loops, best of 3: 1.55 µs per loop

def rg():
    from requests import get
    return 'hello'
timeit rg() # output: 100000 loops, best of 3: 2.53 µs per loop

beautifulsoup library

def bs():
    import bs4
    return 'hello'
timeit bs() # output: 1000000 loops, best of 3: 1.53 µs per loop

def be():
    from bs4 import BeautifulSoup
    return 'hello'
timeit be() # output: 100000 loops, best of 3: 2.59 µs per loop

json library

def js():
    import json
    return 'hello'
timeit js() # output: 1000000 loops, best of 3: 1.53 µs per loop

def jl():
    from json import loads
    return 'hello'
timeit jl() # output: 100000 loops, best of 3: 2.56 µs per loop

sys library

def s():
    import sys
    return 'hello'
timeit s() # output: 1000000 loops, best of 3: 1.55 µs per loop

def ar():
    from sys import argv
    return 'hello'
timeit ar() # output: 100000 loops, best of 3: 2.87 µs per loop

It seems to me that there is a slight difference in performance.


问:
How do purely functional languages handle modularity?
I come from an object oriented background where I've learned that classes are or at least can be used to make a layer of abstraction that allows for easy recycling of code which can then either be used to make objects or be used in inheritance.

Like for example I can have an animal class and then from that inherit cats and dogs and such that all inherit many of the same traits, and from those sub-classes I can then make objects that can specify a breed of animal or even the name of it.

Or I can use classes to specify multiple instances of the same code that handles or contains slightly different things; like nodes in a search-tree or multiple different database connections and what not.

I'm recently moving into functional programming, so I was starting to wonder:

How do purely functional languages handle things like that? That is, languages without any concept of classes and objects.

答:
Actually, OO code is far less reusable, and that's by design.  The idea behind OOP is to restrict operations on particular pieces of data to certain privileged code that's either in the class or in the appropriate place in the inheritance hierarchy.  This limits the adverse effects of mutability.  If a data structure changes, there are only so many places in the code that can be responsible.

With immutability, you don't care who can operate on any given data structure, because no one can change your copy of the data.  This makes creating new functions to work on existing data structures much easier.  You just create the functions and group them into modules that seem appropriate from a domain point of view.  You don't have to worry about where to fit them into the inheritance hierarchy.

The other kind of code reuse is creating new data structures to work on existing functions.  This is handled in functional languages using features like generics and type classes.  For example, Haskell's Ord type class allows you to use the sort function on any type with an Ord instance.  Instances are easy to create if they don't already exist.

Take your Animal example, and consider implementing a feeding feature.  The straightforward OOP implementation is to maintain a collection of Animal objects, and loop through all of them, calling the feed method on each of them.

However, things get tricky when you get down to details.  An Animal object naturally knows what kind of food it eats, and how much it needs in order to feel full.  It does not naturally know where the food is kept and how much is available, so a FoodStore object has just become a dependency of every Animal, either as a field of the Animal object, or passed in as a parameter of the feed method.  Alternately, to keep the Animal class more cohesive, you might move feed(animal) to the FoodStore object, or you might create an abomination of a class called an AnimalFeeder or some such.

In FP, there is no inclination for the fields of an Animal to always stay grouped together, which has some interesting implications for reusability.  Say you have a list of Animal records, with fields like name, species, location, food type, food amount, etc.  You also have a list of FoodStore records with fields like location, food type, and food amount.

The first step in feeding might be to map each of those lists of records to lists of (food amount, food type) pairs, with negative numbers for the animals' amounts.  You can then create functions to do all sorts of things with these pairs, like sum the amounts of each type of food.  These functions don't belong perfectly to either an Animal or a FoodStore module, but are highly reusable by both.

You end up with a bunch of functions that do useful stuff with [(Num A, Eq B)] that are reusable and modular, but you have trouble figuring out where to put them or what to call them as a group.  The effect is that FP modules are more difficult to classify, but the classification is much less important.


答:
In general, the lower bound is the best case (least amount of work performed) and the upper bound is the worst case (most work the algorithm will have to do). Average case is a probabilistic calculation between upper and lower bounds (the result is not necessarily somewhere in the middle, as sometimes the lower bound is potentially rare - or when probability is not simple to establish).

These bounds are useful generally because they narrow down your consideration to "nothing I do can make this particular algorithm perform better than this bound or worse than this one".  So yes, they will be a recurring theme in your future endevours to understand algorithms.

I'd only note that many programs stop dead at worst-case considerations, with just a minor salting of discussion of average-case. It doesn't mean that's all people in various fields consider - it's just all most lower level education classes/books bother to discuss.

Title: Lower and Upper bound of an algorithm

Body:
I am learning about analysis of algorithms.

I came across the term "upper bound" and "lower bound" in "worst-case" running time of an algorithm.

Are they applicable to only the "worst case" or can be used with other cases also ("average case " and "best case")?


问:
OOP best practice: Optional referenced-type parameter
I know in C#, by default, referenced type variables are passed by reference to a method. I have a function which sometimes I just need its return value and sometimes both return value and the changes on the input object are needed outside of the function.

Then I wrote it as the following:

```<code>public static List<RuleSet> Load(XmlDocument dom= null)
{
   if (dom == null)
   {
       dom = new XmlDocument();
   }
   string path = ...
   dom.Load(path);
   List<RuleSet> ruleSets = new List<RuleSet>();
   // load the dom with file and read rules
   return ruleSets;
}
</code>```

Sometimes I need the dom outside of the function and I call it Load(dom) and when I just need the return value I call it as Load().

However, I know if the parameter was declared with ref I couldn't set a default value for it and there must be a reason for if, but now I did it with no problem.

Then I suspect my approach to the problem (my function above) is correct or not, or could it be implemented in a better way?

答:
I know in C#, by default, objects are passed by reference

That's not correct. In C#, parameters are passed by value by default. But if that parameter is a reference type (like a class or interface), then it's the reference that's passed by value, not the object itself. If you actually want to pass by reference, you need to use ref, out or in.

However I know if the parameter was declared with ref I couldn't set a default value for it and there must be a reason for that

Yes, if a parameter is ref, then it basically means that it's an alias to a variable (local variable, field or array element). And a default value like null is not a variable.

What you're looking for could be solved by using two overloads instead of default value:

```<code>public static List<RuleSet> Load(out XmlDocument dom)
{
    dom = new XmlDocument();
    // rest of the method here
}

public static List<RuleSet> Load()
{
    XmlDocument ignored;
    return Load(out ignored);
}
</code>```

Using ref XmlDocument dom and checking for null would also work, but I think it's less clear. The only advantage I can see with that is that it allows you to reuse the XmlDocument. But that's almost certainly a premature optimization.

ContentLicense: CC BY-SA 4.0

Body:
I know in C#, by default, objects are passed by reference

That's not correct. In C#, parameters are passed by value by default. But if that parameter is a reference type (like a class or interface), then it's the reference that's passed by value, not the object itself. If you actually want to pass by reference, you need to use ref, out or in.

However I know if the parameter was declared with ref I couldn't set a default value for it and there must be a reason for that

Yes, if a parameter is ref, then it basically means that it's an alias to a variable (local variable, field or array element). And a default value like null is not a variable.

What you're looking for could be solved by using two overloads instead of default value:

public static List<RuleSet> Load(out XmlDocument dom)
{
    dom = new XmlDocument();
    // rest of the method here
}

public static List<RuleSet> Load()
{
    XmlDocument ignored;
    return Load(out ignored);
}

Using ref XmlDocument dom and checking for null would also work, but I think it's less clear. The only advantage I can see with that is that it allows you to reuse the XmlDocument. But that's almost certainly a premature optimization.


问:
Which code should I use to inform me of updates on websites?
I don't know if this is the correct place to ask this question but I'll ask it here anyway.

I don't know much about programming (I only ever learned MATLAB!) but I wonder if there is a simple way to write a code to check a website every x minutes, say, to see if a certain thing changes and notify me when it does?

I hope this question is not too vague!

Thanks in advance for your help.

EDIT: I have computer running Windows and one running Ubuntu.

答:
If you know MATLAB, then this should be the right way for you:

Get the page: webread

Search the page for the HTML pattern containing your data: strfind

Extract the data

Compare the data to the last one recorded

If changed, notify, else wait until timer runs out and re-run the process


问:
How to encourage to write pure functions in imperative environments?
In functional programming languages, a developer is always cautious about writing pure functions. Some functional languages simply do not allow impure functions, and some other functional languages force to to state explicitly when something impure is happening. But in imperative languages, there is usually no such distinction between pure and impure codes/functions. And impure fictions are quite the norm in imperative languages. But someone can still write pure functions in imperative languages and that is good for code health in my opinion.

So, how can we convince or encourage others to write pure functions within an imperative languages?

答:
The only good way to encourage others to work a certain way, is to demonstrate the benefits of doing so. You could also force people to work a certain way, via code reviews, but if the people don't understand the benefits, you will simply make everyone unhappy.

Pure functions have plenty of benefits even in imperative languages:

Predictability: with the same input, you always have the same output.

Isolation: invoking a pure function cannot modify program behavior, since the function has no side effects. Explaining the benefits of isolation is probably a big task in itself...

Easier to debug, in part due to the above attributes.

Less places to debug: most bugs occur when the program state is modified.

This is off the top of my head, so I hope others will chime in.


答: If you treat this problem as a workflow problem, you can employ one of the workflow tools such as jBPM or Windows Workflow Foundation and focus on the business logic and let the tool handle the persistence for you.</p>


问:
How to randomly get or find a sub-tree (include root node) from a given tree that contains n leaf-nodes
I have a non-binary tree.
I want to randomly find a "sub-trees" that are connected from root to leaf which must have at least n leaf-nodes (leaf-nodes of sub-tree must be leaf-nodes of given tree).
For example: given a tree as below

```<code>           *A
          /   \
         B     C
        / \     \
      *E   D    *F
            \
             *J
</code>```

n = 2

A random sub-tree that contains 2 leaf-nodes could be:

```<code>            A
          /   \
         B     C
        /       \
       E         F
</code>```

or

```<code>            A
          /   \
         B     C
          \     \
           D     F
            \
             J
</code>```

or

```<code>            A
          /
         B
        / \
       E   D
            \
             J
</code>```

I don't need to get all possible sub-trees, just randomly get it with a given tree and n

答:
Randomly choose n leaf nodes. Return the tree that contains those leaf nodes and their ancestors.

Whenever you say "random", you should specify what probability distribution you want. Unless you nail down the probability distribution, any of these "solutions" will meet your needs:

Always pick the leftmost n leaves.

Always pick the rightmost n leaves.

Always pick the middle n leaves.

Visit all the leaves in some desired order. Put each one at the end of a list. If the list now contains more than n nodes, delete one of them at random.

Make a list of all the leaves, and then repeatedly discard one at random until there are only n left.

Choose a random number from 1 to 5. Use the corresponding method from the above list.

Notice that the word "random" appears several times in the above. Each of these instances of "random" should be qualified by its probability distribution.

But especially notice that randomly picking n items from a larger set is a completely separate sub-problem that has nothing to do with trees. First you solve that, then you come back to the trees domain to build the tree that has your chosen leaf nodes.

Try solving each of these sub-problems. I think you'll find that neither is terribly difficult, once you put the other sub-problem temporarily out of your mind.

Otherwise, if you're trying to build the tree while you're picking the leaves to go into it, you're going to drive yourself mad.


问:
Controlling concurrency by Thread Pool in scenario of CPU-intensive and I/O intensive requests?
My goal is to understand how to handle jobs coming from the client side at very high frequency each job is either CPU-intensive or I/O intensive, but both types of jobs are continuously arriving at my server.

For example if ThreadPoolExecutor is configured with 50 threads and request frequency is 100 req/sec and each CPU-intensive job takes 2sec to complete and each I/O job is querying a data base and with 100 req/second first 50 jobs are CPU-intensive and remaining are mixture of CPU & I/O intensive. Then in this scenario all 50 threads woulb be busy executing CPU-intensive jobs and remaining clients have to wait a lot. I want to solve this problem by using distributed thread pool executor so that CPU-intensive jobs take advantage of cluster and go to other nodes for execution and my server just execute I/O intensive jobs. how can I design a server in this scenario?

答:
In general, there is very little you can do anything with the information you gather from a task. The reason is this: once you start a task, you have to let it finish, no matter what.

You can't suspend it forever. Some other parts of code might be depending on the task result; suspending it means the dependent code will be waiting forever.

You can't kill it. The task might have been making changes in the file system or database; it might leave the system or datastore in an inconsistent state if you kill the task in the middle of execution.

For the same reason you can't kill it, you can't even move an executing task to another computer, or even to another thread.

Basically, once a task is executing, you have to let it finish, no matter much much resource it takes.

The only other remedial action you can take is that if you see a significant number of worker threads are:

Executing on some tasks;

Are not consuming much CPU time due to them being in the blocked state (by blocking I/O)

Then you can perhaps spin up a few more worker threads and give them new tasks, in the hope that some of them are CPU intensive and therefore recoup the "wasted CPU cycles".

Some thread pool designs simply try to opportunistically increase the number of worker threads (and have them run concurrently on tasks) until the point where they see diminishing marginal returns.

http://msdn.microsoft.com/en-us/magazine/ff960958.aspx

I can only give a rough overview of ideas. Feedback are welcome.

First, some definitions.

"CPU intensive" - tasks which, once started executing on a thread, will keep on executing on the CPU until finish, making continuous progress along the way, and rarely volunteer up for a "pause" (blocking operation).

"IO intensive" - tasks which, during its execution on a thread, tend to frequently request for a "pause" (entering sleep or a blocking operation) voluntarily. This "pause" typically occurs when the task is waiting for an external event to arrive, such as waiting for the data from reading a file, or from a database query.

"CPU wasting" - tasks which superficially look like the "CPU intensive" type, but is in fact "busy-spinning" - running a tight empty loop without making progress. It could be further divided down to "programming errors (software defect)" or "malicious workload".

Notice I use the word "volunteer" several times. This is because an operating system may preemptively suspend a thread as it context-switch to other threads so that the OS can support a total number of threads in far excess of the number of CPU cores. (Typical CPU cores are in single-digit or low ten's, but typical total number of threads in OS is in the hundreds to thousands.)

It is possible for a watchdog thread (with suitable administrative privileges) to query the OS for the CPU time consumed by each worker thread. This can be used to decide how "CPU intensive" a particular running task is.

Also, it might be possible for the watchdog thread to wake up with a regular interval to check what each worker thread is up to. If one wants to find out whether a thread spends most of its time being blocked (waiting for I/O), one will need to drill down to the reason of the thread's reason of suspension. As described before, one has to distinguish between threads that are preempted by the OS, versus threads that are blocked by its own IO operations. This is doable on some OSes, but may be impossible on some other OSes.

I have never used any of these techniques so I don't know how feasible or effective these techniques are.


问:
How are financial organizations planning for the degradation of old programming languages such as COBOL?
I know that some financial organizations still use "dead" languages such as COBOL. I'm wondering what will happen in the future, when almost no one will program in those languages, and the maintenance of their systems will be a nightmare because there won't be any resources. (This is what I think will happen).

Are these enterprises currently building modern versions of their systems? If not, how will they deal with this?

答:
Funny you should ask that.

My father is a COBOL programmer. Graduated from college, got a job at a large insurance firm. Worked on the same app for his entire career (the same physical mainframe for 30 odd years of that). Spent the last 4 years remotely teaching a team of new graduates in India how to write COBOL. He retired last year.

I wouldn't count that as a "plan" or even a good business decision in the long term. But corporations aren't renowned for making good long term technical decisions.


问:
Do you memorize the classes of your frameworks?
I've been an iOS developer for about 5 years. There are still things that NSString and NSArray do that I didn't know they could. All optional attributes and behaviors that I haven't seen, because I've never actually sat down and memorized all of the optionals and capabilities of the NSString class. I know the most of the basic stuff off the top of my head, but I typically google for the fastest way to do it if I can't on the fly. My question is: Do you guys have most of the classes of your most commonly used frameworks memorized? (For me it would be Cocoa Touch). Does this have any effect on the classification of "level" of programmer you are? Technically I'm a senior and do well at my job but the above makes me wonder... Who's on the same page, if anyone?</p>

答:
Yes, I can tell you off the top of my head probably 80% of the operations are on the things I use day to day. After all, how can you plan to build something if you don't know what your tools are?

Though to be fair, I have an exceptional memory.


答:
I'm a C#/.Net developper.

.Net is HUGE so I don't beat myself up for not knowing all of it inside and out.  That said, because I've been focusing on mostly desktop/server business apps for the past few years I'm very familiar with the subset of .Net that solves my typical problems.  That's not to say that I don't occasionally discover new gems here and there.

I do periodically find new things (or even old things) that are useful but that I never knew about.  For me, these new gems are usually found by reading other people's code -- not by pouring through API documentation.

Other people's code has been my main resource for learning for a long time.  Eventually, when I had built up an acceptably solid foundation, I threw myself at crazy blogs like Eric Lippert's.

In my experience, this approach isn't uncommon.  About 80% of the devs I meet on the job don't care about progressing as a developper.  The other 20% seem to have approaches more or less identical to mine.  And yes, that involves googling anything I don't know off the top of my head.

I wouldn't worry too much if I were you.  The mere fact that you think about these things enough to ask a question on here is a very good sign.

Title: Do you memorize the classes of your frameworks?

Body: I've been an iOS developer for about 5 years. There are still things that NSString and NSArray do that I didn't know they could. All optional attributes and behaviors that I haven't seen, because I've never actually sat down and memorized all of the optionals and capabilities of the NSString class. I know the most of the basic stuff off the top of my head, but I typically google for the fastest way to do it if I can't on the fly. My question is: Do you guys have most of the classes of your most commonly used frameworks memorized? (For me it would be Cocoa Touch). Does this have any effect on the classification of "level" of programmer you are? Technically I'm a senior and do well at my job but the above makes me wonder... Who's on the same page, if anyone?</p>


答:
I think Joshua is heading down the right path, just not to it's conclusion.

Suppose you have an architecture where there are three threads that are written to do as much as they can--when they finish what they are doing they do it again.  To keep performance up these threads do not release control for anything--they don't want to risk the lag from the Windows task scheduler.  So long as there are 4 or more cores this works fine, it fails badly if there aren't.

In general this would be bad programming but games are another matter--when you're faced with a choice between a design that's inferior on all hardware or a design that is superior on sufficiently good hardware or a failure on inferior hardware game developers usually choose to require the hardware.


答:
Most of my knowledge is in the world, as opposed to in my mind.

I mostly do .net. Unlike Telastyn, I probably only have about 20% of what I use regularly memorized. The rest, I know just barely enough that intellisense can pull up the rest that I need, which is just as fast as had I memorized it. That knowledge is what's in the world.

Other times, working on python for example, a greater percentage is memorized, however, that comprises much less than what's immediately accessible in C#. That knowledge is in my mind.

In neither case do I expend any effort commuting things to rote memory. All of it is memorized through osmosis, simply by using it repeatedly enough.


答:
Programmers aren't the limiting resource.  Learning a new language is easy compared to all the domain and program-specific stuff you have to learn when starting at a new company, and people move to new companies all the time.  You're talking a language with 300 keywords versus a program with hundreds of thousands of functions.  It's not even close.

Hardware eventually wears out, and if there comes a point when they can't get compiler support for the new hardware, they will migrate to a new language then.  These types of hardware migrations are usually planned several years in advance.  I once worked on a major rewrite prompted by a dot matrix ribbon supplier going out of business.  My company bought out a bunch of the ribbon stock to buy us enough time for the migration.  "Luckily" the old language had sufficient support to move to the new hardware at the time, but the rewrite was extensive enough that we may as well have moved to a new language.

Title: How are financial organizations planning for the degradation of old programming languages such as COBOL?

Body:
I know that some financial organizations still use "dead" languages such as COBOL. I'm wondering what will happen in the future, when almost no one will program in those languages, and the maintenance of their systems will be a nightmare because there won't be any resources. (This is what I think will happen).

Are these enterprises currently building modern versions of their systems? If not, how will they deal with this?


答:
I've been programming professionally for 20 years, and I am still amazed at how much I don't know. My strongest languages are C++ and Perl, and I know the basics of the language extremely well, but then there's STL and Boost which I have to look up all the time, and all of CPAN which I peruse occasionally and am always happy to find someone else has already made a module for the code I'd been writing for a week.

Languages, APIs, and Other People's Code (OPC) are always coming and going, evolving and dying. My 20 years experience would amount to nothing if all I could do was spout with 100% accuracy the properties and methods of this year's popular twist on development. Learn how to program beautifully with the tools you have, and don't worry if you don't know everything about every class. Odds are some new API will be introduced eventually that makes obsolete the API you've spent thousands of hours learning and mastering.

Windows used to be a C-based API, so I learned Windows API, then Win32. Then it went to C++ in Borland so I learned OWL. That didn't turn out well, so I learned MFC. Had to learn OLE then OLE2 then COM then .NET. What's next? Apple is turning to Swift. I don't know anything about it, so this might be inaccurate: Time to abandon your Cocoa Touch?

I'm sure any old timer has similar stories. Whatever. Learn to program, use all you know of the available APIs, embrace new knowledge as you find it, but don't kill yourself learning every last piece of it, 'cause it might not be relevant in a few years anyway.


问:
How does a developer code in anticipation of change?
I ask this question based on the fact that currently my environment is under constant change due to the type of work we do. We do not always work on a project bases we often have smaller changes that just go through a quick change request process to trigger a developer to make the necessary changes. But my question relates more to the actual programming and anticipating that this program will be enhanced and modified in the future and often not by the same developer.

Is there a strategy or a way programmers themselves can ensure there program can stand up to quick effortless modifications? Right now I am trying to ensure any developments are broken down into core function processes and function modules or objects are created to perform each process. This means that the final program is actually more a list of function calls for the various process steps. This ensures you can easily change processes called or function calls to enhance or change the code easily. This helps especially when additional validations and checks need to be built in in-between process steps .

This has its downfalls though, so this leads me to the question "How does a developer code in anticipation of change?", how would you code ensuring the next person can easily and quickly change or enhance your code?

答:
Two very essential things to understand are that:

You can never anticipate every change a customer may ask. I had a customer who decided to switch a two months project from PHP to ASP.NET one week before release and was convinced that this would be an easy change.

Any change will have a cost. It doesn't matter if you are using Agile or if you have clean and extensible design, the cost will still be there.

Having said that, there are multiple techniques which lead to less expensive changes. As already noted by Jörg W Mittag, this is pretty much just "good design", but if you want something more specific, here are some hints:

Avoid code duplication at all costs. Having to make a change in this class, then in this one, and finally there—be careful, the code there is not exactly the same as in the first two locations—would increase the cost.

The major problem here is that on large projects, developers cannot possibly know which pieces were already written and may develop their own in a different location in the code base. Clean architecture and proper documentation helps, but doesn't make the problem disappear.

Your system should be decoupled as much as possible. If a small change in the module which displays generated invoices on the screen requires to rewrite a few classes in the module which handles registration of new users, there is something wrong with decoupling.

Decoupling may be done through interfaces. This means that you can work on the underlying logic while the interface remains the same and other parts of the system are unaffected. For example, you may have an interface for a logger component; when your customer asks to switch from syslog to a RabbitMQ-based message queue, your changes are constrained to the class which implements the given interface, and the users of this interface don't really care where the logs go.

The major problem here is that interfaces may be leaky. For example, you move from syslog to RabbitMQ, and then notice a new type of exceptions when message queue service is unreachable. Classes using the logging interface should now handle this additional exception.

Additional decoupling may be achieved with Dependency injection. The benefit is that instead of working on the class itself, you create a new one, test it separately, and when ready, swap the old class with the new one, while keeping the possibility to go back to the old class seamlessly (either by modifying a single line of code, or through configuration).

The cons is that this requires more work. If used too much (every part of the application is injected), the design may become too complicated.

Environment matters as much as design. Some hints:

Your code should be maintainable. Spaghetti code is problematic not by itself, but specifically because it makes it difficult to modify the code base.

Uniform style matters, because it makes the code easier to read; difficult to read code is difficult to change as well.

Automated regression testing makes maintenance less stressful. A year ago I worked in a company where the key product had 0% code coverage. When somebody had to make a change, he made it, and then if something bad happened in production, the programmer was the culprit. This is an excellent example of how not to anticipate changes.

The problem is that tests should be maintained as well, so instead of simply changing code, one should change tests and code. The time spent changing tests is nothing compared to the time wasted because of the lack of proper testing, but inexperienced project managers may not understand that.

All members of the team should be aware of all parts of the code base (of the product, not the whole company).


问:
PHP and performance
I always hear that PHP is for medium and small websites whereas .NET and Java for enterprise applications. My question is about PHP. Why is PHP not a good option for enterprise web applications? Is it because if the web application becomes bigger then PHP will be slower as it is an interpreted language?

I know that corporate world will choose .NET or J2EE because of the integration with their products and because of back end services, etc. However, if we just have PHP for building sites and web applications then how can we use it to perform well with big sites?

In short, Is there a relationship between the performance of PHP and the size of the website?
What are the factors that make PHP not appropriate option for big sites?

答:
There is not such thing as only enterprise web application. In the enterprise world you typically have a full enterprise system, where the web part is pretty small compared to the whole system. There are many systems underneath the web that perform batch processing, queue messaging, enterprise integration, gateways and many more. Event at the top there might not be only

While PHP is good for web is not that good for the other types of distributed systems. And since you do all these systems in Java or .NET it's easy to also build the web stack on the same platform rather than in PHP.

Title: PHP and performance


问:
Online code editors programming language
When searching for online code editors (out of interest how all the course sites such as Codecademy has been made) I noticed they are all been written in JavaScript.

Why are all those code editors such as CodeMirror or Ace written in JavaScript and not in PHP or Java for web applications?

答:
PHP or Java Servlets are purely server-sided technologies. They can't do anything unless the user clicks on a link. When you want any interactivity while the website is displayed, you need Javascript, at least for an AJAX call to a PHP/JSP backend.

When you want an application like a code editor the user is interacting with very closely, you want as much of it as possible to run on the user machine. This improves the user experience due to better responsiveness and means less strain on your server resources. When you want a rich application in a web browser, you can either use plugins like Flash, Silverlight or Java applets the user might or might not have installed, up-to-date and enabled. Or you can just use Javascript which - thanks to HTML5 - became almost as powerful as the plugin solutions.


问:
Does using == in JavaScript ever make sense?
In JavaScript, the Good Parts, Douglas Crockford wrote:

JavaScript has two sets of equality operators: === and !==, and their evil twins == and !=.  The good ones work the way you would expect.  If the two operands are of the same type and have the same value, then === produces true and !== produces false.  The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values.  The rules by which they do that are complicated and unmemorable.  These are some of the interesting cases:

```<code>'' == '0'           // false
0 == ''             // true
0 == '0'            // true

false == 'false'    // false
false == '0'        // true

false == undefined  // false
false == null       // false
null == undefined   // true

' \t\r\n ' == 0     // true
</code>```

The lack of transitivity is alarming.  My advice is to never use the evil twins.  Instead, always use === and !==.  All of the comparisons just shown produce false with the === operator.

Given this unequivocal observation, is there ever a time when using == might actually be appropriate?

答:
I ran across a pretty useful application today. If you want to compare padded numbers, like 01 to normal integers, == works just fine. For example:

```<code>'01' == 1 // true
'02' == 1 // false
</code>```

It saves you removing the 0 and converting to an integer.

Body:
I ran across a pretty useful application today. If you want to compare padded numbers, like 01 to normal integers, == works just fine. For example:

'01' == 1 // true
'02' == 1 // false

It saves you removing the 0 and converting to an integer.


问:
How to avoid old commits/PRs that are painful to merge?
We have a pretty good code review process, but there are couple of issues. The most annoying problem is merging. From time to time the number of stuff to merge grows rapidly and some of the commits are getting old. As a result, rebasing is a pain (conflicts).

What can we do to avoid it? I can think of two possible solutions:

Merging Thursday. From 10 to 12 every week we merge PRs

Have a timeout on PR. If it's in peer review for a week, spam everybody in the team.

答:
A somewhat excessive solution is to prevent your developers from committing, while they have reviews to do.

But as Bart writes, it is really an organisational issue, an you ought to solve it by reminding the developers that reviewing has a higher priority than writing new code.  Some developers might like to have a tool giving a "live" indication of how many reviews they have in their queue.

I don't know how you organise reviews in your group, but if you don't assign specific reviews to specific developers, that may lead to everybody hoping that somebody else will do the work.


答:
PHP and Java are processed Server-Side. JavaScript is processed by the web browser.
If you were processing in real-time with PHP, you would need a piece of JavaScript (such as an AJAX POST) to send the entire chunk of code to the server for re-processing, wait for a return and then replace the entire display with newly formatted text every time a user changes a single character.

JavaScript can do this all itself directly in the web browser and leverage the power of the User’s PC to accomplish the task.

Using JavaScript speeds up the process as nothing ends to be passed backwards and forwards all the time, it lowers bandwidth usage as nothing is passed to and fro and it also reduces the server workload for the same reason.

There is also an argument that it is more reliable and secure. If you are not passing anything back to a server, there is no need to worry as much about properly "escaping" special characters, less of a concern about an insecure piece of server-side code being exploited etc.

It is also good practise to not send anything back and put a load on your server unless there is a good reason to do so.

While you could have a button on your "code editor" which would send an entire chunk of script back to a server for processing and send back a formatted return, it's generally not the done thing for the reason stated above. Also, a "reformat all" button wouldn't add much value as you can simply reformat everything in JavaScript.

Title: Online code editors programming language

Body:
When searching for online code editors (out of interest how all the course sites such as Codecademy has been made) I noticed they are all been written in JavaScript.

Why are all those code editors such as CodeMirror or Ace written in JavaScript and not in PHP or Java for web applications?


答:
How are organisations planning to replace dead languages?
In ways that are likely business secrets.

When I was last working for a bank they were facing 3 issues:

They had hired a new Core system programmer once in the last 20 years an all others were retiring

The language was more dead than Cobol.

The core system was on a mainframe, which are incredibly expensive to run, compared to say a cluster of a few dozen commodity machines.

I was now privy to the actual business plan to handle it (so I can't break the aforementioned business secrets).
My team did come up with a plan to solve it, and implemented most of it.

We built a tool which would translate from the aforementioned dead language,
into Java.
The tool we made generated fairly awful java. Mostly because work around for not having GOTOs, and also for the existence of Reentrant Functions, and a few other quirks that have not made it into modern languages.
The java it made was awful, but now it could be replaced module by module as new/changed functionality was required.
The translation tool was written in C#, making use of ANTLR.

The other side of the tool was in Java having implementations most of  the operators and datatypes from the dead language.
Which included having to implement a virtual memory layer since Unions and pointers were sometimes used.

After we were mostly done, we cam to the conclusion it would have been better to not translate into Java, but into C++, because C++ still has alot of those features that haven't made it into modern languages.
Java was originally chosen because of the cheapness of Java VMs in some particular cluster product/architecture.

When I left this was mostly done. It would compile/translate any sample code you could find for the dead language. What it couldn't do was the database layer. Which was handled with 3 layers of preprocessing on the source code in the dead language, and which needed to be replace with remote db connections instead of what ever interface you use on a mainframe to access the database on it.

I left convinced it would work. But I have no idea if it has been checked back out of version control since I stopped working on it. It could be they are using it today.

I suspect in the long term (which many now be the past), the larger banking group company that bought them 6 months before I started will gut there IT systems entirely, and force them to just use a thin shell infront of the parent companies systems.


答:
As MainMa already said: The changes that actual happen are unpredictable. (in almost all cases)

Keep the implementation as simple as possible from the technically POV and as close as possible to the actual requirements (i.e. use speaking identifiers), or, in other words

The best you can do for future changes is help understanding how your solution works.

Body:
I ask this question based on the fact that currently my environment is under constant change due to the type of work we do. We do not always work on a project bases we often have smaller changes that just go through a quick change request process to trigger a developer to make the necessary changes. But my question relates more to the actual programming and anticipating that this program will be enhanced and modified in the future and often not by the same developer.

Is there a strategy or a way programmers themselves can ensure there program can stand up to quick effortless modifications? Right now I am trying to ensure any developments are broken down into core function processes and function modules or objects are created to perform each process. This means that the final program is actually more a list of function calls for the various process steps. This ensures you can easily change processes called or function calls to enhance or change the code easily. This helps especially when additional validations and checks need to be built in in-between process steps .

This has its downfalls though, so this leads me to the question "How does a developer code in anticipation of change?", how would you code ensuring the next person can easily and quickly change or enhance your code?


问:
TDD and Writing Tests for File Processing
These questions are very similar: TDD for batch processing: How to do it? and TDD: how to test file outputs? but I'm still left wondering some things

The app processes files/pdfs. It takes them from one FTP server, processes/reads file content, places them in a destination FTP server, depending on content read.

How can I approach file processing in a TDD way? My formal education on TDD was not the best, so even "obvious" suggestions will help me.

My ideas:

The "success" of my app depends on if it reads/processes a source file properly, and also if it places it in the proper destination. So it makes sense to me to write tests for seeing if a file

is "read" correctly

ends up in the right place

I could test on specific dummy files, but does this mean I should always ship these specific files with the code, for the tests to pass? The file's name will be different after being processed, so I'm not sure how to test if it ends up in the right place -- should I eyeball the files and then hardcode expected results into the tests?

答:
It's important to understand TDD as less about the end result - tested code, and more about the journey in getting there. TDD is a design process that encourages all the code qualities I'm sure you have learned about: reusability, separation of concerns, readability, maintainability, etc. Oh, and of course, correctness. Your tests drive you towards writing isolatable, testable code, which in turn drives these qualities. If you only understand TDD as about testing, you are in for a hard time.

Maybe you have already written this program (or maybe you have at least designed it in your head), and you are thinking about how to wrap tests around it. If this is true then start again as you are not driving the code from the tests: you're not doing TDD.

Writing your tests first, then making each pass, might lead you towards the following units:

A thing that opens a file and exposes a byte stream

A thing that parses a byte stream as a PDF

A thing that takes the representation of a PDF and extracts what you're interested in

A thing that groups by something extracted previously

A thing that allocates a file location based on group

A thing that moves a file to a location

(I may not have understood your program correctly, but you get the gist.)

Hopefully you can see that you've broken it down into easily testable units, many of which can be reused, most of which are agnostic of files, or PDFs. Those units that actually deal with real file operations are very simple and are few and far between, so testing them is not difficult; everything else deals with some in-memory representation.

It's not easy to get right at first, but the golden rule is to remember that if your tests becomes complicated, it's probably a clue that your code has become too complicated and is trying to do too much in one unit. It's your cue to take a step back and keep asking questions like these.


答:
Concentrate on the problem-to-class path.

Prefer grasping concepts and patterns over memorizing class details, because these first are much more portable than the latter. Sure you'll know the details of classes you use on a daily basis, but you'll not be able to keep track of all classes of your (future) framework(s).

A house may be defined as consisting of bricks (or even atoms), but this doesn't help much when building one. Classes are of course much more abstract than single lines of code, but memorizing all its facets is overhead, just memorize key classes.

Most probably, your memory for plain facts will decrease with increasing age, whereas your ability to integrate knowledge (if well-developed) will stay some longer.


答:
It is naive to assume that more modern languages are somehow preferable to older ones for a given problem domain.

Beyond the issues of risk and the immense cost associated with such modernization is the fact that many legacy environments have their own ecosystem, and have evolved symbiotically with it.  These solutions are often the best elixir for a problem they actually contributed to.

IBM is not giving up on DB2/RPG any time soon.  COBOL flat files are not going extinct in our lifetimes.  This is a fact.  And even if we could remove (insert demonized technology here) we would have to address a problem domain for which we may not have the correct tools.

Is it simpler to do a minor amount of maintenance on a system that works or reengineer something we likely do not fully understand?

For better or worse COBOL, VB6, RPG and the like are no where near dead.


答:
I'm not advocating this as the answer, but this is a list of techniques that has helped me greatly over the past 20 or so years.

Self documenting code

You might be tempted to add comments everywhere to make the code easier to understand. But the simple fact is that a lot of the time, if extensive comments are required within a method, the code isn't simple enough and should be refactored.

KISS

Modules or methods that do a lot are a code smell and should be refactored as far as possible to make them easier to read, understand and modify.

YAGNI

With change in mind it is tempting to add a whole raft of bells and whistles which might save time later. Resist the urge to do this. Extra code requires testing, can contain bugs and makes your code base larger.

Path of least resistance

If you are faced with a change and the customer doesn't know exactly what they want, code the knowns and either wireframe or mock the rest. The last thing you want to be doing is say, coding 4 options when only 1 is ever going to be kept.

Mock

As mentioned above, if you can mock data or screens rather than code them, this will save time.

Slay sacred cows

Any developer who has been round a while has worked on systems where there is a business critical complex piece of code that nobody wants to touch under pain of death! If the code is changed, it often breaks, behaves in unanticipated ways or technical debt is added to code round it.

Firstly, make sure you aren't creating any sacred cows. Secondly, if any exist, break them down so they're more easily digestible. Automated unit tests can assist greatly here.

Title: How does a developer code in anticipation of change?


答:
There are plenty of methodologies given for making your code more adaptable and maintainable, but most of them have the following points at their core:

Keep methods small: Your methods should only do one thing well.  If they try to do more than one thing they will almost certainly fail at doing at least one of those things under certain circumstances, and they definitely will grow very complex warty internal logic.  For this reason you should also keep the number of arguments passed in to a minimum.  As well as making the code simpler to comprehend (and therefore adapt), it also makes reuse much easier.  If you've got a 500 line method with 10 lines of logic somewhere inside that you need to reuse in a different circumstance, then you have to either add a flag argument and wrap that logic (or the surrounding logic) in if statements, or copy and paste it somewhere else.  If that logic was all in its own 10 line method, you can just call it.

Keep your classes small and focused.  Just like with methods, classes should only do one thing well.  For example on our e-commerce platform the order class contains a lot of logic for order delivery.  We've recently needed to add functionality to allow a single order's components to ship from multiple locations (should some of the stock be at one location and the rest at a different one).  Because delivery was so tightly coupled with the concept of an order, eventually the only viable solution was to build convoluted logic to analyse the order and create multiple orders based on the original, each mapped to ship from the location with the stock, and kludge and work around the issues related to payment for that order, which are also embedded in the order class.  In short our order class tries to do too much and its responsibilities aren't clear.  If we had separate order payment classes and order delivery classes that payment and delivery were delegated to, the work would have been far simpler and would most likely simply been a matter of adapting the order class to support multiple deliveries per order.

In software, small is always beautiful, and if your code is too big to fit on your screen without scrolling then it's probably also too big to fit in your comprehension.


答:
Besides those facts mentioned already, I would like to add that at the heart of all major browsers there is a powerful EcmaScript/JavaScript engine, incredibly improved by years and years of evolution of the Web.

Therefore all major browsers natively speak this language and are capable of running programs in it in the most efficient way. Why in the world would you use something else to write a client-side program intended to run within one of these beasts?

Even when the browsers only speak this language is not like you are forced to use JavaScript to write your programs. Over the years several other languages have been created to try to replace or "improve" JavaScript. You could write your programs in one of these languages and then a compiler translates your code to JavaScript in order to run it in the browser, which is ultimately, what the browser understands.

Examples of these alternative languages are the now decadent GWT which translates Java to JavaScript. Other more modern approaches like CoffeeScript and more recently TypeScript. With the renewd popularity of functional programming these days we have seen the appearance of ClojureScript and Google has not gotten tired of failing trying to dethrone JavaScript and created the awesome Dart programming language for that matter (even one of their browsers named Dartium has a native engine for it). And believe me, Google is not done failing and as we talk they are already working in another one of these called AtScript. Finally, now thanks to the effort in Emscripten you can even port your programs from C/C++. All of these are languages that are ultimately complied to JavaScript in order to run the code in the browser.

So, as you can see it is not like you are forced to "write" your client-side programs in JavaScript, you are just forced to "run" them in JavaScript because that is what most browsers understand.


问:
What's so difficult about SVN merges?

Possible Duplicate:

  I’m a Subversion geek, why should I consider or not consider Mercurial or Git or any other DVCS?

Every once in a while, you hear someone saying that distributed version control (Git, HG) is inherently better than centralized version control (like SVN) because merging is difficult and painful in SVN.  The thing is, I've never had any trouble with merging in SVN, and since you only ever hear that claim being made by DVCS advocates, and not by actual SVN users, it tends to remind me of those obnoxious commercials on TV where they try to sell you something you don't need by having bumbling actors pretend that the thing you already have and works just fine is incredibly difficult to use.

And the use case that's invariably brought up is re-merging a branch, which again reminds me of those strawman product advertisements; if you know what you're doing, you shouldn't (and shouldn't ever have to) re-merge a branch in the first place.  (Of course it's difficult to do when you're doing something fundamentally wrong and silly!)

So, discounting the ridiculous strawman use case, what is there in SVN merging that is inherently more difficult than merging in a DVCS system?

答:
The internal data models are fundamentally different.

Fundamentally, in SVN, when you look at the history of a branch, you only see what has happened in that branch. So when you merge from branch B to branch A, the history of branch A will contain one large commit containing all the changes made explicitly to B since it was branched.

In the first versions of SVN, if you had to merge branch B into branch A once more, you had to manually specify which revision range of branch B you wanted to merge in order to avoid merging the same revisions twice. The clever developer would of course use a commit message like 'Merged in B:1234'.

SVN 1.5 "fixed" this. But it did not change how merges are applied fundamentally. It merely added some extra metadata to branch A, letting SVN know that revision 1234 had been merged in, allowing SVN to automatically choose the correct revision range.

But this solution is basically a workaround for a data model that fundamentally doesn't support track what has been merged.

Merging two branches is a relatively simple example. But imaging this more complex scenario

Create branch A from trunk, and make a few commits here

Create branch B from A and make a few commits here

Make a few commits in trunk and A

Merge B into trunk

Merge A into B

Merge A into trunk

Merge B into trunk (this shouldn't actually do anything)

Handling this correctly using the metadata model becomes extremely complex (I don't know if SVN does indeed handle this scenario correctly, and I don't feel inclined to test it out).

Handling this scenario in git is extremely simple.

In git, every time you commit, the internal object representing that commit contains a reference to the previous head. When you merge in a branch, the commit contains references to the previous head of all the branches being merged (you can merge more than one branch at a time in git)

Therefore, when you examine the history of a single commit in git, you can see all history, you can see when it was branched, when it was merged, and you can see the history of both branches between the branching and the merging.

Thus when merging in a branch that has been partly merged, it is extremely simple to determine what has been merged already, and what has not.

I have no experience with Mercurial, but I suspect that its internal workings are similar to git.

So fundamentally, for SVN, it was a design goal to make branching cheap. But in git, it was a design goal to make merging cheap.

Lastly, last time I used SVN, it was not able to handle merges, where a file was renamed in one branch, and modified in another.

Title: What's so difficult about SVN merges?


问:
Asp.net Web Form for Local Use
I am trying to build an Asp.Net Web Form project to use at my job. We work with a lot of excel files and a large task for me and others at my company is combining/comparing/updating data in these excel files. My goal is to build a webpage and use asp.net Fileupload to parse our excel files and in the code-behind format the results for us/along with other reports we might want to save time.

Here is my issue. At our workplace we don't have admin rights, so we can't use IIS. In addition opening the aspx pages with a web browser is giving us this message.

```<code>This page contains the following errors:
error on line 1 at column 2: StartTag: invalid element name
Below is a rendering of the page up to the first error.
</code>```

Is there a way to get a working web form project without iis or admin right? Kinda like a portable apps for your flash drive.

答:
The asp.net framework is implemented in such a way that it is not dependant on any particular web server: it can be easily embedded in any web server that uses the .net runtime (and, with a little more work, in just about any web server).

Robert Harvey has already linked to one such server in the comments above. You can also use Microsoft's Cassini, which is a little less advanced, but is likely yo be good enough for your duplication. It can be downloaded from https://code.google.com/p/cassini/

Title: Asp.net Web Form for Local Use


问:
How to make max-heap stable with counter and account for counter overflow?
Generally I need size-efficient data structure similar to std::priority_queue but stable (preserving order of insertion).

By adding just 4 bytes to the object I could have 1 byte serving as priority and 3 bytes as counter to keep the order of insertion. Closest competitor - std::forward_list - would add an overhead of 5 bytes (in practice 8 due to alignment) - one for priority and 4 for link (32-bit architecture). It would also be slower than max-heap due to traversal when adding new element.

The problem with counter is the behavior of container when the counter overflows. In Boost the counter is 64-bits long and when it overflows, the container throws an exception ( http://www.boost.org/doc/libs/1_49_0/doc/html/heap/concepts.html#heap.concepts.stability ).

One solution to this problem that comes to mind is resetting the counter whenever the queue gets empty, but this is only a partial solution to the problem.

Is there a generic (and optimal - without traversing through the whole heap each time) way this could be solved? I planned to use std::push_heap() and std::pop_heap() from <algorithm> with an array of raw storage, so I have access to "internals" of the entries. If possible, I'd prefer to stick to these standard functions instead of implementing my own heap (or any other data structure) that would be stable.

EDIT:

As requested in a comment, I'm adding some more information about my use case here.

I need this data structure to implement a message queue in the RTOS I'm developing ( https://github.com/DISTORTEC/distortos ). This message queue must follow all POSIX requirements, and one of them is "stability" - new entries with the same priority must be placed AFTER older entries:

From http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_send.html# :

A message shall be inserted after other messages in the queue, if any, with equal msg_prio.

This is an RTOS for embedded microcontrollers, so I cannot give any specific usage scenario, because there will be many. Some devices will run for a second and shutdown, other devices may run for years without reboot. As I'm aiming for a design with no limitations, I'm mostly interested in a solution that works in all cases, without limits like the one imposed by simple counter - which fails when the counter overflows.

Because of the fact that this is for a microcontroller, I'd really prefer the solution to be size efficient, so solutions like "use 128-bit counter" are not acceptable.

Generally I see two options - using singly-linked list or using max-heap (as I originally intended).

Using singly-linked list would add 5 (in practice 8 - due to alignment) bytes to each stored object - 1 for priority, 4 for link. This solution is stable "by design", but insertion of object can be slow when there are many objects in the list - because a lot of nodes will have to be traversed to find the spot for insertion.

Using heap I could try to limit the overhead to 4 bytes - 1 for priority, 3 for counter. This option could possibly be faster than the list, but requires a solution for counter overflow. I see several options here (multiple can be used at the same time):

use 56-bit counter (total overhead would be 8 bytes per object),

reset the counter when the list gets empty (only partial solution),

when the overflow happens, counter has to be reset, whole heap has to be traversed and each visited entry's counter has to be updated to "low" value.

Of course I know I could just ignore the overflow, but if there is a real solution, I'd like to implement it.

As there seems to be no simple, robust and deterministic solution to the counter overflow problem, I'm starting to lean towards the use of  - I would expect that there wouldn't be many entries in the message queue (most microcontrollers have really limited RAM), so the speed benefit of using heap would probably be negligible. Especially when I'd account for all these copying during insertion/deletion, while the contents of the singly-linked list would be mostly static in this regard. And there's also "instantaneous" extraction from the head of the list...

答:
I don't think there's any good way to fix your overflow problem with the constraints given.

Renumbering your heap periodically means a stop-the-world housekeeping operation, which I reckon needs O(n log n) time without extra allocation, or perhaps O(n) time with O(n) temporary space.

Note that if O(n log n) stop-the-world operations aren't acceptable - you did say it's an RTOS - then you need to make sure there's always room to allocate the temporary working space. If you're reserving that anyway, you could equally spend twice as much space overhead per object on a structure that doesn't need stop-the-world housekeeping operations in the first place.

The simplest solution I can think of is to amortize that overhead over multiple objects, rather than adding a pointer to each instance. So, let's split it into two issues:

FIFO-ordering of elements with the same priority

You can just use a FIFO-ordered container with constant-time pop_front and push_back: this is a deque.

You could try std::deque, and write a custom implementation if you need finer control over the block size, for example. The per-block overhead is amortized over all the objects in that block, so you can control the tradeoff between slack allocation and per-object overhead.

key-ordered container of priority levels:

Since 8 bits are enough to store your priority, an array of 256 levels (deques) would be the simplest solution.

If that wastes too much slack space for you, a regular priority queue will still work, but pop_front/pop_heap should pop from the front priority level object, and only pop that level from the overall queue when it is empty.

Note that you can write this logic as a thin wrapper around the existing pop_heap to re-use its heap logic, but the same isn't true of push_heap, and in fact that will end up slower in this scheme (you'll have to search for the insertion point to see if it exists, and then sift it up if it doesn't).

This should have notionally constant-time everything, so is asymptotically better than a heap; whether it's fast enough in practice will depend on the platform, cache behaviour, and how well you can tune the allocators, block sizes, etc.

Space overhead is a few bytes (something like 2-4 pointers) per distinct priority level, plus hopefully 1 byte or fewer per element (depending on the deque block size and number of elements per block). So, this will only save space if you have a sufficiently large number of elements per priority level. However, it can never encounter your overflow problem, doesn't require any stop-the-world cleanup, and doesn't degrade significantly with time or size.


答:
Languages, such as COBOL, don't require exotic hardware. The only resources that might run out are staff. But as long as there are money to be earned, people will learn necessary skills.

Some companies today are rewriting their systems into more fashionable languages - but hey, I had an offer last year from a bank which seemed to be rewriting from C++ to Java... So, to answer "how will they deal with this?" I guess for every imaginable approach there is at least one company putting it into action. From training new ranks of programmer-archeologists to rewriting everything over and over again.